gbpptx 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +220 -158
  2. package/package.json +15 -4
  3. package/src/index.js +16 -8
  4. package/src/kernel/model.js +397 -91
  5. package/src/kernel/parts.js +3 -3
  6. package/src/kernel/pptx.js +17 -12
  7. package/src/kernel/read.js +747 -157
  8. package/src/kernel/roundtrip.js +118 -18
  9. package/src/kernel/text.js +133 -28
  10. package/src/kernel/write.js +160 -49
  11. package/web/app/main.js +8 -8
  12. package/web/app/slides/arrange.js +101 -0
  13. package/web/app/slides/clipboard.js +93 -0
  14. package/web/app/slides/file.js +243 -0
  15. package/web/app/slides/find.js +199 -0
  16. package/web/app/slides/history.js +100 -0
  17. package/web/app/slides/keys.js +174 -0
  18. package/web/app/slides/menus.js +105 -0
  19. package/web/app/slides/objects.js +254 -0
  20. package/web/app/slides/render.js +183 -0
  21. package/web/app/slides/shared.js +85 -0
  22. package/web/app/slides/slides.js +103 -0
  23. package/web/app/slides/table.js +102 -0
  24. package/web/app/slides/text.js +418 -0
  25. package/web/app/slides/view.js +130 -0
  26. package/web/app/slides-app.js +285 -844
  27. package/web/app/ui/backstage.js +255 -0
  28. package/web/app/ui/canvas.js +762 -277
  29. package/web/app/ui/color-panel.js +58 -58
  30. package/web/app/ui/dialogs.js +120 -120
  31. package/web/app/ui/file-io.js +116 -89
  32. package/web/app/ui/format-pane.js +427 -0
  33. package/web/app/ui/icons.js +129 -0
  34. package/web/app/ui/menu.js +75 -105
  35. package/web/app/ui/notify.js +45 -45
  36. package/web/app/ui/page-view.js +221 -150
  37. package/web/app/ui/popup.js +80 -80
  38. package/web/app/ui/present.js +292 -100
  39. package/web/app/ui/ribbon.js +878 -0
  40. package/web/app/ui/slide-pane.js +189 -0
  41. package/web/app/ui/statusbar.js +93 -0
  42. package/web/app/ui/text-styles.js +84 -84
  43. package/web/app/ui/titlebar.js +208 -0
  44. package/web/app/ui/transition.js +30 -0
  45. package/web/index.html +23 -21
  46. package/web/serve.mjs +20 -67
  47. package/web/styles/office.css +642 -0
  48. package/web/styles/slides.css +210 -212
  49. package/web/styles/tokens.css +154 -155
  50. package/src/kernel/menus.js +0 -123
  51. package/src/kernel/util/escape.js +0 -21
  52. package/src/kernel/util/id.js +0 -13
  53. package/web/app/ui/inspector.js +0 -209
  54. package/web/app/ui/slides-bar.js +0 -63
  55. package/web/app/ui/toolbar.js +0 -200
