Skip to content

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

68.3 KB
Raw Blame
import {patchAppearancePreference} from "../../config/tabs/appearancePreferences";
import {saveOutlineState} from "../../config/util/resourceConfig";
import {toggleMenu} from "../../menus/menuToggle";
import {bindPanelSearch} from "../../layout/dock/panelSearch";
import type {BlockBreadcrumbRequestInput} from "../../types/api";
import {Tree} from "../../util/Tree";
import {fetchPost} from "../../util/fetch";
import {confirmBlockRef} from "../../util/checkBlockRef";
import {hasClosestBlock, hasClosestByClassName} from "../../protyle/util/hasClosest";
import {writeBlockDOMClipboard} from "../../protyle/util/compatibility";
import {Constants} from "../../constants";
import {MenuItem} from "../../menus/Menu";
import {getPreviousBlock} from "../../protyle/wysiwyg/getBlock";
import type {App} from "../../index";
import {checkFold} from "../../util/noRelyPCFunction";
import {transaction, turnsIntoTransaction} from "../../protyle/wysiwyg/transaction";
import {mathRender} from "../../protyle/render/mathRender";
import {genEmptyElement} from "../../block/util";
import {focusBlock} from "../../protyle/util/selection";
import {openMobileFileById} from "../editor";
import {Model} from "../../layout/Model";
import {genUUID} from "../../util/genID";
import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";
import {dragOverScroll, stopScrollAnimation} from "../../boot/globalEvent/dragover";
import {escapeHtml} from "../../util/escape";
import {getFileTreeIconHTML} from "../../emoji/fileTreeIcon";
import {syncDocTitleIAL} from "../../protyle/util/docTitleIAL";
import {canBatchConvertHeadings, showHeadingBatchDialog} from "../../protyle/util/headingBatch";
import {bindMousePointerTouchBridge, isMousePointerTouchEvent} from "../util/mousePointerTouchBridge";
import {waitForPendingTransactions} from "../../protyle/util/transactionQueue";
import {getHeadingLevel} from "../../block/headingLevel";
import {
    operationsMayChangeOutline,
    transactionsMayChangeRootHeadingNumberSetting
} from "../../protyle/util/headingNumberCore";

export class MobileOutline extends Model {
    private currentRequestID = 0;
    public tree: Tree;
    public element: HTMLElement;
    public blockId: string;
    private notebookId: string;
    public isPreview: boolean;
    private preFilterExpandIds: string[] | null = null;
    private reloadId = 0;
    private touchDragState: {
        selectedElement: HTMLElement;
        startX: number;
        startY: number;
        isDragging: boolean;
        ghostElement: HTMLElement;
        startTime: number;
        selectItem: HTMLElement;
    };

    constructor(options: {
        app: App,
        blockId: string,
        isPreview: boolean,
        element: HTMLElement
    }) {
        super({app: options.app});
        this.connect({
            id: genUUID(),
            type: "outline",
            msgCallback: this.handleMsgCallback.bind(this)
        });

        this.isPreview = options.isPreview;
        this.blockId = options.blockId;
        this.element = options.element;
        this.element.innerHTML = `<div class="toolbar toolbar--dark">
    <div class="fn__space"></div>
    <div class="toolbar__text">
        ${window.siyuan.languages.outline}
    </div>
    <div class="fn__space"></div>
    <input spellcheck="false" class="b3-text-field search__label fn__none fn__size200" placeholder="${window.siyuan.languages.searchPlaceholder}" />
    <svg data-type="search" class="toolbar__icon"><use xlink:href='#iconSearch'></use></svg>
    <svg data-type="keepCurrentExpand" class="toolbar__icon${window.siyuan.config.appearance.outlineKeepCurrentExpand ? " toolbar__icon--active" : ""}"><use xlink:href="#iconFocus"></use></svg>
    <svg data-type="expandLevel" class="toolbar__icon"><use xlink:href="#iconExpandLevel"></use></svg>
</div>
<div class="b3-list-item fn__none" data-type="doc-title"></div>
<div class="fn__flex-1" style="padding: 3px 0 calc(8px + env(safe-area-inset-bottom))"></div>`;
        const inputElement = this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement;
        const showSearch = bindPanelSearch(inputElement,
            this.element.querySelector('[data-type="search"]'), () => this.setFilter(), {
                trim: false, activeClass: "toolbar__icon--active", updateLabel: false,
            });
        this.tree = new Tree({
            element: this.element.lastElementChild as HTMLElement,
            data: null,
            click: (element: HTMLElement, event) => {
                if (event) {
                    const actionElement = hasClosestByClassName(event.target as HTMLElement, "b3-list-item__action");
                    if (actionElement) {
                        this.showContextMenu(element, actionElement);
                        return;
                    }
                }
                const id = element.getAttribute("data-node-id");
                if (this.isPreview) {
                    const headElement = document.getElementById(id);
                    if (headElement) {
                        headElement.scrollIntoView();
                    } else {
                        openMobileFileById(options.app, this.blockId);
                    }
                } else {
                    checkFold(id, (zoomIn) => {
                        openMobileFileById(options.app, id, zoomIn ? [Constants.CB_GET_HL, Constants.CB_GET_ALL, Constants.CB_GET_HTML, Constants.CB_GET_OUTLINE] :
                                [Constants.CB_GET_HL, Constants.CB_GET_OUTLINE, Constants.CB_GET_SETID, Constants.CB_GET_CONTEXT, Constants.CB_GET_HTML],
                            "start");
                    });
                }
            },
            toggleClick: (liElement) => {
                if (!liElement.nextElementSibling) {
                    return;
                }
                const svgElement = liElement.firstElementChild.firstElementChild;
                if (svgElement.classList.contains("b3-list-item__arrow--open")) {
                    svgElement.classList.remove("b3-list-item__arrow--open");
                    liElement.nextElementSibling.classList.add("fn__none");
                    if (liElement.nextElementSibling.nextElementSibling && liElement.nextElementSibling.nextElementSibling.tagName === "UL") {
                        liElement.nextElementSibling.nextElementSibling.classList.add("fn__none");
                    }
                } else {
                    svgElement.classList.add("b3-list-item__arrow--open");
                    liElement.nextElementSibling.classList.remove("fn__none");
                    if (liElement.nextElementSibling.nextElementSibling && liElement.nextElementSibling.nextElementSibling.tagName === "UL") {
                        liElement.nextElementSibling.nextElementSibling.classList.remove("fn__none");
                    }
                }
                this.saveExpendIds();
            },
            blockExtHTML: window.siyuan.config.readonly ? undefined : '<span class="b3-list-item__action"><svg><use xlink:href="#iconMore"></use></svg></span>',
            topExtHTML: window.siyuan.config.readonly ? undefined : '<span class="b3-list-item__action"><svg><use xlink:href="#iconMore"></use></svg></span>',
        });
        // 保持当前标题展开功能
        this.element.querySelector('[data-type="keepCurrentExpand"]').addEventListener("click", async (event: MouseEvent & {
            target: Element
        }) => {
            const iconElement = hasClosestByClassName(event.target, "toolbar__icon");
            if (!iconElement) {
                return;
            }
            if (window.siyuan.config.readonly || window.siyuan.isPublish) return;
            await patchAppearancePreference("outlineKeepCurrentExpand", !window.siyuan.config.appearance.outlineKeepCurrentExpand);
            if (!window.siyuan.config.appearance.outlineKeepCurrentExpand) {
                iconElement.classList.remove("toolbar__icon--active");
            } else {
                iconElement.classList.add("toolbar__icon--active");
                let focusElement;
                const blockElement = hasClosestBlock(window.siyuan.mobile.editor.protyle.toolbar.range?.startContainer);
                if (blockElement) {
                    focusElement = blockElement;
                }
                if (focusElement) {
                    this.setCurrent(focusElement);
                }
            }
        });
        this.element.addEventListener("click", (event: MouseEvent & { target: HTMLElement }) => {
            let target = event.target as HTMLElement;
            if (target.tagName === "INPUT") {
                return;
            }
            while (target && !target.isEqualNode(this.element)) {
                if (target.classList.contains("toolbar__icon")) {
                    const type = target.getAttribute("data-type");
                    switch (type) {
                        case "search":
                            showSearch();
                            break;
                        case "expandLevel":
                            this.showExpandLevelMenu();
                            event.preventDefault();
                            event.stopPropagation();
                            break;
                    }
                    break;
                }
                target = target.parentElement;
            }
        });

        this.bindSort();
        this.element.querySelector('[data-type="doc-title"]').addEventListener("click", () => {
            openMobileFileById(this.app, this.blockId);
        });
        this.reload();
    }

