File size: 1,703 Bytes
420bd16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import React from 'react';
export const NodeEditModal = ({
editValue,
setEditValue,
handleEditSave,
handleEditCancel
}) => {
return (
<div className="node-edit-modal">
<textarea
value={editValue}
onChange={(e) => setEditValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleEditSave();
}
}}
autoFocus
/>
<div className="edit-buttons">
<button onClick={handleEditSave}>Save</button>
<button onClick={handleEditCancel}>Cancel</button>
</div>
</div>
);
};
export const handleNodeDoubleClick = (event, node, setEditingNode, setEditValue, setIsEditing) => {
event.preventDefault();
setEditingNode(node);
setEditValue(node.data.label);
setIsEditing(true);
};
export const handleEditSave = (editingNode, editValue, setNodes, setIsEditing, setEditingNode, setEditValue) => {
if (!editingNode) return;
setNodes(nds =>
nds.map(n => {
if (n.id === editingNode.id) {
return {
...n,
data: { ...n.data, label: editValue }
};
}
return n;
})
);
setIsEditing(false);
setEditingNode(null);
setEditValue('');
};
export const handleNodeDragStop = (event, node, setNodes, SNAP_GRID) => {
const newX = Math.round(node.position.x / SNAP_GRID[0]) * SNAP_GRID[0];
const newY = Math.round(node.position.y / SNAP_GRID[1]) * SNAP_GRID[1];
setNodes(nds =>
nds.map(n => {
if (n.id === node.id) {
return {
...n,
position: { x: newX, y: newY }
};
}
return n;
})
);
}; |