Skip to content

Commit

Permalink
refactor(clipboard): extract addOrCreateClipboardContext to handle ad…
Browse files Browse the repository at this point in the history
…dClipboardContext and createClipboardContext requirement (#983)
  • Loading branch information
pubuzhixing8 authored Nov 25, 2024
1 parent 23cf8e5 commit 5d87652
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-doors-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@plait/core': minor
---

extract addOrCreateClipboardContext to handle addClipboardContext and createClipboardContext requirement
23 changes: 7 additions & 16 deletions packages/core/src/plugins/with-related-fragment.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/utils/clipboard/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '&lt;').replace(/>/g, '&gt;');
};
Expand Down
18 changes: 7 additions & 11 deletions packages/draw/src/plugins/with-draw-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
WritableClipboardContext,
WritableClipboardOperationType,
WritableClipboardType,
addClipboardContext,
createClipboardContext,
addOrCreateClipboardContext,
getSelectedElements
} from '@plait/core';
import { getSelectedDrawElements } from '../utils/selected';
Expand Down Expand Up @@ -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);
};
Expand Down
20 changes: 8 additions & 12 deletions packages/mind/src/plugins/with-mind-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
};
Expand Down

0 comments on commit 5d87652

Please sign in to comment.