Skip to content

Build your personal knowledge base with Trilium Notes

client: fix some performance issues

Adorian Dorane42f5bece900
diff --git a/apps/client/src/services/content_renderer_text.spec.ts b/apps/client/src/services/content_renderer_text.spec.ts
index 882acd7552..979999b53f 100644
--- a/apps/client/src/services/content_renderer_text.spec.ts
+++ b/apps/client/src/services/content_renderer_text.spec.ts
@@ -311,6 +311,15 @@ describe("Text content renderer", () => {
expect(tableWidth(excerpt)).toBe("800px");
});
+ it("marks the list items whose first paragraph is aligned", async () => {
+ const full = document.createElement("div");
+ await renderTextContent(buildNote({ title: "Lists" }),
+ "<ul><li><p style=\"text-align:center;\">a</p></li><li>b</li></ul>", $(full));
+
+ expect([ ...full.querySelectorAll("li") ].map((item) => item.className))
+ .toEqual([ "trilium-list-item-aligned-center", "" ]);
+ });
+
it("renders nothing for an empty note with no children when noChildrenList is set", async () => {
const contentEl = document.createElement("div");
const note = buildNote({ title: "Empty note" });
diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.ts
index 8b5c643001..44198e5fb8 100644
--- a/apps/client/src/services/content_renderer_text.ts
+++ b/apps/client/src/services/content_renderer_text.ts
@@ -1,3 +1,6 @@
+import {
+ applyListItemAlignment
+} from "@triliumnext/ckeditor5/src/plugins/list_item_alignment_read_only.js";
import { applyTableScroll } from "@triliumnext/ckeditor5/src/plugins/table_scroll_read_only.js";
import {
applyStickyTableHeaders
@@ -55,6 +58,7 @@ export async function renderTextContent(
} else if (hasContent) {
addContentDisposer($content[0], applyTableScroll($content[0]));
addContentDisposer($content[0], applyStickyTableHeaders($content[0]));
+ applyListItemAlignment($content[0]);
// Runs before `postProcessRichContent()` renders embeds, so their blocks are not searched.
if (options.highlightBlock) {
highlightBlockReference($content[0], options.highlightBlock);
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index fb87661b29..d73bf9de88 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1290,7 +1290,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t
}
&:has(.include-note-content[hidden]) > .ck-widget__resize-handles
- > :not(.ck-widget__resize-handle_width) {
+ > .ck-widget__resize-handle:not(.ck-widget__resize-handle_width) {
display: none;
}
}
diff --git a/apps/client/src/widgets/dialogs/revisions.spec.tsx b/apps/client/src/widgets/dialogs/revisions.spec.tsx
index bd3b2c6d89..364433eccf 100644
--- a/apps/client/src/widgets/dialogs/revisions.spec.tsx
+++ b/apps/client/src/widgets/dialogs/revisions.spec.tsx
@@ -53,6 +53,16 @@ describe("RevisionContentText", () => {
});
});
+ it("marks the list items whose first paragraph is aligned", async () => {
+ const container = await renderRevision(
+ "<p>before</p><ul><li><p style=\"text-align:center;\">a</p></li><li>b</li></ul>");
+
+ await vi.waitFor(() => {
+ expect([ ...container.querySelectorAll("li") ].map((item) => item.className))
+ .toEqual([ "trilium-list-item-aligned-center", "" ]);
+ });
+ });
+
it("resolves reference link titles against the current note tree", async () => {
const note = buildNote({ title: "Target note" });
const container = await renderRevision(
diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsx
index b327060035..1843b6da22 100644
--- a/apps/client/src/widgets/dialogs/revisions.tsx
+++ b/apps/client/src/widgets/dialogs/revisions.tsx
@@ -1,5 +1,8 @@
import "./revisions.css";
+import {
+ applyListItemAlignment
+} from "@triliumnext/ckeditor5/src/plugins/list_item_alignment_read_only.js";
import { dayjs, type RevisionItem, type RevisionPojo, safeHostname, safeLinkPreviewHref } from "@triliumnext/commons";
import clsx from "clsx";
import { diffWords } from "diff";
@@ -685,6 +688,7 @@ export function RevisionContentText({ content, noteId }: {
if (!container) return;
applyLinkEmbeds(container);
+ applyListItemAlignment(container);
void applyReferenceLinks(container, noteId);
}, [content, noteId]);
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
index 0e63f7e52f..f16835241b 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.spec.tsx
@@ -562,6 +562,22 @@ describe("ReadOnlyText Mermaid diagrams", () => {
});
});
+describe("ReadOnlyText lists", () => {
+ it("marks the list items whose first paragraph is aligned", async () => {
+ const harness = setupHarness({ isVisible: true });
+ harness.note.getBlob = async () => ({
+ content: "<ul><li><p style=\"text-align:right;\">a</p></li><li>b</li></ul>"
+ }) as never;
+
+ await harness.mount();
+
+ expect([ ...harness.container.querySelectorAll("li") ].map((item) => item.className))
+ .toEqual([ "trilium-list-item-aligned-right", "" ]);
+ await act(async () => render(null, harness.container));
+ harness.container.remove();
+ });
+});
+
describe("ReadOnlyText tables", () => {
it("sizes its tables for scrolling and keeps their header rows in view", async () => {
const harness = setupHarness({ isVisible: true });
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index 0a55f646ff..256a292599 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -4,6 +4,9 @@ import "./ReadOnlyText.css";
// (see https://github.com/zadam/trilium/issues/1590 for example of such conflict)
import "@triliumnext/ckeditor5";
+import {
+ applyListItemAlignment
+} from "@triliumnext/ckeditor5/src/plugins/list_item_alignment_read_only.js";
import { applyTableScroll } from "@triliumnext/ckeditor5/src/plugins/table_scroll_read_only.js";
import {
applyStickyTableHeaders
@@ -164,6 +167,7 @@ export function ReadOnlyTextContent({
]));
applyNoteImageVersions(container);
+ applyListItemAlignment(container);
const disposeTableScroll = applyTableScroll(container);
const disposeStickyTableHeaders = applyStickyTableHeaders(container);
applyMath(container);
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index d445f9bfdf..6547b6451c 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -67,6 +67,7 @@ import LinkEmbedToolbar from "./plugins/link_embed/link_embed_toolbar.js";
import TodoListMultistate from "./plugins/todo_list_multistate/todo_list_multistate.js";
import TodoListUncheckOnEnter from "./plugins/todo_list_uncheck_on_enter.js";
import CollapsibleListItems from "./plugins/collapsible_list_items.js";
+import ListItemAlignment from "./plugins/list_item_alignment.js";
import TableIndent from "./plugins/table_indent.js";
import TableContextMenu from "./plugins/table_context_menu.js";
import TableMove from "./plugins/table_move/table_move.js";
@@ -121,6 +122,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
LinkEmbedToolbar,
TodoListMultistate,
CollapsibleListItems,
+ ListItemAlignment,
TableIndent,
TableContextMenu,
TableMove,
diff --git a/packages/ckeditor5/src/plugins/list_item_alignment.spec.ts b/packages/ckeditor5/src/plugins/list_item_alignment.spec.ts
new file mode 100644
index 0000000000..a8905c69c3
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/list_item_alignment.spec.ts
@@ -0,0 +1,121 @@
+import {
+ _setModelData as setModelData, Alignment, Bold, ClassicEditor, GeneralHtmlSupport, Heading,
+ List, type ModelElement, Paragraph, TodoList, Undo
+} from "ckeditor5";
+import { describe, expect, it } from "vitest";
+
+import { createTestEditor } from "../../test/editor-kit.js";
+import ListItemAlignment from "./list_item_alignment.js";
+import { applyListItemAlignment } from "./list_item_alignment_read_only.js";
+
+const PLUGINS = [ Paragraph, Heading, List, TodoList, Alignment, Bold, Undo, ListItemAlignment ];
+const ITEM = 'listIndent="0" listType="bulleted"';
+const CENTER = "trilium-list-item-aligned-center";
+const RIGHT = "trilium-list-item-aligned-right";
+
+async function createEditor(extraConfig = {}) {
+ return createTestEditor([ ...PLUGINS, GeneralHtmlSupport ], extraConfig);
+}
+
+function getBlock(editor: ClassicEditor, index: number) {
+ return editor.model.document.getRoot()?.getChild(index) as ModelElement;
+}
+
+/** The alignment classes of each `<li>` in the editing view. */
+function getItemClasses(editor: ClassicEditor) {
+ const items = editor.editing.view.getDomRoot()?.querySelectorAll("li") ?? [];
+ return [ ...items ].map((item) => [ ...item.classList ]
+ .filter((name) => name.startsWith("trilium-list-item-aligned-")).join(" "));
+}
+
+describe("ListItemAlignment", () => {
+ it("marks the items of aligned first paragraphs in the editing view only", async () => {
+ const editor = await createEditor();
+ editor.setData(
+ "<ul><li><p style=\"text-align:center;\">a</p></li>" +
+ "<li><p style=\"text-align:right;\">b</p></li><li>c</li></ul>" +
+ "<ol><li><p style=\"text-align:justify;\">d</p></li></ol>" +
+ "<ul class=\"todo-list\"><li><label class=\"todo-list__label\">" +
+ "<input type=\"checkbox\" disabled=\"disabled\"></label>" +
+ "<p style=\"text-align:center;\">e</p></li></ul>");
+
+ expect(getItemClasses(editor)).toEqual([ CENTER, RIGHT, "", "", CENTER ]);
+ expect(getBlock(editor, 0).getAttribute("listItemAlignment")).toBe("center");
+ expect(editor.getData()).not.toContain("trilium-list-item-aligned");
+ });
+
+ it("follows the item's first paragraph, on all of its blocks and through undo", async () => {
+ const editor = await createEditor();
+ setModelData(editor.model,
+ `<paragraph ${ITEM} listItemId="a">[]First</paragraph>` +
+ `<paragraph ${ITEM} listItemId="a">Second</paragraph>` +
+ `<heading1 ${ITEM} listItemId="b" alignment="center">Heading</heading1>` +
+ `<paragraph ${ITEM} listItemId="b" alignment="right">After it</paragraph>` +
+ `<heading1 ${ITEM} listItemId="c" alignment="center">Only a heading</heading1>`);
+ const values = () => [ 0, 1, 2, 3, 4 ]
+ .map((index) => getBlock(editor, index).getAttribute("listItemAlignment"));
+ expect(values()).toEqual([ undefined, undefined, "right", "right", undefined ]);
+
+ editor.execute("alignment", { value: "center" });
+ expect(values()).toEqual([ "center", "center", "right", "right", undefined ]);
+ expect(getItemClasses(editor)[0]).toBe(CENTER);
+
+ // Removing the first block makes the second one decide.
+ editor.model.change((writer) => writer.remove(getBlock(editor, 0)));
+ expect(getBlock(editor, 0).getAttribute("listItemAlignment")).toBeUndefined();
+
+ editor.execute("undo");
+ expect(values()).toEqual([ "center", "center", "right", "right", undefined ]);
+ editor.execute("undo");
+ expect(values()).toEqual([ undefined, undefined, "right", "right", undefined ]);
+ });
+
+ it("leaves the attribute alone for typing and inline formatting", async () => {
+ const editor = await createEditor();
+ setModelData(editor.model,
+ `<paragraph ${ITEM} listItemId="a" alignment="center">[Text]</paragraph>`);
+ const block = getBlock(editor, 0);
+
+ editor.execute("bold");
+ editor.model.change((writer) => writer.insertText("more", block, "end"));
+ expect(block.getAttribute("listItemAlignment")).toBe("center");
+ });
+
+ it("allows the attribute on list items only", async () => {
+ const editor = await createEditor();
+ setModelData(editor.model,
+ `<paragraph ${ITEM} listItemId="a">Item</paragraph><paragraph>Text</paragraph>`);
+ const { schema } = editor.model;
+
+ expect(schema.checkAttribute(getBlock(editor, 0), "listItemAlignment")).toBe(true);
+ expect(schema.checkAttribute(getBlock(editor, 1), "listItemAlignment")).toBe(false);
+ });
+
+ it("drops the class from pasted HTML, which General HTML Support keeps otherwise", async () => {
+ const allowAll = { name: /.*/, classes: true, attributes: true, styles: true };
+ const editor = await createEditor({ htmlSupport: { allow: [ allowAll ] } });
+ editor.setData(`<ul><li class="${CENTER} other">a</li></ul>`);
+
+ expect(editor.getData()).toContain("<li class=\"other\"");
+ expect(editor.getData()).not.toContain("trilium-list-item-aligned");
+ expect(getItemClasses(editor)).toEqual([ "" ]);
+ });
+});
+
+describe("applyListItemAlignment", () => {
+ it("marks each item by its own first paragraph, and clears a stale mark", () => {
+ const container = document.createElement("div");
+ container.innerHTML =
+ "<ul><li><p style=\"text-align:center\">a</p>" +
+ "<ul><li><p style=\"text-align:right\">b</p></li></ul></li>" +
+ "<li><p>c</p><p style=\"text-align:center\">d</p></li>" +
+ "<li class=\"trilium-list-item-aligned-left\">" +
+ "<p style=\"text-align:justify\">e</p></li>" +
+ "<li>f</li></ul>";
+
+ applyListItemAlignment(container);
+
+ expect([ ...container.querySelectorAll("li") ].map((item) => item.className))
+ .toEqual([ CENTER, RIGHT, "", "", "" ]);
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/list_item_alignment.ts b/packages/ckeditor5/src/plugins/list_item_alignment.ts
new file mode 100644
index 0000000000..ae28ce7ac5
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/list_item_alignment.ts
@@ -0,0 +1,130 @@
+import {
+ _getAllListItemBlocks, _isListItemBlock, ListEditing, type ModelElement, type ModelItem,
+ type ModelWriter, Plugin, type UpcastElementEvent
+} from "ckeditor5";
+
+import {
+ getListItemAlignmentClass, isListItemAlignment, LIST_ITEM_ALIGNMENTS,
+ type ListItemAlignmentValue
+} from "./list_item_alignment_read_only.js";
+
+/** The alignment of the item's first paragraph, set on every block of the item. */
+const ATTRIBUTE = "listItemAlignment";
+
+/** The changes that can move an item's blocks or the alignment of its first paragraph. */
+const SOURCE_ATTRIBUTES = new Set([ "alignment", "listItemId", "listIndent", "listType" ]);
+
+/**
+ * Puts a class on each list item whose first paragraph is aligned, so that `lists.css` can move
+ * the item, and its marker, next to the text. The class exists in the editing view only: saved
+ * content keeps the paragraph's `text-align`, and `applyListItemAlignment()` adds the class where
+ * that content is shown read-only.
+ */
+export default class ListItemAlignment extends Plugin {
+
+ static get requires() {
+ return [ ListEditing ] as const;
+ }
+
+ static get pluginName() {
+ return "ListItemAlignment" as const;
+ }
+
+ init() {
+ const editor = this.editor;
+
+ // Every block of an item has its own `<li>`, and the editing view merges them into one.
+ editor.plugins.get(ListEditing).registerDowncastStrategy({
+ scope: "item",
+ attributeName: ATTRIBUTE,
+ setAttributeOnDowncast(writer, value, element, options) {
+ if (!options.dataPipeline) {
+ const name = getListItemAlignmentClass(value as ListItemAlignmentValue);
+ writer.addClass(name, element);
+ }
+ }
+ });
+
+ // Read-only content has the classes, and General HTML Support keeps pasted classes.
+ editor.conversion.for("upcast").add((dispatcher) => {
+ dispatcher.on<UpcastElementEvent>("element:li", (_evt, data, conversionApi) => {
+ for (const alignment of LIST_ITEM_ALIGNMENTS) {
+ const classes = getListItemAlignmentClass(alignment);
+ conversionApi.consumable.consume(data.viewItem, { classes });
+ }
+ }, { priority: "high" });
+ });
+
+ editor.model.document.registerPostFixer((writer) => this.syncAlignments(writer));
+ }
+
+ afterInit() {
+ const schema = this.editor.model.schema;
+ schema.extend("$listItem", { allowAttributes: ATTRIBUTE });
+ schema.addAttributeCheck((context) => {
+ if (!context.last.getAttribute("listItemId")) {
+ return false;
+ }
+ }, ATTRIBUTE);
+ }
+
+ /** Updates the attribute on the items of the blocks that the last change touched. */
+ private syncAlignments(writer: ModelWriter) {
+ const blocks = new Set<ModelElement>();
+ const add = (item: ModelItem | null) => {
+ if (_isListItemBlock(item)) {
+ blocks.add(item);
+ }
+ };
+
+ for (const change of this.editor.model.document.differ.getChanges()) {
+ if (change.type === "attribute") {
+ if (SOURCE_ATTRIBUTES.has(change.attributeKey)) {
+ for (const item of change.range.getItems({ shallow: true })) {
+ add(item);
+ }
+ }
+ } else {
+ add(change.position.nodeBefore);
+ add(change.position.nodeAfter);
+ if (change.type === "insert" && change.name !== "$text") {
+ const end = change.position.getShiftedBy(change.length);
+ for (const item of writer.createRange(change.position, end).getItems()) {
+ add(item);
+ }
+ }
+ }
+ }
+
+ const synced = new Set<ModelElement>();
+ let isChanged = false;
+ for (const block of blocks) {
+ if (synced.has(block)) {
+ continue;
+ }
+ const itemBlocks = _getAllListItemBlocks(block);
+ const paragraph = itemBlocks.find((itemBlock) => itemBlock.is("element", "paragraph"));
+ const alignment = paragraph?.getAttribute("alignment");
+ const value = isListItemAlignment(alignment) ? alignment : undefined;
+ for (const itemBlock of itemBlocks) {
+ synced.add(itemBlock);
+ if (itemBlock.getAttribute(ATTRIBUTE) === value) {
+ continue;
+ }
+ if (value) {
+ writer.setAttribute(ATTRIBUTE, value, itemBlock);
+ } else {
+ writer.removeAttribute(ATTRIBUTE, itemBlock);
+ }
+ isChanged = true;
+ }
+ }
+ return isChanged;
+ }
+}
+
+declare module "ckeditor5" {
+ interface PluginsMap {
+ [ListItemAlignment.pluginName]: ListItemAlignment;
+ }
+}
diff --git a/packages/ckeditor5/src/plugins/list_item_alignment_read_only.ts b/packages/ckeditor5/src/plugins/list_item_alignment_read_only.ts
new file mode 100644
index 0000000000..0afc18f8e0
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/list_item_alignment_read_only.ts
@@ -0,0 +1,27 @@
+/** The paragraph alignments that `lists.css` moves a list item for. */
+export const LIST_ITEM_ALIGNMENTS = [ "left", "center", "right" ] as const;
+
+export type ListItemAlignmentValue = typeof LIST_ITEM_ALIGNMENTS[number];
+
+/** The class `lists.css` styles on a list item whose first paragraph has `alignment`. */
+export function getListItemAlignmentClass(alignment: ListItemAlignmentValue) {
+ return `trilium-list-item-aligned-${alignment}`;
+}
+
+export function isListItemAlignment(value: unknown): value is ListItemAlignmentValue {
+ return (LIST_ITEM_ALIGNMENTS as readonly unknown[]).includes(value);
+}
+
+/**
+ * Puts the alignment class on each list item in `container` whose first paragraph is aligned,
+ * for saved content shown outside the editor. The `ListItemAlignment` plugin does the same in
+ * the editor.
+ */
+export function applyListItemAlignment(container: ParentNode) {
+ for (const item of container.querySelectorAll("li")) {
+ const alignment = item.querySelector<HTMLElement>(":scope > p")?.style.textAlign;
+ for (const value of LIST_ITEM_ALIGNMENTS) {
+ item.classList.toggle(getListItemAlignmentClass(value), value === alignment);
+ }
+ }
+}
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
index 5c32d5e3c2..b29a1ab58f 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_layout.spec.ts
@@ -172,6 +172,24 @@ describe("tabs layout styles", () => {
}
});
+ it("trims the outer margins of the shown panel's content, saved and edited", async () => {
+ const html = `<div class="trilium-tabs">` +
+ `${tab("A", "<p>First</p><p>Middle</p><p>Last</p>")}${tab("B", "<p>Other</p>")}</div>`;
+ const editor = await createTestEditor([Essentials, Paragraph, Tabs]);
+ editor.setData(html);
+ const editable = editor.ui.view.editable.element as HTMLElement;
+
+ for (const root of [renderSaved(html).container, editable]) {
+ const panel = root.querySelector(".trilium-tab--active > .trilium-tab-panel");
+ const blocks = [...panel?.querySelectorAll(":scope > p") ?? []];
+ expect(blocks.map(block => block.textContent)).toEqual(["First", "Middle", "Last"]);
+ const [first, middle, last] = blocks.map(block => getComputedStyle(block));
+ expect({ top: first.marginTop, bottom: last.marginBottom })
+ .toEqual({ top: "0px", bottom: "0px" });
+ expect(middle.marginTop).not.toBe("0px");
+ }
+ });
+
it("clips a long title at 30% of the block (45% on mobile) and scrolls it back", async () => {
const long = tab("A title far too long for a third of the block", "<p>Body</p>");
const html = `<div class="trilium-tabs">${long}${tab("B", "")}</div>`;
diff --git a/packages/ckeditor5/src/theme/lists.css b/packages/ckeditor5/src/theme/lists.css
index e556e37f34..20066883c5 100644
--- a/packages/ckeditor5/src/theme/lists.css
+++ b/packages/ckeditor5/src/theme/lists.css
@@ -1,23 +1,24 @@
/*
* An aligned list item shrinks to its text and moves to that side, so that its marker or to-do
* checkbox stays next to the text. The selectors match the `style` that `TableCellProperties`
- * writes on a cell and that `Alignment` writes on the item's paragraph.
+ * writes on a cell, and the class that `ListItemAlignment` (in the editor) and
+ * `applyListItemAlignment()` (elsewhere) put on an item whose first paragraph is aligned.
*/
.ck-content {
:is(td, th)[style*="text-align:center"] li,
- li:has(> p:first-of-type[style*="text-align:center"]) {
+ li.trilium-list-item-aligned-center {
width: fit-content;
margin-inline: auto;
}
:is(td, th)[style*="text-align:right"] li,
- li:has(> p:first-of-type[style*="text-align:right"]) {
+ li.trilium-list-item-aligned-right {
width: fit-content;
margin-left: auto;
}
:is(td, th)[style*="text-align:left"] li,
- li:has(> p:first-of-type[style*="text-align:left"]) {
+ li.trilium-list-item-aligned-left {
width: fit-content;
margin-right: auto;
}
diff --git a/packages/ckeditor5/src/theme/lists.spec.ts b/packages/ckeditor5/src/theme/lists.spec.ts
index e2f2e24857..fd96cde758 100644
--- a/packages/ckeditor5/src/theme/lists.spec.ts
+++ b/packages/ckeditor5/src/theme/lists.spec.ts
@@ -7,9 +7,19 @@ import type { ClassicEditor } from "ckeditor5";
import { describe, expect, it, onTestFinished } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
+import ListItemAlignment from "../plugins/list_item_alignment.js";
+import { applyListItemAlignment } from "../plugins/list_item_alignment_read_only.js";
-const PLUGINS = [Essentials, Paragraph, List, TodoList, Table, TableCellProperties, Alignment];
+const PLUGINS = [
+ Essentials, Paragraph, List, TodoList, Table, TableCellProperties, Alignment, ListItemAlignment
+];
const TODO_ITEM = `<li><label class="todo-list__label"><input type="checkbox" disabled="disabled"></label>`;
+const ALIGNED_ITEMS = `
+ <ul><li><p style="text-align:center">a</p></li></ul>
+ <ol><li><p style="text-align:right">b</p></li></ol>
+ <ul><li>c</li></ul>
+ <ul class="todo-list">${TODO_ITEM}<p style="text-align:center">d</p></li></ul>
+`;
describe("lists.css", () => {
it("moves the items of a list in a centered or right-aligned cell with their text", async () => {
@@ -22,7 +32,7 @@ describe("lists.css", () => {
<td><ul><li>d</li></ul></td>
<td style="text-align:center"><ul class="todo-list">${TODO_ITEM}<p>e</p></li></ul></td>
</tr></tbody></table></figure>`);
- const [ centered, rightAligned, plain, todo ] = items(editor);
+ const [ centered, rightAligned, plain, todo ] = items(editingRoot(editor));
expectCentered(centered);
expectRightAligned(rightAligned);
@@ -32,13 +42,23 @@ describe("lists.css", () => {
});
it("moves list items aligned with the alignment button with their text", async () => {
- const editor = await editorWith(`
- <ul><li><p style="text-align:center">a</p></li></ul>
- <ol><li><p style="text-align:right">b</p></li></ol>
- <ul><li>c</li></ul>
- <ul class="todo-list">${TODO_ITEM}<p style="text-align:center">d</p></li></ul>
- `);
- const [ centered, rightAligned, plain, todo ] = items(editor);
+ const editor = await editorWith(ALIGNED_ITEMS);
+ const [ centered, rightAligned, plain, todo ] = items(editingRoot(editor));
+
+ expectCentered(centered);
+ expectRightAligned(rightAligned);
+ expectFullWidth(plain);
+ expectCentered(todo);
+ expectCheckboxBeforeText(todo);
+ });
+
+ it("moves aligned list items in saved content shown outside the editor", () => {
+ const container = document.body.appendChild(document.createElement("div"));
+ onTestFinished(() => container.remove());
+ container.className = "ck-content";
+ container.innerHTML = ALIGNED_ITEMS;
+ applyListItemAlignment(container);
+ const [ centered, rightAligned, plain, todo ] = items(container);
expectCentered(centered);
expectRightAligned(rightAligned);
@@ -53,7 +73,7 @@ describe("lists.css", () => {
<p>-</p>
<ul><li>b</li></ul>
`, "ar");
- const [ leftAligned, plain ] = items(editor);
+ const [ leftAligned, plain ] = items(editingRoot(editor));
expect(leftAligned.getBoundingClientRect().left).toBeCloseTo(listBox(leftAligned).left, 0);
expect(leftAligned.getBoundingClientRect().width).toBeLessThan(listBox(leftAligned).width / 2);
@@ -67,13 +87,17 @@ async function editorWith(data: string, contentLanguage = "en") {
return editor;
}
-/** The first list item of each top-level list, or of each table cell, in document order. */
-function items(editor: ClassicEditor) {
+function editingRoot(editor: ClassicEditor) {
const root = editor.editing.view.getDomRoot();
if (!root) {
throw new Error("The editor has no editing root.");
}
- const lists = root.querySelectorAll(":is(td, .ck-editor__editable_inline) > :is(ul, ol)");
+ return root;
+}
+
+/** The first list item of each top-level list, or of each table cell, in document order. */
+function items(root: HTMLElement) {
+ const lists = root.querySelectorAll(":is(td, .ck-content) > :is(ul, ol)");
const result: HTMLLIElement[] = [];
for (const list of lists) {
const item = list.querySelector("li");
diff --git a/packages/ckeditor5/src/theme/tabs.css b/packages/ckeditor5/src/theme/tabs.css
index 39a7978040..16e85d66b2 100644
--- a/packages/ckeditor5/src/theme/tabs.css
+++ b/packages/ckeditor5/src/theme/tabs.css
@@ -67,14 +67,6 @@
padding: .75rem 1rem;
border-radius: 0 var(--_tabs-border-radius) var(--_tabs-border-radius);
background: var(--_tabs-background);
-
- & > :first-child {
- margin-top: 0;
- }
-
- & > :last-child {
- margin-bottom: 0;
- }
}
& > .trilium-tab.trilium-tab--active > .trilium-tab-panel {
@@ -116,6 +108,19 @@
}
}
}
+
+ /* Only the active panel is shown. Kept out of the `:has()` block: under a `:has()`, a selector
+ ending without a class or tag makes Chromium restyle whole subtrees. */
+ :is(.ck-editor__editable, .ck-content) .trilium-tabs > .trilium-tab.trilium-tab--active
+ > .trilium-tab-panel {
+ & > :first-child {
+ margin-top: 0;
+ }
+
+ & > :last-child {
+ margin-bottom: 0;
+ }
+ }
}
@media print {
diff --git a/packages/share-theme/src/index.spec.ts b/packages/share-theme/src/index.spec.ts
index 42117b3b94..9321028931 100644
--- a/packages/share-theme/src/index.spec.ts
+++ b/packages/share-theme/src/index.spec.ts
@@ -16,6 +16,7 @@ const mocks = vi.hoisted(() => ({
setupMermaid: vi.fn(),
applyTableScroll: vi.fn(),
applyStickyTableHeaders: vi.fn(),
+ applyListItemAlignment: vi.fn(),
applyTabs: vi.fn(),
revealFragment: vi.fn(),
showBlockReference: vi.fn(),
@@ -44,6 +45,9 @@ vi.mock("@triliumnext/ckeditor5/src/plugins/table_scroll_read_only.js", () => ({
vi.mock("@triliumnext/ckeditor5/src/plugins/table_sticky_header_read_only.js", () => ({
applyStickyTableHeaders: mocks.applyStickyTableHeaders
}));
+vi.mock("@triliumnext/ckeditor5/src/plugins/list_item_alignment_read_only.js", () => ({
+ applyListItemAlignment: mocks.applyListItemAlignment
+}));
vi.mock("@triliumnext/ckeditor5/src/plugins/tabs/tabs_read_only.js", () => ({
applyTabs: mocks.applyTabs,
revealFragment: mocks.revealFragment
@@ -118,6 +122,7 @@ describe("share theme entry", () => {
expect(mocks.applyTableScroll.mock.calls)
.toEqual([ [ content ], [ content?.querySelector(".include-note") ] ]);
expect(mocks.applyStickyTableHeaders.mock.calls).toEqual([ [ content ] ]);
+ expect(mocks.applyListItemAlignment.mock.calls).toEqual([ [ content ] ]);
expect(mocks.applyTabs).toHaveBeenCalledWith(content, {
placeholder: "Tab",
scrollLabels: { start: "Back", end: "On" }
diff --git a/packages/share-theme/src/index.ts b/packages/share-theme/src/index.ts
index e819649274..1513909e48 100644
--- a/packages/share-theme/src/index.ts
+++ b/packages/share-theme/src/index.ts
@@ -34,6 +34,9 @@ import "@triliumnext/ckeditor5/src/theme/table_sticky_header.css";
import "@triliumnext/ckeditor5/src/theme/todo_lists.css";
import "@triliumnext/commons/src/lib/scroll_fade.css";
+import {
+ applyListItemAlignment
+} from "@triliumnext/ckeditor5/src/plugins/list_item_alignment_read_only.js";
import { applyTableScroll } from "@triliumnext/ckeditor5/src/plugins/table_scroll_read_only.js";
import {
applyStickyTableHeaders
@@ -82,6 +85,7 @@ function setupTextNote() {
$try(() => enhanceLinkPreviews(document.body));
$try(setupTableScroll);
$try(setupStickyTableHeaders);
+ $try(setupListItemAlignment);
$try(setupTabs);
return diagrams;
}
@@ -121,6 +125,13 @@ function setupStickyTableHeaders() {
}
}
+function setupListItemAlignment() {
+ const content = document.getElementById("content");
+ if (content) {
+ applyListItemAlignment(content);
+ }
+}
+
function setupTabs() {
const content = document.getElementById("content");
if (content) {