@minnalstack/editor-plugin-find 0.0.0-stage → 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aranghat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,17 @@
1
- # Temporary Holding Version
1
+ # @minnalstack/editor-plugin-find
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ `FindPlugin` adds find and replace, in a panel of Aranghat's components before the document, with a button on the toolbar.
4
+
5
+ ```ts
6
+ import { FindPlugin } from '@minnalstack/editor-plugin-find';
7
+
8
+ builder.plugins.add(new FindPlugin());
9
+ ```
10
+
11
+ - **Keys.** Mod-f opens find, looking for the selected text. Mod-h, or Mod-Shift-h on a Mac, where the system takes Mod-h, opens find and replace. In the panel, Enter and Shift-Enter go to the next and previous match, Enter in the replacement replaces one, and Escape closes it. Mod-g and Mod-Shift-g work in the text too.
12
+ - **Matching.** Each block's text is searched on its own, so a match never runs from one block into the next. Match case and whole word are options. A search finds at most 1,000 matches, and the count says when there are more.
13
+ - **Painting.** Matches are painted with the CSS Custom Highlight API, as `::highlight(mn-find)`, and the current one as `::highlight(mn-find-current)`, so the editor's elements don't change. The editor's stylesheet colours them from Aranghat's tokens. In a browser without the API, moving to a match selects it.
14
+ - **Replacing.** Replace all is one edit, and one press of undo. New text takes the formats of the first character it replaces.
15
+ - **Read-only.** A read-only editor offers find, from its keys while text in it is selected, and never replace.
16
+
17
+ It needs the editor on a page. The commands, such as `OpenFind`, `FindNext` and `ReplaceAllMatches`, are for an application that draws its own toolbar.
@@ -0,0 +1,15 @@
1
+ /** Opens find, with the keyboard in it. Selected text in one block becomes what to look for. */
2
+ export declare const OpenFind: import("@minnalstack/editor-core").Command<void>;
3
+ /** Opens find with replacing offered too. */
4
+ export declare const OpenFindAndReplace: import("@minnalstack/editor-core").Command<void>;
5
+ /** Moves to the next match, and from the last back to the first. */
6
+ export declare const FindNext: import("@minnalstack/editor-core").Command<void>;
7
+ /** Moves to the previous match, and from the first back to the last. */
8
+ export declare const FindPrevious: import("@minnalstack/editor-core").Command<void>;
9
+ /** Replaces the current match, keeping its formats, and moves to the next. It can run only while replacing is offered. */
10
+ export declare const ReplaceMatch: import("@minnalstack/editor-core").Command<void>;
11
+ /** Replaces every match, as one undo step. It can run only while replacing is offered. */
12
+ export declare const ReplaceAllMatches: import("@minnalstack/editor-core").Command<void>;
13
+ /** Closes find and takes its highlights away. From the panel, the current match becomes the selection and the keyboard goes back to the text. */
14
+ export declare const CloseFind: import("@minnalstack/editor-core").Command<void>;
15
+ //# sourceMappingURL=commands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.d.ts","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":"AAEA,gGAAgG;AAChG,eAAO,MAAM,QAAQ,kDAAoI,CAAC;AAC1J,6CAA6C;AAC7C,eAAO,MAAM,kBAAkB,kDAA+E,CAAC;AAC/G,oEAAoE;AACpE,eAAO,MAAM,QAAQ,kDAAsI,CAAC;AAC5J,wEAAwE;AACxE,eAAO,MAAM,YAAY,kDAA8I,CAAC;AACxK,0HAA0H;AAC1H,eAAO,MAAM,YAAY,kDAAqG,CAAC;AAC/H,0FAA0F;AAC1F,eAAO,MAAM,iBAAiB,kDAA8F,CAAC;AAC7H,iJAAiJ;AACjJ,eAAO,MAAM,SAAS,kDAAoH,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { defineCommand } from '@minnalstack/editor-core';
2
+ /** Opens find, with the keyboard in it. Selected text in one block becomes what to look for. */
3
+ export const OpenFind = defineCommand('find.open', { description: 'Opens find, looking for the selected text if there is some', runsWhenReadOnly: true });
4
+ /** Opens find with replacing offered too. */
5
+ export const OpenFindAndReplace = defineCommand('find.openReplace', { description: 'Opens find and replace' });
6
+ /** Moves to the next match, and from the last back to the first. */
7
+ export const FindNext = defineCommand('find.next', { description: 'Moves to the next match, and from the last back to the first', runsWhenReadOnly: true });
8
+ /** Moves to the previous match, and from the first back to the last. */
9
+ export const FindPrevious = defineCommand('find.previous', { description: 'Moves to the previous match, and from the first back to the last', runsWhenReadOnly: true });
10
+ /** Replaces the current match, keeping its formats, and moves to the next. It can run only while replacing is offered. */
11
+ export const ReplaceMatch = defineCommand('find.replace', { description: 'Replaces the current match and moves to the next' });
12
+ /** Replaces every match, as one undo step. It can run only while replacing is offered. */
13
+ export const ReplaceAllMatches = defineCommand('find.replaceAll', { description: 'Replaces every match, as one undo step' });
14
+ /** Closes find and takes its highlights away. From the panel, the current match becomes the selection and the keyboard goes back to the text. */
15
+ export const CloseFind = defineCommand('find.close', { description: 'Closes find and takes its highlights away', runsWhenReadOnly: true });
16
+ //# sourceMappingURL=commands.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commands.js","sourceRoot":"","sources":["../src/commands.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD,gGAAgG;AAChG,MAAM,CAAC,MAAM,QAAQ,GAAG,aAAa,CAAC,WAAW,EAAE,EAAE,WAAW,EAAE,4DAA4D,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;AAC1J,6CAA6C;AAC7C,MAAM,CAAC,MAAM,kBAAkB,GAAG,aAAa,CAAC,kBAAkB,EAAE,EAAE,WAAW,EAAE,wBAAwB,EAAE,CAAC,CAAC;AAC/G,oEAAoE;AACpE,MAAM,CAAC,MAAM,QAAQ,GAAG,aAAa,CAAC,WAAW,EAAE,EAAE,WAAW,EAAE,8DAA8D,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;AAC5J,wEAAwE;AACxE,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,CAAC,eAAe,EAAE,EAAE,WAAW,EAAE,kEAAkE,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC;AACxK,0HAA0H;AAC1H,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,CAAC,cAAc,EAAE,EAAE,WAAW,EAAE,kDAAkD,EAAE,CAAC,CAAC;AAC/H,0FAA0F;AAC1F,MAAM,CAAC,MAAM,iBAAiB,GAAG,aAAa,CAAC,iBAAiB,EAAE,EAAE,WAAW,EAAE,wCAAwC,EAAE,CAAC,CAAC;AAC7H,iJAAiJ;AACjJ,MAAM,CAAC,MAAM,SAAS,GAAG,aAAa,CAAC,YAAY,EAAE,EAAE,WAAW,EAAE,2CAA2C,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAC"}
@@ -0,0 +1,67 @@
1
+ import type { Disposable, Editor } from '@minnalstack/editor-core';
2
+ export declare class Finder implements Disposable {
3
+ private readonly editor;
4
+ private readonly root;
5
+ private readonly panel;
6
+ private readonly painter;
7
+ private found;
8
+ private current;
9
+ /** The document `found` was read from. The editor hands out the same reader until the document changes. */
10
+ private searched;
11
+ /** Where the search starts: the caret, when the panel opened. */
12
+ private origin;
13
+ private watching;
14
+ private readonly readOnlyWatch;
15
+ private askedSearch;
16
+ private isWatchingKeys;
17
+ constructor(editor: Editor, root: HTMLElement, host: HTMLElement);
18
+ get isOpen(): boolean;
19
+ get hasMatches(): boolean;
20
+ get canReplace(): boolean;
21
+ /** Opens the panel, or brings the keyboard back to it, looking for the selected text if there is some. */
22
+ open(replace: boolean): boolean;
23
+ /** Closes the panel and takes its highlights away. From the panel, the current match becomes the selection, and the keyboard goes back to the text. */
24
+ close(): boolean;
25
+ next(): boolean;
26
+ previous(): boolean;
27
+ /** Replaces the current match and moves to the next one after it, so text that the replacement holds is not found again at once. */
28
+ replace(): boolean;
29
+ /** Replaces every match in one edit, which is one undo step. Past `matchLimit`, it replaces those found, and the count shows what is left. */
30
+ replaceAll(): boolean;
31
+ private step;
32
+ /** What to look for, or how, has changed: search again soon, once a frame however fast it is typed. */
33
+ private askForSearch;
34
+ private cancelSearch;
35
+ /** Searches from where the panel opened, paints it all, and brings the first match after there into view. */
36
+ private search;
37
+ private scan;
38
+ /**
39
+ * Makes what was found current before acting on it: a search asked for by typing runs now if its frame hasn't come, as when Enter follows the last key at once, and the
40
+ * document is searched again if it has changed and its notification hasn't come yet, so a match that is acted on is still there.
41
+ */
42
+ private freshen;
43
+ private currentStart;
44
+ private paint;
45
+ private showCount;
46
+ /** After the frame, merged, as every notification is. The current match stays the same one, or becomes the next if it has gone. Only changed blocks are painted again. */
47
+ private documentChanged;
48
+ /**
49
+ * Brings the current match into view. It becomes the selection when the keyboard is in the text, as with Mod-g, but not while it is in the panel, where the highlight shows
50
+ * it, until the panel closes. Where nothing can paint it, it becomes the selection at once.
51
+ */
52
+ private reveal;
53
+ /** Selects a match. A read-only editor keeps its selection, because it refuses every edit. */
54
+ private select;
55
+ /** The selected text, if it is within one block and short enough to look for. */
56
+ private selectedText;
57
+ private selectionStart;
58
+ /**
59
+ * A read-only editor refuses every command, so its keys can't open find. While it is read-only, this listens for them on the page, for a selection in this editor. No key
60
+ * types then, so none of this is on a keystroke that writes.
61
+ */
62
+ private readOnlyChanged;
63
+ private readonly pageKey;
64
+ private holdsSelection;
65
+ dispose(): void;
66
+ }
67
+ //# sourceMappingURL=finder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finder.d.ts","sourceRoot":"","sources":["../src/finder.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAA4B,UAAU,EAA0D,MAAM,EAA2B,MAAM,0BAA0B,CAAC;AAgB9K,qBAAa,MAAO,YAAW,UAAU;IAerC,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAfvB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAY;IAClC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAU;IAClC,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,OAAO,CAAM;IACrB,2GAA2G;IAC3G,OAAO,CAAC,QAAQ,CAA+B;IAC/C,iEAAiE;IACjE,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,QAAQ,CAA2B;IAC3C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAa;IAC3C,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,cAAc,CAAS;IAE/B,YACmB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,WAAW,EAClC,IAAI,EAAE,WAAW,EAsBlB;IAED,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,IAAI,UAAU,IAAI,OAAO,CAExB;IAED,IAAI,UAAU,IAAI,OAAO,CAExB;IAED,0GAA0G;IAC1G,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAY9B;IAED,uJAAuJ;IACvJ,KAAK,IAAI,OAAO,CAkBf;IAED,IAAI,IAAI,OAAO,CAEd;IAED,QAAQ,IAAI,OAAO,CAElB;IAED,oIAAoI;IACpI,OAAO,IAAI,OAAO,CAajB;IAED,8IAA8I;IAC9I,UAAU,IAAI,OAAO,CAkBpB;IAED,OAAO,CAAC,IAAI;IAWZ,uGAAuG;IACvG,OAAO,CAAC,YAAY;IAQpB,OAAO,CAAC,YAAY;IAKpB,6GAA6G;IAC7G,OAAO,CAAC,MAAM;IAMd,OAAO,CAAC,IAAI;IAOZ;;;OAGG;IACH,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,KAAK;IAMb,OAAO,CAAC,SAAS;IAIjB,0KAA0K;IAC1K,OAAO,CAAC,eAAe;IAKvB;;;OAGG;IACH,OAAO,CAAC,MAAM;IAQd,8FAA8F;IAC9F,OAAO,CAAC,MAAM;IAId,iFAAiF;IACjF,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,cAAc;IAOtB;;;OAGG;IACH,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAWtB;IAEF,OAAO,CAAC,cAAc;IAKtB,OAAO,IAAI,IAAI,CAOd;CACF"}
package/dist/finder.js ADDED
@@ -0,0 +1,296 @@
1
+ import { CloseFind, FindNext, FindPrevious, ReplaceAllMatches, ReplaceMatch } from './commands.js';
2
+ import { Painter, canPaint, scrollIntoView } from './highlights.js';
3
+ import { FindPanel, hasModifier } from './panel.js';
4
+ import { formatsAt, patternOf, searchDocument } from './search.js';
5
+ const nothingFound = { matches: [], hasMore: false, first: -1 };
6
+ /** Longer selected text isn't something to look for. */
7
+ const longestSeed = 200;
8
+ /** Replaces one match with text that takes the formats of the first character it replaces, so a bold word stays bold. */
9
+ function replaceIn(edit, match, text, formats) {
10
+ edit.deleteText(match);
11
+ if (text !== '')
12
+ edit.insertInline({ block: match.block, offset: match.from }, [formats === undefined ? { insert: text } : { insert: text, attributes: formats }]);
13
+ }
14
+ export class Finder {
15
+ editor;
16
+ root;
17
+ panel;
18
+ painter;
19
+ found = nothingFound;
20
+ current = -1;
21
+ /** The document `found` was read from. The editor hands out the same reader until the document changes. */
22
+ searched = null;
23
+ /** Where the search starts: the caret, when the panel opened. */
24
+ origin = null;
25
+ watching = null;
26
+ readOnlyWatch;
27
+ askedSearch = 0;
28
+ isWatchingKeys = false;
29
+ constructor(editor, root, host) {
30
+ this.editor = editor;
31
+ this.root = root;
32
+ // Through the commands, so that middleware sees them, once what was found is current, because whether a command can run depends on it. A read-only editor refuses every
33
+ // command, so there the panel moves between matches itself, which changes nothing.
34
+ const run = (command, itself) => {
35
+ this.freshen();
36
+ if (editor.readOnly)
37
+ itself();
38
+ else
39
+ editor.execute(command);
40
+ };
41
+ this.panel = new FindPanel({
42
+ translate: (text, values) => editor.translate(text, values),
43
+ search: () => this.askForSearch(),
44
+ next: () => run(FindNext, () => this.next()),
45
+ previous: () => run(FindPrevious, () => this.previous()),
46
+ replace: () => run(ReplaceMatch, () => false),
47
+ replaceAll: () => run(ReplaceAllMatches, () => false),
48
+ close: () => run(CloseFind, () => this.close()),
49
+ });
50
+ this.painter = new Painter(editor, root);
51
+ host.insertBefore(this.panel.element, root);
52
+ this.readOnlyWatch = editor.readOnlyChanged.subscribe(() => this.readOnlyChanged());
53
+ this.readOnlyChanged();
54
+ }
55
+ get isOpen() {
56
+ return this.panel.isOpen;
57
+ }
58
+ get hasMatches() {
59
+ return this.panel.isOpen && this.found.matches.length > 0;
60
+ }
61
+ get canReplace() {
62
+ return this.panel.isOpen && this.panel.showsReplace && !this.editor.readOnly && this.found.matches.length > 0;
63
+ }
64
+ /** Opens the panel, or brings the keyboard back to it, looking for the selected text if there is some. */
65
+ open(replace) {
66
+ const seed = this.selectedText();
67
+ if (seed !== null)
68
+ this.panel.query = seed;
69
+ this.origin = this.selectionStart();
70
+ // Only while the panel is open, so a closed one costs nothing after a change.
71
+ this.watching ??= this.editor.documentChanged.subscribe((_, e) => this.documentChanged(e));
72
+ this.panel.open(replace);
73
+ if (replace && this.panel.showsReplace && this.panel.query !== '')
74
+ this.panel.focusReplace();
75
+ else
76
+ this.panel.focusFind();
77
+ this.cancelSearch();
78
+ this.search();
79
+ return true;
80
+ }
81
+ /** Closes the panel and takes its highlights away. From the panel, the current match becomes the selection, and the keyboard goes back to the text. */
82
+ close() {
83
+ if (!this.panel.isOpen)
84
+ return false;
85
+ this.freshen();
86
+ const hadFocus = this.panel.hasFocus;
87
+ const match = this.found.matches[this.current];
88
+ this.cancelSearch();
89
+ this.watching?.dispose();
90
+ this.watching = null;
91
+ this.panel.close();
92
+ this.painter.clear();
93
+ this.found = nothingFound;
94
+ this.current = -1;
95
+ this.searched = null;
96
+ if (hadFocus) {
97
+ if (match !== undefined)
98
+ this.select(match);
99
+ this.editor.focus();
100
+ }
101
+ return true;
102
+ }
103
+ next() {
104
+ return this.step(1);
105
+ }
106
+ previous() {
107
+ return this.step(-1);
108
+ }
109
+ /** Replaces the current match and moves to the next one after it, so text that the replacement holds is not found again at once. */
110
+ replace() {
111
+ if (!this.canReplace)
112
+ return false;
113
+ this.freshen();
114
+ const match = this.found.matches[this.current];
115
+ if (match === undefined)
116
+ return false;
117
+ const text = this.panel.replacement;
118
+ const formats = formatsAt(this.editor.document.find(match.block)?.inline ?? [], match.from);
119
+ // Replacing text with the same text changes nothing, and it still moves on.
120
+ this.editor.edit((edit) => replaceIn(edit, match, text, formats));
121
+ this.scan({ block: match.block, offset: match.from + text.length });
122
+ this.paint(new Set([match.block]));
123
+ this.reveal();
124
+ return true;
125
+ }
126
+ /** Replaces every match in one edit, which is one undo step. Past `matchLimit`, it replaces those found, and the count shows what is left. */
127
+ replaceAll() {
128
+ if (!this.canReplace)
129
+ return false;
130
+ this.freshen();
131
+ const { matches } = this.found;
132
+ const text = this.panel.replacement;
133
+ const document = this.editor.document;
134
+ const contents = new Map();
135
+ for (const match of matches)
136
+ if (!contents.has(match.block))
137
+ contents.set(match.block, document.find(match.block)?.inline ?? []);
138
+ this.editor.edit((edit) => {
139
+ // From the last to the first, so that replacing one doesn't move those before it.
140
+ for (let index = matches.length - 1; index >= 0; index--) {
141
+ const match = matches[index];
142
+ replaceIn(edit, match, text, formatsAt(contents.get(match.block), match.from));
143
+ }
144
+ });
145
+ this.scan(this.origin);
146
+ this.paint(new Set(contents.keys()));
147
+ return true;
148
+ }
149
+ step(by) {
150
+ this.freshen();
151
+ const total = this.found.matches.length;
152
+ if (!this.panel.isOpen || total === 0)
153
+ return false;
154
+ this.current = (this.current + by + total) % total;
155
+ this.painter.setCurrent(this.found.matches[this.current]);
156
+ this.showCount();
157
+ this.reveal();
158
+ return true;
159
+ }
160
+ /** What to look for, or how, has changed: search again soon, once a frame however fast it is typed. */
161
+ askForSearch() {
162
+ if (this.askedSearch !== 0)
163
+ return;
164
+ this.askedSearch = requestAnimationFrame(() => {
165
+ this.askedSearch = 0;
166
+ if (this.panel.isOpen)
167
+ this.search();
168
+ });
169
+ }
170
+ cancelSearch() {
171
+ cancelAnimationFrame(this.askedSearch);
172
+ this.askedSearch = 0;
173
+ }
174
+ /** Searches from where the panel opened, paints it all, and brings the first match after there into view. */
175
+ search() {
176
+ this.scan(this.origin);
177
+ this.paint(null);
178
+ this.reveal();
179
+ }
180
+ scan(from) {
181
+ const pattern = patternOf(this.panel.query, this.panel.options);
182
+ this.searched = this.editor.document;
183
+ this.found = pattern === null ? nothingFound : searchDocument(this.searched, pattern, from);
184
+ this.current = this.found.first;
185
+ }
186
+ /**
187
+ * Makes what was found current before acting on it: a search asked for by typing runs now if its frame hasn't come, as when Enter follows the last key at once, and the
188
+ * document is searched again if it has changed and its notification hasn't come yet, so a match that is acted on is still there.
189
+ */
190
+ freshen() {
191
+ if (this.askedSearch !== 0) {
192
+ this.cancelSearch();
193
+ this.search();
194
+ }
195
+ else if (this.searched !== null && this.editor.document !== this.searched)
196
+ this.scan(this.currentStart());
197
+ }
198
+ currentStart() {
199
+ const match = this.found.matches[this.current];
200
+ return match === undefined ? this.origin : { block: match.block, offset: match.from };
201
+ }
202
+ paint(changed) {
203
+ this.painter.paint(this.found.matches, changed);
204
+ this.painter.setCurrent(this.found.matches[this.current]);
205
+ this.showCount();
206
+ }
207
+ showCount() {
208
+ this.panel.showCount(this.current, this.found.matches.length, this.found.hasMore);
209
+ }
210
+ /** After the frame, merged, as every notification is. The current match stays the same one, or becomes the next if it has gone. Only changed blocks are painted again. */
211
+ documentChanged(e) {
212
+ this.scan(this.currentStart());
213
+ this.paint(new Set(e.changedBlocks.map((block) => block.id)));
214
+ }
215
+ /**
216
+ * Brings the current match into view. It becomes the selection when the keyboard is in the text, as with Mod-g, but not while it is in the panel, where the highlight shows
217
+ * it, until the panel closes. Where nothing can paint it, it becomes the selection at once.
218
+ */
219
+ reveal() {
220
+ const match = this.found.matches[this.current];
221
+ if (match === undefined)
222
+ return;
223
+ if (!this.panel.hasFocus || !canPaint())
224
+ this.select(match);
225
+ const range = this.painter.rangeOf(match);
226
+ if (range !== null)
227
+ scrollIntoView(range);
228
+ }
229
+ /** Selects a match. A read-only editor keeps its selection, because it refuses every edit. */
230
+ select(match) {
231
+ this.editor.edit((edit) => edit.setSelection({ block: match.block, offset: match.from }, { block: match.block, offset: match.to }));
232
+ }
233
+ /** The selected text, if it is within one block and short enough to look for. */
234
+ selectedText() {
235
+ const selection = this.editor.selection;
236
+ if (selection === null || selection.kind !== 'text' || selection.isEmpty || selection.anchor.block !== selection.head.block)
237
+ return null;
238
+ const from = Math.min(selection.anchor.offset, selection.head.offset);
239
+ const to = Math.max(selection.anchor.offset, selection.head.offset);
240
+ const text = this.editor.document.find(selection.anchor.block)?.text.slice(from, to) ?? '';
241
+ return text === '' || text.length > longestSeed || /[\n]/.test(text) ? null : text;
242
+ }
243
+ selectionStart() {
244
+ const selection = this.editor.selection;
245
+ if (selection === null)
246
+ return null;
247
+ const { anchor, head } = selection;
248
+ return anchor.block === head.block ? { block: anchor.block, offset: Math.min(anchor.offset, head.offset) } : anchor;
249
+ }
250
+ /**
251
+ * A read-only editor refuses every command, so its keys can't open find. While it is read-only, this listens for them on the page, for a selection in this editor. No key
252
+ * types then, so none of this is on a keystroke that writes.
253
+ */
254
+ readOnlyChanged() {
255
+ const readOnly = this.editor.readOnly;
256
+ this.panel.setReadOnly(readOnly);
257
+ if (readOnly === this.isWatchingKeys)
258
+ return;
259
+ if (readOnly)
260
+ this.root.ownerDocument.addEventListener('keydown', this.pageKey);
261
+ else
262
+ this.root.ownerDocument.removeEventListener('keydown', this.pageKey);
263
+ this.isWatchingKeys = readOnly;
264
+ }
265
+ pageKey = (event) => {
266
+ if (event.defaultPrevented || event.isComposing || this.panel.element.contains(event.target) || !this.holdsSelection())
267
+ return;
268
+ const key = event.key.toLowerCase();
269
+ const withModifier = hasModifier(event) && !event.altKey;
270
+ let isHandled = false;
271
+ if (withModifier && key === 'f' && !event.shiftKey)
272
+ isHandled = this.open(false);
273
+ else if (withModifier && key === 'g' && this.panel.isOpen)
274
+ isHandled = event.shiftKey ? this.previous() : this.next();
275
+ else if (event.key === 'Escape' && this.panel.isOpen)
276
+ isHandled = this.close();
277
+ if (!isHandled)
278
+ return;
279
+ event.preventDefault();
280
+ event.stopPropagation();
281
+ };
282
+ holdsSelection() {
283
+ const selection = this.root.ownerDocument.getSelection();
284
+ return selection !== null && selection.rangeCount > 0 && this.root.contains(selection.anchorNode);
285
+ }
286
+ dispose() {
287
+ this.cancelSearch();
288
+ this.watching?.dispose();
289
+ this.readOnlyWatch.dispose();
290
+ if (this.isWatchingKeys)
291
+ this.root.ownerDocument.removeEventListener('keydown', this.pageKey);
292
+ this.painter.clear();
293
+ this.panel.element.remove();
294
+ }
295
+ }
296
+ //# sourceMappingURL=finder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"finder.js","sourceRoot":"","sources":["../src/finder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACnG,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,W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O,SAAS,KAAK,IAAI,IAAI,SAAS,CAAC,UAAU,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;IACpG,CAAC;IAED,OAAO;QACL,IAAI,CAAC,YAAY,EAAE,CAAC;QACpB,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,CAAC;QACzB,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;QAC7B,IAAI,IAAI,CAAC,cAAc;YAAE,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9F,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;IAC9B,CAAC;CACF"}
@@ -0,0 +1,42 @@
1
+ import type { Editor, TextRange } from '@minnalstack/editor-core';
2
+ /** Whether this browser can paint matches. Where it can't, moving to a match selects it, because nothing else would show it. */
3
+ export declare function canPaint(): boolean;
4
+ /**
5
+ * What one editor has painted. The editor finds a match's range in time that grows with how far into the document its block is: 1,000 of them at the end of 20,000 blocks
6
+ * take most of a second. So only the blocks near the view are painted, as an IntersectionObserver reports them, and a block keeps its ranges until it changes.
7
+ * https://developer.mozilla.org/docs/Web/API/Intersection_Observer_API
8
+ */
9
+ export declare class Painter {
10
+ private readonly editor;
11
+ private readonly root;
12
+ /** The matches of each block that has some. */
13
+ private matches;
14
+ /** The ranges painted for a block, one for each of its matches, and null where the page has none. */
15
+ private readonly painted;
16
+ /** The element of each block that has matches, which the observer watches. */
17
+ private readonly watched;
18
+ /** The blocks the observer last said are near the view. */
19
+ private readonly near;
20
+ private readonly observer;
21
+ private current;
22
+ constructor(editor: Editor, root: HTMLElement);
23
+ /** Paints the matches. A block's ranges are found again when it is in `changed`, or always when that is null, and when the page has drawn it again. */
24
+ paint(matches: readonly TextRange[], changed: ReadonlySet<string> | null): void;
25
+ /** Paints the current match over the others, or none. It is painted wherever it is, because it is what was moved to. */
26
+ setCurrent(match: TextRange | undefined): void;
27
+ /** The range of a match, painted or not, for bringing it into view. */
28
+ rangeOf(match: TextRange): Range | null;
29
+ /** Takes away everything this editor painted, and stops watching. */
30
+ clear(): void;
31
+ private paintBlock;
32
+ private unpaint;
33
+ private seen;
34
+ /** The elements of these blocks, from the `data-block-id` the editor draws on each: one look through the page for many, or one look each for a few. */
35
+ private elementsOf;
36
+ }
37
+ /**
38
+ * Brings a match into view in each box that scrolls around it, from the inside out, and to the middle when it is out of view, as a browser's own find does. In the middle it
39
+ * stays clear of what sticks to the top of the page, such as a toolbar.
40
+ */
41
+ export declare function scrollIntoView(range: Range): void;
42
+ //# sourceMappingURL=highlights.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"highlights.d.ts","sourceRoot":"","sources":["../src/highlights.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAMlE,gIAAgI;AAChI,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAoBD;;;;GAIG;AACH,qBAAa,OAAO;IAahB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAbvB,+CAA+C;IAC/C,OAAO,CAAC,OAAO,CAA2C;IAC1D,qGAAqG;IACrG,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgD;IACxE,8EAA8E;IAC9E,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8B;IACtD,2DAA2D;IAC3D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAqB;IAC1C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA8B;IACvD,OAAO,CAAC,OAAO,CAAsB;IAErC,YACmB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,WAAW,EAGnC;IAED,uJAAuJ;IACvJ,KAAK,CAAC,OAAO,EAAE,SAA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