gbpptx 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +253 -253
- package/package.json +48 -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/main.js +8 -8
- package/web/app/slides/arrange.js +101 -101
- package/web/app/slides/clipboard.js +93 -93
- package/web/app/slides/file.js +240 -240
- package/web/app/slides/find.js +201 -201
- package/web/app/slides/history.js +100 -100
- package/web/app/slides/keys.js +187 -187
- package/web/app/slides/menus.js +105 -105
- package/web/app/slides/objects.js +254 -254
- package/web/app/slides/render.js +183 -183
- package/web/app/slides/shared.js +85 -85
- package/web/app/slides/slides.js +103 -103
- package/web/app/slides/table.js +102 -102
- package/web/app/slides/text.js +418 -418
- package/web/app/slides/view.js +182 -182
- package/web/app/slides-app.js +354 -354
- package/web/app/ui/canvas.js +769 -769
- package/web/app/ui/color-panel.js +58 -58
- package/web/app/ui/dialogs.js +120 -120
- package/web/app/ui/file-io.js +116 -116
- package/web/app/ui/format-pane.js +427 -427
- package/web/app/ui/icons.js +241 -133
- package/web/app/ui/menu.js +77 -75
- package/web/app/ui/notify.js +45 -45
- package/web/app/ui/page-view.js +221 -221
- package/web/app/ui/popup.js +80 -80
- package/web/app/ui/present.js +292 -292
- package/web/app/ui/ribbon.js +855 -855
- package/web/app/ui/slide-pane.js +197 -197
- package/web/app/ui/statusbar.js +93 -93
- package/web/app/ui/text-styles.js +84 -84
- package/web/app/ui/titlebar.js +253 -253
- package/web/app/ui/transition.js +30 -30
- package/web/index.html +30 -29
- package/web/serve.mjs +26 -23
- package/web/styles/office.css +463 -463
- package/web/styles/slides.css +97 -97
- package/web/styles/tokens.css +154 -154
package/web/app/ui/page-view.js
CHANGED
|
@@ -1,221 +1,221 @@
|
|
|
1
|
-
// [文件] gbpptx·页渲染 —— 一页幻灯片 → DOM(缩略图/画布/放映/打印共用同一坐标系与画幅)
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
DASH_STYLES, SLIDE_H, SLIDE_W, normalizeSize, objectBoxStyle, objectRect, shapePath, tableStyleColors, themeOf,
|
|
5
|
-
} from '../../../src/kernel/model.js';
|
|
6
|
-
import { sanitizeTextHtml } from '../../../src/kernel/text.js';
|
|
7
|
-
|
|
8
|
-
const svgNS = 'http://www.w3.org/2000/svg';
|
|
9
|
-
|
|
10
|
-
const esc = (s) => String(s ?? '');
|
|
11
|
-
|
|
12
|
-
/** 图形内文字(纯文本,\n 分行) */
|
|
13
|
-
const shapeTextHtml = (text) => esc(text).split('\n').map((line) => {
|
|
14
|
-
const safe = line.replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c]));
|
|
15
|
-
return `<div>${safe || '<br>'}</div>`;
|
|
16
|
-
}).join('');
|
|
17
|
-
|
|
18
|
-
const anchorFlex = (anchor, axis = 'column') => {
|
|
19
|
-
const justify = anchor === 'middle' ? 'center' : anchor === 'bottom' ? 'flex-end' : 'flex-start';
|
|
20
|
-
return axis === 'column' ? `display:flex;flex-direction:column;justify-content:${justify}` : `justify-content:${justify}`;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/** 一个对象的 DOM(static 与编辑两层共用;interactive=true 时挂 data-obj-id) */
|
|
24
|
-
function renderObject(o, theme, interactive) {
|
|
25
|
-
const p = o.props || {};
|
|
26
|
-
const el = document.createElement('div');
|
|
27
|
-
el.className = `sl-obj sl-obj--${o.type}${p.shadow ? ' has-shadow' : ''}`;
|
|
28
|
-
const box = objectBoxStyle(o);
|
|
29
|
-
if (o.type === 'line') {
|
|
30
|
-
// 轴向线的逻辑高度为 0,但 DOM 需要 1px 才能画箭头/选中
|
|
31
|
-
box.width = `${Math.max(1, parseFloat(box.width))}px`;
|
|
32
|
-
box.height = `${Math.max(1, parseFloat(box.height))}px`;
|
|
33
|
-
}
|
|
34
|
-
Object.assign(el.style, box);
|
|
35
|
-
if (interactive) el.dataset.objId = o.id;
|
|
36
|
-
el.dataset.objType = o.type;
|
|
37
|
-
|
|
38
|
-
if (o.type === 'text') {
|
|
39
|
-
if (p.fill) el.style.background = p.fill;
|
|
40
|
-
if (p.stroke && p.strokeW > 0) el.style.border = `${p.strokeW}px solid ${p.stroke}`;
|
|
41
|
-
const body = document.createElement('div');
|
|
42
|
-
body.className = 'sl-textbox';
|
|
43
|
-
body.style.cssText = `height:100%;${anchorFlex(p.anchor)}`;
|
|
44
|
-
const text = document.createElement('div');
|
|
45
|
-
text.className = 'sl-text';
|
|
46
|
-
text.innerHTML = sanitizeTextHtml(p.html || '');
|
|
47
|
-
Object.assign(text.style, {
|
|
48
|
-
fontSize: `${p.fontSize || 24}px`,
|
|
49
|
-
color: p.color || theme.text,
|
|
50
|
-
textAlign: p.align || 'left',
|
|
51
|
-
fontFamily: p.font || '',
|
|
52
|
-
fontWeight: p.bold ? '700' : '400',
|
|
53
|
-
fontStyle: p.italic ? 'italic' : 'normal',
|
|
54
|
-
textDecoration: [p.underline ? 'underline' : '', p.strike ? 'line-through' : ''].filter(Boolean).join(' ') || 'none',
|
|
55
|
-
lineHeight: String((p.lineSpacing || 1) * 1.2),
|
|
56
|
-
});
|
|
57
|
-
body.appendChild(text);
|
|
58
|
-
el.appendChild(body);
|
|
59
|
-
} else if (o.type === 'shape') {
|
|
60
|
-
const r = objectRect(o);
|
|
61
|
-
const w = Math.max(1, r.width);
|
|
62
|
-
const h = Math.max(1, r.height);
|
|
63
|
-
const svg = document.createElementNS(svgNS, 'svg');
|
|
64
|
-
svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
|
|
65
|
-
svg.setAttribute('preserveAspectRatio', 'none');
|
|
66
|
-
svg.classList.add('sl-shape-svg');
|
|
67
|
-
const path = document.createElementNS(svgNS, 'path');
|
|
68
|
-
path.setAttribute('d', shapePath(p.kind, w, h));
|
|
69
|
-
path.setAttribute('fill', p.fill || 'none');
|
|
70
|
-
if (p.stroke && p.strokeW > 0) {
|
|
71
|
-
path.setAttribute('stroke', p.stroke);
|
|
72
|
-
path.setAttribute('stroke-width', String(p.strokeW));
|
|
73
|
-
path.setAttribute('stroke-linejoin', 'round');
|
|
74
|
-
if (p.dash && p.dash !== 'solid') path.setAttribute('stroke-dasharray', DASH_STYLES[p.dash] || '');
|
|
75
|
-
}
|
|
76
|
-
svg.appendChild(path);
|
|
77
|
-
el.appendChild(svg);
|
|
78
|
-
const text = document.createElement('div');
|
|
79
|
-
text.className = 'sl-shape-text';
|
|
80
|
-
text.innerHTML = shapeTextHtml(p.text || '');
|
|
81
|
-
Object.assign(text.style, {
|
|
82
|
-
fontSize: `${p.fontSize || 18}px`,
|
|
83
|
-
color: p.textColor || theme.text,
|
|
84
|
-
fontFamily: p.font || '',
|
|
85
|
-
fontWeight: p.bold ? '700' : '400',
|
|
86
|
-
fontStyle: p.italic ? 'italic' : 'normal',
|
|
87
|
-
textDecoration: [p.underline ? 'underline' : '', p.strike ? 'line-through' : ''].filter(Boolean).join(' ') || 'none',
|
|
88
|
-
textAlign: p.align || 'center',
|
|
89
|
-
lineHeight: String((p.lineSpacing || 1) * 1.2),
|
|
90
|
-
justifyContent: p.anchor === 'bottom' ? 'flex-end' : p.anchor === 'middle' ? 'center' : 'flex-start',
|
|
91
|
-
});
|
|
92
|
-
el.appendChild(text);
|
|
93
|
-
} else if (o.type === 'line') {
|
|
94
|
-
const r = objectRect(o);
|
|
95
|
-
const w = Math.max(1, r.width);
|
|
96
|
-
const h = Math.max(1, r.height);
|
|
97
|
-
const svg = document.createElementNS(svgNS, 'svg');
|
|
98
|
-
svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
|
|
99
|
-
svg.setAttribute('preserveAspectRatio', 'none');
|
|
100
|
-
svg.classList.add('sl-line-svg');
|
|
101
|
-
const x1 = o.w < 0 ? w : 0;
|
|
102
|
-
const y1 = o.h < 0 ? h : 0;
|
|
103
|
-
const x2 = o.w < 0 ? 0 : w;
|
|
104
|
-
const y2 = o.h < 0 ? 0 : h;
|
|
105
|
-
const stroke = p.stroke || theme.text;
|
|
106
|
-
const width = p.strokeW > 0 ? p.strokeW : 2;
|
|
107
|
-
const line = document.createElementNS(svgNS, 'line');
|
|
108
|
-
line.setAttribute('x1', String(x1));
|
|
109
|
-
line.setAttribute('y1', String(y1));
|
|
110
|
-
line.setAttribute('x2', String(x2));
|
|
111
|
-
line.setAttribute('y2', String(y2));
|
|
112
|
-
line.setAttribute('stroke', stroke);
|
|
113
|
-
line.setAttribute('stroke-width', String(width));
|
|
114
|
-
if (p.dash && p.dash !== 'solid') line.setAttribute('stroke-dasharray', DASH_STYLES[p.dash] || '');
|
|
115
|
-
svg.appendChild(line);
|
|
116
|
-
const angle = Math.atan2(y2 - y1, x2 - x1);
|
|
117
|
-
const size = Math.max(9, width * 3.2 + 3);
|
|
118
|
-
if (p.arrowEnd) svg.appendChild(arrowHead(x2, y2, angle, size, stroke));
|
|
119
|
-
if (p.arrowStart) svg.appendChild(arrowHead(x1, y1, angle + Math.PI, size, stroke));
|
|
120
|
-
el.appendChild(svg);
|
|
121
|
-
} else if (o.type === 'image') {
|
|
122
|
-
const crop = p.crop || {};
|
|
123
|
-
const l = Math.max(0, Number(crop.l) || 0);
|
|
124
|
-
const t = Math.max(0, Number(crop.t) || 0);
|
|
125
|
-
const r = Math.max(0, Number(crop.r) || 0);
|
|
126
|
-
const b = Math.max(0, Number(crop.b) || 0);
|
|
127
|
-
const img = document.createElement('img');
|
|
128
|
-
img.src = p.src;
|
|
129
|
-
img.alt = p.alt || '';
|
|
130
|
-
img.draggable = false;
|
|
131
|
-
if (l || t || r || b) {
|
|
132
|
-
el.style.overflow = 'hidden';
|
|
133
|
-
const sx = 100 / Math.max(1, 100 - l - r);
|
|
134
|
-
const sy = 100 / Math.max(1, 100 - t - b);
|
|
135
|
-
Object.assign(img.style, {
|
|
136
|
-
position: 'absolute',
|
|
137
|
-
width: `${sx * 100}%`,
|
|
138
|
-
height: `${sy * 100}%`,
|
|
139
|
-
left: `${(-l * sx)}%`,
|
|
140
|
-
top: `${(-t * sy)}%`,
|
|
141
|
-
objectFit: 'fill',
|
|
142
|
-
});
|
|
143
|
-
} else {
|
|
144
|
-
Object.assign(img.style, { width: '100%', height: '100%', objectFit: 'fill' });
|
|
145
|
-
}
|
|
146
|
-
el.appendChild(img);
|
|
147
|
-
} else if (o.type === 'table') {
|
|
148
|
-
const rows = p.rows || [];
|
|
149
|
-
const cols = rows[0]?.length || 1;
|
|
150
|
-
const style = tableStyleColors(p.tableStyle, theme.key);
|
|
151
|
-
const table = document.createElement('table');
|
|
152
|
-
table.className = 'sl-table';
|
|
153
|
-
const cg = document.createElement('colgroup');
|
|
154
|
-
for (let c = 0; c < cols; c += 1) {
|
|
155
|
-
const col = document.createElement('col');
|
|
156
|
-
const pct = Array.isArray(p.colW) && p.colW[c] > 0 ? p.colW[c] : 100 / cols;
|
|
157
|
-
col.style.width = `${pct}%`;
|
|
158
|
-
cg.appendChild(col);
|
|
159
|
-
}
|
|
160
|
-
table.appendChild(cg);
|
|
161
|
-
const tbody = document.createElement('tbody');
|
|
162
|
-
rows.forEach((row, ri) => {
|
|
163
|
-
const tr = document.createElement('tr');
|
|
164
|
-
if (Array.isArray(p.rowH) && p.rowH[ri] > 0) tr.style.height = `${p.rowH[ri]}%`;
|
|
165
|
-
const header = p.headerRow && ri === 0;
|
|
166
|
-
const band = p.bandedRows && !header && (p.headerRow ? ri % 2 === 1 : ri % 2 === 0);
|
|
167
|
-
for (let c = 0; c < cols; c += 1) {
|
|
168
|
-
const td = document.createElement('td');
|
|
169
|
-
td.textContent = c < row.length ? row[c] : '';
|
|
170
|
-
td.style.fontSize = `${p.fontSize || 16}px`;
|
|
171
|
-
td.style.fontFamily = p.font || '';
|
|
172
|
-
td.style.color = header ? (style.headerText || theme.text) : (p.color || style.bodyText || theme.text);
|
|
173
|
-
td.style.fontWeight = header ? '700' : '400';
|
|
174
|
-
td.style.background = header ? (style.headerFill || 'transparent') : band ? (style.bandFill || 'transparent') : 'transparent';
|
|
175
|
-
td.style.border = `1px solid ${style.border}`;
|
|
176
|
-
tr.appendChild(td);
|
|
177
|
-
}
|
|
178
|
-
tbody.appendChild(tr);
|
|
179
|
-
});
|
|
180
|
-
table.appendChild(tbody);
|
|
181
|
-
el.appendChild(table);
|
|
182
|
-
}
|
|
183
|
-
return el;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/** 箭头三角形(按线宽缩放;不用 <marker>,避免缩略图与画布同 id 互相串引用) */
|
|
187
|
-
function arrowHead(x, y, angle, size, color) {
|
|
188
|
-
const poly = document.createElementNS(svgNS, 'polygon');
|
|
189
|
-
const half = size * 0.42;
|
|
190
|
-
const back = size * 0.9;
|
|
191
|
-
const pts = [
|
|
192
|
-
[x, y],
|
|
193
|
-
[x - Math.cos(angle) * back - Math.sin(angle) * half, y - Math.sin(angle) * back + Math.cos(angle) * half],
|
|
194
|
-
[x - Math.cos(angle) * back + Math.sin(angle) * half, y - Math.sin(angle) * back - Math.cos(angle) * half],
|
|
195
|
-
];
|
|
196
|
-
poly.setAttribute('points', pts.map(([px, py]) => `${px},${py}`).join(' '));
|
|
197
|
-
poly.setAttribute('fill', color);
|
|
198
|
-
return poly;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* 渲染一页幻灯片为 DOM(宽高恒为逻辑画幅;缩放由调用方 transform: scale 承担)
|
|
203
|
-
* @param {object} slide
|
|
204
|
-
* @param {string} theme 主题键
|
|
205
|
-
* @param {{interactive?:boolean,size?:{w:number,h:number}}} [opts]
|
|
206
|
-
* @returns {HTMLElement}
|
|
207
|
-
*/
|
|
208
|
-
export function renderPage(slide, theme, opts = {}) {
|
|
209
|
-
const key = typeof theme === 'string' ? theme : 'clean';
|
|
210
|
-
const t = { ...themeOf(key), key };
|
|
211
|
-
const size = normalizeSize(opts.size || { w: SLIDE_W, h: SLIDE_H });
|
|
212
|
-
const page = document.createElement('div');
|
|
213
|
-
page.className = 'sl-page';
|
|
214
|
-
page.style.width = `${size.w}px`;
|
|
215
|
-
page.style.height = `${size.h}px`;
|
|
216
|
-
page.style.background = slide.bg || t.bg;
|
|
217
|
-
page.style.color = t.text;
|
|
218
|
-
const objects = [...(slide.objects || [])].sort((a, b) => (a.z || 0) - (b.z || 0));
|
|
219
|
-
for (const o of objects) page.appendChild(renderObject(o, t, !!opts.interactive));
|
|
220
|
-
return page;
|
|
221
|
-
}
|
|
1
|
+
// [文件] gbpptx·页渲染 —— 一页幻灯片 → DOM(缩略图/画布/放映/打印共用同一坐标系与画幅)
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DASH_STYLES, SLIDE_H, SLIDE_W, normalizeSize, objectBoxStyle, objectRect, shapePath, tableStyleColors, themeOf,
|
|
5
|
+
} from '../../../src/kernel/model.js';
|
|
6
|
+
import { sanitizeTextHtml } from '../../../src/kernel/text.js';
|
|
7
|
+
|
|
8
|
+
const svgNS = 'http://www.w3.org/2000/svg';
|
|
9
|
+
|
|
10
|
+
const esc = (s) => String(s ?? '');
|
|
11
|
+
|
|
12
|
+
/** 图形内文字(纯文本,\n 分行) */
|
|
13
|
+
const shapeTextHtml = (text) => esc(text).split('\n').map((line) => {
|
|
14
|
+
const safe = line.replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c]));
|
|
15
|
+
return `<div>${safe || '<br>'}</div>`;
|
|
16
|
+
}).join('');
|
|
17
|
+
|
|
18
|
+
const anchorFlex = (anchor, axis = 'column') => {
|
|
19
|
+
const justify = anchor === 'middle' ? 'center' : anchor === 'bottom' ? 'flex-end' : 'flex-start';
|
|
20
|
+
return axis === 'column' ? `display:flex;flex-direction:column;justify-content:${justify}` : `justify-content:${justify}`;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** 一个对象的 DOM(static 与编辑两层共用;interactive=true 时挂 data-obj-id) */
|
|
24
|
+
function renderObject(o, theme, interactive) {
|
|
25
|
+
const p = o.props || {};
|
|
26
|
+
const el = document.createElement('div');
|
|
27
|
+
el.className = `sl-obj sl-obj--${o.type}${p.shadow ? ' has-shadow' : ''}`;
|
|
28
|
+
const box = objectBoxStyle(o);
|
|
29
|
+
if (o.type === 'line') {
|
|
30
|
+
// 轴向线的逻辑高度为 0,但 DOM 需要 1px 才能画箭头/选中
|
|
31
|
+
box.width = `${Math.max(1, parseFloat(box.width))}px`;
|
|
32
|
+
box.height = `${Math.max(1, parseFloat(box.height))}px`;
|
|
33
|
+
}
|
|
34
|
+
Object.assign(el.style, box);
|
|
35
|
+
if (interactive) el.dataset.objId = o.id;
|
|
36
|
+
el.dataset.objType = o.type;
|
|
37
|
+
|
|
38
|
+
if (o.type === 'text') {
|
|
39
|
+
if (p.fill) el.style.background = p.fill;
|
|
40
|
+
if (p.stroke && p.strokeW > 0) el.style.border = `${p.strokeW}px solid ${p.stroke}`;
|
|
41
|
+
const body = document.createElement('div');
|
|
42
|
+
body.className = 'sl-textbox';
|
|
43
|
+
body.style.cssText = `height:100%;${anchorFlex(p.anchor)}`;
|
|
44
|
+
const text = document.createElement('div');
|
|
45
|
+
text.className = 'sl-text';
|
|
46
|
+
text.innerHTML = sanitizeTextHtml(p.html || '');
|
|
47
|
+
Object.assign(text.style, {
|
|
48
|
+
fontSize: `${p.fontSize || 24}px`,
|
|
49
|
+
color: p.color || theme.text,
|
|
50
|
+
textAlign: p.align || 'left',
|
|
51
|
+
fontFamily: p.font || '',
|
|
52
|
+
fontWeight: p.bold ? '700' : '400',
|
|
53
|
+
fontStyle: p.italic ? 'italic' : 'normal',
|
|
54
|
+
textDecoration: [p.underline ? 'underline' : '', p.strike ? 'line-through' : ''].filter(Boolean).join(' ') || 'none',
|
|
55
|
+
lineHeight: String((p.lineSpacing || 1) * 1.2),
|
|
56
|
+
});
|
|
57
|
+
body.appendChild(text);
|
|
58
|
+
el.appendChild(body);
|
|
59
|
+
} else if (o.type === 'shape') {
|
|
60
|
+
const r = objectRect(o);
|
|
61
|
+
const w = Math.max(1, r.width);
|
|
62
|
+
const h = Math.max(1, r.height);
|
|
63
|
+
const svg = document.createElementNS(svgNS, 'svg');
|
|
64
|
+
svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
|
|
65
|
+
svg.setAttribute('preserveAspectRatio', 'none');
|
|
66
|
+
svg.classList.add('sl-shape-svg');
|
|
67
|
+
const path = document.createElementNS(svgNS, 'path');
|
|
68
|
+
path.setAttribute('d', shapePath(p.kind, w, h));
|
|
69
|
+
path.setAttribute('fill', p.fill || 'none');
|
|
70
|
+
if (p.stroke && p.strokeW > 0) {
|
|
71
|
+
path.setAttribute('stroke', p.stroke);
|
|
72
|
+
path.setAttribute('stroke-width', String(p.strokeW));
|
|
73
|
+
path.setAttribute('stroke-linejoin', 'round');
|
|
74
|
+
if (p.dash && p.dash !== 'solid') path.setAttribute('stroke-dasharray', DASH_STYLES[p.dash] || '');
|
|
75
|
+
}
|
|
76
|
+
svg.appendChild(path);
|
|
77
|
+
el.appendChild(svg);
|
|
78
|
+
const text = document.createElement('div');
|
|
79
|
+
text.className = 'sl-shape-text';
|
|
80
|
+
text.innerHTML = shapeTextHtml(p.text || '');
|
|
81
|
+
Object.assign(text.style, {
|
|
82
|
+
fontSize: `${p.fontSize || 18}px`,
|
|
83
|
+
color: p.textColor || theme.text,
|
|
84
|
+
fontFamily: p.font || '',
|
|
85
|
+
fontWeight: p.bold ? '700' : '400',
|
|
86
|
+
fontStyle: p.italic ? 'italic' : 'normal',
|
|
87
|
+
textDecoration: [p.underline ? 'underline' : '', p.strike ? 'line-through' : ''].filter(Boolean).join(' ') || 'none',
|
|
88
|
+
textAlign: p.align || 'center',
|
|
89
|
+
lineHeight: String((p.lineSpacing || 1) * 1.2),
|
|
90
|
+
justifyContent: p.anchor === 'bottom' ? 'flex-end' : p.anchor === 'middle' ? 'center' : 'flex-start',
|
|
91
|
+
});
|
|
92
|
+
el.appendChild(text);
|
|
93
|
+
} else if (o.type === 'line') {
|
|
94
|
+
const r = objectRect(o);
|
|
95
|
+
const w = Math.max(1, r.width);
|
|
96
|
+
const h = Math.max(1, r.height);
|
|
97
|
+
const svg = document.createElementNS(svgNS, 'svg');
|
|
98
|
+
svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
|
|
99
|
+
svg.setAttribute('preserveAspectRatio', 'none');
|
|
100
|
+
svg.classList.add('sl-line-svg');
|
|
101
|
+
const x1 = o.w < 0 ? w : 0;
|
|
102
|
+
const y1 = o.h < 0 ? h : 0;
|
|
103
|
+
const x2 = o.w < 0 ? 0 : w;
|
|
104
|
+
const y2 = o.h < 0 ? 0 : h;
|
|
105
|
+
const stroke = p.stroke || theme.text;
|
|
106
|
+
const width = p.strokeW > 0 ? p.strokeW : 2;
|
|
107
|
+
const line = document.createElementNS(svgNS, 'line');
|
|
108
|
+
line.setAttribute('x1', String(x1));
|
|
109
|
+
line.setAttribute('y1', String(y1));
|
|
110
|
+
line.setAttribute('x2', String(x2));
|
|
111
|
+
line.setAttribute('y2', String(y2));
|
|
112
|
+
line.setAttribute('stroke', stroke);
|
|
113
|
+
line.setAttribute('stroke-width', String(width));
|
|
114
|
+
if (p.dash && p.dash !== 'solid') line.setAttribute('stroke-dasharray', DASH_STYLES[p.dash] || '');
|
|
115
|
+
svg.appendChild(line);
|
|
116
|
+
const angle = Math.atan2(y2 - y1, x2 - x1);
|
|
117
|
+
const size = Math.max(9, width * 3.2 + 3);
|
|
118
|
+
if (p.arrowEnd) svg.appendChild(arrowHead(x2, y2, angle, size, stroke));
|
|
119
|
+
if (p.arrowStart) svg.appendChild(arrowHead(x1, y1, angle + Math.PI, size, stroke));
|
|
120
|
+
el.appendChild(svg);
|
|
121
|
+
} else if (o.type === 'image') {
|
|
122
|
+
const crop = p.crop || {};
|
|
123
|
+
const l = Math.max(0, Number(crop.l) || 0);
|
|
124
|
+
const t = Math.max(0, Number(crop.t) || 0);
|
|
125
|
+
const r = Math.max(0, Number(crop.r) || 0);
|
|
126
|
+
const b = Math.max(0, Number(crop.b) || 0);
|
|
127
|
+
const img = document.createElement('img');
|
|
128
|
+
img.src = p.src;
|
|
129
|
+
img.alt = p.alt || '';
|
|
130
|
+
img.draggable = false;
|
|
131
|
+
if (l || t || r || b) {
|
|
132
|
+
el.style.overflow = 'hidden';
|
|
133
|
+
const sx = 100 / Math.max(1, 100 - l - r);
|
|
134
|
+
const sy = 100 / Math.max(1, 100 - t - b);
|
|
135
|
+
Object.assign(img.style, {
|
|
136
|
+
position: 'absolute',
|
|
137
|
+
width: `${sx * 100}%`,
|
|
138
|
+
height: `${sy * 100}%`,
|
|
139
|
+
left: `${(-l * sx)}%`,
|
|
140
|
+
top: `${(-t * sy)}%`,
|
|
141
|
+
objectFit: 'fill',
|
|
142
|
+
});
|
|
143
|
+
} else {
|
|
144
|
+
Object.assign(img.style, { width: '100%', height: '100%', objectFit: 'fill' });
|
|
145
|
+
}
|
|
146
|
+
el.appendChild(img);
|
|
147
|
+
} else if (o.type === 'table') {
|
|
148
|
+
const rows = p.rows || [];
|
|
149
|
+
const cols = rows[0]?.length || 1;
|
|
150
|
+
const style = tableStyleColors(p.tableStyle, theme.key);
|
|
151
|
+
const table = document.createElement('table');
|
|
152
|
+
table.className = 'sl-table';
|
|
153
|
+
const cg = document.createElement('colgroup');
|
|
154
|
+
for (let c = 0; c < cols; c += 1) {
|
|
155
|
+
const col = document.createElement('col');
|
|
156
|
+
const pct = Array.isArray(p.colW) && p.colW[c] > 0 ? p.colW[c] : 100 / cols;
|
|
157
|
+
col.style.width = `${pct}%`;
|
|
158
|
+
cg.appendChild(col);
|
|
159
|
+
}
|
|
160
|
+
table.appendChild(cg);
|
|
161
|
+
const tbody = document.createElement('tbody');
|
|
162
|
+
rows.forEach((row, ri) => {
|
|
163
|
+
const tr = document.createElement('tr');
|
|
164
|
+
if (Array.isArray(p.rowH) && p.rowH[ri] > 0) tr.style.height = `${p.rowH[ri]}%`;
|
|
165
|
+
const header = p.headerRow && ri === 0;
|
|
166
|
+
const band = p.bandedRows && !header && (p.headerRow ? ri % 2 === 1 : ri % 2 === 0);
|
|
167
|
+
for (let c = 0; c < cols; c += 1) {
|
|
168
|
+
const td = document.createElement('td');
|
|
169
|
+
td.textContent = c < row.length ? row[c] : '';
|
|
170
|
+
td.style.fontSize = `${p.fontSize || 16}px`;
|
|
171
|
+
td.style.fontFamily = p.font || '';
|
|
172
|
+
td.style.color = header ? (style.headerText || theme.text) : (p.color || style.bodyText || theme.text);
|
|
173
|
+
td.style.fontWeight = header ? '700' : '400';
|
|
174
|
+
td.style.background = header ? (style.headerFill || 'transparent') : band ? (style.bandFill || 'transparent') : 'transparent';
|
|
175
|
+
td.style.border = `1px solid ${style.border}`;
|
|
176
|
+
tr.appendChild(td);
|
|
177
|
+
}
|
|
178
|
+
tbody.appendChild(tr);
|
|
179
|
+
});
|
|
180
|
+
table.appendChild(tbody);
|
|
181
|
+
el.appendChild(table);
|
|
182
|
+
}
|
|
183
|
+
return el;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** 箭头三角形(按线宽缩放;不用 <marker>,避免缩略图与画布同 id 互相串引用) */
|
|
187
|
+
function arrowHead(x, y, angle, size, color) {
|
|
188
|
+
const poly = document.createElementNS(svgNS, 'polygon');
|
|
189
|
+
const half = size * 0.42;
|
|
190
|
+
const back = size * 0.9;
|
|
191
|
+
const pts = [
|
|
192
|
+
[x, y],
|
|
193
|
+
[x - Math.cos(angle) * back - Math.sin(angle) * half, y - Math.sin(angle) * back + Math.cos(angle) * half],
|
|
194
|
+
[x - Math.cos(angle) * back + Math.sin(angle) * half, y - Math.sin(angle) * back - Math.cos(angle) * half],
|
|
195
|
+
];
|
|
196
|
+
poly.setAttribute('points', pts.map(([px, py]) => `${px},${py}`).join(' '));
|
|
197
|
+
poly.setAttribute('fill', color);
|
|
198
|
+
return poly;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 渲染一页幻灯片为 DOM(宽高恒为逻辑画幅;缩放由调用方 transform: scale 承担)
|
|
203
|
+
* @param {object} slide
|
|
204
|
+
* @param {string} theme 主题键
|
|
205
|
+
* @param {{interactive?:boolean,size?:{w:number,h:number}}} [opts]
|
|
206
|
+
* @returns {HTMLElement}
|
|
207
|
+
*/
|
|
208
|
+
export function renderPage(slide, theme, opts = {}) {
|
|
209
|
+
const key = typeof theme === 'string' ? theme : 'clean';
|
|
210
|
+
const t = { ...themeOf(key), key };
|
|
211
|
+
const size = normalizeSize(opts.size || { w: SLIDE_W, h: SLIDE_H });
|
|
212
|
+
const page = document.createElement('div');
|
|
213
|
+
page.className = 'sl-page';
|
|
214
|
+
page.style.width = `${size.w}px`;
|
|
215
|
+
page.style.height = `${size.h}px`;
|
|
216
|
+
page.style.background = slide.bg || t.bg;
|
|
217
|
+
page.style.color = t.text;
|
|
218
|
+
const objects = [...(slide.objects || [])].sort((a, b) => (a.z || 0) - (b.z || 0));
|
|
219
|
+
for (const o of objects) page.appendChild(renderObject(o, t, !!opts.interactive));
|
|
220
|
+
return page;
|
|
221
|
+
}
|
package/web/app/ui/popup.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
// [文件] gbpptx·Popup —— 自绘浮层基座(定位/限高/外点关闭/Esc 关闭),下拉、右键菜单、色板共用
|
|
2
|
-
|
|
3
|
-
const OPEN = new Set();
|
|
4
|
-
|
|
5
|
-
/** 计算浮层视口定位(fixed 坐标系;放不下自动翻转) */
|
|
6
|
-
export function computePopupPosition(anchor, size, { placement = 'bottom-start', gap = 4, margin = 8 } = {}) {
|
|
7
|
-
const vw = window.innerWidth;
|
|
8
|
-
const vh = window.innerHeight;
|
|
9
|
-
const a = {
|
|
10
|
-
left: anchor.left,
|
|
11
|
-
top: anchor.top,
|
|
12
|
-
right: anchor.right ?? anchor.left,
|
|
13
|
-
bottom: anchor.bottom ?? anchor.top,
|
|
14
|
-
};
|
|
15
|
-
const [side, cross] = String(placement).split('-');
|
|
16
|
-
const width = Math.min(size.width, Math.max(80, vw - margin * 2));
|
|
17
|
-
let left = cross === 'end' ? a.right - width : cross === 'start' ? a.left : (a.left + a.right) / 2 - width / 2;
|
|
18
|
-
left = Math.max(margin, Math.min(left, vw - width - margin));
|
|
19
|
-
let top = side === 'top' ? a.top - size.height - gap : a.bottom + gap;
|
|
20
|
-
let maxHeight = Math.max(80, side === 'top' ? a.top - gap - margin : vh - margin - (a.bottom + gap));
|
|
21
|
-
if (side === 'top' && size.height > a.top - gap - margin) {
|
|
22
|
-
top = a.bottom + gap;
|
|
23
|
-
maxHeight = Math.max(80, vh - margin - (a.bottom + gap));
|
|
24
|
-
}
|
|
25
|
-
return { left: Math.round(left), top: Math.round(top), maxHeight: Math.round(maxHeight) };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 打开一个浮层
|
|
30
|
-
* @param {object} o {anchor, content, placement, className, onClose}
|
|
31
|
-
*/
|
|
32
|
-
export function openPopup({ anchor, content, placement = 'bottom-start', className = '', onClose }) {
|
|
33
|
-
closeAll();
|
|
34
|
-
const wrap = document.createElement('div');
|
|
35
|
-
wrap.className = `gbp-popup ${className}`.trim();
|
|
36
|
-
wrap.appendChild(content);
|
|
37
|
-
document.body.appendChild(wrap);
|
|
38
|
-
|
|
39
|
-
const rect = anchor instanceof Element ? anchor.getBoundingClientRect() : anchor;
|
|
40
|
-
const measure = wrap.getBoundingClientRect();
|
|
41
|
-
const pos = computePopupPosition(rect, { width: measure.width, height: measure.height }, { placement });
|
|
42
|
-
Object.assign(wrap.style, { left: `${pos.left}px`, top: `${pos.top}px` });
|
|
43
|
-
if (pos.maxHeight > 0) wrap.style.maxHeight = `${pos.maxHeight}px`;
|
|
44
|
-
|
|
45
|
-
const close = () => {
|
|
46
|
-
if (!wrap.isConnected) return;
|
|
47
|
-
wrap.remove();
|
|
48
|
-
OPEN.delete(entry);
|
|
49
|
-
document.removeEventListener('mousedown', onDocDown, true);
|
|
50
|
-
document.removeEventListener('keydown', onDocKey, true);
|
|
51
|
-
document.removeEventListener('gbpptx-menu-close', onDocDown);
|
|
52
|
-
window.removeEventListener('resize', onResize);
|
|
53
|
-
onClose?.();
|
|
54
|
-
};
|
|
55
|
-
const onDocDown = (e) => {
|
|
56
|
-
if (wrap.contains(e.target)) return;
|
|
57
|
-
close();
|
|
58
|
-
};
|
|
59
|
-
const onDocKey = (e) => {
|
|
60
|
-
if (e.key === 'Escape') {
|
|
61
|
-
e.preventDefault();
|
|
62
|
-
e.stopPropagation();
|
|
63
|
-
close();
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
const onResize = () => close();
|
|
67
|
-
const entry = { close };
|
|
68
|
-
OPEN.add(entry);
|
|
69
|
-
setTimeout(() => {
|
|
70
|
-
document.addEventListener('mousedown', onDocDown, true);
|
|
71
|
-
window.addEventListener('resize', onResize);
|
|
72
|
-
}, 0);
|
|
73
|
-
document.addEventListener('keydown', onDocKey, true);
|
|
74
|
-
document.addEventListener('gbpptx-menu-close', onDocDown);
|
|
75
|
-
return { el: wrap, close };
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function closeAll() {
|
|
79
|
-
for (const entry of [...OPEN]) entry.close();
|
|
80
|
-
}
|
|
1
|
+
// [文件] gbpptx·Popup —— 自绘浮层基座(定位/限高/外点关闭/Esc 关闭),下拉、右键菜单、色板共用
|
|
2
|
+
|
|
3
|
+
const OPEN = new Set();
|
|
4
|
+
|
|
5
|
+
/** 计算浮层视口定位(fixed 坐标系;放不下自动翻转) */
|
|
6
|
+
export function computePopupPosition(anchor, size, { placement = 'bottom-start', gap = 4, margin = 8 } = {}) {
|
|
7
|
+
const vw = window.innerWidth;
|
|
8
|
+
const vh = window.innerHeight;
|
|
9
|
+
const a = {
|
|
10
|
+
left: anchor.left,
|
|
11
|
+
top: anchor.top,
|
|
12
|
+
right: anchor.right ?? anchor.left,
|
|
13
|
+
bottom: anchor.bottom ?? anchor.top,
|
|
14
|
+
};
|
|
15
|
+
const [side, cross] = String(placement).split('-');
|
|
16
|
+
const width = Math.min(size.width, Math.max(80, vw - margin * 2));
|
|
17
|
+
let left = cross === 'end' ? a.right - width : cross === 'start' ? a.left : (a.left + a.right) / 2 - width / 2;
|
|
18
|
+
left = Math.max(margin, Math.min(left, vw - width - margin));
|
|
19
|
+
let top = side === 'top' ? a.top - size.height - gap : a.bottom + gap;
|
|
20
|
+
let maxHeight = Math.max(80, side === 'top' ? a.top - gap - margin : vh - margin - (a.bottom + gap));
|
|
21
|
+
if (side === 'top' && size.height > a.top - gap - margin) {
|
|
22
|
+
top = a.bottom + gap;
|
|
23
|
+
maxHeight = Math.max(80, vh - margin - (a.bottom + gap));
|
|
24
|
+
}
|
|
25
|
+
return { left: Math.round(left), top: Math.round(top), maxHeight: Math.round(maxHeight) };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 打开一个浮层
|
|
30
|
+
* @param {object} o {anchor, content, placement, className, onClose}
|
|
31
|
+
*/
|
|
32
|
+
export function openPopup({ anchor, content, placement = 'bottom-start', className = '', onClose }) {
|
|
33
|
+
closeAll();
|
|
34
|
+
const wrap = document.createElement('div');
|
|
35
|
+
wrap.className = `gbp-popup ${className}`.trim();
|
|
36
|
+
wrap.appendChild(content);
|
|
37
|
+
document.body.appendChild(wrap);
|
|
38
|
+
|
|
39
|
+
const rect = anchor instanceof Element ? anchor.getBoundingClientRect() : anchor;
|
|
40
|
+
const measure = wrap.getBoundingClientRect();
|
|
41
|
+
const pos = computePopupPosition(rect, { width: measure.width, height: measure.height }, { placement });
|
|
42
|
+
Object.assign(wrap.style, { left: `${pos.left}px`, top: `${pos.top}px` });
|
|
43
|
+
if (pos.maxHeight > 0) wrap.style.maxHeight = `${pos.maxHeight}px`;
|
|
44
|
+
|
|
45
|
+
const close = () => {
|
|
46
|
+
if (!wrap.isConnected) return;
|
|
47
|
+
wrap.remove();
|
|
48
|
+
OPEN.delete(entry);
|
|
49
|
+
document.removeEventListener('mousedown', onDocDown, true);
|
|
50
|
+
document.removeEventListener('keydown', onDocKey, true);
|
|
51
|
+
document.removeEventListener('gbpptx-menu-close', onDocDown);
|
|
52
|
+
window.removeEventListener('resize', onResize);
|
|
53
|
+
onClose?.();
|
|
54
|
+
};
|
|
55
|
+
const onDocDown = (e) => {
|
|
56
|
+
if (wrap.contains(e.target)) return;
|
|
57
|
+
close();
|
|
58
|
+
};
|
|
59
|
+
const onDocKey = (e) => {
|
|
60
|
+
if (e.key === 'Escape') {
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
e.stopPropagation();
|
|
63
|
+
close();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const onResize = () => close();
|
|
67
|
+
const entry = { close };
|
|
68
|
+
OPEN.add(entry);
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
document.addEventListener('mousedown', onDocDown, true);
|
|
71
|
+
window.addEventListener('resize', onResize);
|
|
72
|
+
}, 0);
|
|
73
|
+
document.addEventListener('keydown', onDocKey, true);
|
|
74
|
+
document.addEventListener('gbpptx-menu-close', onDocDown);
|
|
75
|
+
return { el: wrap, close };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function closeAll() {
|
|
79
|
+
for (const entry of [...OPEN]) entry.close();
|
|
80
|
+
}
|