@@ -1,150 +1,221 @@
1
- // [文件] gbpptx·页渲染 —— 一页幻灯片 → DOM(缩略图/画布/放映/打印共用同一坐标系 960×540)
2
-
3
- import { SLIDE_H, SLIDE_W, objectBoxStyle, objectRect, shapePath, themeOf } from '../../../src/kernel/model.js';
4
-
5
- const esc = (s) => String(s ?? '');
6
-
7
- /** 图形内文字(纯文本,\n 分行) */
8
- const shapeTextHtml = (text) => esc(text).split('\n').map((line) => `<div>${line.replace(/[&<>]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' }[c]))}</div>`).join('');
9
-
10
- /** 一个对象的 DOM(static 与编辑两层共用;interactive=true 时挂 data-obj-id) */
11
- function renderObject(o, interactive) {
12
- const el = document.createElement('div');
13
- el.className = `sl-obj sl-obj--${o.type}`;
14
- Object.assign(el.style, objectBoxStyle(o));
15
- if (interactive) el.dataset.objId = o.id;
16
- const p = o.props || {};
17
- if (o.type === 'text') {
18
- const body = document.createElement('div');
19
- body.className = 'sl-text';
20
- body.innerHTML = p.html || '';
21
- Object.assign(body.style, {
22
- fontSize: `${p.fontSize || 24}px`,
23
- color: p.color || 'inherit',
24
- textAlign: p.align || 'left',
25
- fontFamily: p.font || '',
26
- fontWeight: p.bold ? '700' : '400',
27
- fontStyle: p.italic ? 'italic' : 'normal',
28
- textDecoration: p.underline ? 'underline' : 'none',
29
- });
30
- el.appendChild(body);
31
- } else if (o.type === 'shape') {
32
- const svgNs = 'http://www.w3.org/2000/svg';
33
- const svg = document.createElementNS(svgNs, 'svg');
34
- const path = document.createElementNS(svgNs, 'path');
35
- const w = Math.max(1, objectRect(o).width);
36
- const h = Math.max(1, objectRect(o).height);
37
- path.setAttribute('d', shapePath(p.kind, w, h));
38
- path.setAttribute('fill', p.fill || '#e07b1f');
39
- if (p.strokeW > 0) {
40
- path.setAttribute('stroke', p.stroke || '#333333');
41
- path.setAttribute('stroke-width', String(p.strokeW));
42
- }
43
- svg.appendChild(path);
44
- el.appendChild(svg);
45
- const text = document.createElement('div');
46
- text.className = 'sl-shape-text';
47
- text.innerHTML = shapeTextHtml(p.text || '');
48
- Object.assign(text.style, {
49
- fontSize: `${p.fontSize || 18}px`,
50
- color: p.textColor || '#ffffff',
51
- fontFamily: p.font || '',
52
- fontWeight: p.bold ? '700' : '400',
53
- });
54
- el.appendChild(text);
55
- } else if (o.type === 'line') {
56
- const svgNs = 'http://www.w3.org/2000/svg';
57
- const r = objectRect(o);
58
- const svg = document.createElementNS(svgNs, 'svg');
59
- svg.setAttribute('viewBox', `0 0 ${Math.max(1, r.width)} ${Math.max(1, r.height)}`);
60
- const line = document.createElementNS(svgNs, 'line');
61
- const startX = o.w < 0 ? r.width : 0;
62
- const endX = o.w < 0 ? 0 : r.width;
63
- const startY = o.h < 0 ? r.height : 0;
64
- const endY = o.h < 0 ? 0 : r.height;
65
- line.setAttribute('x1', String(startX));
66
- line.setAttribute('y1', String(startY));
67
- line.setAttribute('x2', String(endX));
68
- line.setAttribute('y2', String(endY));
69
- line.setAttribute('stroke', p.stroke || '#202124');
70
- line.setAttribute('stroke-width', String(p.strokeW > 0 ? p.strokeW : 2));
71
- if (p.dash) line.setAttribute('stroke-dasharray', '8 6');
72
- if (p.arrowEnd) {
73
- const NS = 'http://www.w3.org/2000/svg';
74
- const defs = document.createElementNS(NS, 'defs');
75
- const marker = document.createElementNS(NS, 'marker');
76
- marker.setAttribute('id', `arrow-${o.id}`);
77
- marker.setAttribute('markerWidth', '10');
78
- marker.setAttribute('markerHeight', '10');
79
- marker.setAttribute('refX', '8');
80
- marker.setAttribute('refY', '3');
81
- marker.setAttribute('orient', 'auto');
82
- const mp = document.createElementNS(NS, 'path');
83
- mp.setAttribute('d', 'M0,0 L8,3 L0,6 Z');
84
- mp.setAttribute('fill', p.stroke || '#202124');
85
- marker.appendChild(mp);
86
- defs.appendChild(marker);
87
- svg.appendChild(defs);
88
- line.setAttribute('marker-end', `url(#arrow-${o.id})`);
89
- }
90
- svg.appendChild(line);
91
- el.appendChild(svg);
92
- } else if (o.type === 'image') {
93
- const img = document.createElement('img');
94
- img.src = p.src;
95
- img.alt = '';
96
- img.draggable = false;
97
- el.appendChild(img);
98
- } else if (o.type === 'table') {
99
- const rows = p.rows || [];
100
- const cols = rows[0]?.length || 1;
101
- const table = document.createElement('table');
102
- table.className = 'sl-table';
103
- const cg = document.createElement('colgroup');
104
- for (let c = 0; c < cols; c += 1) {
105
- const col = document.createElement('col');
106
- const pct = Array.isArray(p.colW) && p.colW[c] > 0 ? p.colW[c] : 100 / cols;
107
- col.style.width = `${pct}%`;
108
- cg.appendChild(col);
109
- }
110
- table.appendChild(cg);
111
- const tbody = document.createElement('tbody');
112
- rows.forEach((row, ri) => {
113
- const tr = document.createElement('tr');
114
- if (Array.isArray(p.rowH) && p.rowH[ri] > 0) tr.style.height = `${p.rowH[ri]}%`;
115
- for (let c = 0; c < cols; c += 1) {
116
- const td = document.createElement('td');
117
- td.textContent = c < row.length ? row[c] : '';
118
- td.style.fontSize = `${p.fontSize || 16}px`;
119
- if (p.color) td.style.color = p.color;
120
- if (p.font) td.style.fontFamily = p.font;
121
- if (p.headerRow && ri === 0) td.style.fontWeight = '700';
122
- tr.appendChild(td);
123
- }
124
- tbody.appendChild(tr);
125
- });
126
- table.appendChild(tbody);
127
- el.appendChild(table);
128
- }
129
- return el;
130
- }
131
-
132
- /**
133
- * 渲染一页幻灯片为 DOM(宽高恒 960×540 逻辑坐标;缩放由调用方 transform: scale 承担)
134
- * @param {object} slide
135
- * @param {string} theme 主题键
136
- * @param {{interactive?:boolean}} [opts]
137
- * @returns {HTMLElement}
138
- */
139
- export function renderPage(slide, theme, opts = {}) {
140
- const t = themeOf(theme);
141
- const page = document.createElement('div');
142
- page.className = 'sl-page';
143
- page.style.width = `${SLIDE_W}px`;
144
- page.style.height = `${SLIDE_H}px`;
145
- page.style.background = slide.bg || t.bg;
146
- page.style.color = t.text;
147
- const objects = [...(slide.objects || [])].sort((a, b) => (a.z || 0) - (b.z || 0));
148
- for (const o of objects) page.appendChild(renderObject(o, !!opts.interactive));
149
- return page;
150
- }
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) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' }[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,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 = `gb-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
+ }