diff --git a/.changeset/join-pending-property-changes.md b/.changeset/join-pending-property-changes.md new file mode 100644 index 0000000000..084dd4def9 --- /dev/null +++ b/.changeset/join-pending-property-changes.md @@ -0,0 +1,5 @@ +--- +"@stll/docx-core": patch +--- + +Keep a pending paragraph property change consistent with the joined paragraph after a direct join. diff --git a/packages/docx-core/src/ops/__tests__/joinPendingPropertyChange.test.ts b/packages/docx-core/src/ops/__tests__/joinPendingPropertyChange.test.ts new file mode 100644 index 0000000000..bcbdf1c1ab --- /dev/null +++ b/packages/docx-core/src/ops/__tests__/joinPendingPropertyChange.test.ts @@ -0,0 +1,93 @@ +/** + * A direct join keeps the first paragraph's properties and the second's + * mark, with the second's pending property change. That change then records + * the joined paragraph's properties as its current ones, so a saved and + * reopened file reads the same change. Undo gives the second paragraph its + * change back as it was. + */ +import { expect, test } from "bun:test"; + +import type { Document, Paragraph } from "../../model/document"; +import { applyDocumentOp, applyDocumentOps } from "../apply"; +import { normalizeForOps } from "../contract"; +import { DOCUMENT_OP_TYPES, type DocumentOp, OP_STORIES } from "../types"; + +const text = (value: string) => [ + { type: "run" as const, content: [{ type: "text" as const, text: value }] }, +]; + +const documentWith = (first: Paragraph, second: Paragraph): Document => + normalizeForOps({ + package: { document: { content: [first, second] } }, + } as Document); + +const join: DocumentOp = { + type: DOCUMENT_OP_TYPES.JOIN_BLOCKS, + story: OP_STORIES.MAIN, + blockId: "00000001", + nextBlockId: "00000002", +}; + +const joined = (document: Document) => { + const edit = applyDocumentOp(document, join).unwrap(); + const replayed: DocumentOp[] = JSON.parse(JSON.stringify(edit.inverse)); + expect( + applyDocumentOps(edit.document, replayed).unwrap().document, + ).toStrictEqual(document); + const [paragraph] = edit.document.package.document.content; + if (paragraph?.type !== "paragraph") throw new Error("No joined paragraph."); + return paragraph; +}; + +const change = ( + previousFormatting: Paragraph["formatting"], + currentFormatting: Paragraph["formatting"], +) => ({ + type: "paragraphPropertyChange" as const, + info: { id: 5, author: "Reviewer", date: "2026-01-05T10:00:00Z" }, + ...(previousFormatting === undefined ? {} : { previousFormatting }), + ...(currentFormatting === undefined ? {} : { currentFormatting }), +}); + +test("a pending change records that the joined paragraph states no properties", () => { + const paragraph = joined( + documentWith( + { type: "paragraph", paraId: "00000001", content: text("Lead ") }, + { + type: "paragraph", + paraId: "00000002", + formatting: { alignment: "both" }, + propertyChanges: [change(undefined, { alignment: "both" })], + content: text("tail"), + }, + ), + ); + expect(paragraph.formatting).toBeUndefined(); + expect(paragraph.propertyChanges).toEqual([change(undefined, undefined)]); +}); + +test("a pending change records the joined paragraph's properties", () => { + const paragraph = joined( + documentWith( + { + type: "paragraph", + paraId: "00000001", + formatting: { alignment: "center" }, + content: text("Lead "), + }, + { + type: "paragraph", + paraId: "00000002", + formatting: { alignment: "both" }, + propertyChanges: [ + change({ alignment: "right" }, { alignment: "both" }), + ], + content: text("tail"), + }, + ), + ); + expect(paragraph.formatting).toEqual({ alignment: "center" }); + expect(paragraph.propertyChanges).toEqual([ + change({ alignment: "right" }, { alignment: "center" }), + ]); +}); diff --git a/packages/docx-core/src/ops/apply.ts b/packages/docx-core/src/ops/apply.ts index 1e2f4b295e..b3ef04eb36 100644 --- a/packages/docx-core/src/ops/apply.ts +++ b/packages/docx-core/src/ops/apply.ts @@ -2631,6 +2631,21 @@ const joinBlocks = ( if (trailing.sectionProperties !== undefined) { joined.sectionProperties = trailing.sectionProperties; } + // Joined after words, the paragraph states the first's properties under + // the second's mark, so the second's pending property changes now record + // those as the current ones, as a reopened file reads them. Kept second, + // the split that undoes the join gives them back with the survivor's own + // review fields. + if ( + joined.propertyChanges !== undefined && + survivorSide === SPLIT_HALVES.SECOND && + op.expectedSurvivor === undefined && + paragraphLength(leading) > 0 + ) + joined.propertyChanges = changesRecordingAuthored( + joined.propertyChanges, + joined, + ); const length = paragraphLength(leading); const newHalf = survivorSide === SPLIT_HALVES.SECOND diff --git a/packages/editor-web/scripts/check-eager-bundle.ts b/packages/editor-web/scripts/check-eager-bundle.ts index f7e6b099e7..be61f96fd5 100644 --- a/packages/editor-web/scripts/check-eager-bundle.ts +++ b/packages/editor-web/scripts/check-eager-bundle.ts @@ -42,8 +42,9 @@ const BUDGETS = { // Drop text box slots of replaced blocks from the paragraph index (+39 B minified; +9 B React, +6 B Vue gzip). // Restore selections beside inline controls exactly (+706 B minified per adapter; +196 B React, +201 B Vue gzip). // Judge locks when an edit is made, not again on undo or redo (+42 B minified per adapter; +9 B Vue gzip). - react: { minified: 3_920_768, gzip: 1_233_183, docxOps: 803_877 }, - vue: { minified: 3_614_625, gzip: 1_128_966, docxOps: 803_819 }, + // Record joined properties on pending property changes (+278 B React, +286 B Vue minified; +105 B React, +55 B Vue gzip; +204 B React, +205 B Vue eager ops). + react: { minified: 3_921_046, gzip: 1_233_288, docxOps: 804_081 }, + vue: { minified: 3_614_911, gzip: 1_129_021, docxOps: 804_024 }, } as const satisfies Record; const HOST_PEERS = [ diff --git a/test/property-seeds/test%2Fcanonical-session-rich.fuzz.ts.json b/test/property-seeds/test%2Fcanonical-session-rich.fuzz.ts.json index a644e67207..84de221f0d 100644 --- a/test/property-seeds/test%2Fcanonical-session-rich.fuzz.ts.json +++ b/test/property-seeds/test%2Fcanonical-session-rich.fuzz.ts.json @@ -23,6 +23,14 @@ "counterexample": "[1721208328,[{\"kind\":\"cross-block\",\"block\":71,\"other\":134,\"anchor\":0,\"head\":0,\"text\":\"a\"}]]", "note": "a direct edit across a section break saves through the session save", "date": "2026-11-30" + }, + { + "seed": -1561320497, + "path": "3:2:4:2:5:5:6:6:6:6:6:5:5:5:5:5:5:5:5", + "digest": "be5f2894", + "counterexample": "[13,[{\"kind\":\"cross-block\",\"block\":0,\"other\":1,\"anchor\":0,\"head\":0,\"text\":\"a\"},{\"kind\":\"replace\",\"block\":0,\"other\":0,\"anchor\":0,\"head\":0,\"text\":\"a\"}]]", + "note": "a cross-paragraph edit keeps the kept paragraph's pending property change consistent through save", + "date": "2026-11-30" } ] }