Improve note addability (#1440)
* Add valid field * Add isNoteDataValid function * Update _areDefinitionsAddableForcedValue to return proper valid values * Refactor isAnkiConnected check * Force canAdd to false if not valid
This commit is contained in:
parent
ae92e0b378
commit
6bda81b422
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
/* global
|
/* global
|
||||||
* AnkiConnect
|
* AnkiConnect
|
||||||
|
* AnkiUtil
|
||||||
* AudioDownloader
|
* AudioDownloader
|
||||||
* ClipboardMonitor
|
* ClipboardMonitor
|
||||||
* ClipboardReader
|
* ClipboardReader
|
||||||
@ -456,10 +457,12 @@ class Backend {
|
|||||||
|
|
||||||
for (let i = 0; i < notes.length; ++i) {
|
for (let i = 0; i < notes.length; ++i) {
|
||||||
const note = notes[i];
|
const note = notes[i];
|
||||||
const canAdd = canAddArray[i];
|
let canAdd = canAddArray[i];
|
||||||
const info = {canAdd, noteIds: null};
|
const valid = AnkiUtil.isNoteDataValid(note);
|
||||||
|
if (!valid) { canAdd = false; }
|
||||||
|
const info = {canAdd, valid, noteIds: null};
|
||||||
results.push(info);
|
results.push(info);
|
||||||
if (!canAdd) {
|
if (!canAdd && valid) {
|
||||||
cannotAdd.push({note, info});
|
cannotAdd.push({note, info});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,6 +81,21 @@ class AnkiUtil {
|
|||||||
static cloneFieldMarkerPattern(global) {
|
static cloneFieldMarkerPattern(global) {
|
||||||
return new RegExp(this._markerPattern.source, global ? 'g' : '');
|
return new RegExp(this._markerPattern.source, global ? 'g' : '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether or not a note object is valid.
|
||||||
|
* @param note A note object to check.
|
||||||
|
* @return `true` if the note is valid, `false` otherwise.
|
||||||
|
*/
|
||||||
|
static isNoteDataValid(note) {
|
||||||
|
if (!isObject(note)) { return false; }
|
||||||
|
const {fields, deckName, modelName} = note;
|
||||||
|
return (
|
||||||
|
typeof deckName === 'string' &&
|
||||||
|
typeof modelName === 'string' &&
|
||||||
|
Object.entries(fields).length > 0
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-underscore-dangle
|
// eslint-disable-next-line no-underscore-dangle
|
||||||
|
@ -1058,15 +1058,9 @@ class Display extends EventDispatcher {
|
|||||||
const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji'];
|
const modes = isTerms ? ['term-kanji', 'term-kana'] : ['kanji'];
|
||||||
let states;
|
let states;
|
||||||
try {
|
try {
|
||||||
if (this._options.anki.checkForDuplicates) {
|
|
||||||
const noteContext = this._getNoteContext();
|
const noteContext = this._getNoteContext();
|
||||||
states = await this._areDefinitionsAddable(definitions, modes, noteContext);
|
const {checkForDuplicates} = this._options.anki;
|
||||||
} else {
|
states = await this._areDefinitionsAddable(definitions, modes, noteContext, checkForDuplicates ? null : true);
|
||||||
if (!await yomichan.api.isAnkiConnected()) {
|
|
||||||
throw new Error('Anki not connected');
|
|
||||||
}
|
|
||||||
states = this._areDefinitionsAddableForcedValue(definitions, modes, true);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1408,7 +1402,7 @@ class Display extends EventDispatcher {
|
|||||||
return templates;
|
return templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _areDefinitionsAddable(definitions, modes, context) {
|
async _areDefinitionsAddable(definitions, modes, context, forceCanAddValue) {
|
||||||
const modeCount = modes.length;
|
const modeCount = modes.length;
|
||||||
const notePromises = [];
|
const notePromises = [];
|
||||||
for (const definition of definitions) {
|
for (const definition of definitions) {
|
||||||
@ -1419,7 +1413,16 @@ class Display extends EventDispatcher {
|
|||||||
}
|
}
|
||||||
const notes = await Promise.all(notePromises);
|
const notes = await Promise.all(notePromises);
|
||||||
|
|
||||||
const infos = await yomichan.api.getAnkiNoteInfo(notes);
|
let infos;
|
||||||
|
if (forceCanAddValue !== null) {
|
||||||
|
if (!await yomichan.api.isAnkiConnected()) {
|
||||||
|
throw new Error('Anki not connected');
|
||||||
|
}
|
||||||
|
infos = this._getAnkiNoteInfoForceValue(notes, forceCanAddValue);
|
||||||
|
} else {
|
||||||
|
infos = await yomichan.api.getAnkiNoteInfo(notes);
|
||||||
|
}
|
||||||
|
|
||||||
const results = [];
|
const results = [];
|
||||||
for (let i = 0, ii = infos.length; i < ii; i += modeCount) {
|
for (let i = 0, ii = infos.length; i < ii; i += modeCount) {
|
||||||
results.push(infos.slice(i, i + modeCount));
|
results.push(infos.slice(i, i + modeCount));
|
||||||
@ -1427,16 +1430,11 @@ class Display extends EventDispatcher {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
_areDefinitionsAddableForcedValue(definitions, modes, canAdd) {
|
_getAnkiNoteInfoForceValue(notes, canAdd) {
|
||||||
const results = [];
|
const results = [];
|
||||||
const definitionCount = definitions.length;
|
for (const note of notes) {
|
||||||
const modeCount = modes.length;
|
const valid = AnkiUtil.isNoteDataValid(note);
|
||||||
for (let i = 0; i < definitionCount; ++i) {
|
results.push({canAdd, valid, noteIds: null});
|
||||||
const modeArray = [];
|
|
||||||
for (let j = 0; j < modeCount; ++j) {
|
|
||||||
modeArray.push({canAdd, noteIds: null});
|
|
||||||
}
|
|
||||||
results.push(modeArray);
|
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user