From 5e87a490f78888717c58491959745431580bd64e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 11 Jan 2021 18:37:07 -0500 Subject: Refactor display anki functions (#1224) * Refactor note adding/viewing functions * Move _addDefinition * Update where anki field templates are assigned * Update _createNote to not include options/templates arguments * Simplify getAnkiNoteInfo to not require duplicateScope --- ext/bg/js/anki.js | 15 +++++++++++++-- ext/bg/js/backend.js | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'ext/bg') diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index ba5b4161..05c07ce2 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -103,12 +103,12 @@ class AnkiConnect { return await this._invoke('storeMediaFile', {filename: fileName, data: dataBase64}); } - async findNoteIds(notes, duplicateScope) { + async findNoteIds(notes) { if (!this._enabled) { return []; } await this._checkVersion(); const actions = notes.map((note) => { let query = ''; - switch (duplicateScope) { + switch (this._getDuplicateScopeFromNote(note)) { case 'deck': query = `"deck:${this._escapeQuery(note.deckName)}" `; break; @@ -178,4 +178,15 @@ class AnkiConnect { const key = fieldNames[0]; return `"${key.toLowerCase()}:${this._escapeQuery(fields[key])}"`; } + + _getDuplicateScopeFromNote(note) { + const {options} = note; + if (typeof options === 'object' && options !== null) { + const {duplicateScope} = options; + if (typeof duplicateScope !== 'undefined') { + return duplicateScope; + } + } + return null; + } } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index db83d0b7..6410b0fc 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -447,7 +447,7 @@ class Backend { return await this._anki.addNote(note); } - async _onApiGetAnkiNoteInfo({notes, duplicateScope}) { + async _onApiGetAnkiNoteInfo({notes}) { const results = []; const cannotAdd = []; const canAddArray = await this._anki.canAddNotes(notes); @@ -464,7 +464,7 @@ class Backend { if (cannotAdd.length > 0) { const cannotAddNotes = cannotAdd.map(({note}) => note); - const noteIdsArray = await this._anki.findNoteIds(cannotAddNotes, duplicateScope); + const noteIdsArray = await this._anki.findNoteIds(cannotAddNotes); for (let i = 0, ii = Math.min(cannotAdd.length, noteIdsArray.length); i < ii; ++i) { const noteIds = noteIdsArray[i]; if (noteIds.length > 0) { -- cgit v1.2.3