Skip to content

Commit

Permalink
🐛 fix copy/paste/cut behavior in VS Code notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sestinj committed May 2, 2024
1 parent 755e144 commit 1b30b9e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
8 changes: 5 additions & 3 deletions extensions/vscode/src/debugPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ConfigHandler } from "core/config/handler";
import * as vscode from "vscode";
import { VerticalPerLineDiffManager } from "./diff/verticalPerLine/manager";
import { getTheme } from "./util/getTheme";
import { getExtensionVersion } from "./util/util";
import { getExtensionUri, getNonce, getUniqueId } from "./util/vscode";
import { VsCodeWebviewProtocol } from "./webviewProtocol";

Expand Down Expand Up @@ -34,10 +35,10 @@ export class ContinueGUIWebviewViewProvider
}

public resetWebviewProtocolWebview(): void {
if (this._webview) {
if (this._webview) {
this.webviewProtocol.webview = this._webview;
} else{
console.warn("no webview found during reset")
} else {
console.warn("no webview found during reset");
}
}

Expand Down Expand Up @@ -148,6 +149,7 @@ export class ContinueGUIWebviewViewProvider
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
<script>localStorage.setItem("ide", "vscode")</script>
<script>localStorage.setItem("extensionVersion", '"${getExtensionVersion()}"')</script>
<script>window.windowId = "${this.windowId}"</script>
<script>window.vscMachineId = "${getUniqueId()}"</script>
<script>window.vscMediaUrl = "${vscMediaUrl}"</script>
Expand Down
4 changes: 2 additions & 2 deletions extensions/vscode/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function getMetaKeyLabel() {
}
}

export function getExtensionVersion() {
export function getExtensionVersion(): string {
const extension = vscode.extensions.getExtension("continue.continue");
return extension?.packageJSON.version || "";
return extension?.packageJSON.version || "0.1.0";
}
30 changes: 29 additions & 1 deletion gui/src/components/mainInput/TipTapEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
} from "../../redux/slices/stateSlice";
import { RootState } from "../../redux/store";
import { isMetaEquivalentKeyPressed } from "../../util";
import { isJetBrains, postToIde } from "../../util/ide";
import { isJetBrains, isPrerelease, postToIde } from "../../util/ide";
import CodeBlockExtension from "./CodeBlockExtension";
import { SlashCommand } from "./CommandsExtension";
import InputToolbar from "./InputToolbar";
Expand Down Expand Up @@ -405,6 +405,34 @@ function TipTapEditor(props: TipTapEditorProps) {
},
});

useEffect(() => {
if (isJetBrains() || !isPrerelease()) {
// This is only for VS Code .ipynb files
return;
}

const handleKeyDown = async (event: KeyboardEvent) => {
if (!editor) return;

if (event.metaKey && event.key === "x") {
document.execCommand("cut");
event.stopPropagation();
} else if (event.metaKey && event.key === "v") {
document.execCommand("paste");
event.stopPropagation();
} else if (event.metaKey && event.key === "c") {
document.execCommand("copy");
event.stopPropagation();
}
};

document.addEventListener("keydown", handleKeyDown);

return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [editor]);

useEffect(() => {
if (mainEditorContent && editor) {
editor.commands.setContent(mainEditorContent);
Expand Down
16 changes: 16 additions & 0 deletions gui/src/util/ide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "core/web/webviewProtocol";
import { v4 as uuidv4 } from "uuid";
import "vscode-webview";
import { getLocalStorage } from "./localStorage";
interface vscode {
postMessage(message: any): vscode;
}
Expand Down Expand Up @@ -190,3 +191,18 @@ export function appendText(text: string) {
export function isJetBrains() {
return localStorage.getItem("ide") === "jetbrains";
}

export function isPrerelease() {
const extensionVersion = getLocalStorage("extensionVersion");
if (!extensionVersion) {
console.warn(
`Could not find extension version in local storage, assuming it's a prerelease`,
);
return true;
}
const minor = parseInt(extensionVersion.split(".")[1], 10);
if (minor % 2 !== 0) {
return true;
}
return false;
}
11 changes: 10 additions & 1 deletion gui/src/util/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type LocalStorageTypes = {
mainTextEntryCounter: number;
lastSessionId: string | undefined;
inputHistory: JSONContent[];
extensionVersion: string;
};

export function getLocalStorage<T extends keyof LocalStorageTypes>(
Expand All @@ -14,7 +15,15 @@ export function getLocalStorage<T extends keyof LocalStorageTypes>(
if (value === null) {
return undefined;
}
return JSON.parse(value);
try {
return JSON.parse(value);
} catch (error) {
console.error(
`Error parsing ${key} from local storage. Value was ${value}\n\n`,
error,
);
return undefined;
}
}

export function setLocalStorage<T extends keyof LocalStorageTypes>(
Expand Down

0 comments on commit 1b30b9e

Please sign in to comment.