    // 大纲拖拽排序:原生触摸事件实现,长按门槛避免与列表滚动冲突,排序逻辑对齐桌面端 Outline.bindSort
    private bindSort() {
        // 滚动容器为列表本体(不含顶部工具栏),否则边缘滚动判定会把工具栏算进可视区导致拖到列表顶/底部不滚动
        const scrollElement = this.tree.element;
        const contentRect = () => scrollElement.getBoundingClientRect();
        this.element.addEventListener("touchstart", (event: TouchEvent) => {
            if (window.siyuan.config.readonly) return;
            if (this.element.getAttribute("data-loading") === "true") return;
            if (event.touches.length !== 1) return;
            // 仅当触摸到当前文档对应的编辑器时才允许拖拽排序
            const editor = window.siyuan.mobile.editor?.protyle;
            if (!editor || editor.disabled || editor.block.rootID !== this.blockId) return;

            const touch = event.touches[0];
            const liElement = hasClosestByClassName(touch.target as HTMLElement, "b3-list-item") as HTMLElement;
            if (!liElement || liElement.tagName !== "LI") return;

            this.touchDragState = {
                selectedElement: liElement,
                startX: touch.clientX,
                startY: touch.clientY,
                isDragging: false,
                ghostElement: null,
                startTime: Date.now() - (isMousePointerTouchEvent(event) ? Constants.TIMEOUT_LONGPRESS : 0),
                selectItem: null,
            };
            if (!isMousePointerTouchEvent(event)) {
                const state = this.touchDragState;
                window.setTimeout(() => {
                    if (this.touchDragState !== state) {
                        return;
                    }
                    this.startTouchDrag(state, state.startX, state.startY, true);
                }, Constants.TIMEOUT_LONGPRESS);
            }
        }, {passive: false});

        this.element.addEventListener("touchmove", (event: TouchEvent) => {
            const state = this.touchDragState;
            if (!state) return;
            const touch = event.touches[0];

            // 长按门槛:未达到长按时间就移动则视为滚动,放弃拖拽
            if (!state.isDragging) {
                if (Date.now() - state.startTime < Constants.TIMEOUT_LONGPRESS &&
                    (Math.abs(touch.clientX - state.startX) > Constants.SIZE_DRAG_THRESHOLD ||
                        Math.abs(touch.clientY - state.startY) > Constants.SIZE_DRAG_THRESHOLD)) {
                    this.touchDragState = null;
                    return;
                }
                if (Math.abs(touch.clientX - state.startX) < Constants.SIZE_DRAG_THRESHOLD &&
                    Math.abs(touch.clientY - state.startY) < Constants.SIZE_DRAG_THRESHOLD) return;
                this.startTouchDrag(state, touch.clientX, touch.clientY, !isMousePointerTouchEvent(event));
            }

            // 进入拖拽后阻止原生滚动,避免列表伴随手指滚动
            event.preventDefault();
            event.stopPropagation();
            state.ghostElement.style.top = touch.clientY + "px";
            state.ghostElement.style.left = touch.clientX + "px";
            dragOverScroll({clientY: touch.clientY} as MouseEvent, contentRect(), scrollElement);

            // 通过 elementFromPoint 定位手指下的目标项
            const target = document.elementFromPoint(touch.clientX, touch.clientY);
            const selectItem = target?.closest(".b3-list-item") as HTMLElement;
            if (!selectItem || selectItem.tagName !== "LI" || !scrollElement.contains(selectItem)) {
                this.clearDragIndicators();
                state.selectItem = null;
                return;
            }

            this.clearDragIndicators();
            if (selectItem === state.selectedElement) {
                selectItem.classList.add("dragover__current");
                return;
            }
            const selectRect = selectItem.getBoundingClientRect();
            const dragHeight = selectRect.height * .2;
            if (touch.clientY > selectRect.bottom - dragHeight) {
                selectItem.classList.add("dragover__bottom");
            } else if (touch.clientY < selectRect.top + dragHeight) {
                selectItem.classList.add("dragover__top");
            } else {
                selectItem.classList.add("dragover");
            }
            state.selectItem = selectItem;
        }, {passive: false});

        this.element.addEventListener("touchend", () => {
            const state = this.touchDragState;
            if (!state) return;
            stopScrollAnimation();
            state.selectedElement.style.opacity = "";
            const item = state.selectedElement;

            if (state.isDragging) {
                state.ghostElement?.remove();
                let selectItem = state.selectItem;
                if (!selectItem) {
                    selectItem = this.element.querySelector(".dragover__top, .dragover__bottom, .dragover") as HTMLElement;
                }
                const editor = window.siyuan.mobile.editor?.protyle;
                let hasChange = true;
                if (selectItem && editor &&
                    (selectItem.classList.contains("dragover__top") || selectItem.classList.contains("dragover__bottom") || selectItem.classList.contains("dragover"))) {
                    let previousID;
                    let parentID;
                    const undoPreviousID = (item.previousElementSibling && item.previousElementSibling.tagName === "UL") ? item.previousElementSibling.previousElementSibling.getAttribute("data-node-id") : item.previousElementSibling?.getAttribute("data-node-id");
                    const undoParentID = item.parentElement.previousElementSibling?.getAttribute("data-node-id");
                    if (selectItem.classList.contains("dragover")) {
                        parentID = selectItem.getAttribute("data-node-id");
                        if (selectItem.nextElementSibling && selectItem.nextElementSibling.tagName === "UL") {
                            const children = selectItem.nextElementSibling;
                            const lastHeading = children.querySelector(":scope > li:last-of-type");
                            previousID = lastHeading?.getAttribute("data-node-id");
                            if (previousID === item.dataset.nodeId) {
                                hasChange = false;
                            } else {
                                children.insertAdjacentElement("beforeend", item);
                            }
                        } else {
                            selectItem.insertAdjacentHTML("afterend", `<ul>${item.outerHTML}</ul>`);
                            item.remove();
                        }
                    } else if (selectItem.classList.contains("dragover__top")) {
                        parentID = selectItem.parentElement.previousElementSibling?.getAttribute("data-node-id");
                        if (selectItem.previousElementSibling && selectItem.previousElementSibling.tagName === "UL") {
                            previousID = selectItem.previousElementSibling.previousElementSibling.getAttribute("data-node-id");
                        } else {
                            previousID = selectItem.previousElementSibling?.getAttribute("data-node-id");
                        }
                        if (previousID === item.dataset.nodeId || parentID === item.dataset.nodeId) {
                            hasChange = false;
                        } else {
                            selectItem.before(item);
                        }
                    } else if (selectItem.classList.contains("dragover__bottom")) {
                        previousID = selectItem.getAttribute("data-node-id");
                        if (previousID === item.previousElementSibling?.getAttribute("data-node-id")) {
                            hasChange = false;
                        } else {
                            selectItem.after(item);
                        }
                    }
                    if (hasChange) {
                        this.element.setAttribute("data-loading", "true");

                        transaction(editor, [{
                            action: "moveOutlineHeading",
                            id: item.dataset.nodeId,
                            previousID,
                            parentID,
                        }], [{
                            action: "moveOutlineHeading",
                            id: item.dataset.nodeId,
                            previousID: undoPreviousID,
                            parentID: undoParentID,
                        }]);

                        // https://github.com/siyuan-note/siyuan/issues/10828#issuecomment-2044099675
                        editor.wysiwyg.element.querySelectorAll('[data-type="NodeHeading"] [contenteditable="true"][spellcheck]').forEach(headingItem => {
                            headingItem.setAttribute("contenteditable", "false");
                        });
                    }
                }
                this.clearDragIndicators();
            }
            this.touchDragState = null;
        });

        const cancelTouchDrag = () => {
            stopScrollAnimation();
            if (this.touchDragState?.ghostElement) {
                this.touchDragState.ghostElement.remove();
            }
            if (this.touchDragState?.selectedElement) {
                this.touchDragState.selectedElement.style.opacity = "";
            }
            this.clearDragIndicators();
            this.touchDragState = null;
        };
        this.element.addEventListener("touchcancel", cancelTouchDrag);
        this.element.addEventListener("pointercancel", cancelTouchDrag);
        window.addEventListener("blur", cancelTouchDrag);
        bindMousePointerTouchBridge(this.element);
    }

