diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-04-27 18:10:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-27 18:10:59 -0400 |
commit | 0956634d61ef2b6202645ec4b502239573c2e743 (patch) | |
tree | e53dcf42db38c57d0e48cb0970574a3945951372 | |
parent | 48c7010f4ea8daafd30e5650625c377affa0cecd (diff) |
Add duplicateScope: 'deck' option (#476)
* Add duplicateScope: 'deck' option
* Add option to control duplicate scope
* Use duplicateScope for findNoteIds
* Update location of quotes
-rw-r--r-- | ext/bg/data/options-schema.json | 6 | ||||
-rw-r--r-- | ext/bg/js/anki-note-builder.js | 5 | ||||
-rw-r--r-- | ext/bg/js/anki.js | 15 | ||||
-rw-r--r-- | ext/bg/js/backend.js | 2 | ||||
-rw-r--r-- | ext/bg/js/options.js | 1 | ||||
-rw-r--r-- | ext/bg/js/settings/main.js | 2 | ||||
-rw-r--r-- | ext/bg/settings.html | 8 |
7 files changed, 29 insertions, 10 deletions
diff --git a/ext/bg/data/options-schema.json b/ext/bg/data/options-schema.json index 4f9e694d..8622f16b 100644 --- a/ext/bg/data/options-schema.json +++ b/ext/bg/data/options-schema.json @@ -492,6 +492,7 @@ "screenshot", "terms", "kanji", + "duplicateScope", "fieldTemplates" ], "properties": { @@ -587,6 +588,11 @@ } } }, + "duplicateScope": { + "type": "string", + "default": "collection", + "enum": ["collection", "deck"] + }, "fieldTemplates": { "type": ["string", "null"], "default": null diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index 9bab095d..dc1e9427 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -32,7 +32,10 @@ class AnkiNoteBuilder { fields: {}, tags, deckName: modeOptions.deck, - modelName: modeOptions.model + modelName: modeOptions.model, + options: { + duplicateScope: options.anki.duplicateScope + } }; for (const [fieldName, fieldValue] of modeOptionsFieldEntries) { diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index 38823431..0d38837c 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -87,15 +87,14 @@ class AnkiConnect { return await this._invoke('storeMediaFile', {filename, data: dataBase64}); } - async findNoteIds(notes) { + async findNoteIds(notes, duplicateScope) { if (!this._enabled) { return []; } await this._checkVersion(); - const actions = notes.map((note) => ({ - action: 'findNotes', - params: { - query: `deck:"${this._escapeQuery(note.deckName)}" ${this._fieldsToQuery(note.fields)}` - } - })); + const actions = notes.map((note) => { + let query = (duplicateScope === 'deck' ? `"deck:${this._escapeQuery(note.deckName)}" ` : ''); + query += this._fieldsToQuery(note.fields); + return {action: 'findNotes', params: {query}}; + }); return await this._invoke('multi', {actions}); } @@ -132,6 +131,6 @@ class AnkiConnect { } const key = fieldNames[0]; - return `${key.toLowerCase()}:"${this._escapeQuery(fields[key])}"`; + return `"${key.toLowerCase()}:${this._escapeQuery(fields[key])}"`; } } diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 3c47b14e..dd1fd8e9 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -555,7 +555,7 @@ class Backend { } if (cannotAdd.length > 0) { - const noteIdsArray = await this.anki.findNoteIds(cannotAdd.map((e) => e[0])); + const noteIdsArray = await this.anki.findNoteIds(cannotAdd.map((e) => e[0]), options.anki.duplicateScope); for (let i = 0, ii = Math.min(cannotAdd.length, noteIdsArray.length); i < ii; ++i) { const noteIds = noteIdsArray[i]; if (noteIds.length > 0) { diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index da26b628..8e1814ed 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -201,6 +201,7 @@ function profileOptionsCreateDefaults() { screenshot: {format: 'png', quality: 92}, terms: {deck: '', model: '', fields: {}}, kanji: {deck: '', model: '', fields: {}}, + duplicateScope: 'collection', fieldTemplates: null } }; diff --git a/ext/bg/js/settings/main.js b/ext/bg/js/settings/main.js index f03cc631..cf75d629 100644 --- a/ext/bg/js/settings/main.js +++ b/ext/bg/js/settings/main.js @@ -131,6 +131,7 @@ async function formRead(options) { options.anki.tags = utilBackgroundIsolate($('#card-tags').val().split(/[,; ]+/)); options.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10); options.anki.server = $('#interface-server').val(); + options.anki.duplicateScope = $('#duplicate-scope').val(); options.anki.screenshot.format = $('#screenshot-format').val(); options.anki.screenshot.quality = parseInt($('#screenshot-quality').val(), 10); @@ -212,6 +213,7 @@ async function formWrite(options) { $('#card-tags').val(options.anki.tags.join(' ')); $('#sentence-detection-extent').val(options.anki.sentenceExt); $('#interface-server').val(options.anki.server); + $('#duplicate-scope').val(options.anki.duplicateScope); $('#screenshot-format').val(options.anki.screenshot.format); $('#screenshot-quality').val(options.anki.screenshot.quality); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index f0236193..b6120b5f 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -821,6 +821,14 @@ </div> <div class="form-group options-advanced"> + <label for="duplicate-scope">Duplicate scope</label> + <select class="form-control" id="duplicate-scope"> + <option value="collection">Collection</option> + <option value="deck">Deck</option> + </select> + </div> + + <div class="form-group options-advanced"> <label for="screenshot-format">Screenshot format</label> <select class="form-control" id="screenshot-format"> <option value="png">PNG</option> |