From 892f7ed3b8d4a97dd7fd8086a404c5e8ea1355fb Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 10 Sep 2020 16:05:17 -0400 Subject: Anki note api functions (#802) * Assign duplicateScope to a variable * Add api.addAnkiNote * Add api.getAnkiNoteInfo, update returned data format --- ext/mixed/js/api.js | 8 ++++++++ ext/mixed/js/display.js | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'ext/mixed') diff --git a/ext/mixed/js/api.js b/ext/mixed/js/api.js index 7dc77fb6..d9569187 100644 --- a/ext/mixed/js/api.js +++ b/ext/mixed/js/api.js @@ -77,6 +77,14 @@ const api = (() => { return this._invoke('kanjiFind', {text, optionsContext}); } + addAnkiNote(note) { + return this._invoke('addAnkiNote', {note}); + } + + getAnkiNoteInfo(notes, duplicateScope) { + return this._invoke('getAnkiNoteInfo', {notes, duplicateScope}); + } + definitionAdd(definition, mode, context, ownerFrameId, optionsContext) { return this._invoke('definitionAdd', {definition, mode, context, ownerFrameId, optionsContext}); } diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 6fdab46b..7dd5920a 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -889,10 +889,15 @@ class Display extends EventDispatcher { async _setContentTermsOrKanjiUpdateAdderButtons(token, isTerms, definitions) { const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji']; - const states = await this._getDefinitionsAddable(definitions, modes); + let states; + try { + states = await this._getDefinitionsAddable(definitions, modes); + } catch (e) { + return; + } if (this._setContentToken !== token) { return; } - this._updateAdderButtons(states); + this._updateAdderButtons(states, modes); } _setContentExtensionUnloaded() { @@ -953,19 +958,22 @@ class Display extends EventDispatcher { this._navigationHeader.dataset.hasNext = `${!!next}`; } - _updateAdderButtons(states) { - for (let i = 0; i < states.length; ++i) { + _updateAdderButtons(states, modes) { + for (let i = 0, ii = states.length; i < ii; ++i) { + const infos = states[i]; let noteId = null; - for (const [mode, info] of Object.entries(states[i])) { + for (let j = 0, jj = infos.length; j < jj; ++j) { + const {canAdd, noteIds} = infos[j]; + const mode = modes[j]; const button = this._adderButtonFind(i, mode); if (button === null) { continue; } - if (!info.canAdd && noteId === null && info.noteId) { - noteId = info.noteId; + if (Array.isArray(noteIds) && noteIds.length > 0) { + noteId = noteIds[0]; } - button.classList.toggle('disabled', !info.canAdd); + button.classList.toggle('disabled', !canAdd); button.classList.remove('pending'); } if (noteId !== null) { -- cgit v1.2.3