Skip to content

Build your personal knowledge base with Trilium Notes

5.8 KB
Raw Blame
import { extractAttributeDefinitionTypeAndName } from "@triliumnext/commons";
import { t } from "i18next";
import { RefObject } from "preact";
import { useRef } from "preact/hooks";
import { Tabulator } from "tabulator-tables";

import { CommandListenerData, EventData } from "../../../components/app_context";
import FNote from "../../../entities/fnote";
import { Attribute } from "../../../services/attribute_parser";
import attributes from "../../../services/attributes";
import { executeBulkActions } from "../../../services/bulk_action";
import dialog from "../../../services/dialog";
import AttributeDetailWidget from "../../attribute_widgets/attribute_detail";
import { useLegacyImperativeHandlers } from "../../react/hooks";
import { getAttributeFromField } from "./utils";

export default function useColTableEditing(
    api: RefObject<Tabulator | null>,
    attributeDetailWidget: AttributeDetailWidget,
    parentNote: FNote,
    newAttributePosition: RefObject<number | undefined>
) {

    const existingAttributeToEdit = useRef<Attribute | undefined>(undefined);
    const newAttribute = useRef<Attribute | undefined>(undefined);

    useLegacyImperativeHandlers({
        addNewTableColumnCommand({ referenceColumn, columnToEdit, direction, type }: EventData<"addNewTableColumn">) {
            let attr: Attribute | undefined;

            existingAttributeToEdit.current = undefined;
            if (columnToEdit) {
                attr = getAttributeFromField(parentNote, columnToEdit.getField());
                if (attr) {
                    existingAttributeToEdit.current = { ...attr };
                }
            }

            if (!attr) {
                attr = {
                    type: "label",
                    name: `${type ?? "label"}:myLabel`,
                    value: "promoted,single,text",
                    isInheritable: true
                };
            }

            if (referenceColumn && api.current) {
                let newPosition = api.current.getColumns().indexOf(referenceColumn);
                if (direction === "after") {
                    newPosition++;
                }

                newAttributePosition.current = newPosition;
            } else {
                newAttributePosition.current = undefined;
            }

            attributeDetailWidget.showAttributeDetail({
                attribute: attr,
                allAttributes: [ attr ],
                isOwned: true,
                x: 0,
                focus: "name"
            });
        },
        async updateAttributeListCommand({ attributes }: CommandListenerData<"updateAttributeList">) {
            newAttribute.current = attributes[0];
        },
        async saveAttributesCommand() {
            if (!newAttribute.current || !api.current) {
                return;
            }

            const { name, value, isInheritable } = newAttribute.current;

            api.current.blockRedraw();
            const isRename = (existingAttributeToEdit.current && existingAttributeToEdit.current.name !== name);
            try {
                if (isRename) {
                    const [ , oldName ] = extractAttributeDefinitionTypeAndName(existingAttributeToEdit.current!.name);
                    const [ type, newName ] = extractAttributeDefinitionTypeAndName(name);
                    await renameColumn(parentNote.noteId, type, oldName, newName);
                }

                if (existingAttributeToEdit.current && (isRename || existingAttributeToEdit.current.isInheritable !== isInheritable)) {
                    attributes.removeOwnedLabelByName(parentNote, existingAttributeToEdit.current.name);
                }
                attributes.setLabel(parentNote.noteId, name, value, isInheritable);
            } finally {
                api.current.restoreRedraw();
            }
        },
        async deleteTableColumnCommand({ columnToDelete }: CommandListenerData<"deleteTableColumn">) {
            if (!api.current || !columnToDelete || !await dialog.confirm(t("table_view.delete_column_confirmation"))) {
                return;
            }

            let [ type, name ] = columnToDelete.getField()?.split(".", 2);
            if (!type || !name) {
                return;
            }
            type = type.replace("s", "");

            api.current.blockRedraw();
            try {
                await deleteColumn(parentNote.noteId, type as "label" | "relation", name);
                attributes.removeOwnedLabelByName(parentNote, `${type}:${name}`);
            } finally {
                api.current.restoreRedraw();
            }
        }
    });

    function resetNewAttributePosition() {
        newAttribute.current = undefined;
        newAttributePosition.current = undefined;
        existingAttributeToEdit.current = undefined;
    }

    return { resetNewAttributePosition };
}

async function deleteColumn(parentNoteId: string, type: "label" | "relation", columnName: string) {
    if (type === "label") {
        return executeBulkActions([parentNoteId], [{
            name: "deleteLabel",
            labelName: columnName
        }], { includeDescendants: true });
    } else {
        return executeBulkActions([parentNoteId], [{
            name: "deleteRelation",
            relationName: columnName
        }], { includeDescendants: true });
    }
}

async function renameColumn(parentNoteId: string, type: "label" | "relation", originalName: string, newName: string) {
    if (type === "label") {
        return executeBulkActions([parentNoteId], [{
            name: "renameLabel",
            oldLabelName: originalName,
            newLabelName: newName
        }], { includeDescendants: true });
    } else {
        return executeBulkActions([parentNoteId], [{
            name: "renameRelation",
            oldRelationName: originalName,
            newRelationName: newName
        }], { includeDescendants: true });
    }
}