Skip to content

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

10.1 KB
Raw Blame
import {toggleMenu} from "../menus/menuToggle";
import {Constants} from "../constants";
import {fetchPost} from "../util/fetch";
import {setStorageVal} from "../protyle/util/compatibility";
import {getArticle} from "./util";
import {getAttr} from "./attrs";
import {escapeHtmlTextAndAttr, escapeLessThans} from "../util/escape";
import {getIconByType} from "../editor/getIcon";
import {unicode2Emoji} from "../emoji";
import {getDisplayName, getNotebookName} from "../util/pathName";
import {Protyle} from "../protyle";
import {resize} from "../protyle/util/resize";
import {Menu} from "../plugin/Menu";

export const openSearchUnRef = (element: HTMLElement, editor: Protyle) => {
    window.siyuan.menus.menu.remove();
    element.previousElementSibling.previousElementSibling.classList.add("fn__none");
    element.classList.remove("fn__none");
    if (element.querySelector("#searchUnRefResult").innerHTML) {
        return;
    }
    const loadingElement =  element.parentElement.querySelector(".fn__loading") as HTMLElement;
    loadingElement.classList.remove("fn__none");
    loadingElement.style.top = "42px";
    if (element.querySelector("#searchUnRefList").innerHTML !== "") {
        return;
    }
    const localSearch = window.siyuan.storage[Constants.LOCAL_SEARCHUNREF] as ISearchAssetOption;
    if (localSearch.layout === 1) {
        if (localSearch.col) {
            editor.protyle.element.style.width = localSearch.col;
            editor.protyle.element.classList.remove("fn__flex-1");
        }
    } else {
        if (localSearch.row) {
            editor.protyle.element.classList.remove("fn__flex-1");
            editor.protyle.element.style.height = localSearch.row;
        }
    }

    const dragElement = element.querySelector(".search__drag");
    dragElement.addEventListener("mousedown", (event: MouseEvent) => {
        const documentSelf = document;
        const nextElement = dragElement.nextElementSibling as HTMLElement;
        const previousElement = dragElement.previousElementSibling as HTMLElement;
        const direction = localSearch.layout === 1 ? "lr" : "tb";
        const x = event[direction === "lr" ? "clientX" : "clientY"];
        const previousSize = direction === "lr" ? previousElement.clientWidth : previousElement.clientHeight;
        const nextSize = direction === "lr" ? nextElement.clientWidth : nextElement.clientHeight;

        nextElement.classList.remove("fn__flex-1");
        nextElement.style[direction === "lr" ? "width" : "height"] = nextSize + "px";
        element.style.userSelect = "none";
        documentSelf.onmousemove = (moveEvent: MouseEvent) => {
            moveEvent.preventDefault();
            moveEvent.stopPropagation();
            const previousNowSize = (previousSize + (moveEvent[direction === "lr" ? "clientX" : "clientY"] - x));
            const nextNowSize = (nextSize - (moveEvent[direction === "lr" ? "clientX" : "clientY"] - x));
            if (previousNowSize < 120 || nextNowSize < 120) {
                return;
            }
            nextElement.style[direction === "lr" ? "width" : "height"] = nextNowSize + "px";
        };

        documentSelf.onmouseup = () => {
            element.style.userSelect = "";
            documentSelf.onmousemove = null;
            documentSelf.onmouseup = null;
            documentSelf.ondragstart = null;
            documentSelf.onselectstart = null;
            documentSelf.onselect = null;
            window.siyuan.storage[Constants.LOCAL_SEARCHUNREF][direction === "lr" ? "col" : "row"] = nextElement[direction === "lr" ? "clientWidth" : "clientHeight"] + "px";
            setStorageVal(Constants.LOCAL_SEARCHUNREF, window.siyuan.storage[Constants.LOCAL_SEARCHUNREF]);
            if (direction === "lr") {
                resize(editor.protyle);
            }
        };
    });
    dragElement.addEventListener("dblclick", () => {
        editor.protyle.element.style[localSearch.layout === 1 ? "width" : "height"] = "";
        editor.protyle.element.classList.add("fn__flex-1");
        const direction = localSearch.layout === 1 ? "lr" : "tb";
        window.siyuan.storage[Constants.LOCAL_SEARCHUNREF][direction === "lr" ? "col" : "row"] = "";
        setStorageVal(Constants.LOCAL_SEARCHUNREF, window.siyuan.storage[Constants.LOCAL_SEARCHUNREF]);
        if (direction === "lr") {
            resize(editor.protyle);
        }
    });
    getUnRefList(element, editor);
};

