Skip to content

Commit 41e41c2

Browse files
committed
fix(paste): route the paste shortcut through a hidden textarea so engines that only paste into editable elements still paste into the grid
1 parent 3aa96f2 commit 41e41c2

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎lib/hooks/core/useGridClipboardPaste.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,41 @@ export function useGridClipboardPaste<R extends GridRowModel>(params: UseGridCli
216216
const kind = classifyKeyTarget(event.target, viewport);
217217
return kind === 'grid' || kind === 'control';
218218
};
219+
// Some engines (WebKit on Linux) only fire `paste` on an editable element, never on a focused
220+
// cell. On the paste shortcut, focus moves to an off-screen textarea for that one event, so the
221+
// browser's paste lands there and bubbles to the root; focus then goes back to the cell.
222+
let capture: { textarea: HTMLTextAreaElement; restore: HTMLElement | null; timer: number } | null = null;
223+
const endCapture = () => {
224+
if (!capture) return;
225+
const { textarea, restore, timer } = capture;
226+
capture = null;
227+
window.clearTimeout(timer);
228+
const hadFocus = document.activeElement === textarea;
229+
textarea.remove();
230+
if (hadFocus && restore && restore.isConnected) restore.focus({ preventScroll: true });
231+
};
232+
const startCapture = () => {
233+
endCapture();
234+
const textarea = document.createElement('textarea');
235+
textarea.setAttribute('aria-hidden', 'true');
236+
textarea.tabIndex = -1;
237+
textarea.className = 'ogx__paste-capture';
238+
textarea.style.cssText = 'position:fixed;top:0;left:-10000px;width:1px;height:1px;opacity:0;';
239+
root.appendChild(textarea);
240+
const restore = document.activeElement instanceof HTMLElement ? document.activeElement : null;
241+
// No paste event within the timeout (an empty or non-text clipboard): give focus back anyway.
242+
capture = { textarea, restore, timer: window.setTimeout(endCapture, 250) };
243+
textarea.focus({ preventScroll: true });
244+
};
245+
const isCaptureTarget = (event: Event) => capture !== null && event.target === capture.textarea;
219246
const onPaste = (event: ClipboardEvent) => {
247+
if (isCaptureTarget(event)) {
248+
const text = event.clipboardData ? event.clipboardData.getData('text/plain') : '';
249+
event.preventDefault();
250+
endCapture();
251+
if (text !== '') void paste(text, undefined, true);
252+
return;
253+
}
220254
if (!pasteEnabled || !ownsTarget(event)) return;
221255
if (!latestRef.current.navigationColumns.some(c => c.editable)) return;
222256
const text = event.clipboardData ? event.clipboardData.getData('text/plain') : '';
@@ -225,6 +259,10 @@ export function useGridClipboardPaste<R extends GridRowModel>(params: UseGridCli
225259
void paste(text, undefined, true);
226260
};
227261
const onKeyDown = (event: KeyboardEvent) => {
262+
if ((event.ctrlKey || event.metaKey) && !event.altKey && !event.shiftKey && (event.key === 'v' || event.key === 'V' || event.code === 'KeyV')) {
263+
if (pasteEnabled && ownsTarget(event) && latestRef.current.navigationColumns.some(c => c.editable)) startCapture();
264+
return;
265+
}
228266
if (!clearEnabled || (event.key !== 'Delete' && event.key !== 'Backspace')) return;
229267
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return;
230268
if (!ownsTarget(event)) return;
@@ -233,6 +271,7 @@ export function useGridClipboardPaste<R extends GridRowModel>(params: UseGridCli
233271
root.addEventListener('paste', onPaste);
234272
root.addEventListener('keydown', onKeyDown);
235273
return () => {
274+
endCapture();
236275
root.removeEventListener('paste', onPaste);
237276
root.removeEventListener('keydown', onKeyDown);
238277
};

0 commit comments

Comments
 (0)