Spaces:
Running
Running
Commit ·
04bb786
1
Parent(s): 4555b70
feat: improve clipboard paste to support text and refine image handling
Browse files1315 unmodified lines1316131713181319131913201321132113221323132413251326132713281329133013311332133313341335133613301337133813391334133513361337133813391340134113421343134413451346134713481340134113421343134413451346134713481350134913501351135213531355135613541358135913551356135713581 unmodified line136013611362136313641365136613671368136913701371137213731374137513761377137810 unmodified lines1389139013911383138413921393139413951315 unmodified lines if (permission === 'Read') return;
// Don't paste if user is typing in an input/textarea if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') return; const targetTag = event.target.tagName; if (targetTag === 'INPUT' || targetTag === 'TEXTAREA' || event.target.isContentEditable) return;
const items = (event.clipboardData || event.originalEvent.clipboardData).items; const clipboardData = event.clipboardData || window.clipboardData; if (!clipboardData) return;
const items = clipboardData.items; let imageFound = false;
// First check for images 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 = () => { // Create canvas for resizing const canvas = document.createElement('canvas'); let width = img.width; let height = img.height; // Max dimensions const 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; } 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); // Compress as JPEG const compressedBase64 = canvas.toDataURL('image/jpeg', 0.8); // Get current mouse position if possible, otherwise center of screen
const x = window.__lastMouseX || window.innerWidth / 2; const y = window.__lastMouseY || window.innerHeight / 2; addNewNodeAt(x, y, 'image', { url: compressedBase64 });1 unmodified line img.src = e.target.result; }; reader.readAsDataURL(blob); event.preventDefault(); break; // Only paste one image at a time } }
// If no image, try to paste text if (!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 lines window.removeEventListener('paste', handlePasteEvent); window.removeEventListener('mousemove', handleMouseMoveGlobal); }; }, [permission, addNewNodeAt]); const lastCursorSendRef = useRef(0); }, [permission, addNewNodeAt]); const lastCursorSendRef = useRef(0); const broadcastRef = useRef(broadcastMessage); useEffect(() => {src/pages/CanvasPage.jsx+33-25