export const getUnRefList = (element: Element, edit: Protyle, page = 1) => {
    const previousElement = element.querySelector('[data-type="unRefPrevious"]');
    if (page > 1) {
        previousElement.removeAttribute("disabled");
    } else {
        previousElement.setAttribute("disabled", "disabled");
    }
    fetchPost("/api/search/listInvalidBlockRefs", {
        page,
    }, (response) => {
        element.parentElement.querySelector(".fn__loading").classList.add("fn__none");
        const nextElement = element.querySelector('[data-type="unRefNext"]');
        if (page < response.data.pageCount) {
            nextElement.removeAttribute("disabled");
        } else {
            nextElement.setAttribute("disabled", "disabled");
        }
        let resultHTML = "";
        response.data.blocks.forEach((item: IBlock, index: number) => {
            const notebookName = getNotebookName(item.box);
            const hPath = getDisplayName(item.hPath, false);
            const title = notebookName + hPath;
            const tooltip = escapeHtmlTextAndAttr(notebookName) + hPath;
            resultHTML += `<div data-type="search-item" class="b3-list-item${index === 0 ? " b3-list-item--focus" : ""}" data-node-id="${item.id}" data-root-id="${item.rootID}">
<svg class="b3-list-item__graphic"><use xlink:href="#${getIconByType(item.type)}"></use></svg>
${unicode2Emoji(item.ial.icon, "b3-list-item__graphic", true)}
<span class="b3-list-item__text">${item.content}</span>
${getAttr(item)}
<span class="b3-list-item__meta b3-list-item__meta--ellipsis ariaLabel" data-tooltip-encoding="none" aria-label="${escapeHtmlTextAndAttr(tooltip)}">${escapeLessThans(title)}</span>
</div>`;
        });
        if (response.data.blocks.length > 0) {
            edit.protyle.element.classList.remove("fn__none");
            element.querySelector(".search__drag")?.classList.remove("fn__none");
            getArticle({
                edit,
                id: response.data.blocks[0].id,
            });
        } else {
            edit.protyle.element.classList.add("fn__none");
            element.querySelector(".search__drag")?.classList.add("fn__none");
        }
        element.querySelector("#searchUnRefResult").innerHTML = `${page}/${response.data.pageCount || 1}<span class="fn__space"></span>
<span class="ft__on-surface">${window.siyuan.languages.findInDoc.replace("${x}", response.data.matchedRootCount).replace("${y}", response.data.matchedBlockCount)}</span>`;
        element.querySelector("#searchUnRefList").innerHTML = resultHTML || `<div class="search__empty">
    ${window.siyuan.languages.emptyContent}
</div>`;
    });
};

export const unRefMoreMenu = (target: Element, element: Element, edit: Protyle) => {
    toggleMenu({
        target: target,
        build: (_menu, session) => {
            const menu = new Menu(Constants.MENU_SEARCH_UNREF_MORE);
            if (menu.isOpen) {
                return;
            }
            const localData = window.siyuan.storage[Constants.LOCAL_SEARCHUNREF];
            /// #if !MOBILE
            menu.addItem({
                icon: "iconLayout",
                label: window.siyuan.languages.layout,
                type: "submenu",
                submenu: [{
                    iconHTML: "",
                    label: window.siyuan.languages.topBottomLayout,
                    current: localData.layout === 0,
                    click() {
                        element.querySelector(".search__layout").classList.remove("search__layout--row");
                        edit.protyle.element.style.width = "";
                        if (localData.row) {
                            edit.protyle.element.style.height = localData.row;
                            edit.protyle.element.classList.remove("fn__flex-1");
                        } else {
                            edit.protyle.element.classList.add("fn__flex-1");
                        }
                        resize(edit.protyle);
                        localData.layout = 0;
                        setStorageVal(Constants.LOCAL_SEARCHUNREF, window.siyuan.storage[Constants.LOCAL_SEARCHUNREF]);
                    }
                }, {
                    iconHTML: "",
                    label: window.siyuan.languages.leftRightLayout,
                    current: localData.layout === 1,
                    click() {
                        element.querySelector(".search__layout").classList.add("search__layout--row");
                        edit.protyle.element.style.height = "";
                        if (localData.col) {
                            edit.protyle.element.style.width = localData.col;
                            edit.protyle.element.classList.remove("fn__flex-1");
                        } else {
                            edit.protyle.element.classList.add("fn__flex-1");
                        }
                        resize(edit.protyle);
                        localData.layout = 1;
                        setStorageVal(Constants.LOCAL_SEARCHUNREF, window.siyuan.storage[Constants.LOCAL_SEARCHUNREF]);
                    }
                }]
            });
            /// #endif
            menu.addItem({
                icon: "iconRefresh",
                label: window.siyuan.languages.refresh,
                click() {
                    const loadingElement = element.parentElement.querySelector(".fn__loading") as HTMLElement;
                    loadingElement.classList.remove("fn__none");
                    loadingElement.style.top = "42px";
                    getUnRefList(element, edit);
                },
            });
            /// #if MOBILE
            session.show(() => menu.fullscreen());
            /// #else
            const rect = target.getBoundingClientRect();
            session.show(() => menu.open({x: rect.right, y: rect.bottom, h: rect.height, isLeft: true}));
            /// #endif

        },
    });
};