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
client/text editor/tabs: wrap the selected content into a tab instead of replacing it when inserting a tabs block
Adorian Doran5092df31bf60
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.htmlindex 3bd0b4f0b7..1ba75c9d44 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Content tabs.html@@ -16,8 +16,9 @@<li>Alternatively, type <code>/tabs</code> and press <kbd>Enter</kbd>, as describedin <a class="reference-link" href="#root/_help_ZlN4nump6EbW">Slash Commands</a>.</li></ul>-<p>A new block contains two tabs, named “Tab 1” and “Tab 2”. The title of- the first tab is selected, so typing replaces it.</p>+<p>A new block contains two tabs, named “Tab 1” and “Tab 2”. Any content+ that was selected prior to inserting the tabs will be moved into the first+ tab. The title of the first tab is selected, so typing replaces it.</p><h2>Editing tabs</h2><ul><li>To show a tab, click its title or move the cursor into it.</li>diff --git a/docs/User Guide/User Guide/Note Types/Text/Content tabs.md b/docs/User Guide/User Guide/Note Types/Text/Content tabs.mdindex e36a45d77e..f2ce025bce 100644--- a/docs/User Guide/User Guide/Note Types/Text/Content tabs.md+++ b/docs/User Guide/User Guide/Note Types/Text/Content tabs.md@@ -11,7 +11,7 @@ A tabs block consists of:* In the formatting bar, go to _Insert_ → _Tabs_.* Alternatively, type `/tabs` and press <kbd>Enter</kbd>, as described in <a class="reference-link" href="Slash%20Commands.md">Slash Commands</a>.-A new block contains two tabs, named “Tab 1” and “Tab 2”. The title of the first tab is selected, so typing replaces it.+A new block contains two tabs, named “Tab 1” and “Tab 2”. Any content that was selected prior to inserting the tabs will be moved into the first tab. The title of the first tab is selected, so typing replaces it.## Editing tabsdiff --git a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs.spec.tsindex 321adac2af..bb9c1a3609 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts@@ -100,6 +100,36 @@ describe("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\">" +diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts b/packages/ckeditor5/src/plugins/tabs/tabs_commands.tsindex 2f4da5bbb3..e045bc510f 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts@@ -7,7 +7,7 @@ import {/*** Inserts a tabs block with two numbered tabs at the selection and selects the first tab's title,- * so typing replaces it.+ * so typing replaces it. Selected content moves into the first tab's panel.*/export class InsertTabsCommand extends Command {@@ -19,13 +19,18 @@ export class InsertTabsCommand extends Command {public override execute(): void {const model = this.editor.model;+ const selection = model.document.selection;model.change(writer => {+ const content = selection.isCollapsed ? null : model.getSelectedContent(selection);const tabs = writer.createElement(ELEMENTS.tabs);const firstTab = createTab(writer, getDefaultTitle(this.editor, 0));writer.append(firstTab, tabs);writer.append(createTab(writer, getDefaultTitle(this.editor, 1)), tabs);model.insertObject(tabs, null, null, { setSelection: "on" });+ if (content) {+ model.insertContent(content, writer.createRangeIn(getPanel(firstTab)));+ }selectTitle(writer, firstTab);});}@@ -242,6 +247,11 @@ function getTitle(tab: ModelElement): ModelElement {return tab.getChild(0) as ModelElement;}+/** Returns the panel of `tab`, which the post-fixer keeps as its second child. */+function getPanel(tab: ModelElement): ModelElement {+ return tab.getChild(1) as ModelElement;+}+/** Returns "Tab N" for the tab at the zero-based `index`. */function getDefaultTitle(editor: Editor, index: number): string {const t = editor.t;