import {Dialog} from "../dialog"; import {confirmDialog} from "../dialog/confirmDialog"; import {fetchPost} from "./fetch"; import {Constants} from "../constants"; import {MenuItem} from "../menus/Menu"; import {unicode2Emoji} from "../emoji"; import {escapeHtml} from "./escape"; import {isMobile} from "./functions"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; import {renderAssetsPreview} from "../asset/renderAssets"; const renderDoc = (notebook: INotebook, element: HTMLElement) => { if (!notebook || !notebook.id) { return; } fetchPost("/api/history/getDocHistory", { notebook: notebook.id }, (response) => { const switchHTML = `
  • ${unicode2Emoji(notebook.icon || Constants.SIYUAN_IMAGE_NOTE)} ${escapeHtml(notebook.name)}
  • `; if (response.data.histories.length === 0) { element.lastElementChild.innerHTML = ""; element.firstElementChild.innerHTML = `${switchHTML}
  • ${window.siyuan.languages.emptyContent}
  • `; return; } let logsHTML = switchHTML; response.data.histories.forEach((item: { items: { path: string, title: string }[], hCreated: string }, index: number) => { logsHTML += `
  • 0 ? "" : " fn__hidden"}"> ${item.hCreated}
  • `; if (item.items.length > 0) { logsHTML += `"; if (index === 0) { fetchPost("/api/history/getDocHistoryContent", { historyPath: item.items[0].path }, (response) => { element.lastElementChild.innerHTML = response.data.content; }); } } }); element.firstElementChild.innerHTML = logsHTML; }); }; const renderAssets = (element: HTMLElement) => { element.setAttribute("data-init", "true"); fetchPost("/api/history/getAssetsHistory", {}, (response) => { if (response.data.histories.length === 0) { element.lastElementChild.innerHTML = ""; element.firstElementChild.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; return; } let logsHTML = ""; response.data.histories.forEach((item: { items: { path: string, title: string }[], hCreated: string }, index: number) => { logsHTML += `
  • 0 ? "" : " fn__hidden"}"> ${item.hCreated}
  • `; if (item.items.length > 0) { logsHTML += `"; if (index === 0) { element.lastElementChild.innerHTML = renderAssetsPreview(item.items[0].path); } } }); element.firstElementChild.innerHTML = logsHTML; }); }; const renderRepoItem = (response: IWebSocketData, element: Element, type: string) => { if (response.data.snapshots.length === 0) { element.lastElementChild.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; return; } let actionHTML = ""; if (type === "cloudTag") { actionHTML = ` `; } else if (type === "localTag") { actionHTML = ` `; } else if (type === "local") { actionHTML = ` `; } let repoHTML = ""; response.data.snapshots.forEach((item: { memo: string, id: string, hCreated: string, count: number, hSize: string, tag: string }) => { if (isMobile()) { repoHTML += `
  • ${item.tag} ${item.hCreated} ${item.hSize} ${actionHTML}
    ${escapeHtml(item.memo)}
  • `; } else { repoHTML += `
  • ${escapeHtml(item.memo)} ${item.tag}
    ${item.hCreated} ${item.hSize} ${window.siyuan.languages.fileCount}${item.count}
    ${actionHTML}
  • `; } }); element.lastElementChild.innerHTML = `${repoHTML}`; }; const renderRepo = (element: Element, currentPage: number) => { element.lastElementChild.innerHTML = '
  • '; const previousElement = element.querySelector('[data-type="previous"]'); const nextElement = element.querySelector('[data-type="next"]'); if (currentPage < 0) { if (currentPage === -1) { fetchPost("/api/repo/getRepoTagSnapshots", {}, (response) => { renderRepoItem(response, element, "localTag"); }); } if (currentPage === -2) { fetchPost("/api/repo/getCloudRepoTagSnapshots", {}, (response) => { renderRepoItem(response, element, "cloudTag"); }); } previousElement.classList.add("fn__none"); nextElement.classList.add("fn__none"); } else { previousElement.classList.remove("fn__none"); nextElement.classList.remove("fn__none"); element.setAttribute("data-init", "true"); element.setAttribute("data-page", currentPage.toString()); if (currentPage > 1) { previousElement.removeAttribute("disabled"); } else { previousElement.setAttribute("disabled", "disabled"); } fetchPost("/api/repo/getRepoSnapshots", {page: currentPage}, (response) => { if (currentPage < response.data.pageCount) { nextElement.removeAttribute("disabled"); } else { nextElement.setAttribute("disabled", "disabled"); } renderRepoItem(response, element, "local"); }); } }; const renderRmNotebook = (element: HTMLElement) => { element.setAttribute("data-init", "true"); fetchPost("/api/history/getNotebookHistory", {}, (response) => { if (response.data.histories.length === 0) { element.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; return; } let logsHTML = ""; response.data.histories.forEach((item: { items: { path: string, title: string }[], hCreated: string }, index: number) => { logsHTML += `
  • 0 ? "" : " fn__hidden"}"> ${item.hCreated}
  • `; if (item.items.length > 0) { logsHTML += `"; } }); element.innerHTML = logsHTML; }); }; export const openHistory = () => { const exitDialog = window.siyuan.dialogs.find((item) => { if (item.element.querySelector("#historyContainer")) { item.destroy(); return true; } }); if (exitDialog) { return; } const dialog = new Dialog({ content: `
    ${window.siyuan.languages.doc}
    ${window.siyuan.languages.assets}
    ${window.siyuan.languages.removedNotebook}
    ${window.siyuan.languages.dataSnapshot}
    • ${window.siyuan.languages.emptyContent}
    • ${window.siyuan.languages.emptyContent}
    • ${window.siyuan.languages.emptyContent}
    `, width: "80vw", height: "80vh", }); let currentNotebook: INotebook = { name: window.siyuan.languages.newFileTip, id: "", closed: true, icon: "", sort: 0 }; const currentNotebookId = localStorage.getItem(Constants.LOCAL_HISTORYNOTEID); window.siyuan.notebooks.find((item) => { if (!item.closed) { if (!currentNotebook.id) { currentNotebook = item; } if (currentNotebookId) { if (item.id === currentNotebookId) { currentNotebook = item; return true; } } else { currentNotebook = item; return true; } } }); const firstPanelElement = dialog.element.querySelector("#historyContainer [data-type=doc]") as HTMLElement; renderDoc(currentNotebook, firstPanelElement); const repoElement = dialog.element.querySelector('#historyContainer [data-type="repo"]'); const selectElement = repoElement.querySelector(".b3-select") as HTMLSelectElement; selectElement.addEventListener("change", () => { const value = selectElement.value; if (value === "0") { renderRepo(repoElement, 1); } else if (value === "1") { renderRepo(repoElement, -1); } else if (value === "2") { renderRepo(repoElement, -2); } }); dialog.element.addEventListener("click", (event) => { let target = event.target as HTMLElement; while (target && !target.isEqualNode(dialog.element)) { const type = target.getAttribute("data-type"); if (target.classList.contains("item")) { target.parentElement.querySelector(".item--focus").classList.remove("item--focus"); Array.from(dialog.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 === "assets") { renderAssets(item); } else if (type === "notebook") { renderRmNotebook(item); } else if (type === "repo") { renderRepo(item, 1); } } } else { item.classList.add("fn__none"); item.classList.remove("fn__block"); } }); break; } else if (type === "switchNotebook") { window.siyuan.menus.menu.remove(); window.siyuan.notebooks.forEach(item => { if (!item.closed) { window.siyuan.menus.menu.append(new MenuItem({ label: item.name, click: () => { if (item.id === currentNotebook.id) { return; } currentNotebook = item; window.localStorage.setItem(Constants.LOCAL_HISTORYNOTEID, item.id); renderDoc(item, firstPanelElement); } }).element); } }); window.siyuan.menus.menu.popup({x: event.clientX, y: event.clientY}); window.siyuan.menus.menu.element.style.zIndex = "310"; break; } else 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") { fetchPost("/api/history/rollbackDocHistory", { notebook: currentNotebook.id, 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") }); } }); break; } else if (type === "toggle") { target.nextElementSibling.classList.toggle("fn__none"); target.firstElementChild.firstElementChild.classList.toggle("b3-list-item__arrow--open"); break; } else if (target.classList.contains("b3-list-item") && (type === "assets" || type === "doc")) { const dataPath = target.getAttribute("data-path"); if (type === "assets") { firstPanelElement.nextElementSibling.lastElementChild.innerHTML = renderAssetsPreview(dataPath); } else if (type === "doc") { fetchPost("/api/history/getDocHistoryContent", { historyPath: dataPath }, (response) => { firstPanelElement.lastElementChild.innerHTML = response.data.content; }); } 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"); break; } else if (type === "genRepo") { const genRepoDialog = new Dialog({ title: window.siyuan.languages.snapshotMemo, content: `
    `, width: isMobile() ? "80vw" : "520px", }); const textareaElement = genRepoDialog.element.querySelector("textarea"); textareaElement.focus(); const btnsElement = genRepoDialog.element.querySelectorAll(".b3-button"); genRepoDialog.bindInput(textareaElement, () => { (btnsElement[1] as HTMLButtonElement).click(); }); btnsElement[0].addEventListener("click", () => { genRepoDialog.destroy(); }); btnsElement[1].addEventListener("click", () => { fetchPost("/api/repo/createSnapshot", {memo: textareaElement.value}, () => { renderRepo(repoElement, 1); }); genRepoDialog.destroy(); }); break; } else if (type === "removeRepoTagSnapshot" || type === "removeCloudRepoTagSnapshot") { const tag = target.parentElement.getAttribute("data-tag"); confirmDialog(window.siyuan.languages.delete, `${window.siyuan.languages.confirmDelete} ${tag}?`, () => { fetchPost("/api/repo/" + type, {tag}, () => { if (target.parentElement.parentElement.childElementCount === 1) { target.parentElement.parentElement.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; } else { target.parentElement.remove(); } }); }); break; } else if (type === "uploadSnapshot") { fetchPost("/api/repo/uploadCloudSnapshot", { tag: target.parentElement.getAttribute("data-tag"), id: target.parentElement.getAttribute("data-id") }); break; } else if (type === "downloadSnapshot") { fetchPost("/api/repo/downloadCloudSnapshot", { tag: target.parentElement.getAttribute("data-tag"), id: target.parentElement.getAttribute("data-id") }); break; } else if (type === "genTag") { const genTagDialog = new Dialog({ title: window.siyuan.languages.tagSnapshot, content: `
    `, width: isMobile() ? "80vw" : "520px", }); const inputElement = genTagDialog.element.querySelector(".b3-text-field") as HTMLInputElement; inputElement.focus(); const btnsElement = genTagDialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { genTagDialog.destroy(); }); genTagDialog.bindInput(inputElement, () => { (btnsElement[1] as HTMLButtonElement).click(); }); btnsElement[1].addEventListener("click", () => { fetchPost("/api/repo/tagSnapshot", { id: target.parentElement.getAttribute("data-id"), name: inputElement.value }, () => { renderRepo(repoElement, 1); }); genTagDialog.destroy(); }); 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); break; } target = target.parentElement; } }); };