Skip to content

Commit

Permalink
feat: optimize clipboard api
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed Oct 12, 2024
1 parent c471034 commit 09bace6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions packages/core/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { genUuid, increaseIdGenerator, noop } from '@suika/common';
import {
boxToRect,
invertMatrix,
type IPoint,
mergeBoxes,
multiplyMatrix,
} from '@suika/geo';
Expand Down Expand Up @@ -138,9 +139,9 @@ export class ClipboardManager {
/**
* paste at special coords
*/
pasteAt(x: number, y: number) {
pasteAt(point: IPoint) {
navigator.clipboard.readText().then((pastedData) => {
this.addGraphsFromClipboard(pastedData, x, y);
this.addGraphsFromClipboard(pastedData, point);
});
}

Expand Down Expand Up @@ -265,8 +266,8 @@ export class ClipboardManager {
}

private addGraphsFromClipboard(dataStr: string): void;
private addGraphsFromClipboard(dataStr: string, x: number, y: number): void;
private addGraphsFromClipboard(dataStr: string, x?: number, y?: number) {
private addGraphsFromClipboard(dataStr: string, point: IPoint): void;
private addGraphsFromClipboard(dataStr: string, point?: IPoint) {
let pastedData: IEditorPaperData | null = null;
try {
pastedData = JSON.parse(dataStr);
Expand Down Expand Up @@ -306,18 +307,17 @@ export class ClipboardManager {
const boundingRect = boxToRect(
mergeBoxes(selectedItems.map((item) => item.getBbox())),
);
if (
(x === undefined || y === undefined) &&
pastedData.paperId !== editor.paperId
) {
if (!point && pastedData.paperId !== editor.paperId) {
const vwCenter = this.editor.viewportManager.getCenter();
x = vwCenter.x - boundingRect.width / 2;
y = vwCenter.y - boundingRect.height / 2;
point = {
x: vwCenter.x - boundingRect.width / 2,
y: vwCenter.y - boundingRect.height / 2,
};
}

if (x !== undefined && y !== undefined) {
const dx = x - boundingRect.x;
const dy = y - boundingRect.y;
if (point) {
const dx = point.x - boundingRect.x;
const dy = point.y - boundingRect.y;
if (dx || dy) {
SuikaGraphics.dMove(selectedItems, dx, dy);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/suika/src/components/ContextMenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const ContextMenu: FC = () => {
},
true,
);
editor.clipboard.pasteAt(scenePos.x, scenePos.y);
editor.clipboard.pasteAt({ x: scenePos.x, y: scenePos.y });
}
}}
>
Expand Down

0 comments on commit 09bace6

Please sign in to comment.