aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-09-10 16:05:17 -0400
committerGitHub <noreply@github.com>2020-09-10 16:05:17 -0400
commit892f7ed3b8d4a97dd7fd8086a404c5e8ea1355fb (patch)
treeccc4551b2dbef20193c85a946e61d8774873cad2 /ext/mixed
parent3dd4822ab3e1312336065f70cf31b87dac39473b (diff)
Anki note api functions (#802)
* Assign duplicateScope to a variable * Add api.addAnkiNote * Add api.getAnkiNoteInfo, update returned data format
Diffstat (limited to 'ext/mixed')
-rw-r--r--ext/mixed/js/api.js8
-rw-r--r--ext/mixed/js/display.js24
2 files changed, 24 insertions, 8 deletions
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) {