Skip to content

Build your personal knowledge base with Trilium Notes

14.3 KB
Raw Blame
import { HiddenSubtreeAttribute, HiddenSubtreeItem } from "@triliumnext/commons";
import { t } from "i18next";

export default function buildHiddenSubtreeTemplates() {
    const hideSubtreeAttributes: HiddenSubtreeAttribute = {
        name: "subtreeHidden",
        type: "label",
        value: "false"
    };

    const templates: HiddenSubtreeItem = {
        id: "_templates",
        title: t("hidden_subtree_templates.built-in-templates"),
        type: "book",
        children: [
            {
                id: "_template_text_snippet",
                type: "text",
                title: t("hidden_subtree_templates.text-snippet"),
                icon: "bx-align-left",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "textSnippet",
                        type: "label"
                    },
                    {
                        name: "label:textSnippetDescription",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.description")},single,text`
                    }
                ]
            },
            {
                id: "_template_markdown_snippet",
                type: "code",
                mime: "text/x-markdown",
                title: t("hidden_subtree_templates.markdown-snippet"),
                icon: "bx-align-left",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "snippet",
                        type: "label"
                    },
                    {
                        name: "label:snippetDescription",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.description")},single,text`
                    }
                ]
            },
            {
                id: "_template_code_snippet",
                type: "code",
                mime: "text/plain",
                title: t("hidden_subtree_templates.code-snippet"),
                icon: "bx-align-left",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "snippet",
                        type: "label"
                    },
                    {
                        name: "label:snippetDescription",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.description")},single,text`
                    }
                ]
            },
            {
                // A text note rather than a code one: the content is an instruction written in
                // prose, and the editor is where the user already is when they think of it.
                id: "_template_ai_quick_action",
                type: "text",
                title: t("hidden_subtree_templates.ai-quick-action"),
                icon: "bx-bot",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "aiQuickAction",
                        type: "label"
                    }
                ]
            },
            {
                id: "_template_list_view",
                type: "book",
                title: t("hidden_subtree_templates.list-view"),
                icon: "bx bx-list-ul",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "list"
                    }
                ]
            },
            {
                id: "_template_grid_view",
                type: "book",
                title: t("hidden_subtree_templates.grid-view"),
                icon: "bx bxs-grid",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "grid"
                    }
                ]
            },
            {
                id: "_template_calendar",
                type: "book",
                title: t("hidden_subtree_templates.calendar"),
                icon: "bx bx-calendar",
                attributes: [
                    {
                        name: "template",
                        type: "label",
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "calendar"
                    },
                    {
                        name: "hidePromotedAttributes",
                        type: "label"
                    },
                    hideSubtreeAttributes,
                    {
                        name: "label:startDate",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.start-date")},single,date`,
                        isInheritable: true
                    },
                    {
                        name: "label:endDate",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.end-date")},single,date`,
                        isInheritable: true
                    },
                    {
                        name: "label:startTime",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.start-time")},single,time`,
                        isInheritable: true
                    },
                    {
                        name: "label:endTime",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.end-time")},single,time`,
                        isInheritable: true
                    }
                ]
            },
            {
                id: "_template_table",
                type: "book",
                title: t("hidden_subtree_templates.table"),
                icon: "bx bx-table",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    hideSubtreeAttributes,
                    {
                        name: "viewType",
                        type: "label",
                        value: "table"
                    }
                ]
            },
            {
                id: "_template_geo_map",
                type: "book",
                title: t("hidden_subtree_templates.geo-map"),
                icon: "bx bx-map-alt",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "geoMap"
                    },
                    {
                        name: "hidePromotedAttributes",
                        type: "label"
                    },
                    hideSubtreeAttributes,
                    {
                        name: "label:geolocation",
                        type: "label",
                        value: `promoted,alias=${t("hidden_subtree_templates.geolocation")},single,text`,
                        isInheritable: true
                    }
                ]
            },
            {
                id: "_template_board",
                type: "book",
                title: t("hidden_subtree_templates.board"),
                icon: "bx bx-columns",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "board"
                    },
                    {
                        name: "hidePromotedAttributes",
                        type: "label"
                    },
                    {
                        // A board shows its children as cards, so the tree would repeat them.
                        name: "subtreeHidden",
                        type: "label"
                    }
                    // Deliberately no `label:status`: the columns a board shows are the options of a
                    // select definition, and a definition here would be shared by every board in the
                    // document — one board's columns would be everyone's. Each board owns its own
                    // instead, written by the board view once it knows its columns (see
                    // BoardApi#syncColumnsToDefinition) and by migration 0240 for boards that predate
                    // it. `enforceAttributes` deletes the definition this template used to carry.
                ],
                children: [
                    {
                        id: "_template_board_first",
                        title: t("hidden_subtree_templates.board_note_first"),
                        attributes: [{
                            name: "status",
                            value: t("hidden_subtree_templates.board_status_todo"),
                            type: "label"
                        }],
                        type: "text"
                    },
                    {
                        id: "_template_board_second",
                        title: t("hidden_subtree_templates.board_note_second"),
                        attributes: [{
                            name: "status",
                            value: t("hidden_subtree_templates.board_status_progress"),
                            type: "label"
                        }],
                        type: "text"
                    },
                    {
                        id: "_template_board_third",
                        title: t("hidden_subtree_templates.board_note_third"),
                        attributes: [{
                            name: "status",
                            value: t("hidden_subtree_templates.board_status_done"),
                            type: "label"
                        }],
                        type: "text"
                    }
                ]
            },
            {
                // Bare, but kept: slides made by earlier versions link to it with `~template`.
                id: "_template_presentation_slide",
                type: "text",
                title: t("hidden_subtree_templates.presentation_slide")
            },
            {
                id: "_template_presentation",
                type: "book",
                title: t("hidden_subtree_templates.presentation"),
                icon: "bx bx-slideshow",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "presentation"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        // The slide sidebar lists the slides, so the tree would repeat them.
                        name: "subtreeHidden",
                        type: "label"
                    }
                ],
                children: [
                    {
                        id: "_template_presentation_first",
                        type: "text",
                        title: t("hidden_subtree_templates.presentation_slide_first"),
                        content: t("hidden_subtree_templates.presentation_slide_first")
                    },
                    {
                        id: "_template_presentation_second",
                        type: "text",
                        title: t("hidden_subtree_templates.presentation_slide_second"),
                        content: t("hidden_subtree_templates.presentation_slide_second")
                    }
                ]
            },
            {
                id: "_template_dashboard",
                type: "book",
                title: t("hidden_subtree_templates.dashboard"),
                icon: "bx bxs-dashboard",
                attributes: [
                    {
                        name: "template",
                        type: "label"
                    },
                    {
                        name: "collection",
                        type: "label"
                    },
                    {
                        name: "viewType",
                        type: "label",
                        value: "dashboard"
                    },
                    {
                        // Marks the collection as a beta feature in the note creation menu.
                        name: "beta",
                        type: "label"
                    }
                ]
            }
        ]
    };

    // Enforce attributes.
    templates.enforceAttributes = true;
    for (const template of templates.children ?? []) {
        template.enforceAttributes = true;
    }

    return templates;
}