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
feat(text): name new tabs "Tab N" and select the title
Elian Doran7d30c8e34c2b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex fb8abda205..c73e39ee18 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4378,6 +4378,7 @@"insert-a-canvas-to-draw-on": "Insert a canvas to draw on.","tabs": "Tabs","tab-title": "Tab title",+ "tab-0": "Tab %0","tab-content": "Tab content","add-tab": "Add tab","remove-tab": "Remove tab",diff --git a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts b/packages/ckeditor5/src/plugins/tabs/tabs.spec.tsindex 4e37e20f40..d86ae73188 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs.spec.ts@@ -64,15 +64,15 @@ describe("Tabs", () => {expect(editor.getData()).toBe(TWO_TABS);});- it("inserts two empty tabs and places the caret in the first title", () => {+ 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>[]</tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" +- "<tab><tabTitle></tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" ++ "<tab><tabTitle>[Tab 1]</tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" ++ "<tab><tabTitle>Tab 2</tabTitle><tabPanel><paragraph></paragraph></tabPanel></tab>" +"</tabs>");});@@ -135,7 +135,8 @@ describe("Tabs", () => {editor.model.change(writer => writer.setSelection(windows.getChild(0) as ModelElement, 0));editor.execute("insertTab");- editor.model.change(writer => writer.insertText("macOS", editor.model.document.selection.getFirstPosition()));+ 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");diff --git a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts b/packages/ckeditor5/src/plugins/tabs/tabs_commands.tsindex 6f46e65319..8a404d77b1 100644--- a/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts+++ b/packages/ckeditor5/src/plugins/tabs/tabs_commands.ts@@ -3,8 +3,8 @@ import { Command, type Editor, type ModelElement, type ModelWriter } from "ckediimport { ELEMENTS } from "./constants.js";/**- * Inserts a tabs block with two empty tabs at the selection and places the caret in the first- * tab's title.+ * Inserts a tabs block with two numbered tabs at the selection and selects the first tab's title,+ * so typing replaces it.*/export class InsertTabsCommand extends Command {@@ -19,21 +19,18 @@ export class InsertTabsCommand extends Command {model.change(writer => {const tabs = writer.createElement(ELEMENTS.tabs);- writer.append(createTab(writer), tabs);- writer.append(createTab(writer), 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" });-- const firstTitle = getTitle(tabs.getChild(0) as ModelElement);- if (firstTitle) {- writer.setSelection(firstTitle, 0);- }+ selectTitle(writer, firstTab);});}}/**- * Adds a tab next to the one holding the selection and places the caret in its title. With- * the whole block selected, the tab goes at the end.+ * Adds a tab next to 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 {@@ -49,18 +46,18 @@ export class InsertTabCommand extends Command {}const currentTab = getSelectedTab(editor);+ const index = currentTab+ ? (currentTab.index ?? 0) + (position === "after" ? 1 : 0)+ : tabs.childCount;+editor.model.change(writer => {- const tab = createTab(writer);+ const tab = createTab(writer, getDefaultTitle(editor, index));if (currentTab) {writer.insert(tab, currentTab, position);} else {writer.insert(tab, tabs, "end");}-- const title = getTitle(tab);- if (title) {- writer.setSelection(title, 0);- }+ selectTitle(writer, tab);});}}@@ -140,10 +137,12 @@ export class MoveTabCommand extends Command {}}-/** Creates a tab with an empty title and a panel holding one empty paragraph. */-export function createTab(writer: ModelWriter): ModelElement {+/** Creates a tab with the given title and a panel holding one empty paragraph. */+export function createTab(writer: ModelWriter, titleText: string): ModelElement {const tab = writer.createElement(ELEMENTS.tab);- writer.append(writer.createElement(ELEMENTS.tabTitle), tab);+ const title = writer.createElement(ELEMENTS.tabTitle);+ writer.appendText(titleText, title);+ writer.append(title, tab);const panel = writer.createElement(ELEMENTS.tabPanel);writer.append(writer.createElement("paragraph"), panel);writer.append(panel, tab);@@ -174,3 +173,16 @@ function getTitle(tab: ModelElement): ModelElement | null {const title = tab.getChild(0);return title?.is("element", ELEMENTS.tabTitle) ? title : null;}++/** Returns "Tab N" for the tab at the zero-based `index`. */+function getDefaultTitle(editor: Editor, index: number): string {+ const t = editor.t;+ return t("Tab %0", index + 1);+}++function selectTitle(writer: ModelWriter, tab: ModelElement) {+ const title = getTitle(tab);+ if (title) {+ writer.setSelection(writer.createRangeIn(title));+ }+}