From ed2f0906f242996f1486adbee802c3d0746e0a34 Mon Sep 17 00:00:00 2001 From: pubuzhixing8 Date: Wed, 31 Jul 2024 14:07:31 +0800 Subject: [PATCH] fix(mind): Identify mind elements by the type of root node rather than the node properties Handle the waring of drawing on element can not been identified --- .changeset/green-ladybugs-breathe.md | 8 ++++++++ packages/core/src/plugins/create-board.ts | 7 ++++++- packages/core/src/utils/element.ts | 5 ++++- packages/mind/src/interfaces/element.ts | 3 ++- packages/mind/src/plugins/with-mind.ts | 2 +- 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 .changeset/green-ladybugs-breathe.md diff --git a/.changeset/green-ladybugs-breathe.md b/.changeset/green-ladybugs-breathe.md new file mode 100644 index 000000000..7213526d7 --- /dev/null +++ b/.changeset/green-ladybugs-breathe.md @@ -0,0 +1,8 @@ +--- +'@plait/core': patch +'@plait/mind': patch +--- + +Identify mind elements by the type of root node rather than the node properties + +Handle the waring of drawing on element can not been identified diff --git a/packages/core/src/plugins/create-board.ts b/packages/core/src/plugins/create-board.ts index 28670fabb..5ede4c35e 100644 --- a/packages/core/src/plugins/create-board.ts +++ b/packages/core/src/plugins/create-board.ts @@ -11,6 +11,8 @@ import { ThemeColorMode } from '../interfaces/theme'; import { CoreTransforms } from '../transforms/element'; import { ClipboardData, WritableClipboardContext, WritableClipboardOperationType, drawEntireActiveRectangleG } from '../utils'; import { Point, RectangleClient } from '../interfaces'; +import { isDebug } from '../utils/debug'; +import { ElementFlavour } from '../core/element/element-flavour'; export function createBoard(children: PlaitElement[], options?: PlaitBoardOptions): PlaitBoard { const board: PlaitBoard = { @@ -109,7 +111,10 @@ export function createBoard(children: PlaitElement[], options?: PlaitBoardOption getDeletedFragment: (data: PlaitElement[]) => data, getRelatedFragment: (data: PlaitElement[], originData?: PlaitElement[]) => data, drawElement: (context: PlaitPluginElementContext) => { - throw new Error(`can not resolve plugin element component type: ${context.element.type}`); + if (isDebug()) { + console.error(`can not resolve plugin element: ${JSON.stringify(context.element)}`); + } + return ElementFlavour; }, isWithinSelection: element => false, isRectangleHit: element => false, diff --git a/packages/core/src/utils/element.ts b/packages/core/src/utils/element.ts index 9e2df8814..fbe8d1855 100644 --- a/packages/core/src/utils/element.ts +++ b/packages/core/src/utils/element.ts @@ -1,5 +1,6 @@ import { Ancestor, PlaitBoard, PlaitElement, Point, RectangleClient } from '../interfaces'; import { getSelectionAngle, hasSameAngle, getRotatedBoundingRectangle, rotatePointsByElement, getRectangleByAngle } from './angle'; +import { isDebug } from './debug'; import { depthFirstRecursion, getIsRecursionFunc } from './tree'; export function getRectangleByElements(board: PlaitBoard, elements: PlaitElement[], recursion: boolean): RectangleClient { @@ -11,7 +12,9 @@ export function getRectangleByElements(board: PlaitBoard, elements: PlaitElement const rotatedCornerPoints = rotatePointsByElement(cornerPoints, node) || cornerPoints; rectanglesCornerPoints.push(rotatedCornerPoints); } else { - console.error(`can not get rectangle of element:`, node); + if (isDebug()) { + console.error(`can not get rectangle of element:`, node); + } } }; elements.forEach(element => { diff --git a/packages/mind/src/interfaces/element.ts b/packages/mind/src/interfaces/element.ts index b8873d82f..9c1f2aa09 100644 --- a/packages/mind/src/interfaces/element.ts +++ b/packages/mind/src/interfaces/element.ts @@ -54,7 +54,8 @@ export const MindElement = { return isIndentedLayout(_layout); }, isMindElement(board: PlaitBoard, element: PlaitElement): element is MindElement { - if (element.data && element.data.topic && element.width && element.height) { + const root = MindElement.getRoot(board, element as MindElement); + if (root && PlaitMind.isMind(root)) { return true; } else { return false; diff --git a/packages/mind/src/plugins/with-mind.ts b/packages/mind/src/plugins/with-mind.ts index 36c272ab2..622596605 100644 --- a/packages/mind/src/plugins/with-mind.ts +++ b/packages/mind/src/plugins/with-mind.ts @@ -69,7 +69,7 @@ export const withMind = (baseBoard: PlaitBoard) => { board.getRectangle = element => { if (MindElement.isMindElement(board, element)) { if (!PlaitElement.hasMounted(element)) { - console.error('mind element has not been mounted'); + console.error(`mind element has not been mounted: ${JSON.stringify(element)}`); } return getRectangleByNode(MindElement.getNode(element)); }