    private startTouchDrag(state: MobileOutline["touchDragState"], clientX: number, clientY: number, vibrate = false) {
        if (state.isDragging) {
            return;
        }
        state.isDragging = true;
        state.selectedElement.style.opacity = "0.38";
        const ghostElement = state.selectedElement.cloneNode(true) as HTMLElement;
        ghostElement.setAttribute("id", "dragGhost");
        ghostElement.firstElementChild.setAttribute("style", "padding-left:4px");
        ghostElement.setAttribute("style", `border-radius: var(--b3-border-radius);background-color: var(--b3-list-hover);pointer-events:none;position: fixed; top: ${clientY}px; left: ${clientX}px; z-index:999997;`);
        document.body.append(ghostElement);
        state.ghostElement = ghostElement;
        if (vibrate) {
            if (window.webkit?.messageHandlers.vibrate) {
                window.webkit.messageHandlers.vibrate.postMessage("");
            } else if (navigator.vibrate) {
                navigator.vibrate(Constants.TIMEOUT_VIBRATION_DURATION);
            }
        }
    }

    // 清理拖拽指示线
    private clearDragIndicators = () => {
        this.element.querySelectorAll(".dragover__top, .dragover__bottom, .dragover, .dragover__current").forEach(item => {
            item.classList.remove("dragover__top", "dragover__bottom", "dragover", "dragover__current");
        });
    };

    private handleMsgCallback(data: IWebSocketData) {
        if (data) {
            switch (data.cmd) {
                case "savedoc":
                    this.onTransaction(data);
                    break;
                case "transactions":
                    if (transactionsMayChangeRootHeadingNumberSetting(data.data, this.blockId)) {
                        this.reload();
                    }
                    break;
                case "rename":
                    if (this.blockId === data.data.id) {
                        this.updateDocTitle(syncDocTitleIAL({
                            icon: Constants.ZWSP,
                        }, data.data.title, data.data.empty, Constants.CUSTOM_SY_TITLE_EMPTY), -1);
                    }
                    break;
                case "closeBox":
                case "removeBox":
                    if (this.notebookId === data.data.box) {
                        this.clear();
                    }
                    break;
                case "removeDoc":
                    if (data.data.ids.includes(this.blockId)) {
                        this.clear();
                    }
                    break;
            }
        }
    }

