vscode-colaview 0.3.3 → 0.4.0

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 (41) hide show
  1. package/.vscode/settings.json +1 -1
  2. package/CHANGELOG.md +20 -4
  3. package/README.md +310 -286
  4. package/README_CN.md +310 -286
  5. package/RELEASE_NOTES.md +36 -4
  6. package/esbuild.js +2 -0
  7. package/package.json +58 -19
  8. package/package.nls.json +14 -0
  9. package/package.nls.zh-cn.json +14 -0
  10. package/src/commands/commands.ts +48 -0
  11. package/src/docx/docx-builder.ts +241 -0
  12. package/src/export/docx-exporter.ts +961 -961
  13. package/src/export/html-exporter.ts +8 -2
  14. package/src/export/pdf-exporter.ts +65 -0
  15. package/src/export/pptx-exporter.ts +63 -0
  16. package/src/export/render-export-images.ts +386 -386
  17. package/src/export/theme-style.ts +195 -195
  18. package/src/pptx/pptx-builder.ts +249 -0
  19. package/src/pptx/zip.ts +158 -0
  20. package/src/present/presenter.ts +64 -0
  21. package/src/preview/preview-manager.ts +783 -541
  22. package/src/preview/webview/app.ts +588 -370
  23. package/src/preview/webview/index.html +167 -146
  24. package/src/slides/deck.ts +397 -0
  25. package/src/slides/markdown.ts +393 -0
  26. package/src/slides/render.ts +484 -0
  27. package/src/slides/slides.css +1082 -0
  28. package/src/themes/built-in/bear.css +89 -38
  29. package/src/themes/built-in/dark.css +87 -28
  30. package/src/themes/built-in/dracula.css +93 -34
  31. package/src/themes/built-in/elegant.css +109 -45
  32. package/src/themes/built-in/gruvbox.css +97 -38
  33. package/src/themes/built-in/light.css +80 -20
  34. package/src/themes/built-in/midnight.css +93 -35
  35. package/src/themes/built-in/nord.css +93 -34
  36. package/src/themes/built-in/notion.css +98 -46
  37. package/src/themes/built-in/sepia.css +99 -44
  38. package/src/themes/built-in/solarized-dark.css +94 -35
  39. package/src/themes/built-in/writer.css +99 -52
  40. package/src/themes/theme-manager.ts +207 -180
  41. package/src/types/colamd-renderer.d.ts +47 -47
