diff --git a/apps/client/src/widgets/collections/table/col_editing.ts b/apps/client/src/widgets/collections/table/col_editing.ts index 6aa415a5a..54c513d61 100644 --- a/apps/client/src/widgets/collections/table/col_editing.ts +++ b/apps/client/src/widgets/collections/table/col_editing.ts @@ -3,12 +3,16 @@ import { Attribute } from "../../../services/attribute_parser"; import { RefObject } from "preact"; import { Tabulator } from "tabulator-tables"; import { useEffect, useState } from "preact/hooks"; -import { EventData } from "../../../components/app_context"; +import { CommandListenerData, EventData } from "../../../components/app_context"; import AttributeDetailWidget from "../../attribute_widgets/attribute_detail"; +import attributes from "../../../services/attributes"; +import { renameColumn } from "../../view_widgets/table_view/bulk_actions"; +import FNote from "../../../entities/fnote"; -export default function useColTableEditing(api: RefObject, attributeDetailWidget: AttributeDetailWidget) { +export default function useColTableEditing(api: RefObject, attributeDetailWidget: AttributeDetailWidget, parentNote: FNote) { const [ existingAttributeToEdit, setExistingAttributeToEdit ] = useState(); + const [ newAttribute, setNewAttribute ] = useState(); const [ newAttributePosition, setNewAttributePosition ] = useState(); useEffect(() => { @@ -57,6 +61,33 @@ export default function useColTableEditing(api: RefObject, attributeD focus: "name", hideMultiplicity: true }); + }, + async updateAttributeListCommand({ attributes }: CommandListenerData<"updateAttributeList">) { + setNewAttribute(attributes[0]); + }, + async saveAttributesCommand() { + if (!newAttribute || !api.current) { + return; + } + + const { name, value, isInheritable } = newAttribute; + + api.current.blockRedraw(); + const isRename = (this.existingAttributeToEdit && this.existingAttributeToEdit.name !== name); + try { + if (isRename) { + const oldName = this.existingAttributeToEdit!.name.split(":")[1]; + const [ type, newName ] = name.split(":"); + await renameColumn(parentNote.noteId, type as "label" | "relation", oldName, newName); + } + + if (existingAttributeToEdit && (isRename || existingAttributeToEdit.isInheritable !== isInheritable)) { + attributes.removeOwnedLabelByName(parentNote, this.existingAttributeToEdit.name); + } + attributes.setLabel(parentNote.noteId, name, value, isInheritable); + } finally { + api.current.restoreRedraw(); + } } }); diff --git a/apps/client/src/widgets/collections/table/index.tsx b/apps/client/src/widgets/collections/table/index.tsx index 82e99ead9..d372ce12c 100644 --- a/apps/client/src/widgets/collections/table/index.tsx +++ b/apps/client/src/widgets/collections/table/index.tsx @@ -51,7 +51,7 @@ export default function TableView({ note, noteIds, notePath, viewConfig, saveCon const contextMenuEvents = useContextMenu(note, parentComponent, tabulatorRef); const persistenceProps = usePersistence(viewConfig, saveConfig); const rowEditingEvents = useRowTableEditing(tabulatorRef, attributeDetailWidget, notePath); - const colEditingEvents = useColTableEditing(tabulatorRef, attributeDetailWidget); + const colEditingEvents = useColTableEditing(tabulatorRef, attributeDetailWidget, note); const dataTreeProps = useMemo(() => { if (!hasChildren) return {}; return { 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 306a9d6fb..4114fdf5c 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 @@ -10,7 +10,6 @@ import { t } from "../../../services/i18n"; export default class TableColumnEditing extends Component { - private attributeDetailWidget: AttributeDetailWidget; private api: Tabulator; private parentNote: FNote; @@ -23,35 +22,6 @@ export default class TableColumnEditing extends Component { this.parentNote = parentNote; } - async updateAttributeListCommand({ attributes }: CommandListenerData<"updateAttributeList">) { - this.newAttribute = attributes[0]; - } - - async saveAttributesCommand() { - if (!this.newAttribute) { - return; - } - - const { name, value, isInheritable } = this.newAttribute; - - this.api.blockRedraw(); - const isRename = (this.existingAttributeToEdit && this.existingAttributeToEdit.name !== name); - try { - if (isRename) { - const oldName = this.existingAttributeToEdit!.name.split(":")[1]; - const [ type, newName ] = name.split(":"); - await renameColumn(this.parentNote.noteId, type as "label" | "relation", oldName, newName); - } - - if (this.existingAttributeToEdit && (isRename || this.existingAttributeToEdit.isInheritable !== isInheritable)) { - attributes.removeOwnedLabelByName(this.parentNote, this.existingAttributeToEdit.name); - } - attributes.setLabel(this.parentNote.noteId, name, value, isInheritable); - } finally { - this.api.restoreRedraw(); - } - } - async deleteTableColumnCommand({ columnToDelete }: CommandListenerData<"deleteTableColumn">) { if (!columnToDelete || !await dialog.confirm(t("table_view.delete_column_confirmation"))) { return;