Skip to content

Commit

Permalink
fix: Prevent crash when pasting empty clipboard data in DataStory
Browse files Browse the repository at this point in the history
  • Loading branch information
stone-lyl committed Feb 21, 2025
1 parent 9f14ef9 commit b697536
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/ui/src/components/DataStory/common/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export const writeToClipboard = ({
}),
);
}

export const readFromClipboard = async(): Promise<{ nodes: SNode[]; edges: SEdge[] }> => {
let nodes: SNode[] = [];
let edges: SEdge[] = [];
try {
// Read from system clipboard
const text = await navigator.clipboard.readText();
nodes = JSON.parse(text).nodes;
edges = JSON.parse(text).edges;
nodes = JSON.parse(text).nodes ?? [];
edges = JSON.parse(text).edges ?? [];
} catch(e) {
console.warn('Error reading from clipboard', e);
}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/components/DataStory/useCopyPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function useCopyPaste() {
const generateCopiedId = (originalId: string) => `${originalId}-${now}`;

const { nodes: copiedNodes, edges: copiedEdges } = await readFromClipboard();
if (!copiedNodes?.length) return;

const calculateNewPosition = (originalPos: XYPosition) => ({
x: position.x + (originalPos.x - Math.min(...copiedNodes.map(n => n.position.x))),
Expand Down

0 comments on commit b697536

Please sign in to comment.