🐛 Fix text-align before typing and sync attrs with v2 editor

This commit is contained in:
Alejandro Alonso
2026-03-24 08:54:01 +01:00
parent ee1c96f3a1
commit efd3efff00
2 changed files with 29 additions and 0 deletions

View File

@@ -2030,6 +2030,18 @@ export class SelectionController extends EventTarget {
this.#textNodeIterator.nextNode();
} while (this.#textNodeIterator.currentNode);
} else {
// Empty paragraph uses a text span with <br> 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();
}

View File

@@ -1626,6 +1626,23 @@ describe("SelectionController", () => {
);
});
test("`applyStyles` sets paragraph styles when selection is on <br> (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, "], {