From 54b122b77e9345146835aa45cdc034e2d88ec61f Mon Sep 17 00:00:00 2001 From: pipipi-pikachu Date: Mon, 2 May 2022 21:10:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AF=8C=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=91=BD=E4=BB=A4=E5=8F=82=E6=95=B0=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/emitter.ts | 9 +++++++-- .../Editor/Toolbar/ElementStylePanel/ShapeStylePanel.vue | 2 +- .../Editor/Toolbar/ElementStylePanel/TextStylePanel.vue | 8 ++++---- src/views/Editor/Toolbar/SymbolPanel.vue | 4 ++-- src/views/components/element/ProsemirrorEditor.vue | 9 +++++---- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/utils/emitter.ts b/src/utils/emitter.ts index 1edd9c77..7403cce2 100644 --- a/src/utils/emitter.ts +++ b/src/utils/emitter.ts @@ -6,13 +6,18 @@ export const enum EmitterEvents { OPEN_LATEX_EDITOR = 'OPEN_LATEX_EDITOR', } -export interface RichTextCommand { +export interface RichTextAction { command: string; value?: string; } +export interface RichTextCommand { + target?: string; + action: RichTextAction | RichTextAction[]; +} + type Events = { - [EmitterEvents.RICH_TEXT_COMMAND]: RichTextCommand | RichTextCommand[]; + [EmitterEvents.RICH_TEXT_COMMAND]: RichTextCommand; [EmitterEvents.OPEN_CHART_DATA_EDITOR]: void; [EmitterEvents.OPEN_LATEX_EDITOR]: void; } diff --git a/src/views/Editor/Toolbar/ElementStylePanel/ShapeStylePanel.vue b/src/views/Editor/Toolbar/ElementStylePanel/ShapeStylePanel.vue index e195013b..6f5ad207 100644 --- a/src/views/Editor/Toolbar/ElementStylePanel/ShapeStylePanel.vue +++ b/src/views/Editor/Toolbar/ElementStylePanel/ShapeStylePanel.vue @@ -289,7 +289,7 @@ export default defineComponent({ ] const emitRichTextCommand = (command: string, value?: string) => { - emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { command, value }) + emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { action: { command, value } }) } return { diff --git a/src/views/Editor/Toolbar/ElementStylePanel/TextStylePanel.vue b/src/views/Editor/Toolbar/ElementStylePanel/TextStylePanel.vue index f0e3f44a..e0b637f5 100644 --- a/src/views/Editor/Toolbar/ElementStylePanel/TextStylePanel.vue +++ b/src/views/Editor/Toolbar/ElementStylePanel/TextStylePanel.vue @@ -240,7 +240,7 @@ import { defineComponent, ref, watch } from 'vue' import { storeToRefs } from 'pinia' import { useMainStore, useSlidesStore } from '@/store' import { PPTTextElement } from '@/types/slides' -import emitter, { EmitterEvents, RichTextCommand } from '@/utils/emitter' +import emitter, { EmitterEvents, RichTextAction } from '@/utils/emitter' import { WEB_FONTS } from '@/configs/font' import useHistorySnapshot from '@/hooks/useHistorySnapshot' import { message } from 'ant-design-vue' @@ -379,12 +379,12 @@ export default defineComponent({ // 发射富文本设置命令 const emitRichTextCommand = (command: string, value?: string) => { - emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { command, value }) + emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { action: { command, value } }) } // 发射富文本设置命令(批量) - const emitBatchRichTextCommand = (payload: RichTextCommand[]) => { - emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, payload) + const emitBatchRichTextCommand = (action: RichTextAction[]) => { + emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { action }) } // 设置富文本超链接 diff --git a/src/views/Editor/Toolbar/SymbolPanel.vue b/src/views/Editor/Toolbar/SymbolPanel.vue index 562e379a..3686406d 100644 --- a/src/views/Editor/Toolbar/SymbolPanel.vue +++ b/src/views/Editor/Toolbar/SymbolPanel.vue @@ -33,8 +33,8 @@ export default defineComponent({ return selectedSymbol?.children || [] }) - const selectSymbol = (item: string) => { - emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { command: 'insert', value: item }) + const selectSymbol = (value: string) => { + emitter.emit(EmitterEvents.RICH_TEXT_COMMAND, { action: { command: 'insert', value } }) } return { diff --git a/src/views/components/element/ProsemirrorEditor.vue b/src/views/components/element/ProsemirrorEditor.vue index 8418fda0..c84fe6e3 100644 --- a/src/views/components/element/ProsemirrorEditor.vue +++ b/src/views/components/element/ProsemirrorEditor.vue @@ -124,12 +124,13 @@ export default defineComponent({ // 执行富文本命令(可以是一个或多个) // 部分命令在执行前先判断当前选区是否为空,如果选区为空先进行全选操作 - const execCommand = (payload: RichTextCommand | RichTextCommand[]) => { - if (handleElementId.value !== props.elementId) return + const execCommand = ({ target, action }: RichTextCommand) => { + if (!target && handleElementId.value !== props.elementId) return + if (target && target !== props.elementId) return - const commands = ('command' in payload) ? [payload] : payload + const actions = ('command' in action) ? [action] : action - for (const item of commands) { + for (const item of actions) { if (item.command === 'fontname' && item.value) { const mark = editorView.state.schema.marks.fontname.create({ fontname: item.value }) autoSelectAll(editorView)