diff options
Diffstat (limited to 'ext/js/display/display-anki.js')
-rw-r--r-- | ext/js/display/display-anki.js | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/ext/js/display/display-anki.js b/ext/js/display/display-anki.js index 4e3692b9..aedf33c7 100644 --- a/ext/js/display/display-anki.js +++ b/ext/js/display/display-anki.js @@ -31,8 +31,9 @@ class DisplayAnki { this._ankiNoteNotificationEventListeners = null; this._ankiTagNotification = null; this._updateAdderButtonsPromise = Promise.resolve(); - this._updateAdderButtonsToken = null; + this._updateDictionaryEntryDetailsToken = null; this._eventListeners = new EventListenerCollection(); + this._dictionaryEntryDetails = null; this._noteContext = null; this._checkForDuplicates = false; this._suspendNewCards = false; @@ -66,7 +67,8 @@ class DisplayAnki { } cleanupEntries() { - this._updateAdderButtonsToken = null; + this._updateDictionaryEntryDetailsToken = null; + this._dictionaryEntryDetails = null; this._hideAnkiNoteErrors(false); } @@ -81,7 +83,7 @@ class DisplayAnki { } setupEntriesComplete() { - this._updateAdderButtons(); + this._updateDictionaryEntryDetails(); } async getLogData(dictionaryEntry) { @@ -258,47 +260,45 @@ class DisplayAnki { return {type, term, reading}; } - async _updateAdderButtons() { + async _updateDictionaryEntryDetails() { const {dictionaryEntries} = this._display; const token = {}; - this._updateAdderButtonsToken = token; - await this._updateAdderButtonsPromise; - if (this._updateAdderButtonsToken !== token) { return; } + this._updateDictionaryEntryDetailsToken = token; + if (this._updateAdderButtonsPromise !== null) { + await this._updateAdderButtonsPromise; + } + if (this._updateDictionaryEntryDetailsToken !== token) { return; } const {promise, resolve} = deferPromise(); try { this._updateAdderButtonsPromise = promise; - - let states; - try { - states = await this._areDictionaryEntriesAddable(dictionaryEntries); - } catch (e) { - return; - } - - if (this._updateAdderButtonsToken !== token) { return; } - - this._updateAdderButtons2(states); + const dictionaryEntryDetails = await this._getDictionaryEntryDetails(dictionaryEntries); + if (this._updateDictionaryEntryDetailsToken !== token) { return; } + this._dictionaryEntryDetails = dictionaryEntryDetails; + this._updateAdderButtons(); } finally { resolve(); + if (this._updateAdderButtonsPromise === promise) { + this._updateAdderButtonsPromise = null; + } } } - _updateAdderButtons2(states) { + _updateAdderButtons() { const displayTags = this._displayTags; - for (let i = 0, ii = states.length; i < ii; ++i) { + const dictionaryEntryDetails = this._dictionaryEntryDetails; + for (let i = 0, ii = dictionaryEntryDetails.length; i < ii; ++i) { let noteId = null; - for (const {mode, canAdd, noteIds, noteInfos} of states[i]) { + for (const {mode, canAdd, noteIds, noteInfos, ankiError} of dictionaryEntryDetails[i]) { const button = this._adderButtonFind(i, mode); - if (button === null) { - continue; + if (button !== null) { + button.disabled = !canAdd; + button.hidden = (ankiError !== null); } if (Array.isArray(noteIds) && noteIds.length > 0) { noteId = noteIds[0]; } - button.disabled = !canAdd; - button.hidden = false; if (displayTags !== 'never' && Array.isArray(noteInfos)) { this._setupTagsIndicator(i, noteInfos); @@ -459,7 +459,7 @@ class DisplayAnki { return templates; } - async _areDictionaryEntriesAddable(dictionaryEntries) { + async _getDictionaryEntryDetails(dictionaryEntries) { const forceCanAddValue = (this._checkForDuplicates ? null : true); const fetchAdditionalInfo = (this._displayTags !== 'never'); @@ -481,13 +481,19 @@ class DisplayAnki { const notes = noteInfoList.map(({note}) => note); let infos; - if (forceCanAddValue !== null) { - if (!await yomichan.api.isAnkiConnected()) { - throw new Error('Anki not connected'); + let ankiError = null; + try { + if (forceCanAddValue !== null) { + if (!await yomichan.api.isAnkiConnected()) { + throw new Error('Anki not connected'); + } + infos = this._getAnkiNoteInfoForceValue(notes, forceCanAddValue); + } else { + infos = await yomichan.api.getAnkiNoteInfo(notes, fetchAdditionalInfo); } - infos = this._getAnkiNoteInfoForceValue(notes, forceCanAddValue); - } else { - infos = await yomichan.api.getAnkiNoteInfo(notes, fetchAdditionalInfo); + } catch (e) { + infos = this._getAnkiNoteInfoForceValue(notes, false); + ankiError = e; } const results = []; @@ -498,7 +504,7 @@ class DisplayAnki { while (index >= results.length) { results.push([]); } - results[index].push({mode, note, errors, canAdd, valid, noteIds, noteInfos}); + results[index].push({mode, note, errors, canAdd, valid, noteIds, noteInfos, ankiError}); } return results; } |