gbpptx 0.1.1 → 0.3.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 (55) hide show
  1. package/README.md +220 -158
  2. package/package.json +15 -4
  3. package/src/index.js +16 -8
  4. package/src/kernel/model.js +397 -91
  5. package/src/kernel/parts.js +3 -3
  6. package/src/kernel/pptx.js +17 -12
  7. package/src/kernel/read.js +747 -157
  8. package/src/kernel/roundtrip.js +118 -18
  9. package/src/kernel/text.js +133 -28
  10. package/src/kernel/write.js +160 -49
  11. package/web/app/main.js +8 -8
  12. package/web/app/slides/arrange.js +101 -0
  13. package/web/app/slides/clipboard.js +93 -0
  14. package/web/app/slides/file.js +243 -0
  15. package/web/app/slides/find.js +199 -0
  16. package/web/app/slides/history.js +100 -0
  17. package/web/app/slides/keys.js +174 -0
  18. package/web/app/slides/menus.js +105 -0
  19. package/web/app/slides/objects.js +254 -0
  20. package/web/app/slides/render.js +183 -0
  21. package/web/app/slides/shared.js +85 -0
  22. package/web/app/slides/slides.js +103 -0
  23. package/web/app/slides/table.js +102 -0
  24. package/web/app/slides/text.js +418 -0
  25. package/web/app/slides/view.js +130 -0
  26. package/web/app/slides-app.js +285 -844
  27. package/web/app/ui/backstage.js +255 -0
  28. package/web/app/ui/canvas.js +762 -277
  29. package/web/app/ui/color-panel.js +58 -58
  30. package/web/app/ui/dialogs.js +120 -120
  31. package/web/app/ui/file-io.js +116 -89
  32. package/web/app/ui/format-pane.js +427 -0
  33. package/web/app/ui/icons.js +129 -0
  34. package/web/app/ui/menu.js +75 -105
  35. package/web/app/ui/notify.js +45 -45
  36. package/web/app/ui/page-view.js +221 -150
  37. package/web/app/ui/popup.js +80 -80
  38. package/web/app/ui/present.js +292 -100
  39. package/web/app/ui/ribbon.js +878 -0
  40. package/web/app/ui/slide-pane.js +189 -0
  41. package/web/app/ui/statusbar.js +93 -0
  42. package/web/app/ui/text-styles.js +84 -84
  43. package/web/app/ui/titlebar.js +208 -0
  44. package/web/app/ui/transition.js +30 -0
  45. package/web/index.html +23 -21
  46. package/web/serve.mjs +20 -67
  47. package/web/styles/office.css +642 -0
  48. package/web/styles/slides.css +210 -212
  49. package/web/styles/tokens.css +154 -155
  50. package/src/kernel/menus.js +0 -123
  51. package/src/kernel/util/escape.js +0 -21
  52. package/src/kernel/util/id.js +0 -13
  53. package/web/app/ui/inspector.js +0 -209
  54. package/web/app/ui/slides-bar.js +0 -63
  55. package/web/app/ui/toolbar.js +0 -200
@@ -1,15 +1,22 @@
1
- // [文件] gbpptx·画布对象模型 —— 逻辑画布常量 + 主题表 + 图形路径 + 对象/页工厂与快照归一(移植 guabi office/pptx slidesModel.js)
1
+ // [文件] gbpptx·画布对象模型 —— 逻辑画布常量 + 主题/画幅 + 图形路径 + 对象/页工厂与快照归一
2
2
 
3
- import { escapeHtml } from './util/escape.js';
4
- import { genId } from './util/id.js';
3
+ import { escapeHtml, genId } from 'guabiutils';
5
4
 
6
- /** 快照结构版本:一期恒为 1,只留演进位 */
7
- export const SNAPSHOT_VERSION = 1;
5
+ /** 快照结构版本:2 起带画幅/切换/隐藏页与更完整的对象字段 */
6
+ export const SNAPSHOT_VERSION = 2;
8
7
 
9
- /** 逻辑画布尺寸(16:9),与 .pptx 写出的 sldSz 一一对应 */
8
+ /** 默认逻辑画布尺寸(16:9),也是 fallback 尺寸 */
10
9
  export const SLIDE_W = 960;
11
10
  export const SLIDE_H = 540;
12
11
 
12
+ /** 画幅预设(逻辑像素;与 .pptx 的 sldSz 一一对应) */
13
+ export const SLIDE_SIZES = [
14
+ { key: 'wide', label: '宽屏 (16:9)', w: SLIDE_W, h: SLIDE_H },
15
+ { key: 'standard', label: '标准 (4:3)', w: 720, h: SLIDE_H },
16
+ ];
17
+
18
+ export const DEFAULT_SIZE = { w: SLIDE_W, h: SLIDE_H };
19
+
13
20
  /** 主题:底色/前景/强调色(pptx 不支持渐变故一律纯色;与 pptx 主题部件的 clrScheme 由同一张表推导) */
