Skip to content

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

Open
pierreneter wants to merge 1 commit into
mainfrom
fix/docx-find-late-selection
Open

pierreneter wants to merge 1 commit into
mainfrom
fix/docx-find-late-selection

Conversation

@pierreneter

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 in the document comes first. A click, a key or the wheel in .de-scroll drops the jump on its way, and so does a newer jump. 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 its own keys (Enter for the next match) don't drop anything.
  • A selection made since the jump began is kept. At the end of the jump, the selection is put in place only if the document's selection is where it was when the jump began, or not in the document at all (the focus is still in the Find bar). This also covers a selection a host or assistive tool sets through the DOM, with no pointer or key event.
  • No selection in a paragraph edited meanwhile. The match's offsets were taken before the edit (a collaborator's, or Replace). The paragraph object is compared, since an edit replaces it.

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:

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
  • 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

…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 <[email protected]>

This branch has not been deployed

No deployments
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