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.
Files changed (43) hide show
  1. package/.release-notes-v0.4.1.md +29 -0
  2. package/.vscode/settings.json +1 -1
  3. package/CHANGELOG.md +45 -16
  4. package/README.md +318 -287
  5. package/README_CN.md +318 -287
  6. package/RELEASE_NOTES.md +80 -21
  7. package/esbuild.js +2 -0
  8. package/package.json +204 -164
  9. package/package.nls.json +14 -0
  10. package/package.nls.zh-cn.json +14 -0
  11. package/src/commands/commands.ts +48 -0
  12. package/src/docx/docx-builder.ts +241 -0
  13. package/src/export/docx-exporter.ts +961 -961
  14. package/src/export/html-exporter.ts +8 -2
  15. package/src/export/pdf-exporter.ts +69 -0
  16. package/src/export/pptx-exporter.ts +63 -0
  17. package/src/export/render-export-images.ts +386 -386
  18. package/src/export/theme-style.ts +195 -195
  19. package/src/pptx/pptx-builder.ts +249 -0
  20. package/src/pptx/zip.ts +158 -0
  21. package/src/present/presenter.ts +89 -0
  22. package/src/preview/preview-manager.ts +783 -541
  23. package/src/preview/webview/app.ts +695 -370
  24. package/src/preview/webview/index.html +167 -146
  25. package/src/slides/deck.ts +408 -0
  26. package/src/slides/markdown.ts +420 -0
  27. package/src/slides/render.ts +580 -0
  28. package/src/slides/slides.css +1082 -0
  29. package/src/themes/built-in/bear.css +64 -13
  30. package/src/themes/built-in/dark.css +64 -5
  31. package/src/themes/built-in/dracula.css +64 -5
  32. package/src/themes/built-in/elegant.css +75 -11
  33. package/src/themes/built-in/gruvbox.css +65 -6
  34. package/src/themes/built-in/light.css +62 -2
  35. package/src/themes/built-in/midnight.css +64 -6
  36. package/src/themes/built-in/nord.css +64 -5
  37. package/src/themes/built-in/notion.css +63 -11
  38. package/src/themes/built-in/sepia.css +65 -10
  39. package/src/themes/built-in/solarized-dark.css +64 -5
  40. package/src/themes/built-in/writer.css +66 -19
  41. package/src/themes/theme-manager.ts +207 -180
  42. package/src/types/colamd-renderer.d.ts +47 -47
  43. package/src/types/pele-lazy.d.ts +21 -0