14
21
  export const THEMES = {
15
22
  clean: { label: '简洁', bg: '#ffffff', text: '#202124', accent: '#e07b1f' },
@@ -20,72 +27,209 @@ export const THEMES = {
20
27
  paper: { label: '纸感', bg: '#f6f1e5', text: '#3d3a33', accent: '#8e44ad' },
21
28
  };
22
29
 
23
- /** 主题键列表(「主题」下拉条目源,声明顺序即展示顺序) */
24
30
  export const THEME_KEYS = Object.keys(THEMES);
25
-
26
31
  export const themeOf = (key) => THEMES[key] || THEMES.clean;
27
32
 
28
- /** 图形种类(顺序即插入菜单与「图形」下拉顺序) */
33
+ /* ---------- 尺寸 ---------- */
34
+
35
+ /** 任意输入 → 合法画幅 */
36
+ export function normalizeSize(raw) {
37
+ const w = Math.round(Number(raw?.w));
38
+ const h = Math.round(Number(raw?.h));
39
+ if (!Number.isFinite(w) || !Number.isFinite(h) || w < 120 || h < 120) return { ...DEFAULT_SIZE };
40
+ return { w: Math.min(w, 4000), h: Math.min(h, 4000) };
41
+ }
42
+
43
+ /** 快照的画幅(老快照没有 size 时回落到 16:9) */
44
+ export const sizeOf = (snapshot) => normalizeSize(snapshot?.size);
45
+
46
+ export const sizePresetKey = (size) => {
47
+ const s = normalizeSize(size);
48
+ const hit = SLIDE_SIZES.find((p) => p.w === s.w && p.h === s.h);
49
+ return hit ? hit.key : 'custom';
50
+ };
51
+
52
+ /* ---------- 主题与表格样式 ---------- */
53
+
54
+ const hexToRgb = (hex) => {
55
+ const v = String(hex || '').replace('#', '');
56
+ const n = Number.parseInt(v.length === 3 ? v.split('').map((c) => c + c).join('') : v, 16);
57
+ return Number.isFinite(n) ? { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 } : { r: 0, g: 0, b: 0 };
58
+ };
59
+ const rgbToHex = ({ r, g, b }) => `#${[r, g, b].map((v) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, '0')).join('')}`;
60
+ const mixHex = (a, b, t) => {
61
+ const x = hexToRgb(a);
62
+ const y = hexToRgb(b);
63
+ const k = Math.max(0, Math.min(1, t));
64
+ return rgbToHex({ r: x.r + (y.r - x.r) * k, g: x.g + (y.g - x.g) * k, b: x.b + (y.b - x.b) * k });
65
+ };
66
+
67
+ const luminanceOf = (hex) => {
68
+ const { r, g, b } = hexToRgb(hex);
69
+ return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
70
+ };
71
+
72
+ /** 底色上的可读文字色(浅强调色用深字,深强调色用白字) */
73
+ export const contrastText = (background) => (luminanceOf(background) > 0.55 ? '#201f1e' : '#ffffff');
74
+
75
+ /** 表格样式(与 PowerPoint 的「表格样式」同口径:表头/镶边/深色三种可往返外观) */
76
+ export const TABLE_STYLES = [
77
+ { key: 'plain', label: '无样式' },
78
+ { key: 'header', label: '标题行' },
79
+ { key: 'banded', label: '镶边行' },
80
+ { key: 'dark', label: '深色' },
81
+ ];
82
+
83
+ /** 表格样式在当前主题下的具体取色(渲染与写出共用,禁各算一份) */
84
+ export function tableStyleColors(styleKey, themeKey) {
85
+ const t = themeOf(themeKey);
86
+ const key = TABLE_STYLES.some((s) => s.key === styleKey) ? styleKey : 'plain';
87
+ const base = { headerFill: '', headerText: '', bandFill: '', border: mixHex(t.text, t.bg, 0.78), bodyText: '' };
88
+ if (key === 'plain') return base;
89
+ base.headerFill = t.accent;
90
+ base.headerText = contrastText(t.accent);
91
+ base.border = mixHex(t.accent, t.bg, 0.55);
92
+ if (key === 'banded') base.bandFill = mixHex(t.accent, t.bg, 0.88);
93
+ if (key === 'dark') {
94
+ base.bandFill = mixHex(t.accent, '#000000', 0.72);
95
+ base.border = mixHex(t.accent, '#000000', 0.35);
96
+ base.bodyText = '#ffffff';
97
+ }
98
+ return base;
99
+ }
100
+
101
+ /* ---------- 切换 ---------- */
102
+
103
+ export const TRANSITIONS = [
104
+ { key: 'none', label: '无', icon: 'none' },
105
+ { key: 'fade', label: '淡出', icon: 'fade' },
106
+ { key: 'push', label: '推进', icon: 'push', dir: 'u' },
107
+ { key: 'wipe', label: '擦除', icon: 'wipe', dir: 'l' },
108
+ { key: 'cover', label: '覆盖', icon: 'cover', dir: 'l' },
109
+ { key: 'split', label: '分割', icon: 'split' },
110
+ { key: 'zoom', label: '缩放', icon: 'zoom' },
111
+ ];
112
+
113
+ const TRANSITION_KEYS = new Set(TRANSITIONS.map((t) => t.key));
114
+ const DIRECTIONS = new Set(['l', 'r', 'u', 'd']);
115
+ const SPEEDS = new Set(['slow', 'med', 'fast']);
116
+
117
+ export const DEFAULT_TRANSITION = { type: 'none', dir: 'l', speed: 'med' };
118
+
119
+ export function normalizeTransition(raw) {
120
+ const type = TRANSITION_KEYS.has(raw?.type) ? raw.type : 'none';
121
+ return {
122
+ type,
123
+ dir: DIRECTIONS.has(raw?.dir) ? raw.dir : 'l',
124
+ speed: SPEEDS.has(raw?.speed) ? raw.speed : 'med',
125
+ };
126
+ }
127
+
128
+ export const transitionOf = (slide) => normalizeTransition(slide?.transition);
129
+
130
+ /* ---------- 图形 ---------- */
131
+
132
+ /** 图形种类(顺序即插入菜单与形状库顺序;prst 为 PresentationML 预设名) */
29
133
  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: '折角' },
