aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-10-31 19:03:45 -0400
committerGitHub <noreply@github.com>2020-10-31 19:03:45 -0400
commit215ef627f13744f4041e4537ab163f71312215e5 (patch)
tree90e9200647d55fb76963b2c60c4e106f53af5ac0 /ext/bg
parent94620f4f224ffec20b774dd75143ce5023c60b83 (diff)
Duplicate scope options (#955)
* Add deck-root duplicate scope option * Implement support for deck-root scope
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/data/options-schema.json2
-rw-r--r--ext/bg/js/anki-note-builder.js21
-rw-r--r--ext/bg/js/anki.js15
-rw-r--r--ext/bg/settings.html1
4 files changed, 36 insertions, 3 deletions
diff --git a/ext/bg/data/options-schema.json b/ext/bg/data/options-schema.json
index d344a7b9..b66577f8 100644
--- a/ext/bg/data/options-schema.json
+++ b/ext/bg/data/options-schema.json
@@ -769,7 +769,7 @@
"duplicateScope": {
"type": "string",
"default": "collection",
- "enum": ["collection", "deck"]
+ "enum": ["collection", "deck", "deck-root"]
},
"fieldTemplates": {
"type": ["string", "null"],
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index f04a6bf7..aa76ecd9 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -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) {
diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js
index 182b4ded..84d81d49 100644
--- a/ext/bg/js/anki.js
+++ b/ext/bg/js/anki.js
@@ -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() {
diff --git a/ext/bg/settings.html b/ext/bg/settings.html
index 1b1e9124..fbd777d8 100644
--- a/ext/bg/settings.html
+++ b/ext/bg/settings.html
@@ -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>