Spaces:
Running
Running
Commit ·
04bb786
1
Parent(s): 4555b70
feat: improve clipboard paste to support text and refine image handling
Browse files1315 unmodified lines131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621 unmodified line13641365136613671368136910 unmodified lines138013811382138313841385138613871315 unmodified linesif (permission === 'Read') return;// Don't paste if user is typing in an input/textareaif (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') return;const items = (event.clipboardData || event.originalEvent.clipboardData).items;for (const item of items) {if (item.type.indexOf('image') !== -1) {const blob = item.getAsFile();const reader = new FileReader();reader.onload = (e) => {const img = new Image();img.onload = () => {// Create canvas for resizingconst canvas = document.createElement('canvas');let width = img.width;let height = img.height;// Max dimensionsconst MAX_WIDTH = 1200;const MAX_HEIGHT = 1200;if (width > height) {if (width > MAX_WIDTH) {height *= MAX_WIDTH / width;width = MAX_WIDTH;}} else {if (height > MAX_HEIGHT) {width *= MAX_HEIGHT / height;height = MAX_HEIGHT;}}canvas.width = width;canvas.height = height;const ctx = canvas.getContext('2d');ctx.drawImage(img, 0, 0, width, height);// Compress as JPEGconst compressedBase64 = canvas.toDataURL('image/jpeg', 0.8);// Get current mouse position if possible, otherwise center of screenconst x = window.__lastMouseX || window.innerWidth / 2;const y = window.__lastMouseY || window.innerHeight / 2;addNewNodeAt(x, y, 'image', { url: compressedBase64 });1 unmodified lineimg.src = e.target.result;};reader.readAsDataURL(blob);}}};10 unmodified lineswindow.removeEventListener('paste', handlePasteEvent);window.removeEventListener('mousemove', handleMouseMoveGlobal);};}, [permission, addNewNodeAt]);const lastCursorSendRef = useRef(0);const broadcastRef = useRef(broadcastMessage);useEffect(() => {1315 unmodified lines13161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581 unmodified line136013611362136313641365136613671368136913701371137213731374137513761377137810 unmodified lines13891390139113921393139413951315 unmodified linesif (permission === 'Read') return;// Don't paste if user is typing in an input/textareaconst targetTag = event.target.tagName;if (targetTag === 'INPUT' || targetTag === 'TEXTAREA' || event.target.isContentEditable) return;const clipboardData = event.clipboardData || window.clipboardData;if (!clipboardData) return;const items = clipboardData.items;let imageFound = false;// First check for imagesfor (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);const x = window.__lastMouseX || window.innerWidth / 2;const y = window.__lastMouseY || window.innerHeight / 2;addNewNodeAt(x, y, 'image', { url: compressedBase64 });1 unmodified lineimg.src = e.target.result;};reader.readAsDataURL(blob);event.preventDefault();break; // Only paste one image at a time}}// If no image, try to paste textif (!imageFound) {const text = clipboardData.getData('text/plain');if (text && text.trim()) {const x = window.__lastMouseX || window.innerWidth / 2;const y = window.__lastMouseY || window.innerHeight / 2;addNewNodeAt(x, y, 'text', { label: text });event.preventDefault();}}};10 unmodified lineswindow.removeEventListener('paste', handlePasteEvent);window.removeEventListener('mousemove', handleMouseMoveGlobal);};}, [permission, addNewNodeAt]); const lastCursorSendRef = useRef(0);const broadcastRef = useRef(broadcastMessage);useEffect(() => {
src/pages/CanvasPage.jsx+33-25