Skip to content

fix(docx): conversion answers applied to the right paragraphs, during IME, and in new tables - #38

Open
pierreneter wants to merge 4 commits into
mainfrom
fix/docx-conversion-results
Open

pierreneter wants to merge 4 commits into
mainfrom
fix/docx-conversion-results

Conversation

@pierreneter

Copy link
Copy Markdown
Member

Fixes #26, fixes #27, fixes #28, fixes #29.

Four bugs in how the DOCX editor applies the worker's conversion answers, found while reviewing #22. One commit per issue, plus a follow-up for #29.

#26 / #29: which answer is current

The editor decided whether a paragraph had been edited again by comparing paragraph object identity. That key is wrong in both directions:

Other operations also replace the object without editing it (a reskin, an earlier conversion), and undo can restore the very object an older request sent.

The fix:

  • Every write to the worker now gets a sequence number (writes), and writtenIn keeps the last write that carried each paragraph. An answer to write seq is obsolete for a paragraph when writtenIn > seq.
  • Every sender goes through one send(): commit, restore, remark, replaceBlocks and commitTable. Each write asks for the conversion of any stale paragraph it carries, so the latest answer always carries the latest conversion.
  • The marker loop skips only paragraphs that the answer converted and that were written again. Renumbering of other paragraphs is still applied (there is a test for this). The marker loop also records heir.
  • The answer chain is now .finally(inflight--).then(apply + emitState).catch(log). A throwing onState subscriber is logged instead of becoming an unhandled rejection.
  • When the last answer to arrive is obsolete and nothing is held, the current state is published once more, so busy does not stay true.

#28: answers during an IME composition

An answer that arrives during a composition is kept with its sequence number instead of being dropped. RichInput calls the new optional RichHost.composed() after the composed text is committed. The editor then applies the held answers and publishes the state. The PPTX editor does not implement the hook, so it is unchanged.

#27: lists in tables inserted or rebuilt this session

I took the worker-side direction, because it makes the editor show what the file has: marker, lst, the numbering of list items after the table, styles, and removal on the second click.

  • applyTable now returns an EditResult like applyEdit: converted cell paragraphs for the requested pids (TableOp.want) and changed markers. The shared tail moved into editResult().
  • A paragraph whose generated XML has not changed is not converted again. On a 320-cell new table, a keystroke takes 3.3 ms median, against 2.9 ms on main.
  • recomputeMarkers counts the paragraphs of new and regenerated tables where they stand. A deleted file table no longer counts in the numbering.
  • docx.table returns EditResult (was void). These are internal types, so the public API does not change.

Tests

test/docx-state.test.ts (holdEdits now also holds docx.table):

e2e/docx-table.spec.ts: insert a table, type, click "Bulleted list": aria-pressed="true" and a marker; a second click removes both. The saved and reopened file agrees both times.

The new unit tests fail on main, except the renumbering guard, which pins behaviour that must stay. The #27 e2e test times out on main.

Results

On the final commit, in the Linux test VM with Bun 1.4.4:

Check Result
typecheck clean
bun test 1202 pass, 0 fail
build ok
e2e/docx.spec.ts 23 pass
e2e/docx-table.spec.ts 5 pass
e2e/docx-qa2.spec.ts 13 pass
e2e/docx-track.spec.ts 2 pass

e2e/collab.spec.ts (5) and e2e/comments.spec.ts (6) passed before the rebase.

Behaviour changes worth knowing

Not changed, noticed on the way

  • Flaky e2e test, also on main: "track my changes" in e2e/docx-track.spec.ts fails about 1 in 10 runs. Tracked-change dates have one-second resolution, so two keystrokes that straddle a second boundary produce two tracked changes instead of one.
  • Table style not shown: new-table cell paragraphs are converted with the template paragraph's layer, so the table style (for example Table Grid's zero spacing) shows only after reopening.
  • Other items, not touched:
    • commit() returns for table paragraphs before its comment-mark logic;
    • tableSpec drops nested tables when a table is regenerated;
    • pass() polls isComposing every 50 ms;
    • RichInput.detach() does not reset composing.

🤖 Generated with Claude Code

pierreneter and others added 4 commits November 20, 2026 11:50
…arried (#26, #29)

The editor told an obsolete answer from a current one by the paragraph objects it had sent. Any other replacement of
the object broke that: the list markers of a first answer replaced the paragraph, and the answer to a second edit,
sent before the first came back, was taken for an obsolete one and dropped (the paragraph kept its old look and the
toolbar its old state, #26). The marker loop had no such check at all: an answer made obsolete by an undo still
showed its list for a moment (#29).

Every write to the worker is numbered, and each paragraph remembers the last write that carried it. An answer is
applied to a paragraph only when no later write carried it; a later write always asks for the conversion of a
paragraph whose conversion is still due, so its answer is the one shown. The same holds for the markers of the
paragraphs an answer converted; the numbering it gives other paragraphs is always applied (a later answer only
reports what changed after it). All writes (paragraphs moved back by a collaboration rebase, comment marks, table
inserts) go through the same path. A marker update keeps the paragraph "the same one" for the code that follows
paragraphs by identity (update style to match).

The toolbar is told from inside the chain, before its catch: a listener that throws is reported, not left as an
unhandled rejection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed when it ends (#28)

A conversion answer that arrived while an IME composition was open (Telex, CJK) was dropped whole: the paragraph a
list or style was given kept its old look, the toolbar its old state, and the list numbering it changed in other
paragraphs was lost, until each of them was edited again.

The answer is kept and applied when the composition ends, after the composed text is in (so a paragraph the
composition wrote is converted with that later write), and the toolbar is told then. The input tells its host that a
composition ended (`RichHost.composed`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n and can be taken away (#27)

A table inserted, or one of the file restructured, is written whole (`docx.table`), and that write gave nothing
back: a list (or a style) given to a paragraph of it was in the file but never on screen, the list button stayed
off, and its numbers did not count in the lists around it.

`docx.table` now answers like `docx.edit`: the worker converts the table's paragraphs, counts them where they stand in
the document when it numbers lists, and returns the conversions asked for (`TableOp.want`: the paragraphs whose list
or style changed, and those whose conversion is still due) with every list marker that changed. The editor applies
that answer as it does an edit's, so the marker, the toolbar state, the numbers of the list items after the table
and a second click (which takes the list away) all follow the file. A deleted table no longer counts in the numbering
either. A paragraph whose XML did not change is not converted again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er busy (#29)

An obsolete answer publishes nothing, so when it was the last one to come back (two overlapping edits answered in
the other order, a list turned on and undone with the undo answered first), the last state the host had been told
said busy until the selection moved. The same held for any edit whose answer changed nothing (typing, bold): its
synchronous notification, sent while the write was out, stayed the last one.

When every write is answered and taken in, the state is published even if the answer changed nothing. It is the
model as it stands, so an obsolete answer still never shows its list. An answer held for an IME composition is not
taken in yet: nothing is published until the composition ends and it is applied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

1 participant