Find & replace
Every Folio editor finds and replaces natively: documents (FolioView, FolioApp), read-only views and readers (find only), and decks (SlidesApp). The search runs on the document model, not the DOM, so it covers every page of a 7,000-page book, including pages that were never laid out or rendered. It also covers text that lives outside the flow: headers and footers, text boxes and shapes, chart titles and captions; and in decks, tables, SmartArt and speaker notes. Matches are highlighted where they are drawn: in the body, in the header and footer bands of every page showing that part, on the slide, and in the notes strip.
Using it
| Keys | |
|---|---|
| Ctrl/⌘+F | find (the selection, if short, becomes the query; otherwise the last one) |
| Ctrl+H, ⌘⌥F on a Mac | find and replace (⌘H hides the app on a Mac) |
| Enter / Shift+Enter, F3 / Shift+F3, Ctrl/⌘+G | next / previous match |
| Enter in "Replace with" | replace this match and go to the next |
| Ctrl+Alt+Enter | replace all |
| Esc | close the bar; the current match becomes the selection |
The bar sits at the top right of the page area (full width on phones). It is a role="search" landmark: the count ("3 of 41") is tied to the field and announced once it settles, every control has a name and a tooltip, and it all works from the keyboard alone. Options: match case, whole words, regular expression, and ignore Arabic diacritics and letter forms (on by default: tashkeel, tatweel and Quranic marks are ignored; أ إ آ ٱ match ا, ى matches ي, ة matches ه). It's the same matching as the reader's find (compileTextQuery in folio-model). The results list shows each match in context with its page, or its place ("Header", "Text box", "Slide 3 of 10: Title").
How it works
FindController (folio-editor) scans the document in slices of a few milliseconds, starting at the caret. The first result shows right away, and typing never waits for a long count. The scan covers the 7,281-page stress book in under a second (first result in about 40 ms). Results are cached per block, so after an edit only the changed blocks are matched again. Highlights follow edits at once, and the count catches up after a short pause.
Matches are drawn through the editor's highlight layers (addHighlightLayer). Views ask a layer only for the pages on screen, so a book with 100,000 matches costs what two pages hold.
Replace and replace all are each one ordinary transaction:
- one undo step;
- sent to collaborators like any other edit, so concurrent edits rebase and converge;
- the replaced text keeps the formatting of its first character (bold stays bold).
import { FindController } from '@nextgensoftwares/folio-editor';
const find = new FindController(editor, { sources: composed.find });
find.setQuery('colour', { wholeWord: true });
find.flush(); // finish counting now (tests, scripts)
console.log(find.state().matches.length);
find.replaceAll('color'); // one undo step
find.destroy();Text outside the flow
A plugin describes where it keeps text with FolioPlugin.find, a FindSource:
nodes: surfaces inside body nodes (a drawing's shapes);doc: surfaces held by the document (headers, footers, page objects).
Each surface hands over Folio JSON content and a write(tr, content, at) that puts the edited content back in the same transaction. The built-in plugins ship theirs: headerFooterFind, layersFind and chartsFind. composePlugins(...).find collects them, and FolioApp passes them to the bar.
Host results: server search and streaming loads
The bar takes a host search (the same idea as the reader's source.search). It is called once the query settles, and its results are listed under "More results":
<FolioView editor={editor} find={{ search: async (q, options, signal) => (await api.search(q, options, { signal })).map(toHit) }} />While a chunked document is still loading (assembleDocument), the editor only holds the chunks it has received. storedDocumentSearch from folio-sync searches the rest from the cache or the server:
const search = storedDocumentSearch({ api, docId, manifest: session.manifest, cache, loaded: () => editor.state.doc.childCount });Pass find={false} to turn the bar off in a view.
Decks
SlidesApp finds across every slide: shape and table text, SmartArt (find only, since the diagram's data part is its source), chart titles and, with the "Search notes" option, the speaker notes. Results are grouped by slide. Going to a match shows its slide, selects its shape and highlights the text on the slide (tables, charts and SmartArt get the shape outlined); matches in the notes are marked in the notes strip. Replacing is one deck update: one undo step, one change for collaborators. Headless, it's DeckFind / findInDeck / replaceInDeck from folio-slides-editor. Custom shells use useDeckFind() and <SlidesFind> from plugin-slides.
Checking it
tools/find-e2e/find.mjs runs on the stress book (7,281 pages) and tools/find-e2e/slides.mjs on the sample deck, both in Chromium and WebKit. Measured (headless, Linux):
| Chromium | WebKit | |
|---|---|---|
| First result ("algorithm", 12,536 matches) | 39–41 ms | 21–31 ms |
| Full count | 0.6–0.8 s | 0.9 s |
| First result (Arabic "المكتبة", 4,412 matches) | 59–81 ms | 55–62 ms |
| Typing, key to next frame (median / p95), find closed | 33 / 41–51 ms | 24–28 / 38–65 ms |
| Typing with a live query | 32 / 37–39 ms | 30–31 / 37–46 ms |
| Replace all 1,919 matches (one undo step) | 1.0 s | 1.4 s |
Typing costs the same with a live query as without one: what remains is the book's incremental layout. Both scripts also check keyboard-only stepping, the landmark, replace all followed by undo, and axe on the open bar (no serious issues).