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,183 +1,183 @@
1
- // [文件] gbpptx·slides·render —— 选择与渲染 —— 当前页/选区读取、整体重绘、缩略图与阅读视图
2
-
3
- // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { clamp } from 'guabiutils';
7
- import { createSlide } from '../../../src/kernel/model.js';
8
- import { renderPage } from '../ui/page-view.js';
9
- import { icon } from '../ui/icons.js';
10
- import { el } from './shared.js';
11
- function currentSlide() {
12
- return this.snapshot.slides[this.index] || this.snapshot.slides[0] || null;
13
- }
14
-
15
- function selectedObjects() {
16
- const slide = this.currentSlide();
17
- if (!slide) return [];
18
- return slide.objects.filter((o) => this.selectedIds.includes(o.id));
19
- }
20
-
21
- function firstSelected() {
22
- return this.selectedObjects()[0] || null;
23
- }
24
-
25
- function selectedById(id) {
26
- return this.currentSlide()?.objects.find((o) => o.id === id) || null;
27
- }
28
-
29
- function renderAll() {
30
- if (!this.snapshot.slides.length) this.snapshot.slides.push(createSlide());
31
- this.index = clamp(this.index, 0, this.snapshot.slides.length - 1);
32
- this.updateLayout();
33
- if (this.view === 'normal') {
34
- this.canvas.render();
35
- this.renderNotes();
36
- } else if (this.view === 'sorter') {
37
- this.renderSorter();
38
- } else if (this.view === 'reading') {
39
- this.renderReading();
40
- }
41
- this.slidePane.render();
42
- this.formatPane.render();
43
- this.ribbon.refresh();
44
- this.titlebar.refresh();
45
- this.statusbar.refresh();
46
- }
47
-
48
- function updateLayout() {
49
- const normal = this.view === 'normal';
50
- this.slidePane.el.hidden = !normal;
51
- this.canvasHost.hidden = !normal;
52
- this.canvas.el.hidden = !normal;
53
- this.notesEl.hidden = !normal || !this.notesVisible;
54
- this.sorterEl.hidden = this.view !== 'sorter';
55
- this.readingEl.hidden = this.view !== 'reading';
56
- this.root.dataset.view = this.view;
57
- this.root.classList.toggle('has-pane', normal && this.formatPaneOpen);
58
- this.root.classList.toggle('has-notes', normal && this.notesVisible);
59
- if (!normal || !this.formatPaneOpen) this.formatPane.el.hidden = true;
60
- else if (this.formatPaneOpen) this.formatPane.el.hidden = false;
61
- }
62
-
63
- function renderNotes() {
64
- const slide = this.currentSlide();
65
- if (this.notesText && document.activeElement !== this.notesText) this.notesText.value = slide?.notes || '';
66
- }
67
-
68
- function renderSorter() {
69
- const size = this.size;
70
- const theme = this.snapshot.theme;
71
- const width = 248;
72
- this.sorterEl.replaceChildren();
73
- this.snapshot.slides.forEach((slide, i) => {
74
- const card = el('div', `pp-sorter-card${i === this.index ? ' gbp-is-active' : ''}${slide.hidden ? ' gbp-is-hidden' : ''}`);
75
- card.dataset.index = String(i);
76
- card.draggable = true;
77
- const no = el('div', 'pp-sorter-card__no', String(i + 1));
78
- const box = el('div', 'pp-sorter-card__box');
79
- box.style.height = `${size.h * (width / size.w)}px`;
80
- const page = renderPage(slide, theme, { size });
81
- page.style.transform = `scale(${width / size.w})`;
82
- page.style.transformOrigin = 'top left';
83
- box.appendChild(page);
84
- card.append(no, box);
85
- if (slide.hidden) {
86
- const flag = el('span', 'pp-sorter-card__flag');
87
- flag.innerHTML = icon('hide', 14);
88
- card.appendChild(flag);
89
- }
90
- this.sorterEl.appendChild(card);
91
- });
92
- }
93
-
94
- function renderReading() {
95
- const size = this.size;
96
- const slide = this.currentSlide();
97
- this.readingEl.replaceChildren();
98
- if (!slide) return;
99
- const availW = Math.max(320, (this.readingEl.clientWidth || window.innerWidth) - 120);
100
- const availH = Math.max(240, (this.readingEl.clientHeight || window.innerHeight) - 130);
101
- const scale = Math.min(availW / size.w, availH / size.h);
102
- const stage = el('div', 'pp-reading__stage');
103
- stage.style.width = `${size.w * scale}px`;
104
- stage.style.height = `${size.h * scale}px`;
105
- const page = renderPage(slide, this.snapshot.theme, { size });
106
- page.style.transform = `scale(${scale})`;
107
- page.style.transformOrigin = 'top left';
108
- stage.appendChild(page);
109
- const prev = el('button', 'pp-reading__nav pp-reading__nav--prev');
110
- prev.type = 'button';
111
- prev.title = '上一页';
112
- prev.innerHTML = icon('chevronLeft', 24);
113
- prev.disabled = this.index === 0;
114
- prev.addEventListener('click', () => this.selectSlide(this.index - 1));
115
- const next = el('button', 'pp-reading__nav pp-reading__nav--next');
116
- next.type = 'button';
117
- next.title = '下一页';
118
- next.innerHTML = icon('chevronRight', 24);
119
- next.disabled = this.index >= this.snapshot.slides.length - 1;
120
- next.addEventListener('click', () => this.selectSlide(this.index + 1));
121
- const bar = el('div', 'pp-reading__bar');
122
- const counter = el('span', '', `${this.index + 1} / ${this.snapshot.slides.length}`);
123
- const quit = el('button', 'pp-reading__quit', '关闭阅读视图 (Esc)');
124
- quit.type = 'button';
125
- quit.addEventListener('click', () => this.setView('normal'));
126
- bar.append(counter, quit);
127
- this.readingEl.append(prev, stage, next, bar);
128
- }
129
-
130
- function selectObjects(ids) {
131
- const next = [...new Set(ids)].filter((id) => this.currentSlide()?.objects.some((o) => o.id === id));
132
- if (this.editing && !next.includes(this.editing.id)) this.commitEditing({ render: false });
133
- this.selectedIds = next;
134
- this.canvas.syncSelection();
135
- this.formatPane.render();
136
- this.ribbon.refresh();
137
- this.statusbar.refresh();
138
- }
139
-
140
- function toggleSelect(id) {
141
- const set = new Set(this.selectedIds);
142
- if (set.has(id)) set.delete(id);
143
- else set.add(id);
144
- this.selectObjects([...set]);
145
- }
146
-
147
- function clearSelection() {
148
- if (this.editing) this.commitEditing({ render: false });
149
- if (!this.selectedIds.length) return;
150
- this.selectedIds = [];
151
- this.canvas.syncSelection();
152
- this.formatPane.render();
153
- this.ribbon.refresh();
154
- this.statusbar.refresh();
155
- }
156
-
157
- function selectAll() {
158
- const slide = this.currentSlide();
159
- if (!slide) return;
160
- this.selectObjects(slide.objects.map((o) => o.id));
161
- }
162
-
163
- function selectSlide(i) {
164
- this.commitEditing({ render: false });
165
- const next = clamp(Number(i) || 0, 0, this.snapshot.slides.length - 1);
166
- this.index = next;
167
- this.selectedIds = [];
168
- this.tableCursor = null;
169
- this.notesSession = false;
170
- this.renderAll();
171
- }
172
-
173
- function onSelectionChange() {
174
- const e = this.editing;
175
- if (!e?.node) return;
176
- const sel = window.getSelection();
177
- if (!sel?.rangeCount) return;
178
- if (e.node.contains(sel.anchorNode) && e.node.contains(sel.focusNode)) {
179
- e.range = sel.getRangeAt(0).cloneRange();
180
- }
181
- }
182
-
183
- export const renderMethods = { currentSlide, selectedObjects, firstSelected, selectedById, renderAll, updateLayout, renderNotes, renderSorter, renderReading, selectObjects, toggleSelect, clearSelection, selectAll, selectSlide, onSelectionChange };
1
+ // [文件] gbpptx·slides·render —— 选择与渲染 —— 当前页/选区读取、整体重绘、缩略图与阅读视图
2
+
3
+ // [设计] 方法以独立函数导出,由 slides-app.js 末尾 Object.assign 装配到 SlidesApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { clamp } from 'guabiutils';
7
+ import { createSlide } from '../../../src/kernel/model.js';
8
+ import { renderPage } from '../ui/page-view.js';
9
+ import { icon } from '../ui/icons.js';
10
+ import { el } from './shared.js';
11
+ function currentSlide() {
12
+ return this.snapshot.slides[this.index] || this.snapshot.slides[0] || null;
13
+ }
14
+
15
+ function selectedObjects() {
16
+ const slide = this.currentSlide();
17
+ if (!slide) return [];
18
+ return slide.objects.filter((o) => this.selectedIds.includes(o.id));
19
+ }
20
+
21
+ function firstSelected() {
22
+ return this.selectedObjects()[0] || null;
23
+ }
24
+
25
+ function selectedById(id) {
26
+ return this.currentSlide()?.objects.find((o) => o.id === id) || null;
27
+ }
28
+
29
+ function renderAll() {
30
+ if (!this.snapshot.slides.length) this.snapshot.slides.push(createSlide());
31
+ this.index = clamp(this.index, 0, this.snapshot.slides.length - 1);
32
+ this.updateLayout();
33
+ if (this.view === 'normal') {
34
+ this.canvas.render();
35
+ this.renderNotes();
36
+ } else if (this.view === 'sorter') {
37
+ this.renderSorter();
38
+ } else if (this.view === 'reading') {
39
+ this.renderReading();
40
+ }
41
+ this.slidePane.render();
42
+ this.formatPane.render();
43
+ this.ribbon.refresh();
44
+ this.titlebar.refresh();
45
+ this.statusbar.refresh();
46
+ }
47
+
48
+ function updateLayout() {
49
+ const normal = this.view === 'normal';
50
+ this.slidePane.el.hidden = !normal;
51
+ this.canvasHost.hidden = !normal;
52
+ this.canvas.el.hidden = !normal;
53
+ this.notesEl.hidden = !normal || !this.notesVisible;
54
+ this.sorterEl.hidden = this.view !== 'sorter';
55
+ this.readingEl.hidden = this.view !== 'reading';
56
+ this.root.dataset.view = this.view;
57
+ this.root.classList.toggle('has-pane', normal && this.formatPaneOpen);
58
+ this.root.classList.toggle('has-notes', normal && this.notesVisible);
59
+ if (!normal || !this.formatPaneOpen) this.formatPane.el.hidden = true;
60
+ else if (this.formatPaneOpen) this.formatPane.el.hidden = false;
61
+ }
62
+
63
+ function renderNotes() {
64
+ const slide = this.currentSlide();
65
+ if (this.notesText && document.activeElement !== this.notesText) this.notesText.value = slide?.notes || '';
66
+ }
67
+
68
+ function renderSorter() {
69
+ const size = this.size;
70
+ const theme = this.snapshot.theme;
71
+ const width = 248;
72
+ this.sorterEl.replaceChildren();
73
+ this.snapshot.slides.forEach((slide, i) => {
74
+ const card = el('div', `pp-sorter-card${i === this.index ? ' gbp-is-active' : ''}${slide.hidden ? ' gbp-is-hidden' : ''}`);
75
+ card.dataset.index = String(i);
76
+ card.draggable = true;
77
+ const no = el('div', 'pp-sorter-card__no', String(i + 1));
78
+ const box = el('div', 'pp-sorter-card__box');
79
+ box.style.height = `${size.h * (width / size.w)}px`;
80
+ const page = renderPage(slide, theme, { size });
81
+ page.style.transform = `scale(${width / size.w})`;
82
+ page.style.transformOrigin = 'top left';
83
+ box.appendChild(page);
84
+ card.append(no, box);
85
+ if (slide.hidden) {
86
+ const flag = el('span', 'pp-sorter-card__flag');
87
+ flag.innerHTML = icon('hide', 14);
88
+ card.appendChild(flag);
89
+ }
90
+ this.sorterEl.appendChild(card);
91
+ });
92
+ }
93
+
94
+ function renderReading() {
95
+ const size = this.size;
96
+ const slide = this.currentSlide();
97
+ this.readingEl.replaceChildren();
98
+ if (!slide) return;
99
+ const availW = Math.max(320, (this.readingEl.clientWidth || window.innerWidth) - 120);
100
+ const availH = Math.max(240, (this.readingEl.clientHeight || window.innerHeight) - 130);
101
+ const scale = Math.min(availW / size.w, availH / size.h);
102
+ const stage = el('div', 'pp-reading__stage');
103
+ stage.style.width = `${size.w * scale}px`;
104
+ stage.style.height = `${size.h * scale}px`;
105
+ const page = renderPage(slide, this.snapshot.theme, { size });
106
+ page.style.transform = `scale(${scale})`;
107
+ page.style.transformOrigin = 'top left';
108
+ stage.appendChild(page);
109
+ const prev = el('button', 'pp-reading__nav pp-reading__nav--prev');
110
+ prev.type = 'button';
111
+ prev.title = '上一页';
112
+ prev.innerHTML = icon('chevronLeft', 24);
113
+ prev.disabled = this.index === 0;
114
+ prev.addEventListener('click', () => this.selectSlide(this.index - 1));
115
+ const next = el('button', 'pp-reading__nav pp-reading__nav--next');
116
+ next.type = 'button';
117
+ next.title = '下一页';
118
+ next.innerHTML = icon('chevronRight', 24);
119
+ next.disabled = this.index >= this.snapshot.slides.length - 1;
120
+ next.addEventListener('click', () => this.selectSlide(this.index + 1));
121
+ const bar = el('div', 'pp-reading__bar');
122
+ const counter = el('span', '', `${this.index + 1} / ${this.snapshot.slides.length}`);
123
+ const quit = el('button', 'pp-reading__quit', '关闭阅读视图 (Esc)');
124
+ quit.type = 'button';
125
+ quit.addEventListener('click', () => this.setView('normal'));
126
+ bar.append(counter, quit);
127
+ this.readingEl.append(prev, stage, next, bar);
128
+ }
129
+
130
+ function selectObjects(ids) {
131
+ const next = [...new Set(ids)].filter((id) => this.currentSlide()?.objects.some((o) => o.id === id));
132
+ if (this.editing && !next.includes(this.editing.id)) this.commitEditing({ render: false });
133
+ this.selectedIds = next;
134
+ this.canvas.syncSelection();
135
+ this.formatPane.render();
136
+ this.ribbon.refresh();
137
+ this.statusbar.refresh();
138
+ }
139
+
140
+ function toggleSelect(id) {
141
+ const set = new Set(this.selectedIds);
142
+ if (set.has(id)) set.delete(id);
143
+ else set.add(id);
144
+ this.selectObjects([...set]);
145
+ }
146
+
147
+ function clearSelection() {
148
+ if (this.editing) this.commitEditing({ render: false });
149
+ if (!this.selectedIds.length) return;
150
+ this.selectedIds = [];
151
+ this.canvas.syncSelection();
152
+ this.formatPane.render();
153
+ this.ribbon.refresh();
154
+ this.statusbar.refresh();
155
+ }
156
+
157
+ function selectAll() {
158
+ const slide = this.currentSlide();
159
+ if (!slide) return;
160
+ this.selectObjects(slide.objects.map((o) => o.id));
161
+ }
162
+
163
+ function selectSlide(i) {
164
+ this.commitEditing({ render: false });
165
+ const next = clamp(Number(i) || 0, 0, this.snapshot.slides.length - 1);
166
+ this.index = next;
167
+ this.selectedIds = [];
168
+ this.tableCursor = null;
169
+ this.notesSession = false;
170
+ this.renderAll();
171
+ }
172
+
173
+ function onSelectionChange() {
174
+ const e = this.editing;
175
+ if (!e?.node) return;
176
+ const sel = window.getSelection();
177
+ if (!sel?.rangeCount) return;
178
+ if (e.node.contains(sel.anchorNode) && e.node.contains(sel.focusNode)) {
179
+ e.range = sel.getRangeAt(0).cloneRange();
180
+ }
181
+ }
182
+
183
+ export const renderMethods = { currentSlide, selectedObjects, firstSelected, selectedById, renderAll, updateLayout, renderNotes, renderSorter, renderReading, selectObjects, toggleSelect, clearSelection, selectAll, selectSlide, onSelectionChange };
@@ -1,85 +1,85 @@
1
- // [文件] gbpptx·slides·shared —— 编辑器常量与无状态工具(el / 偏好读写 / 行内样式读取 / 查找替换文本工具)
2
- // [设计] 这些函数不碰 this,装配前后都是模块级函数;转义与颜色归一一律走 guabiutils,不重复实现。
3
-
4
- import { escapeRegExp, normalizeColor } from 'guabiutils';
5
-
6
- export const LS_KEY = 'gbpptx.office.prefs';
7
- export const HISTORY_LIMIT = 100;
8
- export const AUTOSAVE_MS = 2600;
9
- export const ZOOM_MIN = 20;
10
- export const ZOOM_MAX = 400;
11
-
12
- export const el = (tag, cls, text) => {
13
- const node = document.createElement(tag);
14
- if (cls) node.className = cls;
15
- if (text != null) node.textContent = text;
16
- return node;
17
- };
18
-
19
- export function loadPrefs() {
20
- try { return JSON.parse(localStorage.getItem(LS_KEY) || '{}') || {}; } catch { return {}; }
21
- }
22
-
23
- export function savePrefs(prefs) {
24
- try { localStorage.setItem(LS_KEY, JSON.stringify(prefs)); } catch { /* 忽略存储失败 */ }
25
- }
26
-
27
- export function styleFromNode(node, root) {
28
- let bold = false;
29
- let italic = false;
30
- let underline = false;
31
- let strike = false;
32
- let font = '';
33
- let size = 0;
34
- let color = '';
35
- let el = node;
36
- while (el && el !== root.parentElement) {
37
- const tag = el.tagName;
38
- if (tag === 'B' || tag === 'STRONG') bold = true;
39
- if (tag === 'I' || tag === 'EM') italic = true;
40
- if (tag === 'U') underline = true;
41
- if (tag === 'S' || tag === 'STRIKE' || tag === 'DEL') strike = true;
42
- const cs = window.getComputedStyle(el);
43
- if (cs.fontWeight === 'bold' || Number(cs.fontWeight) >= 600) bold = true;
44
- if (cs.fontStyle === 'italic') italic = true;
45
- if (cs.textDecorationLine?.includes('underline') || cs.textDecoration?.includes('underline')) underline = true;
46
- if (cs.textDecorationLine?.includes('line-through') || cs.textDecoration?.includes('line-through')) strike = true;
47
- if (!font) font = cs.fontFamily || '';
48
- if (!size) size = parseFloat(cs.fontSize) || 0;
49
- if (!color) color = normalizeColor(cs.color);
50
- if (el === root) break;
51
- el = el.parentElement;
52
- }
53
- return { bold, italic, underline, strike, font, fontSize: size, color };
54
- }
55
-
56
- export function closestBlock(node, root) {
57
- let el = node;
58
- while (el && el !== root) {
59
- if (['DIV', 'P', 'LI', 'H1', 'H2', 'H3', 'BLOCKQUOTE'].includes(el.tagName)) return el;
60
- el = el.parentElement;
61
- }
62
- return root;
63
- }
64
-
65
- export function closeAlign(block) {
66
- const align = window.getComputedStyle(block).textAlign;
67
- return align || 'left';
68
- }
69
-
70
- export function replaceInHtml(html, query, replacement, all = false) {
71
- if (!html || !query) return html;
72
- const doc = new DOMParser().parseFromString(`<body>${html}</body>`, 'text/html');
73
- const re = new RegExp(escapeRegExp(query), all ? 'gi' : 'i');
74
- const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_TEXT);
75
- const nodes = [];
76
- let n = walker.nextNode();
77
- while (n) { nodes.push(n); n = walker.nextNode(); }
78
- for (const node of nodes) node.data = node.data.replace(re, () => replacement);
79
- return doc.body.innerHTML;
80
- }
81
-
82
- export function replaceInPlain(text, query, replacement, all = false) {
83
- if (!text || !query) return text;
84
- return String(text).replace(new RegExp(escapeRegExp(query), all ? 'gi' : 'i'), () => replacement);
85
- }
1
+ // [文件] gbpptx·slides·shared —— 编辑器常量与无状态工具(el / 偏好读写 / 行内样式读取 / 查找替换文本工具)
2
+ // [设计] 这些函数不碰 this,装配前后都是模块级函数;转义与颜色归一一律走 guabiutils,不重复实现。
3
+
4
+ import { escapeRegExp, normalizeColor } from 'guabiutils';
5
+
6
+ export const LS_KEY = 'gbpptx.office.prefs';
7
+ export const HISTORY_LIMIT = 100;
8
+ export const AUTOSAVE_MS = 2600;
9
+ export const ZOOM_MIN = 20;
10
+ export const ZOOM_MAX = 400;
11
+
12
+ export const el = (tag, cls, text) => {
13
+ const node = document.createElement(tag);
14
+ if (cls) node.className = cls;
15
+ if (text != null) node.textContent = text;
16
+ return node;
17
+ };
18
+
19
+ export function loadPrefs() {
20
+ try { return JSON.parse(localStorage.getItem(LS_KEY) || '{}') || {}; } catch { return {}; }
21
+ }
22
+
23
+ export function savePrefs(prefs) {
24
+ try { localStorage.setItem(LS_KEY, JSON.stringify(prefs)); } catch { /* 忽略存储失败 */ }
25
+ }
26
+
27
+ export function styleFromNode(node, root) {
28
+ let bold = false;
29
+ let italic = false;
30
+ let underline = false;
31
+ let strike = false;
32
+ let font = '';
33
+ let size = 0;
34
+ let color = '';
35
+ let el = node;
36
+ while (el && el !== root.parentElement) {
37
+ const tag = el.tagName;
38
+ if (tag === 'B' || tag === 'STRONG') bold = true;
39
+ if (tag === 'I' || tag === 'EM') italic = true;
40
+ if (tag === 'U') underline = true;
41
+ if (tag === 'S' || tag === 'STRIKE' || tag === 'DEL') strike = true;
42
+ const cs = window.getComputedStyle(el);
43
+ if (cs.fontWeight === 'bold' || Number(cs.fontWeight) >= 600) bold = true;
44
+ if (cs.fontStyle === 'italic') italic = true;
45
+ if (cs.textDecorationLine?.includes('underline') || cs.textDecoration?.includes('underline')) underline = true;
46
+ if (cs.textDecorationLine?.includes('line-through') || cs.textDecoration?.includes('line-through')) strike = true;
47
+ if (!font) font = cs.fontFamily || '';
48
+ if (!size) size = parseFloat(cs.fontSize) || 0;
49
+ if (!color) color = normalizeColor(cs.color);
50
+ if (el === root) break;
51
+ el = el.parentElement;
52
+ }
53
+ return { bold, italic, underline, strike, font, fontSize: size, color };
54
+ }
55
+
56
+ export function closestBlock(node, root) {
57
+ let el = node;
58
+ while (el && el !== root) {
59
+ if (['DIV', 'P', 'LI', 'H1', 'H2', 'H3', 'BLOCKQUOTE'].includes(el.tagName)) return el;
60
+ el = el.parentElement;
61
+ }
62
+ return root;
63
+ }
64
+
65
+ export function closeAlign(block) {
66
+ const align = window.getComputedStyle(block).textAlign;
67
+ return align || 'left';
68
+ }
69
+
70
+ export function replaceInHtml(html, query, replacement, all = false) {
71
+ if (!html || !query) return html;
72
+ const doc = new DOMParser().parseFromString(`<body>${html}</body>`, 'text/html');
73
+ const re = new RegExp(escapeRegExp(query), all ? 'gi' : 'i');
74
+ const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_TEXT);
75
+ const nodes = [];
76
+ let n = walker.nextNode();
77
+ while (n) { nodes.push(n); n = walker.nextNode(); }
78
+ for (const node of nodes) node.data = node.data.replace(re, () => replacement);
79
+ return doc.body.innerHTML;
80
+ }
81
+
82
+ export function replaceInPlain(text, query, replacement, all = false) {
83
+ if (!text || !query) return text;
84
+ return String(text).replace(new RegExp(escapeRegExp(query), all ? 'gi' : 'i'), () => replacement);
85
+ }