From 6bda81b4220cf6057f24e5b08aa9a62e4e535bc2 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 24 Feb 2021 22:23:40 -0500 Subject: 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 --- ext/js/data/anki-util.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'ext/js/data') diff --git a/ext/js/data/anki-util.js b/ext/js/data/anki-util.js index fc081ddc..c815ccc7 100644 --- a/ext/js/data/anki-util.js +++ b/ext/js/data/anki-util.js @@ -81,6 +81,21 @@ class AnkiUtil { static cloneFieldMarkerPattern(global) { 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 -- cgit v1.2.3