import {Protyle} from "../protyle"; import {fetchPost} from "../util/fetch"; import {Dialog} from "../dialog"; import {isMobile} from "../util/functions"; import {escapeAttr, escapeHtml} from "../util/escape"; import {getDisplayName, getNotebookName} from "../util/pathName"; import {getIconByType} from "../editor/getIcon"; import {unicode2Emoji} from "../emoji"; import {addLoading} from "../protyle/ui/initUI"; import {Constants} from "../constants"; import {disabledProtyle, onGet} from "../protyle/util/onGet"; import {App} from "../index"; export const viewCards = (app: App, deckID: string, title: string, deckType: "Tree" | "" | "Notebook", cb?: (response: IWebSocketData) => void) => { let pageIndex = 1; let edit: Protyle; fetchPost(`/api/riff/get${deckType}RiffCards`, { id: deckID, page: pageIndex }, (response) => { const dialog = new Dialog({ content: `
${escapeHtml(title)} ${pageIndex}/${response.data.pageCount || 1} ${response.data.total} ${isMobile() ? `
` : ""}
${window.siyuan.languages.emptyContent}
`, width: isMobile() ? "100vw" : "80vw", height: isMobile() ? "100vh" : "70vh", destroyCallback() { if (edit) { edit.destroy(); if (window.siyuan.mobile) { window.siyuan.mobile.popEditor = null; } } } }); if (response.data.blocks.length > 0) { edit = new Protyle(app, dialog.element.querySelector("#cardPreview") as HTMLElement, { blockId: "", render: { gutter: true, breadcrumbDocName: true }, }); if (window.siyuan.mobile) { window.siyuan.mobile.popEditor = edit; } if (window.siyuan.config.editor.readOnly) { disabledProtyle(edit.protyle); } getArticle(edit, dialog.element.querySelector(".b3-list-item--focus")?.getAttribute("data-id")); } const previousElement = dialog.element.querySelector('[data-type="previous"]'); const nextElement = dialog.element.querySelector('[data-type="next"]'); const listElement = dialog.element.querySelector(".b3-list--background"); if (response.data.pageCount > 1) { nextElement.removeAttribute("disabled"); } dialog.element.style.zIndex = "200"; dialog.element.setAttribute("data-key", "viewCards"); dialog.element.addEventListener("click", (event) => { if (typeof event.detail === "string") { let currentElement = listElement.querySelector(".b3-list-item--focus"); if (currentElement) { currentElement.classList.remove("b3-list-item--focus"); if (event.detail === "arrowup") { currentElement = currentElement.previousElementSibling || currentElement.parentElement.lastElementChild; } else if (event.detail === "arrowdown") { currentElement = currentElement.nextElementSibling || currentElement.parentElement.firstElementChild; } const currentRect = currentElement.getBoundingClientRect(); const parentRect = currentElement.parentElement.getBoundingClientRect(); if (currentRect.top < parentRect.top || currentRect.bottom > parentRect.bottom) { currentElement.scrollIntoView(currentRect.top < parentRect.top); } getArticle(edit, currentElement.getAttribute("data-id")); currentElement.classList.add("b3-list-item--focus"); } event.stopPropagation(); event.preventDefault(); return; } let target = event.target as HTMLElement; while (target && !dialog.element.isSameNode(target)) { const type = target.getAttribute("data-type"); if (type === "close") { dialog.destroy(); event.stopPropagation(); event.preventDefault(); break; } else if (type === "previous") { if (pageIndex <= 1) { return; } pageIndex--; if (pageIndex <= 1) { previousElement.setAttribute("disabled", "disabled"); } fetchPost(`/api/riff/get${deckType}RiffCards`, {id: deckID, page: pageIndex}, (cardsResponse) => { if (pageIndex === cardsResponse.data.pageCount) { nextElement.setAttribute("disabled", "disabled"); } else if (cardsResponse.data.pageCount > 1) { nextElement.removeAttribute("disabled"); } nextElement.nextElementSibling.nextElementSibling.textContent = `${pageIndex}/${cardsResponse.data.pageCount || 1}`; listElement.innerHTML = renderViewItem(cardsResponse.data.blocks, title, deckType); listElement.scrollTop = 0; getArticle(edit, dialog.element.querySelector(".b3-list-item--focus")?.getAttribute("data-id")); }); event.stopPropagation(); event.preventDefault(); break; } else if (type === "next") { if (pageIndex >= response.data.pageCount) { return; } pageIndex++; previousElement.removeAttribute("disabled"); fetchPost(`/api/riff/get${deckType}RiffCards`, {id: deckID, page: pageIndex}, (cardsResponse) => { if (pageIndex === cardsResponse.data.pageCount) { nextElement.setAttribute("disabled", "disabled"); } else if (cardsResponse.data.pageCount > 1) { nextElement.removeAttribute("disabled"); } nextElement.nextElementSibling.nextElementSibling.textContent = `${pageIndex}/${cardsResponse.data.pageCount || 1}`; listElement.innerHTML = renderViewItem(cardsResponse.data.blocks, title, deckType); listElement.scrollTop = 0; getArticle(edit, dialog.element.querySelector(".b3-list-item--focus")?.getAttribute("data-id")); }); event.stopPropagation(); event.preventDefault(); break; } else if (type === "card-item") { getArticle(edit, target.getAttribute("data-id")); listElement.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 === "remove") { fetchPost("/api/riff/removeRiffCards", { deckID: deckType === "" ? deckID : Constants.QUICK_DECK_ID, blockIDs: [target.getAttribute("data-id")] }, (removeResponse) => { let nextElment = target.parentElement.nextElementSibling; if (!nextElment) { nextElment = target.parentElement.previousElementSibling; } if (!nextElment && target.parentElement.parentElement.childElementCount > 1) { nextElment = target.parentElement.parentElement.firstElementChild; } if (!nextElment) { getArticle(edit, ""); listElement.innerHTML = `
${window.siyuan.languages.emptyContent}
`; } else { getArticle(edit, nextElment.getAttribute("data-id")); listElement.querySelector(".b3-list-item--focus")?.classList.remove("b3-list-item--focus"); nextElment.classList.add("b3-list-item--focus"); target.parentElement.remove(); } dialog.element.querySelector(".counter").textContent = (parseInt(dialog.element.querySelector(".counter").textContent) - 1).toString(); if (cb) { cb(removeResponse); } }); event.stopPropagation(); event.preventDefault(); break; } target = target.parentElement; } }); }); }; const renderViewItem = (blocks: IBlock[], title: string, deckType: string) => { let listHTML = ""; let isFirst = true; const pathArray = title.split("/"); pathArray.splice(0, 1); blocks.forEach((item: IBlock) => { if (item.type) { let hPath; if (deckType === "") { hPath = getNotebookName(item.box) + getDisplayName(Lute.UnEscapeHTMLStr(item.hPath), false); } else { hPath = getDisplayName(Lute.UnEscapeHTMLStr(item.hPath), false).replace("/" + pathArray.join("/"), ""); if (hPath.startsWith("/")) { hPath = hPath.substring(1); } } listHTML += `
${unicode2Emoji(item.ial.icon, "b3-list-item__graphic", true)} ${item.content || Constants.ZWSP} ${escapeHtml(hPath)} ${item.riffCardReps}
`; isFirst = false; } else { // 块被删除的情况 listHTML += `
${item.content}
`; } }); if (blocks.length === 0) { listHTML = `
${window.siyuan.languages.emptyContent}
`; } return listHTML; }; const getArticle = (edit: Protyle, id: string) => { if (!id) { edit.protyle.element.classList.add("fn__none"); edit.protyle.element.nextElementSibling.classList.remove("fn__none"); return; } edit.protyle.element.classList.remove("fn__none"); edit.protyle.element.nextElementSibling.classList.add("fn__none"); edit.protyle.scroll.lastScrollTop = 0; addLoading(edit.protyle); fetchPost("/api/filetree/getDoc", { id, mode: 0, size: Constants.SIZE_GET_MAX, }, getResponse => { onGet({ data: getResponse, protyle: edit.protyle, action: [Constants.CB_GET_ALL, Constants.CB_GET_HTML] }); }); };