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.
Files changed (50) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +253 -253
  3. package/package.json +48 -47
  4. package/src/index.js +29 -29
  5. package/src/kernel/model.js +651 -651
  6. package/src/kernel/parts.js +173 -173
  7. package/src/kernel/pptx.js +72 -72
  8. package/src/kernel/print.js +76 -76
  9. package/src/kernel/read.js +1018 -1018
  10. package/src/kernel/roundtrip.js +174 -174
  11. package/src/kernel/text.js +278 -278
  12. package/src/kernel/write.js +385 -385
  13. package/web/app/main.js +8 -8
  14. package/web/app/slides/arrange.js +101 -101
  15. package/web/app/slides/clipboard.js +93 -93
  16. package/web/app/slides/file.js +240 -240
  17. package/web/app/slides/find.js +201 -201
  18. package/web/app/slides/history.js +100 -100
  19. package/web/app/slides/keys.js +187 -187
  20. package/web/app/slides/menus.js +105 -105
  21. package/web/app/slides/objects.js +254 -254
  22. package/web/app/slides/render.js +183 -183
  23. package/web/app/slides/shared.js +85 -85
  24. package/web/app/slides/slides.js +103 -103
  25. package/web/app/slides/table.js +102 -102
  26. package/web/app/slides/text.js +418 -418
  27. package/web/app/slides/view.js +182 -182
  28. package/web/app/slides-app.js +354 -354
  29. package/web/app/ui/canvas.js +769 -769
  30. package/web/app/ui/color-panel.js +58 -58
  31. package/web/app/ui/dialogs.js +120 -120
  32. package/web/app/ui/file-io.js +116 -116
  33. package/web/app/ui/format-pane.js +427 -427
  34. package/web/app/ui/icons.js +241 -133
  35. package/web/app/ui/menu.js +77 -75
  36. package/web/app/ui/notify.js +45 -45
  37. package/web/app/ui/page-view.js +221 -221
  38. package/web/app/ui/popup.js +80 -80
  39. package/web/app/ui/present.js +292 -292
  40. package/web/app/ui/ribbon.js +855 -855
  41. package/web/app/ui/slide-pane.js +197 -197
  42. package/web/app/ui/statusbar.js +93 -93
  43. package/web/app/ui/text-styles.js +84 -84
  44. package/web/app/ui/titlebar.js +253 -253
  45. package/web/app/ui/transition.js +30 -30
  46. package/web/index.html +30 -29
  47. package/web/serve.mjs +26 -23
  48. package/web/styles/office.css +463 -463
  49. package/web/styles/slides.css +97 -97
  50. package/web/styles/tokens.css +154 -154
