vscode-colaview 0.3.3 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.release-notes-v0.4.1.md +29 -0
- package/.vscode/settings.json +1 -1
- package/CHANGELOG.md +45 -16
- package/README.md +318 -287
- package/README_CN.md +318 -287
- package/RELEASE_NOTES.md +80 -21
- package/esbuild.js +2 -0
- package/package.json +204 -164
- package/package.nls.json +14 -0
- package/package.nls.zh-cn.json +14 -0
- package/src/commands/commands.ts +48 -0
- package/src/docx/docx-builder.ts +241 -0
- package/src/export/docx-exporter.ts +961 -961
- package/src/export/html-exporter.ts +8 -2
- package/src/export/pdf-exporter.ts +69 -0
- package/src/export/pptx-exporter.ts +63 -0
- package/src/export/render-export-images.ts +386 -386
- package/src/export/theme-style.ts +195 -195
- package/src/pptx/pptx-builder.ts +249 -0
- package/src/pptx/zip.ts +158 -0
- package/src/present/presenter.ts +89 -0
- package/src/preview/preview-manager.ts +783 -541
- package/src/preview/webview/app.ts +695 -370
- package/src/preview/webview/index.html +167 -146
- package/src/slides/deck.ts +408 -0
- package/src/slides/markdown.ts +420 -0
- package/src/slides/render.ts +580 -0
- package/src/slides/slides.css +1082 -0
- package/src/themes/built-in/bear.css +64 -13
- package/src/themes/built-in/dark.css +64 -5
- package/src/themes/built-in/dracula.css +64 -5
- package/src/themes/built-in/elegant.css +75 -11
- package/src/themes/built-in/gruvbox.css +65 -6
- package/src/themes/built-in/light.css +62 -2
- package/src/themes/built-in/midnight.css +64 -6
- package/src/themes/built-in/nord.css +64 -5
- package/src/themes/built-in/notion.css +63 -11
- package/src/themes/built-in/sepia.css +65 -10
- package/src/themes/built-in/solarized-dark.css +64 -5
- package/src/themes/built-in/writer.css +66 -19
- package/src/themes/theme-manager.ts +207 -180
- package/src/types/colamd-renderer.d.ts +47 -47
- package/src/types/pele-lazy.d.ts +21 -0
|
@@ -1,370 +1,695 @@
|
|
|
1
|
-
/// <reference lib="dom" />
|
|
2
|
-
/**
|
|
3
|
-
* ColaView MD — WebView runtime
|
|
4
|
-
* CodeMirror 6 based ColaMD editor in read-only mode + right-click context menu.
|
|
5
|
-
* @bytechain.cn/colamd@2.7.9 适配
|
|
6
|
-
*/
|
|
7
|
-
import { createColaMDEditor, type ColaMDRendererHandle } from '@bytechain.cn/colamd/renderer';
|
|
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
|
-
if (
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
function
|
|
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
|
-
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
/**
|
|
3
|
+
* ColaView MD — WebView runtime
|
|
4
|
+
* CodeMirror 6 based ColaMD editor in read-only mode + right-click context menu.
|
|
5
|
+
* @bytechain.cn/colamd@2.7.9 适配
|
|
6
|
+
*/
|
|
7
|
+
import { createColaMDEditor, type ColaMDRendererHandle } from '@bytechain.cn/colamd/renderer';
|
|
8
|
+
/** KaTeX:幻灯片渲染层只输出占位容器,由这里水合成公式。 */
|
|
9
|
+
import katex from 'katex';
|
|
10
|
+
// mermaid 图走 pele 渲染(与 ColaMD-extend 同一实现):懒加载,按需 import。
|
|
11
|
+
|
|
12
|
+
/** 图内文字字体:测量与绘制必须同一套,否则 CJK 标签会被量窄、裁掉(上游 #94)。 */
|
|
13
|
+
const DIAGRAM_FONT = '"PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif';
|
|
14
|
+
const DIAGRAM_MONO_FONT = "'SF Mono', 'Fira Code', Menlo, Consolas, monospace";
|
|
15
|
+
|
|
16
|
+
// @ts-ignore — VSCode WebView API
|
|
17
|
+
const vscode = acquireVsCodeApi();
|
|
18
|
+
|
|
19
|
+
let handle: ColaMDRendererHandle | null = null;
|
|
20
|
+
let currentMarkdown = '';
|
|
21
|
+
let currentTheme = 'elegant';
|
|
22
|
+
let themeListData: { builtins: string[]; customs: string[]; current: string } | null = null;
|
|
23
|
+
let scrollDebounceTimer: number | null = null;
|
|
24
|
+
let receivedScrollFromEditor = false;
|
|
25
|
+
/** 当前预览模式:整篇文档(CodeMirror)/ 幻灯片(扩展侧渲染好的舞台 HTML)。 */
|
|
26
|
+
let mode: 'document' | 'slides' = 'document';
|
|
27
|
+
|
|
28
|
+
/** 右键菜单文案(扩展宿主按 VS Code 显示语言下发,缺省英文)。 */
|
|
29
|
+
interface MenuLabels {
|
|
30
|
+
present: string;
|
|
31
|
+
exportPdf: string;
|
|
32
|
+
exportHtml: string;
|
|
33
|
+
exportWord: string;
|
|
34
|
+
exportPptx: string;
|
|
35
|
+
theme: string;
|
|
36
|
+
importTheme: string;
|
|
37
|
+
loading: string;
|
|
38
|
+
groupLight: string;
|
|
39
|
+
groupDark: string;
|
|
40
|
+
groupCustom: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let labels: MenuLabels = {
|
|
44
|
+
present: 'Present (Fullscreen)',
|
|
45
|
+
exportPdf: 'Export to PDF',
|
|
46
|
+
exportHtml: 'Export to HTML',
|
|
47
|
+
exportWord: 'Export to Word',
|
|
48
|
+
exportPptx: 'Export to PPTX',
|
|
49
|
+
theme: 'Theme',
|
|
50
|
+
importTheme: 'Import Theme…',
|
|
51
|
+
loading: 'Loading…',
|
|
52
|
+
groupLight: 'Built-in · Light',
|
|
53
|
+
groupDark: 'Built-in · Dark',
|
|
54
|
+
groupCustom: 'Custom',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** 暗色内置主题集合,用于主题子菜单分组(亮色 = 内置全集 - 暗色)。 */
|
|
58
|
+
const DARK_BUILTIN_THEMES = new Set(['dark', 'solarized-dark', 'nord', 'gruvbox', 'dracula', 'midnight']);
|
|
59
|
+
|
|
60
|
+
/** 图内文字字体:测量与绘制必须同一套,否则 CJK 标签会被量窄、裁掉(上游 #94)。 */
|
|
61
|
+
|
|
62
|
+
let peleLoaded: Promise<typeof import('pele/lazy')> | null = null;
|
|
63
|
+
|
|
64
|
+
function loadPele(): Promise<typeof import('pele/lazy')> {
|
|
65
|
+
peleLoaded ??= import('pele/lazy');
|
|
66
|
+
return peleLoaded;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
let mermaidReady = false;
|
|
71
|
+
let mermaidSeq = 0;
|
|
72
|
+
|
|
73
|
+
/** 颜色混合:a 占比 t,与引擎 pele 令牌同一公式(18%/28%/45% 混合)。 */
|
|
74
|
+
function mixColor(a: string, b: string, t: number): string {
|
|
75
|
+
const parse = (c: string): [number, number, number] | null => {
|
|
76
|
+
const v = c.trim();
|
|
77
|
+
if (v.startsWith('#')) {
|
|
78
|
+
const hex = v.length === 4 ? '#' + v.slice(1).split('').map((x) => x + x).join('') : v;
|
|
79
|
+
return [parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16)];
|
|
80
|
+
}
|
|
81
|
+
const m = /rgba?\(([^)]+)\)/.exec(v);
|
|
82
|
+
if (m) {
|
|
83
|
+
const parts = (m[1] ?? '').split(',').map((s) => parseFloat(s));
|
|
84
|
+
return [parts[0] ?? 128, parts[1] ?? 128, parts[2] ?? 128];
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
};
|
|
88
|
+
const pa = parse(a) ?? [128, 128, 128];
|
|
89
|
+
const pb = parse(b) ?? [255, 255, 255];
|
|
90
|
+
const r = Math.round(pa[0] * t + pb[0] * (1 - t));
|
|
91
|
+
const g = Math.round(pa[1] * t + pb[1] * (1 - t));
|
|
92
|
+
const bl = Math.round(pa[2] * t + pb[2] * (1 - t));
|
|
93
|
+
return `rgb(${r},${g},${bl})`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** 按当前主题推导 mermaid 配色(与文档模式的 pele 令牌一致:画在页面底色上)。 */
|
|
97
|
+
function mermaidThemeVariables(): Record<string, string> {
|
|
98
|
+
const style = getComputedStyle(document.body);
|
|
99
|
+
const bg = style.backgroundColor || '#ffffff';
|
|
100
|
+
const fg = style.color || '#1f1f1f';
|
|
101
|
+
const accentRaw = (style.getPropertyValue('--seed-accent') || style.getPropertyValue('--link-color') || '').trim();
|
|
102
|
+
const accent = /^#|^rgb/.test(accentRaw) ? accentRaw : mixColor(fg, bg, 0.45);
|
|
103
|
+
const borderRaw = (style.getPropertyValue('--border-color') || '').trim();
|
|
104
|
+
const border = /^#|^rgb/.test(borderRaw) ? borderRaw : mixColor(fg, bg, 0.45);
|
|
105
|
+
return {
|
|
106
|
+
background: bg,
|
|
107
|
+
primaryColor: mixColor(fg, bg, 0.18),
|
|
108
|
+
primaryTextColor: fg,
|
|
109
|
+
primaryBorderColor: accent,
|
|
110
|
+
lineColor: mixColor(fg, bg, 0.28),
|
|
111
|
+
secondaryColor: mixColor(fg, bg, 0.28),
|
|
112
|
+
tertiaryColor: mixColor(fg, bg, 0.18),
|
|
113
|
+
clusterBkg: mixColor(fg, bg, 0.08),
|
|
114
|
+
clusterBorder: border,
|
|
115
|
+
edgeLabelBackground: bg,
|
|
116
|
+
fontFamily: style.fontFamily,
|
|
117
|
+
fontSize: '28px',
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 水合幻灯片里的 mermaid 图与块级公式。
|
|
123
|
+
* 渲染层(src/slides/render.ts)把 ```mermaid 围栏输出为
|
|
124
|
+
* `<pre class="mermaid-src" hidden>`、`$$` 块输出为 `<span class="math-src" hidden>`,
|
|
125
|
+
* 由这里替换成 SVG / KaTeX HTML;失败时保留源码兜底。
|
|
126
|
+
*/
|
|
127
|
+
async function hydrateSlides(): Promise<void> {
|
|
128
|
+
const host = document.getElementById('slides-host');
|
|
129
|
+
if (!host) return;
|
|
130
|
+
|
|
131
|
+
for (const node of Array.from(host.querySelectorAll<HTMLElement>('pre.mermaid-src'))) {
|
|
132
|
+
const holder = node.parentElement;
|
|
133
|
+
if (!holder) continue;
|
|
134
|
+
try {
|
|
135
|
+
const { renderAsync } = await loadPele();
|
|
136
|
+
const { svg } = await renderAsync(node.textContent ?? '', {
|
|
137
|
+
fontFamily: DIAGRAM_FONT,
|
|
138
|
+
fontFamilyMono: DIAGRAM_MONO_FONT,
|
|
139
|
+
});
|
|
140
|
+
holder.innerHTML = svg;
|
|
141
|
+
node.remove();
|
|
142
|
+
} catch (e) {
|
|
143
|
+
console.error('[ColaView] slide mermaid render failed:', e);
|
|
144
|
+
/* 渲染失败保留源码 */
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
for (const node of Array.from(host.querySelectorAll<HTMLElement>('span.math-src'))) {
|
|
149
|
+
const holder = node.parentElement;
|
|
150
|
+
if (!holder) continue;
|
|
151
|
+
try {
|
|
152
|
+
katex.render(node.textContent ?? '', holder, { displayMode: true, throwOnError: false });
|
|
153
|
+
} catch (e) {
|
|
154
|
+
console.error('[ColaView] slide math render failed:', e);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 幻灯片样式表只注入一次(几十 KB,逐键更新时不重复下发)。 */
|
|
160
|
+
function injectSlidesCss(css: string | undefined): void {
|
|
161
|
+
if (!css) return;
|
|
162
|
+
let el = document.getElementById('colaview-slides-css');
|
|
163
|
+
if (!el) {
|
|
164
|
+
el = document.createElement('style');
|
|
165
|
+
el.id = 'colaview-slides-css';
|
|
166
|
+
document.head.appendChild(el);
|
|
167
|
+
}
|
|
168
|
+
el.textContent = css;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** 整副牌按面板宽度整体缩放(纸面固定 1920×1080,缩放只改视觉尺寸)。 */
|
|
172
|
+
function fitSlidesStage(): void {
|
|
173
|
+
const host = document.getElementById('slides-host');
|
|
174
|
+
const stage = host?.querySelector<HTMLElement>('.ct-stage');
|
|
175
|
+
if (!host || !stage) return;
|
|
176
|
+
const available = host.clientWidth || window.innerWidth;
|
|
177
|
+
const scale = Math.min(1, Math.max(0.1, (available - 32) / 1920));
|
|
178
|
+
stage.style.setProperty('--deck-scale', scale.toFixed(4));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function renderSlides(html: string): void {
|
|
182
|
+
const host = document.getElementById('slides-host');
|
|
183
|
+
if (!host) return;
|
|
184
|
+
host.innerHTML = html;
|
|
185
|
+
fitSlidesStage();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function setMode(next: 'document' | 'slides'): void {
|
|
189
|
+
mode = next;
|
|
190
|
+
document.body.classList.toggle('slides-mode', mode === 'slides');
|
|
191
|
+
if (mode === 'slides') {
|
|
192
|
+
fitSlidesStage();
|
|
193
|
+
window.scrollTo(0, 0);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
window.addEventListener('resize', () => {
|
|
198
|
+
if (mode === 'slides') fitSlidesStage();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
function getScrollContainer(): HTMLElement | null {
|
|
202
|
+
// CodeMirror 6 把滚动放在 .cm-scroller 上,#editor 本身不动
|
|
203
|
+
return document.querySelector<HTMLElement>('.cm-scroller');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function setupScrollSync(): void {
|
|
207
|
+
const scroller = getScrollContainer();
|
|
208
|
+
if (!scroller) return;
|
|
209
|
+
|
|
210
|
+
scroller.addEventListener('scroll', (e) => {
|
|
211
|
+
const target = e.target as HTMLElement;
|
|
212
|
+
if (scrollDebounceTimer) clearTimeout(scrollDebounceTimer);
|
|
213
|
+
|
|
214
|
+
scrollDebounceTimer = window.setTimeout(() => {
|
|
215
|
+
if (receivedScrollFromEditor) {
|
|
216
|
+
receivedScrollFromEditor = false;
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const scrollPercent = target.scrollTop / (target.scrollHeight - target.clientHeight);
|
|
221
|
+
vscode.postMessage({
|
|
222
|
+
type: 'scrollSync',
|
|
223
|
+
scrollPercent: Math.max(0, Math.min(1, scrollPercent)),
|
|
224
|
+
});
|
|
225
|
+
}, 50);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function scrollToPercent(percent: number): void {
|
|
230
|
+
const scroller = getScrollContainer();
|
|
231
|
+
if (!scroller || !handle) return;
|
|
232
|
+
|
|
233
|
+
receivedScrollFromEditor = true;
|
|
234
|
+
|
|
235
|
+
const maxScroll = scroller.scrollHeight - scroller.clientHeight;
|
|
236
|
+
const targetScrollTop = Math.max(0, Math.min(percent * maxScroll, maxScroll));
|
|
237
|
+
|
|
238
|
+
scroller.scrollTo({
|
|
239
|
+
top: targetScrollTop,
|
|
240
|
+
behavior: 'smooth'
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function scrollToLine(line: number, totalLines: number, topRatio: number = 0.3): void {
|
|
245
|
+
const scroller = getScrollContainer();
|
|
246
|
+
if (!scroller || !handle) return;
|
|
247
|
+
|
|
248
|
+
receivedScrollFromEditor = true;
|
|
249
|
+
|
|
250
|
+
const contentEl = document.querySelector<HTMLElement>('#editor .cm-content');
|
|
251
|
+
if (!contentEl) {
|
|
252
|
+
const scrollPercent = totalLines > 1 ? line / (totalLines - 1) : 0;
|
|
253
|
+
scrollToPercent(scrollPercent);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// CM6 只渲染视口内的内容,所以只能用比例估算
|
|
258
|
+
const scrollPercent = totalLines > 1 ? line / (totalLines - 1) : 0;
|
|
259
|
+
const maxScroll = scroller.scrollHeight - scroller.clientHeight;
|
|
260
|
+
const targetScrollTop = Math.max(0, Math.min(scrollPercent * maxScroll, maxScroll));
|
|
261
|
+
|
|
262
|
+
scroller.scrollTo({
|
|
263
|
+
top: targetScrollTop,
|
|
264
|
+
behavior: 'smooth'
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function injectThemeCSS(css: string | undefined, themeName: string, isCustom: boolean): void {
|
|
269
|
+
if (!css) return;
|
|
270
|
+
let el = document.getElementById('colaview-theme-css');
|
|
271
|
+
if (!el) {
|
|
272
|
+
el = document.createElement('style');
|
|
273
|
+
el.id = 'colaview-theme-css';
|
|
274
|
+
document.head.appendChild(el);
|
|
275
|
+
}
|
|
276
|
+
// 主题文件的变量写在 :root 上,而引擎把同一批变量写在 body.theme-X 上
|
|
277
|
+
// (特异性 0,1,1 > 0,1,0),直接注入 :root 会被引擎盖掉。这里把选择器
|
|
278
|
+
// 提升到 body.theme-X:与引擎同特异性且后注入,主题文件里的新值才生效。
|
|
279
|
+
const scope = isCustom ? 'body.theme-custom' : `body.theme-${themeName}`;
|
|
280
|
+
el.textContent = css.replace(/:root\b/g, `:root, ${scope}`);
|
|
281
|
+
ensureDshBaseCss();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* DSH(DeepSeek Harness)插件渲染对齐层。
|
|
286
|
+
*
|
|
287
|
+
* DSH 的文档渲染 = 主题令牌(:root)+ BASE_CSS 基础版式,且 BASE_CSS 后注入、
|
|
288
|
+
* 同特异性下覆盖主题元素规则。这里把 BASE_CSS 翻译到编辑器 DOM(.cm-content
|
|
289
|
+
* + .cm-md-*),并用 `:where()` 降权,形成三级级联:
|
|
290
|
+
*
|
|
291
|
+
* 引擎默认 < 本层(:where,令牌兜底) < 主题文件个性规则
|
|
292
|
+
*
|
|
293
|
+
* 令牌与 DSH BASE_CSS 完全同名同兜底值:--font-body / --editor-font-size /
|
|
294
|
+
* --leading-body(1.78) / --editor-column-width(46rem) / 字距 0.01em /
|
|
295
|
+
* --type-* 标题度量 / --seed-emphasis / --layout-radius 等。
|
|
296
|
+
*/
|
|
297
|
+
const DSH_BASE_CSS = `
|
|
298
|
+
#editor :where(.cm-content) {
|
|
299
|
+
font-size: var(--editor-font-size, 16px);
|
|
300
|
+
line-height: var(--leading-body, 1.78);
|
|
301
|
+
letter-spacing: 0.01em;
|
|
302
|
+
width: min(100%, calc(var(--editor-column-width, 46rem) + 4.4rem));
|
|
303
|
+
padding: 3.4rem 2.2rem 6rem;
|
|
304
|
+
}
|
|
305
|
+
#editor :where(.cm-content) :where(.cm-md-atxheading1, .cm-md-atxheading2, .cm-md-atxheading3, .cm-md-atxheading4, .cm-md-atxheading5, .cm-md-atxheading6) {
|
|
306
|
+
font-weight: var(--weight-display, 700);
|
|
307
|
+
line-height: var(--leading-display, 1.3);
|
|
308
|
+
letter-spacing: var(--tracking-display, -0.01em);
|
|
309
|
+
border-bottom: none;
|
|
310
|
+
}
|
|
311
|
+
#editor :where(.cm-content) :where(.cm-md-atxheading1) { font-size: var(--type-display, 2rem); }
|
|
312
|
+
#editor :where(.cm-content) :where(.cm-md-atxheading2) { font-size: var(--type-section, 1.5rem); letter-spacing: var(--tracking-section, 0); }
|
|
313
|
+
#editor :where(.cm-content) :where(.cm-md-atxheading3) { font-size: var(--type-subhead, 1.2rem); font-weight: var(--weight-subhead, 650); letter-spacing: var(--tracking-subhead, 0); }
|
|
314
|
+
#editor :where(.cm-content) :where(.cm-md-atxheading4) { font-size: var(--type-body, 1.05rem); }
|
|
315
|
+
#editor :where(.cm-content) :where(.cm-md-strong) {
|
|
316
|
+
font-weight: var(--weight-body, 700);
|
|
317
|
+
color: var(--seed-emphasis, inherit);
|
|
318
|
+
}
|
|
319
|
+
#editor :where(.cm-content) :where(.cm-md-strike) { color: var(--text-muted, #8a8a8a); }
|
|
320
|
+
#editor :where(.cm-content) :where(.cm-md-highlight) {
|
|
321
|
+
background: var(--highlight-bg, rgba(255, 214, 102, 0.42));
|
|
322
|
+
color: inherit;
|
|
323
|
+
padding: 0 0.15em;
|
|
324
|
+
border-radius: 2px;
|
|
325
|
+
}
|
|
326
|
+
#editor :where(.cm-content) :where(.cm-md-link) {
|
|
327
|
+
color: var(--link-color, #0969da);
|
|
328
|
+
text-decoration: underline;
|
|
329
|
+
text-underline-offset: 0.18em;
|
|
330
|
+
text-decoration-thickness: 1px;
|
|
331
|
+
}
|
|
332
|
+
#editor :where(.cm-content) :where(.cm-md-inlinecode) {
|
|
333
|
+
font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
|
|
334
|
+
font-size: 0.9em;
|
|
335
|
+
background: var(--code-bg, rgba(0, 0, 0, 0.05));
|
|
336
|
+
padding: 0.15em 0.38em;
|
|
337
|
+
border-radius: var(--layout-radius, 4px);
|
|
338
|
+
}
|
|
339
|
+
#editor :where(.cm-content) :where(.cm-md-codeblock) {
|
|
340
|
+
font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
|
|
341
|
+
font-size: 0.86em;
|
|
342
|
+
line-height: 1.62;
|
|
343
|
+
background: var(--code-block-bg, #23252b);
|
|
344
|
+
color: var(--code-block-text, #e7e7ea);
|
|
345
|
+
}
|
|
346
|
+
#editor :where(.cm-content) :where(.cm-md-codeblock-first) {
|
|
347
|
+
border-top-left-radius: var(--layout-radius, 8px);
|
|
348
|
+
border-top-right-radius: var(--layout-radius, 8px);
|
|
349
|
+
}
|
|
350
|
+
#editor :where(.cm-content) :where(.cm-md-codeblock-last) {
|
|
351
|
+
border-bottom-left-radius: var(--layout-radius, 8px);
|
|
352
|
+
border-bottom-right-radius: var(--layout-radius, 8px);
|
|
353
|
+
}
|
|
354
|
+
#editor :where(.cm-content) :where(.cm-md-blockquote) {
|
|
355
|
+
color: var(--text-secondary, #555555);
|
|
356
|
+
border-left: 3px solid var(--blockquote-border, var(--seed-accent, #b0401f));
|
|
357
|
+
background: var(--blockquote-bg, transparent);
|
|
358
|
+
}
|
|
359
|
+
#editor :where(.cm-content) :where(.cm-md-listmark) {
|
|
360
|
+
color: var(--layout-list-marker, var(--seed-accent, currentColor));
|
|
361
|
+
}
|
|
362
|
+
#editor :where(.cm-content) :where(.cm-md-hr) {
|
|
363
|
+
border-top: 1px solid var(--border-color, var(--seed-border, #d8d6d2));
|
|
364
|
+
margin: 2.6em 0;
|
|
365
|
+
}
|
|
366
|
+
#editor :where(.cm-content) :where(.cm-md-table-widget) { font-size: 0.94em; }
|
|
367
|
+
#editor :where(.cm-content) :where(.cm-md-table-widget th) {
|
|
368
|
+
background: var(--table-header-bg, var(--seed-panel, #eaeae9));
|
|
369
|
+
font-weight: 650;
|
|
370
|
+
padding: 0.6em 0.8em;
|
|
371
|
+
}
|
|
372
|
+
#editor :where(.cm-content) :where(.cm-md-table-widget td) { padding: 0.58em 0.8em; }
|
|
373
|
+
::selection { background: var(--selection-bg, rgba(176, 64, 31, 0.18)); }
|
|
374
|
+
`;
|
|
375
|
+
|
|
376
|
+
/** 保证对齐层存在且位于主题样式之后(DSH 语义:后注入者在同特异性下胜出)。 */
|
|
377
|
+
function ensureDshBaseCss(): void {
|
|
378
|
+
let el = document.getElementById('colaview-dsh-base');
|
|
379
|
+
if (!el) {
|
|
380
|
+
el = document.createElement('style');
|
|
381
|
+
el.id = 'colaview-dsh-base';
|
|
382
|
+
el.textContent = DSH_BASE_CSS;
|
|
383
|
+
}
|
|
384
|
+
document.head.appendChild(el);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 确保 body 挂上主题类。colamd 的 buildExportHTML 会把 document.body 的 class
|
|
389
|
+
* 原样复制到导出 HTML 的 <body>,而它收集的主题规则全部以 body.theme-X 为作用域
|
|
390
|
+
* (字体、强调色、主题变量)。预览本身靠注入的 :root 变量 + #editor 规则就能生效,
|
|
391
|
+
* 不依赖 body 类 —— 但导出不补上这个类就会整体丢格式。
|
|
392
|
+
*/
|
|
393
|
+
function ensureBodyThemeClass(): void {
|
|
394
|
+
const builtins = themeListData?.builtins ?? [];
|
|
395
|
+
if (builtins.length > 0 && !builtins.includes(currentTheme)) {
|
|
396
|
+
document.body.classList.add('theme-custom');
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
// 内置主题:与 colamd 内部映射一致(theme-<name>)
|
|
400
|
+
document.body.classList.add(`theme-${currentTheme}`);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
window.addEventListener('message', async (event: MessageEvent) => {
|
|
404
|
+
const data = event.data;
|
|
405
|
+
switch (data.type) {
|
|
406
|
+
case 'init': {
|
|
407
|
+
try {
|
|
408
|
+
currentTheme = data.theme || 'elegant';
|
|
409
|
+
if (data.labels) labels = { ...labels, ...data.labels };
|
|
410
|
+
injectThemeCSS(data.themeCSS, currentTheme, !!(data.customCSS && data.theme));
|
|
411
|
+
|
|
412
|
+
// 2.7.9 API: 先创建编辑器(options 中不含 theme)
|
|
413
|
+
handle = await createColaMDEditor({
|
|
414
|
+
rootId: 'editor',
|
|
415
|
+
editable: false,
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// 然后应用主题
|
|
419
|
+
if (data.customCSS && data.theme) {
|
|
420
|
+
handle.applyTheme('custom:' + data.theme, data.customCSS);
|
|
421
|
+
} else {
|
|
422
|
+
handle.applyTheme(currentTheme);
|
|
423
|
+
}
|
|
424
|
+
ensureBodyThemeClass();
|
|
425
|
+
|
|
426
|
+
// 最后设置 markdown
|
|
427
|
+
if (data.markdown) {
|
|
428
|
+
handle.setMarkdown(data.markdown);
|
|
429
|
+
currentMarkdown = data.markdown;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
setupScrollSync();
|
|
433
|
+
if (data.mode === 'slides') setMode('slides');
|
|
434
|
+
} catch (e) {
|
|
435
|
+
console.error('[ColaView] init failed:', e);
|
|
436
|
+
vscode.postMessage({ type: 'error', message: String(e) });
|
|
437
|
+
}
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
case 'mode': {
|
|
441
|
+
setMode(data.mode === 'slides' ? 'slides' : 'document');
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
case 'slides': {
|
|
445
|
+
injectSlidesCss(data.css);
|
|
446
|
+
if (data.bodyClass) {
|
|
447
|
+
document.body.classList
|
|
448
|
+
.forEach((cls) => { if (cls.startsWith('theme-')) document.body.classList.remove(cls); });
|
|
449
|
+
document.body.classList.add(data.bodyClass);
|
|
450
|
+
}
|
|
451
|
+
setMode('slides');
|
|
452
|
+
renderSlides(data.html || '');
|
|
453
|
+
void hydrateSlides();
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
case 'update': {
|
|
457
|
+
if (!handle) return;
|
|
458
|
+
if (data.markdown !== undefined && data.markdown !== currentMarkdown) {
|
|
459
|
+
handle.setMarkdown(data.markdown);
|
|
460
|
+
currentMarkdown = data.markdown;
|
|
461
|
+
}
|
|
462
|
+
if (data.theme) {
|
|
463
|
+
currentTheme = data.theme;
|
|
464
|
+
injectThemeCSS(data.themeCSS, data.theme, !!data.customCSS);
|
|
465
|
+
if (data.customCSS) {
|
|
466
|
+
handle.applyTheme('custom:' + data.theme, data.customCSS);
|
|
467
|
+
} else {
|
|
468
|
+
handle.applyTheme(data.theme);
|
|
469
|
+
}
|
|
470
|
+
ensureBodyThemeClass();
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
case 'exportHTML': {
|
|
475
|
+
if (!handle) {
|
|
476
|
+
vscode.postMessage({ type: 'exportHTMLResult', html: '' });
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
ensureBodyThemeClass();
|
|
481
|
+
const html = await handle.buildExportHTML({
|
|
482
|
+
title: data.title || document.title || 'Document',
|
|
483
|
+
});
|
|
484
|
+
vscode.postMessage({ type: 'exportHTMLResult', html });
|
|
485
|
+
} catch (e) {
|
|
486
|
+
console.error('[ColaView] exportHTML failed:', e);
|
|
487
|
+
vscode.postMessage({ type: 'exportHTMLResult', html: '', error: String(e) });
|
|
488
|
+
}
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
case 'updateTheme': {
|
|
492
|
+
if (!handle || !data.theme) return;
|
|
493
|
+
currentTheme = data.theme;
|
|
494
|
+
injectThemeCSS(data.themeCSS, data.theme, !!data.customCSS);
|
|
495
|
+
if (data.customCSS) {
|
|
496
|
+
handle.applyTheme('custom:' + data.theme, data.customCSS);
|
|
497
|
+
} else {
|
|
498
|
+
handle.applyTheme(data.theme);
|
|
499
|
+
}
|
|
500
|
+
ensureBodyThemeClass();
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
case 'applyCustomTheme': {
|
|
504
|
+
if (!data.css || !handle) return;
|
|
505
|
+
const themeName = data.name || currentTheme;
|
|
506
|
+
handle.applyTheme('custom:' + themeName, data.css);
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
case 'themeList': {
|
|
510
|
+
themeListData = {
|
|
511
|
+
builtins: data.builtins || [],
|
|
512
|
+
customs: data.customs || [],
|
|
513
|
+
current: data.current || currentTheme,
|
|
514
|
+
};
|
|
515
|
+
refreshThemeSubmenu();
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
case 'editorScroll': {
|
|
519
|
+
if (data.scrollPercent !== undefined) {
|
|
520
|
+
scrollToPercent(data.scrollPercent);
|
|
521
|
+
} else if (data.line !== undefined) {
|
|
522
|
+
scrollToLine(data.line, data.totalLines || 100, data.topRatio || 0.3);
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
// ── Context Menu ──────────────────────────────────────────────
|
|
530
|
+
let menuEl: HTMLElement | null = null;
|
|
531
|
+
let themeSubmenuEl: HTMLElement | null = null;
|
|
532
|
+
|
|
533
|
+
function closeMenu(): void {
|
|
534
|
+
if (menuEl) {
|
|
535
|
+
menuEl.remove();
|
|
536
|
+
menuEl = null;
|
|
537
|
+
themeSubmenuEl = null;
|
|
538
|
+
}
|
|
539
|
+
document.removeEventListener('click', closeMenu);
|
|
540
|
+
document.removeEventListener('contextmenu', closeMenu);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function createMenuItem(label: string, onClick: () => void): HTMLElement {
|
|
544
|
+
const item = document.createElement('div');
|
|
545
|
+
item.className = 'ctx-menu-item';
|
|
546
|
+
item.textContent = label;
|
|
547
|
+
item.addEventListener('click', (e) => {
|
|
548
|
+
e.stopPropagation();
|
|
549
|
+
closeMenu();
|
|
550
|
+
onClick();
|
|
551
|
+
});
|
|
552
|
+
return item;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function createMenuSeparator(): HTMLElement {
|
|
556
|
+
const sep = document.createElement('div');
|
|
557
|
+
sep.className = 'ctx-menu-separator';
|
|
558
|
+
return sep;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function createGroupHeader(label: string): HTMLElement {
|
|
562
|
+
const header = document.createElement('div');
|
|
563
|
+
header.className = 'ctx-menu-group';
|
|
564
|
+
header.textContent = label;
|
|
565
|
+
return header;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function createThemeItem(name: string, label: string): HTMLElement {
|
|
569
|
+
const item = document.createElement('div');
|
|
570
|
+
item.className = 'ctx-menu-item' + (themeListData && name === themeListData.current ? ' ctx-menu-active' : '');
|
|
571
|
+
item.textContent = label;
|
|
572
|
+
item.addEventListener('click', (e) => {
|
|
573
|
+
e.stopPropagation();
|
|
574
|
+
if (themeListData) themeListData.current = name;
|
|
575
|
+
closeMenu();
|
|
576
|
+
vscode.postMessage({ type: 'switchTheme', name });
|
|
577
|
+
});
|
|
578
|
+
return item;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function buildThemeSubmenu(): HTMLElement {
|
|
582
|
+
const submenu = document.createElement('div');
|
|
583
|
+
submenu.className = 'ctx-menu-submenu';
|
|
584
|
+
themeSubmenuEl = submenu;
|
|
585
|
+
|
|
586
|
+
if (!themeListData) {
|
|
587
|
+
vscode.postMessage({ type: 'requestThemeList' });
|
|
588
|
+
const loading = document.createElement('div');
|
|
589
|
+
loading.className = 'ctx-menu-item ctx-menu-disabled';
|
|
590
|
+
loading.textContent = labels.loading;
|
|
591
|
+
submenu.appendChild(loading);
|
|
592
|
+
return submenu;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// 内置主题按亮色/暗色分组,自定义主题单独一组;子菜单限高可滚动,项多时不会超出页面。
|
|
596
|
+
const pretty = (name: string): string => name.charAt(0).toUpperCase() + name.slice(1).replace(/-/g, ' ');
|
|
597
|
+
const lights = themeListData.builtins.filter((n) => !DARK_BUILTIN_THEMES.has(n));
|
|
598
|
+
const darks = themeListData.builtins.filter((n) => DARK_BUILTIN_THEMES.has(n));
|
|
599
|
+
|
|
600
|
+
submenu.appendChild(createGroupHeader(labels.groupLight));
|
|
601
|
+
for (const name of lights) {
|
|
602
|
+
submenu.appendChild(createThemeItem(name, pretty(name)));
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
submenu.appendChild(createGroupHeader(labels.groupDark));
|
|
606
|
+
for (const name of darks) {
|
|
607
|
+
submenu.appendChild(createThemeItem(name, pretty(name)));
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (themeListData.customs.length > 0) {
|
|
611
|
+
submenu.appendChild(createGroupHeader(labels.groupCustom));
|
|
612
|
+
for (const name of themeListData.customs) {
|
|
613
|
+
submenu.appendChild(createThemeItem(name, name));
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
return submenu;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function refreshThemeSubmenu(): void {
|
|
621
|
+
if (!menuEl || !themeSubmenuEl) return;
|
|
622
|
+
const parent = themeSubmenuEl.parentElement;
|
|
623
|
+
if (!parent) return;
|
|
624
|
+
const newSubmenu = buildThemeSubmenu();
|
|
625
|
+
parent.replaceChild(newSubmenu, themeSubmenuEl);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
function showMenu(x: number, y: number): void {
|
|
629
|
+
closeMenu();
|
|
630
|
+
|
|
631
|
+
const menu = document.createElement('div');
|
|
632
|
+
menu.className = 'ctx-menu';
|
|
633
|
+
menu.style.left = x + 'px';
|
|
634
|
+
menu.style.top = y + 'px';
|
|
635
|
+
menuEl = menu;
|
|
636
|
+
|
|
637
|
+
// 全屏显示固定排在最前面,文档/幻灯片模式均可放映;导出项按当前模式区分:文档给 PDF/HTML/Word,幻灯片只给 PDF/PPTX。
|
|
638
|
+
menu.appendChild(createMenuItem(labels.present, () => {
|
|
639
|
+
vscode.postMessage({ type: 'requestPresent', mode });
|
|
640
|
+
}));
|
|
641
|
+
menu.appendChild(createMenuSeparator());
|
|
642
|
+
|
|
643
|
+
menu.appendChild(createMenuItem(labels.exportPdf, () => {
|
|
644
|
+
vscode.postMessage({ type: 'requestExportPDF' });
|
|
645
|
+
}));
|
|
646
|
+
|
|
647
|
+
if (mode === 'slides') {
|
|
648
|
+
menu.appendChild(createMenuItem(labels.exportPptx, () => {
|
|
649
|
+
vscode.postMessage({ type: 'requestExportPPTX' });
|
|
650
|
+
}));
|
|
651
|
+
} else {
|
|
652
|
+
menu.appendChild(createMenuItem(labels.exportHtml, () => {
|
|
653
|
+
vscode.postMessage({ type: 'requestExportHTML' });
|
|
654
|
+
}));
|
|
655
|
+
menu.appendChild(createMenuItem(labels.exportWord, () => {
|
|
656
|
+
vscode.postMessage({ type: 'requestExportWord' });
|
|
657
|
+
}));
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
menu.appendChild(createMenuSeparator());
|
|
661
|
+
|
|
662
|
+
const themeItem = document.createElement('div');
|
|
663
|
+
themeItem.className = 'ctx-menu-item ctx-menu-has-submenu';
|
|
664
|
+
themeItem.textContent = labels.theme;
|
|
665
|
+
themeItem.appendChild(buildThemeSubmenu());
|
|
666
|
+
menu.appendChild(themeItem);
|
|
667
|
+
|
|
668
|
+
menu.appendChild(createMenuSeparator());
|
|
669
|
+
|
|
670
|
+
menu.appendChild(createMenuItem(labels.importTheme, () => {
|
|
671
|
+
vscode.postMessage({ type: 'requestImportTheme' });
|
|
672
|
+
}));
|
|
673
|
+
|
|
674
|
+
document.body.appendChild(menu);
|
|
675
|
+
|
|
676
|
+
const rect = menu.getBoundingClientRect();
|
|
677
|
+
if (rect.right > window.innerWidth) {
|
|
678
|
+
menu.style.left = (x - rect.width) + 'px';
|
|
679
|
+
}
|
|
680
|
+
if (rect.bottom > window.innerHeight) {
|
|
681
|
+
menu.style.top = (y - rect.height) + 'px';
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
setTimeout(() => {
|
|
685
|
+
document.addEventListener('click', closeMenu);
|
|
686
|
+
document.addEventListener('contextmenu', closeMenu);
|
|
687
|
+
}, 0);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
document.addEventListener('contextmenu', (e) => {
|
|
691
|
+
e.preventDefault();
|
|
692
|
+
showMenu(e.clientX, e.clientY);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
vscode.postMessage({ type: 'ready' });
|