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 {hasClosestByClassName} from "../protyle/util/hasClosest";
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) => {
let blocks = cardsResponse.data;
let countHTML = "";
let index = 0;
if (blocks.length > 0) {
countHTML = `1/${blocks.length}`;
}
const dialog = new Dialog({
title: window.siyuan.languages.riffCard,
content: `
${countHTML}
${window.siyuan.languages.noDueCard}
`,
width: isMobile() ? "96vw" : "50vw",
height: "70vh",
});
dialog.element.querySelector("input").focus();
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 (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(".b3-dialog__cardaction");
const selectElement = dialog.element.querySelector("select");
selectElement.addEventListener("change", () => {
fetchPost("/api/riff/getRiffDueCards", {deckID: selectElement.value}, (cardsChangeResponse) => {
blocks = cardsChangeResponse.data;
index = 0;
editor.protyle.element.classList.add("b3-dialog__cardblock--hide");
if (blocks.length > 0) {
countElement.innerHTML = `