diff --git a/src/utils/prosemirror/utils.ts b/src/utils/prosemirror/utils.ts index 5fa122ee..e43e63bd 100644 --- a/src/utils/prosemirror/utils.ts +++ b/src/utils/prosemirror/utils.ts @@ -77,7 +77,23 @@ export const getAttrValueInSelection = (view: EditorView, attr: string) => { return value } -export const getTextAttrs = (view: EditorView) => { +interface DefaultAttrs { + color?: string; + backcolor?: string; + fontsize?: string; + fontname?: string; + align?: string; +} +const _defaultAttrs: DefaultAttrs = { + color: '#000', + backcolor: '#000', + fontsize: '20px', + fontname: '微软雅黑', + align: 'left', +} +export const getTextAttrs = (view: EditorView, defaultAttrs: DefaultAttrs = {}) => { + defaultAttrs = { ..._defaultAttrs, ...defaultAttrs } + const marks = getMarkAttrs(view) const isBold = isActiveMark(marks, 'strong') @@ -87,11 +103,11 @@ export const getTextAttrs = (view: EditorView) => { const isSuperscript = isActiveMark(marks, 'superscript') const isSubscript = isActiveMark(marks, 'subscript') const isCode = isActiveMark(marks, 'code') - const color = getAttrValue(marks, 'forecolor', 'color') || '#000' - const backcolor = getAttrValue(marks, 'backcolor', 'backcolor') || '#000' - const fontsize = getAttrValue(marks, 'fontsize', 'fontsize') || '20px' - const fontname = getAttrValue(marks, 'fontname', 'fontname') || '微软雅黑' - const align = getAttrValueInSelection(view, 'align') || 'left' + const color = getAttrValue(marks, 'forecolor', 'color') || defaultAttrs.color + const backcolor = getAttrValue(marks, 'backcolor', 'backcolor') || defaultAttrs.backcolor + const fontsize = getAttrValue(marks, 'fontsize', 'fontsize') || defaultAttrs.fontsize + const fontname = getAttrValue(marks, 'fontname', 'fontname') || defaultAttrs.fontname + const align = getAttrValueInSelection(view, 'align') || defaultAttrs.align const isBulletList = isActiveOfParentNodeType('bullet_list', view.state) const isOrderedList = isActiveOfParentNodeType('ordered_list', view.state) const isBlockquote = isActiveOfParentNodeType('blockquote', view.state) diff --git a/src/views/components/element/TextElement/index.vue b/src/views/components/element/TextElement/index.vue index 77f07ff0..0dd4f28c 100644 --- a/src/views/components/element/TextElement/index.vue +++ b/src/views/components/element/TextElement/index.vue @@ -173,7 +173,10 @@ export default defineComponent({ } const handleClick = debounce(function() { - const attrs = getTextAttrs(editorView) + const attrs = getTextAttrs(editorView, { + color: props.elementInfo.defaultColor, + fontname: props.elementInfo.defaultFontName, + }) store.commit(MutationTypes.SET_RICHTEXT_ATTRS, attrs) }, 30, { trailing: true })