danylokhodus commited on
Commit
dcaa853
·
1 Parent(s): dae4143

feat: synchronize node copying to system clipboard with custom prefix

Browse files
src/features/canvas/hooks/useCanvasLogic.js CHANGED
@@ -1261,14 +1261,22 @@ export const useCanvasLogic = (projectId, user) => {
1261
 
1262
  if (selectedNodes.length === 0) return;
1263
 
1264
- window.__flowClipboard = {
1265
  nodes: selectedNodes.map(n => ({
1266
  ...n,
1267
  originalPosition: { ...n.position }
1268
  })),
1269
  edges: selectedEdges
1270
  };
 
 
1271
  setClipboardVersion(v => v + 1);
 
 
 
 
 
 
1272
  }, [nodes, edges]);
1273
 
1274
  const handleCut = useCallback(() => {
@@ -1280,15 +1288,23 @@ export const useCanvasLogic = (projectId, user) => {
1280
 
1281
  takeHistorySnapshot();
1282
 
1283
- window.__flowClipboard = {
1284
  nodes: selectedNodes.map(n => ({
1285
  ...n,
1286
  originalPosition: { ...n.position }
1287
  })),
1288
  edges: selectedEdges
1289
  };
 
 
1290
  setClipboardVersion(v => v + 1);
1291
 
 
 
 
 
 
 
1292
  selectedNodes.forEach(node => {
1293
  setNodes((nds) => nds.filter((n) => n.id !== node.id));
1294
  if (broadcastMessageRef.current) {
 
1261
 
1262
  if (selectedNodes.length === 0) return;
1263
 
1264
+ const clipboardPayload = {
1265
  nodes: selectedNodes.map(n => ({
1266
  ...n,
1267
  originalPosition: { ...n.position }
1268
  })),
1269
  edges: selectedEdges
1270
  };
1271
+
1272
+ window.__flowClipboard = clipboardPayload;
1273
  setClipboardVersion(v => v + 1);
1274
+
1275
+ try {
1276
+ navigator.clipboard.writeText('FLOWWEB_NODES_DATA:' + JSON.stringify(clipboardPayload)).catch(() => {});
1277
+ } catch (err) {
1278
+ console.warn("Failed to write to system clipboard", err);
1279
+ }
1280
  }, [nodes, edges]);
1281
 
1282
  const handleCut = useCallback(() => {
 
1288
 
1289
  takeHistorySnapshot();
1290
 
1291
+ const clipboardPayload = {
1292
  nodes: selectedNodes.map(n => ({
1293
  ...n,
1294
  originalPosition: { ...n.position }
1295
  })),
1296
  edges: selectedEdges
1297
  };
1298
+
1299
+ window.__flowClipboard = clipboardPayload;
1300
  setClipboardVersion(v => v + 1);
1301
 
1302
+ try {
1303
+ navigator.clipboard.writeText('FLOWWEB_NODES_DATA:' + JSON.stringify(clipboardPayload)).catch(() => {});
1304
+ } catch (err) {
1305
+ console.warn("Failed to write to system clipboard", err);
1306
+ }
1307
+
1308
  selectedNodes.forEach(node => {
1309
  setNodes((nds) => nds.filter((n) => n.id !== node.id));
1310
  if (broadcastMessageRef.current) {
src/pages/CanvasPage.jsx CHANGED
@@ -1555,6 +1555,21 @@ const CanvasInner = () => {
1555
  if (permission === 'Read') return;
1556
  if (!clipboardData) return;
1557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1558
  const items = clipboardData.items;
1559
  let imageFound = false;
1560
 
@@ -1589,12 +1604,11 @@ const CanvasInner = () => {
1589
  }
1590
 
1591
  if (!imageFound) {
1592
- const text = clipboardData.getData('text/plain');
1593
  if (text && text.trim()) {
1594
  addNewNodeAt(clientX, clientY, 'text', { label: text });
1595
  }
1596
  }
1597
- }, [permission, addNewNodeAt]);
1598
 
1599
  // Keyboard and Global Event Handlers
1600
  useEffect(() => {
@@ -1648,39 +1662,31 @@ const CanvasInner = () => {
1648
 
1649
  // Ctrl+V / Cmd+V (Paste)
1650
  if ((event.ctrlKey || event.metaKey) && (event.code === 'KeyV' || keyLower === 'v' || keyLower === 'м')) {
1651
- // Only prevent default if we have internal nodes to paste
1652
- // Otherwise, let the 'paste' event fire for system clipboard via focus catcher
1653
- if (hasClipboardContent) {
1654
- event.preventDefault();
1655
- event.stopPropagation();
1656
- handlePaste(mousePosRef.current);
1657
- } else {
1658
- if (permission !== 'Read') {
1659
- const activeEl = document.activeElement;
1660
- const tempEditable = document.createElement('div');
1661
- tempEditable.contentEditable = 'true';
1662
- tempEditable.style.position = 'fixed';
1663
- tempEditable.style.left = '-9999px';
1664
- tempEditable.style.top = '-9999px';
1665
- document.body.appendChild(tempEditable);
1666
 
1667
- tempEditable.addEventListener('paste', (e) => {
1668
- e.preventDefault();
1669
- e.stopPropagation();
1670
- processClipboardData(e.clipboardData || window.clipboardData, window.__lastMouseX, window.__lastMouseY);
1671
-
1672
- setTimeout(() => {
1673
- if (tempEditable.parentNode) {
1674
- document.body.removeChild(tempEditable);
1675
- }
1676
- if (activeEl && typeof activeEl.focus === 'function') {
1677
- activeEl.focus();
1678
- }
1679
- }, 0);
1680
- }, { once: true });
1681
-
1682
- tempEditable.focus();
1683
- }
1684
  }
1685
  return;
1686
  }
@@ -2095,11 +2101,15 @@ const CanvasInner = () => {
2095
  selectedNodes.some(n => n.id === edge.source) &&
2096
  selectedNodes.some(n => n.id === edge.target)
2097
  );
2098
- window.__flowClipboard = {
2099
  nodes: selectedNodes.map(n => ({ ...n, originalPosition: { ...n.position } })),
2100
  edges: selectedEdges
2101
  };
 
2102
  triggerClipboardUpdate();
 
 
 
2103
  }
2104
  setContextMenu(null);
2105
  }}
@@ -2120,11 +2130,15 @@ const CanvasInner = () => {
2120
  selectedNodes.some(n => n.id === edge.source) &&
2121
  selectedNodes.some(n => n.id === edge.target)
2122
  );
2123
- window.__flowClipboard = {
2124
  nodes: selectedNodes.map(n => ({ ...n, originalPosition: { ...n.position } })),
2125
  edges: selectedEdges
2126
  };
 
2127
  triggerClipboardUpdate();
 
 
 
2128
  // Delete selected nodes
2129
  selectedNodes.forEach(n => deleteNode(n.id));
2130
  }
 
1555
  if (permission === 'Read') return;
1556
  if (!clipboardData) return;
1557
 
1558
+ // Check if the clipboard contains serialized node data
1559
+ const text = clipboardData.getData('text/plain');
1560
+ if (text && text.startsWith('FLOWWEB_NODES_DATA:')) {
1561
+ try {
1562
+ const parsed = JSON.parse(text.substring('FLOWWEB_NODES_DATA:'.length));
1563
+ if (parsed && parsed.nodes) {
1564
+ window.__flowClipboard = parsed;
1565
+ handlePaste({ x: clientX, y: clientY });
1566
+ return;
1567
+ }
1568
+ } catch (err) {
1569
+ console.error("Failed to parse flowweb nodes from clipboard", err);
1570
+ }
1571
+ }
1572
+
1573
  const items = clipboardData.items;
1574
  let imageFound = false;
1575
 
 
1604
  }
1605
 
1606
  if (!imageFound) {
 
1607
  if (text && text.trim()) {
1608
  addNewNodeAt(clientX, clientY, 'text', { label: text });
1609
  }
1610
  }
1611
+ }, [permission, addNewNodeAt, handlePaste]);
1612
 
1613
  // Keyboard and Global Event Handlers
1614
  useEffect(() => {
 
1662
 
1663
  // Ctrl+V / Cmd+V (Paste)
1664
  if ((event.ctrlKey || event.metaKey) && (event.code === 'KeyV' || keyLower === 'v' || keyLower === 'м')) {
1665
+ if (permission !== 'Read') {
1666
+ const activeEl = document.activeElement;
1667
+ const tempEditable = document.createElement('div');
1668
+ tempEditable.contentEditable = 'true';
1669
+ tempEditable.style.position = 'fixed';
1670
+ tempEditable.style.left = '-9999px';
1671
+ tempEditable.style.top = '-9999px';
1672
+ document.body.appendChild(tempEditable);
1673
+
1674
+ tempEditable.addEventListener('paste', (e) => {
1675
+ e.preventDefault();
1676
+ e.stopPropagation();
1677
+ processClipboardData(e.clipboardData || window.clipboardData, window.__lastMouseX, window.__lastMouseY);
 
 
1678
 
1679
+ setTimeout(() => {
1680
+ if (tempEditable.parentNode) {
1681
+ document.body.removeChild(tempEditable);
1682
+ }
1683
+ if (activeEl && typeof activeEl.focus === 'function') {
1684
+ activeEl.focus();
1685
+ }
1686
+ }, 0);
1687
+ }, { once: true });
1688
+
1689
+ tempEditable.focus();
 
 
 
 
 
 
1690
  }
1691
  return;
1692
  }
 
2101
  selectedNodes.some(n => n.id === edge.source) &&
2102
  selectedNodes.some(n => n.id === edge.target)
2103
  );
2104
+ const clipboardPayload = {
2105
  nodes: selectedNodes.map(n => ({ ...n, originalPosition: { ...n.position } })),
2106
  edges: selectedEdges
2107
  };
2108
+ window.__flowClipboard = clipboardPayload;
2109
  triggerClipboardUpdate();
2110
+ try {
2111
+ navigator.clipboard.writeText('FLOWWEB_NODES_DATA:' + JSON.stringify(clipboardPayload)).catch(() => {});
2112
+ } catch (err) {}
2113
  }
2114
  setContextMenu(null);
2115
  }}
 
2130
  selectedNodes.some(n => n.id === edge.source) &&
2131
  selectedNodes.some(n => n.id === edge.target)
2132
  );
2133
+ const clipboardPayload = {
2134
  nodes: selectedNodes.map(n => ({ ...n, originalPosition: { ...n.position } })),
2135
  edges: selectedEdges
2136
  };
2137
+ window.__flowClipboard = clipboardPayload;
2138
  triggerClipboardUpdate();
2139
+ try {
2140
+ navigator.clipboard.writeText('FLOWWEB_NODES_DATA:' + JSON.stringify(clipboardPayload)).catch(() => {});
2141
+ } catch (err) {}
2142
  // Delete selected nodes
2143
  selectedNodes.forEach(n => deleteNode(n.id));
2144
  }