Skip to content

Commit

Permalink
fix: fallback on readText permisisons
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick committed Jan 15, 2025
1 parent 2697144 commit 7b8e7cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
27 changes: 17 additions & 10 deletions frontend/src/components/editor/cell/cell-context-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { CellOutputId } from "@/core/cells/ids";
import { Logger } from "@/utils/Logger";
import { copyToClipboard } from "@/utils/copy";
import { toast } from "@/components/ui/use-toast";
import { sendToPanelManager } from "@/core/vscode/vscode-bindings";

interface Props extends CellActionButtonProps {
children: React.ReactNode;
Expand Down Expand Up @@ -81,16 +82,22 @@ export const CellActionsContextMenu = ({ children, ...props }: Props) => {
}
// We can't use the native browser paste since we don't have focus
// so instead we use the editorViewView
const clipText = await navigator.clipboard.readText();
if (clipText) {
// Get the current selection, or the start of the document if nothing is selected
const { from, to } = editorView.state.selection.main;
// Create a new transaction that replaces the selection with the clipboard text
const tr = editorView.state.update({
changes: { from, to, insert: clipText },
});
// Apply the transaction
editorView.dispatch(tr);
try {
const clipText = await navigator.clipboard.readText();
if (clipText) {
// Get the current selection, or the start of the document if nothing is selected
const { from, to } = editorView.state.selection.main;
// Create a new transaction that replaces the selection with the clipboard text
const tr = editorView.state.update({
changes: { from, to, insert: clipText },
});
// Apply the transaction
editorView.dispatch(tr);
}
} catch (error) {
Logger.error("Failed to paste from clipboard", error);
// Try vscode or other parent
sendToPanelManager({ command: "paste" });
}
},
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/core/vscode/vscode-bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function registerContextMenu() {
});
}

function sendToPanelManager(msg: VscodeMessage) {
export function sendToPanelManager(msg: VscodeMessage) {
window.parent?.postMessage(msg, "*");
}

Expand Down

0 comments on commit 7b8e7cc

Please sign in to comment.