Spaces:
Running
Running
Commit ·
83904fc
1
Parent(s): 4133e4e
feat: implement image and text pasting on canvas via Ctrl+V
Browse files1575 unmodified lines1576157715781579158015816 unmodified lines158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163928 unmodified lines1668166916701671167216731674167516761677167834 unmodified lines17131714171517161717171817191575 unmodified linesreturn () => window.removeEventListener('click', handleCloseMenu);}, []);// Keyboard and Global Event HandlersuseEffect(() => {const handlePasteEvent = (event) => {6 unmodified linesconst clipboardData = event.clipboardData || window.clipboardData;if (!clipboardData) return;const items = clipboardData.items;let imageFound = false;for (const item of items) {if (item.type.indexOf('image') !== -1) {imageFound = true;event.preventDefault();const blob = item.getAsFile();const reader = new FileReader();reader.onload = (e) => {const img = new Image();img.onload = () => {const canvas = document.createElement('canvas');let width = img.width;let height = img.height;const MAX_SIZE = 1200;if (width > height && width > MAX_SIZE) {height *= MAX_SIZE / width; width = MAX_SIZE;} else if (height > MAX_SIZE) {width *= MAX_SIZE / height; height = MAX_SIZE;}canvas.width = width; canvas.height = height;const ctx = canvas.getContext('2d');ctx.drawImage(img, 0, 0, width, height);const compressedBase64 = canvas.toDataURL('image/jpeg', 0.8);addNewNodeAt(window.__lastMouseX, window.__lastMouseY, 'image', { url: compressedBase64 });};img.src = e.target.result;};reader.readAsDataURL(blob);break;}}if (!imageFound) {const text = clipboardData.getData('text/plain');if (text && text.trim()) {event.preventDefault();addNewNodeAt(window.__lastMouseX, window.__lastMouseY, 'text', { label: text });}}};window.addEventListener('paste', handlePasteEvent);return () => window.removeEventListener('paste', handlePasteEvent);}, [permission, addNewNodeAt]);// Handle global keyboard shortcuts: Copy/Cut/Paste/Delete/BackspaceuseEffect(() => {28 unmodified lines// Ctrl+V / Cmd+V (Paste)if ((event.ctrlKey || event.metaKey) && (event.code === 'KeyV' || keyLower === 'v' || keyLower === 'м')) {// Only prevent default if we have internal nodes to paste// Otherwise, let the 'paste' event fire for system clipboardif (hasClipboardContent) {event.preventDefault();event.stopPropagation();handlePaste(mousePosRef.current);}return;}34 unmodified lineswindow.addEventListener('keydown', handleKeyDown, { capture: true });return () => window.removeEventListener('keydown', handleKeyDown, { capture: true });}, [nodes, edges, deleteNode, deleteEdge, permission, handleCopy, handleCut, handlePaste, undo, redo, requestDeletion]);const handleNodeContextMenu = useCallback((event, node) => {1575 unmodified lines1576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516266 unmodified lines163316341635163616371638163916401641164216431644164528 unmodified lines1674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171134 unmodified lines17461747174817491750175117521575 unmodified linesreturn () => window.removeEventListener('click', handleCloseMenu);}, []);const processClipboardData = useCallback((clipboardData, clientX, clientY) => {if (permission === 'Read') return;if (!clipboardData) return;const items = clipboardData.items;let imageFound = false;for (const item of items) {if (item.type.indexOf('image') !== -1) {imageFound = true;const blob = item.getAsFile();const reader = new FileReader();reader.onload = (e) => {const img = new Image();img.onload = () => {const canvas = document.createElement('canvas');let width = img.width;let height = img.height;const MAX_SIZE = 1200;if (width > height && width > MAX_SIZE) {height *= MAX_SIZE / width; width = MAX_SIZE;} else if (height > MAX_SIZE) {width *= MAX_SIZE / height; height = MAX_SIZE;}canvas.width = width; canvas.height = height;const ctx = canvas.getContext('2d');ctx.drawImage(img, 0, 0, width, height);const compressedBase64 = canvas.toDataURL('image/jpeg', 0.8);addNewNodeAt(clientX, clientY, 'image', { url: compressedBase64 });};img.src = e.target.result;};reader.readAsDataURL(blob);break;}}if (!imageFound) {const text = clipboardData.getData('text/plain');if (text && text.trim()) {addNewNodeAt(clientX, clientY, 'text', { label: text });}}}, [permission, addNewNodeAt]);// Keyboard and Global Event HandlersuseEffect(() => {const handlePasteEvent = (event) => {6 unmodified linesconst clipboardData = event.clipboardData || window.clipboardData;if (!clipboardData) return;event.preventDefault();processClipboardData(clipboardData, window.__lastMouseX, window.__lastMouseY);};window.addEventListener('paste', handlePasteEvent);return () => window.removeEventListener('paste', handlePasteEvent);}, [permission, processClipboardData]);// Handle global keyboard shortcuts: Copy/Cut/Paste/Delete/BackspaceuseEffect(() => {28 unmodified lines// Ctrl+V / Cmd+V (Paste)if ((event.ctrlKey || event.metaKey) && (event.code === 'KeyV' || keyLower === 'v' || keyLower === 'м')) {// Only prevent default if we have internal nodes to paste// Otherwise, let the 'paste' event fire for system clipboard via focus catcherif (hasClipboardContent) {event.preventDefault();event.stopPropagation();handlePaste(mousePosRef.current);} else {if (permission !== 'Read') {const activeEl = document.activeElement;const tempEditable = document.createElement('div');tempEditable.contentEditable = 'true';tempEditable.style.position = 'fixed';tempEditable.style.left = '-9999px';tempEditable.style.top = '-9999px';document.body.appendChild(tempEditable);tempEditable.addEventListener('paste', (e) => {e.preventDefault();e.stopPropagation();processClipboardData(e.clipboardData || window.clipboardData, window.__lastMouseX, window.__lastMouseY);setTimeout(() => {if (tempEditable.parentNode) {document.body.removeChild(tempEditable);}if (activeEl && typeof activeEl.focus === 'function') {activeEl.focus();}}, 0);}, { once: true });tempEditable.focus();}}return;}34 unmodified lineswindow.addEventListener('keydown', handleKeyDown, { capture: true });return () => window.removeEventListener('keydown', handleKeyDown, { capture: true });}, [nodes, edges, deleteNode, deleteEdge, permission, handleCopy, handleCut, handlePaste, undo, redo, requestDeletion, processClipboardData]);const handleNodeContextMenu = useCallback((event, node) => {
src/pages/CanvasPage.jsx+77-44