134
+ { kind: 'rect', label: '矩形', prst: 'rect' },
135
+ { kind: 'round', label: '圆角矩形', prst: 'roundRect' },
136
+ { kind: 'ellipse', label: '椭圆', prst: 'ellipse' },
137
+ { kind: 'triangle', label: '等腰三角形', prst: 'triangle' },
138
+ { kind: 'rightTriangle', label: '直角三角形', prst: 'rtTriangle' },
139
+ { kind: 'diamond', label: '菱形', prst: 'diamond' },
140
+ { kind: 'parallelogram', label: '平行四边形', prst: 'parallelogram' },
141
+ { kind: 'trapezoid', label: '梯形', prst: 'trapezoid' },
142
+ { kind: 'pentagon', label: '五边形', prst: 'pentagon' },
143
+ { kind: 'hexagon', label: '六边形', prst: 'hexagon' },
144
+ { kind: 'octagon', label: '八边形', prst: 'octagon' },
145
+ { kind: 'star', label: '五角星', prst: 'star5' },
146
+ { kind: 'arrow', label: '右箭头', prst: 'rightArrow' },
147
+ { kind: 'arrowLeft', label: '左箭头', prst: 'leftArrow' },
148
+ { kind: 'arrowUp', label: '上箭头', prst: 'upArrow' },
149
+ { kind: 'arrowDown', label: '下箭头', prst: 'downArrow' },
150
+ { kind: 'chevron', label: 'V 形', prst: 'chevron' },
151
+ { kind: 'plus', label: '十字形', prst: 'plus' },
38
152
  ];
39
153
 
154
+ export const SHAPE_KIND_KEYS = new Set(SHAPE_KINDS.map((k) => k.kind));
155
+ export const shapeKindLabel = (kind) => SHAPE_KINDS.find((k) => k.kind === kind)?.label || '图形';
156
+ export const shapePreset = (kind) => SHAPE_KINDS.find((k) => k.kind === kind)?.prst || 'rect';
157
+
40
158
  /** 对象类型中文名(属性面板标题用) */
41
159
  export const TYPE_NAMES = {
42
160
  text: '文本框',
43
161
  image: '图片',
44
- shape: '图形',
162
+ shape: '形状',
45
163
  line: '线条',
46
164
  table: '表格',
47
165
  };
48
166
 
49
- /** 复制对象(props 浅拷贝即断开;rows 是二维数组需逐行复制) */
167
+ /** 复制对象(props 深一层拷贝,rows/crop 单独断开) */
50
168
  export const cloneObject = (o) => ({
51
169
  ...o,
52
170
  id: genId('o'),
53
- props: { ...o.props, rows: o.props.rows?.map((row) => [...row]) },
171
+ props: {
172
+ ...o.props,
173
+ rows: o.props.rows?.map((row) => [...row]),
174
+ crop: o.props.crop ? { ...o.props.crop } : undefined,
175
+ },
54
176
  });
55
177
 
56
178
  /** 复制页面 */
