Spaces:
Running
Running
Commit ·
04bb786
1
Parent(s): 4555b70
feat: improve clipboard paste to support text and refine image handling
Browse files- src/pages/CanvasPage.jsx +33 -25
src/pages/CanvasPage.jsx
CHANGED
|
@@ -1316,47 +1316,43 @@ const CanvasInner = () => {
|
|
| 1316 |
if (permission === 'Read') return;
|
| 1317 |
|
| 1318 |
// Don't paste if user is typing in an input/textarea
|
| 1319 |
-
|
|
|
|
| 1320 |
|
| 1321 |
-
const
|
|
|
|
| 1322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1323 |
for (const item of items) {
|
| 1324 |
if (item.type.indexOf('image') !== -1) {
|
|
|
|
| 1325 |
const blob = item.getAsFile();
|
| 1326 |
const reader = new FileReader();
|
| 1327 |
reader.onload = (e) => {
|
| 1328 |
const img = new Image();
|
| 1329 |
img.onload = () => {
|
| 1330 |
-
// Create canvas for resizing
|
| 1331 |
const canvas = document.createElement('canvas');
|
| 1332 |
let width = img.width;
|
| 1333 |
let height = img.height;
|
| 1334 |
-
|
| 1335 |
-
|
| 1336 |
-
|
| 1337 |
-
|
| 1338 |
-
|
| 1339 |
-
if (
|
| 1340 |
-
|
| 1341 |
-
|
| 1342 |
-
width = MAX_WIDTH;
|
| 1343 |
-
}
|
| 1344 |
-
} else {
|
| 1345 |
-
if (height > MAX_HEIGHT) {
|
| 1346 |
-
width *= MAX_HEIGHT / height;
|
| 1347 |
-
height = MAX_HEIGHT;
|
| 1348 |
-
}
|
| 1349 |
}
|
| 1350 |
-
|
| 1351 |
canvas.width = width;
|
| 1352 |
canvas.height = height;
|
| 1353 |
const ctx = canvas.getContext('2d');
|
| 1354 |
ctx.drawImage(img, 0, 0, width, height);
|
| 1355 |
-
|
| 1356 |
-
// Compress as JPEG
|
| 1357 |
const compressedBase64 = canvas.toDataURL('image/jpeg', 0.8);
|
| 1358 |
-
|
| 1359 |
-
// Get current mouse position if possible, otherwise center of screen
|
| 1360 |
const x = window.__lastMouseX || window.innerWidth / 2;
|
| 1361 |
const y = window.__lastMouseY || window.innerHeight / 2;
|
| 1362 |
addNewNodeAt(x, y, 'image', { url: compressedBase64 });
|
|
@@ -1364,6 +1360,19 @@ const CanvasInner = () => {
|
|
| 1364 |
img.src = e.target.result;
|
| 1365 |
};
|
| 1366 |
reader.readAsDataURL(blob);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1367 |
}
|
| 1368 |
}
|
| 1369 |
};
|
|
@@ -1380,8 +1389,7 @@ const CanvasInner = () => {
|
|
| 1380 |
window.removeEventListener('paste', handlePasteEvent);
|
| 1381 |
window.removeEventListener('mousemove', handleMouseMoveGlobal);
|
| 1382 |
};
|
| 1383 |
-
}, [permission, addNewNodeAt]);
|
| 1384 |
-
const lastCursorSendRef = useRef(0);
|
| 1385 |
const broadcastRef = useRef(broadcastMessage);
|
| 1386 |
|
| 1387 |
useEffect(() => {
|
|
|
|
| 1316 |
if (permission === 'Read') return;
|
| 1317 |
|
| 1318 |
// Don't paste if user is typing in an input/textarea
|
| 1319 |
+
const targetTag = event.target.tagName;
|
| 1320 |
+
if (targetTag === 'INPUT' || targetTag === 'TEXTAREA' || event.target.isContentEditable) return;
|
| 1321 |
|
| 1322 |
+
const clipboardData = event.clipboardData || window.clipboardData;
|
| 1323 |
+
if (!clipboardData) return;
|
| 1324 |
|
| 1325 |
+
const items = clipboardData.items;
|
| 1326 |
+
let imageFound = false;
|
| 1327 |
+
|
| 1328 |
+
// First check for images
|
| 1329 |
for (const item of items) {
|
| 1330 |
if (item.type.indexOf('image') !== -1) {
|
| 1331 |
+
imageFound = true;
|
| 1332 |
const blob = item.getAsFile();
|
| 1333 |
const reader = new FileReader();
|
| 1334 |
reader.onload = (e) => {
|
| 1335 |
const img = new Image();
|
| 1336 |
img.onload = () => {
|
|
|
|
| 1337 |
const canvas = document.createElement('canvas');
|
| 1338 |
let width = img.width;
|
| 1339 |
let height = img.height;
|
| 1340 |
+
const MAX_SIZE = 1200;
|
| 1341 |
+
|
| 1342 |
+
if (width > height && width > MAX_SIZE) {
|
| 1343 |
+
height *= MAX_SIZE / width;
|
| 1344 |
+
width = MAX_SIZE;
|
| 1345 |
+
} else if (height > MAX_SIZE) {
|
| 1346 |
+
width *= MAX_SIZE / height;
|
| 1347 |
+
height = MAX_SIZE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1348 |
}
|
| 1349 |
+
|
| 1350 |
canvas.width = width;
|
| 1351 |
canvas.height = height;
|
| 1352 |
const ctx = canvas.getContext('2d');
|
| 1353 |
ctx.drawImage(img, 0, 0, width, height);
|
|
|
|
|
|
|
| 1354 |
const compressedBase64 = canvas.toDataURL('image/jpeg', 0.8);
|
| 1355 |
+
|
|
|
|
| 1356 |
const x = window.__lastMouseX || window.innerWidth / 2;
|
| 1357 |
const y = window.__lastMouseY || window.innerHeight / 2;
|
| 1358 |
addNewNodeAt(x, y, 'image', { url: compressedBase64 });
|
|
|
|
| 1360 |
img.src = e.target.result;
|
| 1361 |
};
|
| 1362 |
reader.readAsDataURL(blob);
|
| 1363 |
+
event.preventDefault();
|
| 1364 |
+
break; // Only paste one image at a time
|
| 1365 |
+
}
|
| 1366 |
+
}
|
| 1367 |
+
|
| 1368 |
+
// If no image, try to paste text
|
| 1369 |
+
if (!imageFound) {
|
| 1370 |
+
const text = clipboardData.getData('text/plain');
|
| 1371 |
+
if (text && text.trim()) {
|
| 1372 |
+
const x = window.__lastMouseX || window.innerWidth / 2;
|
| 1373 |
+
const y = window.__lastMouseY || window.innerHeight / 2;
|
| 1374 |
+
addNewNodeAt(x, y, 'text', { label: text });
|
| 1375 |
+
event.preventDefault();
|
| 1376 |
}
|
| 1377 |
}
|
| 1378 |
};
|
|
|
|
| 1389 |
window.removeEventListener('paste', handlePasteEvent);
|
| 1390 |
window.removeEventListener('mousemove', handleMouseMoveGlobal);
|
| 1391 |
};
|
| 1392 |
+
}, [permission, addNewNodeAt]); const lastCursorSendRef = useRef(0);
|
|
|
|
| 1393 |
const broadcastRef = useRef(broadcastMessage);
|
| 1394 |
|
| 1395 |
useEffect(() => {
|