fix: 处理 prosemirror 无法获取到文字继承的样式(#38)

This commit is contained in:
pipipi-pikachu 2021-06-22 11:12:01 +08:00
parent c23d9c32aa
commit 1501203a39
2 changed files with 26 additions and 7 deletions

View File

@ -77,7 +77,23 @@ export const getAttrValueInSelection = (view: EditorView, attr: string) => {
return value 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 marks = getMarkAttrs(view)
const isBold = isActiveMark(marks, 'strong') const isBold = isActiveMark(marks, 'strong')
@ -87,11 +103,11 @@ export const getTextAttrs = (view: EditorView) => {
const isSuperscript = isActiveMark(marks, 'superscript') const isSuperscript = isActiveMark(marks, 'superscript')
const isSubscript = isActiveMark(marks, 'subscript') const isSubscript = isActiveMark(marks, 'subscript')
const isCode = isActiveMark(marks, 'code') const isCode = isActiveMark(marks, 'code')
const color = getAttrValue(marks, 'forecolor', 'color') || '#000' const color = getAttrValue(marks, 'forecolor', 'color') || defaultAttrs.color
const backcolor = getAttrValue(marks, 'backcolor', 'backcolor') || '#000' const backcolor = getAttrValue(marks, 'backcolor', 'backcolor') || defaultAttrs.backcolor
const fontsize = getAttrValue(marks, 'fontsize', 'fontsize') || '20px' const fontsize = getAttrValue(marks, 'fontsize', 'fontsize') || defaultAttrs.fontsize
const fontname = getAttrValue(marks, 'fontname', 'fontname') || '微软雅黑' const fontname = getAttrValue(marks, 'fontname', 'fontname') || defaultAttrs.fontname
const align = getAttrValueInSelection(view, 'align') || 'left' const align = getAttrValueInSelection(view, 'align') || defaultAttrs.align
const isBulletList = isActiveOfParentNodeType('bullet_list', view.state) const isBulletList = isActiveOfParentNodeType('bullet_list', view.state)
const isOrderedList = isActiveOfParentNodeType('ordered_list', view.state) const isOrderedList = isActiveOfParentNodeType('ordered_list', view.state)
const isBlockquote = isActiveOfParentNodeType('blockquote', view.state) const isBlockquote = isActiveOfParentNodeType('blockquote', view.state)

View File

@ -173,7 +173,10 @@ export default defineComponent({
} }
const handleClick = debounce(function() { 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) store.commit(MutationTypes.SET_RICHTEXT_ATTRS, attrs)
}, 30, { trailing: true }) }, 30, { trailing: true })