57
- export const copySlide = (slide) => ({ ...slide, id: genId('s'), objects: slide.objects.map(cloneObject) });
179
+ export const copySlide = (slide) => ({
180
+ ...slide,
181
+ id: genId('s'),
182
+ transition: { ...normalizeTransition(slide.transition) },
183
+ objects: slide.objects.map(cloneObject),
184
+ });
58
185
 
59
186
  /** 空页 */
60
- export const createSlide = () => ({ id: genId('s'), bg: '', notes: '', objects: [] });
187
+ export const createSlide = () => ({
188
+ id: genId('s'),
189
+ bg: '',
190
+ notes: '',
191
+ hidden: false,
192
+ transition: { ...DEFAULT_TRANSITION },
193
+ objects: [],
194
+ });
61
195
 
62
196
  /** 下一个层级号(追加对象/置顶都按它) */
63
197
  export const topZ = (objects) => objects.reduce((max, o) => Math.max(max, o.z || 0), 0);
64
198
 
65
199
  export const createTextObject = (x, y, z) => ({
66
200
  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 },
201
+ props: {
202
+ html: '', font: '', fontSize: 24, color: '', align: 'left', bold: false, italic: false,
203
+ underline: false, strike: false, anchor: 'top', lineSpacing: 1.0, fill: '', stroke: '', strokeW: 0,
204
+ },
68
205
  });
69
206
 
70
207
  export const createImageObject = (src, x, y, w, h, z) => ({
71
- id: genId('o'), type: 'image', x, y, w, h, rot: 0, z, props: { src },
208
+ id: genId('o'), type: 'image', x, y, w, h, rot: 0, z,
209
+ props: { src, alt: '', crop: { l: 0, t: 0, r: 0, b: 0 }, shadow: false },
72
210
  });
73
211
 
74
212
  export const createShapeObject = (kind, x, y, z) => ({
75
213
  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 },
214
+ props: {
215
+ kind: SHAPE_KIND_KEYS.has(kind) ? kind : 'rect',
216
+ fill: '#e07b1f', stroke: '#333333', strokeW: 2, dash: 'solid', shadow: false,
217
+ text: '', textColor: '#ffffff', font: '', fontSize: 18, bold: false, italic: false,
218
+ underline: false, strike: false, align: 'center', anchor: 'middle', lineSpacing: 1,
219
+ },
77
220
  });
78
221
 
79
222
  export const createLineObject = (x, y, z, arrowEnd = false) => ({
80
223
  id: genId('o'), type: 'line', x, y, w: 240, h: 0, rot: 0, z,
81
- props: { stroke: '#202124', strokeW: 2, dash: false, arrowEnd },
224
+ props: { stroke: '#202124', strokeW: 2, dash: 'solid', arrowStart: false, arrowEnd },
82
225
  });
83
226
 
