Skip to content

Commit 740bf34

Browse files
committed
🎨 #15627
1 parent b6a9ef2 commit 740bf34

File tree

2 files changed

+78
-89
lines changed

2 files changed

+78
-89
lines changed

app/src/card/newCardTab.ts

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Tab} from "../layout/Tab";
22
import {Custom} from "../layout/dock/Custom";
3-
import {bindCardEvent, genCardHTML, initCardComponent} from "./openCard";
3+
import {bindCardEvent, genCardHTML} from "./openCard";
44
import {fetchPost} from "../util/fetch";
55
import {Protyle} from "../protyle";
66
import {setPanelFocus} from "../layout/util";
@@ -19,76 +19,65 @@ export const newCardModel = (options: {
1919
}
2020
}) => {
2121
let editor: Protyle;
22-
23-
const fetchCardsData = async (): Promise<ICardData> => {
24-
return new Promise((resolve) => {
25-
fetchPost(options.data.cardType === "all" ? "/api/riff/getRiffDueCards" :
26-
(options.data.cardType === "doc" ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), {
27-
rootID: options.data.id,
28-
deckID: options.data.id,
29-
notebook: options.data.id,
30-
}, async (response) => {
31-
let cardsData: ICardData = response.data;
32-
for (let i = 0; i < options.app.plugins.length; i++) {
33-
cardsData = await options.app.plugins[i].updateCards(response.data);
34-
}
35-
resolve(cardsData);
36-
});
37-
});
38-
};
39-
40-
const renderCardsAndBindEvents = async (element: HTMLElement, data: any, cardsData: ICardData, index?: number, isUpdate?: boolean) => {
41-
customObj.editors.forEach(editor => {
42-
editor.destroy();
43-
});
44-
customObj.editors.length = 0;
45-
46-
element.innerHTML = genCardHTML({
47-
id: data.id,
48-
cardType: data.cardType,
49-
cardsData,
50-
isTab: true,
51-
});
52-
53-
const cardOptions = {
54-
app: options.app,
55-
element: element,
56-
id: data.id,
57-
title: data.title,
58-
cardType: data.cardType,
59-
cardsData,
60-
index,
61-
};
62-
63-
if (isUpdate) {
64-
const initResult = await initCardComponent(cardOptions);
65-
editor = initResult.editor;
66-
} else {
67-
editor = await bindCardEvent(cardOptions);
68-
}
69-
70-
customObj.editors.push(editor);
71-
};
72-
7322
const customObj = new Custom({
7423
app: options.app,
7524
type: "siyuan-card",
7625
tab: options.tab,
7726
data: options.data,
7827
async init() {
7928
if (options.data.cardsData) {
80-
// 使用现有的 cardsData
29+
let cardsData = options.data.cardsData;
8130
for (let i = 0; i < options.app.plugins.length; i++) {
82-
options.data.cardsData = await options.app.plugins[i].updateCards(options.data.cardsData);
31+
cardsData = await options.app.plugins[i].updateCards(options.data.cardsData);
8332
}
84-
await renderCardsAndBindEvents(this.element, this.data, options.data.cardsData, options.data.index);
33+
this.element.innerHTML = genCardHTML({
34+
id: this.data.id,
35+
cardType: this.data.cardType,
36+
cardsData,
37+
isTab: true,
38+
});
39+
40+
editor = await bindCardEvent({
41+
app: options.app,
42+
element: this.element,
43+
id: this.data.id,
44+
title: this.data.title,
45+
cardType: this.data.cardType,
46+
cardsData,
47+
index: options.data.index,
48+
});
49+
customObj.editors.push(editor);
8550
// https://github.com/siyuan-note/siyuan/issues/9561#issuecomment-1794473512
8651
delete options.data.cardsData;
8752
delete options.data.index;
8853
} else {
89-
// 获取新的 cardsData
90-
const cardsData = await fetchCardsData();
91-
await renderCardsAndBindEvents(this.element, this.data, cardsData);
54+
fetchPost(this.data.cardType === "all" ? "/api/riff/getRiffDueCards" :
55+
(this.data.cardType === "doc" ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), {
56+
rootID: this.data.id,
57+
deckID: this.data.id,
58+
notebook: this.data.id,
59+
}, async (response) => {
60+
let cardsData = response.data;
61+
for (let i = 0; i < options.app.plugins.length; i++) {
62+
cardsData = await options.app.plugins[i].updateCards(cardsData);
63+
}
64+
this.element.innerHTML = genCardHTML({
65+
id: this.data.id,
66+
cardType: this.data.cardType,
67+
cardsData,
68+
isTab: true,
69+
});
70+
71+
editor = await bindCardEvent({
72+
app: options.app,
73+
element: this.element,
74+
id: this.data.id,
75+
title: this.data.title,
76+
cardType: this.data.cardType,
77+
cardsData,
78+
});
79+
customObj.editors.push(editor);
80+
});
9281
}
9382
},
9483
destroy() {
@@ -101,9 +90,36 @@ export const newCardModel = (options: {
10190
editor.resize();
10291
}
10392
},
104-
async update() {
105-
const cardsData = await fetchCardsData();
106-
await renderCardsAndBindEvents(this.element, this.data, cardsData ,undefined, true);
93+
update() {
94+
fetchPost(this.data.cardType === "all" ? "/api/riff/getRiffDueCards" :
95+
(this.data.cardType === "doc" ? "/api/riff/getTreeRiffDueCards" : "/api/riff/getNotebookRiffDueCards"), {
96+
rootID: this.data.id,
97+
deckID: this.data.id,
98+
notebook: this.data.id,
99+
}, async (response) => {
100+
let cardsData = response.data;
101+
for (let i = 0; i < options.app.plugins.length; i++) {
102+
cardsData = await options.app.plugins[i].updateCards(cardsData);
103+
}
104+
customObj.editors.forEach(item => {
105+
item.destroy();
106+
});
107+
this.element.innerHTML = genCardHTML({
108+
id: this.data.id,
109+
cardType: this.data.cardType,
110+
cardsData,
111+
isTab: true,
112+
});
113+
editor = await bindCardEvent({
114+
app: options.app,
115+
element: this.element,
116+
id: this.data.id,
117+
title: this.data.title,
118+
cardType: this.data.cardType,
119+
cardsData,
120+
});
121+
customObj.editors.push(editor);
122+
});
107123
}
108124
});
109125
customObj.element.addEventListener("click", () => {

app/src/card/openCard.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const getEditor = (id: string, protyle: IProtyle, element: Element, currentCard:
225225

226226
};
227227

228-
export const initCardComponent = async (options: {
228+
export const bindCardEvent = async (options: {
229229
app: App,
230230
element: Element,
231231
title?: string,
@@ -301,33 +301,6 @@ export const initCardComponent = async (options: {
301301
};
302302

303303
countElement.innerHTML = genCardCount(options.cardsData, index);
304-
305-
return {
306-
editor,
307-
index,
308-
actionElements,
309-
countElement,
310-
filterElement,
311-
fetchNewRound
312-
};
313-
};
314-
315-
export const bindCardEvent = async (options: {
316-
app: App,
317-
element: Element,
318-
title?: string,
319-
cardsData: ICardData
320-
cardType: TCardType,
321-
id?: string,
322-
dialog?: Dialog,
323-
index?: number
324-
}) => {
325-
// 初始化卡片组件
326-
const initResult = await initCardComponent(options);
327-
const { editor, actionElements, countElement, filterElement, fetchNewRound } = initResult;
328-
let index = initResult.index;
329-
330-
// 绑定点击事件
331304
options.element.addEventListener("click", (event: MouseEvent) => {
332305
const target = event.target as HTMLElement;
333306
let type = "";

0 commit comments

Comments
 (0)