Skip to content

fix(docx): what the reader does while Find or the outline is on its way comes first - #60

Merged
pierreneter merged 3 commits into
mainfrom
fix/docx-find-late-selection
Dec 1, 2026
Merged

pierreneter merged 3 commits into
mainfrom
fix/docx-find-late-selection

Conversation

@pierreneter

@pierreneter pierreneter commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Find and the outline scroll to their target, then select it once the scroll has come to rest: afterScroll runs a few frames later, up to 120 on a slow machine. Nothing stopped that late selection, so it could land after the reader had already put the caret somewhere else.

What went wrong

A reader presses Enter in Find, then clicks into the text and types. If the late selection came after the click, the match was selected again and the keys typed replaced the word found. Without Track changes on, the word was gone without a trace. The outline did the same: its late caret took the place of the one just clicked.

This is the cause behind the flaky e2e/plain.spec.ts in #59, which only makes that test wait for the selection.

Fix (src/react/docx/DocxView.tsx)

  • What the reader does comes first. These drop the jump on its way:

    • a click, a key or the wheel in .de-scroll;
    • a newer jump, including a page (gotoPage) or a comment shown;
    • a new query typed in the Find bar.

    Each afterScroll takes a number, and a jump runs only while its number is still the latest. The Find bar is outside .de-scroll, so Enter for the next match doesn't drop anything. The key press that began a jump is older than the jump, so a host shortcut listening before the document doesn't drop its own jump.

  • The jump lands only where the reader still is. At its end, the selection is put in place only while the focus is on the element that had it when the jump began, or that element is gone (the Find bar closed). This covers:

    • a caret the host sets in the text, which focuses the document;
    • a field outside the document the reader went to (Replace, the toolbar). The outline's caret would otherwise take the focus back.

    A collaborator's edit doesn't move the focus, so it doesn't stop the jump.

  • No selection in a paragraph whose text changed meanwhile, since the match's offsets were taken before. The paragraph's id and text are compared, so a paragraph rebuilt with the same text (a style, a comment mark) doesn't stop the jump. The outline only checks which block it is, since its caret goes to the block's start.

Closing the Find bar alone doesn't drop the jump: Enter then Escape still leaves the match selected, as when the frames come on time.

Tests

e2e/docx-jump.spec.ts (new), with every animation frame made 1 s late (the last two rows fail on this PR's first version, before the review):

Test Without the fix With it
Enter in Find, click after NEIGHBOR, type XY fails: alpha XY omega passes
Same, with the caret set through the DOM (selectText) fails: alpha XY omega passes
Enter in Find, nothing else (frames 300 ms late): the match is selected passes passes
Outline jump from a caret pages above: the caret goes to the heading passes passes
Enter in Find, then the query typed on: the old match is not selected fails passes
Outline jump, then a click into the Find field: the keys typed stay in the field fails passes
  • plain.spec.ts's Find test on this Mac, 4 runs each: fails 3 times on main and 2 times with this PR alone. With fix: three flaky tests: Find's late selection in the long document, tracked typing across a second, the big CSV's time #59's wait added it passed 4 of 4.
  • bun test ./test: 1751 pass, 0 fail
  • tsc --noEmit: clean
  • bun test ./e2e: 381 pass, 13 fail on macOS (CI runs Linux):
    • 5 need what a local run lacks: dist/ for the four *-built specs (bun run build), and the generated large-deck.pptx;
    • 7 fail the same way on main on this machine (Mac key bindings: Control+c, End, Tab in a slide);
    • plain.spec.ts (the known flaky one) failed once in the full run, while a source file was saved during it. It passed in the two runs that compared main with the fix, and 3 out of 3 times with docx-jump.spec.ts.

🤖 Generated with Claude Code

pierreneter and others added 3 commits December 1, 2026 11:35
…ay comes first

Find and the outline scroll to their target and select it once the scroll has come to rest, some frames later.
Nothing stopped that late selection: a reader who pressed Enter in Find, then clicked into the text and typed, could
have the match selected again after the click, and the keys typed replaced the word found.

- A click, a key or the wheel in the document drops the jump on its way, and so does a newer jump.
- At its end, the jump selects its target only while the document's selection is where it was when the jump began
  (or not in the document): one set since, by the reader or by the host through the DOM, is kept.
- No selection is put in a paragraph edited meanwhile: the match's offsets are out of date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the focus, not the selection

Review findings on the late selection of Find and the outline:
- The reader who went to a field outside the document (the Find bar's Replace, the toolbar) had the jump land
  anyway: the selection was no longer in the document and counted as untouched, and the outline's caret took the
  focus back. The jump now lands only while the focus is where it was when the jump began, or the element that had it
  is gone (the Find bar closed). A caret set in the text by the host focuses the document, and is kept as before.
- This replaces comparing the document's selection, which a collaborator's edit above the caret moved, so the jump
  did not land though the reader had done nothing.
- A query typed on in the Find bar drops the jump to the match of the query before.
- A paragraph rebuilt with the same text (a style or a comment mark put on it) no longer stops the jump: its id and
  text are compared, not the object. The outline compares only which block it is: the caret goes to its start.
- The key press that began a jump (a host's shortcut listening before the document) no longer drops it.
- A page or a comment shown meanwhile drops the jump, as a newer jump does.
- Tests: frames made late can still be cancelled; the document's selection is told from the Find field's; a query
  typed on and a field gone to are covered (both fail on the version before).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pierreneter
pierreneter merged commit b0e8e25 into main Dec 1, 2026
2 checks passed
@pierreneter
pierreneter deleted the fix/docx-find-late-selection branch December 1, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant