-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
256 lines (226 loc) · 8.14 KB
/
extension.js
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const vscode = require("vscode");
const os = require("os");
import base64url from "base64url";
import { traverse, customTraverse } from "./traverse";
const is_web_ext = os.homedir === undefined; // Runs as web extension in browser.
let client_html_template_web = null; // Query history does not replace this structure, it is also used to store partially entered queries for preview window switch.
let client_html_template = null;
let sidebar_html_template_web = null; // Query history does not replace this structure, it is also used to store partially entered queries for preview window switch.
let sidebar_html_template = null;
let preview_panel = null;
let sidebar_preview_panel = null;
let productEndpoints = null;
let isVirtualWorkspace = false;
let absolute_path_map = {};
// MDX Preview
const docterMDXPreview = async () => {
let active_window = vscode.window;
if (!active_window) return;
let active_editor = active_window.activeTextEditor;
if (!active_editor) return;
let active_doc = active_editor.document;
if (!active_doc) return;
let orig_uri = active_doc.uri;
if (!orig_uri) return;
let encodedMDX = base64url(active_doc.getText());
// Error for Max header value when sending as query parameter
// TODO: Find a better way to solve this
// Currently, showing a message to users regarding this and taking them to an editor page
if (encodedMDX.length > 15000) {
const selection = await vscode.window.showWarningMessage(
"This MDX file contains more characters than the accepted limit. \nPlease visit our preview page where you can copy this content and preview accordingly.",
"Go to Preview page"
);
if (selection !== undefined) {
vscode.env.openExternal(
vscode.Uri.parse("https://docs.setu.co/content-preview")
);
}
return;
}
preview_panel = vscode.window.createWebviewPanel(
"docter-preview",
`${active_doc.uri.path.split("/").slice(-1)} - MDX Preview`,
vscode.ViewColumn.Beside,
{ enableScripts: true }
);
if (!client_html_template) {
if (is_web_ext) {
client_html_template = client_html_template_web;
} else {
client_html_template = readFileSync(
absolute_path_map["preview.html"],
"utf8"
);
}
}
let client_html = client_html_template;
preview_panel.webview.html = client_html;
preview_panel.webview.postMessage({
value: encodedMDX,
type: "MDX_PREVIEW",
});
return;
};
// Sidebar Preview
const docterSidebarPreview = async () => {
let active_window = vscode.window;
if (!active_window) return;
let active_editor = active_window.activeTextEditor;
if (!active_editor) return;
let active_doc = active_editor.document;
if (!active_doc) return;
let orig_uri = active_doc.uri;
if (!orig_uri) return;
let endpoints = JSON.parse(active_doc.getText());
let options = [];
for (let endpoint of endpoints["home"]) {
if (endpoint.visible_in_sidebar) {
for (let child of endpoint["children"]) {
options.push({
name: `${endpoint.name} - ${child.name}`,
value: `${endpoint.path}/${child.path}`,
});
}
}
}
sidebar_preview_panel = vscode.window.createWebviewPanel(
"docter-preview",
`Sidebar Preview`,
vscode.ViewColumn.Beside,
{ enableScripts: true }
);
if (!sidebar_html_template) {
if (is_web_ext) {
sidebar_html_template = sidebar_html_template_web;
} else {
sidebar_html_template = readFileSync(
absolute_path_map["sidebar.html"],
"utf8"
);
}
}
let sidebar_html = sidebar_html_template;
sidebar_preview_panel.webview.html = sidebar_html;
sidebar_preview_panel.webview.postMessage({
value: options,
type: "SIDEBAR_PREVIEW",
});
sidebar_preview_panel.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case "CATEGORY_SELECTED": {
productEndpoints = await customTraverse(
data.value,
JSON.parse(active_doc.getText())
);
let encodedEndoints = base64url(
JSON.stringify(productEndpoints)
);
sidebar_preview_panel.webview.postMessage({
value: encodedEndoints,
type: "RELOAD_SIDEBAR_PREVIEW",
});
break;
}
}
});
};
// Building Menu items
const buildMenuItems = async () => {
// Notification with progress
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: "Please wait while the menu items are updated",
cancellable: false,
},
async () => {
let data = await buildMenuItemsLogic();
if (data) {
vscode.window.showInformationMessage(
"Menu items have been updated!"
);
return;
} else {
vscode.window.showErrorMessage("Please try again!");
return;
}
}
);
};
// Logic to build menu items
const buildMenuItemsLogic = async () => {
let active_window = vscode.window;
if (!active_window) return;
let active_editor = active_window.activeTextEditor;
if (!active_editor) return;
let active_doc = active_editor.document;
if (!active_doc) return;
let orig_uri = active_doc.uri;
if (!orig_uri) return;
let selectFolder = vscode.workspace.workspaceFolders[0];
let endpoints = JSON.parse(active_doc.getText());
let result = await traverse(endpoints);
let utf8Encode = new TextEncoder();
let uri = `${selectFolder.uri.scheme}://${selectFolder.uri.authority}${selectFolder.uri.path}/content/menuItems.json`;
try {
await vscode.workspace.fs.writeFile(
vscode.Uri.parse(uri),
utf8Encode.encode(JSON.stringify(result))
);
return true;
} catch (e) {
console.log(e);
return false;
}
};
const activate = async (context) => {
if (is_web_ext) {
let client_uri = vscode.Uri.joinPath(
context.extensionUri,
"preview.html"
);
let sidebar_uri = vscode.Uri.joinPath(
context.extensionUri,
"sidebar.html"
);
let bytes = await vscode.workspace.fs.readFile(client_uri);
client_html_template_web = new TextDecoder().decode(bytes);
let sidebar_bytes = await vscode.workspace.fs.readFile(sidebar_uri);
sidebar_html_template_web = new TextDecoder().decode(sidebar_bytes);
}
for (let local_path in absolute_path_map) {
if (absolute_path_map.hasOwnProperty(local_path)) {
if (is_web_ext) {
absolute_path_map[local_path] = vscode.Uri.joinPath(
context.extensionUri,
local_path
);
} else {
absolute_path_map[local_path] =
context.asAbsolutePath(local_path);
}
}
}
let previewCommand = vscode.commands.registerCommand(
"docter-preview.commands.openPreview",
docterMDXPreview
);
let sidebarPreviewCommand = vscode.commands.registerCommand(
"docter-preview.commands.openSidebarPreview",
docterSidebarPreview
);
let buildMenuCommand = vscode.commands.registerCommand(
"docter-preview.commands.buildMenuItems",
buildMenuItems
);
// The only purpose to add the entries to context.subscriptions is to guarantee their disposal during extension deactivation
context.subscriptions.push(previewCommand);
context.subscriptions.push(sidebarPreviewCommand);
context.subscriptions.push(buildMenuCommand);
};
function deactivate() {
// This method is called when extension is deactivated.
}
exports.activate = activate;
exports.deactivate = deactivate;