Skip to content

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

64.3 KB
Raw Blame
import {toggleMenu} from "../menus/menuToggle";
import {showMessage} from "../dialog/message";
import {Dialog} from "../dialog";
import {openInputDialog} from "../dialog/inputDialog";
import {confirmDialog} from "../dialog/confirmDialog";
import {Constants} from "../constants";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {renderAssetsPreview} from "../asset/renderAssets";
import {Protyle} from "../protyle";
import {disabledProtyle, onGet} from "../protyle/util/onGet";
import dayjs from "dayjs";
import {fetchPost, fetchSyncPost} from "../util/fetch";
import {escapeAttr, escapeHtml} from "../util/escape";
import {isMobile} from "../util/functions";
import {showDiff} from "./diff";
import {setStorageVal} from "../protyle/util/compatibility";
import {openModel} from "../mobile/menu/model";
import {closeModel} from "../mobile/util/closePanel";
import type {App} from "../index";
import {resizeSide} from "./resizeSide";
import {isSupportCSSHL, searchMarkRender} from "../protyle/render/searchMarkRender";
import {renderRepoFile, renderRepoFileList, rollbackRepoFile, saveRepoFile} from "./repoFile";
import {openDocHistory} from "./doc";
import {getRepoSnapshotRange, getRepoSnapshotType, initRepoPanel, updateRepoSelection} from "./repoPanel";
import {repoSnapshotInRange} from "./repoRange";
import {MenuItem} from "../menus/Menu";
import {renderSnapshotActions} from "./snapshotActions";

let historyEditor: Protyle;
const repoPanelCleanup = new WeakMap<Element, () => void>();
const repoRequests = new WeakMap<Element, number>();
const repoHistoryEditors = new WeakMap<Element, Protyle>();
const snapshotMemos = new WeakMap<Element, Map<string, string>>();

const destroyRepoPanel = (root: Element) => {
    repoPanelCleanup.get(root)?.();
    root.querySelectorAll("[data-repo-source]").forEach(pane => {
        repoRequests.set(pane, (repoRequests.get(pane) || 0) + 1);
        repoHistoryEditors.get(pane)?.destroy();
        repoHistoryEditors.delete(pane);
    });
};

const openSnapshotMemo = (repoElement: Element, id?: string, memo = "", fullCheck = false) => {
    const dialog = openInputDialog({
        title: id ? window.siyuan.languages.editSnapshotMemo : window.siyuan.languages.snapshotMemo,
        value: memo,
        multiline: true,
        resize: "vertical",
        placeholder: window.siyuan.languages.snapshotMemoTip,
        description: id ? window.siyuan.languages.snapshotMemoLocalTip : fullCheck ? window.siyuan.languages.fullCheckSnapshotTip : undefined,
        onConfirm: async (value, dialog) => {
            const textarea = dialog.element.querySelector("textarea");
            const button = dialog.element.querySelector<HTMLButtonElement>(".b3-button--text");
            if (button.disabled) {
                return;
            }
            button.disabled = true;
            textarea.readOnly = true;
            try {
                if (id) {
                    await fetchPost("/api/repo/setSnapshotMemo", {id, memo: value}, () => {
                        dialog.destroy();
                        renderRepo(repoElement, parseInt(repoElement.getAttribute("data-page")) || 1);
                    });
                } else {
                    await fetchPost("/api/repo/createSnapshot", {memo: value, fullCheck}, (response) => {
                        if (response.data.created || fullCheck) {
                            dialog.destroy();
                        }
                        if (response.data.created) {
                            renderRepo(repoElement, 1);
                        }
                    });
                }
            } finally {
                button.disabled = false;
                textarea.readOnly = false;
            }
        },
    });
    dialog.element.setAttribute("data-key", Constants.DIALOG_SNAPSHOTMEMO);
};

const renderDoc = (element: HTMLElement, currentPage: number) => {
    const previousElement = element.querySelector('[data-type="docprevious"]');
    const nextElement = element.querySelector('[data-type="docnext"]');
    element.setAttribute("data-page", currentPage.toString());
    if (currentPage > 1) {
        previousElement.removeAttribute("disabled");
    } else {
        previousElement.setAttribute("disabled", "disabled");
    }
    const pageBtn = element.querySelector('button[data-type="jumpHistoryPage"]');
    pageBtn.textContent = `${currentPage}`;

    const inputElement = element.querySelector(".b3-text-field") as HTMLInputElement;
    const opElement = element.querySelector('.b3-select[data-type="opselect"]') as HTMLSelectElement;
    const typeElement = element.querySelector('.b3-select[data-type="typeselect"]') as HTMLSelectElement;
    const notebookElement = element.querySelector('.b3-select[data-type="notebookselect"]') as HTMLSelectElement;
    const docElement = element.querySelector('.history__text[data-type="docPanel"]');
    const assetElement = element.querySelector('.history__text[data-type="assetPanel"]');
    const mdElement = element.querySelector('.history__text[data-type="mdPanel"]') as HTMLTextAreaElement;
    const listElement = element.querySelector(".b3-list");
    element.querySelector(".protyle-title__input").classList.add("fn__none");
    assetElement.classList.add("fn__none");
    mdElement.classList.add("fn__none");
    docElement.classList.add("fn__none");
    element.querySelector(".history__panel").classList.add("history__panel--empty");
    if (typeElement.value === "2" || typeElement.value === "4") {
        notebookElement.setAttribute("disabled", "disabled");
        if (window.siyuan.storage[Constants.LOCAL_HISTORY].type !== 2 && window.siyuan.storage[Constants.LOCAL_HISTORY].type !== 4) {
            opElement.value = "all";
        }
        if (typeElement.value === "4") {
            opElement.querySelector('option[value="update"]').classList.add("fn__none");
            opElement.querySelector('option[value="sync"]').classList.add("fn__none");
        } else {
            opElement.querySelector('option[value="update"]').classList.remove("fn__none");
            opElement.querySelector('option[value="sync"]').classList.remove("fn__none");
        }
        opElement.querySelector('option[value="clean"]').classList.remove("fn__none");
        opElement.querySelector('option[value="delete"]').classList.add("fn__none");
        opElement.querySelector('option[value="format"]').classList.add("fn__none");
        opElement.querySelector('option[value="replace"]').classList.add("fn__none");
        opElement.querySelector('option[value="outline"]').classList.add("fn__none");
    } else {
        notebookElement.removeAttribute("disabled");
        if (window.siyuan.storage[Constants.LOCAL_HISTORY].type === 2 || window.siyuan.storage[Constants.LOCAL_HISTORY].type === 4) {
            opElement.value = "all";
        }
        opElement.querySelector('option[value="clean"]').classList.add("fn__none");
        opElement.querySelector('option[value="update"]').classList.remove("fn__none");
        opElement.querySelector('option[value="delete"]').classList.remove("fn__none");
        opElement.querySelector('option[value="format"]').classList.remove("fn__none");
        opElement.querySelector('option[value="sync"]').classList.remove("fn__none");
        opElement.querySelector('option[value="replace"]').classList.remove("fn__none");
        opElement.querySelector('option[value="outline"]').classList.remove("fn__none");
    }
    window.siyuan.storage[Constants.LOCAL_HISTORY].notebookId = notebookElement.value;
    window.siyuan.storage[Constants.LOCAL_HISTORY].type = parseInt(typeElement.value);
    window.siyuan.storage[Constants.LOCAL_HISTORY].operation = opElement.value;
    setStorageVal(Constants.LOCAL_HISTORY, window.siyuan.storage[Constants.LOCAL_HISTORY]);
    fetchPost("/api/history/searchHistory", {
        notebook: notebookElement.value,
        query: inputElement.value,
        page: currentPage,
        op: opElement.value,
        type: parseInt(typeElement.value)
    }, (response) => {
        if (currentPage < response.data.pageCount) {
            nextElement.removeAttribute("disabled");
        } else {
            nextElement.setAttribute("disabled", "disabled");
        }
        pageBtn.setAttribute("data-totalpage", (response.data.pageCount || 1).toString());
        const pageElement = nextElement.nextElementSibling.nextElementSibling;
        pageElement.textContent = `${window.siyuan.languages.pageCountAndHistoryCount.replace("${x}", response.data.pageCount).replace("${y}", response.data.totalCount || 0)}`;
        pageElement.classList.remove("fn__none");
        const histories = response.data.histories || [];
        if (histories.length === 0) {
            listElement.innerHTML = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
            return;
        }
        let logsHTML = "";
        histories.forEach((item: string) => {
            logsHTML += `<li class="b3-list-item" data-type="toggle" data-created="${item}">
    <span class="b3-list-item__toggle b3-list-item__toggle--hl"><svg class="b3-list-item__arrow"><use xlink:href="#iconRight"></use></svg></span>
    <span style="padding-left: 4px" class="b3-list-item__text">${dayjs(parseInt(item) * 1000).format("YYYY-MM-DD HH:mm:ss")}</span>
</li>`;
        });
        listElement.innerHTML = logsHTML;
    });
};

