gbpptx 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 甲宁
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
- {
2
- "name": "gbpptx",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "gbpptx",
3
+ "version": "0.1.0",
4
+ "description": "Front-end presentation engine: canvas object model + real .pptx (PresentationML) read/write, plus a self-contained HTML slide editor UI.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "main": "./src/index.js",
9
+ "exports": {
10
+ ".": "./src/index.js",
11
+ "./model": "./src/kernel/model.js",
12
+ "./pptx": "./src/kernel/pptx-read.js",
13
+ "./package.json": "./package.json",
14
+ "./*": "./src/*.js"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "web",
19
+ "LICENSE"
20
+ ],
21
+ "keywords": [
22
+ "pptx",
23
+ "presentation",
24
+ "powerpoint",
25
+ "ooxml",
26
+ "slides",
27
+ "editor"
28
+ ],
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "dependencies": {
33
+ "gbooxml": "^0.1.0"
34
+ }
35
+ }
package/src/index.js ADDED
@@ -0,0 +1,21 @@
1
+ // [文件] gbpptx·统一入口 —— 画布模型 + 真实 .pptx 读写 + 打印/菜单 能力聚合再导出
2
+
3
+ /* 画布对象模型 */
4
+ export {
5
+ SNAPSHOT_VERSION, SLIDE_W, SLIDE_H, THEMES, THEME_KEYS, themeOf,
6
+ SHAPE_KINDS, TYPE_NAMES,
7
+ cloneObject, copySlide, createSlide, topZ,
8
+ createTextObject, createImageObject, createShapeObject, createLineObject, createTableObject,
9
+ objectRect, objectBoxStyle, shapePath, isBlankHtml, htmlToPlain, plainToHtml,
10
+ clearInlineFontSizes, setObjectFontSize, normalizeSnapshot, SLIDE_LAYOUTS, createSlideWithLayout, createDraft,
11
+ } from './kernel/model.js';
12
+
13
+ /* pptx 读写(唯一出入口) */
14
+ export { parsePptx } from './kernel/read.js';
15
+ export { buildPptx } from './kernel/write.js';
16
+ export { buildPptxVerified } from './kernel/roundtrip.js';
17
+ export { isPptx, KIND_TO_PRST, PRST_TO_KIND, fontSizeToSz, szToFontSize } from './kernel/pptx.js';
18
+
19
+ /* 打印 / 菜单 */
20
+ export { printSlides } from './kernel/print.js';
21
+ export { buildMenus } from './kernel/menus.js';
@@ -0,0 +1,123 @@
1
+ // [文件] gbpptx·菜单栏结构 —— 文件/编辑/插入/页/格式/设计/视图 七菜单唯一定义处(纯数据,动作经编辑器注入的 ctx 提供;移植 slidesMenus.js)
2
+ /**
3
+ * [设计] 只收录本对象模型能往返的条目(text/image/shape/line/table):参考实现的图表/视频/音频/转场/组合/动画/链接
4
+ * 一律不落条目,禁做点了没反应的死按钮。
5
+ * [设计] items 一律函数——每次打开才求值,勾选态与可用性始终是当下真实状态。
6
+ */
7
+
8
+ import { SLIDE_LAYOUTS, THEMES, THEME_KEYS } from './model.js';
9
+
10
+ const ALIGN_KEYS = [
11
+ { key: 'left', label: '左对齐' },
12
+ { key: 'centerH', label: '水平居中' },
13
+ { key: 'right', label: '右对齐' },
14
+ { key: 'top', label: '上对齐' },
15
+ { key: 'middle', label: '垂直居中' },
16
+ { key: 'bottom', label: '下对齐' },
17
+ ];
18
+
19
+ const ORDER_KEYS = [
20
+ { key: 'front', label: '置于顶层' },
21
+ { key: 'forward', label: '上移一层' },
22
+ { key: 'backward', label: '下移一层' },
23
+ { key: 'back', label: '置于底层' },
24
+ ];
25
+
26
+ /**
27
+ * 构建菜单栏结构
28
+ * @param {object} ctx 编辑器注入的动作集合与状态读取器
29
+ * @returns {Array<{key:string,label:string,items:()=>Array}>}
30
+ */
31
+ export const buildMenus = (ctx) => {
32
+ const { state, file, edit, insert, slide, format, design, view } = ctx;
33
+
34
+ const fileMenu = () => [
35
+ { label: '新建', shortcut: 'Ctrl+Alt+N', onClick: file.newDoc },
36
+ { label: '打开…', shortcut: 'Ctrl+O', onClick: file.open },
37
+ { divided: true },
38
+ { label: '保存', shortcut: 'Ctrl+S', disabled: !state.hasDoc, onClick: file.save },
39
+ { label: '另存为…', shortcut: 'Ctrl+Shift+S', disabled: !state.hasDoc, onClick: file.saveAs },
40
+ { label: '自动保存', checked: state.autosave, onClick: () => file.setAutosave(!state.autosave) },
41
+ { divided: true },
42
+ { label: '导入 PowerPoint…', onClick: file.importDoc },
43
+ { label: '导出', disabled: !state.hasDoc, children: [
44
+ { label: 'PPTX 文档 (.pptx)', onClick: file.exportPptx },
45
+ { label: 'PDF 文档 (.pdf)', onClick: file.exportPdf },
46
+ ] },
47
+ { divided: true },
48
+ { label: '打印 / PDF…', shortcut: 'Ctrl+P', disabled: !state.hasDoc, onClick: file.print },
49
+ ];
50
+
51
+ const editMenu = () => [
52
+ { label: '撤销', shortcut: 'Ctrl+Z', disabled: !state.canUndo, onClick: edit.undo },
53
+ { label: '重做', shortcut: 'Ctrl+Y', disabled: !state.canRedo, onClick: edit.redo },
54
+ { divided: true },
55
+ { label: '剪切', shortcut: 'Ctrl+X', disabled: !state.hasSelection, onClick: edit.cut },
56
+ { label: '复制', shortcut: 'Ctrl+C', disabled: !state.hasSelection, onClick: edit.copy },
57
+ { label: '粘贴', shortcut: 'Ctrl+V', disabled: !state.canPaste, onClick: edit.paste },
58
+ { divided: true },
59
+ { label: '删除对象', shortcut: 'Del', disabled: !state.hasSelection, onClick: edit.remove },
60
+ { label: '全选', shortcut: 'Ctrl+A', onClick: edit.selectAll },
61
+ ];
62
+
63
+ const insertMenu = () => [
64
+ { label: '文本框', onClick: insert.text },
65
+ { label: '图片…', onClick: insert.image },
66
+ { label: '形状', children: insert.shapeKinds.map((s) => ({ label: s.label, onClick: () => insert.shape(s.kind) })) },
67
+ { label: '线条', onClick: insert.line },
68
+ { label: '箭头', onClick: insert.arrow },
69
+ { label: '表格', onClick: insert.table },
70
+ { divided: true },
71
+ { label: '新建页', shortcut: 'Ctrl+M', onClick: slide.new },
72
+ { label: '新建页(版式)', children: SLIDE_LAYOUTS.map((l) => ({ label: l.label, onClick: () => slide.newWithLayout(l.key) })) },
73
+ ];
74
+
75
+ const slideMenu = () => [
76
+ { label: '新建页', shortcut: 'Ctrl+M', onClick: slide.new },
77
+ { label: '复制页', shortcut: 'Ctrl+D', onClick: slide.duplicate },
78
+ { label: '删除页', disabled: !state.canRemoveSlide, onClick: slide.remove },
79
+ { divided: true },
80
+ { label: '上移一页', disabled: !state.canMoveUp, onClick: slide.moveUp },
81
+ { label: '下移一页', disabled: !state.canMoveDown, onClick: slide.moveDown },
82
+ { divided: true },
83
+ { label: '页面背景…', onClick: slide.background },
84
+ ];
85
+
86
+ const alignChildren = () => ALIGN_KEYS.map((a) => ({ label: a.label, divided: a.key === 'top', onClick: () => format.align(a.key) }));
87
+
88
+ const formatMenu = () => [
89
+ { label: '项目符号列表', shortcut: 'Ctrl+Shift+8', checked: state.bulletList, disabled: !state.editing, onClick: format.bullet },
90
+ { label: '编号列表', shortcut: 'Ctrl+Shift+7', checked: state.numberList, disabled: !state.editing, onClick: format.numbered },
91
+ { divided: true },
92
+ { label: '对齐页面', disabled: !state.hasSelection, children: alignChildren() },
93
+ { label: '层级', disabled: !state.hasSelection, children: ORDER_KEYS.map((o) => ({ label: o.label, onClick: () => format.order(o.key) })) },
94
+ { divided: true },
95
+ { label: '复制对象', shortcut: 'Ctrl+D', disabled: !state.hasSelection, onClick: format.duplicate },
96
+ { label: '删除对象', shortcut: 'Del', disabled: !state.hasSelection, onClick: format.remove },
97
+ ];
98
+
99
+ const designMenu = () => [
100
+ ...THEME_KEYS.map((key) => ({ label: THEMES[key].label, checked: state.theme === key, onClick: () => design.theme(key) })),
101
+ ];
102
+
103
+ const viewMenu = () => [
104
+ { label: '放大', shortcut: 'Ctrl+=', onClick: view.zoomIn },
105
+ { label: '缩小', shortcut: 'Ctrl+-', onClick: view.zoomOut },
106
+ { label: '重置缩放', shortcut: 'Ctrl+0', onClick: view.zoomReset },
107
+ { divided: true },
108
+ { label: '放映', shortcut: 'F5', onClick: view.present },
109
+ { label: '从第一页放映', onClick: view.presentFromStart },
110
+ { divided: true },
111
+ { label: '演讲者备注', checked: state.notesVisible, onClick: view.toggleNotes },
112
+ ];
113
+
114
+ return [
115
+ { key: 'file', label: '文件', items: fileMenu },
116
+ { key: 'edit', label: '编辑', items: editMenu },
117
+ { key: 'insert', label: '插入', items: insertMenu },
118
+ { key: 'slide', label: '页', items: slideMenu },
119
+ { key: 'format', label: '格式', items: formatMenu },
120
+ { key: 'design', label: '设计', items: designMenu },
121
+ { key: 'view', label: '视图', items: viewMenu },
122
+ ];
123
+ };
@@ -0,0 +1,345 @@
1
+ // [文件] gbpptx·画布对象模型 —— 逻辑画布常量 + 主题表 + 图形路径 + 对象/页工厂与快照归一(移植 guabi office/pptx slidesModel.js)
2
+
3
+ import { escapeHtml } from './util/escape.js';
4
+ import { genId } from './util/id.js';
5
+
6
+ /** 快照结构版本:一期恒为 1,只留演进位 */
7
+ export const SNAPSHOT_VERSION = 1;
8
+
9
+ /** 逻辑画布尺寸(16:9),与 .pptx 写出的 sldSz 一一对应 */
10
+ export const SLIDE_W = 960;
11
+ export const SLIDE_H = 540;
12
+
13
+ /** 主题:底色/前景/强调色(pptx 不支持渐变故一律纯色;与 pptx 主题部件的 clrScheme 由同一张表推导) */
14
+ export const THEMES = {
15
+ clean: { label: '简洁', bg: '#ffffff', text: '#202124', accent: '#e07b1f' },
16
+ midnight: { label: '夜幕', bg: '#232a36', text: '#e8eaed', accent: '#4c9be8' },
17
+ ocean: { label: '海滨', bg: '#0f4c75', text: '#f4faff', accent: '#bbe1fa' },
18
+ sunset: { label: '晚霞', bg: '#c0392b', text: '#fdf2ec', accent: '#f8c471' },
19
+ forest: { label: '林间', bg: '#0f3d33', text: '#eafaf1', accent: '#7dcea0' },
20
+ paper: { label: '纸感', bg: '#f6f1e5', text: '#3d3a33', accent: '#8e44ad' },
21
+ };
22
+
23
+ /** 主题键列表(「主题」下拉条目源,声明顺序即展示顺序) */
24
+ export const THEME_KEYS = Object.keys(THEMES);
25
+
26
+ export const themeOf = (key) => THEMES[key] || THEMES.clean;
27
+
28
+ /** 图形种类(顺序即插入菜单与「图形」下拉顺序) */
29
+ export const SHAPE_KINDS = [
30
+ { kind: 'rect', label: '矩形' },
31
+ { kind: 'round', label: '圆角矩形' },
32
+ { kind: 'ellipse', label: '椭圆' },
33
+ { kind: 'triangle', label: '三角形' },
34
+ { kind: 'diamond', label: '菱形' },
35
+ { kind: 'arrow', label: '箭头' },
36
+ { kind: 'star', label: '五角星' },
37
+ { kind: 'chevron', label: '折角' },
38
+ ];
39
+
40
+ /** 对象类型中文名(属性面板标题用) */
41
+ export const TYPE_NAMES = {
42
+ text: '文本框',
43
+ image: '图片',
44
+ shape: '图形',
45
+ line: '线条',
46
+ table: '表格',
47
+ };
48
+
49
+ /** 复制对象(props 浅拷贝即断开;rows 是二维数组需逐行复制) */
50
+ export const cloneObject = (o) => ({
51
+ ...o,
52
+ id: genId('o'),
53
+ props: { ...o.props, rows: o.props.rows?.map((row) => [...row]) },
54
+ });
55
+
56
+ /** 复制页面 */
57
+ export const copySlide = (slide) => ({ ...slide, id: genId('s'), objects: slide.objects.map(cloneObject) });
58
+
59
+ /** 空页 */
60
+ export const createSlide = () => ({ id: genId('s'), bg: '', notes: '', objects: [] });
61
+
62
+ /** 下一个层级号(追加对象/置顶都按它) */
63
+ export const topZ = (objects) => objects.reduce((max, o) => Math.max(max, o.z || 0), 0);
64
+
65
+ export const createTextObject = (x, y, z) => ({
66
+ id: genId('o'), type: 'text', x, y, w: 360, h: 110, rot: 0, z,
67
+ props: { html: '', font: '', fontSize: 24, color: '', align: 'left', bold: false, italic: false, underline: false },
68
+ });
69
+
70
+ export const createImageObject = (src, x, y, w, h, z) => ({
71
+ id: genId('o'), type: 'image', x, y, w, h, rot: 0, z, props: { src },
72
+ });
73
+
74
+ export const createShapeObject = (kind, x, y, z) => ({
75
+ id: genId('o'), type: 'shape', x, y, w: 220, h: 140, rot: 0, z,
76
+ props: { kind, fill: '#e07b1f', stroke: '#333333', strokeW: 2, text: '', textColor: '#ffffff', font: '', fontSize: 18, bold: false },
77
+ });
78
+
79
+ export const createLineObject = (x, y, z, arrowEnd = false) => ({
80
+ id: genId('o'), type: 'line', x, y, w: 240, h: 0, rot: 0, z,
81
+ props: { stroke: '#202124', strokeW: 2, dash: false, arrowEnd },
82
+ });
83
+
84
+ export const createTableObject = (x, y, z) => ({
85
+ id: genId('o'), type: 'table', x, y, w: 440, h: 200, rot: 0, z,
86
+ props: {
87
+ rows: [['', '', ''], ['', '', ''], ['', '', '']],
88
+ headerRow: true, font: '', fontSize: 16, color: '', colW: [], rowH: [],
89
+ },
90
+ });
91
+
92
+ /**
93
+ * 对象外接矩形(线条的 w/h 是到终点的向量、可为负,故取绝对值并回推左上角)。
94
+ * @returns {{left:number,top:number,width:number,height:number}}
95
+ */
96
+ export const objectRect = (o) => ({
97
+ left: Math.min(o.x, o.x + o.w),
98
+ top: Math.min(o.y, o.y + o.h),
99
+ width: Math.abs(o.w),
100
+ height: Math.abs(o.h),
101
+ });
102
+
103
+ /** 外接矩形 → 绝对定位样式(旋转只发生在这一处 transform 上) */
104
+ export const objectBoxStyle = (o) => {
105
+ const r = objectRect(o);
106
+ return {
107
+ left: `${r.left}px`,
108
+ top: `${r.top}px`,
109
+ width: `${r.width}px`,
110
+ height: `${r.height}px`,
111
+ ...(o.rot ? { transform: `rotate(${o.rot}deg)` } : {}),
112
+ };
113
+ };
114
+
115
+ /**
116
+ * 图形路径(按对象实际像素尺寸生成,故描边与箭头不会因缩放变形)。
117
+ * [坑] 不能用 viewBox + preserveAspectRatio="none" 拉伸:非等比缩放会把描边和箭头拉歪。
118
+ */
119
+ export function shapePath(kind, w, h) {
120
+ const n = (v) => Math.round(v * 100) / 100;
121
+ switch (kind) {
122
+ case 'ellipse': {
123
+ const rx = w / 2;
124
+ const ry = h / 2;
125
+ return `M0,${n(ry)}A${n(rx)},${n(ry)} 0 1 1 ${n(w)},${n(ry)}A${n(rx)},${n(ry)} 0 1 1 0,${n(ry)}Z`;
126
+ }
127
+ case 'round': {
128
+ const r = n(Math.min(w, h) * 0.18);
129
+ return `M${r},0H${n(w - r)}A${r},${r} 0 0 1 ${n(w)},${r}V${n(h - r)}A${r},${r} 0 0 1 ${n(w - r)},${n(h)}H${r}A${r},${r} 0 0 1 0,${n(h - r)}V${r}A${r},${r} 0 0 1 ${r},0Z`;
130
+ }
131
+ case 'triangle':
132
+ return `M${n(w / 2)},0L${n(w)},${n(h)}L0,${n(h)}Z`;
133
+ case 'diamond':
134
+ return `M${n(w / 2)},0L${n(w)},${n(h / 2)}L${n(w / 2)},${n(h)}L0,${n(h / 2)}Z`;
135
+ case 'arrow':
136
+ return `M0,${n(h * 0.2)}L${n(w * 0.6)},${n(h * 0.2)}L${n(w * 0.6)},0L${n(w)},${n(h / 2)}L${n(w * 0.6)},${n(h)}L${n(w * 0.6)},${n(h * 0.8)}L0,${n(h * 0.8)}Z`;
137
+ case 'chevron':
138
+ return `M0,0L${n(w * 0.65)},0L${n(w)},${n(h / 2)}L${n(w * 0.65)},${n(h)}L0,${n(h)}L${n(w * 0.35)},${n(h / 2)}Z`;
139
+ case 'star': {
140
+ let d = '';
141
+ for (let i = 0; i < 10; i += 1) {
142
+ const r = i % 2 === 0 ? 1 : 0.42;
143
+ const a = -Math.PI / 2 + (i * Math.PI) / 5;
144
+ d += `${i ? 'L' : 'M'}${n(w / 2 + (w / 2) * r * Math.cos(a))},${n(h / 2 + (h / 2) * r * Math.sin(a))}`;
145
+ }
146
+ return `${d}Z`;
147
+ }
148
+ default:
149
+ return `M0,0H${n(w)}V${n(h)}H0Z`;
150
+ }
151
+ }
152
+
153
+ /** 槽位/单元格正文是否算空:无文字且不含自带视觉的元素(contenteditable 清空后剩 `<p><br></p>`) */
154
+ export function isBlankHtml(html) {
155
+ return !html || (!html.replace(/<[^>]*>/g, '').trim() && !/<(img|hr|table)\b/i.test(html));
156
+ }
157
+
158
+ /** HTML → 纯文本行(图形内文字存纯文本,\n 分行) */
159
+ export const htmlToPlain = (html) =>
160
+ (html || '')
161
+ .replace(/<br\s*\/?>/gi, '\n')
162
+ .replace(/<\/(div|p|li)>/gi, '\n')
163
+ .replace(/<[^>]*>/g, '')
164
+ .replace(/&nbsp;/g, ' ')
165
+ .replace(/&lt;/g, '<')
166
+ .replace(/&gt;/g, '>')
167
+ .replace(/&amp;/g, '&')
168
+ .replace(/\n+$/, '');
169
+
170
+ /** 纯文本行 → 单行 HTML(图形内文字回灌给就地编辑器) */
171
+ export const plainToHtml = (text) =>
172
+ (text || '').split('\n').map(escapeHtml).join('<br>');
173
+
174
+ /**
175
+ * 就地剥掉一个 DOM 子树里的内联字号(活 DOM 与 DOMParser 文档共用同一份实现)。
176
+ * [坑] 内联 font-size 优先级高于容器字号:对象级字号改动只改容器,正文里残留的内联字号会把文字
177
+ * 体积锁死——「设置字号不生效」的根因,故改对象字号必须同步走这里。
178
+ */
179
+ export function clearInlineFontSizes(root) {
180
+ root?.querySelectorAll('*').forEach((el) => {
181
+ if (el.style?.fontSize) el.style.removeProperty('font-size');
182
+ if (el.tagName === 'FONT') el.removeAttribute('size');
183
+ if ((el.tagName === 'SPAN' || el.tagName === 'FONT') && !el.attributes.length) el.replaceWith(...el.childNodes);
184
+ });
185
+ }
186
+
187
+ /** HTML 片段版的内联字号清理(模型数据侧入口) */
188
+ function stripInlineFontSize(html) {
189
+ if (!html) return html || '';
190
+ const doc = new DOMParser().parseFromString(`<div>${html}</div>`, 'text/html');
191
+ const box = doc.body.firstElementChild;
192
+ clearInlineFontSizes(box);
193
+ return box?.innerHTML || '';
194
+ }
195
+
196
+ /** 对象级字号唯一入口:整框统一,改字号同时清掉正文内联字号(pptx 往返也只认「字号随对象走」) */
197
+ export function setObjectFontSize(obj, px) {
198
+ obj.props.fontSize = px;
199
+ if (obj.type === 'text' && obj.props.html) obj.props.html = stripInlineFontSize(obj.props.html);
200
+ }
201
+
202
+ const num = (v, fallback = 0) => (typeof v === 'number' && Number.isFinite(v) ? v : fallback);
203
+ const str = (v) => (typeof v === 'string' ? v : '');
204
+
205
+ /** 归一单个对象:类型白名单收口、坐标补默认值、props 按类型只留有效字段 */
206
+ function normalizeObject(raw, z) {
207
+ const type = str(raw?.type);
208
+ if (!TYPE_NAMES[type]) return null;
209
+ const props = raw?.props && typeof raw.props === 'object' ? raw.props : {};
210
+ const base = {
211
+ id: str(raw?.id) || genId('o'),
212
+ type,
213
+ x: num(raw?.x),
214
+ y: num(raw?.y),
215
+ w: num(raw?.w),
216
+ h: num(raw?.h),
217
+ rot: num(raw?.rot),
218
+ z,
219
+ props: {},
220
+ };
221
+ if (type === 'text') {
222
+ base.props = {
223
+ html: str(props.html),
224
+ font: str(props.font),
225
+ fontSize: num(props.fontSize, 24),
226
+ color: str(props.color),
227
+ align: ['left', 'center', 'right', 'justify'].includes(props.align) ? props.align : 'left',
228
+ bold: !!props.bold,
229
+ italic: !!props.italic,
230
+ underline: !!props.underline,
231
+ };
232
+ } else if (type === 'image') {
233
+ base.props = { src: str(props.src) };
234
+ if (!base.props.src) return null;
235
+ } else if (type === 'shape') {
236
+ base.props = {
237
+ kind: SHAPE_KINDS.some((k) => k.kind === props.kind) ? props.kind : 'rect',
238
+ fill: str(props.fill) || '#e07b1f',
239
+ stroke: str(props.stroke),
240
+ strokeW: num(props.strokeW),
241
+ text: str(props.text),
242
+ textColor: str(props.textColor) || '#ffffff',
243
+ font: str(props.font),
244
+ fontSize: num(props.fontSize, 18),
245
+ bold: !!props.bold,
246
+ };
247
+ } else if (type === 'line') {
248
+ base.props = {
249
+ stroke: str(props.stroke) || '#202124',
250
+ strokeW: num(props.strokeW, 2),
251
+ dash: !!props.dash,
252
+ arrowEnd: !!props.arrowEnd,
253
+ };
254
+ } else {
255
+ const rows = Array.isArray(props.rows) ? props.rows.map((row) => (Array.isArray(row) ? row.map((c) => str(c)) : [])).filter((r) => r.length) : [];
256
+ if (!rows.length) return null;
257
+ base.props = {
258
+ rows,
259
+ headerRow: !!props.headerRow,
260
+ font: str(props.font),
261
+ fontSize: num(props.fontSize, 16),
262
+ color: str(props.color),
263
+ colW: Array.isArray(props.colW) ? props.colW.map((v) => num(v)) : [],
264
+ rowH: Array.isArray(props.rowH) ? props.rowH.map((v) => num(v)) : [],
265
+ };
266
+ }
267
+ return base;
268
+ }
269
+
270
+ /** 归一单页:对象逐个归一并按 z 排序(z 缺失时按数组顺序补) */
271
+ function normalizeSlide(raw) {
272
+ const objects = (Array.isArray(raw?.objects) ? raw.objects : [])
273
+ .map((o, i) => normalizeObject(o, typeof o?.z === 'number' ? o.z : i + 1))
274
+ .filter(Boolean)
275
+ .sort((a, b) => a.z - b.z);
276
+ return {
277
+ id: str(raw?.id) || genId('s'),
278
+ bg: str(raw?.bg),
279
+ notes: str(raw?.notes),
280
+ objects,
281
+ };
282
+ }
283
+
284
+ /** 快照归一:slides 非数组一律返回 null,由调用方按空稿打开(空稿不置脏) */
285
+ export function normalizeSnapshot(raw) {
286
+ if (!raw || !Array.isArray(raw.slides)) return null;
287
+ return {
288
+ version: SNAPSHOT_VERSION,
289
+ title: str(raw.title),
290
+ theme: THEMES[raw.theme] ? raw.theme : 'clean',
291
+ slides: raw.slides.map(normalizeSlide),
292
+ };
293
+ }
294
+
295
+ /** 新建页版式预设(「插入 → 新建页(版式)」条目源;文字色一律留空走主题兜底) */
296
+ export const SLIDE_LAYOUTS = [
297
+ { key: 'title', label: '标题页' },
298
+ { key: 'titleOnly', label: '仅标题' },
299
+ { key: 'titleContent', label: '标题与内容' },
300
+ { key: 'twoContent', label: '两栏内容' },
301
+ { key: 'caption', label: '底部题注' },
302
+ ];
303
+
304
+ const layoutText = (html, x, y, w, h, fontSize, align = 'left') => {
305
+ const o = createTextObject(x, y, 0);
306
+ o.w = w;
307
+ o.h = h;
308
+ o.props.html = html;
309
+ o.props.fontSize = fontSize;
310
+ o.props.align = align;
311
+ return o;
312
+ };
313
+
314
+ /** 按版式建页(未知 key 与 createSlide 同为空页);对象 z 按数组顺序补齐 */
315
+ export function createSlideWithLayout(key) {
316
+ const presets = {
317
+ title: [
318
+ layoutText('单击输入标题', 80, 190, 800, 90, 44, 'center'),
319
+ layoutText('单击输入副标题', 180, 300, 600, 50, 20, 'center'),
320
+ ],
321
+ titleOnly: [layoutText('单击输入标题', 50, 34, 860, 66, 32)],
322
+ titleContent: [
323
+ layoutText('单击输入标题', 50, 34, 860, 66, 32),
324
+ layoutText('单击输入正文', 50, 130, 860, 340, 22),
325
+ ],
326
+ twoContent: [
327
+ layoutText('单击输入标题', 50, 34, 860, 66, 32),
328
+ layoutText('左侧内容', 50, 130, 420, 340, 20),
329
+ layoutText('右侧内容', 490, 130, 420, 340, 20),
330
+ ],
331
+ caption: [layoutText('单击输入题注', 80, 440, 800, 60, 20, 'center')],
332
+ };
333
+ const slide = createSlide();
334
+ const objects = presets[key];
335
+ if (objects) slide.objects = objects.map((o, i) => ({ ...o, z: i + 1 }));
336
+ return slide;
337
+ }
338
+
339
+ /** 空稿:一页空白演示,新建文档与损坏快照共用 */
340
+ export const createDraft = () => ({
341
+ version: SNAPSHOT_VERSION,
342
+ title: '',
343
+ theme: 'clean',
344
+ slides: [createSlide()],
345
+ });