Skip to content

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

5.3 KB
Raw Blame
import {mountHelp, newNotebook} from "../../util/mount";
import {newFile} from "../../util/newFile";
import {popSearch} from "../menu/search";
import {getRecentDocs} from "../menu/getRecentDocs";
import {openHistory} from "../../history/history";
import type {App} from "../../index";
import {setTitle} from "../../util/processTitle";
import {clearMobileBarsScroll} from "./mobileBars";
import {updateMobileTopBarLayout} from "./mobileTopBar";
import {invalidateTrackedRanges} from "../../protyle/util/trackedRange";
import {closeAVCellEditor} from "../../protyle/render/av/cellEditor";
import {closeMobileBottomBacklinkPanel} from "./bottomBacklink";
import {finishMobileStartup} from "./startup";

export const setEmpty = (app: App) => {
    void closeMobileBottomBacklinkPanel()?.catch(error => console.error(error));
    closeAVCellEditor();
    if (window.siyuan.mobile.editor?.protyle) {
        invalidateTrackedRanges(window.siyuan.mobile.editor.protyle);
    }
    if (window.siyuan.mobile.popEditor?.protyle) {
        invalidateTrackedRanges(window.siyuan.mobile.popEditor.protyle);
    }
    setTitle("", true);
    clearMobileBarsScroll();
    document.getElementById("mobileTopBar").classList.remove("fn__none");
    document.getElementById("toolbarName").classList.add("fn__hidden");
    document.getElementById("toolbarNameReadonly").classList.add("fn__hidden");
    document.getElementById("editor").classList.add("fn__none");
    window.siyuan.mobile.docks.outline?.reload();
    updateMobileTopBarLayout();
    const emptyElement = document.getElementById("empty");
    emptyElement.classList.remove("fn__none");
    if (emptyElement.innerHTML !== "") {
        finishMobileStartup();
        return;
    }
    emptyElement.innerHTML = `<div id="emptySearch" class="b3-list-item">
    <svg class="b3-list-item__graphic"><use xlink:href="#iconSearch"></use></svg><span class="fn__space"></span><span class="b3-list-item__text">${window.siyuan.languages.search}</span>
</div>
<div id="emptyRecent" class="b3-list-item">
    <svg class="b3-list-item__graphic"><use xlink:href="#iconRecentDocs"></use></svg><span class="fn__space"></span><span class="b3-list-item__text">${window.siyuan.languages.recentDocs}</span>
</div>
<div id="emptyHistory" class="b3-list-item${window.siyuan.config.readonly ? " fn__none" : ""}">
    <svg class="b3-list-item__graphic"><use xlink:href="#iconHistory"></use></svg><span class="fn__space"></span><span class="b3-list-item__text">${window.siyuan.languages.dataHistory}</span>
</div>
<div id="emptyNewFile" class="b3-list-item${window.siyuan.config.readonly ? " fn__none" : ""}">
    <svg class="b3-list-item__graphic"><use xlink:href="#iconAddDoc"></use></svg><span class="fn__space"></span><span class="b3-list-item__text">${window.siyuan.languages.newFile}</span>
</div>
<div class="b3-list-item${window.siyuan.config.readonly ? " fn__none" : ""}" id="emptyNewNotebook">
    <svg class="b3-list-item__graphic"><use xlink:href="#iconNewNoteBook"></use></svg><span class="fn__space"></span><span class="b3-list-item__text">${window.siyuan.languages.newNotebook}</span>
</div>
<div class="b3-list-item${window.siyuan.config.readonly ? " fn__none" : ""}" id="emptyHelp">
    <svg class="b3-list-item__graphic"><use xlink:href="#iconHelp"></use></svg><span class="fn__space"></span><span class="b3-list-item__text">${window.siyuan.languages.userGuide}</span>
</div>`;
    emptyElement.addEventListener("click", (event) => {
        let target = event.target as HTMLElement;
        while (target && !target.isEqualNode(emptyElement)) {
            if (target.id === "emptySearch") {
                popSearch(app);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.id === "emptyRecent") {
                getRecentDocs(app);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.id === "emptyHistory") {
                openHistory(app);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.id === "emptyNewFile") {
                newFile(app);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.id === "emptyNewNotebook") {
                newNotebook();
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.id === "emptyHelp") {
                mountHelp();
                event.stopPropagation();
                event.preventDefault();
                break;
            }
            target = target.parentElement;
        }
    });
    finishMobileStartup();
};

export const setEditor = (showTitle = true) => {
    document.getElementById("mobileTopBar").classList.remove("fn__none");
    const toolbarNameElement = document.getElementById("toolbarName") as HTMLInputElement;
    setTitle(toolbarNameElement.value);
    toolbarNameElement.classList.toggle("fn__hidden", !showTitle);
    document.getElementById("toolbarNameReadonly").classList.toggle("fn__hidden", !showTitle);
    document.getElementById("editor").classList.remove("fn__none");
    document.getElementById("empty").classList.add("fn__none");
    updateMobileTopBarLayout();
    if (showTitle) {
        finishMobileStartup();
    }
};