Skip to content

Commit

Permalink
fix(text): fix error when first element has no text
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed May 10, 2024
1 parent dcb5263 commit fd7808e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changeset/old-llamas-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@plait/common': patch
---

add findFirstTextEditor to get first text editor correctly

fix error when first element has no text
7 changes: 5 additions & 2 deletions packages/common/src/transforms/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 14 additions & 3 deletions packages/common/src/utils/text.ts
Original file line number Diff line number Diff line change
@@ -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) || [];
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit fd7808e

Please sign in to comment.