diff --git a/src/components/popups/popupDict/component.tsx b/src/components/popups/popupDict/component.tsx index 2bc98ae9..577736c3 100644 --- a/src/components/popups/popupDict/component.tsx +++ b/src/components/popups/popupDict/component.tsx @@ -89,10 +89,18 @@ class PopupDict extends React.Component { return; } } - this.handleDict(originalText); - this.handleRecordHistory(originalText, this.props.originalSentence || ""); + const dictText = await this.handleDict(originalText); + this.handleRecordHistory( + originalText, + this.props.originalSentence || "", + dictText || "" + ); } - handleRecordHistory = async (text: string, sentence: string) => { + handleRecordHistory = async ( + text: string, + sentence: string, + dictText: string = "" + ) => { let bookKey = this.props.currentBook.key; let bookLocation = ConfigService.getObjectConfig( bookKey, @@ -104,7 +112,7 @@ class PopupDict extends React.Component { let word = new DictHistory(bookKey, text, chapter, sentence); await DatabaseService.saveRecord(word, "words"); this.syncWordToEudic(text, sentence); - this.syncWordToAnki(text, sentence, bookName, chapter); + this.syncWordToAnki(text, sentence, bookName, chapter, dictText); }; syncWordToEudic = async (text: string, sentence: string) => { @@ -174,7 +182,8 @@ class PopupDict extends React.Component { text: string, sentence: string, bookName: string, - chapter: string + chapter: string, + dictText: string = "" ) => { if (ConfigService.getReaderConfig("isEnableAnkiSync") !== "yes") return; try { @@ -203,12 +212,19 @@ class PopupDict extends React.Component { if (!existingModels.includes(MODEL_NAME)) { await ankiRequest("createModel", { modelName: MODEL_NAME, - inOrderFields: ["Word", "Sentence", "Book", "Chapter"], + inOrderFields: [ + "Word", + "Sentence", + "Book", + "Chapter", + "Definition", + ], cardTemplates: [ { Name: "Card 1", - Front: "{{Word}}

{{Sentence}}", - Back: "{{FrontSide}}
{{Book}}
{{Chapter}}", + Front: + "{{Word}}

{{Sentence}}

{{Book}}
{{Chapter}}", + Back: "{{Definition}}", }, ], }); @@ -230,6 +246,7 @@ class PopupDict extends React.Component { Sentence: sentence || "", Book: bookName || "", Chapter: chapter || "", + Definition: dictText || "", }, options: { allowDuplicate: false }, tags: [], @@ -240,7 +257,7 @@ class PopupDict extends React.Component { } }; - handleDict = async (text: string) => { + handleDict = async (text: string): Promise => { let dictText = ""; let isFullAnalysis = true; try { @@ -249,7 +266,7 @@ class PopupDict extends React.Component { let plugin = this.props.plugins.find( (item) => item.key === "custom-ai-dict-plugin" ); - if (!plugin) return; + if (!plugin) return ""; let targetLang = this.state.dictTarget || ConfigService.getReaderConfig("dictTarget") || @@ -287,7 +304,7 @@ class PopupDict extends React.Component { this.stopUpdateInterval(); this.aiTextAccumulator = ""; this.setState({ isAiWaiting: false, dictText: " " }); - return; + return ""; } else if ( this.state.dictService && this.state.dictService !== "official-ai-dict-plugin" @@ -295,7 +312,7 @@ class PopupDict extends React.Component { let plugin = this.props.plugins.find( (item) => item.key === this.state.dictService ); - if (!plugin) return; + if (!plugin) return ""; let dictFunc = plugin.script; // eslint-disable-next-line no-eval eval(dictFunc); @@ -336,6 +353,7 @@ class PopupDict extends React.Component { if (!doc) continue; doc.getSelection()?.empty(); } + return ""; } else { this.setState( { @@ -358,6 +376,7 @@ class PopupDict extends React.Component { ) { this.handleDictionaryStream(text, isFullAnalysis); } + return dictText; } catch (error) { toast.error( this.props.t("Error happened") + @@ -368,6 +387,7 @@ class PopupDict extends React.Component { this.setState({ dictText: this.props.t("Error happened"), }); + return ""; } }; handleDictionaryStream = async (text: string, isFullAnalysis: boolean) => {