84
- export const createTableObject = (x, y, z) => ({
227
+ export const createTableObject = (x, y, z, rows = 3, cols = 3) => ({
85
228
  id: genId('o'), type: 'table', x, y, w: 440, h: 200, rot: 0, z,
86
229
  props: {
87
- rows: [['', '', ''], ['', '', ''], ['', '', '']],
88
- headerRow: true, font: '', fontSize: 16, color: '', colW: [], rowH: [],
230
+ rows: Array.from({ length: Math.max(1, rows) }, () => Array.from({ length: Math.max(1, cols) }, () => '')),
231
+ headerRow: true, bandedRows: false, tableStyle: 'header',
232
+ font: '', fontSize: 16, color: '', colW: [], rowH: [],
89
233
  },
90
234
  });
91
235
 
@@ -112,50 +256,127 @@ export const objectBoxStyle = (o) => {
112
256
  };
113
257
  };
114
258
 
259
+ /** 一组对象的并集矩形 */
260
+ export function unionRect(objects) {
261
+ if (!objects.length) return { left: 0, top: 0, width: 0, height: 0 };
262
+ const rects = objects.map(objectRect);
263
+ const left = Math.min(...rects.map((r) => r.left));
264
+ const top = Math.min(...rects.map((r) => r.top));
265
+ const right = Math.max(...rects.map((r) => r.left + r.width));
266
+ const bottom = Math.max(...rects.map((r) => r.top + r.height));
267
+ return { left, top, width: right - left, height: bottom - top };
268
+ }
269
+
270
+ const n2 = (v) => Math.round(v * 100) / 100;
271
+ const polygon = (points) => `M${points.map(([x, y]) => `${n2(x)},${n2(y)}`).join('L')}Z`;
272
+ const regularPolygon = (w, h, count, rotation = -Math.PI / 2) => {
273
+ const points = [];
274
+ for (let i = 0; i < count; i += 1) {
275
+ const a = rotation + (i * Math.PI * 2) / count;
276
+ points.push([w / 2 + (w / 2) * Math.cos(a), h / 2 + (h / 2) * Math.sin(a)]);
277
+ }
278
+ return polygon(points);
279
+ };
280
+
115
281
  /**
116
282
  * 图形路径(按对象实际像素尺寸生成,故描边与箭头不会因缩放变形)。
117
283
  * [坑] 不能用 viewBox + preserveAspectRatio="none" 拉伸:非等比缩放会把描边和箭头拉歪。
118
284
  */
119
285
  export function shapePath(kind, w, h) {
120
- const n = (v) => Math.round(v * 100) / 100;
286
+ w = Math.max(1, w);
287
+ h = Math.max(1, h);
121
288
  switch (kind) {
122
289
  case 'ellipse': {
123
290
  const rx = w / 2;
124
291
  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`;
292
+ return `M0,${n2(ry)}A${n2(rx)},${n2(ry)} 0 1 1 ${n2(w)},${n2(ry)}A${n2(rx)},${n2(ry)} 0 1 1 0,${n2(ry)}Z`;
126
293
  }
127
294
  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`;
295
+ const r = n2(Math.min(w, h) * 0.18);
296
+ return `M${r},0H${n2(w - r)}A${r},${r} 0 0 1 ${n2(w)},${r}V${n2(h - r)}A${r},${r} 0 0 1 ${n2(w - r)},${n2(h)}H${r}A${r},${r} 0 0 1 0,${n2(h - r)}V${r}A${r},${r} 0 0 1 ${r},0Z`;
130
297
  }
131
298
  case 'triangle':
132
- return `M${n(w / 2)},0L${n(w)},${n(h)}L0,${n(h)}Z`;
299
+ return polygon([[w / 2, 0], [w, h], [0, h]]);
300
+ case 'rightTriangle':
301
+ return polygon([[0, 0], [0, h], [w, h]]);
133
302
  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`;
303
+ return polygon([[w / 2, 0], [w, h / 2], [w / 2, h], [0, h / 2]]);
304
+ case 'parallelogram': {
305
+ const off = Math.min(w * 0.28, h * 0.55);
306
+ return polygon([[off, 0], [w, 0], [w - off, h], [0, h]]);
307
+ }
308
+ case 'trapezoid': {
309
+ const off = Math.min(w * 0.22, h * 0.55);
310
+ return polygon([[off, 0], [w - off, 0], [w, h], [0, h]]);
311
+ }
312
+ case 'pentagon':
313
+ return regularPolygon(w, h, 5);
314
+ case 'hexagon': {
315
+ const c = Math.min(w * 0.25, h * 0.5);
316
+ return polygon([[c, 0], [w - c, 0], [w, h / 2], [w - c, h], [c, h], [0, h / 2]]);
317
+ }
318
+ case 'octagon': {
319
+ const c = Math.min(w, h) * 0.29;
320
+ return polygon([[c, 0], [w - c, 0], [w, c], [w, h - c], [w - c, h], [c, h], [0, h - c], [0, c]]);
321
+ }
139
322
  case 'star': {
140
323
  let d = '';
141
324
  for (let i = 0; i < 10; i += 1) {
142
325
  const r = i % 2 === 0 ? 1 : 0.42;
143
326
  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))}`;
327
+ d += `${i ? 'L' : 'M'}${n2(w / 2 + (w / 2) * r * Math.cos(a))},${n2(h / 2 + (h / 2) * r * Math.sin(a))}`;
145
328
  }
146
329
  return `${d}Z`;
147
330
  }
331
+ case 'arrow': {
332
+ const top = h * 0.25;
333
+ const bottom = h * 0.75;
334
+ const neck = w * 0.55;
335
+ return polygon([[0, top], [neck, top], [neck, 0], [w, h / 2], [neck, h], [neck, bottom], [0, bottom]]);
336
+ }
337
+ case 'arrowLeft': {
338
+ const top = h * 0.25;
339
+ const bottom = h * 0.75;
340
+ const neck = w * 0.45;
341
+ return polygon([[w, top], [neck, top], [neck, 0], [0, h / 2], [neck, h], [neck, bottom], [w, bottom]]);
342
+ }
343
+ case 'arrowUp':
344
+ return polygon([[w * 0.25, h], [w * 0.25, h * 0.45], [0, h * 0.45], [w / 2, 0], [w, h * 0.45], [w * 0.75, h * 0.45], [w * 0.75, h]]);
345
+ case 'arrowDown':
346
+ return polygon([[w * 0.25, 0], [w * 0.25, h * 0.55], [0, h * 0.55], [w / 2, h], [w, h * 0.55], [w * 0.75, h * 0.55], [w * 0.75, 0]]);
347
+ case 'chevron':
348
+ return polygon([[0, 0], [w * 0.65, 0], [w, h / 2], [w * 0.65, h], [0, h], [w * 0.35, h / 2]]);
349
+ case 'plus': {
350
+ const c = Math.min(w, h) * 0.32;
351
+ return polygon([
352
+ [c, 0], [w - c, 0], [w - c, c], [w, c], [w, h - c], [w - c, h - c],
353
+ [w - c, h], [c, h], [c, h - c], [0, h - c], [0, c], [c, c],
354
+ ]);
355
+ }
148
356
  default:
149
- return `M0,0H${n(w)}V${n(h)}H0Z`;
357
+ return `M0,0H${n2(w)}V${n2(h)}H0Z`;
150
358
  }
