From efd3efff005c4122a9885057dbf3ec8044079b9a Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Tue, 24 Mar 2026 08:54:01 +0100 Subject: [PATCH] :bug: Fix text-align before typing and sync attrs with v2 editor --- .../editor/controllers/SelectionController.js | 12 ++++++++++++ .../controllers/SelectionController.test.js | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/frontend/text-editor/src/editor/controllers/SelectionController.js b/frontend/text-editor/src/editor/controllers/SelectionController.js index ba06969893..e78282fa0e 100644 --- a/frontend/text-editor/src/editor/controllers/SelectionController.js +++ b/frontend/text-editor/src/editor/controllers/SelectionController.js @@ -2030,6 +2030,18 @@ export class SelectionController extends EventTarget { this.#textNodeIterator.nextNode(); } while (this.#textNodeIterator.currentNode); + } else { + // Empty paragraph uses a text span with
only (no text node). The + // selection is then on the line-break element, not a TEXT_NODE, so none + // of the branches above run — only setRootStyles applied. Paragraph + // styles (e.g. text-align) must still be applied before the user types. + const paragraph = this.startParagraph; + if (paragraph) { + setParagraphStyles(paragraph, newStyles); + for (const textSpan of paragraph.children) { + setTextSpanStyles(textSpan, newStyles); + } + } } return this.#notifyStyleChange(); } diff --git a/frontend/text-editor/src/editor/controllers/SelectionController.test.js b/frontend/text-editor/src/editor/controllers/SelectionController.test.js index ff7e372c9d..eb2deede42 100644 --- a/frontend/text-editor/src/editor/controllers/SelectionController.test.js +++ b/frontend/text-editor/src/editor/controllers/SelectionController.test.js @@ -1626,6 +1626,23 @@ describe("SelectionController", () => { ); }); + test("`applyStyles` sets paragraph styles when selection is on
(empty paragraph)", () => { + const textEditorMock = TextEditorMock.createTextEditorMockWithText(""); + const root = textEditorMock.root; + const selection = document.getSelection(); + const selectionController = new SelectionController( + textEditorMock, + selection, + ); + const lineBreak = root.firstChild.firstChild.firstChild; + expect(lineBreak.nodeName).toBe("BR"); + focus(selection, textEditorMock, lineBreak, 0, lineBreak, 0); + selectionController.applyStyles({ + "text-align": "center", + }); + expect(root.firstChild.style.textAlign).toBe("center"); + }); + test("`selectAll` should select everything", () => { const textEditorMock = TextEditorMock.createTextEditorMockWithParagraphs([ createParagraphWith(["Hello, "], {