Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

2.1 KB
Raw Blame
import {Menu} from "../../../plugin/Menu";
import {transaction} from "../../wysiwyg/transaction";
import {Constants} from "../../../constants";
import {getAllEditor} from "../../../layout/getAll";
import {formatDateDisplay, getLabelByDateFormat} from "./dateFormat";

const refreshDatabaseAttributePanels = (protyle: IProtyle, avID: string) => {
    protyle.databaseAttributePanel?.refresh();
    getAllEditor().forEach((editor) => {
        if (editor.protyle !== protyle && editor.protyle.databaseAttributePanel?.hasDatabase(avID)) {
            editor.protyle.databaseAttributePanel.refresh();
        }
    });
};

export const setDateFieldFormat = (protyle: IProtyle, avID: string, colId: string,
                                   type: "date" | "created" | "updated", format: TAVDateFormat, oldFormat: TAVDateFormat) => {
    transaction(protyle, [{action: "setAttrViewColDateFormat", id: colId, avID, format, type}],
        [{action: "setAttrViewColDateFormat", id: colId, avID, format: oldFormat, type}], {
            callback: () => refreshDatabaseAttributePanels(protyle, avID),
        });
};

export const formatDate = (options: {
    avPanelElement: Element,
    element: HTMLElement,
    protyle: IProtyle,
    colId: string,
    avID: string,
    type: "date" | "created" | "updated",
    oldFormat: TAVDateFormat
}) => {
    const menu = new Menu(Constants.MENU_AV_COL_FORMAT_DATE);
    (["", "full", "month-day-year", "day-month-year", "year-month-day"] as TAVDateFormat[]).forEach((format) => {
        menu.addItem({
            checked: format === options.oldFormat,
            iconHTML: "",
            label: getLabelByDateFormat(format),
            accelerator: format === "full" ? formatDateDisplay(Date.now(), format) : undefined,
            click() {
                setDateFieldFormat(options.protyle, options.avID, options.colId, options.type, format, options.oldFormat);
                options.avPanelElement.remove();
            }
        });
    });
    const rect = options.element.getBoundingClientRect();
    menu.open({x: rect.left, y: rect.bottom, h: rect.height, w: rect.width, isLeft: true});
};