From f4e64cec73f8c793d70b06ecf78213fd73a57656 Mon Sep 17 00:00:00 2001 From: pipipi-pikachu Date: Wed, 9 Jun 2021 23:14:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=8C=E6=96=87=E6=9C=AC=E5=85=89?= =?UTF-8?q?=E6=A0=87=E5=A4=84=E5=9C=A8=E6=9C=AB=E5=B0=BE=E6=88=96=E4=BD=BF?= =?UTF-8?q?=E7=94=A8Ctrl=20+=20A=E5=85=A8=E9=80=89=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=88=B0=E7=9A=84=E5=BD=93=E5=89=8D=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=A0=B7=E5=BC=8F=E4=B8=8D=E6=AD=A3=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mocks/slides.ts | 4 ++-- src/utils/prosemirror/utils.ts | 37 ++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/mocks/slides.ts b/src/mocks/slides.ts index 1ef790c1..7c08fdac 100644 --- a/src/mocks/slides.ts +++ b/src/mocks/slides.ts @@ -40,7 +40,7 @@ export const slides: Slide[] = [ width: 585, height: 188, lineHeight: 1.2, - content: '

PPTIST

', + content: '

PPTIST

', rotate: 0, defaultFontName: 'Microsoft Yahei', defaultColor: '#333' @@ -52,7 +52,7 @@ export const slides: Slide[] = [ top: 253.25, width: 585, height: 56, - content: '

基于 Vue 3.x + TypeScript 的在线演示文稿应用

', + content: '

基于 Vue 3.x + TypeScript 的在线演示文稿应用

', rotate: 0, defaultFontName: 'Microsoft Yahei', defaultColor: '#333' diff --git a/src/utils/prosemirror/utils.ts b/src/utils/prosemirror/utils.ts index 41ec6939..5fa122ee 100644 --- a/src/utils/prosemirror/utils.ts +++ b/src/utils/prosemirror/utils.ts @@ -1,4 +1,4 @@ -import { Node, NodeType, ResolvedPos } from 'prosemirror-model' +import { Node, NodeType, ResolvedPos, Mark } from 'prosemirror-model' import { EditorState, Selection } from 'prosemirror-state' import { EditorView } from 'prosemirror-view' @@ -40,20 +40,21 @@ export const isActiveOfParentNodeType = (nodeType: string, state: EditorState) = export const getMarkAttrs = (view: EditorView) => { const { selection, doc } = view.state const { from } = selection - const node = doc.nodeAt(from) + + let node = doc.nodeAt(from) || doc.nodeAt(from - 1) + if (node?.lastChild) node = node.lastChild + return node?.marks || [] } -export const getAttrValue = (view: EditorView, markType: string, attr: string) => { - const marks = getMarkAttrs(view) +export const getAttrValue = (marks: Mark[], markType: string, attr: string) => { for (const mark of marks) { if (mark.type.name === markType && mark.attrs[attr]) return mark.attrs[attr] } return null } -export const isActiveMark = (view: EditorView, markType: string) => { - const marks = getMarkAttrs(view) +export const isActiveMark = (marks: Mark[], markType: string) => { for (const mark of marks) { if (mark.type.name === markType) return true } @@ -77,17 +78,19 @@ export const getAttrValueInSelection = (view: EditorView, attr: string) => { } export const getTextAttrs = (view: EditorView) => { - const isBold = isActiveMark(view, 'strong') - const isEm = isActiveMark(view, 'em') - const isUnderline = isActiveMark(view, 'underline') - const isStrikethrough = isActiveMark(view, 'strikethrough') - const isSuperscript = isActiveMark(view, 'superscript') - const isSubscript = isActiveMark(view, 'subscript') - const isCode = isActiveMark(view, 'code') - const color = getAttrValue(view, 'forecolor', 'color') || '#000' - const backcolor = getAttrValue(view, 'backcolor', 'backcolor') || '#000' - const fontsize = getAttrValue(view, 'fontsize', 'fontsize') || '20px' - const fontname = getAttrValue(view, 'fontname', 'fontname') || '微软雅黑' + const marks = getMarkAttrs(view) + + const isBold = isActiveMark(marks, 'strong') + const isEm = isActiveMark(marks, 'em') + const isUnderline = isActiveMark(marks, 'underline') + const isStrikethrough = isActiveMark(marks, 'strikethrough') + 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 isBulletList = isActiveOfParentNodeType('bullet_list', view.state) const isOrderedList = isActiveOfParentNodeType('ordered_list', view.state)