chore(react/collections/table): reintroduce column creation

This commit is contained in:
Elian Doran
2025-09-07 22:16:21 +03:00
parent 41c4bc69cc
commit 49c4776dbd
3 changed files with 34 additions and 33 deletions

View File

@@ -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<Tabulator>, attributeDetailWidget: AttributeDetailWidget) {
export default function useColTableEditing(api: RefObject<Tabulator>, attributeDetailWidget: AttributeDetailWidget, parentNote: FNote) {
const [ existingAttributeToEdit, setExistingAttributeToEdit ] = useState<Attribute>();
const [ newAttribute, setNewAttribute ] = useState<Attribute>();
const [ newAttributePosition, setNewAttributePosition ] = useState<number>();
useEffect(() => {
@@ -57,6 +61,33 @@ export default function useColTableEditing(api: RefObject<Tabulator>, 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();
}
}
});

View File

@@ -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<Options>(() => {
if (!hasChildren) return {};
return {

View File

@@ -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;