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): merge extra titles and panels of a tab into its first panel
Elian Doran67f81f6192bf
diff --git a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs.spec.tsindex d86ae73188..6c1f7cd0fa 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts@@ -94,6 +94,34 @@ describe("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"]);diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts b/packages/ckeditor5/src/plugins/tabs/tabs_editing.tsindex 2f31f7ecfe..9179eacde0 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_editing.ts@@ -354,9 +354,9 @@ function fixTabs(writer: ModelWriter, tabs: ModelElement): boolean {if (!tab.is("element", ELEMENTS.tab)) {continue;}- const children = [...tab.getChildren()];+ const children = [...tab.getChildren()] as ModelElement[];const title = children.find(child => child.is("element", ELEMENTS.tabTitle));- const panel = children.find(child => child.is("element", ELEMENTS.tabPanel)) as ModelElement | undefined;+ let panel: ModelElement | undefined = children.find(child => child.is("element", ELEMENTS.tabPanel));if (!title) {writer.insertElement(ELEMENTS.tabTitle, tab, 0);@@ -367,14 +367,54 @@ function fixTabs(writer: ModelWriter, tabs: ModelElement): boolean {}if (!panel) {- const newPanel = writer.createElement(ELEMENTS.tabPanel);- writer.appendElement("paragraph", newPanel);- writer.append(newPanel, tab);+ panel = writer.createElement(ELEMENTS.tabPanel);+ writer.append(panel, tab);changed = true;- } else if (panel.isEmpty) {+ }++ if (mergeIntoPanel(writer, children, title, panel)) {+ changed = true;+ }++ if (panel.isEmpty) {writer.appendElement("paragraph", panel);changed = true;}}return changed;}++/**+ * Moves the content of every title and panel of a tab other than `title` and `panel` into+ * `panel`, keeping document order: what precedes `panel` goes before its content, what follows it+ * goes after. An extra title becomes a paragraph.+ */+function mergeIntoPanel(writer: ModelWriter, children: ModelElement[], title: ModelElement | undefined, panel: ModelElement) {+ let offset: number | "end" = 0;+ let changed = false;+ for (const child of children) {+ if (child === title) {+ continue;+ }+ if (child === panel) {+ offset = "end";+ continue;+ }++ const target = writer.createPositionAt(panel, offset);+ let count = 1;+ if (child.is("element", ELEMENTS.tabPanel)) {+ count = child.childCount;+ writer.move(writer.createRangeIn(child), target);+ writer.remove(child);+ } else {+ writer.rename(child, "paragraph");+ writer.move(writer.createRangeOn(child), target);+ }+ if (offset !== "end") {+ offset += count;+ }+ changed = true;+ }+ return changed;+}