diff --git a/app/src/history/doc.ts b/app/src/history/doc.ts index 4aa599ba1..041a15300 100644 --- a/app/src/history/doc.ts +++ b/app/src/history/doc.ts @@ -11,6 +11,7 @@ import {isMobile} from "../util/functions"; import {App} from "../index"; let historyEditor: Protyle; +let isLoading = false; const renderDoc = (element: HTMLElement, currentPage: number, id: string) => { const previousElement = element.querySelector('[data-type="docprevious"]'); @@ -22,12 +23,9 @@ const renderDoc = (element: HTMLElement, currentPage: number, id: string) => { previousElement.setAttribute("disabled", "disabled"); } const opElement = element.querySelector('.b3-select[data-type="opselect"]') 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; - docElement.classList.add("fn__none"); - mdElement.classList.add("fn__none"); - assetElement.classList.remove("fn__none"); + const listElement = element.querySelector('.b3-list--background') + element.querySelector('.history__text[data-type="docPanel"]').classList.add("fn__none"); + element.querySelector('.history__text[data-type="mdPanel"]').classList.add("fn__none"); fetchPost("/api/history/searchHistory", { query: id, page: currentPage, @@ -41,30 +39,32 @@ const renderDoc = (element: HTMLElement, currentPage: number, id: string) => { } nextElement.nextElementSibling.nextElementSibling.textContent = `${currentPage}/${response.data.pageCount || 1}`; if (response.data.histories.length === 0) { - element.lastElementChild.lastElementChild.previousElementSibling.classList.add("fn__none"); - element.lastElementChild.lastElementChild.classList.add("fn__none"); - element.lastElementChild.firstElementChild.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; + listElement.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; return; } let logsHTML = ""; response.data.histories.forEach((item: string) => { - logsHTML += `
  • - - ${dayjs(parseInt(item) * 1000).format("YYYY-MM-DD HH:mm:ss")} + logsHTML += `
  • + ${dayjs(parseInt(item) * 1000).format("YYYY-MM-DD HH:mm:ss")} + + + +
  • `; }); - element.lastElementChild.firstElementChild.innerHTML = logsHTML; + listElement.innerHTML = logsHTML; }); }; -export const openDocHistory = (app: App, id: string) => { +export const openDocHistory = (options: { + app: App, + id: string, + notebookId: string, + pathString: string +}) => { const contentHTML = `
    - - - - - 1/1 + ${isMobile() ? "" : options.pathString}
    + + + + + + 1/1 + ${isMobile() ? '' : ""}
    -
    +
    • ${window.siyuan.languages.emptyContent}
    -
    - +
    `; const dialog = new Dialog({ content: contentHTML, - width: isMobile() ? "92vw" : "768px", - height: isMobile() ? "80vh" : "70vh", + width: isMobile() ? "100vw" : "1024px", + height: isMobile() ? "100vh" : "80vh", destroyCallback() { historyEditor = undefined; } }); - bindEvent(app, dialog.element, id); -}; -const bindEvent = (app: App, element: HTMLElement, id: string) => { - element.querySelector(".b3-select").addEventListener("change", () => { - renderDoc(element, 1, id); + const opElement = dialog.element.querySelector(".b3-select") as HTMLSelectElement; + opElement.addEventListener("change", () => { + renderDoc(dialog.element, 1, options.id); }); - const docElement = element.querySelector('.history__text[data-type="docPanel"]') as HTMLElement; - const assetElement = element.querySelector('.history__text[data-type="assetPanel"]'); - const mdElement = element.querySelector('.history__text[data-type="mdPanel"]') as HTMLTextAreaElement; - renderDoc(element, 1, id); - historyEditor = new Protyle(app, docElement, { + const docElement = dialog.element.querySelector('.history__text[data-type="docPanel"]') as HTMLElement; + const mdElement = dialog.element.querySelector('.history__text[data-type="mdPanel"]') as HTMLTextAreaElement; + renderDoc(dialog.element, 1, options.id); + historyEditor = new Protyle(options.app, docElement, { blockId: "", action: [Constants.CB_GET_HISTORY], render: { @@ -118,35 +121,29 @@ const bindEvent = (app: App, element: HTMLElement, id: string) => { typewriterMode: false, }); disabledProtyle(historyEditor.protyle); - element.addEventListener("click", (event) => { + dialog.element.addEventListener("click", (event) => { let target = event.target as HTMLElement; - while (target && !target.isEqualNode(element)) { + while (target && !target.isEqualNode(dialog.element)) { const type = target.getAttribute("data-type"); - if (target.classList.contains("b3-list-item__action") && type === "rollback" && !window.siyuan.config.readonly) { - confirmDialog("⚠️ " + window.siyuan.languages.rollback, `${window.siyuan.languages.rollbackConfirm.replace("${date}", target.parentElement.textContent.trim())}`, () => { - const dataType = target.parentElement.getAttribute("data-type"); - if (dataType === "assets") { - fetchPost("/api/history/rollbackAssetsHistory", { - historyPath: target.parentElement.getAttribute("data-path") - }); - } else if (dataType === "doc") { + if (type === "close") { + dialog.destroy() + } else if (type === "rollback" && !isLoading) { + getHistoryPath(target.parentElement, opElement.value, options.id, (dataPath) => { + isLoading = false; + confirmDialog("⚠️ " + window.siyuan.languages.rollback, `${window.siyuan.languages.rollbackConfirm.replace("${date}", target.parentElement.textContent.trim())}`, () => { fetchPost("/api/history/rollbackDocHistory", { - // notebook: (firstPanelElement.querySelector('.b3-select[data-type="notebookselect"]') as HTMLSelectElement).value, - historyPath: target.parentElement.getAttribute("data-path") + notebook: options.notebookId, + historyPath: dataPath }); - } + }); }); event.stopPropagation(); event.preventDefault(); break; - } else if (target.classList.contains("b3-list-item")) { - const dataPath = target.getAttribute("data-path"); - if (type === "assets") { - assetElement.innerHTML = renderAssetsPreview(dataPath); - } else if (type === "doc") { + } else if (target.classList.contains("b3-list-item") && !isLoading) { + getHistoryPath(target, opElement.value, options.notebookId, (dataPath) => { fetchPost("/api/history/getDocHistoryContent", { historyPath: dataPath, - // k: (firstPanelElement.querySelector(".b3-text-field") as HTMLInputElement).value }, (response) => { if (response.data.isLargeDoc) { mdElement.value = response.data.content; @@ -161,22 +158,17 @@ const bindEvent = (app: App, element: HTMLElement, id: string) => { action: [Constants.CB_GET_HISTORY, Constants.CB_GET_HTML], }); } + isLoading = false; }); - } - 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"); + target.parentElement.querySelector(".b3-list-item--focus")?.classList.remove("b3-list-item--focus"); + target.classList.add("b3-list-item--focus"); + }); event.stopPropagation(); event.preventDefault(); break; } else if ((type === "docprevious" || type === "docnext") && target.getAttribute("disabled") !== "disabled") { - const currentPage = parseInt(element.getAttribute("data-page")); - renderDoc(element, type === "docprevious" ? currentPage - 1 : currentPage + 1, id); + const currentPage = parseInt(dialog.element.getAttribute("data-page")); + renderDoc(dialog.element, type === "docprevious" ? currentPage - 1 : currentPage + 1, options.id); event.stopPropagation(); event.preventDefault(); break; @@ -185,3 +177,20 @@ const bindEvent = (app: App, element: HTMLElement, id: string) => { } }); }; + + +const getHistoryPath = (target: Element, op: string, id: string, cb: (path: string) => void) => { + isLoading = true; + const path = target.getAttribute("data-path") + if (path) { + cb(path); + } + fetchPost("/api/history/getHistoryItems", { + query: id, + op, + type: 3, + created: target.getAttribute("data-created") + }, (response) => { + cb(response.data.items[0].path) + }); +} diff --git a/app/src/menus/navigation.ts b/app/src/menus/navigation.ts index fecc182c3..949abd065 100644 --- a/app/src/menus/navigation.ts +++ b/app/src/menus/navigation.ts @@ -546,7 +546,7 @@ export const initFileMenu = (app: App, notebookId: string, pathString: string, l label: window.siyuan.languages.dataHistory, icon: "iconHistory", click() { - openDocHistory(app, id); + openDocHistory({app, id, notebookId, pathString: name}); } }).element); } diff --git a/app/src/plugin/loader.ts b/app/src/plugin/loader.ts index e7b7f7b6d..15ae0caa3 100644 --- a/app/src/plugin/loader.ts +++ b/app/src/plugin/loader.ts @@ -148,9 +148,9 @@ export const afterLoadPlugin = (plugin: Plugin) => { document.querySelector("#" + (element.getAttribute("data-position") === "right" ? "barPlugins" : "drag")).before(element); } }); - resizeTopbar(); } /// #if !MOBILE + resizeTopbar(); mergePluginHotkey(plugin); plugin.statusBarIcons.forEach(element => { const statusElement = document.getElementById("status");