diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-02-24 22:23:40 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-24 22:23:40 -0500 |
commit | 6bda81b4220cf6057f24e5b08aa9a62e4e535bc2 (patch) | |
tree | 62a73f8270e0e044f18f8a1d395fec7e86573154 /ext/js/display | |
parent | ae92e0b3781e27d54cbac2570ba2a1b8a6b11999 (diff) |
Improve note addability (#1440)
* Add valid field
* Add isNoteDataValid function
* Update _areDefinitionsAddableForcedValue to return proper valid values
* Refactor isAnkiConnected check
* Force canAdd to false if not valid
Diffstat (limited to 'ext/js/display')
-rw-r--r-- | ext/js/display/display.js | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/ext/js/display/display.js b/ext/js/display/display.js index 0e029748..30a8e294 100644 --- a/ext/js/display/display.js +++ b/ext/js/display/display.js @@ -1058,15 +1058,9 @@ class Display extends EventDispatcher { const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji']; let states; try { - if (this._options.anki.checkForDuplicates) { - const noteContext = this._getNoteContext(); - states = await this._areDefinitionsAddable(definitions, modes, noteContext); - } else { - if (!await yomichan.api.isAnkiConnected()) { - throw new Error('Anki not connected'); - } - states = this._areDefinitionsAddableForcedValue(definitions, modes, true); - } + const noteContext = this._getNoteContext(); + const {checkForDuplicates} = this._options.anki; + states = await this._areDefinitionsAddable(definitions, modes, noteContext, checkForDuplicates ? null : true); } catch (e) { return; } @@ -1408,7 +1402,7 @@ class Display extends EventDispatcher { return templates; } - async _areDefinitionsAddable(definitions, modes, context) { + async _areDefinitionsAddable(definitions, modes, context, forceCanAddValue) { const modeCount = modes.length; const notePromises = []; for (const definition of definitions) { @@ -1419,7 +1413,16 @@ class Display extends EventDispatcher { } const notes = await Promise.all(notePromises); - const infos = await yomichan.api.getAnkiNoteInfo(notes); + let infos; + 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); + } + const results = []; for (let i = 0, ii = infos.length; i < ii; i += modeCount) { results.push(infos.slice(i, i + modeCount)); @@ -1427,16 +1430,11 @@ class Display extends EventDispatcher { return results; } - _areDefinitionsAddableForcedValue(definitions, modes, canAdd) { + _getAnkiNoteInfoForceValue(notes, canAdd) { const results = []; - const definitionCount = definitions.length; - const modeCount = modes.length; - for (let i = 0; i < definitionCount; ++i) { - const modeArray = []; - for (let j = 0; j < modeCount; ++j) { - modeArray.push({canAdd, noteIds: null}); - } - results.push(modeArray); + for (const note of notes) { + const valid = AnkiUtil.isNoteDataValid(note); + results.push({canAdd, valid, noteIds: null}); } return results; } |