gbpptx 0.5.2 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/package.json +47 -47
- package/src/index.js +29 -29
- package/src/kernel/model.js +651 -651
- package/src/kernel/parts.js +173 -173
- package/src/kernel/pptx.js +72 -72
- package/src/kernel/print.js +76 -76
- package/src/kernel/read.js +1018 -1018
- package/src/kernel/roundtrip.js +174 -174
- package/src/kernel/text.js +278 -278
- package/src/kernel/write.js +385 -385
- package/web/app/slides/file.js +29 -2
- package/web/app/slides-app.js +25 -1
- package/web/app/ui/icons.js +2 -0
- package/web/app/ui/titlebar.js +253 -263
- package/web/index.html +3 -2
- package/web/serve.mjs +3 -2
- package/web/styles/slides.css +1 -1
package/src/kernel/model.js
CHANGED
|
@@ -1,651 +1,651 @@
|
|
|
1
|
-
// [文件] gbpptx·画布对象模型 —— 逻辑画布常量 + 主题/画幅 + 图形路径 + 对象/页工厂与快照归一
|
|
2
|
-
|
|
3
|
-
import { escapeHtml, genId } from 'guabiutils';
|
|
4
|
-
|
|
5
|
-
/** 快照结构版本:2 起带画幅/切换/隐藏页与更完整的对象字段 */
|
|
6
|
-
export const SNAPSHOT_VERSION = 2;
|
|
7
|
-
|
|
8
|
-
/** 默认逻辑画布尺寸(16:9),也是 fallback 尺寸 */
|
|
9
|
-
export const SLIDE_W = 960;
|
|
10
|
-
export const SLIDE_H = 540;
|
|
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
|
-
|
|
20
|
-
/** 主题:底色/前景/强调色(pptx 不支持渐变故一律纯色;与 pptx 主题部件的 clrScheme 由同一张表推导) */
|
|
21
|
-
export const THEMES = {
|
|
22
|
-
clean: { label: '简洁', bg: '#ffffff', text: '#202124', accent: '#e07b1f' },
|
|
23
|
-
midnight: { label: '夜幕', bg: '#232a36', text: '#e8eaed', accent: '#4c9be8' },
|
|
24
|
-
ocean: { label: '海滨', bg: '#0f4c75', text: '#f4faff', accent: '#bbe1fa' },
|
|
25
|
-
sunset: { label: '晚霞', bg: '#c0392b', text: '#fdf2ec', accent: '#f8c471' },
|
|
26
|
-
forest: { label: '林间', bg: '#0f3d33', text: '#eafaf1', accent: '#7dcea0' },
|
|
27
|
-
paper: { label: '纸感', bg: '#f6f1e5', text: '#3d3a33', accent: '#8e44ad' },
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export const THEME_KEYS = Object.keys(THEMES);
|
|
31
|
-
export const themeOf = (key) => THEMES[key] || THEMES.clean;
|
|
32
|
-
|
|
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 预设名) */
|
|
133
|
-
export const SHAPE_KINDS = [
|
|
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' },
|
|
152
|
-
];
|
|
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
|
-
|
|
158
|
-
/** 对象类型中文名(属性面板标题用) */
|
|
159
|
-
export const TYPE_NAMES = {
|
|
160
|
-
text: '文本框',
|
|
161
|
-
image: '图片',
|
|
162
|
-
shape: '形状',
|
|
163
|
-
line: '线条',
|
|
164
|
-
table: '表格',
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
/** 复制对象(props 深一层拷贝,rows/crop 单独断开) */
|
|
168
|
-
export const cloneObject = (o) => ({
|
|
169
|
-
...o,
|
|
170
|
-
id: genId('o'),
|
|
171
|
-
props: {
|
|
172
|
-
...o.props,
|
|
173
|
-
rows: o.props.rows?.map((row) => [...row]),
|
|
174
|
-
crop: o.props.crop ? { ...o.props.crop } : undefined,
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
/** 复制页面 */
|
|
179
|
-
export const copySlide = (slide) => ({
|
|
180
|
-
...slide,
|
|
181
|
-
id: genId('s'),
|
|
182
|
-
transition: { ...normalizeTransition(slide.transition) },
|
|
183
|
-
objects: slide.objects.map(cloneObject),
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
/** 空页 */
|
|
187
|
-
export const createSlide = () => ({
|
|
188
|
-
id: genId('s'),
|
|
189
|
-
bg: '',
|
|
190
|
-
notes: '',
|
|
191
|
-
hidden: false,
|
|
192
|
-
transition: { ...DEFAULT_TRANSITION },
|
|
193
|
-
objects: [],
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
/** 下一个层级号(追加对象/置顶都按它) */
|
|
197
|
-
export const topZ = (objects) => objects.reduce((max, o) => Math.max(max, o.z || 0), 0);
|
|
198
|
-
|
|
199
|
-
export const createTextObject = (x, y, z) => ({
|
|
200
|
-
id: genId('o'), type: 'text', x, y, w: 360, h: 110, rot: 0, z,
|
|
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
|
-
},
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
export const createImageObject = (src, x, y, w, h, z) => ({
|
|
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 },
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
export const createShapeObject = (kind, x, y, z) => ({
|
|
213
|
-
id: genId('o'), type: 'shape', x, y, w: 220, h: 140, rot: 0, z,
|
|
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
|
-
},
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
export const createLineObject = (x, y, z, arrowEnd = false) => ({
|
|
223
|
-
id: genId('o'), type: 'line', x, y, w: 240, h: 0, rot: 0, z,
|
|
224
|
-
props: { stroke: '#202124', strokeW: 2, dash: 'solid', arrowStart: false, arrowEnd },
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
export const createTableObject = (x, y, z, rows = 3, cols = 3) => ({
|
|
228
|
-
id: genId('o'), type: 'table', x, y, w: 440, h: 200, rot: 0, z,
|
|
229
|
-
props: {
|
|
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: [],
|
|
233
|
-
},
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 对象外接矩形(线条的 w/h 是到终点的向量、可为负,故取绝对值并回推左上角)。
|
|
238
|
-
* @returns {{left:number,top:number,width:number,height:number}}
|
|
239
|
-
*/
|
|
240
|
-
export const objectRect = (o) => ({
|
|
241
|
-
left: Math.min(o.x, o.x + o.w),
|
|
242
|
-
top: Math.min(o.y, o.y + o.h),
|
|
243
|
-
width: Math.abs(o.w),
|
|
244
|
-
height: Math.abs(o.h),
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
/** 外接矩形 → 绝对定位样式(旋转只发生在这一处 transform 上) */
|
|
248
|
-
export const objectBoxStyle = (o) => {
|
|
249
|
-
const r = objectRect(o);
|
|
250
|
-
return {
|
|
251
|
-
left: `${r.left}px`,
|
|
252
|
-
top: `${r.top}px`,
|
|
253
|
-
width: `${r.width}px`,
|
|
254
|
-
height: `${r.height}px`,
|
|
255
|
-
...(o.rot ? { transform: `rotate(${o.rot}deg)` } : {}),
|
|
256
|
-
};
|
|
257
|
-
};
|
|
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
|
-
|
|
281
|
-
/**
|
|
282
|
-
* 图形路径(按对象实际像素尺寸生成,故描边与箭头不会因缩放变形)。
|
|
283
|
-
* [坑] 不能用 viewBox + preserveAspectRatio="none" 拉伸:非等比缩放会把描边和箭头拉歪。
|
|
284
|
-
*/
|
|
285
|
-
export function shapePath(kind, w, h) {
|
|
286
|
-
w = Math.max(1, w);
|
|
287
|
-
h = Math.max(1, h);
|
|
288
|
-
switch (kind) {
|
|
289
|
-
case 'ellipse': {
|
|
290
|
-
const rx = w / 2;
|
|
291
|
-
const ry = h / 2;
|
|
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`;
|
|
293
|
-
}
|
|
294
|
-
case 'round': {
|
|
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`;
|
|
297
|
-
}
|
|
298
|
-
case 'triangle':
|
|
299
|
-
return polygon([[w / 2, 0], [w, h], [0, h]]);
|
|
300
|
-
case 'rightTriangle':
|
|
301
|
-
return polygon([[0, 0], [0, h], [w, h]]);
|
|
302
|
-
case 'diamond':
|
|
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
|
-
}
|
|
322
|
-
case 'star': {
|
|
323
|
-
let d = '';
|
|
324
|
-
for (let i = 0; i < 10; i += 1) {
|
|
325
|
-
const r = i % 2 === 0 ? 1 : 0.42;
|
|
326
|
-
const a = -Math.PI / 2 + (i * Math.PI) / 5;
|
|
327
|
-
d += `${i ? 'L' : 'M'}${n2(w / 2 + (w / 2) * r * Math.cos(a))},${n2(h / 2 + (h / 2) * r * Math.sin(a))}`;
|
|
328
|
-
}
|
|
329
|
-
return `${d}Z`;
|
|
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
|
-
}
|
|
356
|
-
default:
|
|
357
|
-
return `M0,0H${n2(w)}V${n2(h)}H0Z`;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
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
|
-
/** 正文是否为空(无文字且不含自带视觉的元素) */
|
|
375
|
-
export function isBlankHtml(html) {
|
|
376
|
-
return !html || (!html.replace(/<[^>]*>/g, '').trim() && !/<(img|hr|table)\b/i.test(html));
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/** HTML → 纯文本行(图形内文字存纯文本,\n 分行;列表保留标记) */
|
|
380
|
-
export const htmlToPlain = (html) =>
|
|
381
|
-
(html || '')
|
|
382
|
-
.replace(/<br\s*\/?>/gi, '\n')
|
|
383
|
-
.replace(/<\/(div|p|li)>/gi, '\n')
|
|
384
|
-
.replace(/<[^>]*>/g, '')
|
|
385
|
-
.replace(/ /g, ' ')
|
|
386
|
-
.replace(/</g, '<')
|
|
387
|
-
.replace(/>/g, '>')
|
|
388
|
-
.replace(/&/g, '&')
|
|
389
|
-
.replace(/\n+$/, '');
|
|
390
|
-
|
|
391
|
-
/** 纯文本行 → 单行 HTML(图形内文字回灌给就地编辑器) */
|
|
392
|
-
export const plainToHtml = (text) =>
|
|
393
|
-
(text || '').split('\n').map(escapeHtml).join('<br>');
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* 就地剥掉一个 DOM 子树里的内联字号(活 DOM 与 DOMParser 文档共用同一份实现)。
|
|
397
|
-
* [坑] 内联 font-size 优先级高于容器字号:对象级字号改动只改容器,正文里残留的内联字号会把文字
|
|
398
|
-
* 体积锁死——「设置字号不生效」的根因,故改对象字号必须同步走这里。
|
|
399
|
-
*/
|
|
400
|
-
export function clearInlineFontSizes(root) {
|
|
401
|
-
root?.querySelectorAll('*').forEach((el) => {
|
|
402
|
-
if (el.style?.fontSize) el.style.removeProperty('font-size');
|
|
403
|
-
if (el.tagName === 'FONT') el.removeAttribute('size');
|
|
404
|
-
if ((el.tagName === 'SPAN' || el.tagName === 'FONT') && !el.attributes.length) el.replaceWith(...el.childNodes);
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/** HTML 片段版的内联字号清理(模型数据侧入口) */
|
|
409
|
-
function stripInlineFontSize(html) {
|
|
410
|
-
if (!html) return html || '';
|
|
411
|
-
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, 'text/html');
|
|
412
|
-
const box = doc.body.firstElementChild;
|
|
413
|
-
clearInlineFontSizes(box);
|
|
414
|
-
return box?.innerHTML || '';
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
/** 对象级字号唯一入口:整框统一,改字号同时清掉正文内联字号(pptx 往返也只认「字号随对象走」) */
|
|
418
|
-
export function setObjectFontSize(obj, px) {
|
|
419
|
-
obj.props.fontSize = px;
|
|
420
|
-
if (obj.type === 'text' && obj.props.html) obj.props.html = stripInlineFontSize(obj.props.html);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
/* ---------- 归一 ---------- */
|
|
424
|
-
|
|
425
|
-
const num = (v, fallback = 0) => (typeof v === 'number' && Number.isFinite(v) ? v : fallback);
|
|
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
|
-
};
|
|
439
|
-
|
|
440
|
-
/** 归一单个对象:类型白名单收口、坐标补默认值、props 按类型只留有效字段 */
|
|
441
|
-
function normalizeObject(raw, z) {
|
|
442
|
-
const type = str(raw?.type);
|
|
443
|
-
if (!TYPE_NAMES[type]) return null;
|
|
444
|
-
const props = raw?.props && typeof raw.props === 'object' ? raw.props : {};
|
|
445
|
-
const base = {
|
|
446
|
-
id: str(raw?.id) || genId('o'),
|
|
447
|
-
type,
|
|
448
|
-
x: num(raw?.x),
|
|
449
|
-
y: num(raw?.y),
|
|
450
|
-
w: num(raw?.w),
|
|
451
|
-
h: num(raw?.h),
|
|
452
|
-
rot: num(raw?.rot),
|
|
453
|
-
z,
|
|
454
|
-
props: {},
|
|
455
|
-
};
|
|
456
|
-
if (type === 'text') {
|
|
457
|
-
base.props = {
|
|
458
|
-
html: str(props.html),
|
|
459
|
-
font: str(props.font),
|
|
460
|
-
fontSize: num(props.fontSize, 24),
|
|
461
|
-
color: str(props.color),
|
|
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,
|
|
472
|
-
};
|
|
473
|
-
} else if (type === 'image') {
|
|
474
|
-
base.props = {
|
|
475
|
-
src: str(props.src),
|
|
476
|
-
alt: str(props.alt),
|
|
477
|
-
crop: normalizeCrop(props.crop),
|
|
478
|
-
shadow: bool(props.shadow),
|
|
479
|
-
};
|
|
480
|
-
if (!base.props.src) return null;
|
|
481
|
-
} else if (type === 'shape') {
|
|
482
|
-
base.props = {
|
|
483
|
-
kind: SHAPE_KIND_KEYS.has(props.kind) ? props.kind : 'rect',
|
|
484
|
-
fill: str(props.fill),
|
|
485
|
-
stroke: str(props.stroke),
|
|
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),
|
|
489
|
-
text: str(props.text),
|
|
490
|
-
textColor: str(props.textColor) || '#ffffff',
|
|
491
|
-
font: str(props.font),
|
|
492
|
-
fontSize: num(props.fontSize, 18),
|
|
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))),
|
|
500
|
-
};
|
|
501
|
-
} else if (type === 'line') {
|
|
502
|
-
const dash = props.dash === true ? 'dash' : props.dash;
|
|
503
|
-
base.props = {
|
|
504
|
-
stroke: str(props.stroke) || '#202124',
|
|
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),
|
|
509
|
-
};
|
|
510
|
-
} else {
|
|
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
|
-
: [];
|
|
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
|
-
}
|
|
526
|
-
base.props = {
|
|
527
|
-
rows,
|
|
528
|
-
headerRow,
|
|
529
|
-
bandedRows,
|
|
530
|
-
tableStyle,
|
|
531
|
-
font: str(props.font),
|
|
532
|
-
fontSize: num(props.fontSize, 16),
|
|
533
|
-
color: str(props.color),
|
|
534
|
-
colW: Array.isArray(props.colW) ? props.colW.map((v) => num(v)) : [],
|
|
535
|
-
rowH: Array.isArray(props.rowH) ? props.rowH.map((v) => num(v)) : [],
|
|
536
|
-
};
|
|
537
|
-
}
|
|
538
|
-
return base;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/** 归一单页:对象逐个归一并按 z 排序(z 缺失时按数组顺序补) */
|
|
542
|
-
function normalizeSlide(raw) {
|
|
543
|
-
const objects = (Array.isArray(raw?.objects) ? raw.objects : [])
|
|
544
|
-
.map((o, i) => normalizeObject(o, typeof o?.z === 'number' ? o.z : i + 1))
|
|
545
|
-
.filter(Boolean)
|
|
546
|
-
.sort((a, b) => a.z - b.z);
|
|
547
|
-
return {
|
|
548
|
-
id: str(raw?.id) || genId('s'),
|
|
549
|
-
bg: str(raw?.bg),
|
|
550
|
-
notes: str(raw?.notes),
|
|
551
|
-
hidden: bool(raw?.hidden),
|
|
552
|
-
transition: normalizeTransition(raw?.transition),
|
|
553
|
-
objects,
|
|
554
|
-
};
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
/** 快照归一:slides 非数组一律返回 null,由调用方按空稿打开(空稿不置脏) */
|
|
558
|
-
export function normalizeSnapshot(raw) {
|
|
559
|
-
if (!raw || !Array.isArray(raw.slides)) return null;
|
|
560
|
-
return {
|
|
561
|
-
version: SNAPSHOT_VERSION,
|
|
562
|
-
title: str(raw.title),
|
|
563
|
-
theme: THEMES[raw.theme] ? raw.theme : 'clean',
|
|
564
|
-
size: normalizeSize(raw.size),
|
|
565
|
-
slides: raw.slides.map(normalizeSlide),
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
/* ---------- 版式 ---------- */
|
|
570
|
-
|
|
571
|
-
/** 新建页版式预设(「新建幻灯片」版式源;位置以默认 960×540 的比例定义,换画幅自动缩放) */
|
|
572
|
-
export const SLIDE_LAYOUTS = [
|
|
573
|
-
{ key: 'title', label: '标题幻灯片' },
|
|
574
|
-
{ key: 'titleContent', label: '标题和内容' },
|
|
575
|
-
{ key: 'section', label: '节标题' },
|
|
576
|
-
{ key: 'twoContent', label: '两栏内容' },
|
|
577
|
-
{ key: 'titleOnly', label: '仅标题' },
|
|
578
|
-
{ key: 'blank', label: '空白' },
|
|
579
|
-
{ key: 'caption', label: '底部题注' },
|
|
580
|
-
];
|
|
581
|
-
|
|
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: '单击输入题注' }],
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
/** 按版式建页(未知 key 与空白页同义);坐标/字号随画幅缩放 */
|
|
606
|
-
export function createSlideWithLayout(key, size = DEFAULT_SIZE) {
|
|
607
|
-
const preset = normalizeSize(size);
|
|
608
|
-
const slide = createSlide();
|
|
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
|
-
});
|
|
622
|
-
return slide;
|
|
623
|
-
}
|
|
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
|
-
|
|
644
|
-
/** 空稿:一页空白演示,新建文档与损坏快照共用 */
|
|
645
|
-
export const createDraft = (size = DEFAULT_SIZE) => ({
|
|
646
|
-
version: SNAPSHOT_VERSION,
|
|
647
|
-
title: '',
|
|
648
|
-
theme: 'clean',
|
|
649
|
-
size: normalizeSize(size),
|
|
650
|
-
slides: [createSlide()],
|
|
651
|
-
});
|
|
1
|
+
// [文件] gbpptx·画布对象模型 —— 逻辑画布常量 + 主题/画幅 + 图形路径 + 对象/页工厂与快照归一
|
|
2
|
+
|
|
3
|
+
import { escapeHtml, genId } from 'guabiutils';
|
|
4
|
+
|
|
5
|
+
/** 快照结构版本:2 起带画幅/切换/隐藏页与更完整的对象字段 */
|
|
6
|
+
export const SNAPSHOT_VERSION = 2;
|
|
7
|
+
|
|
8
|
+
/** 默认逻辑画布尺寸(16:9),也是 fallback 尺寸 */
|
|
9
|
+
export const SLIDE_W = 960;
|
|
10
|
+
export const SLIDE_H = 540;
|
|
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
|
+
|
|
20
|
+
/** 主题:底色/前景/强调色(pptx 不支持渐变故一律纯色;与 pptx 主题部件的 clrScheme 由同一张表推导) */
|
|
21
|
+
export const THEMES = {
|
|
22
|
+
clean: { label: '简洁', bg: '#ffffff', text: '#202124', accent: '#e07b1f' },
|
|
23
|
+
midnight: { label: '夜幕', bg: '#232a36', text: '#e8eaed', accent: '#4c9be8' },
|
|
24
|
+
ocean: { label: '海滨', bg: '#0f4c75', text: '#f4faff', accent: '#bbe1fa' },
|
|
25
|
+
sunset: { label: '晚霞', bg: '#c0392b', text: '#fdf2ec', accent: '#f8c471' },
|
|
26
|
+
forest: { label: '林间', bg: '#0f3d33', text: '#eafaf1', accent: '#7dcea0' },
|
|
27
|
+
paper: { label: '纸感', bg: '#f6f1e5', text: '#3d3a33', accent: '#8e44ad' },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const THEME_KEYS = Object.keys(THEMES);
|
|
31
|
+
export const themeOf = (key) => THEMES[key] || THEMES.clean;
|
|
32
|
+
|
|
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 预设名) */
|
|
133
|
+
export const SHAPE_KINDS = [
|
|
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' },
|
|
152
|
+
];
|
|
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
|
+
|
|
158
|
+
/** 对象类型中文名(属性面板标题用) */
|
|
159
|
+
export const TYPE_NAMES = {
|
|
160
|
+
text: '文本框',
|
|
161
|
+
image: '图片',
|
|
162
|
+
shape: '形状',
|
|
163
|
+
line: '线条',
|
|
164
|
+
table: '表格',
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/** 复制对象(props 深一层拷贝,rows/crop 单独断开) */
|
|
168
|
+
export const cloneObject = (o) => ({
|
|
169
|
+
...o,
|
|
170
|
+
id: genId('o'),
|
|
171
|
+
props: {
|
|
172
|
+
...o.props,
|
|
173
|
+
rows: o.props.rows?.map((row) => [...row]),
|
|
174
|
+
crop: o.props.crop ? { ...o.props.crop } : undefined,
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
/** 复制页面 */
|
|
179
|
+
export const copySlide = (slide) => ({
|
|
180
|
+
...slide,
|
|
181
|
+
id: genId('s'),
|
|
182
|
+
transition: { ...normalizeTransition(slide.transition) },
|
|
183
|
+
objects: slide.objects.map(cloneObject),
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
/** 空页 */
|
|
187
|
+
export const createSlide = () => ({
|
|
188
|
+
id: genId('s'),
|
|
189
|
+
bg: '',
|
|
190
|
+
notes: '',
|
|
191
|
+
hidden: false,
|
|
192
|
+
transition: { ...DEFAULT_TRANSITION },
|
|
193
|
+
objects: [],
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
/** 下一个层级号(追加对象/置顶都按它) */
|
|
197
|
+
export const topZ = (objects) => objects.reduce((max, o) => Math.max(max, o.z || 0), 0);
|
|
198
|
+
|
|
199
|
+
export const createTextObject = (x, y, z) => ({
|
|
200
|
+
id: genId('o'), type: 'text', x, y, w: 360, h: 110, rot: 0, z,
|
|
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
|
+
},
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
export const createImageObject = (src, x, y, w, h, z) => ({
|
|
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 },
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
export const createShapeObject = (kind, x, y, z) => ({
|
|
213
|
+
id: genId('o'), type: 'shape', x, y, w: 220, h: 140, rot: 0, z,
|
|
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
|
+
},
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
export const createLineObject = (x, y, z, arrowEnd = false) => ({
|
|
223
|
+
id: genId('o'), type: 'line', x, y, w: 240, h: 0, rot: 0, z,
|
|
224
|
+
props: { stroke: '#202124', strokeW: 2, dash: 'solid', arrowStart: false, arrowEnd },
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
export const createTableObject = (x, y, z, rows = 3, cols = 3) => ({
|
|
228
|
+
id: genId('o'), type: 'table', x, y, w: 440, h: 200, rot: 0, z,
|
|
229
|
+
props: {
|
|
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: [],
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 对象外接矩形(线条的 w/h 是到终点的向量、可为负,故取绝对值并回推左上角)。
|
|
238
|
+
* @returns {{left:number,top:number,width:number,height:number}}
|
|
239
|
+
*/
|
|
240
|
+
export const objectRect = (o) => ({
|
|
241
|
+
left: Math.min(o.x, o.x + o.w),
|
|
242
|
+
top: Math.min(o.y, o.y + o.h),
|
|
243
|
+
width: Math.abs(o.w),
|
|
244
|
+
height: Math.abs(o.h),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/** 外接矩形 → 绝对定位样式(旋转只发生在这一处 transform 上) */
|
|
248
|
+
export const objectBoxStyle = (o) => {
|
|
249
|
+
const r = objectRect(o);
|
|
250
|
+
return {
|
|
251
|
+
left: `${r.left}px`,
|
|
252
|
+
top: `${r.top}px`,
|
|
253
|
+
width: `${r.width}px`,
|
|
254
|
+
height: `${r.height}px`,
|
|
255
|
+
...(o.rot ? { transform: `rotate(${o.rot}deg)` } : {}),
|
|
256
|
+
};
|
|
257
|
+
};
|
|
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
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 图形路径(按对象实际像素尺寸生成,故描边与箭头不会因缩放变形)。
|
|
283
|
+
* [坑] 不能用 viewBox + preserveAspectRatio="none" 拉伸:非等比缩放会把描边和箭头拉歪。
|
|
284
|
+
*/
|
|
285
|
+
export function shapePath(kind, w, h) {
|
|
286
|
+
w = Math.max(1, w);
|
|
287
|
+
h = Math.max(1, h);
|
|
288
|
+
switch (kind) {
|
|
289
|
+
case 'ellipse': {
|
|
290
|
+
const rx = w / 2;
|
|
291
|
+
const ry = h / 2;
|
|
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`;
|
|
293
|
+
}
|
|
294
|
+
case 'round': {
|
|
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`;
|
|
297
|
+
}
|
|
298
|
+
case 'triangle':
|
|
299
|
+
return polygon([[w / 2, 0], [w, h], [0, h]]);
|
|
300
|
+
case 'rightTriangle':
|
|
301
|
+
return polygon([[0, 0], [0, h], [w, h]]);
|
|
302
|
+
case 'diamond':
|
|
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
|
+
}
|
|
322
|
+
case 'star': {
|
|
323
|
+
let d = '';
|
|
324
|
+
for (let i = 0; i < 10; i += 1) {
|
|
325
|
+
const r = i % 2 === 0 ? 1 : 0.42;
|
|
326
|
+
const a = -Math.PI / 2 + (i * Math.PI) / 5;
|
|
327
|
+
d += `${i ? 'L' : 'M'}${n2(w / 2 + (w / 2) * r * Math.cos(a))},${n2(h / 2 + (h / 2) * r * Math.sin(a))}`;
|
|
328
|
+
}
|
|
329
|
+
return `${d}Z`;
|
|
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
|
+
}
|
|
356
|
+
default:
|
|
357
|
+
return `M0,0H${n2(w)}V${n2(h)}H0Z`;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
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
|
+
/** 正文是否为空(无文字且不含自带视觉的元素) */
|
|
375
|
+
export function isBlankHtml(html) {
|
|
376
|
+
return !html || (!html.replace(/<[^>]*>/g, '').trim() && !/<(img|hr|table)\b/i.test(html));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** HTML → 纯文本行(图形内文字存纯文本,\n 分行;列表保留标记) */
|
|
380
|
+
export const htmlToPlain = (html) =>
|
|
381
|
+
(html || '')
|
|
382
|
+
.replace(/<br\s*\/?>/gi, '\n')
|
|
383
|
+
.replace(/<\/(div|p|li)>/gi, '\n')
|
|
384
|
+
.replace(/<[^>]*>/g, '')
|
|
385
|
+
.replace(/ /g, ' ')
|
|
386
|
+
.replace(/</g, '<')
|
|
387
|
+
.replace(/>/g, '>')
|
|
388
|
+
.replace(/&/g, '&')
|
|
389
|
+
.replace(/\n+$/, '');
|
|
390
|
+
|
|
391
|
+
/** 纯文本行 → 单行 HTML(图形内文字回灌给就地编辑器) */
|
|
392
|
+
export const plainToHtml = (text) =>
|
|
393
|
+
(text || '').split('\n').map(escapeHtml).join('<br>');
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* 就地剥掉一个 DOM 子树里的内联字号(活 DOM 与 DOMParser 文档共用同一份实现)。
|
|
397
|
+
* [坑] 内联 font-size 优先级高于容器字号:对象级字号改动只改容器,正文里残留的内联字号会把文字
|
|
398
|
+
* 体积锁死——「设置字号不生效」的根因,故改对象字号必须同步走这里。
|
|
399
|
+
*/
|
|
400
|
+
export function clearInlineFontSizes(root) {
|
|
401
|
+
root?.querySelectorAll('*').forEach((el) => {
|
|
402
|
+
if (el.style?.fontSize) el.style.removeProperty('font-size');
|
|
403
|
+
if (el.tagName === 'FONT') el.removeAttribute('size');
|
|
404
|
+
if ((el.tagName === 'SPAN' || el.tagName === 'FONT') && !el.attributes.length) el.replaceWith(...el.childNodes);
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** HTML 片段版的内联字号清理(模型数据侧入口) */
|
|
409
|
+
function stripInlineFontSize(html) {
|
|
410
|
+
if (!html) return html || '';
|
|
411
|
+
const doc = new DOMParser().parseFromString(`<div>${html}</div>`, 'text/html');
|
|
412
|
+
const box = doc.body.firstElementChild;
|
|
413
|
+
clearInlineFontSizes(box);
|
|
414
|
+
return box?.innerHTML || '';
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** 对象级字号唯一入口:整框统一,改字号同时清掉正文内联字号(pptx 往返也只认「字号随对象走」) */
|
|
418
|
+
export function setObjectFontSize(obj, px) {
|
|
419
|
+
obj.props.fontSize = px;
|
|
420
|
+
if (obj.type === 'text' && obj.props.html) obj.props.html = stripInlineFontSize(obj.props.html);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/* ---------- 归一 ---------- */
|
|
424
|
+
|
|
425
|
+
const num = (v, fallback = 0) => (typeof v === 'number' && Number.isFinite(v) ? v : fallback);
|
|
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
|
+
};
|
|
439
|
+
|
|
440
|
+
/** 归一单个对象:类型白名单收口、坐标补默认值、props 按类型只留有效字段 */
|
|
441
|
+
function normalizeObject(raw, z) {
|
|
442
|
+
const type = str(raw?.type);
|
|
443
|
+
if (!TYPE_NAMES[type]) return null;
|
|
444
|
+
const props = raw?.props && typeof raw.props === 'object' ? raw.props : {};
|
|
445
|
+
const base = {
|
|
446
|
+
id: str(raw?.id) || genId('o'),
|
|
447
|
+
type,
|
|
448
|
+
x: num(raw?.x),
|
|
449
|
+
y: num(raw?.y),
|
|
450
|
+
w: num(raw?.w),
|
|
451
|
+
h: num(raw?.h),
|
|
452
|
+
rot: num(raw?.rot),
|
|
453
|
+
z,
|
|
454
|
+
props: {},
|
|
455
|
+
};
|
|
456
|
+
if (type === 'text') {
|
|
457
|
+
base.props = {
|
|
458
|
+
html: str(props.html),
|
|
459
|
+
font: str(props.font),
|
|
460
|
+
fontSize: num(props.fontSize, 24),
|
|
461
|
+
color: str(props.color),
|
|
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,
|
|
472
|
+
};
|
|
473
|
+
} else if (type === 'image') {
|
|
474
|
+
base.props = {
|
|
475
|
+
src: str(props.src),
|
|
476
|
+
alt: str(props.alt),
|
|
477
|
+
crop: normalizeCrop(props.crop),
|
|
478
|
+
shadow: bool(props.shadow),
|
|
479
|
+
};
|
|
480
|
+
if (!base.props.src) return null;
|
|
481
|
+
} else if (type === 'shape') {
|
|
482
|
+
base.props = {
|
|
483
|
+
kind: SHAPE_KIND_KEYS.has(props.kind) ? props.kind : 'rect',
|
|
484
|
+
fill: str(props.fill),
|
|
485
|
+
stroke: str(props.stroke),
|
|
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),
|
|
489
|
+
text: str(props.text),
|
|
490
|
+
textColor: str(props.textColor) || '#ffffff',
|
|
491
|
+
font: str(props.font),
|
|
492
|
+
fontSize: num(props.fontSize, 18),
|
|
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))),
|
|
500
|
+
};
|
|
501
|
+
} else if (type === 'line') {
|
|
502
|
+
const dash = props.dash === true ? 'dash' : props.dash;
|
|
503
|
+
base.props = {
|
|
504
|
+
stroke: str(props.stroke) || '#202124',
|
|
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),
|
|
509
|
+
};
|
|
510
|
+
} else {
|
|
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
|
+
: [];
|
|
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
|
+
}
|
|
526
|
+
base.props = {
|
|
527
|
+
rows,
|
|
528
|
+
headerRow,
|
|
529
|
+
bandedRows,
|
|
530
|
+
tableStyle,
|
|
531
|
+
font: str(props.font),
|
|
532
|
+
fontSize: num(props.fontSize, 16),
|
|
533
|
+
color: str(props.color),
|
|
534
|
+
colW: Array.isArray(props.colW) ? props.colW.map((v) => num(v)) : [],
|
|
535
|
+
rowH: Array.isArray(props.rowH) ? props.rowH.map((v) => num(v)) : [],
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
return base;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** 归一单页:对象逐个归一并按 z 排序(z 缺失时按数组顺序补) */
|
|
542
|
+
function normalizeSlide(raw) {
|
|
543
|
+
const objects = (Array.isArray(raw?.objects) ? raw.objects : [])
|
|
544
|
+
.map((o, i) => normalizeObject(o, typeof o?.z === 'number' ? o.z : i + 1))
|
|
545
|
+
.filter(Boolean)
|
|
546
|
+
.sort((a, b) => a.z - b.z);
|
|
547
|
+
return {
|
|
548
|
+
id: str(raw?.id) || genId('s'),
|
|
549
|
+
bg: str(raw?.bg),
|
|
550
|
+
notes: str(raw?.notes),
|
|
551
|
+
hidden: bool(raw?.hidden),
|
|
552
|
+
transition: normalizeTransition(raw?.transition),
|
|
553
|
+
objects,
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** 快照归一:slides 非数组一律返回 null,由调用方按空稿打开(空稿不置脏) */
|
|
558
|
+
export function normalizeSnapshot(raw) {
|
|
559
|
+
if (!raw || !Array.isArray(raw.slides)) return null;
|
|
560
|
+
return {
|
|
561
|
+
version: SNAPSHOT_VERSION,
|
|
562
|
+
title: str(raw.title),
|
|
563
|
+
theme: THEMES[raw.theme] ? raw.theme : 'clean',
|
|
564
|
+
size: normalizeSize(raw.size),
|
|
565
|
+
slides: raw.slides.map(normalizeSlide),
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/* ---------- 版式 ---------- */
|
|
570
|
+
|
|
571
|
+
/** 新建页版式预设(「新建幻灯片」版式源;位置以默认 960×540 的比例定义,换画幅自动缩放) */
|
|
572
|
+
export const SLIDE_LAYOUTS = [
|
|
573
|
+
{ key: 'title', label: '标题幻灯片' },
|
|
574
|
+
{ key: 'titleContent', label: '标题和内容' },
|
|
575
|
+
{ key: 'section', label: '节标题' },
|
|
576
|
+
{ key: 'twoContent', label: '两栏内容' },
|
|
577
|
+
{ key: 'titleOnly', label: '仅标题' },
|
|
578
|
+
{ key: 'blank', label: '空白' },
|
|
579
|
+
{ key: 'caption', label: '底部题注' },
|
|
580
|
+
];
|
|
581
|
+
|
|
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: '单击输入题注' }],
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
/** 按版式建页(未知 key 与空白页同义);坐标/字号随画幅缩放 */
|
|
606
|
+
export function createSlideWithLayout(key, size = DEFAULT_SIZE) {
|
|
607
|
+
const preset = normalizeSize(size);
|
|
608
|
+
const slide = createSlide();
|
|
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
|
+
});
|
|
622
|
+
return slide;
|
|
623
|
+
}
|
|
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
|
+
|
|
644
|
+
/** 空稿:一页空白演示,新建文档与损坏快照共用 */
|
|
645
|
+
export const createDraft = (size = DEFAULT_SIZE) => ({
|
|
646
|
+
version: SNAPSHOT_VERSION,
|
|
647
|
+
title: '',
|
|
648
|
+
theme: 'clean',
|
|
649
|
+
size: normalizeSize(size),
|
|
650
|
+
slides: [createSlide()],
|
|
651
|
+
});
|