Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/list item alignment: refactor
Adorian Doran7e43f44d8e25
diff --git a/packages/ckeditor5/src/plugins/list_item_alignment.spec.ts b/packages/ckeditor5/src/plugins/list_item_alignment.spec.tsindex a8905c69c3..fd0d826ed6 100644--- a/packages/ckeditor5/src/plugins/list_item_alignment.spec.ts+++ b/packages/ckeditor5/src/plugins/list_item_alignment.spec.ts@@ -70,6 +70,25 @@ describe("ListItemAlignment", () => {expect(values()).toEqual([ undefined, undefined, "right", "right", undefined ]);});+ it("treats the blocks around a nested list as one item", async () => {+ const editor = await createEditor();+ setModelData(editor.model,+ `<paragraph ${ITEM} listItemId="a" alignment="center">First</paragraph>` ++ `<paragraph listIndent="1" listType="bulleted" listItemId="n">Nested</paragraph>` ++ `<paragraph ${ITEM} listItemId="a">After the nested list</paragraph>` ++ `<paragraph ${ITEM} listItemId="b">Next item</paragraph>`);++ const values = () => [ 0, 1, 2, 3 ]+ .map((index) => getBlock(editor, index).getAttribute("listItemAlignment"));+ expect(values()).toEqual([ "center", undefined, "center", undefined ]);+ expect(getItemClasses(editor)).toEqual([ CENTER, "", "" ]);++ // Aligning a later block leaves the item to its first paragraph.+ const after = getBlock(editor, 2);+ editor.model.change((writer) => writer.setAttribute("alignment", "right", after));+ expect(values()).toEqual([ "center", undefined, "center", undefined ]);+ });+it("leaves the attribute alone for typing and inline formatting", async () => {const editor = await createEditor();setModelData(editor.model,diff --git a/packages/ckeditor5/src/plugins/list_item_alignment.ts b/packages/ckeditor5/src/plugins/list_item_alignment.tsindex ae28ce7ac5..30d308df9f 100644--- a/packages/ckeditor5/src/plugins/list_item_alignment.ts+++ b/packages/ckeditor5/src/plugins/list_item_alignment.ts@@ -1,6 +1,6 @@import {- _getAllListItemBlocks, _isListItemBlock, ListEditing, type ModelElement, type ModelItem,- type ModelWriter, Plugin, type UpcastElementEvent+ ListEditing, type ModelElement, type ModelItem, type ModelWriter, Plugin,+ type UpcastElementEvent} from "ckeditor5";import {@@ -72,7 +72,7 @@ export default class ListItemAlignment extends Plugin {private syncAlignments(writer: ModelWriter) {const blocks = new Set<ModelElement>();const add = (item: ModelItem | null) => {- if (_isListItemBlock(item)) {+ if (isListBlock(item)) {blocks.add(item);}};@@ -102,7 +102,7 @@ export default class ListItemAlignment extends Plugin {if (synced.has(block)) {continue;}- const itemBlocks = _getAllListItemBlocks(block);+ const itemBlocks = getItemBlocks(block);const paragraph = itemBlocks.find((itemBlock) => itemBlock.is("element", "paragraph"));const alignment = paragraph?.getAttribute("alignment");const value = isListItemAlignment(alignment) ? alignment : undefined;@@ -123,6 +123,40 @@ export default class ListItemAlignment extends Plugin {}}+function isListBlock(item: ModelItem | null): item is ModelElement {+ return !!item && item.is("element") && item.hasAttribute("listItemId");+}++/**+ * The blocks of the list item that holds `block`, in document order. The blocks of one item can+ * have a nested list between them, at a higher indent.+ */+function getItemBlocks(block: ModelElement) {+ const indent = block.getAttribute("listIndent") as number;+ const itemId = block.getAttribute("listItemId");+ const blocks = [ block ];+ for (const direction of [ "previousSibling", "nextSibling" ] as const) {+ for (+ let sibling = block[direction];+ isListBlock(sibling) && (sibling.getAttribute("listIndent") as number) >= indent;+ sibling = sibling[direction]+ ) {+ if (sibling.getAttribute("listIndent") !== indent) {+ continue;+ }+ if (sibling.getAttribute("listItemId") !== itemId) {+ break;+ }+ if (direction === "previousSibling") {+ blocks.unshift(sibling);+ } else {+ blocks.push(sibling);+ }+ }+ }+ return blocks;+}+declare module "ckeditor5" {interface PluginsMap {[ListItemAlignment.pluginName]: ListItemAlignment;