Skip to content

Commit

Permalink
Merge pull request #673 from bgareth/context-paste_clipboard-fallback
Browse files Browse the repository at this point in the history
added fallback for clipboard copy and context menu paste
  • Loading branch information
sanchit3008 authored Feb 17, 2025
2 parents 5a786e9 + d4a4e28 commit 0cc1827
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/modules/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export default class clipboard {
ele.focus({ preventScroll: true });
document.execCommand("selectAll");
document.execCommand("copy");

// Fallback setup
const plainText = ele.innerText || ele.textContent || ""; // In order to match the clipboard which only stores the visible text without formatting
sessionStorage.setItem("localClipboard", plainText); // Also store in sessionStorage for fallback access

setTimeout(() => {
ele?.blur();
previouslyFocusedElement?.focus?.();
Expand Down
17 changes: 15 additions & 2 deletions packages/react/src/components/ContextMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,22 @@ const ContextMenu: React.FC = () => {
<Menu
key={name}
onClick={async () => {
const clipboardText = await navigator.clipboard.readText();
let clipboardText = "";
const sessionClipboardText =
sessionStorage.getItem("localClipboard") || "";

try {
clipboardText = await navigator.clipboard.readText();
} catch (err) {
console.warn(
"Clipboard access blocked. Attempting to use sessionStorage fallback."
);
}

const finalText = clipboardText || sessionClipboardText;

setContext((draftCtx) => {
handlePasteByClick(draftCtx, clipboardText);
handlePasteByClick(draftCtx, finalText);
draftCtx.contextMenu = {};
});
}}
Expand Down

0 comments on commit 0cc1827

Please sign in to comment.