Duplicate scope options (#955)

* Add deck-root duplicate scope option

* Implement support for deck-root scope
This commit is contained in:
toasted-nutbread 2020-10-31 19:03:45 -04:00 committed by GitHub
parent 94620f4f22
commit 215ef627f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 3 deletions

View File

@ -769,7 +769,7 @@
"duplicateScope": {
"type": "string",
"default": "collection",
"enum": ["collection", "deck"]
"enum": ["collection", "deck", "deck-root"]
},
"fieldTemplates": {
"type": ["string", "null"],

View File

@ -44,6 +44,14 @@ class AnkiNoteBuilder {
await this._injectMedia(anki, definition, fields, mode, audioDetails, screenshotDetails, clipboardDetails);
}
let duplicateScopeDeckName = null;
let duplicateScopeCheckChildren = false;
if (duplicateScope === 'deck-root') {
duplicateScope = 'deck';
duplicateScopeDeckName = this.getRootDeckName(deck);
duplicateScopeCheckChildren = true;
}
const fieldEntries = Object.entries(fields);
const noteFields = {};
const note = {
@ -51,7 +59,13 @@ class AnkiNoteBuilder {
tags,
deckName: deck,
modelName: model,
options: {duplicateScope}
options: {
duplicateScope,
duplicateScopeOptions: {
deckName: duplicateScopeDeckName,
checkChildren: duplicateScopeCheckChildren
}
}
};
const data = this._createNoteData(definition, mode, context, resultOutputMode, compactGlossaries);
@ -81,6 +95,11 @@ class AnkiNoteBuilder {
return false;
}
getRootDeckName(deckName) {
const index = deckName.indexOf('::');
return index >= 0 ? deckName.substring(0, index) : deckName;
}
// Private
_createNoteData(definition, mode, context, resultOutputMode, compactGlossaries) {

View File

@ -92,13 +92,26 @@ class AnkiConnect {
if (!this._enabled) { return []; }
await this._checkVersion();
const actions = notes.map((note) => {
let query = (duplicateScope === 'deck' ? `"deck:${this._escapeQuery(note.deckName)}" ` : '');
let query = '';
switch (duplicateScope) {
case 'deck':
query = `"deck:${this._escapeQuery(note.deckName)}" `;
break;
case 'deck-root':
query = `"deck:${this._escapeQuery(this.getRootDeckName(note.deckName))}" `;
break;
}
query += this._fieldsToQuery(note.fields);
return {action: 'findNotes', params: {query}};
});
return await this._invoke('multi', {actions});
}
getRootDeckName(deckName) {
const index = deckName.indexOf('::');
return index >= 0 ? deckName.substring(0, index) : deckName;
}
// Private
async _checkVersion() {

View File

@ -926,6 +926,7 @@
<select class="form-control" id="duplicate-scope" data-setting="anki.duplicateScope">
<option value="collection">Collection</option>
<option value="deck">Deck</option>
<option value="deck-root">Deck root</option>
</select>
</div>