Skip to content

Build your personal knowledge base with Trilium Notes

test(text): bring the tabs block to full coverage

Elian Doran57cc3546aa2e
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.ts
new file mode 100644
index 0000000000..4061f59d6d
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.ts
@@ -0,0 +1,74 @@
+import {
+ _getModelData as getModelData,
+ ClassicEditor,
+ Essentials,
+ type ModelElement,
+ Paragraph
+} from "ckeditor5";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import Tabs from "./tabs.js";
+
+function tab(title: string, body = "") {
+ return `<section class="trilium-tab"><p class="trilium-tab-title">${title}</p>` +
+ `<div class="trilium-tab-panel"><p>${body}</p></div></section>`;
+}
+
+describe("tabs commands", () => {
+ let domElement: HTMLDivElement;
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ domElement = document.createElement("div");
+ document.body.appendChild(domElement);
+ editor = await ClassicEditor.create(domElement, {
+ licenseKey: "GPL",
+ plugins: [Essentials, Paragraph, Tabs]
+ });
+ });
+
+ afterEach(() => {
+ domElement.remove();
+ return editor.destroy();
+ });
+
+ function titles(tabs: ModelElement) {
+ return [...tabs.getChildren()].map(child => {
+ const title = (child as ModelElement).getChild(0) as ModelElement;
+ return [...title.getChildren()].map(text => text.is("$text") ? text.data : "").join("");
+ });
+ }
+
+ it("adds a tab at the end of a block selected as a whole, including a nested one", () => {
+ editor.setData(
+ `<div class="trilium-tabs">${tab("A")}<section class="trilium-tab">` +
+ `<p class="trilium-tab-title">B</p><div class="trilium-tab-panel">` +
+ `<div class="trilium-tabs">${tab("B1")}${tab("B2")}</div>` +
+ `</div></section></div>`
+ );
+ const outer = editor.model.document.getRoot()?.getChild(0) as ModelElement;
+ const inner = ((outer.getChild(1) as ModelElement).getChild(1) as ModelElement).getChild(0) as ModelElement;
+
+ editor.model.change(writer => writer.setSelection(inner, "on"));
+ editor.execute("insertTab");
+ expect(titles(inner)).toEqual(["B1", "B2", "Tab 3"]);
+ expect(titles(outer)).toEqual(["A", "B"]);
+
+ editor.model.change(writer => writer.setSelection(outer, "on"));
+ editor.execute("insertTab");
+ expect(titles(outer)).toEqual(["A", "B", "Tab 3"]);
+ expect(getModelData(editor.model)).toContain("<tabTitle>[Tab 3]</tabTitle>");
+ });
+
+ it("moves a tab left and disables the move at either end", () => {
+ editor.setData(`<div class="trilium-tabs">${tab("A")}${tab("B")}</div>`);
+ const tabs = editor.model.document.getRoot()?.getChild(0) as ModelElement;
+ editor.model.change(writer => writer.setSelection((tabs.getChild(1) as ModelElement).getChild(0) as ModelElement, 0));
+
+ editor.execute("moveTabLeft");
+
+ expect(titles(tabs)).toEqual(["B", "A"]);
+ expect(editor.commands.get("moveTabLeft")?.isEnabled).toBe(false);
+ expect(editor.commands.get("moveTabRight")?.isEnabled).toBe(true);
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts b/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts
index 8a404d77b1..6272c13d03 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts
@@ -29,7 +29,7 @@ export class InsertTabsCommand extends Command {
}
/**
- * Adds a tab next to the one holding the selection, numbered by its position, and selects its
+ * Adds a tab after the one holding the selection, numbered by its position, and selects its
* title. With the whole block selected, the tab goes at the end.
*/
export class InsertTabCommand extends Command {
@@ -38,22 +38,23 @@ export class InsertTabCommand extends Command {
this.isEnabled = !!getSelectedTabs(this.editor);
}
- public override execute({ position = "after" }: { position?: "before" | "after" } = {}): void {
+ public override execute(): void {
const editor = this.editor;
const tabs = getSelectedTabs(editor);
+ /* v8 ignore next 3 -- execute() runs only while refresh() keeps the command enabled */
if (!tabs) {
return;
}
- const currentTab = getSelectedTab(editor);
-
- const index = currentTab
- ? (currentTab.index ?? 0) + (position === "after" ? 1 : 0)
- : tabs.childCount;
+ // With a nested block selected as a whole, the selection's tab belongs to the outer block.
+ const selectedTab = getSelectedTab(editor);
+ const currentTab = selectedTab?.parent === tabs ? selectedTab : null;
+ // A tabs block holds only elements, so the offset after a tab is its index plus one.
+ const index = currentTab ? editor.model.createPositionAfter(currentTab).offset : tabs.childCount;
editor.model.change(writer => {
const tab = createTab(writer, getDefaultTitle(editor, index));
if (currentTab) {
- writer.insert(tab, currentTab, position);
+ writer.insert(tab, currentTab, "after");
} else {
writer.insert(tab, tabs, "end");
}
@@ -76,6 +77,7 @@ export class RemoveTabCommand extends Command {
const editor = this.editor;
const tab = getSelectedTab(editor);
const tabs = tab?.parent;
+ /* v8 ignore next 3 -- execute() runs only while refresh() keeps the command enabled */
if (!tab || !tabs?.is("element", ELEMENTS.tabs)) {
return;
}
@@ -91,11 +93,7 @@ export class RemoveTabCommand extends Command {
const neighbor = (tab.nextSibling ?? tab.previousSibling) as ModelElement;
writer.remove(tab);
-
- const title = getTitle(neighbor);
- if (title) {
- writer.setSelection(title, "end");
- }
+ writer.setSelection(getTitle(neighbor), "end");
});
}
}
@@ -119,6 +117,7 @@ export class MoveTabCommand extends Command {
const editor = this.editor;
const tab = getSelectedTab(editor);
const neighbor = this.getNeighbor(tab);
+ /* v8 ignore next 3 -- execute() runs only while refresh() keeps the command enabled */
if (!tab || !neighbor) {
return;
}
@@ -169,9 +168,9 @@ export function getSelectedTabs(editor: Editor): ModelElement | null {
return (position?.findAncestor(ELEMENTS.tabs) as ModelElement | null) ?? null;
}
-function getTitle(tab: ModelElement): ModelElement | null {
- const title = tab.getChild(0);
- return title?.is("element", ELEMENTS.tabTitle) ? title : null;
+/** Returns the title of `tab`, which the post-fixer keeps as its first child. */
+function getTitle(tab: ModelElement): ModelElement {
+ return tab.getChild(0) as ModelElement;
}
/** Returns "Tab N" for the tab at the zero-based `index`. */
@@ -181,8 +180,5 @@ function getDefaultTitle(editor: Editor, index: number): string {
}
function selectTitle(writer: ModelWriter, tab: ModelElement) {
- const title = getTitle(tab);
- if (title) {
- writer.setSelection(writer.createRangeIn(title));
- }
+ writer.setSelection(writer.createRangeIn(getTitle(tab)));
}
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_editing.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_editing.spec.ts
new file mode 100644
index 0000000000..63e81ef1e0
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.spec.ts
@@ -0,0 +1,82 @@
+import {
+ _getModelData as getModelData,
+ ClassicEditor,
+ Essentials,
+ FindAndReplaceEditing,
+ type ModelElement,
+ Paragraph
+} from "ckeditor5";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import Tabs from "./tabs.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 it.</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>" +
+ "<p>Read the notes.</p>";
+
+describe("TabsEditing", () => {
+ let domElement: HTMLDivElement;
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ domElement = document.createElement("div");
+ document.body.appendChild(domElement);
+ editor = await ClassicEditor.create(domElement, {
+ licenseKey: "GPL",
+ plugins: [Essentials, FindAndReplaceEditing, Paragraph, Tabs]
+ });
+ editor.setData(TWO_TABS);
+ });
+
+ afterEach(() => {
+ domElement.remove();
+ return editor.destroy();
+ });
+
+ function activeTitles() {
+ const root = editor.editing.view.getDomRoot();
+ return [...root?.querySelectorAll(".trilium-tab--active > .trilium-tab-title") ?? []]
+ .map(title => title.textContent);
+ }
+
+ function linux() {
+ const tabs = editor.model.document.getRoot()?.getChild(0) as ModelElement;
+ return tabs.getChild(1) as ModelElement;
+ }
+
+ it("keeps the tab showing for a find result outside any tab, no result, or a node outside the view", () => {
+ editor.model.change(writer => writer.setSelection(linux().getChild(1) as ModelElement, 0));
+
+ editor.execute("find", "the");
+ expect(activeTitles()).toEqual(["Linux"]);
+ editor.execute("findNext");
+ expect(activeTitles()).toEqual(["Linux"]);
+
+ editor.execute("find", "nowhere");
+ expect(activeTitles()).toEqual(["Linux"]);
+
+ editor.plugins.get("TabsEditing").showTabsAroundDomNode(document.createElement("p"));
+ expect(activeTitles()).toEqual(["Linux"]);
+ });
+
+ it("leaves Enter outside a title to the editor, and attribute changes to the post-fixer", () => {
+ const panel = linux().getChild(1) as ModelElement;
+ editor.model.change(writer => writer.setSelection(panel.getChild(0) as ModelElement, "end"));
+
+ editor.editing.view.document.fire("enter", {
+ preventDefault() {},
+ domEvent: new KeyboardEvent("keydown")
+ });
+ expect(panel.childCount).toBe(2);
+
+ const title = linux().getChild(0) as ModelElement;
+ editor.model.change(writer => writer.setAttribute("bold", true, writer.createRangeIn(title)));
+ expect(getModelData(editor.model, { withoutSelection: true }))
+ .toContain("<tabTitle><$text bold=\"true\">Linux</$text></tabTitle>");
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
index f475bc3452..844fef5853 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts
@@ -62,8 +62,7 @@ export default class TabsEditing extends Plugin {
if (active?.parent === tabs) {
return active;
}
- const first = tabs.getChild(0);
- return first?.is("element", ELEMENTS.tab) ? first : null;
+ return tabs.getChild(0) as ModelElement | null;
}
/** Shows every tab that encloses `position`, without moving the selection. */
@@ -308,13 +307,11 @@ export default class TabsEditing extends Plugin {
const editing = this.editor.editing;
editing.view.change(writer => {
for (const tabs of blocks) {
- if (tabs.root.rootName === "$graveyard") {
- continue;
- }
const active = this.getActiveTab(tabs);
for (const tab of tabs.getChildren() as IterableIterator<ModelElement>) {
const view = editing.mapper.toViewElement(tab);
const title = editing.mapper.toViewElement(tab.getChild(0) as ModelElement);
+ /* v8 ignore next 3 -- only a tab outside the document, which no caller passes, has no view */
if (!view || !title) {
continue;
}
@@ -375,6 +372,7 @@ function collectTabsWithin(node: ModelNode | null, blocks: Set<ModelElement>) {
}
function fixTabs(writer: ModelWriter, tabs: ModelElement): boolean {
+ /* v8 ignore next 3 -- the differ reports no change inside a removed element */
if (tabs.root.rootName === "$graveyard") {
return false;
}
@@ -386,6 +384,7 @@ function fixTabs(writer: ModelWriter, tabs: ModelElement): boolean {
let changed = false;
for (const tab of [...tabs.getChildren()]) {
+ /* v8 ignore next 3 -- the schema allows only tabs in a tabs block */
if (!tab.is("element", ELEMENTS.tab)) {
continue;
}
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts
index 9e568786b0..5c23e15a5f 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.spec.ts
@@ -1,3 +1,4 @@
+import { ClassicEditor, Essentials, Paragraph } from "ckeditor5";
import { describe, expect, it } from "vitest";
import { applyTabs, revealFragment, revealTab } from "./tabs_read_only.js";
@@ -120,6 +121,37 @@ describe("applyTabs", () => {
expect(titles[2].hasAttribute("aria-label")).toBe(false);
});
+ it("reverses the arrow keys right to left and ignores other keys", () => {
+ const container = renderTabs(
+ `<div class="trilium-tabs" dir="rtl">${tab("A", "")}${tab("B", "")}${tab("C", "")}</div>`
+ );
+ document.body.appendChild(container);
+
+ press(titleOf(container, "A"), "ArrowLeft");
+ expect(activeTitles(container)).toEqual(["B"]);
+ press(titleOf(container, "B"), "ArrowRight");
+ expect(activeTitles(container)).toEqual(["A"]);
+
+ expect(press(titleOf(container, "A"), "a").defaultPrevented).toBe(false);
+ expect(activeTitles(container)).toEqual(["A"]);
+ container.remove();
+ });
+
+ it("skips a tab without a title or a panel, and a block left without tabs", () => {
+ const container = renderTabs(
+ `<div class="trilium-tabs">` +
+ `<section class="trilium-tab"><p class="trilium-tab-title">Orphan</p></section>` +
+ `<section class="trilium-tab"><div class="trilium-tab-panel"><p id="untitled">x</p></div></section>` +
+ `</div>` +
+ `<div class="trilium-tabs">${tab("A", "")}${tab("B", "")}</div>`
+ );
+
+ expect(titleOf(container, "Orphan").hasAttribute("role")).toBe(false);
+ expect(activeTitles(container)).toEqual(["A"]);
+ revealTab(container.querySelector("#untitled") as Element);
+ expect(activeTitles(container)).toEqual(["A"]);
+ });
+
it("keeps nested tabs blocks independent", () => {
const inner = `<div class="trilium-tabs">${tab("Inner 1", "")}${tab("Inner 2", "")}</div>`;
const container = renderTabs(
@@ -166,4 +198,21 @@ describe("applyTabs", () => {
expect(revealFragment("#%E0%A4%A")).toBeNull();
container.remove();
});
+
+ it("leaves an editor without the tabs plugin alone", async () => {
+ const domElement = document.createElement("div");
+ document.body.appendChild(domElement);
+ const editor = await ClassicEditor.create(domElement, {
+ licenseKey: "GPL",
+ plugins: [Essentials, Paragraph]
+ });
+ editor.setData("<p>x</p>");
+ const paragraph = editor.editing.view.getDomRoot()?.querySelector("p");
+ expect(paragraph).toBeTruthy();
+
+ expect(() => revealTab(paragraph as Element)).not.toThrow();
+
+ await editor.destroy();
+ domElement.remove();
+ });
});
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts
index 182da49f60..aa1432de7e 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_read_only.ts
@@ -141,13 +141,13 @@ export function revealFragment(hash: string): Element | null {
/** Returns the editor whose editing view holds `element`; only the root editable carries it. */
function getEditor(element: Element): Editor | null {
- let editable = element.closest(".ck-editor__editable");
+ let editable: Element | null | undefined = element.closest(".ck-editor__editable");
while (editable) {
const editor = (editable as Element & { ckeditorInstance?: Editor | null }).ckeditorInstance;
if (editor) {
return editor;
}
- editable = editable.parentElement?.closest(".ck-editor__editable") ?? null;
+ editable = editable.parentElement?.closest(".ck-editor__editable");
}
return null;
}
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_ui.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_ui.spec.ts
new file mode 100644
index 0000000000..5f7933e400
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_ui.spec.ts
@@ -0,0 +1,36 @@
+import { ButtonView, ClassicEditor, Essentials, Paragraph } from "ckeditor5";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import Tabs from "./tabs.js";
+
+describe("TabsUI", () => {
+ let domElement: HTMLDivElement;
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ domElement = document.createElement("div");
+ document.body.appendChild(domElement);
+ editor = await ClassicEditor.create(domElement, {
+ licenseKey: "GPL",
+ plugins: [Essentials, Paragraph, Tabs]
+ });
+ });
+
+ afterEach(() => {
+ domElement.remove();
+ return editor.destroy();
+ });
+
+ it("binds each button to its command and runs it on click", () => {
+ editor.setData("<p>x</p>");
+ const names = ["tabs", "insertTab", "removeTab", "moveTabLeft", "moveTabRight"] as const;
+ const buttons = names.map(name => editor.ui.componentFactory.create(name) as ButtonView);
+ expect(buttons.map(button => button.isEnabled)).toEqual([true, false, false, false, false]);
+
+ buttons[0].fire("execute");
+
+ expect(editor.getData()).toContain("trilium-tabs");
+ expect(buttons.map(button => button.isEnabled)).toEqual([false, true, true, false, true]);
+ expect(buttons.every(button => button.label && button.icon && button.tooltip)).toBe(true);
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_ui.ts b/packages/ckeditor5/src/plugins/tabs/tabs_ui.ts
index c7b3c2d48b..d4a80d8c29 100644
--- a/packages/ckeditor5/src/plugins/tabs/tabs_ui.ts
+++ b/packages/ckeditor5/src/plugins/tabs/tabs_ui.ts
@@ -1,5 +1,6 @@
import {
ButtonView,
+ type Command,
IconNextArrow,
IconPlus,
IconPreviousArrow,
@@ -52,10 +53,9 @@ export default class TabsUI extends Plugin {
const button = new ButtonView(locale);
button.set({ label, icon, tooltip: true });
- const command = editor.commands.get(name);
- if (command) {
- button.bind("isEnabled").to(command, "isEnabled");
- }
+ // TabsEditing, which TabsUI requires, registers every command before this runs.
+ const command = editor.commands.get(name) as Command;
+ button.bind("isEnabled").to(command, "isEnabled");
this.listenTo(button, "execute", () => {
editor.execute(name);