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
import {
_getModelData as getModelData,
_setModelData as setModelData,
Bookmark,
ClassicEditor,
Essentials,
FindAndReplaceEditing,
type ModelElement,
Paragraph,
type ViewElement,
type ViewNode
} from "ckeditor5";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import BlockReferenceEditing from "../block_reference/block_reference_editing.js";
import Tabs from "./tabs.js";
import { revealTab } from "./tabs_read_only.js";
const TWO_TABS =
"<div class=\"trilium-tabs\">" +
"<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">Windows</p>" +
"<div class=\"trilium-tab-panel\"><p>Run the installer.</p></div></section>" +
"<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">Linux</p>" +
"<div class=\"trilium-tab-panel\"><p>Use the package.</p></div></section>" +
"</div>";
describe("Tabs", () => {
let domElement: HTMLDivElement;
let editor: ClassicEditor;
beforeEach(async () => {
domElement = document.createElement("div");
document.body.appendChild(domElement);
editor = await ClassicEditor.create(domElement, {
licenseKey: "GPL",
plugins: [BlockReferenceEditing, Bookmark, Essentials, FindAndReplaceEditing, Paragraph, Tabs]
});
});
afterEach(() => {
domElement.remove();
return editor.destroy();
});
function model() {
return getModelData(editor.model, { withoutSelection: true });
}
function activeTitles() {
const root = editor.editing.view.getDomRoot();
return [...root?.querySelectorAll(".trilium-tab-title--active") ?? []]
.map(title => title.textContent);
}
function tabsElement() {
const tabs = editor.model.document.getRoot()?.getChild(0);
expect(tabs?.is("element", "tabs")).toBe(true);
return tabs as ModelElement;
}
it("round-trips the saved HTML through the model", () => {
editor.setData(TWO_TABS);
expect(model()).toBe(
"<tabs>" +
"<tab><tabTitle>Windows</tabTitle><tabPanel><paragraph>Run the installer.</paragraph></tabPanel></tab>" +
"<tab><tabTitle>Linux</tabTitle><tabPanel><paragraph>Use the package.</paragraph></tabPanel></tab>" +
"</tabs>"
);
expect(editor.getData()).toBe(TWO_TABS);
});
it("round-trips the bottom alignment and drops the default or an unknown one", () => {
const aligned = (value: string) => TWO_TABS.replace(
"<div class=\"trilium-tabs\">",
`<div class="trilium-tabs" data-trilium-tab-alignment="${value}">`
);
editor.setData(aligned("bottom"));
expect(model()).toMatch(/^<tabs tabAlignment="bottom"><tab>/);
expect(editor.getData()).toBe(aligned("bottom"));
const widget = editor.editing.view.getDomRoot()?.querySelector(".trilium-tabs");
expect(widget?.getAttribute("data-trilium-tab-alignment")).toBe("bottom");
for (const value of ["top", "left", ""]) {
editor.setData(aligned(value));
expect(model(), value).toMatch(/^<tabs><tab>/);
expect(editor.getData(), value).toBe(TWO_TABS);
}
});
it("inserts two numbered tabs and selects the first title", () => {
setModelData(editor.model, "<paragraph>[]</paragraph>");
editor.execute("tabs");
expect(getModelData(editor.model)).toBe(
"<tabs>" +
"<tab><tabTitle>[Tab 1]</tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" +
"<tab><tabTitle>Tab 2</tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" +
"</tabs>"
);
});
it("moves selected content into the first tab and selects its title", () => {
const secondTab = "<tab><tabTitle>Tab 2</tabTitle>" +
"<tabPanel><paragraph></paragraph></tabPanel></tab>";
setModelData(editor.model, "<paragraph>Keep [moved] text</paragraph>");
editor.execute("tabs");
expect(getModelData(editor.model)).toBe(
"<paragraph>Keep </paragraph>" +
"<tabs>" +
"<tab><tabTitle>[Tab 1]</tabTitle>" +
"<tabPanel><paragraph>moved</paragraph></tabPanel></tab>" +
secondTab +
"</tabs>" +
"<paragraph> text</paragraph>"
);
setModelData(editor.model, "[<paragraph>One</paragraph><paragraph>Two</paragraph>]");
editor.execute("tabs");
expect(getModelData(editor.model)).toBe(
"<tabs>" +
"<tab><tabTitle>[Tab 1]</tabTitle>" +
"<tabPanel><paragraph>One</paragraph><paragraph>Two</paragraph></tabPanel></tab>" +
secondTab +
"</tabs>"
);
});
it("repairs tabs that lack a title or a panel and drops a block without tabs", () => {
editor.setData(
"<div class=\"trilium-tabs\">" +
"<section class=\"trilium-tab\"><div class=\"trilium-tab-panel\"><p>Body</p></div></section>" +
"<section class=\"trilium-tab\"><p class=\"trilium-tab-title\">Title</p></section>" +
"</div>" +
"<div class=\"trilium-tabs\"></div>"
);
expect(model()).toBe(
"<tabs>" +
"<tab><tabTitle></tabTitle><tabPanel><paragraph>Body</paragraph></tabPanel></tab>" +
"<tab><tabTitle>Title</tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" +
"</tabs>"
);
});
it("merges extra titles and panels of a tab into its first panel, in document order", () => {
editor.setData(
"<div class=\"trilium-tabs\">" +
"<section class=\"trilium-tab\">" +
"<p class=\"trilium-tab-title\">A</p>" +
"<p class=\"trilium-tab-title\">B</p>" +
"<div class=\"trilium-tab-panel\"><p>x</p></div>" +
"<div class=\"trilium-tab-panel\"><p>y</p></div>" +
"<p class=\"trilium-tab-title\">C</p>" +
"</section>" +
"<section class=\"trilium-tab\">" +
"<div class=\"trilium-tab-panel\"><p>z</p></div>" +
"<p class=\"trilium-tab-title\">D</p>" +
"<div class=\"trilium-tab-panel\"></div>" +
"</section>" +
"</div>"
);
expect(model()).toBe(
"<tabs>" +
"<tab><tabTitle>A</tabTitle><tabPanel>" +
"<paragraph>B</paragraph><paragraph>x</paragraph><paragraph>y</paragraph><paragraph>C</paragraph>" +
"</tabPanel></tab>" +
"<tab><tabTitle>D</tabTitle><tabPanel><paragraph>z</paragraph></tabPanel></tab>" +
"</tabs>"
);
});
it("shows the first tab, then whichever tab holds the selection", () => {
editor.setData(TWO_TABS);
expect(activeTitles()).toEqual(["Windows"]);
const linux = tabsElement().getChild(1) as ModelElement;
editor.model.change(writer => writer.setSelection(linux.getChild(1) as ModelElement, 0));
expect(activeTitles()).toEqual(["Linux"]);
editor.model.change(writer => writer.setSelection(editor.model.document.getRoot() as ModelElement, "end"));
expect(activeTitles()).toEqual(["Linux"]);
});
it("shows the tab holding the highlighted find result without moving the caret", () => {
editor.setData(TWO_TABS);
const windowsPanel = (tabsElement().getChild(0) as ModelElement).getChild(1) as ModelElement;
editor.model.change(writer => writer.setSelection(windowsPanel, 0));
const caret = getModelData(editor.model);
editor.execute("find", "the");
expect(activeTitles()).toEqual(["Windows"]);
editor.execute("findNext");
expect(activeTitles()).toEqual(["Linux"]);
expect(getModelData(editor.model)).toBe(caret);
editor.execute("findNext");
expect(activeTitles()).toEqual(["Windows"]);
editor.execute("findPrevious");
expect(activeTitles()).toEqual(["Linux"]);
});
it("shows the tab holding a link target without moving the caret", () => {
editor.setData(TWO_TABS);
const windowsPanel = (tabsElement().getChild(0) as ModelElement).getChild(1) as ModelElement;
editor.model.change(writer => writer.setSelection(windowsPanel, 0));
const caret = getModelData(editor.model);
const target = [...editor.editing.view.getDomRoot()?.querySelectorAll("p") ?? []]
.find(paragraph => paragraph.textContent === "Use the package.");
expect(target).toBeDefined();
revealTab(target as Element);
expect(activeTitles()).toEqual(["Linux"]);
expect(getModelData(editor.model)).toBe(caret);
editor.setData(TWO_TABS.replace("Run the installer.", "<a id=\"install\"></a>Run the installer."));
const linux = tabsElement().getChild(1) as ModelElement;
editor.model.change(writer => writer.setSelection(linux.getChild(1) as ModelElement, 0));
expect(activeTitles()).toEqual(["Linux"]);
const bookmark = editor.editing.view.getDomRoot()?.querySelector("#install");
expect(bookmark).toBeTruthy();
revealTab(bookmark as Element);
expect(activeTitles()).toEqual(["Windows"]);
});
it("gives the tab holding the selection a reference id, saved on its section", () => {
editor.setData(TWO_TABS);
const command = editor.commands.get("assignTabReference");
expect(command).toBeDefined();
editor.model.change(writer => writer.setSelection(tabsElement(), "on"));
expect(command?.isEnabled).toBe(false);
const linux = tabsElement().getChild(1) as ModelElement;
editor.model.change(writer => writer.setSelection(linux.getChild(1) as ModelElement, 0));
expect(command?.isEnabled).toBe(true);
const target = editor.execute("assignTabReference") as { startId: string; endId: string; count: number };
expect(target.startId).toMatch(/^[A-Za-z0-9]{12}$/);
expect(target).toEqual({ startId: target.startId, endId: target.startId, count: 1 });
const data = editor.getData();
expect(data).toContain(`<section class="trilium-tab" data-trilium-block-id="${target.startId}"><p class="trilium-tab-title">Linux</p>`);
expect(editor.execute("assignTabReference")).toEqual(target);
editor.setData(data);
expect((tabsElement().getChild(1) as ModelElement).getAttribute("blockId")).toBe(target.startId);
});
it("exposes the active tab and each panel's title to assistive technology", () => {
editor.setData(TWO_TABS);
const root = editor.editing.view.getDomRoot();
const titles = [...root?.querySelectorAll(".trilium-tab-title") ?? []];
const panels = [...root?.querySelectorAll(".trilium-tab-panel") ?? []];
expect(titles).toHaveLength(2);
expect(panels).toHaveLength(2);
expect(titles.map(title => title.getAttribute("aria-current"))).toEqual(["true", null]);
for (const [index, title] of titles.entries()) {
const panel = panels[index];
expect(panel.id).not.toBe("");
expect(title.getAttribute("aria-controls")).toBe(panel.id);
expect(panel.getAttribute("aria-labelledby")).toBe(title.id);
expect(root?.querySelector(`#${title.id}`)?.textContent).toBe(["Windows", "Linux"][index]);
}
expect(new Set([...titles, ...panels].map(element => element.id)).size).toBe(4);
const linux = tabsElement().getChild(1) as ModelElement;
editor.model.change(writer => writer.setSelection(linux.getChild(1) as ModelElement, 0));
expect(titles.map(title => title.getAttribute("aria-current"))).toEqual([null, "true"]);
});
it("adds, moves and removes the tab holding the selection", () => {
editor.setData(TWO_TABS);
const windows = tabsElement().getChild(0) as ModelElement;
editor.model.change(writer => writer.setSelection(windows.getChild(0) as ModelElement, 0));
editor.execute("insertTab");
expect(getModelData(editor.model)).toContain("<tabTitle>[Tab 2]</tabTitle>");
editor.model.change(writer => editor.model.insertContent(writer.createText("macOS")));
expect(activeTitles()).toEqual(["macOS"]);
editor.execute("moveTabRight");
expect(editor.commands.get("moveTabRight")?.isEnabled).toBe(false);
expect(editor.getData()).toContain("Windows</p>");
expect(editor.getData().indexOf("Linux")).toBeLessThan(editor.getData().indexOf("macOS"));
editor.execute("removeTab");
expect(model()).not.toContain("macOS");
expect(activeTitles()).toEqual(["Linux"]);
editor.execute("removeTab");
editor.execute("removeTab");
expect(model()).toBe("<paragraph></paragraph>");
});
it("keeps the titles in the title bar in tab order, and each panel in its section", () => {
editor.setData(TWO_TABS);
const root = editor.editing.view.getDomRoot();
const barTitles = () =>
[...root?.querySelectorAll(".trilium-tabs-bar .trilium-tab-title") ?? []]
.map(title => title.textContent);
const windows = tabsElement().getChild(0) as ModelElement;
editor.model.change(writer => writer.setSelection(windows.getChild(0) as ModelElement, 0));
editor.execute("insertTab");
editor.model.change(writer => editor.model.insertContent(writer.createText("macOS")));
expect(barTitles()).toEqual(["Windows", "macOS", "Linux"]);
editor.execute("moveTabRight");
expect(barTitles()).toEqual(["Windows", "Linux", "macOS"]);
editor.execute("undo");
expect(barTitles()).toEqual(["Windows", "macOS", "Linux"]);
editor.execute("removeTab");
expect(barTitles()).toEqual(["Windows", "Linux"]);
const linux = tabsElement().getChild(1) as ModelElement;
editor.model.change(writer => writer.remove(linux.getChild(0) as ModelElement));
expect(barTitles()).toEqual(["Windows", ""]);
editor.execute("undo");
expect(barTitles()).toEqual(["Windows", "Linux"]);
const sections = [...root?.querySelectorAll("section.trilium-tab") ?? []];
const panels = [...root?.querySelectorAll(".trilium-tab-panel") ?? []];
expect(sections.map(section => [...section.children].map(child => child.id)))
.toEqual(panels.map(panel => [panel.id]));
expect(editor.getData()).toBe(TWO_TABS);
});
it("maps positions in the title bar and the sections to the model and back", () => {
editor.setData(TWO_TABS);
const { mapper, view } = editor.editing;
const model = editor.model;
const tabs = tabsElement();
const [windows, linux] = tabs.getChildren() as IterableIterator<ModelElement>;
const tabsView = mapper.toViewElement(tabs) as ViewElement;
const section = mapper.toViewElement(linux) as ViewElement;
const windowsTitle = mapper.toViewElement(windows.getChild(0) as ModelElement);
const viewport = windowsTitle?.parent as ViewElement;
const toModelPath = (parent: ViewNode, offset: number) =>
mapper.toModelPosition(view.createPositionAt(parent, offset)).path;
const linuxTitle = mapper.toViewElement(linux.getChild(0) as ModelElement) as ViewElement;
expect(toModelPath(linuxTitle.getChild(0) as ViewNode, 2)).toEqual([0, 1, 0, 2]);
expect(toModelPath(section, 0)).toEqual([0, 1, 1]);
expect(toModelPath(section, 1)).toEqual([0, 1, 2]);
expect(toModelPath(viewport, 1)).toEqual([0, 1]);
expect(toModelPath(tabsView, tabsView.getChildIndex(section))).toEqual([0, 1]);
expect(toModelPath(tabsView, tabsView.childCount)).toEqual([0, 2]);
const toView = (parent: ModelElement, offset: number) => {
const position = mapper.toViewPosition(model.createPositionAt(parent, offset));
return [position.parent, position.offset];
};
expect(toView(linux, 0)).toEqual([viewport, 1]);
expect(toView(linux, 1)).toEqual([section, 0]);
expect(toView(tabs, 1)).toEqual([tabsView, tabsView.getChildIndex(section)]);
expect(toView(tabs, 2)).toEqual([tabsView, tabsView.getChildIndex(section) + 1]);
});
it("keeps a press on a scroll button from selecting the tabs block", () => {
editor.setData(TWO_TABS);
const windows = tabsElement().getChild(0) as ModelElement;
editor.model.change(writer => writer.setSelection(windows.getChild(0) as ModelElement, 0));
const caret = getModelData(editor.model);
const root = editor.editing.view.getDomRoot();
const press = (target: Element | null | undefined) => {
expect(target).toBeTruthy();
target?.dispatchEvent(new PointerEvent("pointerdown", {
bubbles: true, cancelable: true, isPrimary: true
}));
};
press(root?.querySelector(".scrollable-bar-edge--end > .scrollable-bar-button"));
expect(getModelData(editor.model)).toBe(caret);
press(root?.querySelector(".scrollable-bar-viewport"));
expect(editor.model.document.selection.getSelectedElement()).toBe(tabsElement());
});
it("moves the caret from a title into its panel on Enter", () => {
editor.setData(TWO_TABS);
const windows = tabsElement().getChild(0) as ModelElement;
editor.model.change(writer => writer.setSelection(windows.getChild(0) as ModelElement, "end"));
editor.editing.view.document.fire("enter", {
preventDefault() {},
domEvent: new KeyboardEvent("keydown")
});
expect(getModelData(editor.model)).toContain(
"<tabTitle>Windows</tabTitle><tabPanel><paragraph>[]Run the installer.</paragraph></tabPanel>"
);
});
});