@@ -0,0 +1,249 @@
1
+ // 最小 OOXML 生成器 — 纯同步、零依赖,产出真正的 .pptx。
2
+ //
3
+ // PresentationML 里给出 master + 单个 blank layout + theme,每页用显式文本框
4
+ // 排标题、正文与代码块。单位:DrawingML 用 EMU(914400 = 1 英寸),字号写百分
5
+ // 之一磅(`a:rPr@sz`)。
6
+ import { zipStore } from './zip';
7
+
8
+ /** 一页 PPT 的内容(由幻灯片 AST 转换而来)。 */
9
+ export interface PptxSlide {
10
+ kicker?: string;
11
+ title?: string;
12
+ paragraphs?: string[];
13
+ bullets?: string[];
14
+ code?: string;
15
+ }
16
+
17
+ /** 版式与字体覆盖项;缺省值见 buildPptx。 */
18
+ export interface PptxMeta {
19
+ /** 主色,形如 `#b0401f`。 */
20
+ accent?: string;
21
+ /** 正文字体,例如 `Source Han Serif SC`。 */
22
+ fontBody?: string;
23
+ /** 等宽字体,例如 `JetBrains Mono`。 */
24
+ fontMono?: string;
25
+ /** 文档标题,写入 core.xml。 */
26
+ title?: string;
27
+ widthEmu?: number;
28
+ heightEmu?: number;
29
+ }
30
+
31
+ const XML_DECL = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
32
+ /** 所有 zip 条目共用的固定时间戳,保证同一输入产出同一字节。 */
33
+ const FIXED_DATE = new Date(Date.UTC(2024, 0, 1));
34
+ const DEFAULT_ACCENT = 'B0401F';
35
+ const DEFAULT_FONT_BODY = 'Calibri';
36
+ const DEFAULT_FONT_MONO = 'Consolas';
37
+
38
+ const A_NS = 'http://schemas.openxmlformats.org/drawingml/2006/main';
39
+ const R_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
40
+ const P_NS = 'http://schemas.openxmlformats.org/presentationml/2006/main';
41
+ const PKG_REL_NS = 'http://schemas.openxmlformats.org/package/2006/relationships';
42
+ const CT_NS = 'http://schemas.openxmlformats.org/package/2006/content-types';
43
+
44
+ /** 转义 XML 文本/属性,并剔除 XML 1.0 不接受的控制字符。 */
45
+ function esc(value: string): string {
46
+ return value
47
+ .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, '')
48
+ .replace(/&/g, '&amp;')
49
+ .replace(/</g, '&lt;')
50
+ .replace(/>/g, '&gt;')
51
+ .replace(/"/g, '&quot;')
52
+ .replace(/'/g, '&apos;');
53
+ }
54
+
55
+ /** 把 `#b0401f` / `b0401f` 规范成大写六位 RGB;非法值退回 fallback。 */
56
+ function hex(value: string | undefined, fallback: string): string {
57
+ if (value === undefined) return fallback;
58
+ const raw = value.trim().replace(/^#/, '');
59
+ return /^[0-9a-fA-F]{6}$/.test(raw) ? raw.toUpperCase() : fallback;
60
+ }
61
+
62
+ function font(value: string | undefined, fallback: string): string {
63
+ const name = (value ?? '').trim();
64
+ return name === '' ? fallback : name;
65
+ }
66
+
67
+ function pack(files: ReadonlyArray<readonly [string, string]>): Uint8Array {
68
+ const encoder = new TextEncoder();
69
+ return zipStore(files.map(([path, text]) => ({ path, data: encoder.encode(text), date: FIXED_DATE })));
70
+ }
71
+
72
+ interface Box {
73
+ x: number;
74
+ y: number;
75
+ cx: number;
76
+ cy: number;
77
+ }
78
+
79
+ /** 16:9 版式坐标(EMU):标题、正文、底部代码框。 */
80
+ const TITLE_BOX: Box = { x: 838200, y: 457200, cx: 10515600, cy: 914400 };
81
+ const KICKER_BOX: Box = { x: 838200, y: 274320, cx: 10515600, cy: 274320 };
82
+ const BODY_BOX: Box = { x: 838200, y: 1600200, cx: 10515600, cy: 4572000 };
83
+ const CODE_BOX: Box = { x: 838200, y: 5486400, cx: 10515600, cy: 1005840 };
84
+
85
+ interface RunStyle {
86
+ size: number;
87
+ color: string;
88
+ fontFace: string;
89
+ bold?: boolean;
90
+ }
91
+
92
+ /** DrawingML run:始终带 latin/ea/cs 字体,避免回退到主题字体。 */
93
+ function aRun(text: string, style: RunStyle): string {
94
+ const bold = style.bold === true ? ' b="1"' : '';
95
+ return `<a:r><a:rPr lang="zh-CN" altLang="en-US" sz="${style.size}"${bold} dirty="0"><a:solidFill><a:srgbClr val="${style.color}"/></a:solidFill><a:latin typeface="${esc(style.fontFace)}"/><a:ea typeface="${esc(style.fontFace)}"/><a:cs typeface="${esc(style.fontFace)}"/></a:rPr><a:t>${esc(text)}</a:t></a:r>`;
96
+ }
97
+
98
+ /** 一个 a:p;多行文本按 `a:br` 拆行,bullet 时加项目符号。 */
99
+ function aParagraph(text: string, style: RunStyle, bullet = false): string {
100
+ const pPr = bullet
101
+ ? '<a:pPr marL="285750" indent="-285750"><a:lnSpc><a:spcPct val="125000"/></a:lnSpc><a:spcBef><a:spcPts val="600"/></a:spcBef><a:buFont typeface="Arial"/><a:buChar char="•"/></a:pPr>'
102
+ : '<a:pPr><a:lnSpc><a:spcPct val="125000"/></a:lnSpc><a:spcBef><a:spcPts val="600"/></a:spcBef></a:pPr>';
103
+ const runs = text
104
+ .replace(/\r\n?/g, '\n')
105
+ .split('\n')
106
+ .map((line, i) => `${i > 0 ? '<a:br/>' : ''}${aRun(line, style)}`)
107
+ .join('');
108
+ return `<a:p>${pPr}${runs}</a:p>`;
109
+ }
110
+
111
+ /** 一个显式文本框(txBox),可选纯色底。 */
112
+ function textBox(id: number, name: string, box: Box, paragraphs: string, fill?: string): string {
113
+ const fillXml = fill === undefined ? '<a:noFill/>' : `<a:solidFill><a:srgbClr val="${fill}"/></a:solidFill>`;
114
+ const lineXml = fill === undefined ? '' : '<a:ln w="12700"><a:noFill/></a:ln>';
115
+ const inset = fill === undefined ? 91440 : 137160;
116
+ return `<p:sp><p:nvSpPr><p:cNvPr id="${id}" name="${esc(name)}"/><p:cNvSpPr txBox="1"/><p:nvPr/></p:nvSpPr><p:spPr><a:xfrm><a:off x="${box.x}" y="${box.y}"/><a:ext cx="${box.cx}" cy="${box.cy}"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom>${fillXml}${lineXml}</p:spPr><p:txBody><a:bodyPr wrap="square" lIns="${inset}" tIns="${inset}" rIns="${inset}" bIns="${inset}" anchor="t"/><a:lstStyle/>${paragraphs}</p:txBody></p:sp>`;
117
+ }
118
+
119
+ const EMPTY_TREE =
120
+ '<p:nvGrpSpPr><p:cNvPr id="1" name=""/><p:cNvGrpSpPr/><p:nvPr/></p:nvGrpSpPr><p:grpSpPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/><a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm></p:grpSpPr>';
121
+
122
+ /** 主题:色板以 accent 为 accent1,major/minor 字体都取 fontBody。 */
123
+ function themeXml(accent: string, bodyFont: string): string {
124
+ const solid = (val: string): string => `<a:solidFill><a:srgbClr val="${val}"/></a:solidFill>`;
125
+ const scheme = (val: string): string => `<a:solidFill><a:schemeClr val="${val}"/></a:solidFill>`;
126
+ return `${XML_DECL}<a:theme xmlns:a="${A_NS}" name="ColaView"><a:themeElements><a:clrScheme name="ColaView"><a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1><a:lt1><a:sysClr val="window" lastClr="FFFFFF"/></a:lt1><a:dk2>${solid('1F1F1F')}</a:dk2><a:lt2>${solid('F5F3EF')}</a:lt2><a:accent1>${solid(accent)}</a:accent1><a:accent2>${solid('2F5D62')}</a:accent2><a:accent3>${solid('8C6A3F')}</a:accent3><a:accent4>${solid('3F6C8C')}</a:accent4><a:accent5>${solid('6C3F8C')}</a:accent5><a:accent6>${solid('3F8C5A')}</a:accent6><a:hlink>${solid('1F5C99')}</a:hlink><a:folHlink>${solid('7A3F8C')}</a:folHlink></a:clrScheme><a:fontScheme name="ColaView"><a:majorFont><a:latin typeface="${esc(bodyFont)}"/><a:ea typeface="${esc(bodyFont)}"/><a:cs typeface=""/></a:majorFont><a:minorFont><a:latin typeface="${esc(bodyFont)}"/><a:ea typeface="${esc(bodyFont)}"/><a:cs typeface=""/></a:minorFont></a:fontScheme><a:fmtScheme name="ColaView"><a:fillStyleLst>${scheme('phClr')}<a:solidFill><a:schemeClr val="phClr"><a:tint val="50000"/></a:schemeClr></a:solidFill><a:solidFill><a:schemeClr val="phClr"><a:shade val="80000"/></a:schemeClr></a:solidFill></a:fillStyleLst><a:lnStyleLst><a:ln w="9525" cap="flat" cmpd="sng" algn="ctr">${scheme('phClr')}<a:prstDash val="solid"/><a:miter lim="800000"/></a:ln><a:ln w="19050" cap="flat" cmpd="sng" algn="ctr">${scheme('phClr')}<a:prstDash val="solid"/><a:miter lim="800000"/></a:ln><a:ln w="28575" cap="flat" cmpd="sng" algn="ctr">${scheme('phClr')}<a:prstDash val="solid"/><a:miter lim="800000"/></a:ln></a:lnStyleLst><a:effectStyleLst><a:effectStyle><a:effectLst/></a:effectStyle><a:effectStyle><a:effectLst/></a:effectStyle><a:effectStyle><a:effectLst/></a:effectStyle></a:effectStyleLst><a:bgFillStyleLst>${scheme('phClr')}<a:solidFill><a:schemeClr val="phClr"><a:tint val="95000"/></a:schemeClr></a:solidFill><a:solidFill><a:schemeClr val="phClr"><a:shade val="90000"/></a:schemeClr></a:solidFill></a:bgFillStyleLst></a:fmtScheme></a:themeElements><a:objectDefaults/><a:extraClrSchemeLst/></a:theme>`;
127
+ }
128
+
129
+ function slideMasterXml(): string {
130
+ return `${XML_DECL}<p:sldMaster xmlns:a="${A_NS}" xmlns:r="${R_NS}" xmlns:p="${P_NS}"><p:cSld><p:bg><p:bgPr><a:solidFill><a:schemeClr val="bg1"/></a:solidFill><a:effectLst/></p:bgPr></p:bg><p:spTree>${EMPTY_TREE}</p:spTree></p:cSld><p:clrMap bg1="lt1" tx1="dk1" bg2="lt2" tx2="dk2" accent1="accent1" accent2="accent2" accent3="accent3" accent4="accent4" accent5="accent5" accent6="accent6" hlink="hlink" folHlink="folHlink"/><p:sldLayoutIdLst><p:sldLayoutId id="2147483649" r:id="rId1"/></p:sldLayoutIdLst><p:txStyles><p:titleStyle><a:lvl1pPr><a:defRPr sz="3200" b="1"/></a:lvl1pPr></p:titleStyle><p:bodyStyle><a:lvl1pPr><a:defRPr sz="1800"/></a:lvl1pPr></p:bodyStyle><p:otherStyle><a:lvl1pPr><a:defRPr sz="1800"/></a:lvl1pPr></p:otherStyle></p:txStyles></p:sldMaster>`;
131
+ }
132
+
133
+ function slideLayoutXml(): string {
134
+ return `${XML_DECL}<p:sldLayout xmlns:a="${A_NS}" xmlns:r="${R_NS}" xmlns:p="${P_NS}" type="blank" preserve="1"><p:cSld name="Blank"><p:spTree>${EMPTY_TREE}</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sldLayout>`;
135
+ }
136
+
137
+ /** 一页幻灯片:标题 + 正文(段落/项目符号)+ 可选底部代码框。 */
138
+ function slideXml(
139
+ slide: PptxSlide,
140
+ meta: { accent: string; bodyFont: string; monoFont: string; ink: string }
141
+ ): string {
142
+ const shapes: string[] = [];
143
+ let id = 2;
144
+ if (slide.kicker !== undefined && slide.kicker !== '') {
145
+ shapes.push(
146
+ textBox(
147
+ id++,
148
+ 'Kicker',
149
+ KICKER_BOX,
150
+ `<a:p>${aRun(slide.kicker.toUpperCase(), { size: 1200, bold: false, color: meta.accent, fontFace: meta.monoFont })}</a:p>`
151
+ )
152
+ );
153
+ }
154
+ if (slide.title !== undefined && slide.title !== '') {
155
+ shapes.push(
156
+ textBox(
157
+ id++,
158
+ 'Title',
159
+ TITLE_BOX,
160
+ `<a:p>${aRun(slide.title, { size: 3200, bold: true, color: meta.accent, fontFace: meta.bodyFont })}</a:p>`
161
+ )
162
+ );
163
+ }
164
+ const bodyParts: string[] = [];
165
+ for (const text of slide.paragraphs ?? []) {
166
+ bodyParts.push(aParagraph(text, { size: 1800, color: meta.ink, fontFace: meta.bodyFont }));
167
+ }
168
+ for (const text of slide.bullets ?? []) {
169
+ bodyParts.push(aParagraph(text, { size: 1600, color: meta.ink, fontFace: meta.bodyFont }, true));
170
+ }
171
+ if (bodyParts.length > 0) shapes.push(textBox(id++, 'Body', BODY_BOX, bodyParts.join('')));
172
+ if (slide.code !== undefined && slide.code !== '') {
173
+ const codeParts = slide.code
174
+ .replace(/\r\n?/g, '\n')
175
+ .replace(/\t/g, ' ')
176
+ .split('\n')
177
+ .map(
178
+ (line) =>
179
+ `<a:p><a:pPr><a:lnSpc><a:spcPct val="100000"/></a:lnSpc></a:pPr>${aRun(line, { size: 1200, color: 'FFFFFF', fontFace: meta.monoFont })}</a:p>`
180
+ );
181
+ shapes.push(textBox(id++, 'Code', CODE_BOX, codeParts.join(''), '1F1F1F'));
182
+ }
183
+ return `${XML_DECL}<p:sld xmlns:a="${A_NS}" xmlns:r="${R_NS}" xmlns:p="${P_NS}"><p:cSld><p:spTree>${EMPTY_TREE}${shapes.join('')}</p:spTree></p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr></p:sld>`;
184
+ }
185
+
186
+ function coreXml(title: string): string {
187
+ return `${XML_DECL}<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title>${esc(title)}</dc:title><dc:creator>ColaView</dc:creator><cp:lastModifiedBy>ColaView</cp:lastModifiedBy><dcterms:created xsi:type="dcterms:W3CDTF">2024-01-01T00:00:00Z</dcterms:created><dcterms:modified xsi:type="dcterms:W3CDTF">2024-01-01T00:00:00Z</dcterms:modified></cp:coreProperties>`;
188
+ }
189
+
190
+ function appXml(slides: number): string {
191
+ const extra = `<PresentationFormat>Widescreen</PresentationFormat><Slides>${slides}</Slides><Notes>0</Notes><HiddenSlides>0</HiddenSlides><MMClips>0</MMClips>`;
192
+ return `${XML_DECL}<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Application>ColaView</Application>${extra}<DocSecurity>0</DocSecurity><ScaleCrop>false</ScaleCrop><Company></Company><LinksUpToDate>false</LinksUpToDate><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>16.0000</AppVersion></Properties>`;
193
+ }
194
+
195
+ /**
196
+ * 生成一份 PowerPoint 演示文稿(16:9)。
197
+ *
198
+ * 包内只有 1 个 slideMaster、1 个 blank slideLayout 和 1 份 theme;每页的内容都是
199
+ * 显式文本框,因此不依赖占位符继承。
200
+ */
201
+ export function buildPptx(input: { slides: PptxSlide[]; meta?: PptxMeta }): Uint8Array {
202
+ const meta = input.meta ?? {};
203
+ const accent = hex(meta.accent, DEFAULT_ACCENT);
204
+ const bodyFont = font(meta.fontBody, DEFAULT_FONT_BODY);
205
+ const monoFont = font(meta.fontMono, DEFAULT_FONT_MONO);
206
+ const title = meta.title ?? 'ColaView Presentation';
207
+ const width = meta.widthEmu !== undefined && meta.widthEmu > 0 ? Math.round(meta.widthEmu) : 12192000;
208
+ const height = meta.heightEmu !== undefined && meta.heightEmu > 0 ? Math.round(meta.heightEmu) : 6858000;
209
+ const slides = input.slides.length > 0 ? input.slides : [{}];
210
+ const ink = '262626';
211
+
212
+ const slideRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/></Relationships>`;
213
+ const masterRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="../theme/theme1.xml"/></Relationships>`;
214
+ const layoutRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" Target="../slideMasters/slideMaster1.xml"/></Relationships>`;
215
+
216
+ const presentationRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" Target="slideMasters/slideMaster1.xml"/>${slides.map((_, i) => `<Relationship Id="rId${i + 2}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide${i + 1}.xml"/>`).join('')}<Relationship Id="rId${slides.length + 2}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps" Target="presProps.xml"/></Relationships>`;
217
+
218
+ const slideIds = slides.map((_, i) => `<p:sldId id="${256 + i}" r:id="rId${i + 2}"/>`).join('');
219
+ const presentation = `${XML_DECL}<p:presentation xmlns:a="${A_NS}" xmlns:r="${R_NS}" xmlns:p="${P_NS}" saveSubsetFonts="1"><p:sldMasterIdLst><p:sldMasterId id="2147483648" r:id="rId1"/></p:sldMasterIdLst><p:sldIdLst>${slideIds}</p:sldIdLst><p:sldSz cx="${width}" cy="${height}" type="screen16x9"/><p:notesSz cx="6858000" cy="9144000"/><p:defaultTextStyle><a:lvl1pPr><a:defRPr sz="1800"/></a:lvl1pPr></p:defaultTextStyle></p:presentation>`;
220
+
221
+ const slideOverrides = slides
222
+ .map(
223
+ (_, i) =>
224
+ `<Override PartName="/ppt/slides/slide${i + 1}.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/>`
225
+ )
226
+ .join('');
227
+ const contentTypes = `${XML_DECL}<Types xmlns="${CT_NS}"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/><Override PartName="/ppt/slideMasters/slideMaster1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml"/><Override PartName="/ppt/slideLayouts/slideLayout1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml"/><Override PartName="/ppt/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/><Override PartName="/ppt/presProps.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presProps+xml"/>${slideOverrides}<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/><Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/></Types>`;
228
+ const rootRels = `${XML_DECL}<Relationships xmlns="${PKG_REL_NS}"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/><Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/><Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/></Relationships>`;
229
+
230
+ const files: Array<readonly [string, string]> = [
231
+ ['[Content_Types].xml', contentTypes],
232
+ ['_rels/.rels', rootRels],
233
+ ['docProps/core.xml', coreXml(title)],
234
+ ['docProps/app.xml', appXml(slides.length)],
235
+ ['ppt/presentation.xml', presentation],
236
+ ['ppt/_rels/presentation.xml.rels', presentationRels],
237
+ ['ppt/presProps.xml', `${XML_DECL}<p:presentationPr xmlns:a="${A_NS}" xmlns:r="${R_NS}" xmlns:p="${P_NS}"/>`],
238
+ ['ppt/slideMasters/slideMaster1.xml', slideMasterXml()],
239
+ ['ppt/slideMasters/_rels/slideMaster1.xml.rels', masterRels],
240
+ ['ppt/slideLayouts/slideLayout1.xml', slideLayoutXml()],
241
+ ['ppt/slideLayouts/_rels/slideLayout1.xml.rels', layoutRels],
242
+ ['ppt/theme/theme1.xml', themeXml(accent, bodyFont)],
243
+ ];
244
+ slides.forEach((slide, i) => {
245
+ files.push([`ppt/slides/slide${i + 1}.xml`, slideXml(slide, { accent, bodyFont, monoFont, ink })]);
246
+ files.push([`ppt/slides/_rels/slide${i + 1}.xml.rels`, slideRels]);
247
+ });
248
+ return pack(files);
249
+ }
@@ -0,0 +1,158 @@
1
+ // 极简 ZIP 打包器 — 只做 STORE(不压缩),零依赖、同步、可复现。
2
+ // 供 .pptx 这类 OOXML 包使用:包内都是已经压过的小体积 XML。
3
+
4
+ /** 一个待写入 ZIP 的文件条目(只允许文件,不允许目录条目)。 */
5
+ export interface ZipEntry {
6
+ path: string;
7
+ data: Uint8Array;
8
+ /** 可选修改时间;缺省固定,保证同一输入产出同一份字节。 */
9
+ date?: Date;
10
+ }
11
+
12
+ const SIG_LOCAL = 0x04034b50;
13
+ const SIG_CENTRAL = 0x02014b50;
14
+ const SIG_EOCD = 0x06054b50;
15
+ const VERSION_NEEDED = 20;
16
+ /** 通用标志位 bit 11:文件名为 UTF-8。 */
17
+ const FLAG_UTF8 = 0x0800;
18
+ const DOS_DATE_1980 = 0x0021;
19
+ const DOS_TIME_MIDNIGHT = 0x0000;
20
+ const MIN_1980 = Date.UTC(1980, 0, 1);
21
+ const MAX_YEAR = 2107;
22
+
23
+ const CRC_TABLE = (() => {
24
+ const table = new Uint32Array(256);
25
+ for (let i = 0; i < 256; i++) {
26
+ let c = i;
27
+ for (let bit = 0; bit < 8; bit++) {
28
+ c = (c & 1) === 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
29
+ }
30
+ table[i] = c >>> 0;
31
+ }
32
+ return table;
33
+ })();
34
+
35
+ function crc32(data: Uint8Array): number {
36
+ let c = 0xffffffff;
37
+ for (let i = 0; i < data.length; i++) {
38
+ c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
39
+ }
40
+ return (c ^ 0xffffffff) >>> 0;
41
+ }
42
+
43
+ /** 把条目路径规范成 ZIP 约定形式:正斜杠、无前导 `/` 或 `./`。 */
44
+ function cleanPath(raw: string): string {
45
+ let path = raw.replace(/\\/g, '/');
46
+ path = path.replace(/^\/+/, '').replace(/^(?:\.\/)+/, '');
47
+ if (path === '' || path.endsWith('/')) {
48
+ throw new Error(`zipStore: invalid file path "${raw}" (directories are not supported)`);
49
+ }
50
+ return path;
51
+ }
52
+
53
+ function toDos(date: Date | undefined): { time: number; date: number } {
54
+ if (date === undefined || Number.isNaN(date.getTime()) || date.getTime() < MIN_1980) {
55
+ return { time: DOS_TIME_MIDNIGHT, date: DOS_DATE_1980 };
56
+ }
57
+ const year = Math.min(date.getUTCFullYear(), MAX_YEAR);
58
+ const month = date.getUTCMonth() + 1;
59
+ const day = date.getUTCDate();
60
+ const hours = date.getUTCHours();
61
+ const minutes = date.getUTCMinutes();
62
+ const seconds = Math.floor(date.getUTCSeconds() / 2); // DOS 时间精度 2 秒
63
+ return {
64
+ time: (hours << 11) | (minutes << 5) | seconds,
65
+ date: ((year - 1980) << 9) | (month << 5) | day,
66
+ };
67
+ }
68
+
69
+ /** 把若干文件打成 ZIP(STORE,不压缩)。 */
70
+ export function zipStore(entries: readonly ZipEntry[]): Uint8Array {
71
+ if (entries.length > 0xffff) {
72
+ throw new Error(`zipStore: too many entries (${entries.length} > 65535)`);
73
+ }
74
+ const encoder = new TextEncoder();
75
+ const prepared = entries.map((entry) => {
76
+ const name = encoder.encode(cleanPath(entry.path));
77
+ return { name, data: entry.data, crc: crc32(entry.data), dt: toDos(entry.date), offset: 0 };
78
+ });
79
+
80
+ let total = 22; // EOCD
81
+ for (const item of prepared) {
82
+ total += 30 + item.name.length + item.data.length;
83
+ total += 46 + item.name.length;
84
+ }
85
+ if (total > 0xffffffff) {
86
+ throw new Error('zipStore: archive exceeds 4 GiB (ZIP64 is not supported)');
87
+ }
88
+
89
+ const out = new Uint8Array(total);
90
+ const view = new DataView(out.buffer);
91
+ let at = 0;
92
+ const u16 = (value: number): void => {
93
+ view.setUint16(at, value, true);
94
+ at += 2;
95
+ };
96
+ const u32 = (value: number): void => {
97
+ view.setUint32(at, value >>> 0, true);
98
+ at += 4;
99
+ };
100
+ const bytes = (chunk: Uint8Array): void => {
101
+ out.set(chunk, at);
102
+ at += chunk.length;
103
+ };
104
+
105
+ // 本地文件头 + 原始数据。
106
+ for (const item of prepared) {
107
+ item.offset = at;
108
+ u32(SIG_LOCAL);
109
+ u16(VERSION_NEEDED);
110
+ u16(FLAG_UTF8);
111
+ u16(0); // 方法 0 = STORE
112
+ u16(item.dt.time);
113
+ u16(item.dt.date);
114
+ u32(item.crc);
115
+ u32(item.data.length);
116
+ u32(item.data.length);
117
+ u16(item.name.length);
118
+ u16(0);
119
+ bytes(item.name);
120
+ bytes(item.data);
121
+ }
122
+
123
+ // 中央目录。
124
+ const cdStart = at;
125
+ for (const item of prepared) {
126
+ u32(SIG_CENTRAL);
127
+ u16(VERSION_NEEDED);
128
+ u16(VERSION_NEEDED);
129
+ u16(FLAG_UTF8);
130
+ u16(0);
131
+ u16(item.dt.time);
132
+ u16(item.dt.date);
133
+ u32(item.crc);
134
+ u32(item.data.length);
135
+ u32(item.data.length);
136
+ u16(item.name.length);
137
+ u16(0);
138
+ u16(0);
139
+ u16(0);
140
+ u16(0);
141
+ u32(0);
142
+ u32(item.offset);
143
+ bytes(item.name);
144
+ }
145
+ const cdSize = at - cdStart;
146
+
147
+ // 中央目录结束记录。
148
+ u32(SIG_EOCD);
149
+ u16(0);
150
+ u16(0);
151
+ u16(prepared.length);
152
+ u16(prepared.length);
153
+ u32(cdSize);
154
+ u32(cdStart);
155
+ u16(0);
156
+
157
+ return out;
158
+ }
@@ -0,0 +1,64 @@
1
+ // 全屏演示 — 把一份自包含 HTML 落到临时目录,交给系统默认浏览器打开。
2
+ // VS Code 的 WebView 面板拿不到真正的全屏,所以放映走浏览器(F11 / requestFullscreen
3
+ // 都可用,←/→ 翻页、F 切全屏由文档内嵌的放映脚本处理)。
4
+ import * as vscode from 'vscode';
5
+ import * as path from 'path';
6
+ import * as os from 'os';
7
+ import * as fs from 'fs';
8
+
9
+ const PRESENT_DIR = path.join(os.tmpdir(), 'colaview-present');
10
+ const MAX_KEPT_FILES = 8;
11
+
12
+ /** 文件名去噪:保留中英文、数字、空格与短横线,其余替换成短横线。 */
13
+ function safeFilename(title: string, fallback = 'present'): string {
14
+ const cleaned = title
15
+ .trim()
16
+ .replace(/[\\/:*?"<>|]+/g, '-')
17
+ .replace(/\s+/g, '-')
18
+ .replace(/-{2,}/g, '-')
19
+ .replace(/^-|-$/g, '');
20
+ return cleaned === '' ? fallback : cleaned.slice(0, 60);
21
+ }
22
+
23
+ /** 只保留最近若干份放映文件,避免临时目录一直堆积。 */
24
+ function prunePresentDir(keep: string): void {
25
+ try {
26
+ const files = fs
27
+ .readdirSync(PRESENT_DIR, { withFileTypes: true })
28
+ .filter((entry) => entry.isFile() && entry.name.endsWith('.html'))
29
+ .map((entry) => ({ name: entry.name, at: fs.statSync(path.join(PRESENT_DIR, entry.name)).mtimeMs }))
30
+ .sort((a, b) => b.at - a.at);
31
+ for (const file of files.slice(MAX_KEPT_FILES)) {
32
+ if (file.name === keep) continue;
33
+ try { fs.unlinkSync(path.join(PRESENT_DIR, file.name)); } catch { /* ignore */ }
34
+ }
35
+ } catch {
36
+ /* 清理失败不影响放映 */
37
+ }
38
+ }
39
+
40
+ /**
41
+ * 用默认浏览器打开一份全屏演示文档。
42
+ * @param title 文档标题(用于文件名与提示)
43
+ * @param html 自包含 HTML(已内联全部 CSS,并已打入放映脚本)
44
+ */
45
+ export async function openPresentWindow(title: string, html: string): Promise<void> {
46
+ fs.mkdirSync(PRESENT_DIR, { recursive: true });
47
+ const name = `${safeFilename(title)}-${Date.now()}.html`;
48
+ const filePath = path.join(PRESENT_DIR, name);
49
+ fs.writeFileSync(filePath, html, 'utf-8');
50
+ prunePresentDir(name);
51
+
52
+ try {
53
+ const opened = await vscode.env.openExternal(vscode.Uri.file(filePath));
54
+ if (!opened) {
55
+ vscode.window.showWarningMessage(
56
+ `ColaView: 无法自动打开浏览器,请手动打开放映文件:${filePath}`
57
+ );
58
+ }
59
+ } catch (e) {
60
+ vscode.window.showErrorMessage(
61
+ `ColaView: 打开放映窗口失败:${e instanceof Error ? e.message : String(e)}`
62
+ );
63
+ }
64
+ }