Skip to content

Commit 3318084

Browse files
committed
Simplify platform api for vscode (only opening file)
1 parent f25ab01 commit 3318084

File tree

6 files changed

+85
-104
lines changed

6 files changed

+85
-104
lines changed

components/nav.tsx

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import { EditorContext } from "./providers/editor-context-provider";
1717
import { getPlatform } from "@/lib/platform-api/platform-checker";
1818
import { PlatformEnum } from "@/lib/platform-api/available-platforms";
1919
import Loading from "./loading";
20-
import { ViewDocument } from "@/lib/types";
21-
import { View } from "@/lib/views/view";
22-
import { ViewTypeEnum } from "@/lib/views/available-views";
23-
import { ViewManager } from "@/lib/views/view-manager";
2420

2521
export default function Nav({ children }: { children: React.ReactNode }) {
2622
const [mounted, setMounted] = useState(false);
@@ -45,8 +41,6 @@ export default function Nav({ children }: { children: React.ReactNode }) {
4541
const vscodeTheme =
4642
new URLSearchParams(window.location.search).get("theme") ?? "dark";
4743
setTheme(vscodeTheme);
48-
49-
addVSCodeHandlers();
5044
}
5145

5246
setMounted(true);
@@ -67,69 +61,6 @@ export default function Nav({ children }: { children: React.ReactNode }) {
6761
return <Loading />;
6862
}
6963

70-
function addVSCodeHandlers() {
71-
// Listen for ctrl+alt+s to switch back to VSCode original editor
72-
window.addEventListener("keydown", (e) => {
73-
if (e.ctrlKey && e.altKey && e.code === "KeyS") {
74-
// Send a message to parent iframe
75-
window.parent.postMessage(
76-
{ command: "switchToTextEditor", from: "chisel" },
77-
"*",
78-
);
79-
}
80-
});
81-
82-
// Add a listener to listen messages from VSCode Extension
83-
window.addEventListener("message", (e) => {
84-
const message = e.data;
85-
if (message.command === "updateChiselText") {
86-
const text: string = message.text;
87-
console.log("Received text from VSCode:", text);
88-
const view = editorContext?.viewManager?.getActiveView();
89-
if (view) {
90-
view.updateViewDocument({
91-
fileContent: text,
92-
});
93-
}
94-
} else if (message.command === "openFile") {
95-
const text: string = message.text;
96-
const path: string = message.path;
97-
console.log(
98-
"Received file from VSCode. Path: " + path + " Text: " + text,
99-
);
100-
101-
const doc: ViewDocument = {
102-
fileContent: text,
103-
filePath: path,
104-
};
105-
const newView = new View(ViewTypeEnum.Code, doc);
106-
// Send a message to parent iframe to notify changes made in Chisel
107-
const callback = (viewDocument: ViewDocument) => {
108-
if (!viewDocument) {
109-
return;
110-
}
111-
window.parent.postMessage(
112-
{
113-
command: "updateVSCodeText",
114-
text: viewDocument.fileContent,
115-
from: "chisel",
116-
},
117-
"*",
118-
);
119-
};
120-
newView.setViewDocumentChangeCallback(callback);
121-
122-
// Add to view manager
123-
editorContext?.setViewManager((prev) => {
124-
const newVM = new ViewManager();
125-
newVM?.addView(newView);
126-
newVM?.setActiveView(newView);
127-
return newVM;
128-
});
129-
}
130-
});
131-
}
132-
13364
return (
13465
<div className="flex h-screen w-full flex-col overflow-x-hidden">
13566
<PasswordScreen
@@ -224,7 +155,7 @@ export default function Nav({ children }: { children: React.ReactNode }) {
224155
<div
225156
className={`flex h-full w-full overflow-hidden ${isShowNavbar ? "pt-[48px]" : ""}`}
226157
>
227-
<NavMenu isMenuOpen={isMenuOpen} />
158+
{isShowNavbar && <NavMenu isMenuOpen={isMenuOpen} />}
228159

229160
<div className="min-w-0 flex-grow">{children}</div>
230161
</div>

components/providers/editor-context-provider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import { AIModelConfig } from "@/lib/ai-model-config";
44
import usePersistSettings from "@/lib/hooks/use-persist-settings";
5-
import { BaseLLM, getModelLLM } from "@/lib/llm/llm";
6-
import { BaseSTT, getModelSTT } from "@/lib/stt/stt";
7-
import { BaseTTS, getModelTTS } from "@/lib/tts/tts";
5+
import { getModelLLM } from "@/lib/llm/llm";
6+
import { getModelSTT } from "@/lib/stt/stt";
7+
import { getModelTTS } from "@/lib/tts/tts";
88
import { EditorStates, EditorContextType, PersistSettings } from "@/lib/types";
99
import { ViewManager } from "@/lib/views/view-manager";
1010
import { createContext, useEffect, useRef, useState } from "react";

components/view-display-area.tsx

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import AgentChatTerminalView from "./views/agent-chat-terminal-view";
77
import { ViewTypeEnum } from "@/lib/views/available-views";
88
import { getPlatform } from "@/lib/platform-api/platform-checker";
99
import { PlatformEnum } from "@/lib/platform-api/available-platforms";
10+
import { ViewDocument } from "@/lib/types";
11+
import { View } from "@/lib/views/view";
1012

1113
export default function ViewDisplayArea() {
1214
const editorContext = useContext(EditorContext);
@@ -17,20 +19,88 @@ export default function ViewDisplayArea() {
1719
// If running in VSCode extension, notify VSCode that Chisel is ready,
1820
// and create view manager later when VSCode sends a message.
1921
if (getPlatform() === PlatformEnum.VSCode) {
20-
window.parent.postMessage(
21-
{
22-
command: "chiselReady",
23-
from: "chisel",
24-
},
25-
"*",
26-
);
22+
notifyVSCode();
23+
addVSCodeHandlers();
2724
} else {
2825
const viewManager = new ViewManager();
2926
editorContext?.setViewManager(viewManager);
3027
}
3128
}
3229
}, []);
3330

31+
function notifyVSCode() {
32+
window.parent.postMessage(
33+
{
34+
command: "chiselReady",
35+
from: "chisel",
36+
},
37+
"*",
38+
);
39+
}
40+
41+
function addVSCodeHandlers() {
42+
// Listen for ctrl+alt+s to switch back to VSCode original editor
43+
window.addEventListener("keydown", (e) => {
44+
if (e.ctrlKey && e.altKey && e.code === "KeyS") {
45+
// Send a message to parent iframe
46+
window.parent.postMessage(
47+
{ command: "switchToTextEditor", from: "chisel" },
48+
"*",
49+
);
50+
}
51+
});
52+
53+
// Add a listener to listen messages from VSCode Extension
54+
window.addEventListener("message", (e) => {
55+
const message = e.data;
56+
if (message.command === "updateChiselText") {
57+
const text: string = message.text;
58+
console.log("Received text from VSCode:", text);
59+
const view = editorContext?.viewManager?.getActiveView();
60+
if (view) {
61+
view.updateViewDocument({
62+
fileContent: text,
63+
});
64+
}
65+
} else if (message.command === "openFile") {
66+
const text: string = message.text;
67+
const path: string = message.path;
68+
console.log(
69+
"Received file from VSCode. Path: " + path + " Text: " + text,
70+
);
71+
72+
const doc: ViewDocument = {
73+
fileContent: text,
74+
filePath: path,
75+
};
76+
const newView = new View(ViewTypeEnum.Code, doc);
77+
// Send a message to parent iframe to notify changes made in Chisel
78+
const callback = (viewDocument: ViewDocument) => {
79+
if (!viewDocument) {
80+
return;
81+
}
82+
window.parent.postMessage(
83+
{
84+
command: "updateVSCodeText",
85+
text: viewDocument.fileContent,
86+
from: "chisel",
87+
},
88+
"*",
89+
);
90+
};
91+
newView.setViewDocumentChangeCallback(callback);
92+
93+
// Add to view manager
94+
editorContext?.setViewManager((prev) => {
95+
const newVM = new ViewManager();
96+
newVM?.addView(newView);
97+
newVM?.setActiveView(newView);
98+
return newVM;
99+
});
100+
}
101+
});
102+
}
103+
34104
return (
35105
<div className="flex h-full w-full flex-col p-1">
36106
<div className="flex h-full w-full flex-col items-start justify-between gap-1.5 overflow-hidden rounded-xl bg-default p-2">

lib/hooks/use-file-system.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { PlatformEnum } from "../platform-api/available-platforms";
77
import { CapacitorAPI } from "../platform-api/capacitor/capacitor-api";
88
import { AbstractPlatformAPI } from "../platform-api/abstract-platform-api";
99
import { ElectronAPI } from "../platform-api/electron/electron-api";
10-
import { VSCodeAPI } from "../platform-api/vscode-extension/vscode-api";
1110
import { WebAPI } from "../platform-api/web/web-api";
1211

1312
export function useFileSystem() {
@@ -21,10 +20,11 @@ export function useFileSystem() {
2120
platformApi.current = new CapacitorAPI();
2221
} else if (platform === PlatformEnum.Electron) {
2322
platformApi.current = new ElectronAPI();
24-
} else if (platform === PlatformEnum.VSCode) {
25-
platformApi.current = new VSCodeAPI();
2623
} else if (platform === PlatformEnum.Web) {
2724
platformApi.current = new WebAPI();
25+
} else if (platform === PlatformEnum.VSCode) {
26+
// platformApi.current = new VSCodeAPI();
27+
throw new Error("VSCode API not implemented");
2828
} else {
2929
throw new Error("Unknown platform");
3030
}

lib/platform-api/vscode-extension/vscode-api.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

vscode-extension/examples/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12312345
1+
12312

0 commit comments

Comments
 (0)