const renderRepoItem = (response: IWebSocketData, element: Element, type: string) => {
    if (response.data.snapshots.length === 0) {
        element.querySelector('[data-type="repoList"]').innerHTML = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
        updateRepoSelection(element);
        return;
    }
    const actionHTML = renderSnapshotActions(type, window.siyuan.languages, isMobile(), window.siyuan.config.readonly);
    const memos = new Map<string, string>();
    snapshotMemos.set(element, memos);
    let repoHTML = "";
    const isPhone = isMobile();
    const selectId: { id: string, time: string }[] = ["getRepoTagSnapshots", "getRepoSnapshots"].includes(type) ?
        JSON.parse(element.querySelector(".b3-button[data-type='compare']").getAttribute("data-ids") || "[]") : [];
    response.data.snapshots.forEach((item: {
        memo: string,
        id: string,
        hCreated: string,
        count: number,
        hSize: string,
        systemID: string,
        systemName: string,
        systemOS: string,
        tag: string,
        tags?: string[],
        requiresDownload?: boolean,
        typesCount: { type: string, count: number }[]
    }) => {
        memos.set(item.id, item.memo);
        let statHTML = "";
        if (item.typesCount) {
            statHTML = `<div class="b3-list-item__meta${isPhone ? " fn__none" : ""}">
${window.siyuan.languages.fileCount} ${item.count}<span class="fn__space"></span>`;
            item.typesCount.forEach(subItem => {
                statHTML += `${subItem.type} ${subItem.count}<span class="fn__space"></span>`;
            });
            statHTML += "</div>";
        }
        const tags = type === "getRepoSnapshots" ? item.tags || [] : (item.tag ? [item.tag] : []);
        const infoHTML = `<div${isPhone ? ' style="padding-top:8px"' : ""}>
    <span data-type="hCreated">${item.hCreated}</span>
    <span class="fn__space"></span>
    ${item.hSize}
    <span class="fn__space"></span>
    ${item.systemOS}${(item.systemName && item.systemOS) ? "/" : ""}${item.systemName}
    <span class="fn__space"></span>
    ${tags.map(tag => `<span class="b3-chip b3-chip--secondary b3-chip--small">${escapeHtml(tag)}</span>`).join(" ")}
</div>
${item.requiresDownload && ["getRepoTagSnapshots", "getRepoSnapshots"].includes(type) ?
    `<div class="ft__smaller ft__error" style="white-space:normal">${escapeHtml(window.siyuan.languages.syncAssetSnapshotIncomplete)}</div>` : ""}
<div class="b3-list-item__meta${isPhone ? " fn__none" : ""}">
    ${escapeHtml(item.memo)}
    <span class="fn__space"></span>
    <code class="fn__code">${item.id.substring(0, 7)}</code>
</div>
${statHTML}`;
        const hasSelected = selectId.find(subItem => subItem.id === item.id);
        /// #if MOBILE
        repoHTML += `<li class="b3-list-item${hasSelected ? " b3-list-item--focus" : ""}" data-type="repoitem" data-id="${item.id}" data-tag="${escapeAttr(escapeHtml(item.tag || ""))}">
<div class="fn__flex-1">
    ${infoHTML}
    <div class="fn__flex history__repo-actions" data-type="repoitem" data-id="${item.id}" data-tag="${escapeAttr(escapeHtml(item.tag || ""))}">
        ${actionHTML}
        <span class="b3-list-item__action" data-type="more">
            <svg><use xlink:href="#iconMore"></use></svg>
            <span class="fn__space"></span>
            ${window.siyuan.languages.more}
        </span>
        <span class="fn__flex-1"></span>
    </div>
</div>
</li>`;
        /// #else
        repoHTML += `<li class="b3-list-item b3-list-item--hide-action${hasSelected ? " b3-list-item--focus" : ""}" data-type="repoitem" data-id="${item.id}" data-tag="${escapeAttr(escapeHtml(item.tag || ""))}">
<div class="fn__flex-1">${infoHTML}</div>
${actionHTML}
</li>`;
        /// #endif
    });
    element.querySelector('[data-type="repoList"]').innerHTML = `${repoHTML}`;
    updateRepoSelection(element);
};

const clearRepoPreview = (element: Element) => {
    const previewElement = element.querySelector('[data-type="repoPreviewPanel"]');
    repoHistoryEditors.get(element)?.destroy();
    repoHistoryEditors.delete(element);
    element.querySelector('[data-type="repoPreview"] .protyle-title__input').classList.add("fn__none");
    previewElement.classList.add("fn__none");
    previewElement.removeAttribute("data-request-id");
    previewElement.innerHTML = "";
};

