-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(core): preserve text alignment in portable text converters (#1201) #1573
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
Closed
marcusbellamyshaw-cell
wants to merge
1
commit into
emdash-cms:main
from
Emdash-Bug-Testing:fix/1201-textalign-portable-text
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes text alignment being silently dropped when saving rich-text content. Paragraph and heading alignment set with the editor's align toolbar now persists through Portable Text storage and round-trips back into the editor. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/core/tests/unit/converters/text-align-round-trip.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
|
|
||
| import { portableTextToProsemirror } from "../../../src/content/converters/portable-text-to-prosemirror.js"; | ||
| import { prosemirrorToPortableText } from "../../../src/content/converters/prosemirror-to-portable-text.js"; | ||
| import type { | ||
| ProseMirrorDocument, | ||
| PortableTextTextBlock, | ||
| } from "../../../src/content/converters/types.js"; | ||
|
|
||
| describe("Text alignment round-trip (core converters)", () => { | ||
| it("preserves paragraph textAlign through PM → PT → PM", () => { | ||
| const doc: ProseMirrorDocument = { | ||
| type: "doc", | ||
| content: [ | ||
| { | ||
| type: "paragraph", | ||
| attrs: { textAlign: "center" }, | ||
| content: [{ type: "text", text: "Centered" }], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| // PM → PT | ||
| const pt = prosemirrorToPortableText(doc); | ||
| const block = pt[0] as PortableTextTextBlock; | ||
| expect(block._type).toBe("block"); | ||
| expect(block.textAlign).toBe("center"); | ||
|
|
||
| // PT → PM | ||
| const pm = portableTextToProsemirror(pt); | ||
| expect(pm.content[0].type).toBe("paragraph"); | ||
| expect(pm.content[0].attrs?.textAlign).toBe("center"); | ||
| }); | ||
|
|
||
| it("preserves heading textAlign through PM → PT → PM", () => { | ||
| const doc: ProseMirrorDocument = { | ||
| type: "doc", | ||
| content: [ | ||
| { | ||
| type: "heading", | ||
| attrs: { level: 2, textAlign: "right" }, | ||
| content: [{ type: "text", text: "Right heading" }], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const pt = prosemirrorToPortableText(doc); | ||
| const block = pt[0] as PortableTextTextBlock; | ||
| expect(block.style).toBe("h2"); | ||
| expect(block.textAlign).toBe("right"); | ||
|
|
||
| const pm = portableTextToProsemirror(pt); | ||
| expect(pm.content[0].type).toBe("heading"); | ||
| expect(pm.content[0].attrs?.level).toBe(2); | ||
| expect(pm.content[0].attrs?.textAlign).toBe("right"); | ||
| }); | ||
|
|
||
| it("does not add textAlign for default-aligned (left / unset) content", () => { | ||
| const doc: ProseMirrorDocument = { | ||
| type: "doc", | ||
| content: [ | ||
| { type: "paragraph", attrs: { textAlign: "left" }, content: [{ type: "text", text: "A" }] }, | ||
| { type: "paragraph", content: [{ type: "text", text: "B" }] }, | ||
| ], | ||
| }; | ||
|
|
||
| const pt = prosemirrorToPortableText(doc); | ||
| expect((pt[0] as PortableTextTextBlock).textAlign).toBeUndefined(); | ||
| expect((pt[1] as PortableTextTextBlock).textAlign).toBeUndefined(); | ||
|
|
||
| // PT without textAlign must not emit the attr back into PM | ||
| const pm = portableTextToProsemirror(pt); | ||
| expect(pm.content[0].attrs?.textAlign).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[needs fixing]
convertParagraph(here) andconvertHeading(line 170) now forwardtextAlign, but the two other paths that build a text block from an innerparagraphnode still drop it.convertBlockquote(line 266) andconvertListItem(line 213) push their Portable Text blocks without callingextractTextAlign, so alignment set on a paragraph inside a blockquote or list item is silently lost on save.This is reachable in the editor: the align toolbar is rendered unconditionally (
packages/admin/src/components/PortableTextEditor.tsx:3040) andTextAlign.configure({ types: ["heading", "paragraph"] })(PortableTextEditor.tsx:2237) applies the attr to paragraph nodes — including the paragraphs nested inside blockquotes and list items. A user can center-align a blockquote paragraph, save, and have it snap back to left on reload: the exact bug this PR fixes, one node path over.The reverse direction has the matching gap, so even a hand-edited aligned block won't round-trip:
convertTextBlock'sblockquotecase ignoresalignAttr(portable-text-to-prosemirror.ts:176), andconvertListItem(portable-text-to-prosemirror.ts:239) doesn't forwarditem.textAlignonto its inner paragraph.Mirroring
convertParagraphinconvertBlockquotecloses the save side:The same
textAlign: extractTextAlign(child)inconvertListItem(line 213), plus applyingalignAttrin the two reverse-direction spots, would make alignment round-trip consistently across all paragraph-bearing nodes.