Skip to content

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

36.0 KB
Raw Blame
import type {FileTreeGetDocRequestInput} from "../types/api";
import {Constants} from "../constants";
import {Hint} from "./hint";
import {getLute} from "./render/setLute";
import {Preview} from "./preview";
import {addLoading, initUI, removeLoading} from "./ui/initUI";
import {BACKLINK_EDITOR_PADDING} from "./ui/padding";
import {LocalUndo, Undo} from "./undo";
import {Upload} from "./upload";
import {Options} from "./util/Options";
import {destroy} from "./util/destroy";
import {registerSyncEditorSave} from "./util/editorSave";
import {Scroll} from "./scroll";
import {Model} from "../layout/Model";
import {genUUID} from "../util/genID";
import {WYSIWYG} from "./wysiwyg";
import {Toolbar} from "./toolbar";
import {Gutter} from "./gutter";
import {setAutoDirection} from "./render/autoDirection";
import {Breadcrumb} from "./breadcrumb";
import {
    onTransaction,
    transaction,
    turnsIntoOneTransaction,
    turnsIntoTransaction,
    updateBatchTransaction,
    updateTransaction
} from "./wysiwyg/transaction";
import {fetchPost} from "../util/fetch";
import {getDocDisplayName, isEncryptedBox} from "../util/pathName";
import {syncDocTitleIAL} from "./util/docTitleIAL";
import {initMirror, refreshUndoButtons, syncMirrorFromBroadcast} from "./undo/globalUndo";
/// #if !MOBILE
import {updatePanelByEditor} from "../editor/util";
import {setPanelFocus} from "../layout/util";
/// #endif
import {Title} from "./header/Title";
import {Background} from "./header/Background";
import {disabledProtyle, enableProtyle, onGet, setReadonlyByConfig} from "./util/onGet";
import {reloadProtyle} from "./util/reload";
import {renderBacklink} from "./wysiwyg/renderBacklink";
import {setEmpty} from "../mobile/util/setEmpty";
/// #if MOBILE
import {removeMobileSecondaryEditor, unregisterMobileSecondaryEditor} from "../mobile/util/secondaryEditors";
import {destroyMobileBottomBacklinkPanel} from "../mobile/util/bottomBacklink";
/// #endif
import {resize} from "./util/resize";
import {getDocByScroll} from "./scroll/saveScroll";
import type {App} from "../index";
import {insertHTML} from "./util/insertHTML";
import {avRender} from "./render/av/render";
import {focusBlock, getEditorRange} from "./util/selection";
import {hasClosestBlock} from "./util/hasClosest";
import {setStorageVal} from "./util/compatibility";
import {merge} from "./util/merge";
/// #if !MOBILE
import {getAllModels} from "../layout/getAll";
import {
    invalidateSearchPathRequests,
    refreshSearchPathAfterNotebookRename,
    refreshSearchPathAfterRename,
} from "../search/path";
import {syncSearchConfigHPath} from "../search/config";
/// #endif
import {sanitizeKernelHTML} from "../util/hostCapabilities";
import {isSupportCSSHL} from "./render/searchMarkRender";
import {renderAVAttribute} from "./render/av/blockAttr";
import {setFoldById, zoomOut} from "../menus/protyle";
import {setEditMode} from "./util/setEditMode";
import {waitForPendingTransactions} from "./util/transactionQueue";
import {applyViewFoldStates, invalidateViewFoldRequests} from "./util/viewFold";
import {setFullscreen as setFullscreenState} from "./breadcrumb/action";
import {
    queueDatabaseRowRefresh,
    queueDatabaseRowRefreshForOperations
} from "./render/av/databaseRowRefresh";
import {initEditorTabs} from "./wysiwyg/tabs";
import {initListMindmaps} from "./render/listMindmap";
import {initFoldedRender} from "./util/foldedRender";
import {registerCustomBlockRoot} from "../plugin/customBlockRender";
import {getTransactionOperations} from "../util/transactionOperations";
import {
    invalidateTrackedRanges,
    releaseTrackedRange,
    resolveTrackedRange,
    trackRange,
} from "./util/trackedRange";
import {
    applyProtyleLockedOptions,
    areProtyleRuntimePluginExtensionsEnabled,
    disableProtyleUpload,
    isProtyleCustomBlockRenderEnabled,
    registerProtyleRuntimeCapabilities,
    resolveProtyleLute,
} from "./runtimeCapabilities";
import type {ProtyleRuntimeCapabilities} from "./runtimeCapabilities";