151
359
  }
152
360
 
153
- /** 槽位/单元格正文是否算空:无文字且不含自带视觉的元素(contenteditable 清空后剩 `<p><br></p>`) */
361
+ /** 线条虚线样式 → SVG stroke-dasharray / 渲染层共用 */
362
+ export const DASH_STYLES = {
363
+ solid: '',
364
+ dash: '8 6',
365
+ lgDash: '14 7',
366
+ dot: '2 4',
367
+ dashDot: '8 4 2 4',
368
+ };
369
+
370
+ export const DASH_KEYS = new Set(Object.keys(DASH_STYLES));
371
+
372
+ /* ---------- 文本工具 ---------- */
373
+
374
+ /** 正文是否为空(无文字且不含自带视觉的元素) */
154
375
  export function isBlankHtml(html) {
155
376
  return !html || (!html.replace(/<[^>]*>/g, '').trim() && !/<(img|hr|table)\b/i.test(html));
156
377
  }
157
378
 
158
- /** HTML → 纯文本行(图形内文字存纯文本,\n 分行) */
379
+ /** HTML → 纯文本行(图形内文字存纯文本,\n 分行;列表保留标记) */
159
380
  export const htmlToPlain = (html) =>
160
381
  (html || '')
161
382
  .replace(/<br\s*\/?>/gi, '\n')
@@ -199,8 +420,22 @@ export function setObjectFontSize(obj, px) {
199
420
  if (obj.type === 'text' && obj.props.html) obj.props.html = stripInlineFontSize(obj.props.html);
200
421
  }
201
422
 
423
+ /* ---------- 归一 ---------- */
424
+
202
425
  const num = (v, fallback = 0) => (typeof v === 'number' && Number.isFinite(v) ? v : fallback);
203
426
  const str = (v) => (typeof v === 'string' ? v : '');
427
+ const bool = (v) => v === true;
428
+ const ALIGNS = new Set(['left', 'center', 'right', 'justify']);
429
+ const ANCHORS = new Set(['top', 'middle', 'bottom']);
430
+
431
+ const normalizeCrop = (raw) => {
432
+ const pct = (v) => Math.max(0, Math.min(49, Math.round(num(v))));
433
+ const l = pct(raw?.l);
434
+ const t = pct(raw?.t);
435
+ const r = Math.min(pct(raw?.r), 98 - l);
436
+ const b = Math.min(pct(raw?.b), 98 - t);
437
+ return { l, t, r, b };
438
+ };
204
439
 
205
440
  /** 归一单个对象:类型白名单收口、坐标补默认值、props 按类型只留有效字段 */