@@ -1,418 +1,418 @@
1
- // [文件] gbpptx·slides·text —— 文本编辑与格式 —— 就地编辑生命周期、行内格式与段落格式
2
-
3
- // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { clamp, escapeHtml, normalizeColor } from 'guabiutils';
7
- import { htmlToPlain, setObjectFontSize, themeOf } from '../../../src/kernel/model.js';
8
- import { htmlToParas, parasToHTML, sanitizeTextHtml } from '../../../src/kernel/text.js';
9
- import { styleFromNode, closestBlock, closeAlign } from './shared.js';
10
- function beginTextEdit(obj, node, kind, meta = {}) {
11
- this.editing = {
12
- obj, id: obj.id, node, kind, meta,
13
- before: JSON.stringify(this.snapshot),
14
- changed: false,
15
- range: null,
16
- };
17
- if (kind === 'cell') {
18
- this.tableCursor = { objId: obj.id, ri: meta.ri, ci: meta.ci };
19
- }
20
- }
21
-
22
- function syncEditing() {
23
- const e = this.editing;
24
- if (!e) return;
25
- if (!e.changed) {
26
- this.pushHistorySnapshot(e.before);
27
- e.changed = true;
28
- }
29
- if (e.kind === 'cell') {
30
- const { ri, ci } = e.meta;
31
- const rows = e.obj.props.rows;
32
- if (rows[ri]) rows[ri][ci] = String(e.node.innerText || '').replace(/\n+$/, '');
33
- } else if (e.kind === 'text') {
34
- e.obj.props.html = sanitizeTextHtml(e.node.innerHTML);
35
- } else if (e.kind === 'shape') {
36
- e.obj.props.text = String(e.node.innerText || '').replace(/\n+$/, '');
37
- }
38
- this.markDirty();
39
- this.ribbon.refresh();
40
- this.titlebar.refresh();
41
- }
42
-
43
- function noteEditChange() {
44
- const e = this.editing;
45
- if (!e || e.changed) return;
46
- this.pushHistorySnapshot(e.before);
47
- e.changed = true;
48
- this.markDirty();
49
- }
50
-
51
- function commitEditing({ render = true, node = null } = {}) {
52
- const e = this.editing;
53
- if (!e) return;
54
- if (node && e.node !== node) return;
55
- if (e.kind === 'cell') {
56
- this.tableCursor = { objId: e.obj.id, ri: e.meta.ri, ci: e.meta.ci };
57
- }
58
- this.editing = null;
59
- if (e.changed) {
60
- this.setDirty(true);
61
- if (render) this.notifyChange();
62
- else {
63
- // 不重建格式窗格:点击窗格控件触发的 blur 不能把正在点的 DOM 换掉
64
- this.ribbon.refresh();
65
- this.titlebar.refresh();
66
- }
67
- }
68
- }
69
-
70
- function startEditingById(id) {
71
- this.selectSlide(this.index);
72
- this.selectObjects([id]);
73
- setTimeout(() => this.canvas.editObject(id), 0);
74
- }
75
-
76
- function editCellAt(objId, ri, ci) {
77
- const obj = this.selectedById(objId);
78
- if (!obj || obj.type !== 'table') return;
79
- if (ri >= obj.props.rows.length || ci >= (obj.props.rows[0]?.length || 1)) return;
80
- this.selectedIds = [obj.id];
81
- this.renderAll();
82
- this.canvas.editCell(obj, ri, ci);
83
- }
84
-
85
- function canFormatText() {
86
- if (this.editing) return true;
87
- const objs = this.selectedObjects();
88
- return objs.some((o) => o.type === 'text' || o.type === 'shape' || o.type === 'table');
89
- }
90
-
91
- function canRichText() {
92
- if (this.editing) return this.editing.kind !== 'cell';
93
- const objs = this.selectedObjects();
94
- return objs.length > 0 && objs.every((o) => o.type === 'text' || o.type === 'shape');
95
- }
96
-
97
- function canAlignText() {
98
- return this.canRichText();
99
- }
100
-
101
- function canList() {
102
- if (this.editing) return this.editing.kind === 'text';
103
- return this.selectedObjects().some((o) => o.type === 'text');
104
- }
105
-
106
- function textState() {
107
- const base = {
108
- bold: false, italic: false, underline: false, strike: false,
109
- font: '', fontSize: 0, color: '', align: 'left', lineSpacing: 1, list: '', editing: false,
110
- };
111
- if (this.editing) {
112
- const e = this.editing;
113
- const sel = window.getSelection();
114
- let node = sel?.anchorNode || e.node;
115
- if (node.nodeType === Node.TEXT_NODE) node = node.parentElement;
116
- if (!node || !e.node.contains(node)) node = e.node;
117
- const cs = window.getComputedStyle(node);
118
- const style = styleFromNode(node, e.node);
119
- const block = closestBlock(node, e.node);
120
- const alignMap = { left: 'left', start: 'left', center: 'center', right: 'right', justify: 'justify' };
121
- const list = node.closest?.('ul,ol');
122
- return {
123
- ...base,
124
- ...style,
125
- align: alignMap[closeAlign(block)] || 'left',
126
- lineSpacing: e.obj.props.lineSpacing || 1,
127
- list: list ? (list.tagName === 'OL' ? 'number' : 'bullet') : '',
128
- editing: true,
129
- fontSize: parseFloat(cs.fontSize) || e.obj.props.fontSize || 24,
130
- color: normalizeColor(cs.color) || e.obj.props.color || '',
131
- };
132
- }
133
- const o = this.firstSelected();
134
- if (!o) return base;
135
- const p = o.props || {};
136
- const defaultColor = themeOf(this.snapshot.theme).text;
137
- return {
138
- ...base,
139
- bold: !!p.bold,
140
- italic: !!p.italic,
141
- underline: !!p.underline,
142
- strike: !!p.strike,
143
- font: p.font || '',
144
- fontSize: p.fontSize || 0,
145
- color: o.type === 'shape' ? (p.textColor || defaultColor) : (p.color || defaultColor),
146
- align: p.align || 'left',
147
- lineSpacing: p.lineSpacing || 1,
148
- list: '',
149
- editing: false,
150
- };
151
- }
152
-
153
- function toggleTextStyle(key) {
154
- const command = { bold: 'bold', italic: 'italic', underline: 'underline', strike: 'strikeThrough' }[key];
155
- if (!command) return;
156
- if (this.editing?.kind === 'text') {
157
- this.canvas.focusEditing?.();
158
- document.execCommand(command, false);
159
- this.syncEditing();
160
- this.ribbon.refresh();
161
- return;
162
- }
163
- if (this.editing?.kind === 'shape') {
164
- const o = this.editing.obj;
165
- const value = !o.props[key];
166
- o.props[key] = value;
167
- if (key === 'bold') this.editing.node.style.fontWeight = value ? '700' : '400';
168
- else if (key === 'italic') this.editing.node.style.fontStyle = value ? 'italic' : 'normal';
169
- else {
170
- this.editing.node.style.textDecoration = [o.props.underline ? 'underline' : '', o.props.strike ? 'line-through' : '']
171
- .filter(Boolean).join(' ') || 'none';
172
- }
173
- this.noteEditChange();
174
- this.ribbon.refresh();
175
- return;
176
- }
177
- if (this.editing) return;
178
- if (!this.canRichText()) return;
179
- const objs = this.selectedObjects();
180
- const first = objs[0];
181
- const value = !first?.props?.[key];
182
- this.pushHistory();
183
- for (const o of objs) {
184
- if (o.type === 'text' || o.type === 'shape') o.props[key] = value;
185
- }
186
- this.notifyChange();
187
- }
188
-
189
- function applyFontFamily(font) {
190
- if (this.editing?.kind === 'text' && this.canvas.wrapSelection({ fontFamily: font })) {
191
- this.ribbon.refresh();
192
- return;
193
- }
194
- if (this.editing) {
195
- const o = this.editing.obj;
196
- if (this.editing.kind === 'cell') {
197
- o.props.font = font;
198
- this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => { td.style.fontFamily = font; });
199
- } else {
200
- for (const target of this.selectedObjects()) if (target.props) target.props.font = font;
201
- this.editing.node.style.fontFamily = font;
202
- }
203
- this.noteEditChange();
204
- this.ribbon.refresh();
205
- return;
206
- }
207
- if (!this.canFormatText()) return;
208
- this.pushHistory();
209
- for (const o of this.selectedObjects()) {
210
- if (o.props && o.type !== 'line') o.props.font = font;
211
- }
212
- this.notifyChange();
213
- }
214
-
215
- function applyFontSize(px) {
216
- const value = clamp(Math.round(Number(px) || 0), 6, 200);
217
- if (!value) return;
218
- if (this.editing?.kind === 'text' && this.canvas.wrapSelection({ fontSize: `${value}px` })) {
219
- this.ribbon.refresh();
220
- return;
221
- }
222
- if (this.editing) {
223
- const o = this.editing.obj;
224
- if (this.editing.kind === 'cell') {
225
- o.props.fontSize = value;
226
- this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => { td.style.fontSize = `${value}px`; });
227
- } else {
228
- for (const target of this.selectedObjects()) {
229
- if (target.type === 'text') setObjectFontSize(target, value);
230
- else if (target.props) target.props.fontSize = value;
231
- }
232
- this.editing.node.style.fontSize = `${value}px`;
233
- }
234
- this.noteEditChange();
235
- this.ribbon.refresh();
236
- return;
237
- }
238
- if (!this.canFormatText()) return;
239
- this.pushHistory();
240
- for (const o of this.selectedObjects()) {
241
- if (o.type === 'text') setObjectFontSize(o, value);
242
- else if (o.type === 'shape' || o.type === 'table') o.props.fontSize = value;
243
- }
244
- this.notifyChange();
245
- }
246
-
247
- function setFontSize(px) { this.applyFontSize(px); }
248
-
249
- function setFontFamily(font) { this.applyFontFamily(font); }
250
-
251
- function stepFontSize(delta) {
252
- const current = this.textState().fontSize || 24;
253
- this.applyFontSize(current + (delta > 0 ? 2 : -2));
254
- }
255
-
256
- function applyTextColor(color) {
257
- const themed = color || themeOf(this.snapshot.theme).text;
258
- if (this.editing?.kind === 'text' && this.canvas.wrapSelection({ color: themed })) {
259
- this.ribbon.refresh();
260
- return;
261
- }
262
- if (this.editing) {
263
- if (this.editing.kind === 'cell') {
264
- this.editing.obj.props.color = color;
265
- this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => { td.style.color = themed; });
266
- } else {
267
- for (const o of this.selectedObjects()) {
268
- if (o.type === 'shape') o.props.textColor = themed;
269
- else if (o.props && o.type !== 'line') o.props.color = color;
270
- }
271
- this.editing.node.style.color = themed;
272
- }
273
- this.noteEditChange();
274
- this.ribbon.refresh();
275
- return;
276
- }
277
- if (!this.canFormatText()) return;
278
- this.pushHistory();
279
- for (const o of this.selectedObjects()) {
280
- if (o.type === 'shape') o.props.textColor = themed;
281
- else if (o.props && o.type !== 'line') o.props.color = color;
282
- }
283
- this.notifyChange();
284
- }
285
-
286
- function applyAlign(align) {
287
- if (this.editing?.kind === 'text') {
288
- const command = { left: 'justifyLeft', center: 'justifyCenter', right: 'justifyRight', justify: 'justifyFull' }[align];
289
- if (command) document.execCommand(command, false);
290
- this.syncEditing();
291
- this.ribbon.refresh();
292
- return;
293
- }
294
- if (this.editing?.kind === 'shape') {
295
- this.editing.obj.props.align = align;
296
- this.editing.node.style.textAlign = align;
297
- this.noteEditChange();
298
- this.ribbon.refresh();
299
- return;
300
- }
301
- if (this.editing) return;
302
- if (!this.canAlignText()) return;
303
- this.pushHistory();
304
- for (const o of this.selectedObjects()) if (o.props) o.props.align = align;
305
- this.notifyChange();
306
- }
307
-
308
- function applyList(kind) {
309
- if (this.editing) {
310
- if (this.editing.kind !== 'text') return;
311
- document.execCommand(kind === 'bullet' ? 'insertUnorderedList' : 'insertOrderedList', false);
312
- this.syncEditing();
313
- this.ribbon.refresh();
314
- return;
315
- }
316
- if (!this.canRichText()) return;
317
- this.pushHistory();
318
- for (const o of this.selectedObjects()) {
319
- if (o.type !== 'text') continue;
320
- const lines = htmlToPlain(o.props.html).split('\n').map((s) => s.trim()).filter(Boolean);
321
- if (!lines.length) continue;
322
- const tag = kind === 'bullet' ? 'ul' : 'ol';
323
- o.props.html = `<${tag}>${lines.map((line) => `<li>${escapeHtml(line)}</li>`).join('')}</${tag}>`;
324
- }
325
- this.notifyChange();
326
- }
327
-
328
- function applyIndent(delta) {
329
- if (!this.editing || this.editing.kind !== 'text') return;
330
- // 只对列表项改层级:普通段落缩进不在对象模型里,做了也存不下来
331
- if (!this.textState().list) return;
332
- document.execCommand(delta > 0 ? 'indent' : 'outdent', false);
333
- this.syncEditing();
334
- this.ribbon.refresh();
335
- }
336
-
337
- function applyLineSpacing(v) {
338
- const value = clamp(Number(v) || 1, 0.6, 4);
339
- if (this.editing) {
340
- if (this.editing.kind === 'cell') return;
341
- for (const o of this.selectedObjects()) if (o.props && (o.type === 'text' || o.type === 'shape')) o.props.lineSpacing = value;
342
- this.editing.node.style.lineHeight = String(value * 1.2);
343
- this.noteEditChange();
344
- this.ribbon.refresh();
345
- return;
346
- }
347
- if (!this.canRichText()) return;
348
- this.pushHistory();
349
- for (const o of this.selectedObjects()) if (o.props && (o.type === 'text' || o.type === 'shape')) o.props.lineSpacing = value;
350
- this.notifyChange();
351
- }
352
-
353
- function clearFormatting() {
354
- if (this.editing?.kind === 'cell') {
355
- const table = this.editing.obj;
356
- Object.assign(table.props, { font: '', fontSize: 16, color: '' });
357
- this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => {
358
- td.style.fontFamily = '';
359
- td.style.fontSize = '16px';
360
- td.style.color = themeOf(this.snapshot.theme).text;
361
- });
362
- this.noteEditChange();
363
- this.ribbon.refresh();
364
- return;
365
- }
366
- if (this.editing?.kind === 'shape') {
367
- const shape = this.editing.obj;
368
- Object.assign(shape.props, {
369
- font: '', fontSize: 18, textColor: '#ffffff', bold: false, italic: false,
370
- underline: false, strike: false, align: 'center', lineSpacing: 1,
371
- });
372
- Object.assign(this.editing.node.style, {
373
- fontFamily: '', fontSize: '18px', color: '#ffffff', fontWeight: '400',
374
- fontStyle: 'normal', textDecoration: 'none', textAlign: 'center', lineHeight: '1.2',
375
- });
376
- this.noteEditChange();
377
- this.ribbon.refresh();
378
- return;
379
- }
380
- if (this.editing) {
381
- this.canvas.focusEditing();
382
- document.execCommand('removeFormat', false);
383
- const o = this.editing.obj;
384
- Object.assign(o.props, {
385
- font: '', fontSize: 24, color: '', bold: false, italic: false,
386
- underline: false, strike: false, lineSpacing: 1,
387
- });
388
- Object.assign(this.editing.node.style, {
389
- fontFamily: '', fontSize: '24px', color: themeOf(this.snapshot.theme).text,
390
- fontWeight: '400', fontStyle: 'normal', textDecoration: 'none', lineHeight: '1.2',
391
- });
392
- this.syncEditing();
393
- this.ribbon.refresh();
394
- return;
395
- }
396
- if (!this.selectedObjects().length) return;
397
- this.pushHistory();
398
- for (const o of this.selectedObjects()) {
399
- if (o.type === 'text') {
400
- const paras = htmlToParas(o.props.html);
401
- for (const p of paras) {
402
- for (const r of p.runs) {
403
- r.bold = false; r.italic = false; r.underline = false; r.strike = false;
404
- r.color = ''; r.font = ''; r.size = 0;
405
- }
406
- }
407
- o.props.html = parasToHTML(paras);
408
- Object.assign(o.props, { font: '', fontSize: 24, color: '', bold: false, italic: false, underline: false, strike: false, align: 'left', lineSpacing: 1 });
409
- } else if (o.type === 'shape') {
410
- Object.assign(o.props, { font: '', fontSize: 18, textColor: '#ffffff', bold: false, italic: false, underline: false, align: 'center', lineSpacing: 1, shadow: false });
411
- } else if (o.type === 'table') {
412
- Object.assign(o.props, { font: '', fontSize: 16, color: '', tableStyle: 'plain', headerRow: false, bandedRows: false });
413
- }
414
- }
415
- this.notifyChange();
416
- }
417
-
418
- export const textMethods = { beginTextEdit, syncEditing, noteEditChange, commitEditing, startEditingById, editCellAt, canFormatText, canRichText, canAlignText, canList, textState, toggleTextStyle, applyFontFamily, applyFontSize, setFontSize, setFontFamily, stepFontSize, applyTextColor, applyAlign, applyList, applyIndent, applyLineSpacing, clearFormatting };
1
+ // [文件] gbpptx·slides·text —— 文本编辑与格式 —— 就地编辑生命周期、行内格式与段落格式
2
+
3
+ // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { clamp, escapeHtml, normalizeColor } from 'guabiutils';
7
+ import { htmlToPlain, setObjectFontSize, themeOf } from '../../../src/kernel/model.js';
8
+ import { htmlToParas, parasToHTML, sanitizeTextHtml } from '../../../src/kernel/text.js';
9
+ import { styleFromNode, closestBlock, closeAlign } from './shared.js';
10
+ function beginTextEdit(obj, node, kind, meta = {}) {
11
+ this.editing = {
12
+ obj, id: obj.id, node, kind, meta,
13
+ before: JSON.stringify(this.snapshot),
14
+ changed: false,
15
+ range: null,
16
+ };
17
+ if (kind === 'cell') {
18
+ this.tableCursor = { objId: obj.id, ri: meta.ri, ci: meta.ci };
19
+ }
20
+ }
21
+
22
+ function syncEditing() {
23
+ const e = this.editing;
24
+ if (!e) return;
25
+ if (!e.changed) {
26
+ this.pushHistorySnapshot(e.before);
27
+ e.changed = true;
28
+ }
29
+ if (e.kind === 'cell') {
30
+ const { ri, ci } = e.meta;
31
+ const rows = e.obj.props.rows;
32
+ if (rows[ri]) rows[ri][ci] = String(e.node.innerText || '').replace(/\n+$/, '');
33
+ } else if (e.kind === 'text') {
34
+ e.obj.props.html = sanitizeTextHtml(e.node.innerHTML);
35
+ } else if (e.kind === 'shape') {
36
+ e.obj.props.text = String(e.node.innerText || '').replace(/\n+$/, '');
37
+ }
38
+ this.markDirty();
39
+ this.ribbon.refresh();
40
+ this.titlebar.refresh();
41
+ }
42
+
43
+ function noteEditChange() {
44
+ const e = this.editing;
45
+ if (!e || e.changed) return;
46
+ this.pushHistorySnapshot(e.before);
47
+ e.changed = true;
48
+ this.markDirty();
49
+ }
50
+
51
+ function commitEditing({ render = true, node = null } = {}) {
52
+ const e = this.editing;
53
+ if (!e) return;
54
+ if (node && e.node !== node) return;
55
+ if (e.kind === 'cell') {
56
+ this.tableCursor = { objId: e.obj.id, ri: e.meta.ri, ci: e.meta.ci };
57
+ }
58
+ this.editing = null;
59
+ if (e.changed) {
60
+ this.setDirty(true);
61
+ if (render) this.notifyChange();
62
+ else {
63
+ // 不重建格式窗格:点击窗格控件触发的 blur 不能把正在点的 DOM 换掉
64
+ this.ribbon.refresh();
65
+ this.titlebar.refresh();
66
+ }
67
+ }
68
+ }
69
+
70
+ function startEditingById(id) {
71
+ this.selectSlide(this.index);
72
+ this.selectObjects([id]);
73
+ setTimeout(() => this.canvas.editObject(id), 0);
74
+ }
75
+
76
+ function editCellAt(objId, ri, ci) {
77
+ const obj = this.selectedById(objId);
78
+ if (!obj || obj.type !== 'table') return;
79
+ if (ri >= obj.props.rows.length || ci >= (obj.props.rows[0]?.length || 1)) return;
80
+ this.selectedIds = [obj.id];
81
+ this.renderAll();
82
+ this.canvas.editCell(obj, ri, ci);
83
+ }
84
+
85
+ function canFormatText() {
86
+ if (this.editing) return true;
87
+ const objs = this.selectedObjects();
88
+ return objs.some((o) => o.type === 'text' || o.type === 'shape' || o.type === 'table');
89
+ }
90
+
91
+ function canRichText() {
92
+ if (this.editing) return this.editing.kind !== 'cell';
93
+ const objs = this.selectedObjects();
94
+ return objs.length > 0 && objs.every((o) => o.type === 'text' || o.type === 'shape');
95
+ }
96
+
97
+ function canAlignText() {
98
+ return this.canRichText();
99
+ }
100
+
101
+ function canList() {
102
+ if (this.editing) return this.editing.kind === 'text';
103
+ return this.selectedObjects().some((o) => o.type === 'text');
104
+ }
105
+
106
+ function textState() {
107
+ const base = {
108
+ bold: false, italic: false, underline: false, strike: false,
109
+ font: '', fontSize: 0, color: '', align: 'left', lineSpacing: 1, list: '', editing: false,
110
+ };
111
+ if (this.editing) {
112
+ const e = this.editing;
113
+ const sel = window.getSelection();
114
+ let node = sel?.anchorNode || e.node;
115
+ if (node.nodeType === Node.TEXT_NODE) node = node.parentElement;
116
+ if (!node || !e.node.contains(node)) node = e.node;
117
+ const cs = window.getComputedStyle(node);
118
+ const style = styleFromNode(node, e.node);
119
+ const block = closestBlock(node, e.node);
120
+ const alignMap = { left: 'left', start: 'left', center: 'center', right: 'right', justify: 'justify' };
121
+ const list = node.closest?.('ul,ol');
122
+ return {
123
+ ...base,
124
+ ...style,
125
+ align: alignMap[closeAlign(block)] || 'left',
126
+ lineSpacing: e.obj.props.lineSpacing || 1,
127
+ list: list ? (list.tagName === 'OL' ? 'number' : 'bullet') : '',
128
+ editing: true,
129
+ fontSize: parseFloat(cs.fontSize) || e.obj.props.fontSize || 24,
130
+ color: normalizeColor(cs.color) || e.obj.props.color || '',
131
+ };
132
+ }
133
+ const o = this.firstSelected();
134
+ if (!o) return base;
135
+ const p = o.props || {};
136
+ const defaultColor = themeOf(this.snapshot.theme).text;
137
+ return {
138
+ ...base,
139
+ bold: !!p.bold,
140
+ italic: !!p.italic,
141
+ underline: !!p.underline,
142
+ strike: !!p.strike,
143
+ font: p.font || '',
144
+ fontSize: p.fontSize || 0,
145
+ color: o.type === 'shape' ? (p.textColor || defaultColor) : (p.color || defaultColor),
146
+ align: p.align || 'left',
147
+ lineSpacing: p.lineSpacing || 1,
148
+ list: '',
149
+ editing: false,
150
+ };
151
+ }
152
+
153
+ function toggleTextStyle(key) {
154
+ const command = { bold: 'bold', italic: 'italic', underline: 'underline', strike: 'strikeThrough' }[key];
155
+ if (!command) return;
156
+ if (this.editing?.kind === 'text') {
157
+ this.canvas.focusEditing?.();
158
+ document.execCommand(command, false);
159
+ this.syncEditing();
160
+ this.ribbon.refresh();
161
+ return;
162
+ }
163
+ if (this.editing?.kind === 'shape') {
164
+ const o = this.editing.obj;
165
+ const value = !o.props[key];
166
+ o.props[key] = value;
167
+ if (key === 'bold') this.editing.node.style.fontWeight = value ? '700' : '400';
168
+ else if (key === 'italic') this.editing.node.style.fontStyle = value ? 'italic' : 'normal';
169
+ else {
170
+ this.editing.node.style.textDecoration = [o.props.underline ? 'underline' : '', o.props.strike ? 'line-through' : '']
171
+ .filter(Boolean).join(' ') || 'none';
172
+ }
173
+ this.noteEditChange();
174
+ this.ribbon.refresh();
175
+ return;
176
+ }
177
+ if (this.editing) return;
178
+ if (!this.canRichText()) return;
179
+ const objs = this.selectedObjects();
180
+ const first = objs[0];
181
+ const value = !first?.props?.[key];
182
+ this.pushHistory();
183
+ for (const o of objs) {
184
+ if (o.type === 'text' || o.type === 'shape') o.props[key] = value;
185
+ }
186
+ this.notifyChange();
187
+ }
188
+
189
+ function applyFontFamily(font) {
190
+ if (this.editing?.kind === 'text' && this.canvas.wrapSelection({ fontFamily: font })) {
191
+ this.ribbon.refresh();
192
+ return;
193
+ }
194
+ if (this.editing) {
195
+ const o = this.editing.obj;
196
+ if (this.editing.kind === 'cell') {
197
+ o.props.font = font;
198
+ this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => { td.style.fontFamily = font; });
199
+ } else {
200
+ for (const target of this.selectedObjects()) if (target.props) target.props.font = font;
201
+ this.editing.node.style.fontFamily = font;
202
+ }
203
+ this.noteEditChange();
204
+ this.ribbon.refresh();
205
+ return;
206
+ }
207
+ if (!this.canFormatText()) return;
208
+ this.pushHistory();
209
+ for (const o of this.selectedObjects()) {
210
+ if (o.props && o.type !== 'line') o.props.font = font;
211
+ }
212
+ this.notifyChange();
213
+ }
214
+
215
+ function applyFontSize(px) {
216
+ const value = clamp(Math.round(Number(px) || 0), 6, 200);
217
+ if (!value) return;
218
+ if (this.editing?.kind === 'text' && this.canvas.wrapSelection({ fontSize: `${value}px` })) {
219
+ this.ribbon.refresh();
220
+ return;
221
+ }
222
+ if (this.editing) {
223
+ const o = this.editing.obj;
224
+ if (this.editing.kind === 'cell') {
225
+ o.props.fontSize = value;
226
+ this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => { td.style.fontSize = `${value}px`; });
227
+ } else {
228
+ for (const target of this.selectedObjects()) {
229
+ if (target.type === 'text') setObjectFontSize(target, value);
230
+ else if (target.props) target.props.fontSize = value;
231
+ }
232
+ this.editing.node.style.fontSize = `${value}px`;
233
+ }
234
+ this.noteEditChange();
235
+ this.ribbon.refresh();
236
+ return;
237
+ }
238
+ if (!this.canFormatText()) return;
239
+ this.pushHistory();
240
+ for (const o of this.selectedObjects()) {
241
+ if (o.type === 'text') setObjectFontSize(o, value);
242
+ else if (o.type === 'shape' || o.type === 'table') o.props.fontSize = value;
243
+ }
244
+ this.notifyChange();
245
+ }
246
+
247
+ function setFontSize(px) { this.applyFontSize(px); }
248
+
249
+ function setFontFamily(font) { this.applyFontFamily(font); }
250
+
251
+ function stepFontSize(delta) {
252
+ const current = this.textState().fontSize || 24;
253
+ this.applyFontSize(current + (delta > 0 ? 2 : -2));
254
+ }
255
+
256
+ function applyTextColor(color) {
257
+ const themed = color || themeOf(this.snapshot.theme).text;
258
+ if (this.editing?.kind === 'text' && this.canvas.wrapSelection({ color: themed })) {
259
+ this.ribbon.refresh();
260
+ return;
261
+ }
262
+ if (this.editing) {
263
+ if (this.editing.kind === 'cell') {
264
+ this.editing.obj.props.color = color;
265
+ this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => { td.style.color = themed; });
266
+ } else {
267
+ for (const o of this.selectedObjects()) {
268
+ if (o.type === 'shape') o.props.textColor = themed;
269
+ else if (o.props && o.type !== 'line') o.props.color = color;
270
+ }
271
+ this.editing.node.style.color = themed;
272
+ }
273
+ this.noteEditChange();
274
+ this.ribbon.refresh();
275
+ return;
276
+ }
277
+ if (!this.canFormatText()) return;
278
+ this.pushHistory();
279
+ for (const o of this.selectedObjects()) {
280
+ if (o.type === 'shape') o.props.textColor = themed;
281
+ else if (o.props && o.type !== 'line') o.props.color = color;
282
+ }
283
+ this.notifyChange();
284
+ }
285
+
286
+ function applyAlign(align) {
287
+ if (this.editing?.kind === 'text') {
288
+ const command = { left: 'justifyLeft', center: 'justifyCenter', right: 'justifyRight', justify: 'justifyFull' }[align];
289
+ if (command) document.execCommand(command, false);
290
+ this.syncEditing();
291
+ this.ribbon.refresh();
292
+ return;
293
+ }
294
+ if (this.editing?.kind === 'shape') {
295
+ this.editing.obj.props.align = align;
296
+ this.editing.node.style.textAlign = align;
297
+ this.noteEditChange();
298
+ this.ribbon.refresh();
299
+ return;
300
+ }
301
+ if (this.editing) return;
302
+ if (!this.canAlignText()) return;
303
+ this.pushHistory();
304
+ for (const o of this.selectedObjects()) if (o.props) o.props.align = align;
305
+ this.notifyChange();
306
+ }
307
+
308
+ function applyList(kind) {
309
+ if (this.editing) {
310
+ if (this.editing.kind !== 'text') return;
311
+ document.execCommand(kind === 'bullet' ? 'insertUnorderedList' : 'insertOrderedList', false);
312
+ this.syncEditing();
313
+ this.ribbon.refresh();
314
+ return;
315
+ }
316
+ if (!this.canRichText()) return;
317
+ this.pushHistory();
318
+ for (const o of this.selectedObjects()) {
319
+ if (o.type !== 'text') continue;
320
+ const lines = htmlToPlain(o.props.html).split('\n').map((s) => s.trim()).filter(Boolean);
321
+ if (!lines.length) continue;
322
+ const tag = kind === 'bullet' ? 'ul' : 'ol';
323
+ o.props.html = `<${tag}>${lines.map((line) => `<li>${escapeHtml(line)}</li>`).join('')}</${tag}>`;
324
+ }
325
+ this.notifyChange();
326
+ }
327
+
328
+ function applyIndent(delta) {
329
+ if (!this.editing || this.editing.kind !== 'text') return;
330
+ // 只对列表项改层级:普通段落缩进不在对象模型里,做了也存不下来
331
+ if (!this.textState().list) return;
332
+ document.execCommand(delta > 0 ? 'indent' : 'outdent', false);
333
+ this.syncEditing();
334
+ this.ribbon.refresh();
335
+ }
336
+
337
+ function applyLineSpacing(v) {
338
+ const value = clamp(Number(v) || 1, 0.6, 4);
339
+ if (this.editing) {
340
+ if (this.editing.kind === 'cell') return;
341
+ for (const o of this.selectedObjects()) if (o.props && (o.type === 'text' || o.type === 'shape')) o.props.lineSpacing = value;
342
+ this.editing.node.style.lineHeight = String(value * 1.2);
343
+ this.noteEditChange();
344
+ this.ribbon.refresh();
345
+ return;
346
+ }
347
+ if (!this.canRichText()) return;
348
+ this.pushHistory();
349
+ for (const o of this.selectedObjects()) if (o.props && (o.type === 'text' || o.type === 'shape')) o.props.lineSpacing = value;
350
+ this.notifyChange();
351
+ }
352
+
353
+ function clearFormatting() {
354
+ if (this.editing?.kind === 'cell') {
355
+ const table = this.editing.obj;
356
+ Object.assign(table.props, { font: '', fontSize: 16, color: '' });
357
+ this.editing.node.closest('table')?.querySelectorAll('td').forEach((td) => {
358
+ td.style.fontFamily = '';
359
+ td.style.fontSize = '16px';
360
+ td.style.color = themeOf(this.snapshot.theme).text;
361
+ });
362
+ this.noteEditChange();
363
+ this.ribbon.refresh();
364
+ return;
365
+ }
366
+ if (this.editing?.kind === 'shape') {
367
+ const shape = this.editing.obj;
368
+ Object.assign(shape.props, {
369
+ font: '', fontSize: 18, textColor: '#ffffff', bold: false, italic: false,
370
+ underline: false, strike: false, align: 'center', lineSpacing: 1,
371
+ });
372
+ Object.assign(this.editing.node.style, {
373
+ fontFamily: '', fontSize: '18px', color: '#ffffff', fontWeight: '400',
374
+ fontStyle: 'normal', textDecoration: 'none', textAlign: 'center', lineHeight: '1.2',
375
+ });
376
+ this.noteEditChange();
377
+ this.ribbon.refresh();
378
+ return;
379
+ }
380
+ if (this.editing) {
381
+ this.canvas.focusEditing();
382
+ document.execCommand('removeFormat', false);
383
+ const o = this.editing.obj;
384
+ Object.assign(o.props, {
385
+ font: '', fontSize: 24, color: '', bold: false, italic: false,
386
+ underline: false, strike: false, lineSpacing: 1,
387
+ });
388
+ Object.assign(this.editing.node.style, {
389
+ fontFamily: '', fontSize: '24px', color: themeOf(this.snapshot.theme).text,
390
+ fontWeight: '400', fontStyle: 'normal', textDecoration: 'none', lineHeight: '1.2',
391
+ });
392
+ this.syncEditing();
393
+ this.ribbon.refresh();
394
+ return;
395
+ }
396
+ if (!this.selectedObjects().length) return;
397
+ this.pushHistory();
398
+ for (const o of this.selectedObjects()) {
399
+ if (o.type === 'text') {
400
+ const paras = htmlToParas(o.props.html);
401
+ for (const p of paras) {
402
+ for (const r of p.runs) {
403
+ r.bold = false; r.italic = false; r.underline = false; r.strike = false;
404
+ r.color = ''; r.font = ''; r.size = 0;
405
+ }
406
+ }
407
+ o.props.html = parasToHTML(paras);
408
+ Object.assign(o.props, { font: '', fontSize: 24, color: '', bold: false, italic: false, underline: false, strike: false, align: 'left', lineSpacing: 1 });
409
+ } else if (o.type === 'shape') {
410
+ Object.assign(o.props, { font: '', fontSize: 18, textColor: '#ffffff', bold: false, italic: false, underline: false, align: 'center', lineSpacing: 1, shadow: false });
411
+ } else if (o.type === 'table') {
412
+ Object.assign(o.props, { font: '', fontSize: 16, color: '', tableStyle: 'plain', headerRow: false, bandedRows: false });
413
+ }
414
+ }
415
+ this.notifyChange();
416
+ }
417
+
418
+ export const textMethods = { beginTextEdit, syncEditing, noteEditChange, commitEditing, startEditingById, editCellAt, canFormatText, canRichText, canAlignText, canList, textState, toggleTextStyle, applyFontFamily, applyFontSize, setFontSize, setFontFamily, stepFontSize, applyTextColor, applyAlign, applyList, applyIndent, applyLineSpacing, clearFormatting };