2023-04-27 20:03:00 +08:00
|
|
|
import {Tab} from "../layout/Tab";
|
|
|
|
|
import {Custom} from "../layout/dock/Custom";
|
2025-10-14 09:46:23 +08:00
|
|
|
import {bindCardEvent, genCardHTML, initCardComponent} from "./openCard";
|
2023-04-27 20:03:00 +08:00
|
|
|
import {fetchPost} from "../util/fetch";
|
2023-04-28 10:30:27 +08:00
|
|
|
import {Protyle} from "../protyle";
|
2023-05-12 17:11:43 +08:00
|
|
|
import {setPanelFocus} from "../layout/util";
|
2023-05-18 19:27:21 +08:00
|
|
|
import {App} from "../index";
|
2024-04-08 12:05:27 +08:00
|
|
|
import {clearOBG} from "../layout/dock/util";
|
2023-04-27 20:03:00 +08:00
|
|
|
|
2023-04-28 12:02:08 +08:00
|
|
|
export const newCardModel = (options: {
|
2023-05-18 19:27:21 +08:00
|
|
|
app: App,
|
2023-04-28 12:02:08 +08:00
|
|
|
tab: Tab,
|
|
|
|
|
data: {
|
|
|
|
|
cardType: TCardType,
|
|
|
|
|
id: string,
|
|
|
|
|
title?: string
|
2024-01-08 22:12:19 +08:00
|
|
|
cardsData?: ICardData,
|
2023-11-06 18:19:27 +08:00
|
|
|
index?: number,
|
2023-04-28 12:02:08 +08:00
|
|
|
}
|
|
|
|
|
}) => {
|
|
|
|
|
let editor: Protyle;
|
2025-10-14 09:46:23 +08:00
|
|
|
|
|
|
|
|
const fetchCardsData = async (): Promise<ICardData> => {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
fetchPost(options.data.cardType === "all" ? "/api/riff/getRiffDueCards" :
|
|
|
|
|
(options.data.cardType === "doc" ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), {
|
|
|
|
|
rootID: options.data.id,
|
|
|
|
|
deckID: options.data.id,
|
|
|
|
|
notebook: options.data.id,
|
|
|
|
|
}, async (response) => {
|
|
|
|
|
let cardsData: ICardData = response.data;
|
|
|
|
|
for (let i = 0; i < options.app.plugins.length; i++) {
|
|
|
|
|
cardsData = await options.app.plugins[i].updateCards(response.data);
|
|
|
|
|
}
|
|
|
|
|
resolve(cardsData);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderCardsAndBindEvents = async (element: HTMLElement, data: any, cardsData: ICardData, index?: number, isUpdate?: boolean) => {
|
|
|
|
|
customObj.editors.forEach(editor => {
|
|
|
|
|
editor.destroy();
|
|
|
|
|
});
|
|
|
|
|
customObj.editors.length = 0;
|
|
|
|
|
|
|
|
|
|
element.innerHTML = genCardHTML({
|
|
|
|
|
id: data.id,
|
|
|
|
|
cardType: data.cardType,
|
|
|
|
|
cardsData,
|
|
|
|
|
isTab: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const cardOptions = {
|
|
|
|
|
app: options.app,
|
|
|
|
|
element: element,
|
|
|
|
|
id: data.id,
|
|
|
|
|
title: data.title,
|
|
|
|
|
cardType: data.cardType,
|
|
|
|
|
cardsData,
|
|
|
|
|
index,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (isUpdate) {
|
|
|
|
|
const initResult = await initCardComponent(cardOptions);
|
|
|
|
|
editor = initResult.editor;
|
|
|
|
|
} else {
|
|
|
|
|
editor = await bindCardEvent(cardOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customObj.editors.push(editor);
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-12 17:11:43 +08:00
|
|
|
const customObj = new Custom({
|
2023-05-18 19:27:21 +08:00
|
|
|
app: options.app,
|
2023-05-10 00:01:47 +08:00
|
|
|
type: "siyuan-card",
|
2023-04-28 12:02:08 +08:00
|
|
|
tab: options.tab,
|
|
|
|
|
data: options.data,
|
2024-01-08 22:12:19 +08:00
|
|
|
async init() {
|
2023-12-22 10:25:55 +08:00
|
|
|
if (options.data.cardsData) {
|
2025-10-14 09:46:23 +08:00
|
|
|
// 使用现有的 cardsData
|
2024-02-05 22:31:21 +08:00
|
|
|
for (let i = 0; i < options.app.plugins.length; i++) {
|
|
|
|
|
options.data.cardsData = await options.app.plugins[i].updateCards(options.data.cardsData);
|
|
|
|
|
}
|
2025-10-14 09:46:23 +08:00
|
|
|
await renderCardsAndBindEvents(this.element, this.data, options.data.cardsData, options.data.index);
|
2023-11-06 18:19:27 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/9561#issuecomment-1794473512
|
2023-12-22 10:25:55 +08:00
|
|
|
delete options.data.cardsData;
|
2023-11-06 18:19:27 +08:00
|
|
|
delete options.data.index;
|
2023-10-31 20:03:42 +08:00
|
|
|
} else {
|
2025-10-14 09:46:23 +08:00
|
|
|
// 获取新的 cardsData
|
|
|
|
|
const cardsData = await fetchCardsData();
|
|
|
|
|
await renderCardsAndBindEvents(this.element, this.data, cardsData);
|
2023-10-31 20:03:42 +08:00
|
|
|
}
|
2023-04-28 12:02:08 +08:00
|
|
|
},
|
|
|
|
|
destroy() {
|
|
|
|
|
if (editor) {
|
|
|
|
|
editor.destroy();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
resize() {
|
|
|
|
|
if (editor) {
|
|
|
|
|
editor.resize();
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-10-14 09:46:23 +08:00
|
|
|
async update() {
|
|
|
|
|
const cardsData = await fetchCardsData();
|
|
|
|
|
await renderCardsAndBindEvents(this.element, this.data, cardsData ,undefined, true);
|
2023-04-28 12:02:08 +08:00
|
|
|
}
|
|
|
|
|
});
|
2023-05-12 17:11:43 +08:00
|
|
|
customObj.element.addEventListener("click", () => {
|
2024-04-08 12:05:27 +08:00
|
|
|
clearOBG();
|
2023-05-12 17:11:43 +08:00
|
|
|
setPanelFocus(customObj.element.parentElement.parentElement);
|
|
|
|
|
});
|
|
|
|
|
return customObj;
|
2023-04-28 22:13:55 +08:00
|
|
|
};
|