206
441
  function normalizeObject(raw, z) {
@@ -224,39 +459,75 @@ function normalizeObject(raw, z) {
224
459
  font: str(props.font),
225
460
  fontSize: num(props.fontSize, 24),
226
461
  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,
462
+ align: ALIGNS.has(props.align) ? props.align : 'left',
463
+ bold: bool(props.bold),
464
+ italic: bool(props.italic),
465
+ underline: bool(props.underline),
466
+ strike: bool(props.strike),
467
+ anchor: ANCHORS.has(props.anchor) ? props.anchor : 'top',
468
+ lineSpacing: Math.max(0.6, Math.min(4, num(props.lineSpacing, 1.0))),
469
+ fill: str(props.fill),
470
+ stroke: str(props.stroke),
471
+ strokeW: str(props.stroke) ? Math.max(0, num(props.strokeW)) : 0,
231
472
  };
232
473
  } else if (type === 'image') {
233
- base.props = { src: str(props.src) };
474
+ base.props = {
475
+ src: str(props.src),
476
+ alt: str(props.alt),
477
+ crop: normalizeCrop(props.crop),
478
+ shadow: bool(props.shadow),
479
+ };
234
480
  if (!base.props.src) return null;
235
481
  } else if (type === 'shape') {
236
482
  base.props = {
237
- kind: SHAPE_KINDS.some((k) => k.kind === props.kind) ? props.kind : 'rect',
238
- fill: str(props.fill) || '#e07b1f',
483
+ kind: SHAPE_KIND_KEYS.has(props.kind) ? props.kind : 'rect',
484
+ fill: str(props.fill),
239
485
  stroke: str(props.stroke),
240
- strokeW: num(props.strokeW),
486
+ strokeW: str(props.stroke) ? Math.max(0, num(props.strokeW)) : 0,
487
+ dash: DASH_KEYS.has(props.dash) ? props.dash : 'solid',
488
+ shadow: bool(props.shadow),
241
489
  text: str(props.text),
242
490
  textColor: str(props.textColor) || '#ffffff',
243
491
  font: str(props.font),
244
492
  fontSize: num(props.fontSize, 18),
245
- bold: !!props.bold,
493
+ bold: bool(props.bold),
494
+ italic: bool(props.italic),
495
+ underline: bool(props.underline),
496
+ strike: bool(props.strike),
497
+ align: ALIGNS.has(props.align) ? props.align : 'center',
498
+ anchor: ANCHORS.has(props.anchor) ? props.anchor : 'middle',
499
+ lineSpacing: Math.max(0.6, Math.min(4, num(props.lineSpacing, 1))),
246
500
  };
247
501
  } else if (type === 'line') {
502
+ const dash = props.dash === true ? 'dash' : props.dash;
248
503
  base.props = {
249
504
  stroke: str(props.stroke) || '#202124',
250
- strokeW: num(props.strokeW, 2),
251
- dash: !!props.dash,
252
- arrowEnd: !!props.arrowEnd,
505
+ strokeW: Math.max(1, num(props.strokeW, 2)),
506
+ dash: DASH_KEYS.has(dash) ? dash : 'solid',
507
+ arrowStart: bool(props.arrowStart),
508
+ arrowEnd: bool(props.arrowEnd),
253
509
  };
254
510
  } else {
255
- const rows = Array.isArray(props.rows) ? props.rows.map((row) => (Array.isArray(row) ? row.map((c) => str(c)) : [])).filter((r) => r.length) : [];
511
+ const rows = Array.isArray(props.rows)
512
+ ? props.rows.map((row) => (Array.isArray(row) ? row.map((c) => str(c)) : [])).filter((r) => r.length)
513
+ : [];
256
514
  if (!rows.length) return null;
515
+ let tableStyle = TABLE_STYLES.some((s) => s.key === props.tableStyle)
516
+ ? props.tableStyle
517
+ : (props.headerRow ? 'header' : 'plain');
518
+ let headerRow = bool(props.headerRow);
519
+ let bandedRows = bool(props.bandedRows);
520
+ // [设计] 样式与两个开关保持可往返的一致状态:标题行样式必须有表头,镶边/深色样式必须有镶边
521
+ if (tableStyle === 'header' && !headerRow) tableStyle = bandedRows ? 'banded' : 'plain';
522
+ if (tableStyle === 'banded' || tableStyle === 'dark') {
523
+ bandedRows = true;
524
+ if (tableStyle === 'dark') headerRow = true;
525
+ }
257
526
  base.props = {
258
527
  rows,
259
- headerRow: !!props.headerRow,
528
+ headerRow,
529
+ bandedRows,
530
+ tableStyle,
260
531
  font: str(props.font),
261
532
  fontSize: num(props.fontSize, 16),
262
533
  color: str(props.color),
@@ -277,6 +548,8 @@ function normalizeSlide(raw) {
277
548
  id: str(raw?.id) || genId('s'),
278
549
  bg: str(raw?.bg),
279
550
  notes: str(raw?.notes),
551
+ hidden: bool(raw?.hidden),
552
+ transition: normalizeTransition(raw?.transition),
280
553
  objects,
281
554
  };
282
555
  }
@@ -288,58 +561,91 @@ export function normalizeSnapshot(raw) {
288
561
  version: SNAPSHOT_VERSION,
289
562
  title: str(raw.title),
290
563
  theme: THEMES[raw.theme] ? raw.theme : 'clean',
564
+ size: normalizeSize(raw.size),
291
565
  slides: raw.slides.map(normalizeSlide),
292
566
  };
293
567
  }
294
568
 
295
- /** 新建页版式预设(「插入 → 新建页(版式)」条目源;文字色一律留空走主题兜底) */
569
+ /* ---------- 版式 ---------- */
570
+
571
+ /** 新建页版式预设(「新建幻灯片」版式源;位置以默认 960×540 的比例定义,换画幅自动缩放) */
296
572
  export const SLIDE_LAYOUTS = [
297
- { key: 'title', label: '标题页' },
298
- { key: 'titleOnly', label: '仅标题' },
299
- { key: 'titleContent', label: '标题与内容' },
573
+ { key: 'title', label: '标题幻灯片' },
574
+ { key: 'titleContent', label: '标题和内容' },
575
+ { key: 'section', label: '节标题' },
300
576
  { key: 'twoContent', label: '两栏内容' },
577
+ { key: 'titleOnly', label: '仅标题' },
578
+ { key: 'blank', label: '空白' },
301
579
  { key: 'caption', label: '底部题注' },
302
580
  ];