export type {ProtyleRuntimeCapabilities} from "./runtimeCapabilities";

/// #if !MOBILE
const forSearchByEditor = (edit: Protyle, callback: (config: Config.IUILayoutTabSearchConfig, element: Element) => void) => {
    window.siyuan.dialogs.find((item) => {
        const searchElement = item.element.querySelector(".b3-dialog__body");
        if (item.editors?.edit === edit && item.data && searchElement) {
            callback(item.data, searchElement);
            return true;
        }
    });
    getAllModels().search.find((item) => {
        if (item.editors.edit === edit) {
            callback(item.config, item.element);
            return true;
        }
    });
};

const persistRefreshedSearchPath = (config: Config.IUILayoutTabSearchConfig) => {
    const localConfig = window.siyuan.storage[Constants.LOCAL_SEARCHDATA];
    if (syncSearchConfigHPath(localConfig, config)) {
        setStorageVal(Constants.LOCAL_SEARCHDATA, localConfig);
    }
};
/// #endif

export class Protyle {

    public readonly version: string;
    public protyle: IProtyle;
    private destroyed = false;

    /**
     * @param id 要挂载 Protyle 的元素或者元素 ID。
     * @param options Protyle 参数
     */
    constructor(app: App, id: HTMLElement, options: IProtyleOptions,
                runtimeCapabilities: ProtyleRuntimeCapabilities = {}) {
        this.version = Constants.SIYUAN_VERSION;
        let pluginsOptions: IProtyleOptions = options;
        if (areProtyleRuntimePluginExtensionsEnabled(runtimeCapabilities)) {
            app.plugins.forEach(item => {
                if (item.protyleOptions) {
                    pluginsOptions = merge(pluginsOptions, item.protyleOptions);
                }
            });
        }
        pluginsOptions = applyProtyleLockedOptions(pluginsOptions, runtimeCapabilities.lockedOptions);
        const getOptions = new Options(pluginsOptions);
        const mergedOptions = getOptions.merge();
        if (runtimeCapabilities.upload === false) {
            mergedOptions.upload.url = "";
            mergedOptions.upload.linkToImgUrl = "";
            mergedOptions.upload.handler = undefined;
            mergedOptions.upload.file = undefined;
        }
        this.protyle = {
            getInstance: () => this,
            trackRange: (range, trackOptions) => this.trackRange(range, trackOptions),
            resolveTrackedRange: (handle) => this.resolveTrackedRange(handle),
            releaseTrackedRange: (handle) => this.releaseTrackedRange(handle),
            app,
            id: genUUID(),
            disabled: false,
            lite: !!options.lite,
            updated: false,
            element: id,
            notebookId: mergedOptions.notebookId,
            options: mergedOptions,
            block: {},
            highlight: {
                mark: isSupportCSSHL() ? new Highlight() : undefined,
                markHL: isSupportCSSHL() ? new Highlight() : undefined,
                ranges: [],
                rangeIndex: 0,
                styleElement: document.createElement("style"),
            }
        };
        registerProtyleRuntimeCapabilities(this.protyle, runtimeCapabilities);
        registerSyncEditorSave(this.protyle);
        if (runtimeCapabilities.upload === false) {
            disableProtyleUpload(this.protyle);
        }

        if (isSupportCSSHL()) {
            const styleId = genUUID();
            this.protyle.highlight.styleElement.dataset.uuid = styleId;
            this.protyle.highlight.styleElement.textContent = `.protyle-content::highlight(search-mark-${styleId}) {background-color: var(--b3-highlight-background);color: var(--b3-highlight-color);}
  .protyle-content::highlight(search-mark-hl-${styleId}) {color: var(--b3-highlight-color);background-color: var(--b3-highlight-current-background)}`;
        }

        this.protyle.hint = new Hint(this.protyle);
        if (mergedOptions.render.breadcrumb) {
            this.protyle.breadcrumb = new Breadcrumb(this.protyle);
        }
        if (mergedOptions.render.title) {
            this.protyle.title = new Title(this.protyle);
        }
        if (mergedOptions.render.background) {
            this.protyle.background = new Background(this.protyle);
        }

        this.protyle.element.innerHTML = "";
        this.protyle.element.classList.add("protyle");
        this.protyle.element.dataset.protyleLite = String(this.protyle.lite);
        if (this.protyle.notebookId) {
            this.protyle.element.setAttribute("data-notebook-id", this.protyle.notebookId);
        } else {
            this.protyle.element.removeAttribute("data-notebook-id");
        }
        // 启用 RTL 时给 .protyle 元素添加 .rtl 类名,方便主题开发者判断 RTL 方向
        if (window.siyuan.config.editor.rtl) {
            this.protyle.element.classList.add("rtl");
        }
        if (mergedOptions.render.breadcrumb) {
            this.protyle.element.appendChild(this.protyle.breadcrumb.element.parentElement);
        }
        // lite 模式用前端操作日志 undo(不依赖 kernel),其余走 kernel 的 GlobalUndoLog。
        this.protyle.undo = this.protyle.lite ? new LocalUndo() : new Undo();
        this.protyle.wysiwyg = new WYSIWYG(this.protyle);
        setAutoDirection(this.protyle.wysiwyg.element, window.siyuan.config.editor.autoDirection);
        initFoldedRender(this.protyle);
        initEditorTabs(this.protyle);
        initListMindmaps(this.protyle);
        if (isProtyleCustomBlockRenderEnabled(this.protyle)) {
            registerCustomBlockRoot(this.protyle.wysiwyg.element, {
                disabled: () => this.protyle.disabled,
                update: (element, oldHTML) => updateTransaction(this.protyle, element, oldHTML),
            });
        }
        this.protyle.toolbar = new Toolbar(this.protyle);
        this.protyle.scroll = new Scroll(this.protyle); // 不能使用 render.scroll 来判读是否初始化,除非重构后面用到的相关变量
        if (this.protyle.options.render.gutter) {
            this.protyle.gutter = new Gutter(this.protyle);
        }
        if (runtimeCapabilities.upload !== false && (mergedOptions.upload.url || mergedOptions.upload.handler)) {
            this.protyle.upload = new Upload();
        }

        this.init(runtimeCapabilities.lute);
        if (runtimeCapabilities.websocket !== false && !mergedOptions.action.includes(Constants.CB_GET_HISTORY)) {
            this.protyle.ws = new Model({app});
            this.protyle.ws.connect({
                id: this.protyle.id,
                type: "protyle",
                msgCallback: (data) => {
                    switch (data.cmd) {
                        case "reload":
                            if (data.data === this.protyle.block.rootID) {
                                reloadProtyle(this.protyle, false);
                                /// #if !MOBILE
                                getAllModels().outline.forEach(item => {
                                    if (item.blockId === data.data) {
                                        const outlineParam: IObject = {
                                            id: item.blockId,
                                            preview: item.isPreview
                                        };
                                        if (isEncryptedBox(this.protyle.notebookId)) {
                                            outlineParam.notebook = this.protyle.notebookId;
                                        }
                                        fetchPost("/api/outline/getDocOutline", outlineParam, response => {
                                            item.update(response);
                                        });
                                    }
                                });
                                /// #endif
                            }
                            break;
                        case "refreshAttributeView":
                            Array.from(this.protyle.wysiwyg.element.querySelectorAll(`.av[data-av-id="${data.data.id}"]`)).forEach((item: HTMLElement) => {
                                item.removeAttribute("data-render");
                                avRender(item, this.protyle);
                            });
                            if (this.protyle.databaseAttributePanel?.hasDatabase(data.data.id)) {
                                this.protyle.databaseAttributePanel.refresh();
                            }
                            queueDatabaseRowRefresh(this.protyle.id, data.data.id);
                            break;
                        case "addLoading":
                            if (data.data === this.protyle.block.rootID) {
                                addLoading(this.protyle, data.msg);
                            }
                            break;
                        case "unfoldHeading":
                            setFoldById(data.data, this.protyle);
                            break;
                        case "transactions":
                            this.onTransaction(data);
                            break;
                        case "readonly":
                            window.siyuan.config.editor.readOnly = data.data;
                            setReadonlyByConfig(this.protyle, true);
                            break;
                        case "heading2doc":
                        case "li2doc":
                            if (this.protyle.block.rootID === data.data.srcRootBlockID) {
                                if (this.protyle.block.showAll && data.cmd === "heading2doc" && !this.protyle.options.backlinkData) {
                                    const getDocParam: FileTreeGetDocRequestInput = {
                                        id: this.protyle.block.rootID,
                                        size: window.siyuan.config.editor.dynamicLoadBlocks,
                                    };
                                    if (isEncryptedBox(this.protyle.notebookId)) {
                                        getDocParam.notebook = this.protyle.notebookId;
                                    }
                                    fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {
                                        onGet({data: getResponse, protyle: this.protyle});
                                    });
                                } else {
                                    reloadProtyle(this.protyle, false);
                                }
                                /// #if !MOBILE
                                if (data.cmd === "heading2doc") {
                                    // 文档标题互转后,需更新大纲
                                    updatePanelByEditor({
                                        protyle: this.protyle,
                                        focus: false,
                                        pushBackStack: false,
                                        reload: true,
                                        resize: false
                                    });
                                }
                                /// #endif
                            }
                            break;
                        case "renamenotebook":
                            /// #if !MOBILE
                            forSearchByEditor(this, (config, element) => {
                                void refreshSearchPathAfterNotebookRename({
                                    config,
                                    element,
                                    notebookId: data.data.box,
                                    notebookName: data.data.name,
                                }).then((refreshed) => {
                                    if (refreshed) {
                                        persistRefreshedSearchPath(config);
                                    }
                                });
                            });
                            /// #endif
                            break;
                        case "rename":
                            if (this.protyle.path === data.data.path) {
                                if (this.protyle.model) {
                                    this.protyle.model.parent.updateTitle(getDocDisplayName(data.data.title, data.data.empty));
                                }
                                if (this.protyle.background) {
                                    syncDocTitleIAL(this.protyle.background.ial, data.data.title, data.data.empty,
                                        Constants.CUSTOM_SY_TITLE_EMPTY);
                                }
                                if (window.siyuan.config.export.addTitle &&
                                    !this.protyle.preview.element.classList.contains("fn__none")) {
                                    this.protyle.preview.render(this.protyle);
                                }
                            }
                            if (this.protyle.options.render.title && this.protyle.block.parentID === data.data.id) {
                                if (!document.body.classList.contains("body--blur") && getSelection().rangeCount > 0 &&
                                    this.protyle.title.editElement?.contains(getSelection().getRangeAt(0).startContainer)) {
                                    // 标题编辑中的不用更新 https://github.com/siyuan-note/siyuan/issues/6565
                                } else {
                                    this.protyle.title.setTitle(data.data.title, data.data.empty);
                                }
                                if (data.data.empty) {
                                    this.protyle.wysiwyg.element.setAttribute(Constants.CUSTOM_SY_TITLE_EMPTY, "true");
                                } else {
                                    this.protyle.wysiwyg.element.removeAttribute(Constants.CUSTOM_SY_TITLE_EMPTY);
                                }
                            }
                            // update ref
                            this.protyle.wysiwyg.element.querySelectorAll(`[data-type~="block-ref"][data-id="${data.data.id}"]`).forEach(item => {
                                if (item.getAttribute("data-subtype") === "d") {
                                    // 同 updateRef 一样处理 https://github.com/siyuan-note/siyuan/issues/10458
                                    item.innerHTML = sanitizeKernelHTML(data.data.refText);
                                }
                            });
                            /// #if !MOBILE
                            forSearchByEditor(this, (config, element) => {
                                void refreshSearchPathAfterRename({
                                    config,
                                    element,
                                    rename: data.data,
                                }).then((refreshed) => {
                                    if (refreshed) {
                                        persistRefreshedSearchPath(config);
                                    }
                                });
                            });
                            /// #endif
                            break;
                        case "moveDoc":
                            if (this.protyle.path === data.data.fromPath) {
                                this.protyle.path = data.data.newPath;
                                this.protyle.notebookId = data.data.toNotebook;
                                if (this.protyle.notebookId) {
                                    this.protyle.element.setAttribute("data-notebook-id", this.protyle.notebookId);
                                } else {
                                    this.protyle.element.removeAttribute("data-notebook-id");
                                }
                            }
                            /// #if !MOBILE
                            forSearchByEditor(this, (_config, element) => {
                                invalidateSearchPathRequests(element);
                            });
                            /// #endif
                            break;
                        case "closeBox":
                        case "removeBox":
                            if (this.protyle.notebookId === data.data.box) {
                                /// #if MOBILE
                                // 主编辑器由页签管理切换文档,避免切换前显示空白主页
                                if (!removeMobileSecondaryEditor(this) && !window.siyuan.mobile.tabs) {
                                    setEmpty(app);
                                }
                                /// #else
                                if (this.protyle.model) {
                                    this.protyle.model.parent.parent.removeTab(this.protyle.model.parent.id);
                                }
                                /// #endif
                            }
                            break;
                        case "removeDoc":
                            if (data.data.ids.includes(this.protyle.block.rootID)) {
                                /// #if MOBILE
                                // 主编辑器由页签管理切换文档,避免切换前显示空白主页
                                if (!removeMobileSecondaryEditor(this) && !window.siyuan.mobile.tabs) {
                                    setEmpty(app);
                                }
                                /// #else
                                if (this.protyle.model) {
                                    this.protyle.model.parent.parent.removeTab(this.protyle.model.parent.id);
                                }
                                /// #endif
                                delete window.siyuan.storage[Constants.LOCAL_FILEPOSITION][this.protyle.block.rootID];
                                setStorageVal(Constants.LOCAL_FILEPOSITION, window.siyuan.storage[Constants.LOCAL_FILEPOSITION]);
                            }
                            break;
                    }
                }
            });
            if (options.backlinkData) {
                this.protyle.block.rootID = options.blockId;
                renderBacklink(this.protyle, options.backlinkData);
                // 为了满足 eventPath0.style.paddingLeft 从而显示块标 https://github.com/siyuan-note/siyuan/issues/11578
                this.protyle.wysiwyg.element.style.padding = BACKLINK_EDITOR_PADDING;
                return;
            }
            if (!options.blockId) {
                // 搜索页签需提前初始化
                removeLoading(this.protyle);
                if (this.protyle.lite) {
                    resize(this.protyle);
                }
                return;
            }

            const savedScroll = options.scrollAttr?.rootId === options.rootId ? options.scrollAttr :
                window.siyuan.storage?.[Constants.LOCAL_FILEPOSITION]?.[options.rootId];
            if (this.protyle.options.mode !== "preview" &&
                options.rootId && savedScroll &&
                (
                    mergedOptions.action.includes(Constants.CB_GET_SCROLL) ||
                    (mergedOptions.action.includes(Constants.CB_GET_ROOTSCROLL) && options.rootId === options.blockId)
                )
            ) {
                getDocByScroll({
                    protyle: this.protyle,
                    scrollAttr: savedScroll,
                    mergedOptions,
                    cb: () => {
                        this.afterOnGet(mergedOptions);
                    }
                });
            } else {
                this.getDoc(mergedOptions);
            }
        } else {
            this.protyle.contentElement.classList.add("protyle-content--transition");
        }
    }

    private onTransaction(data: IWebSocketData) {
        // 多窗口/多端:用广播附带的撤销状态同步本地镜像
        if (data.context?.undoState) {
            syncMirrorFromBroadcast(data.context.undoState);
            refreshUndoButtons(this.protyle);
        }
        const transactionOperations = getTransactionOperations(data.data);
        queueDatabaseRowRefreshForOperations(this.protyle.id, transactionOperations);
        if (!this.protyle.preview.element.classList.contains("fn__none") &&
            data.context?.rootIDs?.includes(this.protyle.block.rootID)) {
            invalidateTrackedRanges(this.protyle);
            this.protyle.preview.render(this.protyle);
            return;
        }
        const hadContent = this.protyle.wysiwyg.element.childElementCount > 0;
        let needCreateAction = "";
        let hasDeleteOp = false;
        let skippedBacklinkStructure = false;
        const operations: IOperation[] = [];
        transactionOperations.find((item: IOperation) => {
            if (this.protyle.options.backlinkData && ["delete", "move"].includes(item.action)) {
                // 反链上下文只展示源文档的一部分,结构操作等待索引提交后按内容版本增量同步。
                skippedBacklinkStructure = true;
                return true;
            } else {
                if (item.action === "delete") {
                    hasDeleteOp = true;
                }
                operations.push(item);
                // 反链面板移除元素后,文档为空
                if (!(item.action === "delete" && typeof item.data?.createEmptyParagraph === "boolean" && !item.data.createEmptyParagraph)) {
                    needCreateAction = item.action;
                }
            }
        });
        if (operations.length > 0) {
            onTransaction(this.protyle, operations, false);
        } else if (skippedBacklinkStructure) {
            invalidateViewFoldRequests(this.protyle);
            void applyViewFoldStates(this.protyle);
        }
        // 聚焦块被分屏另一侧的删除操作连带删除时(容器块删除会级联删除其所有子孙块,如列表/超级块/引述等),当前页签的聚焦块已成为孤儿但仍显示,需退出聚焦
        // Improve editor state synchronization when deleting blocks https://github.com/siyuan-note/siyuan/issues/17742
        if (this.protyle.block.showAll && hasDeleteOp) {
            fetchPost("/api/block/checkBlockExist", {id: this.protyle.block.id}, response => {
                if (!response.data) {
                    zoomOut({
                        protyle: this.protyle,
                        id: this.protyle.block.rootID
                    });
                }
            });
            return;
        }
        if (this.protyle.element.dataset.loading === "finished" && hadContent &&
            this.protyle.wysiwyg.element.childElementCount === 0 && this.protyle.block.parentID && needCreateAction) {
            if (needCreateAction === "delete" && this.protyle.block.showAll) {
                if (this.protyle.options.handleEmptyContent) {
                    this.protyle.options.handleEmptyContent();
                } else {
                    zoomOut({
                        protyle: this.protyle,
                        id: this.protyle.block.rootID,
                        focusId: this.protyle.block.id
                    });
                }
            } else {
                // 不能使用 transaction,否则分屏后会重复添加
                refreshUndoButtons(this.protyle);
                this.reload(false);
            }
        }
        // undo/redo 重放广播到达后,整批操作已应用,重置 lastHTMLs 防下次本地编辑算错逆操作
        if (data.context?.isUndoReplay === true) {
            this.protyle.wysiwyg.lastHTMLs = {};
        }
    }

    private getDoc(mergedOptions: IProtyleOptions) {
        const getDocParam: FileTreeGetDocRequestInput = {
            id: mergedOptions.blockId,
            includeDocInfo: true,
            isBacklink: mergedOptions.action.includes(Constants.CB_GET_BACKLINK),
            originalRefBlockIDs: mergedOptions.originalRefBlockIDs,
            // 0: 仅当前 ID(默认值),1:向上 2:向下,3:上下都加载,4:加载最后
            mode: (mergedOptions.action && mergedOptions.action.includes(Constants.CB_GET_CONTEXT)) ? 3 : 0,
            size: mergedOptions.action?.includes(Constants.CB_GET_ALL) ? Constants.SIZE_GET_MAX : window.siyuan.config.editor.dynamicLoadBlocks,
        };
        if (isEncryptedBox(this.protyle.notebookId)) {
            getDocParam.notebook = this.protyle.notebookId;
        }
        fetchPost("/api/filetree/getDoc", getDocParam, getResponse => {
            if (this.destroyed) {
                return;
            }
            onGet({
                data: getResponse,
                protyle: this.protyle,
                action: mergedOptions.action,
                scrollPosition: mergedOptions.scrollPosition,
                isValid: () => !this.destroyed,
                afterCB: () => {
                    this.afterOnGet(mergedOptions);
                }
            });
        });
    }

    private afterOnGet(mergedOptions: IProtyleOptions) {
        // 文档加载完成后初始化撤销镜像(低频,不在 selectionchange 热路径)
        if (this.protyle.block?.rootID) {
            initMirror(this.protyle.block.rootID);
        }
        if (this.protyle.model) {
            /// #if !MOBILE
            if (mergedOptions.action?.includes(Constants.CB_GET_FOCUS) || mergedOptions.action?.includes(Constants.CB_GET_OPENNEW)) {
                setPanelFocus(this.protyle.model.element.parentElement.parentElement);
            }
            updatePanelByEditor({
                protyle: this.protyle,
                focus: false,
                pushBackStack: false,
                reload: false,
                resize: false,
                outlineId: mergedOptions.action?.includes(Constants.CB_GET_OUTLINE) ? mergedOptions.blockId : undefined,
            });
            /// #endif
        }
        resize(this.protyle);   // 需等待 fullwidth 获取后设定完毕再重新计算 padding 和元素
        // 需等待 getDoc 完成后再执行,否则在无页签的时候 updatePanelByEditor 会执行2次
        // 只能用 focusin,否则点击表格无法执行
        const activateEditorPanel = () => {
            /// #if !MOBILE
            if (this.protyle && this.protyle.model) {
                let needUpdate = true;
                if (this.protyle.model.element.parentElement.parentElement.classList.contains("layout__wnd--active") && this.protyle.model.headElement.classList.contains("item--focus")) {
                    needUpdate = false;
                }
                if (!needUpdate) {
                    return;
                }
                setPanelFocus(this.protyle.model.element.parentElement.parentElement);
                updatePanelByEditor({
                    protyle: this.protyle,
                    focus: false,
                    pushBackStack: false,
                    reload: false,
                    resize: false,
                });
            } else {
                // 悬浮层应移除其余面板高亮,否则按键会被面板监听到
                document.querySelectorAll(".layout__tab--active").forEach(item => {
                    item.classList.remove("layout__tab--active");
                });
                document.querySelectorAll(".layout__wnd--active").forEach(item => {
                    item.classList.remove("layout__wnd--active");
                });
            }
            /// #endif
        };
        this.protyle.wysiwyg.element.addEventListener("focusin", activateEditorPanel);
        this.protyle.databaseAttributePanel?.element.addEventListener("focusin", activateEditorPanel);
        this.protyle.databaseAttributePanel?.element.addEventListener("pointerdown", activateEditorPanel);
        // 需等渲染完后再回调,用于定位搜索字段 https://github.com/siyuan-note/siyuan/issues/3171
        if (mergedOptions.after) {
            mergedOptions.after(this);
        }
        this.protyle.contentElement.classList.add("protyle-content--transition");
    }

    private init(lute?: Lute) {
        this.protyle.lute = resolveProtyleLute(() => getLute({
            emojiSite: this.protyle.options.hint.emojiPath,
            emojis: this.protyle.options.hint.emoji,
            headingAnchor: false,
            listStyle: this.protyle.options.preview.markdown.listStyle,
            paragraphBeginningSpace: this.protyle.options.preview.markdown.paragraphBeginningSpace,
            sanitize: this.protyle.options.preview.markdown.sanitize,
        }), lute);

        this.protyle.preview = new Preview(this.protyle);

        initUI(this.protyle);
    }

    /** 聚焦到编辑器 */
    public focus() {
        this.protyle.wysiwyg.element.focus();
    }

    /** 上传是否还在进行中 */
    public isUploading() {
        return this.protyle.upload?.isUploading || false;
    }

    /** 清空 undo & redo 栈 */
    public clearStack() {
        this.protyle.undo.clear();
    }

    /** 销毁编辑器 */
    public destroy() {
        this.destroyed = true;
        /// #if MOBILE
        destroyMobileBottomBacklinkPanel(this.protyle);
        unregisterMobileSecondaryEditor(this);
        /// #endif
        destroy(this.protyle);
    }

    public resize() {
        resize(this.protyle);
    }

    public isFullscreen() {
        return this.protyle.element.classList.contains("fullscreen");
    }

    public setFullscreen(enter: boolean) {
        if (setFullscreenState(this.protyle.element, enter)) {
            resize(this.protyle);
        }
    }

    public reload(focus: boolean, updateReadonly?: boolean) {
        reloadProtyle(this.protyle, focus, updateReadonly);
    }

    public insert(html: string, isBlock = false, useProtyleRange = false) {
        insertHTML(html, this.protyle, isBlock, useProtyleRange);
    }

    public async flushPendingTransactions() {
        await this.protyle.wysiwyg.flushPendingInput();
        await waitForPendingTransactions(this.protyle);
    }

    public transaction(doOperations: IOperation[], undoOperations?: IOperation[]) {
        transaction(this.protyle, doOperations, undoOperations);
    }

    /**
     * 多个块转换为一个块
     * @param {TTurnIntoOneSub} [subType] type 为 "BlocksMergeSuperBlock" 时必传
     */
    public turnIntoOneTransaction(selectsElement: Element[], type: TTurnIntoOne, subType?: TTurnIntoOneSub) {
        turnsIntoOneTransaction({
            protyle: this.protyle,
            selectsElement,
            type,
            level: subType
        });
    }

    /**
     * 多个块转换
     * @param {Element} [nodeElement] 优先使用包含 protyle-wysiwyg--select 的块,否则使用 nodeElement 单块
     * @param {number} [subType] type 为 "Blocks2Hs" 时必传
     */
    public turnIntoTransaction(nodeElement: Element, type: TTurnInto, subType?: number) {
        turnsIntoTransaction({
            protyle: this.protyle,
            nodeElement,
            type,
            level: subType,
        });
    }

    public updateTransactionElement(element: Element, oldHTML: string) {
        updateTransaction(this.protyle, element, oldHTML);
    }

    public updateBatchTransaction(nodeElements: Element[], cb: (e: HTMLElement) => void) {
        updateBatchTransaction(nodeElements, this.protyle, cb);
    }

    public getRange(element: Element) {
        return getEditorRange(element);
    }

    public trackRange(range: Range, options: ITrackRangeOptions): ITrackedRangeHandle {
        return trackRange(this.protyle, range, options);
    }

    public resolveTrackedRange(handle: ITrackedRangeHandle): TTrackedRangeResult {
        return resolveTrackedRange(this.protyle, handle);
    }

    public releaseTrackedRange(handle: ITrackedRangeHandle): void {
        releaseTrackedRange(this.protyle, handle);
    }

    public hasClosestBlock(element: Node) {
        return hasClosestBlock(element);
    }

    public focusBlock(element: Element, toStart = true) {
        return focusBlock(element, undefined, toStart);
    }

    public disable() {
        disabledProtyle(this.protyle);
    }

    public enable() {
        enableProtyle(this.protyle);
    }

    public renderAVAttribute(element: HTMLElement, id: string, cb?: (element: HTMLElement) => void) {
        renderAVAttribute(element, id, this.protyle, cb);
    }

    public switchMode(mode: TEditorMode) {
        setEditMode(this.protyle, mode);
    }
}