Add duplicateScope: 'deck' option (#476)

* Add duplicateScope: 'deck' option

* Add option to control duplicate scope

* Use duplicateScope for findNoteIds

* Update location of quotes
This commit is contained in:
toasted-nutbread 2020-04-27 18:10:59 -04:00 committed by GitHub
parent 48c7010f4e
commit 0956634d61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 10 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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])}"`;
}
}

View File

@ -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) {

View File

@ -201,6 +201,7 @@ function profileOptionsCreateDefaults() {
screenshot: {format: 'png', quality: 92},
terms: {deck: '', model: '', fields: {}},
kanji: {deck: '', model: '', fields: {}},
duplicateScope: 'collection',
fieldTemplates: null
}
};

View File

@ -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);

View File

@ -820,6 +820,14 @@
<input type="text" id="interface-server" class="form-control">
</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">