From e7f47a0663e256898153c59d81f8ad97807a0ee7 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 16 Jul 2025 21:40:01 +0300 Subject: [PATCH] feat(views/table): delete column definition as well --- apps/client/src/components/app_context.ts | 3 ++ .../view_widgets/table_view/col_editing.ts | 42 +++++++++++++++---- .../view_widgets/table_view/context_menu.ts | 20 ++------- .../widgets/view_widgets/table_view/index.ts | 21 +++------- 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts index 24545fdaa..73187131c 100644 --- a/apps/client/src/components/app_context.ts +++ b/apps/client/src/components/app_context.ts @@ -288,6 +288,9 @@ export type CommandMappings = { referenceColumn?: ColumnComponent; direction?: "before" | "after"; }; + deleteTableColumn: CommandData & { + columnToDelete?: ColumnComponent; + }; buildTouchBar: CommandData & { TouchBar: typeof TouchBar; diff --git a/apps/client/src/widgets/view_widgets/table_view/col_editing.ts b/apps/client/src/widgets/view_widgets/table_view/col_editing.ts index 4fdc8b6ab..b9c95e35c 100644 --- a/apps/client/src/widgets/view_widgets/table_view/col_editing.ts +++ b/apps/client/src/widgets/view_widgets/table_view/col_editing.ts @@ -5,7 +5,9 @@ import Component from "../../../components/component"; import { CommandListenerData, EventData } from "../../../components/app_context"; import attributes from "../../../services/attributes"; import FNote from "../../../entities/fnote"; -import { renameColumn } from "./bulk_actions"; +import { deleteColumn, renameColumn } from "./bulk_actions"; +import dialog from "../../../services/dialog"; +import { t } from "../../../services/i18n"; export default class TableColumnEditing extends Component { @@ -79,18 +81,40 @@ export default class TableColumnEditing extends Component { const { name, type, value } = this.newAttribute; this.api.blockRedraw(); + try { + if (this.existingAttributeToEdit && this.existingAttributeToEdit.name !== name) { + const oldName = this.existingAttributeToEdit.name.split(":")[1]; + const newName = name.split(":")[1]; + await renameColumn(this.parentNote.noteId, type, oldName, newName); + } - if (this.existingAttributeToEdit && this.existingAttributeToEdit.name !== name) { - const oldName = this.existingAttributeToEdit.name.split(":")[1]; - const newName = name.split(":")[1]; - await renameColumn(this.parentNote.noteId, type, oldName, newName); + attributes.setLabel(this.parentNote.noteId, name, value); + if (this.existingAttributeToEdit) { + attributes.removeOwnedLabelByName(this.parentNote, this.existingAttributeToEdit.name); + } + } finally { + this.api.restoreRedraw(); + } + } + + async deleteTableColumnCommand({ columnToDelete }: CommandListenerData<"deleteTableColumn">) { + if (!columnToDelete || !await dialog.confirm(t("table_view.delete_column_confirmation"))) { + return; } - attributes.setLabel(this.parentNote.noteId, name, value); - if (this.existingAttributeToEdit) { - attributes.removeOwnedLabelByName(this.parentNote, this.existingAttributeToEdit.name); + let [ type, name ] = columnToDelete.getField()?.split(".", 2); + if (!type || !name) { + return; + } + type = type.replace("s", ""); + + this.api.blockRedraw(); + try { + await deleteColumn(this.parentNote.noteId, type as "label" | "relation", name); + attributes.removeOwnedLabelByName(this.parentNote, `${type}:${name}`); + } finally { + this.api.restoreRedraw(); } - this.api.restoreRedraw(); } getNewAttributePosition() { 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 c6f619cbc..ee57e3180 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 @@ -1,4 +1,4 @@ -import { ColumnComponent, MenuSeparator, RowComponent, Tabulator } from "tabulator-tables"; +import { ColumnComponent, RowComponent, Tabulator } from "tabulator-tables"; import contextMenu, { MenuItem } from "../../../menus/context_menu.js"; import { TableData } from "./rows.js"; import branches from "../../../services/branches.js"; @@ -7,8 +7,6 @@ import link_context_menu from "../../../menus/link_context_menu.js"; import type FNote from "../../../entities/fnote.js"; import froca from "../../../services/froca.js"; import type Component from "../../../components/component.js"; -import dialog from "../../../services/dialog.js"; -import { deleteColumn } from "./bulk_actions.js"; export function setupContextMenu(tabulator: Tabulator, parentNote: FNote) { tabulator.on("rowContext", (e, row) => showRowContextMenu(e, row, parentNote, tabulator)); @@ -111,19 +109,9 @@ function showColumnContextMenu(_e: UIEvent, column: ColumnComponent, parentNote: title: t("table_view.delete-column"), uiIcon: "bx bx-trash", enabled: !!column.getField() && column.getField() !== "title", - handler: async () => { - if (!await dialog.confirm(t("table_view.delete_column_confirmation"))) { - return; - } - - let [ type, name ] = column.getField()?.split(".", 2); - if (!type || !name) { - return; - } - - type = type.replace("s", ""); - deleteColumn(parentNote.noteId, type as "label" | "relation", name); - } + handler: () => getParentComponent(e)?.triggerCommand("deleteTableColumn", { + columnToDelete: column + }) } ], selectMenuItemHandler() {}, 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 c2f527108..e664e8cd8 100644 --- a/apps/client/src/widgets/view_widgets/table_view/index.ts +++ b/apps/client/src/widgets/view_widgets/table_view/index.ts @@ -221,22 +221,11 @@ export default class TableView extends ViewMode { this.colEditing?.resetNewAttributePosition(); } - addNewRowCommand(e) { - this.rowEditing?.addNewRowCommand(e); - } - - addNewTableColumnCommand(e) { - this.colEditing?.addNewTableColumnCommand(e); - } - - updateAttributeListCommand(e) { - this.colEditing?.updateAttributeListCommand(e); - } - - saveAttributesCommand() { - this.colEditing?.saveAttributesCommand(); - } - + addNewRowCommand(e) { this.rowEditing?.addNewRowCommand(e); } + addNewTableColumnCommand(e) { this.colEditing?.addNewTableColumnCommand(e); } + deleteTableColumnCommand(e) { this.colEditing?.deleteTableColumnCommand(e); } + updateAttributeListCommand(e) { this.colEditing?.updateAttributeListCommand(e); } + saveAttributesCommand() { this.colEditing?.saveAttributesCommand(); } async #manageRowsUpdate() { if (!this.api) {