import {Dialog} from "../dialog"; import {fetchPost} from "../util/fetch"; import {isMobile} from "../util/functions"; import {Protyle} from "../protyle"; import {Constants} from "../constants"; import {onGet} from "../protyle/util/onGet"; import {hasClosestByAttribute, hasClosestByClassName} from "../protyle/util/hasClosest"; import {hideElements} from "../protyle/ui/hideElements"; import {isPaidUser, needSubscribe} from "../util/needSubscribe"; import {fullscreen} from "../protyle/breadcrumb/action"; import {MenuItem} from "../menus/Menu"; import {escapeHtml} from "../util/escape"; /// #if !MOBILE import {openFile} from "../editor/util"; /// #endif import {getDisplayName, movePathTo} from "../util/pathName"; import {App} from "../index"; import {resize} from "../protyle/util/resize"; import {setStorageVal} from "../protyle/util/compatibility"; import {focusByRange} from "../protyle/util/selection"; import {updateCardHV} from "./util"; const genCardCount = (unreviewedNewCardCount: number, unreviewedOldCardCount: number, index = 1) => { return `${index} / ${unreviewedNewCardCount} + ${unreviewedOldCardCount}`; }; export const genCardHTML = (options: { id: string, cardType: TCardType, cardsData: ICardData, isTab: boolean }) => { let iconsHTML: string; /// #if MOBILE iconsHTML = `
${window.siyuan.languages.riffCard}
${genCardCount(options.cardsData.unreviewedNewCardCount, options.cardsData.unreviewedOldCardCount)}
`; /// #else iconsHTML = `
${options.isTab ? '
' : ``}
${genCardCount(options.cardsData.unreviewedNewCardCount, options.cardsData.unreviewedOldCardCount)}
`; /// #endif return `
${iconsHTML}
🔮
${window.siyuan.languages.noDueCard}
${window.siyuan.languages.nextRound}
`; }; export const bindCardEvent = async (options: { app: App, element: Element, title?: string, cardsData: ICardData cardType: TCardType, id?: string, dialog?: Dialog, index?: number }) => { if (window.siyuan.storage[Constants.LOCAL_FLASHCARD].fullscreen) { fullscreen(options.element.querySelector(".card__main"), options.element.querySelector('[data-type="fullscreen"]')); } let index = 0; if (typeof options.index === "number") { index = options.index; } const editor = new Protyle(options.app, options.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.mobile) { window.siyuan.mobile.popEditor = editor; } if (options.cardsData.cards.length > 0) { fetchPost("/api/block/getDocInfo", { id: options.cardsData.cards[index].blockID, }, (response) => { editor.protyle.wysiwyg.renderCustom(response.data.ial); fetchPost("/api/filetree/getDoc", { id: options.cardsData.cards[index].blockID, mode: 0, size: Constants.SIZE_GET_MAX }, (response) => { onGet({ updateReadonly: true, data: response, protyle: editor.protyle, action: response.data.rootID === response.data.id ? [Constants.CB_GET_HTML] : [Constants.CB_GET_ALL, Constants.CB_GET_HTML], }); }); }); } options.element.setAttribute("data-key", Constants.DIALOG_OPENCARD); const countElement = options.element.querySelector('[data-type="count"]'); countElement.firstElementChild.innerHTML = (index + 1).toString(); const actionElements = options.element.querySelectorAll(".card__action"); if (options.index === 0) { actionElements[0].firstElementChild.setAttribute("disabled", "disabled"); } else { actionElements[0].firstElementChild.removeAttribute("disabled"); } const filterElement = options.element.querySelector('[data-type="filter"]'); const fetchNewRound = () => { const currentCardType = filterElement.getAttribute("data-cardtype"); fetchPost(currentCardType === "all" ? "/api/riff/getRiffDueCards" : (currentCardType === "doc" ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), { rootID: filterElement.getAttribute("data-id"), deckID: filterElement.getAttribute("data-id"), notebook: filterElement.getAttribute("data-id"), }, async (treeCards) => { index = 0; options.cardsData = treeCards.data; for (let i = 0; i < options.app.plugins.length; i++) { options.cardsData = await options.app.plugins[i].updateCards(options.cardsData); } if (options.cardsData.cards.length > 0) { nextCard({ countElement, editor, actionElements, index, cardsData: options.cardsData }); } else { allDone(countElement, editor, actionElements); } }); }; options.element.addEventListener("click", (event: MouseEvent) => { const target = event.target as HTMLElement; let type = ""; if (typeof event.detail === "string") { if (["1", "j", "a"].includes(event.detail)) { type = "1"; } else if (["2", "k", "s"].includes(event.detail)) { type = "2"; } else if (["3", "l", "d"].includes(event.detail)) { type = "3"; } else if (["4", ";", "f"].includes(event.detail)) { type = "4"; } else if ([" ", "enter"].includes(event.detail)) { type = "-1"; } else if (["p", "q"].includes(event.detail)) { type = "-2"; } else if (["0", "x"].includes(event.detail)) { type = "-3"; } } else { const fullscreenElement = hasClosestByAttribute(target, "data-type", "fullscreen"); if (fullscreenElement) { fullscreen(options.element.querySelector(".card__main"), options.element.querySelector('[data-type="fullscreen"]')); resize(editor.protyle); window.siyuan.storage[Constants.LOCAL_FLASHCARD].fullscreen = !window.siyuan.storage[Constants.LOCAL_FLASHCARD].fullscreen; setStorageVal(Constants.LOCAL_FLASHCARD, window.siyuan.storage[Constants.LOCAL_FLASHCARD]); event.stopPropagation(); event.preventDefault(); return; } /// #if !MOBILE const sticktabElement = hasClosestByAttribute(target, "data-type", "sticktab"); if (sticktabElement) { openFile({ app: options.app, position: "right", custom: { icon: "iconRiffCard", title: window.siyuan.languages.spaceRepetition, data: { cardsData: options.cardsData, index, cardType: filterElement.getAttribute("data-cardtype") as TCardType, id: filterElement.getAttribute("data-id"), title: options.title }, id: "siyuan-card" }, }); if (options.dialog) { options.dialog.destroy(); } event.stopPropagation(); event.preventDefault(); return; } /// #endif const closeElement = hasClosestByAttribute(target, "data-type", "close"); if (closeElement) { if (options.dialog) { options.dialog.destroy(); } event.stopPropagation(); event.preventDefault(); return; } const filterTempElement = hasClosestByAttribute(target, "data-type", "filter"); if (filterTempElement) { fetchPost("/api/riff/getRiffDecks", {}, (response) => { window.siyuan.menus.menu.remove(); window.siyuan.menus.menu.append(new MenuItem({ iconHTML: "", label: window.siyuan.languages.all, click() { filterElement.setAttribute("data-id", ""); filterElement.setAttribute("data-cardtype", "all"); fetchNewRound(); }, }).element); window.siyuan.menus.menu.append(new MenuItem({ iconHTML: "", label: window.siyuan.languages.fileTree, click() { movePathTo((toPath, toNotebook) => { filterElement.setAttribute("data-id", toPath[0] === "/" ? toNotebook[0] : getDisplayName(toPath[0], true, true)); filterElement.setAttribute("data-cardtype", toPath[0] === "/" ? "notebook" : "doc"); fetchNewRound(); }, [], undefined, window.siyuan.languages.specifyPath, true); } }).element); if (options.title || response.data.length > 0) { window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element); } if (options.title) { window.siyuan.menus.menu.append(new MenuItem({ iconHTML: "", label: escapeHtml(options.title), click() { filterElement.setAttribute("data-id", options.id); filterElement.setAttribute("data-cardtype", options.cardType); fetchNewRound(); }, }).element); if (response.data.length > 0) { window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element); } } response.data.forEach((deck: { id: string, name: string }) => { window.siyuan.menus.menu.append(new MenuItem({ iconHTML: "", label: escapeHtml(deck.name), click() { filterElement.setAttribute("data-id", deck.id); filterElement.setAttribute("data-cardtype", "all"); fetchNewRound(); }, }).element); }); const filterRect = filterTempElement.getBoundingClientRect(); window.siyuan.menus.menu.popup({x: filterRect.left, y: filterRect.bottom}); }); event.stopPropagation(); event.preventDefault(); return; } const newroundElement = hasClosestByAttribute(target, "data-type", "newround"); if (newroundElement) { fetchNewRound(); event.stopPropagation(); event.preventDefault(); return; } } if (!type) { const buttonElement = hasClosestByClassName(target, "b3-button"); if (buttonElement) { type = buttonElement.getAttribute("data-type"); } } if (!type || !options.cardsData.cards[index]) { return; } event.preventDefault(); event.stopPropagation(); hideElements(["toolbar", "hint", "util", "gutter"], editor.protyle); if (type === "-1") { // 显示答案 if (actionElements[0].classList.contains("fn__none")) { type = "3"; } else { editor.protyle.element.classList.remove("card__block--hidemark", "card__block--hideli", "card__block--hidesb", "card__block--hideh"); actionElements[0].classList.add("fn__none"); actionElements[1].querySelectorAll(".b3-button").forEach((element, btnIndex) => { element.previousElementSibling.textContent = options.cardsData.cards[index].nextDues[btnIndex]; }); actionElements[1].classList.remove("fn__none"); emitEvent(options.app, options.cardsData.cards[index], type); return; } } else if (type === "-2") { // 上一步 if (actionElements[0].classList.contains("fn__none")) { return; } if (index > 0) { index--; nextCard({ countElement, editor, actionElements, index, cardsData: options.cardsData }); emitEvent(options.app, options.cardsData.cards[index + 1], type); } return; } if (["1", "2", "3", "4", "-3"].includes(type) && actionElements[0].classList.contains("fn__none")) { fetchPost(type === "-3" ? "/api/riff/skipReviewRiffCard" : "/api/riff/reviewRiffCard", { deckID: options.cardsData.cards[index].deckID, cardID: options.cardsData.cards[index].cardID, rating: parseInt(type), reviewedCards: options.cardsData.cards }, () => { /// #if MOBILE if (type !== "-3" && ((0 !== window.siyuan.config.sync.provider && isPaidUser()) || (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 > options.cardsData.cards.length - 1) { const currentCardType = filterElement.getAttribute("data-cardtype"); fetchPost(currentCardType === "all" ? "/api/riff/getRiffDueCards" : (currentCardType === "doc" ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), { rootID: filterElement.getAttribute("data-id"), deckID: filterElement.getAttribute("data-id"), notebook: filterElement.getAttribute("data-id"), reviewedCards: options.cardsData.cards }, async (result) => { emitEvent(options.app, options.cardsData.cards[index - 1], type); index = 0; options.cardsData = result.data; for (let i = 0; i < options.app.plugins.length; i++) { options.cardsData = await options.app.plugins[i].updateCards(options.cardsData); } if (options.cardsData.cards.length === 0) { if (options.cardsData.unreviewedCount > 0) { newRound(countElement, editor, actionElements, result.data.unreviewedCount); } else { allDone(countElement, editor, actionElements); } } else { nextCard({ countElement, editor, actionElements, index, cardsData: options.cardsData }); } }); return; } nextCard({ countElement, editor, actionElements, index, cardsData: options.cardsData }); emitEvent(options.app, options.cardsData.cards[index - 1], type); }); } }); return editor; }; const emitEvent = (app: App, card: ICard, type: string) => { app.plugins.forEach(item => { item.eventBus.emit("click-flashcard-action", { type, card }); }); }; export const openCard = (app: App) => { fetchPost("/api/riff/getRiffDueCards", {deckID: ""}, (cardsResponse) => { openCardByData(app, cardsResponse.data, "all"); }); }; export const openCardByData = async (app: App, cardsData: ICardData, cardType: TCardType, id?: string, title?: string) => { const exit = window.siyuan.dialogs.find(item => { if (item.element.getAttribute("data-key") === Constants.DIALOG_OPENCARD) { item.destroy(); return true; } }); if (exit) { return; } let lastRange: Range; if (getSelection().rangeCount > 0) { lastRange = getSelection().getRangeAt(0); } for (let i = 0; i < app.plugins.length; i++) { cardsData = await app.plugins[i].updateCards(cardsData); } const dialog = new Dialog({ positionId: Constants.DIALOG_OPENCARD, content: genCardHTML({id, cardType, cardsData, isTab: false}), width: isMobile() ? "100vw" : "80vw", height: isMobile() ? "100vh" : "70vh", destroyCallback() { if (editor) { editor.destroy(); if (window.siyuan.mobile) { window.siyuan.mobile.popEditor = null; } } if (lastRange) { focusByRange(lastRange); } }, resizeCallback(type: string) { if (type !== "d" && type !== "t" && editor) { editor.resize(); } } }); (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 = await bindCardEvent({ app, element: dialog.element, cardsData, title, id, cardType, dialog }); editor.resize(); dialog.editors = { card: editor }; /// #if !MOBILE const focusElement = dialog.element.querySelector(".block__icons button.block__icon") as HTMLElement; focusElement.focus(); const range = document.createRange(); range.selectNodeContents(focusElement); range.collapse(); focusByRange(range); /// #endif updateCardHV(); }; const nextCard = (options: { countElement: Element, editor: Protyle, actionElements: NodeListOf, index: number, cardsData: ICardData }) => { 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.heading) { options.editor.protyle.element.classList.add("card__block--hideh"); } 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.innerHTML = genCardCount(options.cardsData.unreviewedNewCardCount, options.cardsData.unreviewedOldCardCount, options.index + 1); 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/block/getDocInfo", { id: options.cardsData.cards[options.index].blockID, }, (response) => { options.editor.protyle.wysiwyg.renderCustom(response.data.ial); fetchPost("/api/filetree/getDoc", { id: options.cardsData.cards[options.index].blockID, mode: 0, size: Constants.SIZE_GET_MAX }, (response) => { onGet({ updateReadonly: true, data: response, protyle: options.editor.protyle, action: response.data.rootID === response.data.id ? [Constants.CB_GET_HTML] : [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"); const emptyElement = editor.protyle.element.nextElementSibling; emptyElement.innerHTML = `
🔮
${window.siyuan.languages.noDueCard}`; emptyElement.classList.remove("fn__none"); actionElements[0].classList.add("fn__none"); actionElements[1].classList.add("fn__none"); }; const newRound = (countElement: Element, editor: Protyle, actionElements: NodeListOf, unreviewedCount: number) => { countElement.classList.add("fn__none"); editor.protyle.element.classList.add("fn__none"); const emptyElement = editor.protyle.element.nextElementSibling; emptyElement.innerHTML = `
♻️
${window.siyuan.languages.continueReview2.replace("${count}", unreviewedCount)}
`; emptyElement.classList.remove("fn__none"); actionElements[0].classList.add("fn__none"); actionElements[1].classList.add("fn__none"); };