const setRepoSearchLayout = (element: Element, enabled: boolean) => {
    const listElement = element.querySelector('[data-type="repoList"]') as HTMLElement;
    const resizeElement = element.querySelector(".history__resize");
    const previewElement = element.querySelector('[data-type="repoPreview"]');
    clearRepoPreview(element);
    if (element.getAttribute("data-repo-source") === "local") {
        element.closest(".history__snapshots")?.classList.toggle("history__snapshots--search", enabled);
    }
    if (enabled) {
        listElement.classList.remove("fn__flex-1");
        listElement.classList.add("history__side");
        if (!isMobile()) {
            listElement.style.width = window.siyuan.storage[Constants.LOCAL_HISTORY].sideWidth;
        }
        resizeElement.classList.remove("fn__none");
        previewElement.classList.remove("fn__none");
    } else {
        listElement.classList.add("fn__flex-1");
        listElement.classList.remove("history__side");
        listElement.style.width = "";
        resizeElement.classList.add("fn__none");
        previewElement.classList.add("fn__none");
    }
};

const renderRepoSearchResult = (response: IWebSocketData, element: Element) => {
    renderRepoFileList(response.data.files, element.querySelector('[data-type="repoList"]'), true);
};

const renderRepo = async (element: Element, currentPage: number) => {
    if (element.classList.contains("history__snapshots")) {
        element.setAttribute("data-init", "true");
        await Promise.all(Array.from(element.querySelectorAll("[data-repo-source]")).map(pane => renderRepo(pane, currentPage)));
        return;
    }
    const request = (repoRequests.get(element) || 0) + 1;
    repoRequests.set(element, request);
    const selectValue = getRepoSnapshotType(element);
    const range = getRepoSnapshotRange(element);
    const hasRange = !!(range.startTime || range.endTime);
    const searchInputElement = element.querySelector<HTMLInputElement>(".b3-text-field");
    const keyword = hasRange ? "" : searchInputElement.value.trim();
    const searchSnapshot = selectValue === "getRepoSnapshots" && /^[0-9a-f]{7,40}$/i.test(keyword);
    let searching = Boolean(keyword && selectValue === "getRepoSnapshots" && !searchSnapshot);
    const tagged = selectValue === "getRepoTagSnapshots" || selectValue === "getCloudRepoTagSnapshots";
    const listElement = element.querySelector('[data-type="repoList"]');
    const pageBtn = element.querySelector('button[data-type="jumpRepoPage"]');
    const previousElement = element.querySelector('[data-type="previous"]');
    const nextElement = element.querySelector('[data-type="next"]');
    const pageElement = nextElement.nextElementSibling.nextElementSibling;
    const searchButton = searchInputElement.nextElementSibling as HTMLButtonElement;
    listElement.innerHTML = '<li class="history__snapshot-loading"><div class="fn__loading"><span class="b3-loading" aria-hidden="true"></span></div></li>';
    updateRepoSelection(element);
    element.setAttribute("data-page", String(currentPage));
    pageBtn.textContent = String(currentPage);
    searchInputElement.parentElement.classList.toggle("fn__none", selectValue !== "getRepoSnapshots" || hasRange);
    searchButton.disabled = true;
    [previousElement, nextElement, pageBtn].forEach(button => {
        button.classList.toggle("fn__none", tagged);
        button.setAttribute("disabled", "disabled");
    });
    if (tagged) {
        pageElement.classList.add("fn__none");
    }
    let response: IWebSocketData;
    try {
        if (searchSnapshot) {
            response = await fetchSyncPost("/api/repo/getRepoSnapshots", {id: keyword, page: 1}, undefined, false);
            if (repoRequests.get(element) !== request || !element.isConnected) {
                return;
            }
            searching = response.code === 0 && response.data.totalCount === 0;
            if (searching) {
                response = await fetchSyncPost("/api/repo/searchRepoFile", {keyword, page: currentPage}, undefined, false);
            }
        } else if (searching) {
            response = await fetchSyncPost("/api/repo/searchRepoFile", {keyword, page: currentPage}, undefined, false);
        } else if (tagged) {
            response = await fetchSyncPost(`/api/repo/${selectValue}`, {}, undefined, false);
        } else {
            response = await fetchSyncPost(`/api/repo/${selectValue}`, {page: currentPage, ...range}, undefined, false);
        }
        if (repoRequests.get(element) !== request || !element.isConnected) {
            return;
        }
        if (response.code !== 0) {
            throw new Error(response.msg);
        }
        if (tagged && hasRange) {
            response.data.snapshots = response.data.snapshots.filter((snapshot: {created: number}) => repoSnapshotInRange(snapshot.created, range));
        }
        setRepoSearchLayout(element, searching);
        if (searching) {
            renderRepoSearchResult(response, element);
        } else {
            renderRepoItem(response, element, selectValue);
        }
        if (!tagged) {
            if (currentPage > 1) {
                previousElement.removeAttribute("disabled");
            }
            if (currentPage < response.data.pageCount) {
                nextElement.removeAttribute("disabled");
            }
            pageBtn.removeAttribute("disabled");
            pageBtn.setAttribute("data-totalpage", String(response.data.pageCount || 1));
            pageElement.textContent = window.siyuan.languages.pageCountAndSnapshotCount.replace("${x}", response.data.pageCount || 1).replace("${y}", response.data.totalCount || 0);
            pageElement.classList.remove("fn__none");
        }
    } catch (error) {
        if (repoRequests.get(element) === request && element.isConnected) {
            listElement.innerHTML = `<li class="b3-list--empty">${escapeHtml(String(error))}<div class="fn__hr"></div><button class="b3-button b3-button--outline" data-type="retryRepo">${window.siyuan.languages.retry}</button></li>`;
            updateRepoSelection(element);
        }
    } finally {
        if (repoRequests.get(element) === request) {
            searchButton.disabled = false;
        }
    }
};

const renderRmNotebook = (element: HTMLElement) => {
    element.setAttribute("data-init", "true");
    fetchPost("/api/history/getNotebookHistory", {}, (response) => {
        if (response.data.histories.length === 0) {
            element.innerHTML = `<li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>`;
            return;
        }
        let logsHTML = "";
        response.data.histories.forEach((item: {
            items: { path: string, title: string }[],
            hCreated: string
        }, index: number) => {
            logsHTML += `<li class="b3-list-item" style="padding-left: 0" data-type="rmtoggle">
    <span style="padding-left: 8px" class="b3-list-item__toggle"><svg class="b3-list-item__arrow${index === 0 ? " b3-list-item__arrow--open" : ""}${item.items.length > 0 ? "" : " fn__hidden"}"><use xlink:href="#iconRight"></use></svg></span>
    <span class="b3-list-item__text">${item.hCreated}</span>
</li>`;
            if (item.items.length > 0) {
                logsHTML += `<ul class="${index === 0 ? "" : "fn__none"}">`;
                item.items.forEach((docItem) => {
                    logsHTML += `<li data-type="notebook" data-path="${docItem.path}" class="b3-list-item" style="padding-left: 32px">
    <span class="b3-list-item__text">${escapeHtml(docItem.title)}</span>
    <span class="fn__space"></span>
    <button type="button" class="b3-list-item__action b3-tooltips b3-tooltips__w" data-type="rollback" aria-label="${window.siyuan.languages.rollback}">
        <svg><use xlink:href="#iconUndo"></use></svg>
    </button>
</li>`;
                });
                logsHTML += "</ul>";
            }
        });
        element.innerHTML = logsHTML;
    });
};

