Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(common): extract buildClipboardData and insertClipboardData #979

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/bright-otters-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@plait/common': minor
'@plait/draw': minor
---

extract buildClipboardData and insertClipboardData to common and support scalable fun

rewrite buildClipboardData and insertClipboardData by basic method

fix copy arrow-line error
1 change: 0 additions & 1 deletion packages/angular-board/src/board/board.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ import { BOARD_TO_COMPONENT } from '../utils/weak-maps';
import { withAngular } from '../plugins/with-angular';
import { withImage, withText } from '@plait/common';
import { OnChangeData } from '../plugins/angular-board';
import { VectorPenPointerType } from '@plait/draw';

const ElementLowerHostClass = 'element-lower-host';
const ElementHostClass = 'element-host';
Expand Down
41 changes: 41 additions & 0 deletions packages/common/src/utils/clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { idCreator, PlaitBoard, PlaitElement, Point, Transforms } from '@plait/core';

export const buildClipboardData = (
board: PlaitBoard,
elements: PlaitElement[],
startPoint: Point,
elementBuilder?: (element: PlaitElement) => PlaitElement | undefined
) => {
return elements.map(element => {
const newElement = elementBuilder && elementBuilder(element);
if (newElement) {
return newElement;
}
if (element.points) {
const points = element.points.map(point => [point[0] - startPoint[0], point[1] - startPoint[1]]);
return { ...element, points };
}
return element;
});
};

export const insertClipboardData = (
board: PlaitBoard,
elements: PlaitElement[],
startPoint: Point,
elementHandler?: (element: PlaitElement, idsMap: Record<string, string>) => void
) => {
const idsMap: Record<string, string> = {};
elements.forEach(element => {
idsMap[element.id] = idCreator();
});
elements.forEach(element => {
element.id = idsMap[element.id];
elementHandler && elementHandler(element, idsMap);
if (element.points) {
element.points = element.points.map(point => [startPoint[0] + point[0], startPoint[1] + point[1]]) as [Point, Point];
}
Transforms.insertNode(board, element, [board.children.length]);
});
Transforms.addSelectionWithTemporaryElements(board, elements);
};
1 change: 1 addition & 0 deletions packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './rotate';
export * from './elements';
export * from './animate';
export * from './stroke';
export * from './clipboard';
2 changes: 1 addition & 1 deletion packages/core/src/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function getElementById<T extends PlaitElement = PlaitElement>(
id: string,
dataSource?: PlaitElement[]
): T | undefined {
const cachedElement = getElementMap(board).get(id);
const cachedElement = !dataSource && getElementMap(board).get(id);
if (cachedElement) {
return cachedElement as T;
}
Expand Down
24 changes: 6 additions & 18 deletions packages/draw/src/utils/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { PlaitBoard, Point, Transforms, getElementById, idCreator } from '@plait/core';
import { PlaitArrowLine, PlaitDrawElement, PlaitGeometry, PlaitShapeElement } from '../interfaces';
import { PlaitImage } from '../interfaces/image';
import { PlaitBoard, PlaitElement, Point, Transforms, getElementById, idCreator } from '@plait/core';
import { buildClipboardData as basicBuildClipboard, insertClipboardData as basicInsertClipboard } from '@plait/common';
import { PlaitArrowLine, PlaitDrawElement, PlaitGeometry } from '../interfaces';
import { getConnectionPoint } from './arrow-line/arrow-line-common';
import { PlaitTable } from '../interfaces/table';
import { updateCellIds, updateRowOrColumnIds } from './table';

export const buildClipboardData = (board: PlaitBoard, elements: PlaitDrawElement[], startPoint: Point) => {
return elements.map(element => {
if (PlaitDrawElement.isShapeElement(element)) {
const points = element.points.map(point => [point[0] - startPoint[0], point[1] - startPoint[1]]);
return { ...element, points } as PlaitGeometry;
}
return basicBuildClipboard(board, elements, startPoint, (element: PlaitElement) => {
if (PlaitDrawElement.isArrowLine(element)) {
let source = { ...element.source };
let target = { ...element.target };
Expand All @@ -35,17 +31,12 @@ export const buildClipboardData = (board: PlaitBoard, elements: PlaitDrawElement
points = points.map(point => [point[0] - startPoint[0], point[1] - startPoint[1]]);
return { ...element, points, source, target } as PlaitArrowLine;
}
return element;
return undefined;
});
};

export const insertClipboardData = (board: PlaitBoard, elements: PlaitDrawElement[], startPoint: Point) => {
const idsMap: Record<string, string> = {};
elements.forEach(element => {
idsMap[element.id] = idCreator();
});
elements.forEach(element => {
element.id = idsMap[element.id];
basicInsertClipboard(board, elements, startPoint, (element: PlaitElement, idsMap: Record<string, string>) => {
if (PlaitDrawElement.isArrowLine(element)) {
if (element.source.boundId) {
const boundElement = elements.find(item => [element.source.boundId, idsMap[element.source.boundId!]].includes(item.id));
Expand All @@ -65,8 +56,5 @@ export const insertClipboardData = (board: PlaitBoard, elements: PlaitDrawElemen
updateRowOrColumnIds(element as PlaitTable, 'column');
updateCellIds(element.cells);
}
element.points = element.points.map(point => [startPoint[0] + point[0], startPoint[1] + point[1]]) as [Point, Point];
Transforms.insertNode(board, element, [board.children.length]);
});
Transforms.addSelectionWithTemporaryElements(board, elements);
};
1 change: 1 addition & 0 deletions packages/draw/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './table-selected';
export * from './swimlane';
export * from './line';
export * from './vector-line';
export * from './clipboard';
Loading