diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-09 21:28:27 -0500 |
---|---|---|
committer | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2019-12-13 23:11:56 -0500 |
commit | 233ed4d0fb32291d9ba3f9b0a358496bf1093fb4 (patch) | |
tree | 0a94e59e7882792939e0183a772c56bafe8a2fb9 /ext/bg/js/backend.js | |
parent | c9cd29889dc72ce4b35ab16bfc7294ec3d008697 (diff) |
Move apiDefinitionsAddable implementation into Backend
Diffstat (limited to 'ext/bg/js/backend.js')
-rw-r--r-- | ext/bg/js/backend.js | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 726a1714..77dc0d33 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -339,8 +339,50 @@ class Backend { return this.anki.addNote(note); } - _onApiDefinitionsAddable({definitions, modes, optionsContext}) { - return apiDefinitionsAddable(definitions, modes, optionsContext); + async _onApiDefinitionsAddable({definitions, modes, optionsContext}) { + const options = await this.getOptions(optionsContext); + const states = []; + + try { + const notes = []; + for (const definition of definitions) { + for (const mode of modes) { + const note = await dictNoteFormat(definition, mode, options); + notes.push(note); + } + } + + const cannotAdd = []; + const results = await this.anki.canAddNotes(notes); + for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) { + const state = {}; + for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) { + const index = resultBase + modeOffset; + const result = results[index]; + const info = {canAdd: result}; + state[modes[modeOffset]] = info; + if (!result) { + cannotAdd.push([notes[index], info]); + } + } + + states.push(state); + } + + if (cannotAdd.length > 0) { + const noteIdsArray = await this.anki.findNoteIds(cannotAdd.map((e) => e[0])); + for (let i = 0, ii = Math.min(cannotAdd.length, noteIdsArray.length); i < ii; ++i) { + const noteIds = noteIdsArray[i]; + if (noteIds.length > 0) { + cannotAdd[i][1].noteId = noteIds[0]; + } + } + } + } catch (e) { + // NOP + } + + return states; } _onApiNoteView({noteId}) { |