Skip to content

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

3.8 KB
Raw Blame
import {getAllEditor} from "../../layout/getAll";
import {hideRectResizeHandles} from "../../asset/rectAnnotationResize";
import {isIPhone} from "../util/compatibility";
import {hideGutterElements} from "./gutterVisibility";
import {closeSubElement} from "../toolbar/subElementLifecycle";
import {clearBlockSelectionMode} from "../wysiwyg/blockSelection";

// "gutter", "toolbar", "select", "hint", "util", "dialog", "gutterOnly"
export const hideElements = (panels: string[], protyle?: IProtyle, focusHide = false) => {
    if (!protyle) {
        if (panels.includes("dialog")) {
            const dialogLength = window.siyuan.dialogs.length;
            for (let i = 0; i < dialogLength; i++) {
                window.siyuan.dialogs[i].destroy();
            }
        }
        return;
    }
    if (panels.includes("hint")) {
        clearTimeout(protyle.hint.timeId);
        protyle.hint.deactivateEmojiPanel();
        protyle.hint.element.classList.add("fn__none");
    }
    if (protyle.gutter && panels.includes("gutter")) {
        hideGutterElements([protyle.gutter.element], true);
        // https://ld246.com/article/1651935412480
        protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--hl").forEach((item) => {
            item.classList.remove("protyle-wysiwyg--hl");
        });
    }
    //  不能 remove("protyle-wysiwyg--hl") 否则打开页签的时候 "cb-get-hl" 高亮会被移除
    if (panels.includes("gutterOnly")) {
        const gutterElements: HTMLElement[] = [];
        if (protyle.gutter) {
            gutterElements.push(protyle.gutter.element);
        }
        const nestedGutter = protyle.contentElement.querySelector<HTMLElement>(".protyle-gutters:not(.fn__none)");
        if (nestedGutter) {
            gutterElements.push(nestedGutter);
        }
        hideGutterElements(gutterElements, !isIPhone());
    }
    if (protyle.toolbar && panels.includes("toolbar")) {
        protyle.toolbar.element.classList.add("fn__none");
        protyle.toolbar.element.style.display = "";
    }
    if (protyle.toolbar && panels.includes("util")) {
        const pinElement = protyle.toolbar.subElement.querySelector('[data-type="pin"]');
        if (!protyle.toolbar.isMultiSelectMode() &&
            (focusHide || !pinElement || (pinElement && pinElement.getAttribute("aria-label") === window.siyuan.languages.pin))) {
            protyle.toolbar.subElement.classList.add("fn__none");
            closeSubElement(protyle.toolbar);
        }
    }
    if (panels.includes("select")) {
        clearBlockSelectionMode(protyle.wysiwyg.element, true);
    }
};

// "toolbar", "pdfutil", "gutter", "util"
export const hideAllElements = (types: string[]) => {
    if (types.includes("toolbar")) {
        document.querySelectorAll(".protyle-toolbar").forEach((item: HTMLElement) => {
            item.classList.add("fn__none");
            item.style.display = "";
        });
    }
    if (types.includes("util")) {
        getAllEditor().forEach(item => {
            if (item.protyle.toolbar) {
                const pinElement = item.protyle.toolbar.subElement.querySelector('[data-type="pin"]');
                if (!item.protyle.toolbar.isMultiSelectMode() &&
                    (!pinElement || (pinElement && pinElement.getAttribute("aria-label") === window.siyuan.languages.pin))) {
                    item.protyle.toolbar.subElement.classList.add("fn__none");
                    closeSubElement(item.protyle.toolbar);
                }
            }
        });
    }
    if (types.includes("pdfutil")) {
        document.querySelectorAll(".pdf__util").forEach(item => {
            item.classList.add("fn__none");
        });
        hideRectResizeHandles(document);
    }
    if (types.includes("gutter")) {
        hideGutterElements(Array.from(document.querySelectorAll<HTMLElement>(".protyle-gutters")), true);
    }
};