Spaces:
Running
Running
Commit ·
786fd46
1
Parent(s): e40dcb3
feat(canvas): implement clipboard copy/cut/paste, redesigned left toolbar and context menus
Browse files
src/features/canvas/components/CanvasToolbar.jsx
CHANGED
|
@@ -1,23 +1,176 @@
|
|
| 1 |
-
import React from 'react';
|
| 2 |
-
import { motion } from 'framer-motion';
|
| 3 |
import { useTranslation } from 'react-i18next';
|
| 4 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
const CanvasToolbar = ({ handleAIRequest, isAIActive }) => {
|
| 7 |
-
const { t } = useTranslation();
|
| 8 |
return (
|
| 9 |
-
<div className="
|
| 10 |
-
<motion.button
|
| 11 |
-
whileHover={{ scale: 1.05 }}
|
| 12 |
-
whileTap={{ scale: 0.95 }}
|
| 13 |
-
onClick={
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
>
|
| 18 |
-
|
| 19 |
-
<span>{isAIActive ? t('canvas.ai_thinking', 'AI Thinking...') : t('canvas.ai_assistant', 'AI Assistant')}</span>
|
| 20 |
</motion.button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
</div>
|
| 22 |
);
|
| 23 |
};
|
|
|
|
| 1 |
+
import React, { useState } from 'react';
|
| 2 |
+
import { motion, AnimatePresence } from 'framer-motion';
|
| 3 |
import { useTranslation } from 'react-i18next';
|
| 4 |
+
import {
|
| 5 |
+
Sparkles,
|
| 6 |
+
Target,
|
| 7 |
+
GitFork,
|
| 8 |
+
Timer,
|
| 9 |
+
Copy,
|
| 10 |
+
Scissors,
|
| 11 |
+
Clipboard,
|
| 12 |
+
Image,
|
| 13 |
+
Trash2
|
| 14 |
+
} from 'lucide-react';
|
| 15 |
+
|
| 16 |
+
const TooltipButton = ({ children, tooltip, onClick, disabled = false, className = "" }) => {
|
| 17 |
+
const [hovered, setHovered] = useState(false);
|
| 18 |
+
const [hasClipboard, setHasClipboard] = useState(false);
|
| 19 |
+
|
| 20 |
+
const handleMouseEnter = () => {
|
| 21 |
+
setHovered(true);
|
| 22 |
+
if (tooltip === 'Вставити' || tooltip === 'Paste') {
|
| 23 |
+
setHasClipboard(!!(window.__flowClipboard && window.__flowClipboard.nodes && window.__flowClipboard.nodes.length > 0));
|
| 24 |
+
}
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
const isBtnDisabled = disabled || ((tooltip === 'Вставити' || tooltip === 'Paste') && !hasClipboard);
|
| 28 |
|
|
|
|
|
|
|
| 29 |
return (
|
| 30 |
+
<div className="relative flex items-center">
|
| 31 |
+
<motion.button
|
| 32 |
+
whileHover={{ scale: isBtnDisabled ? 1 : 1.05 }}
|
| 33 |
+
whileTap={{ scale: isBtnDisabled ? 1 : 0.95 }}
|
| 34 |
+
onClick={onClick}
|
| 35 |
+
disabled={isBtnDisabled}
|
| 36 |
+
onMouseEnter={handleMouseEnter}
|
| 37 |
+
onMouseLeave={() => setHovered(false)}
|
| 38 |
+
className={`p-2.5 rounded-xl transition-all relative border-2 border-transparent hover:border-primary hover:bg-surface-variant flex items-center justify-center ${
|
| 39 |
+
isBtnDisabled
|
| 40 |
+
? 'opacity-35 cursor-not-allowed text-primary/40'
|
| 41 |
+
: 'cursor-pointer text-primary hover:shadow-[2px_2px_0px_0px_var(--color-primary)]'
|
| 42 |
+
} ${className}`}
|
| 43 |
>
|
| 44 |
+
{children}
|
|
|
|
| 45 |
</motion.button>
|
| 46 |
+
|
| 47 |
+
<AnimatePresence>
|
| 48 |
+
{hovered && (
|
| 49 |
+
<motion.div
|
| 50 |
+
initial={{ opacity: 0, x: -10 }}
|
| 51 |
+
animate={{ opacity: 1, x: 0 }}
|
| 52 |
+
exit={{ opacity: 0, x: -10 }}
|
| 53 |
+
transition={{ duration: 0.15 }}
|
| 54 |
+
className="absolute left-full ml-3 px-3 py-1.5 bg-primary text-on-primary font-bold text-xs rounded-md shadow-lg border border-primary/20 whitespace-nowrap z-50 pointer-events-none"
|
| 55 |
+
>
|
| 56 |
+
{tooltip}
|
| 57 |
+
<div className="absolute right-full top-1/2 -translate-y-1/2 border-8 border-transparent border-r-primary"></div>
|
| 58 |
+
</motion.div>
|
| 59 |
+
)}
|
| 60 |
+
</AnimatePresence>
|
| 61 |
+
</div>
|
| 62 |
+
);
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
const CanvasToolbar = ({
|
| 66 |
+
handleAIRequest,
|
| 67 |
+
isAIActive,
|
| 68 |
+
addNewNodeAt,
|
| 69 |
+
handleCopy,
|
| 70 |
+
handleCut,
|
| 71 |
+
handlePaste,
|
| 72 |
+
exportToPng,
|
| 73 |
+
onDeleteSelected,
|
| 74 |
+
hasSelectedNodes
|
| 75 |
+
}) => {
|
| 76 |
+
const { t } = useTranslation();
|
| 77 |
+
|
| 78 |
+
return (
|
| 79 |
+
<div className="absolute top-28 left-6 z-20 flex flex-col items-center gap-3 bg-surface-container-lowest border-2 border-primary shadow-[4px_4px_0px_0px_var(--color-primary)] p-2.5 rounded-2xl transition-all">
|
| 80 |
+
{/* AI Assistant Section */}
|
| 81 |
+
<div className="relative group">
|
| 82 |
+
<motion.button
|
| 83 |
+
whileHover={{ scale: isAIActive ? 1 : 1.05 }}
|
| 84 |
+
whileTap={{ scale: isAIActive ? 1 : 0.95 }}
|
| 85 |
+
onClick={handleAIRequest}
|
| 86 |
+
className={`p-3 rounded-xl font-bold transition-all flex items-center justify-center relative border-2 ${
|
| 87 |
+
isAIActive
|
| 88 |
+
? 'bg-secondary text-primary border-primary shadow-[2px_2px_0px_0px_var(--color-primary)] animate-pulse'
|
| 89 |
+
: 'bg-primary text-on-primary border-transparent hover:border-primary hover:bg-primary/95 hover:shadow-[2px_2px_0px_0px_var(--color-primary)]'
|
| 90 |
+
}`}
|
| 91 |
+
>
|
| 92 |
+
<Sparkles size={18} />
|
| 93 |
+
</motion.button>
|
| 94 |
+
<div className="absolute left-full ml-3 top-1/2 -translate-y-1/2 px-3 py-1.5 bg-primary text-on-primary font-bold text-xs rounded-md shadow-lg border border-primary/20 whitespace-nowrap z-50 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity">
|
| 95 |
+
{isAIActive ? t('canvas.ai_thinking', 'AI Thinking...') : t('canvas.ai_assistant', 'AI Assistant')}
|
| 96 |
+
<div className="absolute right-full top-1/2 -translate-y-1/2 border-8 border-transparent border-r-primary"></div>
|
| 97 |
+
</div>
|
| 98 |
+
</div>
|
| 99 |
+
|
| 100 |
+
<div className="w-full h-[1px] bg-primary/10 my-1"></div>
|
| 101 |
+
|
| 102 |
+
{/* Node Creation Section */}
|
| 103 |
+
<div className="flex flex-col gap-2">
|
| 104 |
+
<TooltipButton
|
| 105 |
+
tooltip={t('canvas.toolbar.add_goal', 'Додати ціль')}
|
| 106 |
+
onClick={() => addNewNodeAt(undefined, undefined, 'sketch')}
|
| 107 |
+
>
|
| 108 |
+
<Target size={18} />
|
| 109 |
+
</TooltipButton>
|
| 110 |
+
|
| 111 |
+
<TooltipButton
|
| 112 |
+
tooltip={t('canvas.toolbar.add_condition', 'Додати умову')}
|
| 113 |
+
onClick={() => addNewNodeAt(undefined, undefined, 'condition')}
|
| 114 |
+
>
|
| 115 |
+
<GitFork size={18} />
|
| 116 |
+
</TooltipButton>
|
| 117 |
+
|
| 118 |
+
<TooltipButton
|
| 119 |
+
tooltip={t('canvas.toolbar.add_habit', 'Додати звичку')}
|
| 120 |
+
onClick={() => addNewNodeAt(undefined, undefined, 'habit')}
|
| 121 |
+
>
|
| 122 |
+
<Timer size={18} className="text-secondary" />
|
| 123 |
+
</TooltipButton>
|
| 124 |
+
</div>
|
| 125 |
+
|
| 126 |
+
<div className="w-full h-[1px] bg-primary/10 my-1"></div>
|
| 127 |
+
|
| 128 |
+
{/* Clipboard Actions */}
|
| 129 |
+
<div className="flex flex-col gap-2">
|
| 130 |
+
<TooltipButton
|
| 131 |
+
tooltip={t('canvas.toolbar.copy', 'Копіювати')}
|
| 132 |
+
onClick={handleCopy}
|
| 133 |
+
disabled={!hasSelectedNodes}
|
| 134 |
+
>
|
| 135 |
+
<Copy size={18} />
|
| 136 |
+
</TooltipButton>
|
| 137 |
+
|
| 138 |
+
<TooltipButton
|
| 139 |
+
tooltip={t('canvas.toolbar.cut', 'Вирізати')}
|
| 140 |
+
onClick={handleCut}
|
| 141 |
+
disabled={!hasSelectedNodes}
|
| 142 |
+
>
|
| 143 |
+
<Scissors size={18} />
|
| 144 |
+
</TooltipButton>
|
| 145 |
+
|
| 146 |
+
<TooltipButton
|
| 147 |
+
tooltip={t('canvas.toolbar.paste', 'Вставити')}
|
| 148 |
+
onClick={() => handlePaste()}
|
| 149 |
+
>
|
| 150 |
+
<Clipboard size={18} />
|
| 151 |
+
</TooltipButton>
|
| 152 |
+
</div>
|
| 153 |
+
|
| 154 |
+
<div className="w-full h-[1px] bg-primary/10 my-1"></div>
|
| 155 |
+
|
| 156 |
+
{/* Export & Cleanup */}
|
| 157 |
+
<div className="flex flex-col gap-2">
|
| 158 |
+
<TooltipButton
|
| 159 |
+
tooltip={t('canvas.toolbar.export_png', 'Експорт в PNG')}
|
| 160 |
+
onClick={exportToPng}
|
| 161 |
+
>
|
| 162 |
+
<Image size={18} className="text-green-600" />
|
| 163 |
+
</TooltipButton>
|
| 164 |
+
|
| 165 |
+
<TooltipButton
|
| 166 |
+
tooltip={t('canvas.toolbar.delete_selected', 'Видалити виділене')}
|
| 167 |
+
onClick={onDeleteSelected}
|
| 168 |
+
disabled={!hasSelectedNodes}
|
| 169 |
+
className="hover:bg-red-500/10 hover:text-red-500 hover:border-red-500/30"
|
| 170 |
+
>
|
| 171 |
+
<Trash2 size={18} className={hasSelectedNodes ? "text-red-500" : ""} />
|
| 172 |
+
</TooltipButton>
|
| 173 |
+
</div>
|
| 174 |
</div>
|
| 175 |
);
|
| 176 |
};
|
src/features/canvas/hooks/useCanvasLogic.js
CHANGED
|
@@ -798,8 +798,10 @@ export const useCanvasLogic = (projectId, user) => {
|
|
| 798 |
);
|
| 799 |
|
| 800 |
const addNewNodeAt = useCallback((x, y, type = 'sketch') => {
|
|
|
|
|
|
|
| 801 |
const id = crypto.randomUUID();
|
| 802 |
-
const position = screenToFlowPosition({ x, y });
|
| 803 |
|
| 804 |
const nodeData = {
|
| 805 |
label: '',
|
|
@@ -842,6 +844,158 @@ export const useCanvasLogic = (projectId, user) => {
|
|
| 842 |
return id;
|
| 843 |
}, [setNodes, screenToFlowPosition, updateNodeDataCallback]);
|
| 844 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 845 |
const submitAICanvasPrompt = async (promptText) => {
|
| 846 |
if (!promptText || !projectId) return false;
|
| 847 |
setAiLoading(true);
|
|
@@ -1087,6 +1241,9 @@ export const useCanvasLogic = (projectId, user) => {
|
|
| 1087 |
setShowPaywall,
|
| 1088 |
handleAIRequest,
|
| 1089 |
addNewNodeAt,
|
|
|
|
|
|
|
|
|
|
| 1090 |
handleMouseDown,
|
| 1091 |
handleMouseUp,
|
| 1092 |
saveCanvas,
|
|
|
|
| 798 |
);
|
| 799 |
|
| 800 |
const addNewNodeAt = useCallback((x, y, type = 'sketch') => {
|
| 801 |
+
const finalX = x !== undefined ? x : window.innerWidth / 2;
|
| 802 |
+
const finalY = y !== undefined ? y : window.innerHeight / 2;
|
| 803 |
const id = crypto.randomUUID();
|
| 804 |
+
const position = screenToFlowPosition({ x: finalX, y: finalY });
|
| 805 |
|
| 806 |
const nodeData = {
|
| 807 |
label: '',
|
|
|
|
| 844 |
return id;
|
| 845 |
}, [setNodes, screenToFlowPosition, updateNodeDataCallback]);
|
| 846 |
|
| 847 |
+
const handleCopy = useCallback(() => {
|
| 848 |
+
const selectedNodes = nodes.filter((n) => n.selected);
|
| 849 |
+
const selectedEdges = edges.filter((e) => e.selected);
|
| 850 |
+
|
| 851 |
+
if (selectedNodes.length === 0) return;
|
| 852 |
+
|
| 853 |
+
window.__flowClipboard = {
|
| 854 |
+
nodes: selectedNodes.map(n => ({
|
| 855 |
+
...n,
|
| 856 |
+
originalPosition: { ...n.position }
|
| 857 |
+
})),
|
| 858 |
+
edges: selectedEdges
|
| 859 |
+
};
|
| 860 |
+
}, [nodes, edges]);
|
| 861 |
+
|
| 862 |
+
const handleCut = useCallback(() => {
|
| 863 |
+
if (projectData?.permission === 'Read') return;
|
| 864 |
+
const selectedNodes = nodes.filter((n) => n.selected);
|
| 865 |
+
const selectedEdges = edges.filter((e) => e.selected);
|
| 866 |
+
|
| 867 |
+
if (selectedNodes.length === 0) return;
|
| 868 |
+
|
| 869 |
+
window.__flowClipboard = {
|
| 870 |
+
nodes: selectedNodes.map(n => ({
|
| 871 |
+
...n,
|
| 872 |
+
originalPosition: { ...n.position }
|
| 873 |
+
})),
|
| 874 |
+
edges: selectedEdges
|
| 875 |
+
};
|
| 876 |
+
|
| 877 |
+
selectedNodes.forEach(node => {
|
| 878 |
+
setNodes((nds) => nds.filter((n) => n.id !== node.id));
|
| 879 |
+
if (broadcastMessageRef.current) {
|
| 880 |
+
broadcastMessageRef.current({
|
| 881 |
+
type: 'NODE_DELETE',
|
| 882 |
+
payload: { id: node.id }
|
| 883 |
+
});
|
| 884 |
+
}
|
| 885 |
+
});
|
| 886 |
+
|
| 887 |
+
selectedEdges.forEach(edge => {
|
| 888 |
+
setEdges((eds) => eds.filter((e) => e.id !== edge.id));
|
| 889 |
+
if (broadcastMessageRef.current) {
|
| 890 |
+
broadcastMessageRef.current({
|
| 891 |
+
type: 'EDGE_DELETE',
|
| 892 |
+
payload: { id: edge.id }
|
| 893 |
+
});
|
| 894 |
+
}
|
| 895 |
+
});
|
| 896 |
+
}, [nodes, edges, setNodes, setEdges, projectData]);
|
| 897 |
+
|
| 898 |
+
const handlePaste = useCallback((screenCoords = null) => {
|
| 899 |
+
if (projectData?.permission === 'Read') return;
|
| 900 |
+
if (!window.__flowClipboard || !window.__flowClipboard.nodes || window.__flowClipboard.nodes.length === 0) return;
|
| 901 |
+
|
| 902 |
+
const { nodes: copiedNodes, edges: copiedEdges } = window.__flowClipboard;
|
| 903 |
+
|
| 904 |
+
// De-select all current nodes/edges
|
| 905 |
+
setNodes(nds => nds.map(n => n.selected ? { ...n, selected: false } : n));
|
| 906 |
+
setEdges(eds => eds.map(e => e.selected ? { ...e, selected: false } : e));
|
| 907 |
+
|
| 908 |
+
const idMap = {};
|
| 909 |
+
const newNodes = [];
|
| 910 |
+
const newEdges = [];
|
| 911 |
+
|
| 912 |
+
let getPosition;
|
| 913 |
+
if (screenCoords) {
|
| 914 |
+
const flowMousePos = screenToFlowPosition({ x: screenCoords.x, y: screenCoords.y });
|
| 915 |
+
|
| 916 |
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
| 917 |
+
copiedNodes.forEach(n => {
|
| 918 |
+
if (n.position.x < minX) minX = n.position.x;
|
| 919 |
+
if (n.position.y < minY) minY = n.position.y;
|
| 920 |
+
if (n.position.x > maxX) maxX = n.position.x;
|
| 921 |
+
if (n.position.y > maxY) maxY = n.position.y;
|
| 922 |
+
});
|
| 923 |
+
const centerX = (minX + maxX) / 2;
|
| 924 |
+
const centerY = (minY + maxY) / 2;
|
| 925 |
+
|
| 926 |
+
getPosition = (n) => ({
|
| 927 |
+
x: flowMousePos.x + (n.position.x - centerX),
|
| 928 |
+
y: flowMousePos.y + (n.position.y - centerY)
|
| 929 |
+
});
|
| 930 |
+
} else {
|
| 931 |
+
const offset = 40;
|
| 932 |
+
getPosition = (n) => ({
|
| 933 |
+
x: n.position.x + offset,
|
| 934 |
+
y: n.position.y + offset
|
| 935 |
+
});
|
| 936 |
+
}
|
| 937 |
+
|
| 938 |
+
copiedNodes.forEach(node => {
|
| 939 |
+
const newId = crypto.randomUUID();
|
| 940 |
+
idMap[node.id] = newId;
|
| 941 |
+
|
| 942 |
+
const pastedNode = {
|
| 943 |
+
...node,
|
| 944 |
+
id: newId,
|
| 945 |
+
selected: true,
|
| 946 |
+
position: getPosition(node),
|
| 947 |
+
data: {
|
| 948 |
+
...node.data,
|
| 949 |
+
updateNodeData: updateNodeDataCallback
|
| 950 |
+
}
|
| 951 |
+
};
|
| 952 |
+
|
| 953 |
+
delete pastedNode.originalPosition;
|
| 954 |
+
newNodes.push(pastedNode);
|
| 955 |
+
});
|
| 956 |
+
|
| 957 |
+
copiedEdges.forEach(edge => {
|
| 958 |
+
const newSource = idMap[edge.source];
|
| 959 |
+
const newTarget = idMap[edge.target];
|
| 960 |
+
|
| 961 |
+
if (newSource && newTarget) {
|
| 962 |
+
const newEdgeId = `edge-${crypto.randomUUID()}`;
|
| 963 |
+
const pastedEdge = {
|
| 964 |
+
...edge,
|
| 965 |
+
id: newEdgeId,
|
| 966 |
+
source: newSource,
|
| 967 |
+
target: newTarget,
|
| 968 |
+
selected: true
|
| 969 |
+
};
|
| 970 |
+
newEdges.push(pastedEdge);
|
| 971 |
+
}
|
| 972 |
+
});
|
| 973 |
+
|
| 974 |
+
setNodes(nds => nds.concat(newNodes));
|
| 975 |
+
setEdges(eds => eds.concat(newEdges));
|
| 976 |
+
|
| 977 |
+
if (broadcastMessageRef.current) {
|
| 978 |
+
newNodes.forEach(n => {
|
| 979 |
+
broadcastMessageRef.current({
|
| 980 |
+
type: 'NODE_ADD',
|
| 981 |
+
payload: n
|
| 982 |
+
});
|
| 983 |
+
});
|
| 984 |
+
newEdges.forEach(e => {
|
| 985 |
+
broadcastMessageRef.current({
|
| 986 |
+
type: 'EDGE_ADD',
|
| 987 |
+
payload: e
|
| 988 |
+
});
|
| 989 |
+
});
|
| 990 |
+
}
|
| 991 |
+
|
| 992 |
+
// Update window.__flowClipboard positions so subsequent pastes cascade shift!
|
| 993 |
+
window.__flowClipboard.nodes = copiedNodes.map((n, idx) => ({
|
| 994 |
+
...n,
|
| 995 |
+
position: newNodes[idx].position
|
| 996 |
+
}));
|
| 997 |
+
}, [setNodes, setEdges, screenToFlowPosition, updateNodeDataCallback, projectData]);
|
| 998 |
+
|
| 999 |
const submitAICanvasPrompt = async (promptText) => {
|
| 1000 |
if (!promptText || !projectId) return false;
|
| 1001 |
setAiLoading(true);
|
|
|
|
| 1241 |
setShowPaywall,
|
| 1242 |
handleAIRequest,
|
| 1243 |
addNewNodeAt,
|
| 1244 |
+
handleCopy,
|
| 1245 |
+
handleCut,
|
| 1246 |
+
handlePaste,
|
| 1247 |
handleMouseDown,
|
| 1248 |
handleMouseUp,
|
| 1249 |
saveCanvas,
|
src/pages/CanvasPage.jsx
CHANGED
|
@@ -57,7 +57,7 @@ const getTransformForBoundsCustom = (bounds, width, height, minZoom, maxZoom, pa
|
|
| 57 |
import Sidebar from '../components/Sidebar';
|
| 58 |
import { useAuth } from '../context/AuthContext';
|
| 59 |
import { useTheme } from '../context/ThemeContext';
|
| 60 |
-
import { Maximize2, Play, Settings, Save, ChevronLeft, Loader2, Trash2, Target, GitFork, Image, X, Plus, Timer, Milestone, Share2, User } from 'lucide-react';
|
| 61 |
|
| 62 |
// Feature components
|
| 63 |
import SketchNode from '../features/canvas/components/nodes/SketchNode';
|
|
@@ -704,6 +704,9 @@ const CanvasInner = () => {
|
|
| 704 |
setShowPaywall,
|
| 705 |
handleAIRequest,
|
| 706 |
addNewNodeAt,
|
|
|
|
|
|
|
|
|
|
| 707 |
handleMouseDown,
|
| 708 |
handleMouseUp,
|
| 709 |
saveCanvas,
|
|
@@ -900,7 +903,7 @@ const CanvasInner = () => {
|
|
| 900 |
return () => window.removeEventListener('click', handleCloseMenu);
|
| 901 |
}, []);
|
| 902 |
|
| 903 |
-
// Handle global keyboard Delete/Backspace
|
| 904 |
useEffect(() => {
|
| 905 |
const handleKeyDown = (event) => {
|
| 906 |
// Ignore key events when typing in inputs/textareas
|
|
@@ -912,6 +915,27 @@ const CanvasInner = () => {
|
|
| 912 |
return;
|
| 913 |
}
|
| 914 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 915 |
if (event.key === 'Delete' || event.key === 'Backspace') {
|
| 916 |
if (permission === 'Read') return; // Read-only restriction!
|
| 917 |
|
|
@@ -929,7 +953,7 @@ const CanvasInner = () => {
|
|
| 929 |
|
| 930 |
window.addEventListener('keydown', handleKeyDown);
|
| 931 |
return () => window.removeEventListener('keydown', handleKeyDown);
|
| 932 |
-
}, [nodes, edges, deleteNode, deleteEdge, permission]);
|
| 933 |
|
| 934 |
const handleNodeContextMenu = useCallback(
|
| 935 |
(event, node) => {
|
|
@@ -1093,7 +1117,16 @@ const CanvasInner = () => {
|
|
| 1093 |
<CanvasToolbar
|
| 1094 |
handleAIRequest={handleAIRequest}
|
| 1095 |
isAIActive={isAIActive}
|
| 1096 |
-
user={user}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1097 |
/>
|
| 1098 |
)}
|
| 1099 |
|
|
@@ -1180,16 +1213,73 @@ const CanvasInner = () => {
|
|
| 1180 |
{contextMenu && contextMenu.type === 'node' && (
|
| 1181 |
<div
|
| 1182 |
style={{ top: contextMenu.y, left: contextMenu.x }}
|
| 1183 |
-
className="absolute bg-surface-container-lowest border-2 border-primary shadow-[2px_2px_0px_0px_var(--color-primary)] py-1 px-1 z-50 min-w-[
|
| 1184 |
onClick={(e) => e.stopPropagation()}
|
| 1185 |
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1186 |
<button
|
| 1187 |
onClick={(e) => {
|
| 1188 |
e.stopPropagation();
|
| 1189 |
deleteNode(contextMenu.nodeId);
|
| 1190 |
setContextMenu(null);
|
| 1191 |
}}
|
| 1192 |
-
className="w-full text-left px-2 py-1 hover:bg-red-500/20 text-red-600 hover:text-red-700 font-bold rounded-md transition-colors flex items-center
|
| 1193 |
>
|
| 1194 |
<Trash2 size={14} />
|
| 1195 |
<span>{t('canvas.context_menu.delete')}</span>
|
|
@@ -1205,6 +1295,22 @@ const CanvasInner = () => {
|
|
| 1205 |
>
|
| 1206 |
{permission !== 'Read' && (
|
| 1207 |
<>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1208 |
<div className="px-2.5 py-1 text-[9px] font-bold uppercase tracking-widest text-on-surface-variant/40 border-b border-primary/10">
|
| 1209 |
{t('canvas.context_menu.create_node')}
|
| 1210 |
</div>
|
|
@@ -1241,11 +1347,11 @@ const CanvasInner = () => {
|
|
| 1241 |
<Timer size={15} className="text-secondary" />
|
| 1242 |
<span>{t('canvas.context_menu.habit_node')}</span>
|
| 1243 |
</button>
|
| 1244 |
-
|
| 1245 |
<div className="border-t border-primary/10 my-0.5"></div>
|
| 1246 |
</>
|
| 1247 |
)}
|
| 1248 |
-
|
| 1249 |
<button
|
| 1250 |
onClick={(e) => {
|
| 1251 |
e.stopPropagation();
|
|
|
|
| 57 |
import Sidebar from '../components/Sidebar';
|
| 58 |
import { useAuth } from '../context/AuthContext';
|
| 59 |
import { useTheme } from '../context/ThemeContext';
|
| 60 |
+
import { Maximize2, Play, Settings, Save, ChevronLeft, Loader2, Trash2, Target, GitFork, Image, X, Plus, Timer, Milestone, Share2, User, Copy, Scissors, Clipboard } from 'lucide-react';
|
| 61 |
|
| 62 |
// Feature components
|
| 63 |
import SketchNode from '../features/canvas/components/nodes/SketchNode';
|
|
|
|
| 704 |
setShowPaywall,
|
| 705 |
handleAIRequest,
|
| 706 |
addNewNodeAt,
|
| 707 |
+
handleCopy,
|
| 708 |
+
handleCut,
|
| 709 |
+
handlePaste,
|
| 710 |
handleMouseDown,
|
| 711 |
handleMouseUp,
|
| 712 |
saveCanvas,
|
|
|
|
| 903 |
return () => window.removeEventListener('click', handleCloseMenu);
|
| 904 |
}, []);
|
| 905 |
|
| 906 |
+
// Handle global keyboard shortcuts: Copy/Cut/Paste/Delete/Backspace
|
| 907 |
useEffect(() => {
|
| 908 |
const handleKeyDown = (event) => {
|
| 909 |
// Ignore key events when typing in inputs/textareas
|
|
|
|
| 915 |
return;
|
| 916 |
}
|
| 917 |
|
| 918 |
+
// Ctrl+C / Cmd+C (Copy)
|
| 919 |
+
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'c') {
|
| 920 |
+
event.preventDefault();
|
| 921 |
+
handleCopy();
|
| 922 |
+
return;
|
| 923 |
+
}
|
| 924 |
+
|
| 925 |
+
// Ctrl+X / Cmd+X (Cut)
|
| 926 |
+
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'x') {
|
| 927 |
+
event.preventDefault();
|
| 928 |
+
handleCut();
|
| 929 |
+
return;
|
| 930 |
+
}
|
| 931 |
+
|
| 932 |
+
// Ctrl+V / Cmd+V (Paste)
|
| 933 |
+
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 'v') {
|
| 934 |
+
event.preventDefault();
|
| 935 |
+
handlePaste();
|
| 936 |
+
return;
|
| 937 |
+
}
|
| 938 |
+
|
| 939 |
if (event.key === 'Delete' || event.key === 'Backspace') {
|
| 940 |
if (permission === 'Read') return; // Read-only restriction!
|
| 941 |
|
|
|
|
| 953 |
|
| 954 |
window.addEventListener('keydown', handleKeyDown);
|
| 955 |
return () => window.removeEventListener('keydown', handleKeyDown);
|
| 956 |
+
}, [nodes, edges, deleteNode, deleteEdge, permission, handleCopy, handleCut, handlePaste]);
|
| 957 |
|
| 958 |
const handleNodeContextMenu = useCallback(
|
| 959 |
(event, node) => {
|
|
|
|
| 1117 |
<CanvasToolbar
|
| 1118 |
handleAIRequest={handleAIRequest}
|
| 1119 |
isAIActive={isAIActive}
|
| 1120 |
+
user={user}
|
| 1121 |
+
addNewNodeAt={addNewNodeAt}
|
| 1122 |
+
handleCopy={handleCopy}
|
| 1123 |
+
handleCut={handleCut}
|
| 1124 |
+
handlePaste={handlePaste}
|
| 1125 |
+
exportToPng={exportToPng}
|
| 1126 |
+
onDeleteSelected={() => {
|
| 1127 |
+
nodes.filter(n => n.selected).forEach(n => deleteNode(n.id));
|
| 1128 |
+
}}
|
| 1129 |
+
hasSelectedNodes={nodes.some(n => n.selected)}
|
| 1130 |
/>
|
| 1131 |
)}
|
| 1132 |
|
|
|
|
| 1213 |
{contextMenu && contextMenu.type === 'node' && (
|
| 1214 |
<div
|
| 1215 |
style={{ top: contextMenu.y, left: contextMenu.x }}
|
| 1216 |
+
className="absolute bg-surface-container-lowest border-2 border-primary shadow-[2px_2px_0px_0px_var(--color-primary)] py-1.5 px-1 z-50 min-w-[135px] rounded-lg transition-all flex flex-col gap-0.5"
|
| 1217 |
onClick={(e) => e.stopPropagation()}
|
| 1218 |
>
|
| 1219 |
+
<button
|
| 1220 |
+
onClick={(e) => {
|
| 1221 |
+
e.stopPropagation();
|
| 1222 |
+
// Ensure node is selected for copy if it isn't already
|
| 1223 |
+
const targetNode = nodes.find(n => n.id === contextMenu.nodeId);
|
| 1224 |
+
if (targetNode) {
|
| 1225 |
+
const isSelected = targetNode.selected;
|
| 1226 |
+
if (!isSelected) {
|
| 1227 |
+
setNodes(nds => nds.map(n => n.id === contextMenu.nodeId ? { ...n, selected: true } : n));
|
| 1228 |
+
}
|
| 1229 |
+
// Copy node(s)
|
| 1230 |
+
const selectedNodes = nodes.filter(n => n.selected || n.id === contextMenu.nodeId);
|
| 1231 |
+
const selectedEdges = edges.filter(edge =>
|
| 1232 |
+
selectedNodes.some(n => n.id === edge.source) &&
|
| 1233 |
+
selectedNodes.some(n => n.id === edge.target)
|
| 1234 |
+
);
|
| 1235 |
+
window.__flowClipboard = {
|
| 1236 |
+
nodes: selectedNodes.map(n => ({ ...n, originalPosition: { ...n.position } })),
|
| 1237 |
+
edges: selectedEdges
|
| 1238 |
+
};
|
| 1239 |
+
}
|
| 1240 |
+
setContextMenu(null);
|
| 1241 |
+
}}
|
| 1242 |
+
className="w-full text-left px-2.5 py-1.5 hover:bg-surface-variant text-primary font-bold rounded-md transition-colors flex items-center gap-2 text-xs"
|
| 1243 |
+
>
|
| 1244 |
+
<Copy size={14} className="text-primary" />
|
| 1245 |
+
<span>{t('canvas.context_menu.copy', 'Копіювати')}</span>
|
| 1246 |
+
</button>
|
| 1247 |
+
|
| 1248 |
+
<button
|
| 1249 |
+
onClick={(e) => {
|
| 1250 |
+
e.stopPropagation();
|
| 1251 |
+
// Cut node(s)
|
| 1252 |
+
const targetNode = nodes.find(n => n.id === contextMenu.nodeId);
|
| 1253 |
+
if (targetNode) {
|
| 1254 |
+
const selectedNodes = nodes.filter(n => n.selected || n.id === contextMenu.nodeId);
|
| 1255 |
+
const selectedEdges = edges.filter(edge =>
|
| 1256 |
+
selectedNodes.some(n => n.id === edge.source) &&
|
| 1257 |
+
selectedNodes.some(n => n.id === edge.target)
|
| 1258 |
+
);
|
| 1259 |
+
window.__flowClipboard = {
|
| 1260 |
+
nodes: selectedNodes.map(n => ({ ...n, originalPosition: { ...n.position } })),
|
| 1261 |
+
edges: selectedEdges
|
| 1262 |
+
};
|
| 1263 |
+
// Delete selected nodes
|
| 1264 |
+
selectedNodes.forEach(n => deleteNode(n.id));
|
| 1265 |
+
}
|
| 1266 |
+
setContextMenu(null);
|
| 1267 |
+
}}
|
| 1268 |
+
className="w-full text-left px-2.5 py-1.5 hover:bg-surface-variant text-primary font-bold rounded-md transition-colors flex items-center gap-2 text-xs"
|
| 1269 |
+
>
|
| 1270 |
+
<Scissors size={14} className="text-primary" />
|
| 1271 |
+
<span>{t('canvas.context_menu.cut', 'Вирізати')}</span>
|
| 1272 |
+
</button>
|
| 1273 |
+
|
| 1274 |
+
<div className="border-t border-primary/10 my-0.5"></div>
|
| 1275 |
+
|
| 1276 |
<button
|
| 1277 |
onClick={(e) => {
|
| 1278 |
e.stopPropagation();
|
| 1279 |
deleteNode(contextMenu.nodeId);
|
| 1280 |
setContextMenu(null);
|
| 1281 |
}}
|
| 1282 |
+
className="w-full text-left px-2.5 py-1.5 hover:bg-red-500/20 text-red-600 hover:text-red-700 font-bold rounded-md transition-colors flex items-center gap-2 text-xs"
|
| 1283 |
>
|
| 1284 |
<Trash2 size={14} />
|
| 1285 |
<span>{t('canvas.context_menu.delete')}</span>
|
|
|
|
| 1295 |
>
|
| 1296 |
{permission !== 'Read' && (
|
| 1297 |
<>
|
| 1298 |
+
{window.__flowClipboard && window.__flowClipboard.nodes && window.__flowClipboard.nodes.length > 0 && (
|
| 1299 |
+
<>
|
| 1300 |
+
<button
|
| 1301 |
+
onClick={(e) => {
|
| 1302 |
+
e.stopPropagation();
|
| 1303 |
+
handlePaste({ x: contextMenu.clientX, y: contextMenu.clientY });
|
| 1304 |
+
setContextMenu(null);
|
| 1305 |
+
}}
|
| 1306 |
+
className="w-full text-left px-2.5 py-1.5 hover:bg-surface-variant text-primary font-bold rounded-md transition-colors flex items-center gap-2 text-xs"
|
| 1307 |
+
>
|
| 1308 |
+
<Clipboard size={15} className="text-primary" />
|
| 1309 |
+
<span>{t('canvas.context_menu.paste', 'Вставити')}</span>
|
| 1310 |
+
</button>
|
| 1311 |
+
<div className="border-t border-primary/10 my-0.5"></div>
|
| 1312 |
+
</>
|
| 1313 |
+
)}
|
| 1314 |
<div className="px-2.5 py-1 text-[9px] font-bold uppercase tracking-widest text-on-surface-variant/40 border-b border-primary/10">
|
| 1315 |
{t('canvas.context_menu.create_node')}
|
| 1316 |
</div>
|
|
|
|
| 1347 |
<Timer size={15} className="text-secondary" />
|
| 1348 |
<span>{t('canvas.context_menu.habit_node')}</span>
|
| 1349 |
</button>
|
| 1350 |
+
|
| 1351 |
<div className="border-t border-primary/10 my-0.5"></div>
|
| 1352 |
</>
|
| 1353 |
)}
|
| 1354 |
+
|
| 1355 |
<button
|
| 1356 |
onClick={(e) => {
|
| 1357 |
e.stopPropagation();
|