export const openHistory = (app: App, tab: "doc" | "notebook" | "repo" = "doc", openOnly = false) => {
    if (window.siyuan.config.readonly) {
        return;
    }
    const exitDialog = window.siyuan.dialogs.find((item) => {
        if (item.element.querySelector("#historyContainer")) {
            if (!openOnly) {
                item.destroy();
            }
            return true;
        }
    });
    if (exitDialog) {
        return;
    }

    const localHistory = window.siyuan.storage[Constants.LOCAL_HISTORY];
    let notebookSelectHTML = `<option value='%' ${localHistory.notebookId === "%" ? "selected" : ""}>${window.siyuan.languages.allNotebooks}</option>`;
    window.siyuan.notebooks.forEach((item) => {
        if (!item.closed) {
            notebookSelectHTML += ` <option value="${item.id}"${item.id === localHistory.notebookId ? " selected" : ""}>${escapeHtml(item.name)}</option>`;
        }
    });

    const contentHTML = `<div class="fn__flex-column" style="height: 100%;">
    <div class="layout-tab-bar fn__flex" ${isMobile() ? "" : 'style="border-radius: var(--b3-border-radius-b) var(--b3-border-radius-b) 0 0"'}>
        <div data-type="doc" class="item item--full item--focus"><span class="fn__flex-1"></span><span class="item__text">${window.siyuan.languages.fileHistory}</span><span class="fn__flex-1"></span></div>
        <div data-type="notebook" style="min-width: 160px" class="item item--full"><span class="fn__flex-1"></span><span class="item__text">${window.siyuan.languages.removedNotebook}</span><span class="fn__flex-1"></span></div>
        <div data-type="repo" class="item item--full"><span class="fn__flex-1"></span><span class="item__text">${window.siyuan.languages.dataSnapshot}</span><span class="fn__flex-1"></span></div>
    </div>
    <div class="fn__flex-1 fn__flex" id="historyContainer">
        <div data-type="doc" class="history__repo fn__block" data-init="true">
            <div class="history__action">
                <div class="block__icons${isMobile() ? " fn__flex-wrap" : ""}">
                    <span data-type="docprevious" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.previousLabel}"><svg><use xlink:href='#iconLeft'></use></svg></span>
                    <button class="b3-button b3-button--text ft__selectnone" data-type="jumpHistoryPage" data-totalpage="1">1</button>
                    <span data-type="docnext" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.nextLabel}"><svg><use xlink:href='#iconRight'></use></svg></span>
                    <span class="fn__space"></span>
                    <span class="ft__on-surface fn__flex-shrink ft__selectnone fn__none">${window.siyuan.languages.pageCountAndHistoryCount}</span>
                    <span class="fn__space"></span>
                    <div class="fn__flex-1"></div>
                    <div class="b3-form__icon">
                        <svg class="b3-form__icon-icon"><use xlink:href="#iconSearch"></use></svg>
                        <input spellcheck="false" class="b3-text-field b3-form__icon-input ${isMobile() ? "fn__size96" : "fn__size200"}" placeholder="${window.siyuan.languages.searchPlaceholder}">
                    </div>
                    <span class="fn__space"></span>
                    <select data-type="typeselect" class="b3-select ${isMobile() ? "fn__flex-shrink" : "fn__size200"}">
                        <option value="0" ${localHistory.type === 0 ? "selected" : ""}>${window.siyuan.languages.docName}</option>
                        <option value="1" ${localHistory.type === 1 ? "selected" : ""}>${window.siyuan.languages.docNameAndContent}</option>
                        <option value="2" ${localHistory.type === 2 ? "selected" : ""}>${window.siyuan.languages.assets}</option>
                        <option value="4" ${localHistory.type === 4 ? "selected" : ""}>${window.siyuan.languages.database}</option>
                    </select>
                    <span class="fn__space"></span>
                    <select data-type="opselect" class="b3-select${isMobile() ? " fn__flex-shrink" : ""}">
                        <option value="all" ${localHistory.operation === "all" ? "selected" : ""}>${window.siyuan.languages.allOp}</option>
                        <option value="clean" ${localHistory.operation === "clean" ? "selected" : ""}>${window.siyuan.languages.historyClean}</option>
                        <option value="update" ${localHistory.operation === "update" ? "selected" : ""}>${window.siyuan.languages.historyUpdate}</option>
                        <option value="delete" ${localHistory.operation === "delete" ? "selected" : ""}>${window.siyuan.languages.historyDelete}</option>
                        <option value="format" ${localHistory.operation === "format" ? "selected" : ""}>${window.siyuan.languages.historyFormat}</option>
                        <option value="sync" ${localHistory.operation === "sync" ? "selected" : ""}>${window.siyuan.languages.historySync}</option>
                        <option value="replace" ${localHistory.operation === "replace" ? "selected" : ""}>${window.siyuan.languages.historyReplace}</option>
                        <option value="outline" ${localHistory.operation === "outline" ? "selected" : ""}>${window.siyuan.languages.historyOutline}</option>
                    </select>
                    <span class="fn__space"></span>
                    <select data-type="notebookselect" class="b3-select ${isMobile() ? "fn__size96" : "fn__size200"}">
                        ${notebookSelectHTML}
                    </select>
                    <span class="fn__space"></span>
                    <button data-type="rebuildIndex" class="b3-button b3-button--outline">${window.siyuan.languages.rebuildHistoryIndex}</button>
                </div>
            </div>
            <div class="fn__flex fn__flex-1 history__panel">
                <ul class="b3-list b3-list--background history__side" ${isMobile() ? "" : `style="width: ${localHistory.sideWidth}"`}>
                    <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
                </ul>
                <div class="history__resize"></div>
                <div class="fn__flex-column fn__flex-1">
                    <div class="protyle-title__input ft__center ft__breakword fn__none"></div>
                    <div class="fn__flex-1 history__text fn__none" data-type="assetPanel"></div>
                    <textarea class="fn__flex-1 history__text fn__none" data-type="mdPanel"></textarea>
                    <div class="fn__flex-1 history__text fn__none" style="padding: 0" data-type="docPanel"></div>
                </div>
            </div>
        </div>
        <ul data-type="notebook" style="padding: 8px 0;" class="fn__none b3-list b3-list--background">
            <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
        </ul>
        <div data-type="repo" class="fn__none history__repo">
            <div class="history__action">
                <div class="block__icons history__repo-actions">
                    <div class="history__repo-pagination">
                    <span data-type="previous" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.previousLabel}"><svg><use xlink:href='#iconLeft'></use></svg></span>
                    <button class="b3-button b3-button--text ft__selectnone" data-type="jumpRepoPage" data-totalpage="1">1</button>
                    <span data-type="next" class="block__icon block__icon--show b3-tooltips b3-tooltips__e" disabled="disabled" aria-label="${window.siyuan.languages.nextLabel}"><svg><use xlink:href='#iconRight'></use></svg></span>
                    <span class="fn__space"></span>
                    <span class="ft__on-surface fn__flex-shrink ft__selectnone fn__none">${window.siyuan.languages.pageCountAndSnapshotCount}</span>
                    </div>
                    <div class="b3-form__icon history__repo-search fn__none">
                       <svg class="b3-form__icon-icon"><use xlink:href="#iconSearch"></use></svg>
                       <input class="b3-text-field b3-form__icon-input" style="padding-right: 44px;" spellcheck="false" placeholder="${window.siyuan.languages.searchFileOrSnapshot}">
                       <button class="b3-button b3-button--text" style="position: absolute;right: 0;top: 0;">${window.siyuan.languages.search}</button>
                    </div>
                    <button class="b3-button b3-button--outline" disabled data-type="compare">${window.siyuan.languages.compare}</button>
                    <button class="b3-button b3-button--outline" data-type="genRepo">
                        <svg><use xlink:href="#iconAdd"></use></svg>${window.siyuan.languages.createSnapshot}
                    </button>
                    <button class="b3-button b3-button--outline" data-type="fullCheckRepo">${window.siyuan.languages.fullCheckSnapshot}</button>
                </div>
            </div>
            <div class="fn__flex fn__flex-1 history__panel">
                <ul data-type="repoList" class="b3-list b3-list--background fn__flex-1" style="padding: 8px 0">
                    <li class="b3-list--empty">${window.siyuan.languages.emptyContent}</li>
                </ul>
                <div class="history__resize fn__none"></div>
                <div data-type="repoPreview" class="fn__flex-1 fn__flex-column fn__none">
                    <div class="protyle-title__input fn__none ft__center ft__breakword"></div>
                    <div class="fn__flex-1 history__text fn__none" style="padding: 0" data-type="repoPreviewPanel"></div>
                </div>
            </div>
        </div>
    </div>
</div>`;

    if (isMobile()) {
        let mobileRepoRoot: Element;
        openModel({
            html: contentHTML,
            icon: "iconHistory",
            title: window.siyuan.languages.dataHistory,
            bindEvent(element) {
                element.firstElementChild.setAttribute("style", "background-color:var(--b3-theme-background);height:100%");
                bindEvent(app, element.firstElementChild);
                mobileRepoRoot = element.querySelector(".history__snapshots");
                if (tab !== "doc") {
                    element.firstElementChild.querySelector(`.layout-tab-bar [data-type="${tab}"]`)?.dispatchEvent(new MouseEvent("click", {bubbles: true}));
                }
            },
            destroyCallback() {
                if (mobileRepoRoot) {
                    destroyRepoPanel(mobileRepoRoot);
                }
            },
        });
    } else {
        const dialog = new Dialog({
            content: contentHTML,
            width: "90vw",
            height: "80vh",
            containerClassName: "b3-dialog__container--theme",
            destroyCallback() {
                historyEditor = undefined;
                const repoElement = dialog.element.querySelector('#historyContainer [data-type="repo"]');
                destroyRepoPanel(repoElement);
            }
        });
        dialog.element.setAttribute("data-key", Constants.DIALOG_HISTORY);
        bindEvent(app, dialog.element, dialog);
        if (tab !== "doc") {
            dialog.element.querySelector(`.layout-tab-bar [data-type="${tab}"]`)?.dispatchEvent(new MouseEvent("click", {bubbles: true}));
        } else {
            dialog.element.querySelector("input").focus();
        }
        const docPanelElement = dialog.element.querySelector('#historyContainer [data-type="doc"]');
        const repoPanelElement = dialog.element.querySelector('#historyContainer [data-type="repo"]');
        resizeSide(docPanelElement.querySelector(".history__resize"), docPanelElement.querySelector(".history__side"), "sideWidth");
        resizeSide(repoPanelElement.querySelector(".history__resize"), repoPanelElement.querySelector('[data-type="repoList"]'), "sideWidth");
    }
};

