@a200888213/gbdocx 0.6.2 → 0.6.3

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.
@@ -1,113 +1,113 @@
1
- // [文件] web·editor-kernel·engine·search —— 查找替换:命中采集 + CSS Custom Highlight 高亮(零正文改写)
2
- // [设计] 高亮绝不能往正文里塞 `<mark>`:内核以 DOM 为文档模型,外来节点会被读进正文并随序列化落盘。
3
- // CSS.highlights 只登记 Range,样式靠 ::highlight() 伪元素画,编辑面 DOM 一个字节都不动。
4
- // [坑] Custom Highlight API 需 Chrome 105+ / Safari 17.2+ / Firefox 125+;不支持时只失去高亮,
5
- // 计数、跳转、替换仍全部可用(区间一样由 Range 驱动)。
6
- // [设计] 匹配只在单个文本块内,不跨块跨单元格:跨块命中无法用一个连续 Range 表达,
7
- // 且「一句话被段落切断」的匹配对用户没有意义。
8
-
9
- import { allTextBlocks, blockText, rangeInBlock } from './dom.js';
10
-
11
- const ALL = 'richtext-search';
12
- const CURRENT = 'richtext-search-current';
13
-
14
- const supported = () => typeof Highlight !== 'undefined' && typeof CSS !== 'undefined' && CSS.highlights;
15
-
16
- export function createSearch(root) {
17
- let keyword = '';
18
- let caseSensitive = false;
19
- let activeIndex = -1;
20
- let ranges = [];
21
-
22
- const collect = () => {
23
- ranges = [];
24
- if (!keyword) return;
25
- const needle = caseSensitive ? keyword : keyword.toLowerCase();
26
- for (const block of allTextBlocks(root)) {
27
- const text = blockText(block);
28
- const haystack = caseSensitive ? text : text.toLowerCase();
29
- let index = haystack.indexOf(needle);
30
- while (index !== -1) {
31
- ranges.push(rangeInBlock(block, index, index + needle.length));
32
- index = haystack.indexOf(needle, index + needle.length);
33
- }
34
- }
35
- activeIndex = ranges.length ? Math.min(Math.max(activeIndex, 0), ranges.length - 1) : -1;
36
- };
37
-
38
- const paint = () => {
39
- if (!supported()) return;
40
- CSS.highlights.delete(ALL);
41
- CSS.highlights.delete(CURRENT);
42
- if (!ranges.length) return;
43
- CSS.highlights.set(ALL, new Highlight(...ranges));
44
- if (activeIndex >= 0 && ranges[activeIndex]) CSS.highlights.set(CURRENT, new Highlight(ranges[activeIndex]));
45
- };
46
-
47
- /** 正文变化后重算命中(编辑器 update 时调用) */
48
- const refresh = () => {
49
- collect();
50
- paint();
51
- };
52
-
53
- const setKeyword = (value, options = {}) => {
54
- keyword = String(value ?? '');
55
- if (options.caseSensitive !== undefined) caseSensitive = Boolean(options.caseSensitive);
56
- activeIndex = keyword ? 0 : -1;
57
- refresh();
58
- return snapshot();
59
- };
60
-
61
- const clear = () => {
62
- keyword = '';
63
- activeIndex = -1;
64
- ranges = [];
65
- if (supported()) {
66
- CSS.highlights.delete(ALL);
67
- CSS.highlights.delete(CURRENT);
68
- }
69
- };
70
-
71
- const step = (dir) => {
72
- if (!ranges.length) return false;
73
- activeIndex = (activeIndex + dir + ranges.length) % ranges.length;
74
- paint();
75
- scrollToActive();
76
- return true;
77
- };
78
-
79
- const snapshot = () => ({ keyword, total: ranges.length, activeIndex });
80
-
81
- const replaceAt = (index, text) => {
82
- const range = ranges[index];
83
- if (!range) return false;
84
- const node = document.createTextNode(String(text ?? ''));
85
- range.deleteContents();
86
- range.insertNode(node);
87
- return true;
88
- };
89
-
90
- /** 替换当前命中并保持停在同一序号(下一条自动落在原位置之后) */
91
- const replaceCurrent = (text) => {
92
- if (!replaceAt(activeIndex, text)) return false;
93
- refresh();
94
- return true;
95
- };
96
-
97
- /** 全部替换:倒序改文,前面的偏移不受后面改动影响 */
98
- const replaceAll = (text) => {
99
- if (!ranges.length) return false;
100
- for (let i = ranges.length - 1; i >= 0; i -= 1) replaceAt(i, text);
101
- refresh();
102
- return true;
103
- };
104
-
105
- function scrollToActive() {
106
- const range = ranges[activeIndex];
107
- if (!range) return;
108
- const node = range.startContainer.nodeType === Node.TEXT_NODE ? range.startContainer.parentElement : range.startContainer;
109
- node?.scrollIntoView?.({ block: 'center', behavior: 'smooth' });
110
- }
111
-
112
- return { refresh, setKeyword, clear, snapshot, next: () => step(1), prev: () => step(-1), replaceCurrent, replaceAll, scrollToActive };
113
- }
1
+ // [文件] web·editor-kernel·engine·search —— 查找替换:命中采集 + CSS Custom Highlight 高亮(零正文改写)
2
+ // [设计] 高亮绝不能往正文里塞 `<mark>`:内核以 DOM 为文档模型,外来节点会被读进正文并随序列化落盘。
3
+ // CSS.highlights 只登记 Range,样式靠 ::highlight() 伪元素画,编辑面 DOM 一个字节都不动。
4
+ // [坑] Custom Highlight API 需 Chrome 105+ / Safari 17.2+ / Firefox 125+;不支持时只失去高亮,
5
+ // 计数、跳转、替换仍全部可用(区间一样由 Range 驱动)。
6
+ // [设计] 匹配只在单个文本块内,不跨块跨单元格:跨块命中无法用一个连续 Range 表达,
7
+ // 且「一句话被段落切断」的匹配对用户没有意义。
8
+
9
+ import { allTextBlocks, blockText, rangeInBlock } from './dom.js';
10
+
11
+ const ALL = 'richtext-search';
12
+ const CURRENT = 'richtext-search-current';
13
+
14
+ const supported = () => typeof Highlight !== 'undefined' && typeof CSS !== 'undefined' && CSS.highlights;
15
+
16
+ export function createSearch(root) {
17
+ let keyword = '';
18
+ let caseSensitive = false;
19
+ let activeIndex = -1;
20
+ let ranges = [];
21
+
22
+ const collect = () => {
23
+ ranges = [];
24
+ if (!keyword) return;
25
+ const needle = caseSensitive ? keyword : keyword.toLowerCase();
26
+ for (const block of allTextBlocks(root)) {
27
+ const text = blockText(block);
28
+ const haystack = caseSensitive ? text : text.toLowerCase();
29
+ let index = haystack.indexOf(needle);
30
+ while (index !== -1) {
31
+ ranges.push(rangeInBlock(block, index, index + needle.length));
32
+ index = haystack.indexOf(needle, index + needle.length);
33
+ }
34
+ }
35
+ activeIndex = ranges.length ? Math.min(Math.max(activeIndex, 0), ranges.length - 1) : -1;
36
+ };
37
+
38
+ const paint = () => {
39
+ if (!supported()) return;
40
+ CSS.highlights.delete(ALL);
41
+ CSS.highlights.delete(CURRENT);
42
+ if (!ranges.length) return;
43
+ CSS.highlights.set(ALL, new Highlight(...ranges));
44
+ if (activeIndex >= 0 && ranges[activeIndex]) CSS.highlights.set(CURRENT, new Highlight(ranges[activeIndex]));
45
+ };
46
+
47
+ /** 正文变化后重算命中(编辑器 update 时调用) */
48
+ const refresh = () => {
49
+ collect();
50
+ paint();
51
+ };
52
+
53
+ const setKeyword = (value, options = {}) => {
54
+ keyword = String(value ?? '');
55
+ if (options.caseSensitive !== undefined) caseSensitive = Boolean(options.caseSensitive);
56
+ activeIndex = keyword ? 0 : -1;
57
+ refresh();
58
+ return snapshot();
59
+ };
60
+
61
+ const clear = () => {
62
+ keyword = '';
63
+ activeIndex = -1;
64
+ ranges = [];
65
+ if (supported()) {
66
+ CSS.highlights.delete(ALL);
67
+ CSS.highlights.delete(CURRENT);
68
+ }
69
+ };
70
+
71
+ const step = (dir) => {
72
+ if (!ranges.length) return false;
73
+ activeIndex = (activeIndex + dir + ranges.length) % ranges.length;
74
+ paint();
75
+ scrollToActive();
76
+ return true;
77
+ };
78
+
79
+ const snapshot = () => ({ keyword, total: ranges.length, activeIndex });
80
+
81
+ const replaceAt = (index, text) => {
82
+ const range = ranges[index];
83
+ if (!range) return false;
84
+ const node = document.createTextNode(String(text ?? ''));
85
+ range.deleteContents();
86
+ range.insertNode(node);
87
+ return true;
88
+ };
89
+
90
+ /** 替换当前命中并保持停在同一序号(下一条自动落在原位置之后) */
91
+ const replaceCurrent = (text) => {
92
+ if (!replaceAt(activeIndex, text)) return false;
93
+ refresh();
94
+ return true;
95
+ };
96
+
97
+ /** 全部替换:倒序改文,前面的偏移不受后面改动影响 */
98
+ const replaceAll = (text) => {
99
+ if (!ranges.length) return false;
100
+ for (let i = ranges.length - 1; i >= 0; i -= 1) replaceAt(i, text);
101
+ refresh();
102
+ return true;
103
+ };
104
+
105
+ function scrollToActive() {
106
+ const range = ranges[activeIndex];
107
+ if (!range) return;
108
+ const node = range.startContainer.nodeType === Node.TEXT_NODE ? range.startContainer.parentElement : range.startContainer;
109
+ node?.scrollIntoView?.({ block: 'center', behavior: 'smooth' });
110
+ }
111
+
112
+ return { refresh, setKeyword, clear, snapshot, next: () => step(1), prev: () => step(-1), replaceCurrent, replaceAll, scrollToActive };
113
+ }