This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
252 lines (220 loc) · 6.3 KB
/
main.js
File metadata and controls
252 lines (220 loc) · 6.3 KB
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
/**
* SuperDoc Webview Main Script
* Handles initialization of SuperDoc and communication with VS Code extension
*/
(function () {
// VS Code API for communicating with the extension
const vscode = acquireVsCodeApi();
// State
let superdocInstance = null;
let currentFileName = '';
let isDirty = false;
let saveTimeout = null;
const DEBOUNCE_DELAY = 1000; // 1 second debounce for auto-save
// DOM Elements
const loadingEl = document.getElementById('loading');
const containerEl = document.getElementById('superdoc-container');
const statusTextEl = document.getElementById('status-text');
const saveIndicatorEl = document.getElementById('save-indicator');
/**
* Update status bar text
*/
function setStatus(text) {
if (statusTextEl) {
statusTextEl.textContent = text;
}
}
/**
* Update save indicator
*/
function setSaveState(state) {
if (saveIndicatorEl) {
saveIndicatorEl.className = state;
switch (state) {
case 'saving':
saveIndicatorEl.textContent = 'Saving...';
break;
case 'saved':
saveIndicatorEl.textContent = 'Saved';
break;
case 'dirty':
saveIndicatorEl.textContent = 'Unsaved changes';
break;
default:
saveIndicatorEl.textContent = '';
}
}
}
/**
* Hide loading state
*/
function hideLoading() {
if (loadingEl) {
loadingEl.classList.add('hidden');
}
}
/**
* Show error message
*/
function showError(message) {
hideLoading();
containerEl.innerHTML = `
<div class="error-container">
<h2>Error Loading Document</h2>
<p>${message}</p>
</div>
`;
vscode.postMessage({ type: 'error', message });
}
/**
* Convert array to Uint8Array and then to Blob
*/
function arrayToBlob(array) {
const uint8Array = new Uint8Array(array);
return new Blob([uint8Array], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
});
}
/**
* Initialize SuperDoc with document data
*/
async function initSuperdoc(docData, fileName) {
try {
currentFileName = fileName;
setStatus(`Loading: ${fileName.split(/[/\\]/).pop()}`);
// Convert array data to blob
const docBlob = arrayToBlob(docData);
const docFile = new File([docBlob], fileName.split(/[/\\]/).pop() || 'document.docx', {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
});
// Check if SuperDoc is available
if (typeof SuperDoc === 'undefined') {
throw new Error('SuperDoc library not loaded. Please check your internet connection.');
}
// Initialize SuperDoc
// Based on the vanilla example pattern
superdocInstance = new SuperDoc({
selector: '#superdoc-container',
documents: [
{
id: 'main-doc',
file: docFile,
}
],
// Enable editing
editable: true,
// Toolbar configuration
toolbar: {
enabled: true,
},
// Handle content changes
onContentChange: handleContentChange,
onReady: handleReady,
onError: handleSuperdocError,
});
} catch (error) {
console.error('Failed to initialize SuperDoc:', error);
showError(error.message || 'Failed to initialize document editor');
}
}
/**
* Handle SuperDoc ready event
*/
function handleReady() {
hideLoading();
setStatus(`Editing: ${currentFileName.split(/[/\\]/).pop()}`);
setSaveState('saved');
console.log('SuperDoc initialized successfully');
}
/**
* Handle SuperDoc errors
*/
function handleSuperdocError(error) {
console.error('SuperDoc error:', error);
showError(error.message || 'An error occurred in the document editor');
}
/**
* Handle content changes - debounced auto-save
*/
function handleContentChange() {
isDirty = true;
setSaveState('dirty');
// Clear existing timeout
if (saveTimeout) {
clearTimeout(saveTimeout);
}
// Debounce the save operation
saveTimeout = setTimeout(() => {
saveDocument();
}, DEBOUNCE_DELAY);
}
/**
* Export document and send to extension for saving
*/
async function saveDocument() {
if (!superdocInstance || !isDirty) {
return;
}
try {
setSaveState('saving');
setStatus('Saving...');
// Export the document using SuperDoc's export functionality
const exportedBlob = await superdocInstance.export({
type: 'docx',
});
// Convert blob to array for message passing
const arrayBuffer = await exportedBlob.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
// Send to extension
vscode.postMessage({
type: 'update',
data: Array.from(uint8Array),
});
isDirty = false;
setSaveState('saved');
setStatus(`Editing: ${currentFileName.split(/[/\\]/).pop()}`);
} catch (error) {
console.error('Failed to save document:', error);
setSaveState('dirty');
setStatus('Save failed');
vscode.postMessage({
type: 'error',
message: `Failed to save: ${error.message}`
});
}
}
/**
* Handle messages from the VS Code extension
*/
window.addEventListener('message', async (event) => {
const message = event.data;
switch (message.type) {
case 'init':
// Initialize SuperDoc with the document data
await initSuperdoc(message.data.content, message.data.fileName);
break;
case 'saved':
// Extension confirmed save
setSaveState('saved');
setStatus(`Editing: ${currentFileName.split(/[/\\]/).pop()}`);
break;
case 'reload':
// Reload the document (e.g., after external change)
if (message.data && message.data.content) {
await initSuperdoc(message.data.content, currentFileName);
}
break;
}
});
// Handle keyboard shortcuts
document.addEventListener('keydown', (e) => {
// Ctrl/Cmd + S to save
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
saveDocument();
}
});
// Signal that the webview is ready
vscode.postMessage({ type: 'ready' });
setStatus('Initializing...');
})();