From 84479a2c2a09488b3a94733b0f8b72ab8c3f1331 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 14 Jul 2025 15:38:57 +0300 Subject: [PATCH] feat(views/table): focus if creating child note --- .../view_widgets/table_view/context_menu.ts | 11 ++-- .../widgets/view_widgets/table_view/index.ts | 56 ++++++++++++++----- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/apps/client/src/widgets/view_widgets/table_view/context_menu.ts b/apps/client/src/widgets/view_widgets/table_view/context_menu.ts index 4eff60a04..70054d2ea 100644 --- a/apps/client/src/widgets/view_widgets/table_view/context_menu.ts +++ b/apps/client/src/widgets/view_widgets/table_view/context_menu.ts @@ -123,16 +123,19 @@ export function showRowContextMenu(_e: UIEvent, row: RowComponent, parentNote: F title: t("table_view.row-insert-child"), uiIcon: "bx bx-empty", handler: async () => { - const branchId = row.getData().branchId; - const note = await froca.getBranch(branchId)?.getNote(); - const bestNotePath = note?.getBestNotePath(parentNote.noteId); const target = e.target; if (!target) { return; } + const branchId = row.getData().branchId; + const note = await froca.getBranch(branchId)?.getNote(); const component = $(target).closest(".component").prop("component"); component.triggerCommand("addNewRow", { - parentNotePath: bestNotePath?.join("/") + parentNotePath: note?.noteId, + customOpts: { + target: "after", + targetBranchId: branchId, + } }); } }, diff --git a/apps/client/src/widgets/view_widgets/table_view/index.ts b/apps/client/src/widgets/view_widgets/table_view/index.ts index 9dc361722..03f1a25fd 100644 --- a/apps/client/src/widgets/view_widgets/table_view/index.ts +++ b/apps/client/src/widgets/view_widgets/table_view/index.ts @@ -6,7 +6,7 @@ import SpacedUpdate from "../../../services/spaced_update.js"; import type { CommandListenerData, EventData } from "../../../components/app_context.js"; import type { Attribute } from "../../../services/attribute_parser.js"; import note_create, { CreateNoteOpts } from "../../../services/note_create.js"; -import {Tabulator, SortModule, FormatModule, InteractionModule, EditModule, ResizeColumnsModule, FrozenColumnsModule, PersistenceModule, MoveColumnsModule, MoveRowsModule, ColumnDefinition, DataTreeModule, Options} from 'tabulator-tables'; +import {Tabulator, SortModule, FormatModule, InteractionModule, EditModule, ResizeColumnsModule, FrozenColumnsModule, PersistenceModule, MoveColumnsModule, MoveRowsModule, ColumnDefinition, DataTreeModule, Options, RowComponent} from 'tabulator-tables'; import "tabulator-tables/dist/css/tabulator.css"; import "../../../../src/stylesheets/table.css"; import { canReorderRows, configureReorderingRows } from "./dragging.js"; @@ -107,7 +107,7 @@ export default class TableView extends ViewMode { private newAttribute?: Attribute; private persistentData: StateInfo["tableData"]; /** If set to a note ID, whenever the rows will be updated, the title of the note will be automatically focused for editing. */ - private noteIdToEdit?: string; + private branchIdToEdit?: string; constructor(args: ViewModeArgs) { super(args, "table"); @@ -145,7 +145,7 @@ export default class TableView extends ViewMode { const columnDefs = buildColumnDefinitions(info, movableRows); let opts: Options = { layout: "fitDataFill", - index: "noteId", + index: "branchId", columns: columnDefs, data: rowData, persistence: true, @@ -237,11 +237,12 @@ export default class TableView extends ViewMode { ...customOpts } console.log("Create with ", opts); - note_create.createNote(parentNotePath, opts).then(({ note }) => { - if (!note) { - return; + note_create.createNote(parentNotePath, opts).then(({ branch }) => { + if (branch) { + setTimeout(() => { + this.focusOnBranch(branch?.branchId); + }); } - this.noteIdToEdit = note.noteId; }) } } @@ -285,16 +286,43 @@ export default class TableView extends ViewMode { const info = getAttributeDefinitionInformation(this.parentNote); const { definitions } = await buildRowDefinitions(this.parentNote, info); - this.api.replaceData(definitions); + await this.api.replaceData(definitions); + } - if (this.noteIdToEdit) { - const row = this.api?.getRows().find(r => r.getData().noteId === this.noteIdToEdit); - if (row) { - row.getCell("title").edit(); - } - this.noteIdToEdit = undefined; + focusOnBranch(branchId: string) { + if (!this.api) { + return; } + + const row = findRowDataById(this.api.getRows(), branchId); + if (!row) { + return; + } + + // Expand the parent tree if any. + if (this.api.options.dataTree) { + const parent = row.getTreeParent(); + if (parent) { + parent.treeExpand(); + } + } + + row.getCell("title").edit(); } } + +function findRowDataById(rows: RowComponent[], branchId: string): RowComponent | null { + for (let row of rows) { + const item = row.getIndex() as string; + + if (item === branchId) { + return row; + } + + let found = findRowDataById(row.getTreeChildren(), branchId); + if (found) return found; + } + return null; +}