Skip to content

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

4.4 KB
Raw Blame
import {Tab} from "../layout/Tab";
import {Custom} from "../layout/dock/Custom";
import {bindCardEvent, genCardHTML} from "./openCard";
import {fetchDueCards} from "./fetchDueCards";
import {Protyle} from "../protyle";
import {setPanelFocus} from "../layout/util";
import type {App} from "../index";
import {clearOBG} from "../layout/dock/util";

export const newCardModel = (options: {
    app: App,
    tab: Tab,
    data: {
        cardType: TCardType,
        id: string,
        title?: string
        cardsData?: ICardData,
        index?: number,
    }
}) => {
    let editor: Protyle;
    const customObj = new Custom({
        app: options.app,
        type: "siyuan-card",
        tab: options.tab,
        data: options.data,
        async init() {
            if (options.data.cardsData) {
                let cardsData = options.data.cardsData;
                for (let i = 0; i < options.app.plugins.length; i++) {
                    cardsData = await options.app.plugins[i].updateCards(options.data.cardsData);
                }
                this.element.innerHTML = genCardHTML({
                    id: this.data.id,
                    cardType: this.data.cardType,
                    cardsData,
                    isTab: true,
                });

                editor = await bindCardEvent({
                    app: options.app,
                    element: this.element,
                    id: this.data.id,
                    title: this.data.title,
                    cardType: this.data.cardType,
                    cardsData,
                    index: options.data.index,
                });
                customObj.editors.push(editor);
                editor.resize();
                // https://github.com/siyuan-note/siyuan/issues/9561#issuecomment-1794473512
                delete options.data.cardsData;
                delete options.data.index;
            } else {
                fetchDueCards(this.data.cardType, this.data.id, undefined, async (response) => {
                    let cardsData = response.data;
                    for (let i = 0; i < options.app.plugins.length; i++) {
                        cardsData = await options.app.plugins[i].updateCards(cardsData);
                    }
                    this.element.innerHTML = genCardHTML({
                        id: this.data.id,
                        cardType: this.data.cardType,
                        cardsData,
                        isTab: true,
                    });

                    editor = await bindCardEvent({
                        app: options.app,
                        element: this.element,
                        id: this.data.id,
                        title: this.data.title,
                        cardType: this.data.cardType,
                        cardsData,
                    });
                    editor.resize();
                    customObj.editors.push(editor);
                });
            }
        },
        destroy() {
            if (editor) {
                editor.destroy();
            }
        },
        resize() {
            if (editor) {
                editor.resize();
            }
        },
        update() {
            fetchDueCards(this.data.cardType, this.data.id, undefined, async (response) => {
                let cardsData = response.data;
                for (let i = 0; i < options.app.plugins.length; i++) {
                    cardsData = await options.app.plugins[i].updateCards(cardsData);
                }
                customObj.editors.forEach(item => {
                    item.destroy();
                });
                this.element.innerHTML = genCardHTML({
                    id: this.data.id,
                    cardType: this.data.cardType,
                    cardsData,
                    isTab: true,
                });
                editor = await bindCardEvent({
                    app: options.app,
                    element: this.element,
                    id: this.data.id,
                    title: this.data.title,
                    cardType: this.data.cardType,
                    cardsData,
                });
                customObj.editors.push(editor);
                editor.resize();
            });
        }
    });
    customObj.element.addEventListener("click", () => {
        clearOBG();
        setPanelFocus(customObj.element.parentElement.parentElement);
    });
    return customObj;
};