siyuan/app/src/card/openCard.ts

192 lines
9.3 KiB
TypeScript
Raw Normal View History

import {Dialog} from "../dialog";
import {fetchPost} from "../util/fetch";
import {isMobile} from "../util/functions";
import {Protyle} from "../protyle";
import {Constants} from "../constants";
2022-12-24 17:59:47 +08:00
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();
2022-12-28 22:51:00 +08:00
return true;
}
2022-12-28 22:51:00 +08:00
});
if (exit) {
return;
}
let decksHTML = '<option value="">All</option>';
fetchPost("/api/riff/getRiffDecks", {}, (response) => {
response.data.forEach((deck: { id: string, name: string }) => {
decksHTML += `<option value="${deck.id}">${deck.name}</option>`;
2022-12-22 22:19:04 +08:00
});
fetchPost("/api/riff/getRiffDueCards", {deckID: ""}, (cardsResponse) => {
let blocks = cardsResponse.data;
2022-12-22 22:19:04 +08:00
let countHTML = "";
let index = 0;
if (blocks.length > 0) {
2022-12-22 22:19:04 +08:00
countHTML = `<span>1</span>/${blocks.length}`;
}
const dialog = new Dialog({
title: window.siyuan.languages.riffCard,
content: `<div class="fn__flex-column b3-dialog__content" style="box-sizing: border-box;max-height: 100%">
<div class="fn__flex">
<select class="b3-select fn__flex-1">${decksHTML}</select>
<div style="margin-left: 8px" class="ft__on-surface ft__smaller fn__flex-center${blocks.length === 0 ? " fn__none" : ""}" data-type="count">${countHTML}</div>
</div>
2022-12-23 23:40:29 +08:00
<div class="fn__hr--b"><input style="opacity: 0;height: 1px;box-sizing: border-box"></div>
<div class="b3-dialog__cardblock b3-dialog__cardblock--hide fn__flex-1${blocks.length === 0 ? " fn__none" : ""}" data-type="render"></div>
<div class="b3-dialog__cardempty${blocks.length === 0 ? "" : " fn__none"}" data-type="empty">${window.siyuan.languages.noDueCard}</div>
<div class="fn__flex b3-dialog__cardaction${blocks.length === 0 ? " fn__none" : ""}">
<span class="fn__flex-1"></span>
<button data-type="-1" class="b3-button" style="margin-top: 16px">Show (S)</button>
<span class="fn__flex-1"></span>
</div>
<div class="fn__flex b3-dialog__cardaction fn__none">
<div>
<span></span>
<button data-type="0" class="b3-button b3-button--error">Again (A)</button>
</div>
<span class="fn__flex-1"></span>
<div>
<span></span>
<button data-type="1" class="b3-button b3-button--warning">Hard (H)</button>
</div>
<span class="fn__flex-1"></span>
<div>
<span></span>
<button data-type="2" class="b3-button b3-button--info">Good (G)</button>
</div>
<span class="fn__flex-1"></span>
<div>
<span></span>
<button data-type="3" class="b3-button b3-button--success">Easy (E)</button>
</div>
</div>
</div>`,
width: isMobile() ? "96vw" : "50vw",
height: "70vh",
2022-12-22 22:19:04 +08:00
});
dialog.element.querySelector("input").focus();
const editor = new Protyle(dialog.element.querySelector("[data-type='render']") as HTMLElement, {
blockId: "",
2022-12-24 17:59:47 +08:00
action: [Constants.CB_GET_ALL],
render: {
background: false,
title: false,
gutter: false,
2022-12-24 17:59:47 +08:00
breadcrumbDocName: true,
},
typewriterMode: false
});
if (blocks.length > 0) {
fetchPost("/api/filetree/getDoc", {
id: blocks[index].blockID,
mode: 0,
size: Constants.SIZE_GET_MAX
}, (response) => {
2022-12-24 17:59:47 +08:00
onGet(response, editor.protyle, [Constants.CB_GET_ALL, Constants.CB_GET_HTML]);
});
}
(dialog.element.firstElementChild as HTMLElement).style.zIndex = "200";
2022-12-22 22:19:04 +08:00
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");
2022-12-22 22:19:04 +08:00
const selectElement = dialog.element.querySelector("select");
selectElement.addEventListener("change", () => {
fetchPost("/api/riff/getRiffDueCards", {deckID: selectElement.value}, (cardsChangeResponse) => {
blocks = cardsChangeResponse.data;
2022-12-22 22:19:04 +08:00
index = 0;
editor.protyle.element.classList.add("b3-dialog__cardblock--hide");
if (blocks.length > 0) {
2022-12-22 22:19:04 +08:00
countElement.innerHTML = `<span>1</span>/${blocks.length}`;
countElement.classList.remove("fn__none");
editor.protyle.element.classList.remove("fn__none");
editor.protyle.element.nextElementSibling.classList.add("fn__none");
actionElements[0].classList.remove("fn__none");
actionElements[1].classList.add("fn__none");
fetchPost("/api/filetree/getDoc", {
id: blocks[index].blockID,
mode: 0,
size: Constants.SIZE_GET_MAX
}, (response) => {
2022-12-24 17:59:47 +08:00
onGet(response, editor.protyle, [Constants.CB_GET_ALL, Constants.CB_GET_HTML]);
});
} else {
2022-12-22 22:19:04 +08:00
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");
}
2022-12-22 22:19:04 +08:00
});
});
dialog.element.addEventListener("click", (event) => {
2022-12-22 22:19:04 +08:00
let type = "";
if (typeof event.detail === "string") {
if (event.detail === "a") {
2022-12-22 22:19:04 +08:00
type = "0";
} else if (event.detail === "h") {
2022-12-22 22:19:04 +08:00
type = "1";
} else if (event.detail === "g") {
2022-12-22 22:19:04 +08:00
type = "2";
} else if (event.detail === "e") {
2022-12-22 22:19:04 +08:00
type = "3";
} else if (event.detail === "s") {
2022-12-22 22:19:04 +08:00
type = "-1";
}
}
if (!type) {
2022-12-22 22:19:04 +08:00
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();
if (type === "-1") {
editor.protyle.element.classList.remove("b3-dialog__cardblock--hide");
actionElements[0].classList.add("fn__none");
actionElements[1].querySelectorAll(".b3-button").forEach((element, btnIndex) => {
element.previousElementSibling.textContent = blocks[index].nextDues[btnIndex];
})
actionElements[1].classList.remove("fn__none");
return;
}
if (["0", "1", "2", "3"].includes(type)) {
fetchPost("/api/riff/reviewRiffCard", {
deckID: blocks[index].deckID,
blockID: blocks[index].blockID,
rating: parseInt(type)
}, () => {
2022-12-22 22:19:04 +08:00
index++;
editor.protyle.element.classList.remove("b3-dialog__cardblock--hide");
if (index > blocks.length - 1) {
2022-12-22 22:19:04 +08:00
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");
return;
}
actionElements[0].classList.remove("fn__none");
actionElements[1].classList.add("fn__none");
2022-12-22 22:19:04 +08:00
countElement.firstElementChild.innerHTML = (index + 1).toString();
fetchPost("/api/filetree/getDoc", {
id: blocks[index].blockID,
mode: 0,
size: Constants.SIZE_GET_MAX
}, (response) => {
2022-12-24 17:59:47 +08:00
onGet(response, editor.protyle, [Constants.CB_GET_ALL, Constants.CB_GET_HTML]);
});
2022-12-22 22:19:04 +08:00
});
}
2022-12-22 22:19:04 +08:00
});
});
});
};