From ee7547649693c377065f3ed9ea6d8696f0f132c1 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 8 Jan 2024 22:12:19 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/10065 --- app/src/card/newCardTab.ts | 15 ++++-------- app/src/card/openCard.ts | 47 +++++++++++++------------------------- app/src/plugin/index.ts | 4 ++++ app/src/types/index.d.ts | 8 ++++++- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/app/src/card/newCardTab.ts b/app/src/card/newCardTab.ts index f5971f80b..8a87dd0b8 100644 --- a/app/src/card/newCardTab.ts +++ b/app/src/card/newCardTab.ts @@ -13,12 +13,7 @@ export const newCardModel = (options: { cardType: TCardType, id: string, title?: string - cardsData?: { - cards: ICard[], - unreviewedCount: number - unreviewedNewCardCount: number - unreviewedOldCardCount: number - }, + cardsData?: ICardData, index?: number, } }) => { @@ -28,7 +23,7 @@ export const newCardModel = (options: { type: "siyuan-card", tab: options.tab, data: options.data, - init() { + async init() { if (options.data.cardsData) { this.element.innerHTML = genCardHTML({ id: this.data.id, @@ -37,7 +32,7 @@ export const newCardModel = (options: { isTab: true, }); - editor = bindCardEvent({ + editor = await bindCardEvent({ app: options.app, element: this.element, id: this.data.id, @@ -56,7 +51,7 @@ export const newCardModel = (options: { rootID: this.data.id, deckID: this.data.id, notebook: this.data.id, - }, (response) => { + }, async (response) => { this.element.innerHTML = genCardHTML({ id: this.data.id, cardType: this.data.cardType, @@ -64,7 +59,7 @@ export const newCardModel = (options: { isTab: true, }); - editor = bindCardEvent({ + editor = await bindCardEvent({ app: options.app, element: this.element, id: this.data.id, diff --git a/app/src/card/openCard.ts b/app/src/card/openCard.ts index 3edc44e68..bbbedbafd 100644 --- a/app/src/card/openCard.ts +++ b/app/src/card/openCard.ts @@ -30,12 +30,7 @@ const genCardCount = (unreviewedNewCardCount: number, unreviewedOldCardCount: nu export const genCardHTML = (options: { id: string, cardType: TCardType, - cardsData: { - cards: ICard[], - unreviewedCount: number - unreviewedNewCardCount: number - unreviewedOldCardCount: number - }, + cardsData: ICardData, isTab: boolean }) => { let iconsHTML: string; @@ -129,24 +124,19 @@ ${window.siyuan.config.flashcard.list ? "card__block--hideli" : ""}" data-type=" `; }; -export const bindCardEvent = (options: { +export const bindCardEvent = async (options: { app: App, element: Element, title?: string, - cardsData: { - cards: ICard[], - unreviewedCount: number - unreviewedNewCardCount: number - unreviewedOldCardCount: number - } + cardsData: ICardData cardType: TCardType, id?: string, dialog?: Dialog, index?: number }) => { - options.app.plugins.forEach(item => { - item.eventBus.emit("update-cards", options); - }); + for (let i = 0; i < options.app.plugins.length; i++) { + options.cardsData = await options.app.plugins[i].updateCards(options.cardsData); + } if (window.siyuan.storage[Constants.LOCAL_FLASHCARD].fullscreen) { fullscreen(options.element.querySelector(".card__main"), options.element.querySelector('[data-type="fullscreen"]')); @@ -199,12 +189,12 @@ export const bindCardEvent = (options: { rootID: filterElement.getAttribute("data-id"), deckID: filterElement.getAttribute("data-id"), notebook: filterElement.getAttribute("data-id"), - }, (treeCards) => { + }, async (treeCards) => { index = 0; options.cardsData = treeCards.data; - options.app.plugins.forEach(item => { - item.eventBus.emit("update-cards", options); - }); + 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, @@ -416,12 +406,12 @@ export const bindCardEvent = (options: { deckID: filterElement.getAttribute("data-id"), notebook: filterElement.getAttribute("data-id"), reviewedCards: options.cardsData.cards - }, (result) => { + }, async (result) => { index = 0; options.cardsData = result.data; - options.app.plugins.forEach(item => { - item.eventBus.emit("update-cards", options); - }); + 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); @@ -459,12 +449,7 @@ export const openCard = (app: App) => { }); }; -export const openCardByData = (app: App, cardsData: { - cards: ICard[], - unreviewedCount: number - unreviewedNewCardCount: number - unreviewedOldCardCount: number -}, cardType: TCardType, id?: string, title?: string) => { +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(); @@ -490,7 +475,7 @@ export const openCardByData = (app: App, cardsData: { }); (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 = bindCardEvent({ + const editor = await bindCardEvent({ app, element: dialog.element, cardsData, diff --git a/app/src/plugin/index.ts b/app/src/plugin/index.ts index 5dd3d337d..d508e6dd1 100644 --- a/app/src/plugin/index.ts +++ b/app/src/plugin/index.ts @@ -88,6 +88,10 @@ export class Plugin { // 卸载 } + public async updateCards(options: ICardData) { + return options; + } + public onLayoutReady() { // 布局加载完成 } diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts index 32bd53b99..3105c2b6f 100644 --- a/app/src/types/index.d.ts +++ b/app/src/types/index.d.ts @@ -64,7 +64,6 @@ type TEventBus = "ws-main" | "sync-start" | "sync-end" | "sync-fail" | "loaded-protyle" | "loaded-protyle-dynamic" | "loaded-protyle-static" | "switch-protyle" | "destroy-protyle" | - "update-cards"| "lock-screen" | "mobile-keyboard-show" | "mobile-keyboard-hide" type TAVCol = @@ -218,6 +217,13 @@ interface ICard { nextDues: IObject } +interface ICardData { + cards: ICard[], + unreviewedCount: number + unreviewedNewCardCount: number + unreviewedOldCardCount: number +} + interface IPluginSettingOption { title: string description?: string