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
fix(text): keep a nested tabs block's toolbar off its outer tab
Elian Doran1e756a19f02c
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.tsindex 4061f59d6d..cfcc46ef54 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_commands.spec.ts@@ -60,6 +60,29 @@ describe("tabs commands", () => {expect(getModelData(editor.model)).toContain("<tabTitle>[Tab 3]</tabTitle>");});+ it("disables the commands for a single tab while a nested block is selected as a whole", () => {+ 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>${tab("C")}</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;+ expect(inner.is("element", "tabs")).toBe(true);++ editor.model.change(writer => writer.setSelection(inner, "on"));++ for (const name of [ "removeTab", "moveTabLeft", "moveTabRight", "assignTabReference" ]) {+ const command = editor.commands.get(name);+ expect(command, name).toBeDefined();+ expect(command?.isEnabled, name).toBe(false);+ }+ editor.execute("removeTab");+ expect(titles(outer)).toEqual(["A", "B", "C"]);+ expect(titles(inner)).toEqual(["B1", "B2"]);+ });+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;diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts b/packages/ckeditor5/src/plugins/tabs/tabs_commands.tsindex ac569c645d..47653459a2 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts@@ -46,9 +46,7 @@ export class InsertTabCommand extends Command {if (!tabs) {return;}- // 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;+ const currentTab = getSelectedTab(editor);// 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;@@ -175,9 +173,16 @@ export function createTab(writer: ModelWriter, titleText: string): ModelElementreturn tab;}-/** Returns the innermost tab that contains the selection. */+/**+ * Returns the innermost tab that contains the selection, or `null` while a tabs block is selected+ * as a whole, since the tab around it belongs to an outer block.+ */export function getSelectedTab(editor: Editor): ModelElement | null {- const position = editor.model.document.selection.getFirstPosition();+ const selection = editor.model.document.selection;+ if (selection.getSelectedElement()?.is("element", ELEMENTS.tabs)) {+ return null;+ }+ const position = selection.getFirstPosition();return (position?.findAncestor(ELEMENTS.tab) as ModelElement | null) ?? null;}