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 {getDisplayName} from "./pathName"; 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) { const type = item.items[0].title.substr(item.items[0].title.lastIndexOf(".")).toLowerCase(); if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) { element.lastElementChild.innerHTML = ``; } else if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) { element.lastElementChild.innerHTML = ``; } else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) { element.lastElementChild.innerHTML = ``; } else { element.lastElementChild.innerHTML = item.items[0].path; } } } }); element.firstElementChild.innerHTML = logsHTML; }); }; const renderRepo = (element: HTMLElement) => { element.setAttribute("data-init", "true"); fetchPost("/api/repo/getRepoIndexLogs", {page: 1}, (response) => { if (response.data.logs.length === 0) { element.lastElementChild.firstElementChild.innerHTML = `
  • ${window.siyuan.languages.emptyContent}
  • `; return; } let repoHTML = ""; response.data.logs.forEach((item: { files: { path: string, id: string }[], id: string, hCreated: string }, index: number) => { repoHTML += `
  • 0 ? "" : " fn__hidden"}"> ${item.hCreated}
  • `; if (item.files.length > 0) { repoHTML += `"; } if (index === 0) { // fetchPost("/api/history/getDocHistoryContent", { // historyPath: item.items[0].path // }, (response) => { // element.lastElementChild.innerHTML = response.data.content; // }); } }); element.lastElementChild.firstElementChild.innerHTML = `${repoHTML}`; }); }; 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); 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); } } } 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}); break; } else if (target.classList.contains("b3-list-item__action")) { confirmDialog("⚠️ " + window.siyuan.languages.rollback, `${window.siyuan.languages.rollbackConfirm.replace("${date}", target.parentElement.textContent.trim())}`, () => { if (target.parentElement.getAttribute("data-type") === "assets") { fetchPost("/api/history/rollbackAssetsHistory", { historyPath: target.parentElement.getAttribute("data-path") }); } else if (target.parentElement.getAttribute("data-type") === "doc") { fetchPost("/api/history/rollbackDocHistory", { notebook: currentNotebook.id, historyPath: target.parentElement.getAttribute("data-path") }); } else { fetchPost("/api/history/rollbackNotebookHistory", { historyPath: target.parentElement.getAttribute("data-path") }); } }); 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 !== "notebook") { const dataPath = target.getAttribute("data-path"); if (dataPath) { let currentItem; if (type === "assets") { const type = dataPath.substr(dataPath.lastIndexOf(".")).toLowerCase(); if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) { firstPanelElement.nextElementSibling.lastElementChild.innerHTML = ``; } else if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) { firstPanelElement.nextElementSibling.lastElementChild.innerHTML = ``; } else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) { firstPanelElement.nextElementSibling.lastElementChild.innerHTML = ``; } else { firstPanelElement.nextElementSibling.lastElementChild.innerHTML = dataPath; } currentItem = firstPanelElement.nextElementSibling.querySelector(".b3-list-item--focus"); } else if (type === "doc") { fetchPost("/api/history/getDocHistoryContent", { historyPath: dataPath }, (response) => { firstPanelElement.lastElementChild.innerHTML = response.data.content; }); currentItem = firstPanelElement.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.memo, content: `
    `, width: isMobile() ? "80vw" : "520px", }); const textAreaElement = genRepoDialog.element.querySelector("textarea"); textAreaElement.focus(); const btnsElement = genRepoDialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { genRepoDialog.destroy(); }); btnsElement[1].addEventListener("click", () => { fetchPost("/api/repo/indexRepo", {message: textAreaElement.value}, () => { renderRepo(dialog.element.querySelector('#historyContainer [data-type="repo"]')) }) }); } target = target.parentElement; } }); };