    public async setCurrent(nodeElement: HTMLElement) {
        const requestID = ++this.currentRequestID;
        if (!nodeElement) {
            return;
        }
        if (nodeElement.getAttribute("data-type") === "NodeHeading" &&
            !hasClosestByClassName(nodeElement, "bq") &&
            !hasClosestByClassName(nodeElement, "callout-content") &&
            !hasClosestByClassName(nodeElement, "tab-item")) {
            this.setCurrentById(nodeElement.getAttribute("data-node-id"));
        } else {
            let previousElement = getPreviousBlock(nodeElement);
            while (previousElement) {
                if (previousElement.getAttribute("data-type") === "NodeHeading" &&
                    !hasClosestByClassName(previousElement, "bq") &&
                    !hasClosestByClassName(previousElement, "callout-content") &&
                    !hasClosestByClassName(previousElement, "tab-item")) {
                    break;
                } else {
                    previousElement = getPreviousBlock(previousElement);
                }
            }
            if (previousElement) {
                this.setCurrentById(previousElement.getAttribute("data-node-id"));
            } else {
                const breadcrumbParam: BlockBreadcrumbRequestInput = {
                    id: nodeElement.getAttribute("data-node-id"),
                    excludeTypes: []
                };
                const mobileProtyle = window.siyuan.mobile.editor?.protyle;
                if (mobileProtyle && mobileProtyle.block.rootID === this.blockId) {
                    breadcrumbParam.notebook = mobileProtyle.notebookId;
                }
                const blockID = this.blockId;
                const isCurrent = () => requestID === this.currentRequestID && blockID === this.blockId &&
                    nodeElement.isConnected;
                if (mobileProtyle && mobileProtyle.block.rootID === blockID) {
                    await waitForPendingTransactions(mobileProtyle);
                }
                if (!isCurrent()) {
                    return;
                }
                fetchPost("/api/block/getBlockBreadcrumb", breadcrumbParam, (response) => {
                    if (!isCurrent()) {
                        return;
                    }
                    response.data.reverse().find((item: IBreadcrumb) => {
                        if (item.type === "NodeHeading") {
                            this.setCurrentById(item.id);
                            return true;
                        }
                    });
                });
            }
        }
    }

    public setCurrentByPreview(nodeElement: Element) {
        if (!nodeElement) {
            return;
        }
        let previousElement = nodeElement;
        while (previousElement && !previousElement.classList.contains("b3-typography")) {
            if (["H1", "H2", "H3", "H4", "H5", "H6"].includes(previousElement.tagName)) {
                break;
            } else {
                previousElement = previousElement.previousElementSibling || previousElement.parentElement;
            }
        }
        if (previousElement && previousElement.id) {
            this.setCurrentById(previousElement.id);
        }
    }

    private setCurrentById(id: string) {
        this.element.querySelectorAll(".b3-list-item.b3-list-item--focus").forEach(item => {
            item.classList.remove("b3-list-item--focus");
        });
        let currentElement = this.element.querySelector(`.b3-list-item[data-node-id="${id}"]`) as HTMLElement;
        if (!currentElement) {
            return;
        }
        if (window.siyuan.config.appearance.outlineKeepCurrentExpand) {
            let ulElement = currentElement.parentElement;
            while (ulElement && !ulElement.classList.contains("b3-list") && ulElement.tagName === "UL") {
                ulElement.classList.remove("fn__none");
                ulElement.previousElementSibling.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open");
                ulElement = ulElement.parentElement;
            }
            this.saveExpendIds();
        } else {
            while (currentElement && currentElement.clientHeight === 0) {
                currentElement = currentElement.parentElement.previousElementSibling as HTMLElement;
            }
        }
        if (currentElement) {
            currentElement.classList.add("b3-list-item--focus");
            const elementRect = this.tree.element.getBoundingClientRect();
            const currentRect = currentElement.getBoundingClientRect();
            this.tree.element.scrollTop += currentRect.bottom -
                (elementRect.top + elementRect.height / 2);
        }
    }

    private clear() {
        // 关闭后清除隐藏面板的内容,并使在途响应失效。
        this.reloadId++;
        this.currentRequestID++;
        this.blockId = "";
        this.notebookId = "";
        this.isPreview = false;
        this.preFilterExpandIds = null;
        this.tree.updateData(null);
        this.tree.element.scrollTop = 0;
        this.updateDocTitle();
        this.element.removeAttribute("data-loading");
    }

    public reload(callback?: () => void) {
        if (document.getElementById("editor").classList.contains("fn__none")) {
            this.clear();
            return;
        }
        const protyle = window.siyuan.mobile.editor?.protyle;
        const blockId = protyle?.block.rootID || this.blockId;
        if (!blockId) {
            return;
        }
        const isPreview = protyle ? !protyle.preview.element.classList.contains("fn__none") : this.isPreview;
        if (blockId !== this.blockId) {
            this.tree.updateData(null);
            this.updateDocTitle();
            this.tree.element.scrollTop = 0;
        }
        this.blockId = blockId;
        this.notebookId = protyle?.notebookId || "";
        this.isPreview = isPreview;
        const reloadId = ++this.reloadId;
        const outlineParam: IObject = {
            id: blockId,
            preview: isPreview
        };
        if (protyle && isEncryptedBox(protyle.notebookId)) {
            outlineParam.notebook = protyle.notebookId;
        }
        fetchPost("/api/outline/getDocOutline", outlineParam, response => {
            if (reloadId !== this.reloadId || window.siyuan.mobile.editor?.protyle.block.rootID !== blockId) {
                return;
            }
            this.update(response);
            this.updateDocTitle(protyle?.background?.ial, Array.isArray(response.data) ? response.data.length : 0);
            callback?.();
        });
    }

    public update(data: IWebSocketData) {
        let currentElement = this.element.querySelector(".b3-list-item--focus");
        let currentId;
        if (currentElement) {
            currentId = currentElement.getAttribute("data-node-id");
        }
        const scrollTop = this.tree.element.scrollTop;
        this.tree.updateData(data.data);

        if (this.isPreview) {
            this.tree.element.querySelectorAll(".popover__block").forEach(item => {
                item.classList.remove("popover__block");
            });
            this.tree.element.scrollTop = scrollTop;
        } else if (this.blockId) {
            if ((this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement).value) {
                this.setFilter();
            }
            this.tree.element.scrollTop = scrollTop;
        }
        if (currentId) {
            currentElement = this.element.querySelector(`[data-node-id="${currentId}"]`);
            if (currentElement) {
                currentElement.classList.add("b3-list-item--focus");
            }
        }
        this.element.removeAttribute("data-loading");
    }