const bindEvent = (app: App, element: Element, dialog?: Dialog) => {
    const firstPanelElement = element.querySelector("#historyContainer [data-type=doc]") as HTMLElement;
    firstPanelElement.querySelectorAll(".b3-select").forEach((itemElement) => {
        itemElement.addEventListener("change", () => {
            renderDoc(firstPanelElement, 1);
        });
    });
    firstPanelElement.querySelector(".b3-text-field").addEventListener("input", (event: KeyboardEvent) => {
        if (!event.isComposing) {
            renderDoc(firstPanelElement, 1);
        }
    });
    firstPanelElement.querySelector(".b3-text-field").addEventListener("compositionend", () => {
        renderDoc(firstPanelElement, 1);
    });
    const docElement = firstPanelElement.querySelector('.history__text[data-type="docPanel"]') as HTMLElement;
    const assetElement = firstPanelElement.querySelector('.history__text[data-type="assetPanel"]');
    const mdElement = firstPanelElement.querySelector('.history__text[data-type="mdPanel"]') as HTMLTextAreaElement;
    const titleElement = firstPanelElement.querySelector(".protyle-title__input") as HTMLElement;
    renderDoc(firstPanelElement, 1);
    historyEditor = new Protyle(app, docElement, {
        blockId: "",
        history: {
            created: ""
        },
        action: [Constants.CB_GET_HISTORY],
        render: {
            background: false,
            gutter: false,
            breadcrumb: false,
            breadcrumbDocName: false,
        },
        typewriterMode: false,
    });
    disabledProtyle(historyEditor.protyle);
    const repoRoot = element.querySelector<HTMLElement>('#historyContainer [data-type="repo"]');
    repoPanelCleanup.set(repoRoot, initRepoPanel(repoRoot, renderRepo));
    const repoElement = repoRoot.querySelector('[data-repo-source="local"]');
    const historyElement = element.querySelector('#historyContainer [data-type="doc"]');
    const previewRepoFile = (itemElement: Element) => {
        const previewElement = repoElement.querySelector('[data-type="repoPreviewPanel"]');
        const previewTitleElement = repoElement.querySelector('[data-type="repoPreview"] .protyle-title__input');
        repoHistoryEditors.get(repoElement)?.destroy();
        repoHistoryEditors.delete(repoElement);
        previewTitleElement.textContent = itemElement.querySelector(".b3-list-item__text").textContent.trim();
        previewTitleElement.classList.remove("fn__none");
        previewElement.classList.remove("fn__none");
        renderRepoFile(app, itemElement, previewElement, (editor) => {
            repoHistoryEditors.set(repoElement, editor);
        });
        itemElement.parentElement.querySelector(".b3-list-item--focus")?.classList.remove("b3-list-item--focus");
        itemElement.classList.add("b3-list-item--focus");
    };
    const searchFileElement = repoElement.querySelector<HTMLInputElement>(".b3-text-field");
    searchFileElement.nextElementSibling.addEventListener("click", () => {
        renderRepo(repoElement, 1);
    });
    searchFileElement.addEventListener("keydown", (event: KeyboardEvent) => {
        if (event.key === "Enter" && !event.isComposing) {
            event.preventDefault();
            renderRepo(repoElement, 1);
        }
    });
    element.addEventListener("click", (event) => {
        let target = event.target as HTMLElement;
        const repoElement = target.closest("[data-repo-source]") || repoRoot.querySelector('[data-repo-source="local"]');
        while (target && !target.isEqualNode(element)) {
            const type = target.getAttribute("data-type");
            if (target.classList.contains("item")) {
                target.parentElement.querySelector(".item--focus").classList.remove("item--focus");
                Array.from(element.querySelector("#historyContainer").children).forEach((item: HTMLElement) => {
                    if (item.getAttribute("data-type") === type) {
                        item.classList.remove("fn__none");
                        item.classList.add("fn__block");
                        target.classList.add("item--focus");
                        if (item.getAttribute("data-init") !== "true") {
                            if (type === "notebook") {
                                renderRmNotebook(item);
                            } else if (type === "repo") {
                                renderRepo(item, 1);
                            }
                        }
                    } else {
                        item.classList.add("fn__none");
                        item.classList.remove("fn__block");
                    }
                });
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.classList.contains("b3-list-item__action") && type === "compare") {
                const itemElement = target.closest(".b3-list-item");
                openDocHistory({
                    app,
                    id: itemElement.getAttribute("data-id"),
                    notebookId: itemElement.getAttribute("data-notebook-id"),
                    pathString: itemElement.querySelector(".b3-list-item__text").textContent.trim(),
                });
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.classList.contains("b3-list-item__action") && type === "rollback" && !window.siyuan.config.readonly) {
                const liElement = target.closest(".b3-list-item");
                const dataType = target.parentElement.getAttribute("data-type") || liElement.getAttribute("data-type");
                if (dataType === "searchFileItem") {
                    rollbackRepoFile(liElement);
                    event.stopPropagation();
                    event.preventDefault();
                    break;
                }
                let name: string;
                let time;
                if (dataType === "notebook") {
                    name = target.previousElementSibling.previousElementSibling.textContent.trim();
                    time = target.parentElement.parentElement.previousElementSibling.textContent.trim();
                } else if (dataType === "repoitem") {
                    name = window.siyuan.languages.workspaceData;
                    time = (isMobile() ? target.parentElement.parentElement : target.parentElement).querySelector("span[data-type='hCreated']").textContent.trim();
                } else {
                    name = liElement.querySelector(".b3-list-item__text").textContent.trim();
                    time = dayjs(parseInt(liElement.getAttribute("data-created")) * 1000).format("YYYY-MM-DD HH:mm:ss");
                }
                confirmDialog("⚠️ " + window.siyuan.languages.rollback,
                    window.siyuan.languages.rollbackConfirm.replace("${name}", () => escapeHtml(name)).replace("${time}", time),
                    () => {
                        if (dataType === "assets") {
                            fetchPost("/api/history/rollbackAssetsHistory", {
                                historyPath: target.parentElement.getAttribute("data-path")
                            });
                        } else if (dataType === "doc") {
                            fetchPost("/api/history/rollbackDocHistory", {
                                historyPath: target.parentElement.getAttribute("data-path")
                            });
                        } else if (dataType === "av") {
                            fetchPost("/api/history/rollbackAttributeViewHistory", {
                                historyPath: target.parentElement.getAttribute("data-path")
                            });
                        } else if (dataType === "notebook") {
                            fetchPost("/api/history/rollbackNotebookHistory", {
                                historyPath: target.parentElement.getAttribute("data-path")
                            });
                        } else {
                            fetchPost("/api/repo/checkoutRepo", {
                                id: target.parentElement.getAttribute("data-id")
                            });
                        }
                    });
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "saveAs") {
                saveRepoFile(target.closest(".b3-list-item"));
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.classList.contains("b3-list-item") &&
                target.getAttribute("data-type") === "searchFileItem") {
                previewRepoFile(target);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "more") {
                toggleMenu({
                    target: target,
                    build: (_menu, session) => {
                        const row = target.closest<HTMLElement>('li[data-type="repoitem"]');
                        const menu = window.siyuan.menus.menu;

                        row.querySelectorAll<HTMLElement>(".history__snapshot-menu-action").forEach(action => {
                            menu.append(new MenuItem({
                                icon: action.querySelector("use").getAttribute("xlink:href").substring(1),
                                label: action.getAttribute("aria-label") || action.textContent.trim(),
                                click: () => {
                                    menu.remove();
                                    action.click();
                                },
                            }).element);
                        });
                        menu.append(new MenuItem({
                            icon: "iconInfo",
                            label: window.siyuan.languages.info,
                            click: () => row.querySelectorAll(".b3-list-item__meta").forEach(item => item.classList.toggle("fn__none")),
                        }).element);
                        const rect = target.getBoundingClientRect();
                        session.show(() => menu.popup({x: rect.left, y: rect.bottom, h: rect.height}));

                    },
                });
            event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "toggle") {
                const iconElement = target.firstElementChild.firstElementChild;
                if (iconElement.classList.contains("b3-list-item__arrow--open")) {
                    target.nextElementSibling.classList.add("fn__none");
                    iconElement.classList.remove("b3-list-item__arrow--open");
                } else {
                    if (target.nextElementSibling && target.nextElementSibling.tagName === "UL") {
                        target.nextElementSibling.classList.remove("fn__none");
                        iconElement.classList.add("b3-list-item__arrow--open");
                    } else {
                        const inputElement = firstPanelElement.querySelector(".b3-text-field") as HTMLInputElement;
                        const opElement = firstPanelElement.querySelector('.b3-select[data-type="opselect"]') as HTMLSelectElement;
                        const typeElement = firstPanelElement.querySelector('.b3-select[data-type="typeselect"]') as HTMLSelectElement;
                        const notebookElement = firstPanelElement.querySelector('.b3-select[data-type="notebookselect"]') as HTMLSelectElement;
                        const created = target.getAttribute("data-created");
                        fetchPost("/api/history/getHistoryItems", {
                            notebook: notebookElement.value,
                            query: inputElement.value,
                            op: opElement.value,
                            type: parseInt(typeElement.value),
                            created
                        }, (response) => {
                            iconElement.classList.add("b3-list-item__arrow--open");
                            let html = "";
                            let ariaLabel = "";
                            response.data.items.forEach((docItem: {
                                id: string,
                                title: string,
                                path: string,
                                op: string,
                                notebook: string
                            }) => {
                                let chipClass = " b3-chip b3-chip--list ";
                                if (docItem.op === "clean") {
                                    chipClass += "b3-chip--primary ";
                                    ariaLabel = window.siyuan.languages.historyClean;
                                } else if (docItem.op === "update") {
                                    chipClass += "b3-chip--info ";
                                    ariaLabel = window.siyuan.languages.historyUpdate;
                                } else if (docItem.op === "delete") {
                                    chipClass += "b3-chip--error ";
                                    ariaLabel = window.siyuan.languages.historyDelete;
                                } else if (docItem.op === "format") {
                                    chipClass += "b3-chip--pink ";
                                    ariaLabel = window.siyuan.languages.historyFormat;
                                } else if (docItem.op === "sync") {
                                    chipClass += "b3-chip--success ";
                                    ariaLabel = window.siyuan.languages.historySync;
                                } else if (docItem.op === "replace") {
                                    chipClass += "b3-chip--secondary ";
                                    ariaLabel = window.siyuan.languages.historyReplace;
                                } else if (docItem.op === "outline") {
                                    chipClass += "b3-chip--warning ";
                                    ariaLabel = window.siyuan.languages.historyOutline;
                                }
                                const itemType = typeElement.value === "4" ? "av" : (typeElement.value === "2" ? "assets" : "doc");
                                const compareHTML = itemType === "doc" && docItem.op !== "delete" ? `<span class="b3-list-item__action ariaLabel" data-type="compare" data-position="6south" aria-label="${window.siyuan.languages.compare}">
        <svg><use xlink:href="#iconSplitLR"></use></svg>
    </span>` : "";
                                html += `<li data-id="${docItem.id}" data-notebook-id="${docItem.notebook}" data-created="${created}" data-type="${itemType}" data-path="${docItem.path}" class="b3-list-item" style="padding-left: 22px">
    <span class="${opElement.value === "all" ? "" : "fn__none"}${chipClass}ariaLabel" data-position="6south" aria-label="${ariaLabel}">${docItem.op.substring(0, 1).toUpperCase()}</span>
    <span class="b3-list-item__text" title="${escapeAttr(docItem.title)}">${escapeHtml(docItem.title)}</span>
    <span class="fn__space"></span>
    ${compareHTML}
    <button type="button" class="b3-list-item__action ariaLabel" data-type="rollback" data-position="6south" aria-label="${window.siyuan.languages.rollback}">
        <svg><use xlink:href="#iconUndo"></use></svg>
    </button>
</li>`;
                            });
                            target.insertAdjacentHTML("afterend", `<ul>${html}</ul>`);
                        });
                    }
                }
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "rmtoggle") {
                target.nextElementSibling.classList.toggle("fn__none");
                target.firstElementChild.firstElementChild.classList.toggle("b3-list-item__arrow--open");
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.classList.contains("b3-list-item") && type === "repoitem" &&
                ["getRepoSnapshots", "getRepoTagSnapshots"].includes(getRepoSnapshotType(repoElement))) {
                const btnElement = repoElement.querySelector(".b3-button[data-type='compare']");
                const idJSON = JSON.parse(btnElement.getAttribute("data-ids") || "[]");
                const id = target.getAttribute("data-id");
                if (target.classList.contains("b3-list-item--focus")) {
                    target.classList.remove("b3-list-item--focus");
                    idJSON.find((item: { id: string, time: string }, index: number) => {
                        if (id === item.id) {
                            idJSON.splice(index, 1);
                            return true;
                        }
                    });
                } else {
                    target.classList.add("b3-list-item--focus");
                    while (idJSON.length > 1) {
                        if (idJSON[0].id !== id) {
                            target.parentElement.querySelector(`.b3-list-item[data-id="${idJSON.splice(0, 1)[0].id}"]`)?.classList.remove("b3-list-item--focus");
                        } else {
                            idJSON.splice(0, 1);
                        }
                    }
                    idJSON.push({id, time: target.querySelector('[data-type="hCreated"]').textContent});
                }

                if (idJSON.length === 2) {
                    btnElement.removeAttribute("disabled");
                } else {
                    btnElement.setAttribute("disabled", "disabled");
                }
                btnElement.setAttribute("data-ids", JSON.stringify(idJSON));
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (target.classList.contains("b3-list-item") && ["assets", "doc", "av"].includes(type)) {
                firstPanelElement.querySelector(".history__panel").classList.remove("history__panel--empty");
                const dataPath = target.getAttribute("data-path");
                if (type === "assets") {
                    assetElement.classList.remove("fn__none");
                    assetElement.innerHTML = renderAssetsPreview(dataPath);
                } else if (type === "doc") {
                    const k = (firstPanelElement.querySelector(".b3-text-field") as HTMLInputElement).value;
                    fetchPost("/api/history/getDocHistoryContent", {
                        historyPath: dataPath,
                        highlight: !isSupportCSSHL(),
                        k
                    }, (response) => {
                        if (response.data.isLargeDoc) {
                            mdElement.value = response.data.content;
                            mdElement.classList.remove("fn__none");
                            docElement.classList.add("fn__none");
                        } else {
                            mdElement.classList.add("fn__none");
                            docElement.classList.remove("fn__none");
                            historyEditor.protyle.options.history.created = target.dataset.created;
                            onGet({
                                data: response,
                                protyle: historyEditor.protyle,
                                action: [Constants.CB_GET_HISTORY, Constants.CB_GET_HTML],
                            });
                            searchMarkRender(historyEditor.protyle, k.split(" "));
                        }
                    });
                } else if (type === "av") {
                    mdElement.classList.add("fn__none");
                    docElement.classList.remove("fn__none");
                    historyEditor.protyle.options.history.created = target.dataset.created;
                    onGet({
                        data: {
                            data: {
                                content: `<div class="av" data-node-id="${Lute.NewNodeID()}" data-av-id="${target.querySelector(".b3-list-item__text").textContent}" data-type="NodeAttributeView" data-av-type="table"><div spellcheck="true"></div><div class="protyle-attr" contenteditable="false">${Constants.ZWSP}</div></div>`,
                                id: Lute.NewNodeID(),
                                rootID: Lute.NewNodeID(),
                            },
                            msg: "",
                            code: 0
                        },
                        protyle: historyEditor.protyle,
                        action: [Constants.CB_GET_HISTORY, Constants.CB_GET_HTML],
                    });
                }
                titleElement.classList.remove("fn__none");
                titleElement.textContent = target.querySelector(".b3-list-item__text").textContent;
                let currentItem = hasClosestByClassName(target, "b3-list") as HTMLElement;
                if (currentItem) {
                    currentItem = currentItem.querySelector(".b3-list-item--focus");
                    if (currentItem) {
                        currentItem.classList.remove("b3-list-item--focus");
                    }
                }
                target.classList.add("b3-list-item--focus");
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "editSnapshotMemo" && !window.siyuan.config.readonly) {
                const id = target.closest("[data-id]").getAttribute("data-id");
                openSnapshotMemo(repoElement, id, snapshotMemos.get(repoElement)?.get(id) || "");
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "fullCheckRepo") {
                if (!window.siyuan.config.readonly && !target.hasAttribute("disabled")) {
                    openSnapshotMemo(repoElement, undefined, "", true);
                }
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "genRepo") {
                if (!window.siyuan.config.readonly && !target.hasAttribute("disabled")) {
                    target.setAttribute("disabled", "disabled");
                    fetchPost("/api/repo/checkSnapshot", {}, (response) => {
                        if (response.data.changed && repoElement.isConnected) {
                            openSnapshotMemo(repoElement);
                        }
                    }).finally(() => target.removeAttribute("disabled"));
                }
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "removeRepoTagSnapshot" || type === "removeCloudRepoTagSnapshot") {
                const tag = target.parentElement.getAttribute("data-tag");
                confirmDialog(window.siyuan.languages.deleteOpConfirm, `${window.siyuan.languages.confirmDelete} <b>${escapeHtml(tag)}</b> ?`, () => {
                    fetchPost("/api/repo/" + type, {tag}, () => {
                        renderRepo(repoElement, 1);
                    });
                }, undefined, true);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "uploadSnapshot") {
                fetchPost("/api/repo/uploadCloudSnapshot", {
                    tag: target.parentElement.getAttribute("data-tag"),
                    id: target.parentElement.getAttribute("data-id")
                }, () => {
                    const cloudPane = repoRoot.querySelector('[data-repo-source="cloud"]');
                    renderRepo(cloudPane, parseInt(cloudPane.getAttribute("data-page")) || 1);
                });
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "downloadSnapshot") {
                fetchPost("/api/repo/downloadCloudSnapshot", {
                    tag: target.parentElement.getAttribute("data-tag"),
                    id: target.parentElement.getAttribute("data-id")
                }, () => {
                    const localPane = repoRoot.querySelector('[data-repo-source="local"]');
                    renderRepo(localPane, parseInt(localPane.getAttribute("data-page")) || 1);
                });
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "downloadRollback" && !window.siyuan.config.readonly) {
                confirmDialog("⚠️ " + window.siyuan.languages.downloadRollback, window.siyuan.languages.rollbackConfirm.replace("${name}", window.siyuan.languages.workspaceData)
                    .replace("${time}", (isMobile() ? target.parentElement.parentElement : target.parentElement).querySelector("span[data-type='hCreated']").textContent.trim()), () => {
                    const repoId = target.parentElement.getAttribute("data-id");
                    fetchPost("/api/repo/downloadCloudSnapshot", {
                        tag: target.parentElement.getAttribute("data-tag"),
                        id: repoId
                    }, () => {
                        fetchPost("/api/repo/checkoutRepo", {
                            id: repoId
                        });
                    });
                });
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "genTag") {
                const genTagDialog = openInputDialog({
                    title: window.siyuan.languages.tagSnapshot,
                    value: dayjs().format("YYYYMMDDHHmmss"),
                    placeholder: window.siyuan.languages.tagSnapshotTip,
                    confirmText: window.siyuan.languages.tagSnapshot,
                    actions: [{
                        text: window.siyuan.languages.tagSnapshotUpload,
                        position: "afterConfirm",
                        onClick: (value, genTagDialog) => {
                            fetchPost("/api/repo/tagSnapshot", {
                                id: target.parentElement.getAttribute("data-id"),
                                name: value
                            }, () => {
                                fetchPost("/api/repo/uploadCloudSnapshot", {
                                    tag: value,
                                    id: target.parentElement.getAttribute("data-id")
                                }, () => {
                                    renderRepo(repoRoot, 1);
                                });
                            });
                            genTagDialog.destroy();
                        },
                    }],
                    onConfirm: (value, genTagDialog) => {
                        fetchPost("/api/repo/tagSnapshot", {
                            id: target.parentElement.getAttribute("data-id"),
                            name: value
                        }, () => {
                            renderRepo(repoElement, 1);
                        });
                        genTagDialog.destroy();
                    },
                });
                genTagDialog.element.setAttribute("data-key", Constants.DIALOG_SNAPSHOTTAG);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "retryRepo") {
                renderRepo(repoElement, parseInt(repoElement.getAttribute("data-page")) || 1);
                break;
            } else if ((type === "previous" || type === "next") && target.getAttribute("disabled") !== "disabled") {
                const currentPage = parseInt(repoElement.getAttribute("data-page"));
                renderRepo(repoElement, type === "previous" ? currentPage - 1 : currentPage + 1);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "jumpRepoPage") {
                const currentPage = parseInt(repoElement.getAttribute("data-page"));
                const totalPage = parseInt(target.getAttribute("data-totalpage") || "1");

                if (totalPage > 1) {
                    openInputDialog({
                        title: window.siyuan.languages.jumpToPage.replace("${x}", totalPage),
                        value: String(currentPage),
                        type: "number",
                        min: "1",
                        max: String(totalPage),
                        onConfirm: (value, dialog) => {
                            if (!Number.isFinite(parseInt(value))) {
                                showMessage(window.siyuan.languages.jumpToPage.replace("${x}", totalPage));
                                return;
                            }
                            let page = parseInt(value);
                            page = Math.max(1, Math.min(page, totalPage));
                            renderRepo(repoElement, page);
                            dialog.destroy();
                        },
                    });
                }
            } else if (type === "jumpHistoryPage") {
                const currentPage = parseInt(historyElement.getAttribute("data-page"));
                const totalPage = parseInt(target.getAttribute("data-totalpage") || "1");

                if (totalPage > 1) {
                    openInputDialog({
                        title: window.siyuan.languages.jumpToPage.replace("${x}", totalPage),
                        value: String(currentPage),
                        type: "number",
                        min: "1",
                        max: String(totalPage),
                        onConfirm: (value, dialog) => {
                            if (!Number.isFinite(parseInt(value))) {
                                showMessage(window.siyuan.languages.jumpToPage.replace("${x}", totalPage));
                                return;
                            }
                            let page = parseInt(value);
                            page = Math.max(1, Math.min(page, totalPage));
                            renderDoc(firstPanelElement, page);
                            dialog.destroy();
                        },
                    });
                }
            } else if ((type === "docprevious" || type === "docnext") && target.getAttribute("disabled") !== "disabled") {
                const currentPage = parseInt(firstPanelElement.getAttribute("data-page"));
                renderDoc(firstPanelElement, type === "docprevious" ? currentPage - 1 : currentPage + 1);
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "rebuildIndex") {
                fetchPost("/api/history/reindexHistory");
                if (dialog) {
                    dialog.destroy();
                } else {
                    closeModel();
                    historyEditor = undefined;
                }
                event.stopPropagation();
                event.preventDefault();
                break;
            } else if (type === "compare" && !target.getAttribute("disabled")) {
                showDiff(app, JSON.parse(target.getAttribute("data-ids") || "[]"));
                event.stopPropagation();
                event.preventDefault();
                break;
            }
            target = target.parentElement;
        }
    });
};