From 5d876529f0e835dc10baa17c1583961f13d3485b Mon Sep 17 00:00:00 2001 From: pubuzhixing8 Date: Mon, 25 Nov 2024 19:49:17 +0800 Subject: [PATCH] refactor(clipboard): extract addOrCreateClipboardContext to handle addClipboardContext and createClipboardContext requirement (#983) --- .changeset/great-doors-scream.md | 5 ++++ .../core/src/plugins/with-related-fragment.ts | 23 ++++++------------- packages/core/src/utils/clipboard/common.ts | 8 +++++++ .../draw/src/plugins/with-draw-fragment.ts | 18 ++++++--------- .../mind/src/plugins/with-mind-fragment.ts | 20 +++++++--------- 5 files changed, 35 insertions(+), 39 deletions(-) create mode 100644 .changeset/great-doors-scream.md diff --git a/.changeset/great-doors-scream.md b/.changeset/great-doors-scream.md new file mode 100644 index 000000000..0cef6ad8d --- /dev/null +++ b/.changeset/great-doors-scream.md @@ -0,0 +1,5 @@ +--- +'@plait/core': minor +--- + +extract addOrCreateClipboardContext to handle addClipboardContext and createClipboardContext requirement \ No newline at end of file diff --git a/packages/core/src/plugins/with-related-fragment.ts b/packages/core/src/plugins/with-related-fragment.ts index 2c762b247..e5126edb2 100644 --- a/packages/core/src/plugins/with-related-fragment.ts +++ b/packages/core/src/plugins/with-related-fragment.ts @@ -1,11 +1,5 @@ import { PlaitBoard, PlaitElement, RectangleClient } from '../interfaces'; -import { - WritableClipboardContext, - createClipboardContext, - WritableClipboardType, - addClipboardContext, - WritableClipboardOperationType -} from '../utils'; +import { WritableClipboardContext, WritableClipboardType, WritableClipboardOperationType, addOrCreateClipboardContext } from '../utils'; export function withRelatedFragment(board: PlaitBoard) { const { buildFragment } = board; @@ -22,15 +16,12 @@ export function withRelatedFragment(board: PlaitBoard) { relatedFragment = relatedFragment.filter(item => !originData.map(element => element.id).includes(item.id)); } if (relatedFragment.length) { - if (!clipboardContext) { - clipboardContext = createClipboardContext(WritableClipboardType.elements, relatedFragment, ''); - } else { - clipboardContext = addClipboardContext(clipboardContext, { - text: '', - type: WritableClipboardType.elements, - elements: relatedFragment - }); - } + const addition: WritableClipboardContext = { + text: '', + type: WritableClipboardType.elements, + elements: relatedFragment + }; + addOrCreateClipboardContext(clipboardContext, addition); } } return buildFragment(clipboardContext, rectangle, operationType, originData); diff --git a/packages/core/src/utils/clipboard/common.ts b/packages/core/src/utils/clipboard/common.ts index 22aabf7e7..ba2d0cdbb 100644 --- a/packages/core/src/utils/clipboard/common.ts +++ b/packages/core/src/utils/clipboard/common.ts @@ -81,6 +81,14 @@ export const addClipboardContext = ( return clipboardContext; }; +export const addOrCreateClipboardContext = (clipboardContext: WritableClipboardContext | null, addition: WritableClipboardContext) => { + if (!clipboardContext) { + return addition; + } else { + return addClipboardContext(clipboardContext, addition); + } +}; + export const replaceAngleBrackets = (str: string) => { return str.replace(//g, '>'); }; diff --git a/packages/draw/src/plugins/with-draw-fragment.ts b/packages/draw/src/plugins/with-draw-fragment.ts index f25b618ce..29b84fd08 100644 --- a/packages/draw/src/plugins/with-draw-fragment.ts +++ b/packages/draw/src/plugins/with-draw-fragment.ts @@ -7,8 +7,7 @@ import { WritableClipboardContext, WritableClipboardOperationType, WritableClipboardType, - addClipboardContext, - createClipboardContext, + addOrCreateClipboardContext, getSelectedElements } from '@plait/core'; import { getSelectedDrawElements } from '../utils/selected'; @@ -73,15 +72,12 @@ export const withDrawFragment = (baseBoard: PlaitBoard) => { const selectedElements = [...targetDrawElements, ...boundLineElements]; const elements = buildClipboardData(board, selectedElements, rectangle ? [rectangle.x, rectangle.y] : [0, 0]); const text = getElementsText(selectedElements); - if (!clipboardContext) { - clipboardContext = createClipboardContext(WritableClipboardType.elements, elements, text); - } else { - clipboardContext = addClipboardContext(clipboardContext, { - text, - type: WritableClipboardType.elements, - elements - }); - } + const addition: WritableClipboardContext = { + text, + type: WritableClipboardType.elements, + elements: elements + }; + addOrCreateClipboardContext(clipboardContext, addition); } return buildFragment(clipboardContext, rectangle, operationType, originData); }; diff --git a/packages/mind/src/plugins/with-mind-fragment.ts b/packages/mind/src/plugins/with-mind-fragment.ts index 73ddf99b4..5d89d072a 100644 --- a/packages/mind/src/plugins/with-mind-fragment.ts +++ b/packages/mind/src/plugins/with-mind-fragment.ts @@ -9,9 +9,8 @@ import { WritableClipboardContext, WritableClipboardOperationType, WritableClipboardType, - addClipboardContext, - addSelectedElement, - createClipboardContext + addOrCreateClipboardContext, + addSelectedElement } from '@plait/core'; import { MindElement } from '../interfaces'; import { AbstractNode } from '@plait/layouts'; @@ -65,15 +64,12 @@ export const withMindFragment = (baseBoard: PlaitBoard) => { if (firstLevelElements.length) { const elements = buildClipboardData(board, firstLevelElements, rectangle ? [rectangle.x, rectangle.y] : [0, 0]); const text = getElementsText(targetMindElements); - if (!clipboardContext) { - clipboardContext = createClipboardContext(WritableClipboardType.elements, elements, text); - } else { - clipboardContext = addClipboardContext(clipboardContext, { - text, - type: WritableClipboardType.elements, - elements - }); - } + const addition: WritableClipboardContext = { + text, + type: WritableClipboardType.elements, + elements: elements + }; + addOrCreateClipboardContext(clipboardContext, addition); } return buildFragment(clipboardContext, rectangle, operationType, originData); };