A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
add event listeners to send copy/paste to vscode (#94)
Myles Scolnickefa3ca778670
diff --git a/frontend/src/core/vscode/vscode-bindings.ts b/frontend/src/core/vscode/vscode-bindings.tsnew file mode 100644index 0000000000..568eb9eddc--- /dev/null+++ b/frontend/src/core/vscode/vscode-bindings.ts@@ -0,0 +1,134 @@+/* Copyright 2023 Marimo. All rights reserved. */++import { Logger } from "@/utils/Logger";++// eslint-disable-next-line ssr-friendly/no-dom-globals-in-module-scope+const isEmbedded = typeof window !== "undefined" && window.parent !== window;++// To enable keyboard shortcuts of VS Code when the iframe is focused,+// we have to dispatch keyboard events in the parent window.+// See https://github.com/microsoft/vscode/issues/65452#issuecomment-586036474+export function maybeRegisterVSCodeBindings() {+ if (!isEmbedded) {+ return;+ }+ Logger.log("Registering VS Code bindings");+ registerKeyboard();+ registerCopyPaste();+ registerOpenExternalLink();+ registerContextMenu();+}++function registerCopyPaste() {+ window.addEventListener("copy", () => {+ const selection = window.getSelection()?.toString() ?? "";+ sendToPanelManager({+ command: "copy",+ text: selection,+ });+ });++ window.addEventListener("cut", () => {+ const selection = window.getSelection()?.toString() ?? "";+ // clear+ document.execCommand("insertText", false, "");+ sendToPanelManager({+ command: "cut",+ text: selection,+ });+ });++ window.addEventListener("message", (event) => {+ const message = event.data;+ switch (message.command) {+ case "paste":+ document.execCommand("insertText", false, message.text);+ return;+ }+ });+}++function registerKeyboard() {+ document.addEventListener("keydown", (event) => {+ // Copy+ if ((event.ctrlKey || event.metaKey) && event.key === "c") {+ const selection = window.getSelection()?.toString() ?? "";+ sendToPanelManager({+ command: "copy",+ text: selection,+ });+ return;+ }+ // Cut+ if ((event.ctrlKey || event.metaKey) && event.key === "x") {+ const selection = window.getSelection()?.toString() ?? "";+ // clear+ document.execCommand("insertText", false, "");+ sendToPanelManager({+ command: "cut",+ text: selection,+ });+ return;+ }+ // Paste+ if ((event.ctrlKey || event.metaKey) && event.key === "v") {+ sendToPanelManager({+ command: "paste",+ });+ return;+ }+ });+}++function registerOpenExternalLink() {+ document.addEventListener("click", (event) => {+ const target = event.target as HTMLElement;+ if (target.tagName !== "A") {+ return;+ }+ const href = target.getAttribute("href");+ if (!href) {+ return;+ }+ if (href.startsWith("http://") || href.startsWith("https://")) {+ event.preventDefault();+ sendToPanelManager({+ command: "external_link",+ url: href,+ });+ }+ });+}++function registerContextMenu() {+ document.addEventListener("contextmenu", (event) => {+ event.preventDefault();+ sendToPanelManager({+ command: "context_menu",+ });+ });+}++function sendToPanelManager(msg: VscodeMessage) {+ window.parent?.postMessage(msg, "*");+}++export type VscodeMessage =+ | {+ command: "context_menu";+ }+ | {+ command: "paste";+ }+ | {+ command: "copy";+ text: string;+ }+ | {+ command: "cut";+ text: string;+ }+ | {+ command: "external_link";+ url: string;+ };diff --git a/frontend/src/main.tsx b/frontend/src/main.tsxindex 9775c44729..1d109be20c 100644--- a/frontend/src/main.tsx+++ b/frontend/src/main.tsx@@ -9,6 +9,9 @@ import { Logger } from "./utils/Logger";import { reportVitals } from "./utils/vitals";import { Provider } from "jotai";import { store } from "./core/state/jotai";+import { maybeRegisterVSCodeBindings } from "./core/vscode/vscode-bindings";++maybeRegisterVSCodeBindings();/*** Main entry point for the Marimo app.