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