@@ -1,180 +1,207 @@
1
- // 主题管理器 — 支持内置主题 + .themes 目录自定义主题
2
- import * as vscode from 'vscode';
3
- import * as path from 'path';
4
- import * as fs from 'fs';
5
-
6
- const BUILTIN_THEMES = [
7
- 'light', 'dark', 'elegant', 'sepia', 'notion', 'bear',
8
- 'writer', 'solarized-dark', 'nord', 'gruvbox', 'dracula', 'midnight'
9
- ] as const;
10
- type ThemeName = typeof BUILTIN_THEMES[number];
11
-
12
- const customThemeCache = new Map<string, string>();
13
-
14
- const THEME_CLASS_MAP: Record<string, string> = {
15
- light: 'theme-light',
16
- dark: 'theme-dark',
17
- elegant: 'theme-elegant',
18
- sepia: 'theme-sepia',
19
- notion: 'theme-notion',
20
- bear: 'theme-bear',
21
- writer: 'theme-writer',
22
- 'solarized-dark': 'theme-solarized-dark',
23
- nord: 'theme-nord',
24
- gruvbox: 'theme-gruvbox',
25
- dracula: 'theme-dracula',
26
- midnight: 'theme-midnight',
27
- };
28
-
29
- /**
30
- * 获取资源文件目录 — 优先使用 out/ 目录(打包后),回退到 src/(开发模式)
31
- */
32
- function getResDir(context: vscode.ExtensionContext, ...segments: string[]): string {
33
- const outDir = path.join(context.extensionPath, 'out', ...segments);
34
- if (fs.existsSync(outDir)) {
35
- return outDir;
36
- }
37
- return path.join(context.extensionPath, 'src', ...segments);
38
- }
39
-
40
- /**
41
- * 主题管理器 — 负责主题 CSS 加载、切换、自定义主题导入
42
- */
43
- export class ThemeManager {
44
- private static context: vscode.ExtensionContext;
45
-
46
- /**
47
- * 初始化主题管理器
48
- */
49
- static init(context: vscode.ExtensionContext): void {
50
- this.context = context;
51
- }
52
-
53
- /** 获取当前激活的主题名称 */
54
- static getCurrentTheme(): string {
55
- return vscode.workspace.getConfiguration('colaview').get('theme', 'elegant');
56
- }
57
-
58
- /** 获取主题对应的 CSS class */
59
- static getThemeClass(theme: string): string {
60
- return THEME_CLASS_MAP[theme] || `theme-${theme}`;
61
- }
62
-
63
- /** 获取内置主题列表 */
64
- static getBuiltinThemes(): string[] {
65
- return [...BUILTIN_THEMES];
66
- }
67
-
68
- /**
69
- * 获取 .themes 目录路径(当前工作区根目录下)
70
- * 如果没有打开工作区,返回 null
71
- */
72
- static getThemesDir(): string | null {
73
- const workspaceFolders = vscode.workspace.workspaceFolders;
74
- if (!workspaceFolders || workspaceFolders.length === 0) return null;
75
- return path.join(workspaceFolders[0].uri.fsPath, '.themes');
76
- }
77
-
78
- /**
79
- * 确保 .themes 目录存在
80
- */
81
- private static ensureThemesDir(): string | null {
82
- const dir = this.getThemesDir();
83
- if (!dir) return null;
84
- if (!fs.existsSync(dir)) {
85
- fs.mkdirSync(dir, { recursive: true });
86
- }
87
- return dir;
88
- }
89
-
90
- /**
91
- * 扫描 .themes 目录中的自定义主题
92
- * 返回 { name, path } 列表
93
- */
94
- static scanCustomThemes(): Array<{ name: string; filePath: string }> {
95
- const dir = this.getThemesDir();
96
- if (!dir || !fs.existsSync(dir)) return [];
97
-
98
- const results: Array<{ name: string; filePath: string }> = [];
99
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
100
- if (entry.isFile() && entry.name.endsWith('.css')) {
101
- const name = path.basename(entry.name, '.css');
102
- results.push({ name, filePath: path.join(dir, entry.name) });
103
- }
104
- }
105
- return results;
106
- }
107
-
108
- /**
109
- * 获取完整主题列表(内置 + 自定义)
110
- */
111
- static getThemeList(): { builtins: string[]; customs: string[]; current: string } {
112
- const builtins = this.getBuiltinThemes();
113
- const customs = this.scanCustomThemes().map(t => t.name);
114
- const current = this.getCurrentTheme();
115
- return { builtins, customs, current };
116
- }
117
-
118
- /**
119
- * 加载主题 CSS 内容
120
- * 优先从 .themes/ 加载自定义主题,再加载内置主题
121
- */
122
- static loadThemeCSS(theme: string): string {
123
- // 先检查 .themes 目录
124
- const themesDir = this.getThemesDir();
125
- if (themesDir) {
126
- const customPath = path.join(themesDir, `${theme}.css`);
127
- if (fs.existsSync(customPath)) {
128
- return fs.readFileSync(customPath, 'utf-8');
129
- }
130
- }
131
- // 再检查内存缓存
132
- if (customThemeCache.has(theme)) {
133
- return customThemeCache.get(theme)!;
134
- }
135
- // 最后加载内置主题
136
- if (BUILTIN_THEMES.includes(theme as ThemeName)) {
137
- const cssPath = path.join(
138
- getResDir(this.context, 'themes', 'built-in'), `${theme}.css`
139
- );
140
- if (fs.existsSync(cssPath)) {
141
- return fs.readFileSync(cssPath, 'utf-8');
142
- }
143
- }
144
- return '';
145
- }
146
-
147
- /**
148
- * 导入自定义主题 — 复制到 .themes 目录并缓存
149
- * @param uri 源 CSS 文件 URI
150
- */
151
- static async importCustomTheme(uri: vscode.Uri): Promise<{ name: string; css: string } | null> {
152
- const themesDir = this.ensureThemesDir();
153
- if (!themesDir) {
154
- vscode.window.showWarningMessage('No workspace folder open. Cannot import theme.');
155
- return null;
156
- }
157
-
158
- const css = Buffer.from(await vscode.workspace.fs.readFile(uri)).toString('utf-8');
159
- const name = path.basename(uri.fsPath, '.css');
160
- const destPath = path.join(themesDir, `${name}.css`);
161
-
162
- // 复制文件到 .themes 目录
163
- fs.writeFileSync(destPath, css, 'utf-8');
164
-
165
- // 更新内存缓存
166
- customThemeCache.set(name, css);
167
-
168
- vscode.window.showInformationMessage(`Theme "${name}" imported to .themes/`);
169
- return { name, css };
170
- }
171
-
172
- /**
173
- * 切换主题并持久化到 VSCode 配置
174
- */
175
- static async switchTheme(theme: string): Promise<void> {
176
- await vscode.workspace.getConfiguration('colaview').update(
177
- 'theme', theme, vscode.ConfigurationTarget.Global
178
- );
179
- }
180
- }
1
+ // 主题管理器 — 支持内置主题 + .themes 目录自定义主题
2
+ import * as vscode from 'vscode';
3
+ import * as path from 'path';
4
+ import * as fs from 'fs';
5
+
6
+ const BUILTIN_THEMES = [
7
+ 'light', 'dark', 'elegant', 'sepia', 'notion', 'bear',
8
+ 'writer', 'solarized-dark', 'nord', 'gruvbox', 'dracula', 'midnight'
9
+ ] as const;
10
+ type ThemeName = typeof BUILTIN_THEMES[number];
11
+
12
+ const customThemeCache = new Map<string, string>();
13
+
14
+ const THEME_CLASS_MAP: Record<string, string> = {
15
+ light: 'theme-light',
16
+ dark: 'theme-dark',
17
+ elegant: 'theme-elegant',
18
+ sepia: 'theme-sepia',
19
+ notion: 'theme-notion',
20
+ bear: 'theme-bear',
21
+ writer: 'theme-writer',
22
+ 'solarized-dark': 'theme-solarized-dark',
23
+ nord: 'theme-nord',
24
+ gruvbox: 'theme-gruvbox',
25
+ dracula: 'theme-dracula',
26
+ midnight: 'theme-midnight',
27
+ };
28
+
29
+ /**
30
+ * 获取资源文件目录 — 优先使用 out/ 目录(打包后),回退到 src/(开发模式)
31
+ */
32
+ function getResDir(context: vscode.ExtensionContext, ...segments: string[]): string {
33
+ const outDir = path.join(context.extensionPath, 'out', ...segments);
34
+ if (fs.existsSync(outDir)) {
35
+ return outDir;
36
+ }
37
+ return path.join(context.extensionPath, 'src', ...segments);
38
+ }
39
+
40
+ /**
41
+ * 主题管理器 — 负责主题 CSS 加载、切换、自定义主题导入
42
+ */
43
+ export class ThemeManager {
44
+ private static context: vscode.ExtensionContext;
45
+
46
+ /**
47
+ * 初始化主题管理器
48
+ */
49
+ static init(context: vscode.ExtensionContext): void {
50
+ this.context = context;
51
+ }
52
+
53
+ /** 获取当前激活的主题名称 */
54
+ static getCurrentTheme(): string {
55
+ return vscode.workspace.getConfiguration('colaview').get('theme', 'elegant');
56
+ }
57
+
58
+ /** 获取主题对应的 CSS class */
59
+ static getThemeClass(theme: string): string {
60
+ return THEME_CLASS_MAP[theme] || `theme-${theme}`;
61
+ }
62
+
63
+ /** 获取内置主题列表 */
64
+ static getBuiltinThemes(): string[] {
65
+ return [...BUILTIN_THEMES];
66
+ }
67
+
68
+ /**
69
+ * 获取 .themes 目录路径(当前工作区根目录下)
70
+ * 如果没有打开工作区,返回 null
71
+ */
72
+ static getThemesDir(): string | null {
73
+ const workspaceFolders = vscode.workspace.workspaceFolders;
74
+ if (!workspaceFolders || workspaceFolders.length === 0) return null;
75
+ return path.join(workspaceFolders[0].uri.fsPath, '.themes');
76
+ }
77
+
78
+ /**
79
+ * 确保 .themes 目录存在
80
+ */
81
+ private static ensureThemesDir(): string | null {
82
+ const dir = this.getThemesDir();
83
+ if (!dir) return null;
84
+ if (!fs.existsSync(dir)) {
85
+ fs.mkdirSync(dir, { recursive: true });
86
+ }
87
+ return dir;
88
+ }
89
+
90
+ /**
91
+ * 扫描 .themes 目录中的自定义主题
92
+ * 返回 { name, path } 列表
93
+ */
94
+ static scanCustomThemes(): Array<{ name: string; filePath: string }> {
95
+ const dir = this.getThemesDir();
96
+ if (!dir || !fs.existsSync(dir)) return [];
97
+
98
+ const results: Array<{ name: string; filePath: string }> = [];
99
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
100
+ if (entry.isFile() && entry.name.endsWith('.css')) {
101
+ const name = path.basename(entry.name, '.css');
102
+ results.push({ name, filePath: path.join(dir, entry.name) });
103
+ }
104
+ }
105
+ return results;
106
+ }
107
+
108
+ /**
109
+ * 获取完整主题列表(内置 + 自定义)
110
+ */
111
+ static getThemeList(): { builtins: string[]; customs: string[]; current: string } {
112
+ const builtins = this.getBuiltinThemes();
113
+ const customs = this.scanCustomThemes().map(t => t.name);
114
+ const current = this.getCurrentTheme();
115
+ return { builtins, customs, current };
116
+ }
117
+
118
+ /**
119
+ * 加载主题 CSS 内容
120
+ * 优先从 .themes/ 加载自定义主题,再加载内置主题
121
+ */
122
+ static loadThemeCSS(theme: string): string {
123
+ // 先检查 .themes 目录
124
+ const themesDir = this.getThemesDir();
125
+ if (themesDir) {
126
+ const customPath = path.join(themesDir, `${theme}.css`);
127
+ if (fs.existsSync(customPath)) {
128
+ return fs.readFileSync(customPath, 'utf-8');
129
+ }
130
+ }
131
+ // 再检查内存缓存
132
+ if (customThemeCache.has(theme)) {
133
+ return customThemeCache.get(theme)!;
134
+ }
135
+ // 最后加载内置主题
136
+ if (BUILTIN_THEMES.includes(theme as ThemeName)) {
137
+ const cssPath = path.join(
138
+ getResDir(this.context, 'themes', 'built-in'), `${theme}.css`
139
+ );
140
+ if (fs.existsSync(cssPath)) {
141
+ return fs.readFileSync(cssPath, 'utf-8');
142
+ }
143
+ }
144
+ return '';
145
+ }
146
+
147
+ /**
148
+ * 加载幻灯片版式表(上游 slides.css):预览/放映/PDF 共用同一份。
149
+ */
150
+ static loadSlidesCSS(): string {
151
+ const cssPath = path.join(getResDir(this.context, 'slides'), 'slides.css');
152
+ if (fs.existsSync(cssPath)) {
153
+ return fs.readFileSync(cssPath, 'utf-8');
154
+ }
155
+ return '';
156
+ }
157
+
158
+ /** 主题主色(`--link-color` / `--accent-color`),供 PPTX / Word 导出取色。 */
159
+ static getAccentColor(css: string): string | undefined {
160
+ const match = /--(?:link|accent)-color\s*:\s*([^;}\n]+)/.exec(css);
161
+ return match ? match[1].trim() : undefined;
162
+ }
163
+
164
+ /** 主题正文字体栈(`--font-family-base` / `--font-body`),供文档导出取字体。 */
165
+ static getBodyFontFamily(css: string): string | undefined {
166
+ const match = /--(?:font-(?:family-)?base|font-body)\s*:\s*([^;}\n]+)/.exec(css);
167
+ if (!match) return undefined;
168
+ return match[1].split(',')[0]?.replace(/["']/g, '').trim();
169
+ }
170
+
171
+ /**
172
+ * 导入自定义主题 — 复制到 .themes 目录并缓存
173
+ * @param uri 源 CSS 文件 URI
174
+ */
175
+ static async importCustomTheme(uri: vscode.Uri): Promise<{ name: string; css: string } | null> {
176
+ const themesDir = this.ensureThemesDir();
177
+ if (!themesDir) {
178
+ vscode.window.showWarningMessage('No workspace folder open. Cannot import theme.');
179
+ return null;
180
+ }
181
+
182
+ const css = Buffer.from(await vscode.workspace.fs.readFile(uri)).toString('utf-8');
183
+ const name = path.basename(uri.fsPath, '.css');
184
+ const destPath = path.join(themesDir, `${name}.css`);
185
+
186
+ // 复制文件到 .themes 目录
187
+ fs.writeFileSync(destPath, css, 'utf-8');
188
+
189
+ // 更新内存缓存
190
+ customThemeCache.set(name, css);
191
+
192
+ vscode.window.showInformationMessage(`Theme "${name}" imported to .themes/`);
193
+ return { name, css };
194
+ }
195
+
196
+ /**
197
+ * 切换主题并持久化到 VSCode 配置。
198
+ * 打开了工作区时写入工作区设置:工作区值会遮蔽用户级值,只写 Global
199
+ * 会导致 getCurrentTheme() 仍读到旧值(幻灯片模式下 deck 会合回原主题)。
200
+ */
201
+ static async switchTheme(theme: string): Promise<void> {
202
+ const target = vscode.workspace.workspaceFolders
203
+ ? vscode.ConfigurationTarget.Workspace
204
+ : vscode.ConfigurationTarget.Global;
205
+ await vscode.workspace.getConfiguration('colaview').update('theme', theme, target);
206
+ }
207
+ }
@@ -1,47 +1,47 @@
1
- // ColaMD 2.7.9 渲染库类型声明 + 模块声明
2
- // 基于 @bytechain.cn/colamd@2.7.9 的 src/lib/index.ts 接口定义
3
- // 2.7.9 包未发布 .d.ts 文件,此文件提供类型补全
4
-
5
- declare module '@bytechain.cn/colamd/renderer' {
6
- export interface ColaMDEditorOptions {
7
- /** 编辑器容器 id。默认 `editor`,容器不存在时直接报错。 */
8
- rootId?: string;
9
- /** 初始 markdown。默认空文档。 */
10
- markdown?: string;
11
- /** 是否可编辑。导出场景默认 false。 */
12
- editable?: boolean;
13
- }
14
-
15
- export interface ColaMDRendererHandle {
16
- /** 应用主题:内置主题名,或 `custom:` + `:root` 契约 CSS。 */
17
- applyTheme(name: string, customCSS?: string): void;
18
- /** 替换整篇文档(会清空撤销栈)。 */
19
- setMarkdown(markdown: string): void;
20
- /** 当前缓冲区内容。 */
21
- getMarkdown(): string;
22
- /** 摊平整篇、等 mermaid/图片/字体就绪。 */
23
- ensureAllPluginsRendered(): Promise<void>;
24
- /** 产出一份可以离线打开的完整 HTML。 */
25
- buildExportHTML(options?: { title?: string }): Promise<string>;
26
- /** 销毁编辑器。 */
27
- destroy(): void;
28
- }
29
-
30
- export interface ThemeContract {
31
- contractVersion: string;
32
- liveVariables: string[];
33
- deadVariables: string[];
34
- deadSelectors: string[];
35
- editorHooks: string[];
36
- peleVariables: string[];
37
- constraints: {
38
- rootOnly: boolean;
39
- ptUnitsForbidden: boolean;
40
- verticalPaddingForbiddenOn: string[];
41
- };
42
- }
43
-
44
- export function createColaMDEditor(options?: ColaMDEditorOptions): Promise<ColaMDRendererHandle>;
45
- export const THEME_CONTRACT: ThemeContract;
46
- export const CONTRACT_VERSION: string;
47
- }
1
+ // ColaMD 2.7.9 渲染库类型声明 + 模块声明
2
+ // 基于 @bytechain.cn/colamd@2.7.9 的 src/lib/index.ts 接口定义
3
+ // 2.7.9 包未发布 .d.ts 文件,此文件提供类型补全
4
+
5
+ declare module '@bytechain.cn/colamd/renderer' {
6
+ export interface ColaMDEditorOptions {
7
+ /** 编辑器容器 id。默认 `editor`,容器不存在时直接报错。 */
8
+ rootId?: string;
9
+ /** 初始 markdown。默认空文档。 */
10
+ markdown?: string;
11
+ /** 是否可编辑。导出场景默认 false。 */
12
+ editable?: boolean;
13
+ }
14
+
15
+ export interface ColaMDRendererHandle {
16
+ /** 应用主题:内置主题名,或 `custom:` + `:root` 契约 CSS。 */
17
+ applyTheme(name: string, customCSS?: string): void;
18
+ /** 替换整篇文档(会清空撤销栈)。 */
19
+ setMarkdown(markdown: string): void;
20
+ /** 当前缓冲区内容。 */
21
+ getMarkdown(): string;
22
+ /** 摊平整篇、等 mermaid/图片/字体就绪。 */
23
+ ensureAllPluginsRendered(): Promise<void>;
24
+ /** 产出一份可以离线打开的完整 HTML。 */
25
+ buildExportHTML(options?: { title?: string }): Promise<string>;
26
+ /** 销毁编辑器。 */
27
+ destroy(): void;
28
+ }
29
+
30
+ export interface ThemeContract {
31
+ contractVersion: string;
32
+ liveVariables: string[];
33
+ deadVariables: string[];
34
+ deadSelectors: string[];
35
+ editorHooks: string[];
36
+ peleVariables: string[];
37
+ constraints: {
38
+ rootOnly: boolean;
39
+ ptUnitsForbidden: boolean;
40
+ verticalPaddingForbiddenOn: string[];
41
+ };
42
+ }
43
+
44
+ export function createColaMDEditor(options?: ColaMDEditorOptions): Promise<ColaMDRendererHandle>;
45
+ export const THEME_CONTRACT: ThemeContract;
46
+ export const CONTRACT_VERSION: string;
47
+ }
@@ -0,0 +1,21 @@
1
+ // pele/lazy 本地类型声明:pele 未随包发布可直接解析的子路径声明,
2
+ // 项目 tsconfig 的 moduleResolution 不认 exports 子路径(与 colamd-renderer.d.ts 同因)。
3
+ declare module 'pele/lazy' {
4
+ export interface RenderResult {
5
+ svg: string;
6
+ type: string;
7
+ [key: string]: unknown;
8
+ }
9
+ export interface RenderOptions {
10
+ measurer?: unknown;
11
+ fontFamily?: string;
12
+ fontFamilyMono?: string;
13
+ fontSize?: number;
14
+ idPrefix?: string;
15
+ responsive?: boolean;
16
+ maxWidth?: number;
17
+ [key: string]: unknown;
18
+ }
19
+ export function renderAsync(text: string, options?: RenderOptions): Promise<RenderResult>;
20
+ export function load(type: string | null): Promise<boolean>;
21
+ }