    private updateDocTitle(ial?: Record<string, string>, count?: number) {
        const docTitleElement = this.element.querySelector('[data-type="doc-title"]') as HTMLElement;
        if (this.isPreview || !ial) {
            docTitleElement.innerHTML = "";
            docTitleElement.removeAttribute("title");
            docTitleElement.classList.add("fn__none");
            return;
        }
        let iconHTML = getFileTreeIconHTML(ial.icon, "file", "b3-list-item__graphic", true);
        if (ial.icon === Constants.ZWSP && docTitleElement.firstElementChild) {
            iconHTML = docTitleElement.firstElementChild.outerHTML;
        }
        const title = getDocDisplayName(ial.title, ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");
        docTitleElement.innerHTML = `${iconHTML}<span class="b3-list-item__text">${escapeHtml(title)}</span>${docTitleElement.querySelector(".counter")?.outerHTML || ""}`;
        docTitleElement.setAttribute("title", title);
        docTitleElement.classList.remove("fn__none");
        if (typeof count === "number" && count !== -1) {
            const counterElement = docTitleElement.querySelector(".counter") as HTMLElement;
            if (count > 0) {
                if (counterElement) {
                    counterElement.textContent = count.toString();
                } else {
                    docTitleElement.insertAdjacentHTML("beforeend", `<span class="counter">${count.toString()}</span>`);
                }
            } else {
                counterElement?.remove();
            }
        }
    }

    public saveExpendIds() {
        if (window.siyuan.config.readonly || window.siyuan.isPublish) {
            return;
        }

        // 未打开文档时没有标题状态可持久化
        if (!this.blockId) {
            return;
        }

        if (!this.isPreview) {
            saveOutlineState(this.blockId, this.tree.getExpandIds());
        }
    }

    /**
     * 应用大纲筛选
     */
    private setFilter() {
        // 还原 display
        this.element.querySelectorAll('li.b3-list-item[style$="display: none;"]').forEach((item: HTMLElement) => {
            item.style.display = "";
        });
        this.element.querySelectorAll("ul.fn__none").forEach((item) => {
            item.previousElementSibling.querySelector(".b3-list-item__toggle").classList.remove("fn__hidden");
        });
        const keyword = (this.element.querySelector("input.b3-text-field.search__label") as HTMLInputElement).value.toLowerCase();
        if (keyword) {
            // 首次筛选时记录折叠状态
            if (!this.preFilterExpandIds) {
                this.preFilterExpandIds = this.tree.getExpandIds();
            }
            const processUL = (ul: Element) => {
                let hasMatch = false;
                let hasChildMatch = false;
                const children = ul.querySelectorAll(":scope > li.b3-list-item");

                children.forEach((liItem: HTMLElement) => {
                    const nextUlElement = (liItem.nextElementSibling && liItem.nextElementSibling.tagName === "UL") ? liItem.nextElementSibling as HTMLElement : undefined;

                    let childResult = {hasMatch: false, hasChildMatch: false};
                    if (nextUlElement) {
                        childResult = processUL(nextUlElement);
                    }

                    const arrowElement = liItem.querySelector(".b3-list-item__arrow");
                    if ((liItem.querySelector(".b3-list-item__text")?.textContent || "").trim().toLowerCase().includes(keyword)) {
                        // 当前标题命中
                        liItem.style.display = "";
                        hasMatch = true;

                        if (nextUlElement) {
                            nextUlElement.classList.remove("fn__none");
                            if (childResult.hasMatch || childResult.hasChildMatch) {
                                // 子项也有命中
                                arrowElement.classList.add("b3-list-item__arrow--open");
                                nextUlElement.classList.remove("fn__none");
                            } else {
                                // 子项无命中,折叠所有子项
                                arrowElement.classList.remove("b3-list-item__arrow--open");
                                arrowElement.parentElement.classList.add("fn__hidden");
                                nextUlElement.classList.add("fn__none");
                            }
                        }
                    } else if (childResult.hasMatch || childResult.hasChildMatch) {
                        // 当前标题未命中,但子级有命中
                        liItem.style.display = "";
                        hasChildMatch = true;

                        if (nextUlElement) {
                            nextUlElement.classList.remove("fn__none");
                            arrowElement.classList.add("b3-list-item__arrow--open");
                        }
                    } else {
                        // 当前标题和子级都未命中,隐藏
                        liItem.style.display = "none";
                        if (nextUlElement) {
                            nextUlElement.classList.add("fn__none");
                        }
                    }
                });
                return {hasMatch, hasChildMatch};
            };

            processUL(this.element.lastElementChild.firstElementChild);
            return;
        }
        // 恢复折叠状态
        this.tree.setExpandIds(this.preFilterExpandIds);
        this.preFilterExpandIds = null;
    }

    /**
     * 展开到指定标题级别
     * @param targetLevel 目标标题级别,1-6级(H1-H6),6级表示全部展开
     */
    private expandToLevel(targetLevel: number) {
        if (targetLevel >= 6) {
            // 全部展开
            this.tree.expandAll();
        } else {
            // 展开到指定标题级别
            this.element.querySelectorAll("li.b3-list-item").forEach(item => {
                const headingLevel = getHeadingLevel(item);
                const arrowElement = item.querySelector(".b3-list-item__arrow");
                if (item.nextElementSibling && item.nextElementSibling.tagName === "UL" && arrowElement) {
                    if (headingLevel > 0 && headingLevel < targetLevel) {
                        // 当前标题级别小于目标级别,展开
                        arrowElement.classList.add("b3-list-item__arrow--open");
                        item.nextElementSibling.classList.remove("fn__none");
                    } else if (headingLevel >= targetLevel) {
                        // 当前标题级别大于等于目标级别,折叠
                        arrowElement.classList.remove("b3-list-item__arrow--open");
                        item.nextElementSibling.classList.add("fn__none");
                    }
                }
            });
        }
        this.saveExpendIds();
    }

    private setAllExpanded(expanded: boolean) {
        if (expanded) {
            this.tree.expandAll();
        } else {
            this.tree.collapseAll();
        }
        this.saveExpendIds();
    }

    /**
     * 显示展开层级菜单
     */
    private showExpandLevelMenu() {
        toggleMenu({
            target: this.element.querySelector("[data-type=expandLevel]"),
            build: (_menu, session) => {
                window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_OUTLINE_EXPAND_LEVEL);
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "expandAll",
                    icon: "iconExpand",
                    label: window.siyuan.languages.expandAll,
                    click: () => this.setAllExpanded(true)
                }).element);
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "foldAll",
                    icon: "iconContract",
                    label: window.siyuan.languages.foldAll,
                    click: () => this.setAllExpanded(false)
                }).element);
                window.siyuan.menus.menu.append(new MenuItem({id: "separator_all", type: "separator"}).element);
                for (let i = 1; i <= 6; i++) {
                    window.siyuan.menus.menu.append(new MenuItem({
                        id: `heading${i}`,
                        icon: `iconH${i}`,
                        label: window.siyuan.languages[`heading${i}`],
                        click: () => this.expandToLevel(i)
                    }).element);
                }
                session.show(() => window.siyuan.menus.menu.fullscreen("bottom"));

            },
        });
    }

    /**
     * 切换同层级的所有标题的展开/折叠状态(基于标题级别而不是DOM层级)
     */
    private collapseSameLevel(element: HTMLElement, expand?: boolean) {
        // 获取所有相同标题级别的元素
        this.element.querySelectorAll(`li.b3-list-item[data-subtype="${element.getAttribute("data-subtype")}"]`).forEach(item => {
            const arrowElement = item.querySelector(".b3-list-item__arrow");
            if (typeof expand === "undefined") {
                expand = !element.querySelector(".b3-list-item__arrow").classList.contains("b3-list-item__arrow--open");
            }
            if (expand) {
                if (item.nextElementSibling && item.nextElementSibling.tagName === "UL") {
                    item.nextElementSibling.classList.remove("fn__none");
                    arrowElement.classList.add("b3-list-item__arrow--open");
                }
                let ulElement = item.parentElement;
                while (ulElement && !ulElement.classList.contains("b3-list") && ulElement.tagName === "UL") {
                    ulElement.classList.remove("fn__none");
                    ulElement.previousElementSibling.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open");
                    ulElement = ulElement.parentElement;
                }
            } else {
                if (item.nextElementSibling && item.nextElementSibling.tagName === "UL") {
                    item.nextElementSibling.classList.add("fn__none");
                    arrowElement.classList.remove("b3-list-item__arrow--open");
                }
            }
        });
        this.saveExpendIds();
    }

    private collapseChildren(element: HTMLElement, expand?: boolean) {
        const nextElement = element.nextElementSibling;
        if (!nextElement || nextElement.tagName !== "UL") {
            return;
        }
        const arrowElement = element.querySelector(".b3-list-item__arrow");
        if (typeof expand === "undefined") {
            expand = !arrowElement.classList.contains("b3-list-item__arrow--open");
        }
        if (expand) {
            arrowElement.classList.add("b3-list-item__arrow--open");
            nextElement.classList.remove("fn__none");
            nextElement.querySelectorAll("ul").forEach(item => {
                item.previousElementSibling.querySelector(".b3-list-item__arrow").classList.add("b3-list-item__arrow--open");
                item.classList.remove("fn__none");
            });
        } else {
            arrowElement.classList.remove("b3-list-item__arrow--open");
            nextElement.classList.add("fn__none");
        }
        this.saveExpendIds();
    }

    private onTransaction(data: IWebSocketData) {
        if (data.data.rootID !== this.blockId) {
            return;
        }
        const ops = data.data.sources[0];
        const headingIDs = new Set(Array.from(this.element.querySelectorAll<HTMLElement>(
            ".b3-list-item[data-node-id]"
        )).map(item => item.dataset.nodeId!));
        const needReload = operationsMayChangeOutline(ops.doOperations, headingIDs) ||
            operationsMayChangeOutline(ops.undoOperations, headingIDs);
        if (needReload) {
            this.reload(() => {
                // https://github.com/siyuan-note/siyuan/issues/8372
                if (getSelection().rangeCount > 0) {
                    const blockElement = hasClosestBlock(getSelection().getRangeAt(0).startContainer);
                    if (blockElement && blockElement.getAttribute("data-type") === "NodeHeading") {
                        this.setCurrent(blockElement);
                    }
                }
            });
        }
    }

    /**
     * 显示右键菜单
     */
    private showContextMenu(element: HTMLElement, target?: Element) {
        toggleMenu({
            target: target || element,
            toggle: Boolean(target),
            build: (_menu, session) => {
                if (this.isPreview) {
                    return; // 预览模式下不显示右键菜单
                }
                const currentLevel = getHeadingLevel(element);

                window.siyuan.menus.menu.element.setAttribute("data-name", Constants.MENU_OUTLINE_CONTEXT);
                const id = element.getAttribute("data-node-id");
                if (!window.siyuan.config.readonly) {
                    // 升级
                    if (currentLevel > 1) {
                        window.siyuan.menus.menu.append(new MenuItem({
                            id: "upgrade",
                            icon: "iconUp",
                            label: window.siyuan.languages.upgrade,
                            click: () => {
                                const data = this.getProtyleAndBlockElement(element);
                                if (data) {
                                    getSelection()?.removeAllRanges();
                                    turnsIntoTransaction({
                                        protyle: data.protyle,
                                        selectsElement: [data.blockElement],
                                        type: "Blocks2Hs",
                                        level: currentLevel - 1,
                                        unfocus: true,
                                    });
                                }
                            }
                        }).element);
                    }

                    // 降级
                    if (currentLevel < 6) {
                        window.siyuan.menus.menu.append(new MenuItem({
                            id: "downgrade",
                            icon: "iconDown",
                            label: window.siyuan.languages.downgrade,
                            click: () => {
                                const data = this.getProtyleAndBlockElement(element);
                                if (data) {
                                    getSelection()?.removeAllRanges();
                                    turnsIntoTransaction({
                                        protyle: data.protyle,
                                        selectsElement: [data.blockElement],
                                        type: "Blocks2Hs",
                                        level: currentLevel + 1,
                                        unfocus: true,
                                    });
                                }
                            }
                        }).element);
                    }
                    this.setCurrentById(id);
                    const headingSubMenu = [];
                    if (currentLevel !== 1) {
                        headingSubMenu.push(this.genHeadingTransform(id, 1));
                    }
                    if (currentLevel !== 2) {
                        headingSubMenu.push(this.genHeadingTransform(id, 2));
                    }
                    if (currentLevel !== 3) {
                        headingSubMenu.push(this.genHeadingTransform(id, 3));
                    }
                    if (currentLevel !== 4) {
                        headingSubMenu.push(this.genHeadingTransform(id, 4));
                    }
                    if (currentLevel !== 5) {
                        headingSubMenu.push(this.genHeadingTransform(id, 5));
                    }
                    if (currentLevel !== 6) {
                        headingSubMenu.push(this.genHeadingTransform(id, 6));
                    }

                    if (headingSubMenu.length > 0) {
                        window.siyuan.menus.menu.append(new MenuItem({
                            id: "tWithSubtitle",
                            type: "submenu",
                            icon: "iconRefresh",
                            label: window.siyuan.languages.tWithSubtitle,
                            submenu: headingSubMenu
                        }).element);
                    }

                    const rootID = this.blockId;
                    window.siyuan.menus.menu.append(new MenuItem({
                        id: "headingBatch",
                        icon: "iconHeadings",
                        label: window.siyuan.languages.headingBatch,
                        click: () => {
                            const protyle = window.siyuan.mobile.editor?.protyle;
                            if (canBatchConvertHeadings(protyle, rootID, this.isPreview)) {
                                void showHeadingBatchDialog(protyle, () => rootID === this.blockId &&
                                    window.siyuan.mobile.editor?.protyle === protyle &&
                                    canBatchConvertHeadings(protyle, rootID, this.isPreview));
                            }
                        }
                    }).element);
                    window.siyuan.menus.menu.append(new MenuItem({id: "separator_1", type: "separator"}).element);

                    // 在前面插入同级标题
                    window.siyuan.menus.menu.append(new MenuItem({
                        id: "insertSameLevelHeadingBefore",
                        icon: "iconBefore",
                        label: window.siyuan.languages.insertSameLevelHeadingBefore,
                        click: () => {
                            const data = this.getProtyleAndBlockElement(element);
                            if (!data) {
                                return;
                            }
                            const newId = Lute.NewNodeID();
                            const html = `<div data-subtype="h${currentLevel}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
                            const previousID = data.blockElement.previousElementSibling?.getAttribute("data-node-id");
                            data.blockElement.insertAdjacentHTML("beforebegin", html);
                            const newElement = data.blockElement.previousElementSibling;
                            this.focusInsertedHeading(newElement);
                            transaction(data.protyle, [{
                                action: "insert",
                                data: html,
                                id: newId,
                                previousID,
                                parentID: data.blockElement.parentElement.getAttribute("data-node-id") || data.protyle.block.parentID,
                            }], [{
                                action: "delete",
                                id: newId
                            }]);
                        }
                    }).element);

                    // 在后面插入同级标题
                    window.siyuan.menus.menu.append(new MenuItem({
                        id: "insertSameLevelHeadingAfter",
                        icon: "iconAfter",
                        label: window.siyuan.languages.insertSameLevelHeadingAfter,
                        click: () => {
                            const data = this.getProtyleAndBlockElement(element);
                            if (!data) {
                                return;
                            }
                            const rootID = data.protyle.block.rootID;
                            focusBlock(data.blockElement);
                            fetchPost("/api/block/getHeadingDeleteTransaction", {
                                id,
                            }, (deleteResponse) => {
                                if (window.siyuan.mobile.editor?.protyle !== data.protyle || this.blockId !== rootID ||
                                    !deleteResponse.data?.doOperations?.length) {
                                    return;
                                }
                                const previousID = deleteResponse.data.doOperations[deleteResponse.data.doOperations.length - 1].id;

                                const newId = Lute.NewNodeID();
                                const html = `<div data-subtype="h${currentLevel}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
                                const previousElement = data.protyle.wysiwyg.element.querySelector(`[data-node-id="${previousID}"]`);
                                if (previousElement) {
                                    previousElement.insertAdjacentHTML("afterend", html);
                                    const newElement = previousElement.nextElementSibling;
                                    this.focusInsertedHeading(newElement);
                                }
                                transaction(data.protyle, [{
                                    action: "insert",
                                    data: html,
                                    id: newId,
                                    previousID,
                                }], [{
                                    action: "delete",
                                    id: newId
                                }]);
                            });
                        }
                    }).element);

                    // 添加子标题
                    if (currentLevel < 6) { // 只有当前级别小于6时才能添加子标题
                        window.siyuan.menus.menu.append(new MenuItem({
                            id: "addChildHeading",
                            icon: "iconAdd",
                            label: window.siyuan.languages.addChildHeading,
                            click: () => {
                                const data = this.getProtyleAndBlockElement(element);
                                if (!data) {
                                    return;
                                }
                                const rootID = data.protyle.block.rootID;
                                focusBlock(data.blockElement);
                                fetchPost("/api/block/getHeadingDeleteTransaction", {
                                    id,
                                }, (deleteResponse) => {
                                    if (window.siyuan.mobile.editor?.protyle !== data.protyle || this.blockId !== rootID ||
                                        !deleteResponse.data?.doOperations?.length || !deleteResponse.data?.undoOperations) {
                                        return;
                                    }
                                    let previousID = deleteResponse.data.doOperations[deleteResponse.data.doOperations.length - 1].id;
                                    deleteResponse.data.undoOperations.find((operationsItem, index: number) => {
                                        if (typeof operationsItem.data !== "string") {
                                            return false;
                                        }
                                        const startIndex = operationsItem.data.indexOf(' data-subtype="h');
                                        if (index > 0 && startIndex > -1 && startIndex < 260 && parseInt(operationsItem.data.substring(startIndex + 16, startIndex + 17)) === currentLevel + 1) {
                                            previousID = deleteResponse.data.undoOperations[index - 1].id;
                                            return true;
                                        }
                                    });
                                    const newId = Lute.NewNodeID();
                                    const html = `<div data-subtype="h${currentLevel + 1}" data-node-id="${newId}" data-type="NodeHeading" class="h${currentLevel + 1}"><div contenteditable="true" spellcheck="false"><wbr></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`;
                                    const previousElement = data.protyle.wysiwyg.element.querySelector(`[data-node-id="${previousID}"]`);
                                    if (previousElement) {
                                        previousElement.insertAdjacentHTML("afterend", html);
                                        const newElement = previousElement.nextElementSibling;
                                        this.focusInsertedHeading(newElement);
                                    }
                                    transaction(data.protyle, [{
                                        action: "insert",
                                        data: html,
                                        id: newId,
                                        previousID,
                                    }], [{
                                        action: "delete",
                                        id: newId
                                    }]);
                                });
                            }
                        }).element);
                    }

                    window.siyuan.menus.menu.append(new MenuItem({id: "separator_2", type: "separator"}).element);
                }

                // 复制带子标题
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "copyHeadings1",
                    icon: "iconCopy",
                    label: `${window.siyuan.languages.copy} ${window.siyuan.languages.headings1}`,
                    click: () => {
                        const data = this.getProtyleAndBlockElement(element);
                        fetchPost("/api/block/getHeadingChildrenDOM", {
                            id,
                            removeFoldAttr: false
                        }, async (response) => {
                            await writeBlockDOMClipboard(data.protyle.lute, response.data);
                        });
                    }
                }).element);

                if (!window.siyuan.config.readonly) {
                    // 剪切带子标题
                    window.siyuan.menus.menu.append(new MenuItem({
                        id: "cutHeadings1",
                        icon: "iconCut",
                        label: `${window.siyuan.languages.cut} ${window.siyuan.languages.headings1}`,
                        click: () => {
                            const data = this.getProtyleAndBlockElement(element);
                            fetchPost("/api/block/getHeadingChildrenDOM", {
                                id,
                                removeFoldAttr: false
                            }, (response) => {
                                fetchPost("/api/block/getHeadingDeleteTransaction", {
                                    id,
                                }, async (deleteResponse) => {
                                    const headingTransaction: {doOperations: IOperation[], undoOperations: IOperation[]} = deleteResponse.data;
                                    const deletedIDs = headingTransaction.doOperations.map(
                                        (operation: IOperation) => operation.id);
                                    if (!await confirmBlockRef({
                                        scope: "blocks",
                                        ids: deletedIDs,
                                        deletedIDs,
                                        notebook: data.protyle.notebookId,
                                    }, data.protyle)) {
                                        return;
                                    }
                                    if (!data.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`)) {
                                        return;
                                    }
                                    if (!await writeBlockDOMClipboard(data.protyle.lute, response.data)) {
                                        return;
                                    }
                                    if (!data.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`)) {
                                        return;
                                    }
                                    headingTransaction.doOperations.forEach((operation: IOperation) => {
                                        data.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
                                            itemElement.remove();
                                        });
                                    });
                                    if (data.protyle.wysiwyg.element.childElementCount === 0) {
                                        const newID = Lute.NewNodeID();
                                        const emptyElement = genEmptyElement(false, false, newID);
                                        data.protyle.wysiwyg.element.insertAdjacentElement("afterbegin", emptyElement);
                                        headingTransaction.doOperations.push({
                                            action: "insert",
                                            data: emptyElement.outerHTML,
                                            id: newID,
                                            parentID: data.protyle.block.parentID
                                        });
                                        headingTransaction.undoOperations.push({
                                            action: "delete",
                                            id: newID,
                                        });
                                        focusBlock(emptyElement);
                                    }
                                    transaction(data.protyle, headingTransaction.doOperations, headingTransaction.undoOperations);
                                });
                            });
                        }
                    }).element);

                    // 删除
                    window.siyuan.menus.menu.append(new MenuItem({
                        id: "deleteHeadings1",
                        icon: "iconTrashcan",
                        label: `${window.siyuan.languages.delete} ${window.siyuan.languages.headings1}`,
                        click: () => {
                            const data = this.getProtyleAndBlockElement(element);
                            fetchPost("/api/block/getHeadingDeleteTransaction", {
                                id,
                            }, async (response) => {
                                const headingTransaction: {doOperations: IOperation[], undoOperations: IOperation[]} = response.data;
                                const deletedIDs = headingTransaction.doOperations.map((operation: IOperation) => operation.id);
                                if (!await confirmBlockRef({
                                    scope: "blocks",
                                    ids: deletedIDs,
                                    deletedIDs,
                                    notebook: data.protyle.notebookId,
                                }, data.protyle)) {
                                    return;
                                }
                                if (!data.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`)) {
                                    return;
                                }
                                headingTransaction.doOperations.forEach((operation: IOperation) => {
                                    data.protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
                                        itemElement.remove();
                                    });
                                });
                                if (data.protyle.wysiwyg.element.childElementCount === 0) {
                                    const newID = Lute.NewNodeID();
                                    const emptyElement = genEmptyElement(false, false, newID);
                                    data.protyle.wysiwyg.element.insertAdjacentElement("afterbegin", emptyElement);
                                    headingTransaction.doOperations.push({
                                        action: "insert",
                                        data: emptyElement.outerHTML,
                                        id: newID,
                                        parentID: data.protyle.block.parentID
                                    });
                                    headingTransaction.undoOperations.push({
                                        action: "delete",
                                        id: newID,
                                    });
                                    focusBlock(emptyElement);
                                }
                                transaction(data.protyle, headingTransaction.doOperations, headingTransaction.undoOperations);
                            });
                        }
                    }).element);
                }
                window.siyuan.menus.menu.append(new MenuItem({id: "separator_3", type: "separator"}).element);

                // 展开子标题
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "expandChildHeading",
                    icon: "iconExpand",
                    label: window.siyuan.languages.expandChildHeading,
                    accelerator: "⌘" + window.siyuan.languages.clickArrow,
                    click: () => this.collapseChildren(element, true)
                }).element);

                // 折叠子标题
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "foldChildHeading",
                    icon: "iconContract",
                    label: window.siyuan.languages.foldChildHeading,
                    accelerator: "⌘" + window.siyuan.languages.clickArrow,
                    click: () => this.collapseChildren(element, false)
                }).element);

                // 展开同级标题
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "expandSameLevelHeading",
                    icon: "iconExpand",
                    label: window.siyuan.languages.expandSameLevelHeading,
                    accelerator: "⌥" + window.siyuan.languages.clickArrow,
                    click: () => this.collapseSameLevel(element, true)
                }).element);

                // 折叠同级标题
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "foldSameLevelHeading",
                    icon: "iconContract",
                    label: window.siyuan.languages.foldSameLevelHeading,
                    accelerator: "⌥" + window.siyuan.languages.clickArrow,
                    click: () => this.collapseSameLevel(element, false)
                }).element);

                // 全部展开
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "expandAll",
                    icon: "iconExpand",
                    label: window.siyuan.languages.expandAll,
                    click: () => this.setAllExpanded(true)
                }).element);

                // 全部折叠
                window.siyuan.menus.menu.append(new MenuItem({
                    id: "foldAll",
                    icon: "iconContract",
                    label: window.siyuan.languages.foldAll,
                    click: () => this.setAllExpanded(false)
                }).element);

                session.show(() => window.siyuan.menus.menu.fullscreen("bottom"));

            },
        });
    }

    private getProtyleAndBlockElement(element: HTMLElement) {
        const id = element.getAttribute("data-node-id");
        if (!window.siyuan.mobile.editor?.protyle) {
            return;
        }
        const blockElement = window.siyuan.mobile.editor.protyle.wysiwyg.element.querySelector(`[data-node-id="${id}"]`);
        if (!blockElement) {
            return;
        }
        return {
            protyle: window.siyuan.mobile.editor.protyle, blockElement
        };
    }

    private focusInsertedHeading(element: Element) {
        element.scrollIntoView();
        element.querySelector("wbr")?.remove();
        (element.querySelector('[contenteditable="true"]') as HTMLElement)?.focus({preventScroll: true});
        focusBlock(element);
    }

    /**
     * 生成标题级别转换菜单项
     */
    private genHeadingTransform(id: string, level: number) {
        return {
            id: "heading" + level,
            iconHTML: "",
            icon: "iconHeading" + level,
            label: window.siyuan.languages["heading" + level],
            click: () => {
                const protyle = window.siyuan.mobile.editor?.protyle;
                if (!protyle || !this.tree.element.querySelector(`[data-node-id="${id}"]`)) {
                    return;
                }
                const rootID = protyle.block.rootID;
                getSelection()?.removeAllRanges();
                fetchPost("/api/block/getHeadingLevelTransaction", {
                    id,
                    level
                }, (response) => {
                    if (window.siyuan.mobile.editor?.protyle !== protyle || this.blockId !== rootID ||
                        !response.data?.doOperations || !response.data?.undoOperations) {
                        return;
                    }
                    response.data.doOperations.forEach((operation: any, index: number) => {
                        protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
                            itemElement.outerHTML = operation.data;
                        });
                        // 使用 outer 后元素需要重新查询
                        protyle.wysiwyg.element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach((itemElement: HTMLElement) => {
                            mathRender(itemElement);
                        });
                        if (index === 0) {
                            const focusElement = protyle.wysiwyg.element.querySelector(`[data-node-id="${operation.id}"]`);
                            if (focusElement) {
                                focusElement.scrollIntoView({behavior: "smooth", block: "center"});
                            }
                        }
                    });
                    transaction(protyle, response.data.doOperations, response.data.undoOperations);
                });
            }
        };
    }
}