Skip to content

Build your personal knowledge base with Trilium Notes

feat(views/table): delete column definition as well

Elian Dorane7f47a0663e2
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 24545fdaa6..73187131c5 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 4fdc8b6ab0..b9c95e35cb 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);
+ }
+
+ attributes.setLabel(this.parentNote.noteId, name, value);
+ if (this.existingAttributeToEdit) {
+ attributes.removeOwnedLabelByName(this.parentNote, this.existingAttributeToEdit.name);
+ }
+ } finally {
+ this.api.restoreRedraw();
+ }
+ }
- 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);
+ 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 c6f619cbcb..ee57e31805 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 c2f5271087..e664e8cd8d 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<StateInfo> {
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) {