-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
186 lines (152 loc) · 5.04 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
const vscode = require('vscode');
const path = require('path');
const fs = require('fs');
const WITH_BASIC_INIT_VALUE = {
'8c302956-21b2-47f7-8806-7a53070ab1bf': {
id: '8c302956-21b2-47f7-8806-7a53070ab1bf',
value: [
{
id: 'aa7e2b8c-bb9a-4788-bc36-bafcaa5c6464',
type: 'paragraph',
children: [
{
text: 'Start writing here...',
},
],
props: {
nodeType: 'block',
},
},
],
type: 'Paragraph',
meta: {
order: 0,
depth: 0,
},
},
};
let currentPanel = undefined;
function activate(context) {
vscode.window.registerWebviewPanelSerializer('aptusNotebook', {
async deserializeWebviewPanel(webviewPanel, state) {
currentPanel = webviewPanel;
webviewPanel.webview.options = getWebviewOptions(context);
webviewPanel.webview.html = getWebviewContent(context, webviewPanel.webview);
attachMessageHandlers(webviewPanel, context);
restorePanelData(context);
handleDisposal(context);
}
});
let disposable = vscode.commands.registerCommand('aptus.openNotebook', async function() {
// If we already have a panel, show it instead of creating a new one
if (currentPanel) {
currentPanel.reveal(vscode.ViewColumn.One);
return;
}
const panel = createWebviewPanel(context);
currentPanel = panel;
context.workspaceState.update('aptus-opened', true);
// Pin the editor tab automatically
await vscode.commands.executeCommand('workbench.action.pinEditor');
// Set the webview's initial html content
panel.webview.html = getWebviewContent(context, panel.webview);
restorePanelData(context);
attachMessageHandlers(panel, context);
handleDisposal(context);
});
context.subscriptions.push(disposable);
}
function restorePanelData(context) {
// Restore the state
const savedState = context.workspaceState.get('aptus-state', '');
currentPanel.webview.postMessage({
command: 'savedState',
savedState: Object.keys(savedState).length > 0 ? savedState : WITH_BASIC_INIT_VALUE
});
}
function createWebviewPanel(context) {
const panel = vscode.window.createWebviewPanel(
'aptusNotebook',
'Aptus Notebook',
vscode.ViewColumn.One,
getWebviewOptions(context)
);
return panel;
}
function attachMessageHandlers(panel, context) {
// Handle messages from the webview
panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case 'saveState':
context.workspaceState.update('aptus-state', message.state);
break;
}
},
undefined,
context.subscriptions
);
}
function getWebviewOptions(context) {
return {
enableScripts: true,
localResourceRoots: [vscode.Uri.file(path.join(context.extensionPath, 'webview-ui', 'dist'))],
retainContextWhenHidden: true,
};
}
function handleDisposal(context) {
currentPanel.onDidDispose(() => {
currentPanel = undefined;
context.workspaceState.update('aptus-opened', false);
}, null, context.subscriptions);
}
function getWebviewContent(context, webview) {
const webviewUiFolder = path.join(context.extensionPath, 'webview-ui', 'dist', 'assets');
// Read the contents of the dist folder
const files = fs.readdirSync(webviewUiFolder);
// Find the main JavaScript and CSS files
const scriptFile = files.find(file => file.endsWith('.js'));
const styleFile = files.find(file => file.endsWith('.css'));
if (!scriptFile || !styleFile) {
return 'Error: Unable to load the webview content' + files + scriptFile + styleFile;
}
const isDarkTheme = vscode.window.activeColorTheme.kind === vscode.ColorThemeKind.Dark;
const scriptUri = webview.asWebviewUri(vscode.Uri.file(path.join(webviewUiFolder, scriptFile)));
const styleUri = webview.asWebviewUri(vscode.Uri.file(path.join(webviewUiFolder, styleFile)));
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="${styleUri}">
<title>Aptus</title>
<style>
::selection {
background-color: ${isDarkTheme ? '#ffffff3d' : '#add8e6'} !important;
color: ${isDarkTheme ? '#ffffff' : '#000000'} !important;
}
::-moz-selection {
background-color: ${isDarkTheme ? '#ffffff3d' : '#add8e6'} !important;
color: ${isDarkTheme ? '#ffffff' : '#000000'} !important;
}
</style>
</head>
<body class="${isDarkTheme ? 'dark' : 'light'}" style="color-scheme: ${isDarkTheme ? 'dark' : 'light'};">
<div id="root"></div>
<script type="module" src="${scriptUri}"></script>
<script>
const vscode = acquireVsCodeApi();
window.addEventListener('saveData', (event) => {
vscode.postMessage({ command: 'saveState', state: event.detail.value });
});
</script>
</body>
</html>
`;
}
function deactivate() {}
module.exports = {
activate,
deactivate
}