-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.ts
164 lines (144 loc) · 5.22 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import path from "path";
import * as vscode from "vscode";
const openTabs = new Set();
export function activate(context: vscode.ExtensionContext) {
// Register the webview view provider
context.subscriptions.push(
vscode.window.registerWebviewViewProvider(
"solaceTryMeVscExtension.sideView",
new SolaceTryMeViewProvider(context),
{
webviewOptions: {
retainContextWhenHidden: true,
},
}
)
);
// Register solaceTryMeVscExtension.newWindow command to open the webview in a new window
context.subscriptions.push(
vscode.commands.registerCommand("solaceTryMeVscExtension.newWindow", () => {
let tabNumber = 1;
while (openTabs.has(tabNumber)) {
tabNumber++;
}
openTabs.add(tabNumber);
const panel = vscode.window.createWebviewPanel(
"solaceTryMeVscExtension.newWindow",
`Solace Try Me - Tab ${tabNumber}`,
vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
}
);
const provider = new SolaceTryMeViewProvider(context);
provider.resolveWebviewView(panel);
panel.onDidDispose(() => {
openTabs.delete(tabNumber);
});
})
);
}
class SolaceTryMeViewProvider implements vscode.WebviewViewProvider {
constructor(private readonly context: vscode.ExtensionContext) {}
resolveWebviewView(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
const { webview } = webviewView;
webview.onDidReceiveMessage(async (message) => {
if (message.command === "openInNewTab") {
try {
if (message.filePath && message.fileName) {
// Write content to a file and open it in a new tab
let uri = vscode.Uri.joinPath(
vscode.Uri.file(message.filePath),
message.fileName
);
const isAbsolutePath = path.isAbsolute(message.filePath);
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!isAbsolutePath && workspaceFolders) {
const workspacePath = workspaceFolders[0].uri.fsPath;
uri = vscode.Uri.joinPath(
vscode.Uri.file(workspacePath),
message.filePath,
message.fileName
);
}
await vscode.workspace.fs.writeFile(
uri,
Buffer.from(message.content, "utf8")
);
const document = await vscode.workspace.openTextDocument(uri);
vscode.window.showTextDocument(document, { preview: true });
} else {
const document = await vscode.workspace.openTextDocument({
content: message.content,
language: message.language ?? "plaintext",
});
vscode.window.showTextDocument(document, { preview: true });
}
} catch (error: unknown) {
console.error("Error opening file in new tab: ", error);
// Show VSC error message
vscode.window.showErrorMessage(
"Error opening file in new tab: " +
((error as Error)?.message ?? error)
);
}
} else if (message.command === "savePreferences") {
this.context.globalState.update("preferences", message.preferences);
} else if (message.command === "getPreferences") {
webview.postMessage({
command: "getPreferences/response",
preferences: this.context.globalState.get("preferences", ""),
});
}
});
// Allow scripts in the webview
webview.options = {
enableScripts: true,
};
// Set the HTML content for the webview
webview.html = this.getHtmlForWebview(webview);
// Send the current theme to the webview
this.updateTheme(webview);
// Listen for theme changes and update the webview accordingly
vscode.window.onDidChangeActiveColorTheme((event) => {
this.updateTheme(webview);
});
}
private getHtmlForWebview(webview: vscode.Webview): string {
const uriPrefix = webview.asWebviewUri(
vscode.Uri.joinPath(this.context.extensionUri, "webview-dist")
);
const jsUri = vscode.Uri.joinPath(uriPrefix, "assets", "index.js");
const cssUri = vscode.Uri.joinPath(uriPrefix, "assets", "index.css");
const theme = this.getTheme();
return `
<!DOCTYPE html>
<html lang="en" class="${theme}">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Solace TryMe VSC Extension View</title>
<script type="module" crossorigin src="${jsUri}"></script>
<link rel="stylesheet" crossorigin href="${cssUri}">
</head>
<body>
<div id="root"></div>
</body>
</html>
`;
}
private getTheme() {
// Using dark for high contrast and dark themes
return vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Light
? "light"
: "dark";
}
// Method to send the current theme to the webview
private updateTheme(webview: vscode.Webview) {
const theme = this.getTheme();
// Send a message to the webview to update the theme
webview.postMessage({ command: "setTheme", theme });
}
}
export function deactivate() {}