import {Dialog} from "../dialog"; import {fetchPost} from "../util/fetch"; import {isMobile} from "../util/functions"; import {Protyle} from "../protyle"; import {Constants} from "../constants"; import {disabledProtyle, onGet} from "../protyle/util/onGet"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; import {hideElements} from "../protyle/ui/hideElements"; import {needSubscribe} from "../util/needSubscribe"; export const openCard = () => { const exit = window.siyuan.dialogs.find(item => { if (item.element.getAttribute("data-key") === window.siyuan.config.keymap.general.riffCard.custom) { item.destroy(); return true; } }); if (exit) { return; } let decksHTML = ''; fetchPost("/api/riff/getRiffDecks", {}, (response) => { response.data.forEach((deck: { id: string, name: string }) => { decksHTML += ``; }); fetchPost("/api/riff/getRiffDueCards", {deckID: ""}, (cardsResponse) => { openCardByData(cardsResponse.data, ``); }); }); }; export const openCardByData = (cardsData: ICard[], html = "") => { let blocks = cardsData; let index = 0; if (blocks.length > 0) { html += `
1/${blocks.length}
`; } const dialog = new Dialog({ content: `
${window.siyuan.languages.riffCard} ${html}
🔮
${window.siyuan.languages.noDueCard}
${window.siyuan.languages.reboot}
`, width: isMobile() ? "98vw" : "80vw", height: isMobile() ? "80vh" : "70vh", }); (dialog.element.querySelector(".b3-dialog__scrim") as HTMLElement).style.backgroundColor = "var(--b3-theme-background)"; (dialog.element.querySelector(".b3-dialog__container") as HTMLElement).style.maxWidth = "1024px"; const editor = new Protyle(dialog.element.querySelector("[data-type='render']") as HTMLElement, { blockId: "", action: [Constants.CB_GET_ALL], render: { background: false, title: false, gutter: true, breadcrumbDocName: true, }, typewriterMode: false }); if (window.siyuan.config.editor.readOnly) { disabledProtyle(editor.protyle); } if (blocks.length > 0) { fetchPost("/api/filetree/getDoc", { id: blocks[index].blockID, mode: 0, size: Constants.SIZE_GET_MAX }, (response) => { onGet(response, editor.protyle, [Constants.CB_GET_ALL, Constants.CB_GET_HTML]); }); } (dialog.element.firstElementChild as HTMLElement).style.zIndex = "200"; dialog.element.setAttribute("data-key", window.siyuan.config.keymap.general.riffCard.custom); const countElement = dialog.element.querySelector('[data-type="count"]'); const actionElements = dialog.element.querySelectorAll(".card__action"); const selectElement = dialog.element.querySelector("select"); const titleElement = countElement.previousElementSibling; dialog.element.addEventListener("click", (event) => { let type = ""; if (typeof event.detail === "string") { if (event.detail === "1" || event.detail === "j") { type = "0"; } else if (event.detail === "2" || event.detail === "k") { type = "1"; } else if (event.detail === "3" || event.detail === "l") { type = "2"; } else if (event.detail === "4" || event.detail === ";") { type = "3"; } else if (event.detail === " ") { type = "-1"; } else if (event.detail === "p") { type = "-2"; } else if (event.detail === "0") { type = "-3"; } } if (!type) { const buttonElement = hasClosestByClassName(event.target as HTMLElement, "b3-button"); if (buttonElement) { type = buttonElement.getAttribute("data-type"); } } if (!type || !blocks[index]) { return; } event.preventDefault(); event.stopPropagation(); hideElements(["toolbar", "hint", "util"], editor.protyle); if (type === "-1") { // 显示答案 if (actionElements[0].classList.contains("fn__none")) { return; } editor.protyle.element.classList.remove("card__block--hidemark", "card__block--hideli", "card__block--hidesb"); actionElements[0].classList.add("fn__none"); actionElements[1].querySelectorAll(".b3-button").forEach((element, btnIndex) => { if (btnIndex !== 0) { element.previousElementSibling.textContent = blocks[index].nextDues[btnIndex - 1]; } }); actionElements[1].classList.remove("fn__none"); return; } if (type === "-2") { // 上一步 if (actionElements[0].classList.contains("fn__none")) { return; } if (index > 0) { index--; nextCard({ countElement, editor, actionElements, index, blocks }); } return; } if (["0", "1", "2", "3", "-3"].includes(type) && actionElements[0].classList.contains("fn__none")) { fetchPost(type === "-3" ? "/api/riff/skipReviewRiffCard" : "/api/riff/reviewRiffCard", { deckID: blocks[index].deckID, cardID: blocks[index].cardID, rating: parseInt(type), reviewedCards: blocks }, () => { /// #if MOBILE if (type !== "-3" && (0 !== window.siyuan.config.sync.provider || (0 === window.siyuan.config.sync.provider && !needSubscribe(""))) && window.siyuan.config.repo.key && window.siyuan.config.sync.enabled) { document.getElementById("toolbarSync").classList.remove("fn__none"); } /// #endif index++; if (index > blocks.length - 1) { fetchPost(selectElement ? "/api/riff/getRiffDueCards" : (titleElement.getAttribute("data-id") ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), { rootID: titleElement.getAttribute("data-id"), deckID: selectElement?.value, notebook: titleElement.getAttribute("data-notebookid"), reviewedCards: blocks }, (treeCards) => { index = 0; blocks = treeCards.data; if (treeCards.data.length === 0) { allDone(countElement, editor, actionElements); } else { nextCard({ countElement, editor, actionElements, index, blocks }); } }); return; } nextCard({ countElement, editor, actionElements, index, blocks }); }); } }); if (!selectElement) { return; } selectElement.addEventListener("change", () => { fetchPost("/api/riff/getRiffDueCards", {deckID: selectElement.value}, (cardsChangeResponse) => { blocks = cardsChangeResponse.data; index = 0; if (blocks.length > 0) { nextCard({ countElement, editor, actionElements, index, blocks }); } else { allDone(countElement, editor, actionElements); } }); }); }; const nextCard = (options: { countElement: Element, editor: Protyle, actionElements: NodeListOf, index: number, blocks: ICard[] }) => { options.editor.protyle.element.classList.add("card__block--hide"); if (window.siyuan.config.flashcard.superBlock) { options.editor.protyle.element.classList.add("card__block--hidesb"); } if (window.siyuan.config.flashcard.list) { options.editor.protyle.element.classList.add("card__block--hideli"); } if (window.siyuan.config.flashcard.mark) { options.editor.protyle.element.classList.add("card__block--hidemark"); } options.actionElements[0].classList.remove("fn__none"); options.actionElements[1].classList.add("fn__none"); options.editor.protyle.element.classList.remove("fn__none"); options.editor.protyle.element.nextElementSibling.classList.add("fn__none"); options.countElement.lastElementChild.innerHTML = `${options.index + 1}/${options.blocks.length}`; options.countElement.classList.remove("fn__none"); if (options.index === 0) { options.actionElements[0].firstElementChild.setAttribute("disabled", "disabled"); } else { options.actionElements[0].firstElementChild.removeAttribute("disabled"); } fetchPost("/api/filetree/getDoc", { id: options.blocks[options.index].blockID, mode: 0, size: Constants.SIZE_GET_MAX }, (response) => { onGet(response, options.editor.protyle, [Constants.CB_GET_ALL, Constants.CB_GET_HTML]); }); }; const allDone = (countElement: Element, editor: Protyle, actionElements: NodeListOf) => { countElement.classList.add("fn__none"); editor.protyle.element.classList.add("fn__none"); editor.protyle.element.nextElementSibling.classList.remove("fn__none"); actionElements[0].classList.add("fn__none"); actionElements[1].classList.add("fn__none"); };