From fd7808eacf08ca219611bfe94d791176dc6f7825 Mon Sep 17 00:00:00 2001 From: pubuzhixing8 Date: Fri, 10 May 2024 11:07:29 +0800 Subject: [PATCH] fix(text): fix error when first element has no text --- .changeset/old-llamas-cough.md | 7 +++++++ packages/common/src/transforms/text.ts | 7 +++++-- packages/common/src/utils/text.ts | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 .changeset/old-llamas-cough.md diff --git a/.changeset/old-llamas-cough.md b/.changeset/old-llamas-cough.md new file mode 100644 index 000000000..414532373 --- /dev/null +++ b/.changeset/old-llamas-cough.md @@ -0,0 +1,7 @@ +--- +'@plait/common': patch +--- + +add findFirstTextEditor to get first text editor correctly + +fix error when first element has no text diff --git a/packages/common/src/transforms/text.ts b/packages/common/src/transforms/text.ts index 6c8690a14..9ed5a028e 100644 --- a/packages/common/src/transforms/text.ts +++ b/packages/common/src/transforms/text.ts @@ -2,12 +2,15 @@ import { PlaitBoard, PlaitElement, getSelectedElements } from '@plait/core'; import { AlignEditor, Alignment, FontSizes, MarkTypes, PlaitMarkEditor } from '@plait/text'; import { BaseRange, Editor, Transforms as SlateTransforms } from 'slate'; import { AngularEditor } from 'slate-angular'; -import { getTextEditors } from '../utils/text'; +import { findFirstTextEditor, getTextEditors } from '../utils/text'; const setTextMarks = (board: PlaitBoard, mark: MarkTypes) => { const selectedElements = getSelectedElements(board); if (selectedElements.length) { - const firstEditor = getTextEditors(selectedElements[0])[0]; + const firstEditor = findFirstTextEditor(board); + if (!firstEditor) { + return; + } const activeMarks = PlaitMarkEditor.getMarks(firstEditor); const elements = selectedElements.filter(element => { const editors = getTextEditors(element); diff --git a/packages/common/src/utils/text.ts b/packages/common/src/utils/text.ts index e2960b45e..3f732f543 100644 --- a/packages/common/src/utils/text.ts +++ b/packages/common/src/utils/text.ts @@ -1,7 +1,6 @@ -import { PlaitElement } from '@plait/core'; -import { CommonPluginElement } from '../core/plugin-element'; +import { PlaitBoard, PlaitElement, getSelectedElements } from '@plait/core'; import { CustomText, PlaitMarkEditor, TextManage } from '@plait/text'; -import { Node } from 'slate'; +import { Editor, Node } from 'slate'; export const getTextManages = (element: PlaitElement) => { return ELEMENT_TO_TEXT_MANAGES.get(element) || []; @@ -29,6 +28,18 @@ export const getFirstTextEditor = (element: PlaitElement) => { return textEditor; }; +export const findFirstTextEditor = (board: PlaitBoard) => { + const selectedElements = getSelectedElements(board); + let firstEditor: Editor | null = null; + selectedElements.forEach(element => { + const editors = getTextEditors(element); + if (!firstEditor && editors && editors.length > 0) { + firstEditor = editors[0]; + } + }); + return firstEditor; +}; + export const getTextMarksByElement = (element: PlaitElement) => { const editors = getTextEditors(element); const editor = editors[0];