Repository navigation
fix(docx-core): record joined properties on pending property changes #3979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ec92a33
90a22a5
6e99ddc
39e78c5
d9d2a82
aaf3c0b
3cb2928
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@stll/docx-core": patch | ||
| --- | ||
|
|
||
| Keep a pending paragraph property change consistent with the joined paragraph after a direct join. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" }), | ||
| ]); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| joined.propertyChanges, | ||
| joined, | ||
| ); | ||
| const length = paragraphLength(leading); | ||
| const newHalf = | ||
| survivorSide === SPLIT_HALVES.SECOND | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
sed -n '2590,2670p' packages/docx-core/src/ops/apply.ts git log --oneline a901c5a23b88c73c5ddf790553831f110ef6aaa3..f4f4375424a1d4ad24bf90a3dd12a8ea627f402c -- packages/docx-core/src/ops/apply.tsRepository: stella/folio
Length of output: 3111
Script executed:
Repository: stella/folio
Length of output: 1406
Script executed:
Repository: stella/folio
Length of output: 28435
Record the joined formatting on the second paragraph's pending change for a first-kept join too.
With
survivor: SPLIT_HALVES.FIRST, a direct join of a non-empty first paragraph with alignment"center"into a second paragraph whose pending change recordscurrentFormatting"both"leaves the joined paragraph withformatting"center"and the pending change at"both". The refresh at lines 2634-2648 runs only forSPLIT_HALVES.SECOND, so this state persists. After a save and reopen, the document reads the joined paragraph's current formatting as"center", which is the mismatch this change is meant to remove.The final commit (
f4f43754) limits the refresh to the second survivor. Either extend the refresh to the first survivor, with matching undo restoration for the recreated second paragraph, or document why the first-survivor inconsistency is accepted. The fix must not change the undo result of a first-kept join.