303
581
 
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;
582
+ const LAYOUT_SPECS = {
583
+ title: [
584
+ { x: 0.08, y: 0.33, w: 0.84, h: 0.17, size: 44, align: 'center', html: '单击输入标题' },
585
+ { x: 0.18, y: 0.54, w: 0.64, h: 0.1, size: 20, align: 'center', html: '单击输入副标题' },
586
+ ],
587
+ titleContent: [
588
+ { x: 0.05, y: 0.055, w: 0.9, h: 0.12, size: 32, align: 'left', html: '单击输入标题' },
589
+ { x: 0.05, y: 0.22, w: 0.9, h: 0.68, size: 22, align: 'left', html: '单击输入正文' },
590
+ ],
591
+ section: [
592
+ { x: 0.08, y: 0.3, w: 0.84, h: 0.17, size: 40, align: 'left', html: '单击输入节标题' },
593
+ { x: 0.08, y: 0.49, w: 0.84, h: 0.09, size: 18, align: 'left', html: '单击输入副标题' },
594
+ ],
595
+ twoContent: [
596
+ { x: 0.05, y: 0.055, w: 0.9, h: 0.12, size: 32, align: 'left', html: '单击输入标题' },
597
+ { x: 0.05, y: 0.22, w: 0.43, h: 0.68, size: 20, align: 'left', html: '左侧内容' },
598
+ { x: 0.52, y: 0.22, w: 0.43, h: 0.68, size: 20, align: 'left', html: '右侧内容' },
599
+ ],
600
+ titleOnly: [{ x: 0.05, y: 0.07, w: 0.9, h: 0.14, size: 34, align: 'left', html: '单击输入标题' }],
601
+ blank: [],
602
+ caption: [{ x: 0.08, y: 0.78, w: 0.84, h: 0.12, size: 20, align: 'center', html: '单击输入题注' }],
312
603
  };
313
604
 
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
- };
605
+ /** 按版式建页(未知 key 与空白页同义);坐标/字号随画幅缩放 */
606
+ export function createSlideWithLayout(key, size = DEFAULT_SIZE) {
607
+ const preset = normalizeSize(size);
333
608
  const slide = createSlide();
334
- const objects = presets[key];
335
- if (objects) slide.objects = objects.map((o, i) => ({ ...o, z: i + 1 }));
609
+ const spec = LAYOUT_SPECS[key];
610
+ if (!spec) return slide;
611
+ const sx = preset.w / SLIDE_W;
612
+ const sy = preset.h / SLIDE_H;
613
+ slide.objects = spec.map((t, i) => {
614
+ const o = createTextObject(Math.round(t.x * preset.w), Math.round(t.y * preset.h), i + 1);
615
+ o.w = Math.round(t.w * preset.w);
616
+ o.h = Math.round(t.h * preset.h);
617
+ o.props.html = t.html;
618
+ o.props.fontSize = Math.max(10, Math.round(t.size * Math.min(sx, sy)));
619
+ o.props.align = t.align;
620
+ return o;
621
+ });
336
622
  return slide;
337
623
  }
338
624
 
625
+ /** 换画幅:只拉伸几何,字号/线宽保持不变(与 Office「确保合适」的文字口径一致,也避免与 run 内联字号打架) */
626
+ export function scaleSnapshotSize(snapshot, nextSize) {
627
+ const from = sizeOf(snapshot);
628
+ const to = normalizeSize(nextSize);
629
+ if (from.w === to.w && from.h === to.h) return snapshot;
630
+ const sx = to.w / from.w;
631
+ const sy = to.h / from.h;
632
+ for (const slide of snapshot.slides) {
633
+ for (const o of slide.objects) {
634
+ o.x = Math.round(o.x * sx);
635
+ o.y = Math.round(o.y * sy);
636
+ o.w = Math.round(o.w * sx);
637
+ o.h = Math.round(o.h * sy);
638
+ }
639
+ }
640
+ snapshot.size = to;
641
+ return snapshot;
642
+ }
643
+
339
644
  /** 空稿:一页空白演示,新建文档与损坏快照共用 */
340
- export const createDraft = () => ({
645
+ export const createDraft = (size = DEFAULT_SIZE) => ({
341
646
  version: SNAPSHOT_VERSION,
342
647
  title: '',
343
648
  theme: 'clean',
649
+ size: normalizeSize(size),
344
650
  slides: [createSlide()],
345
651
  });