diff options
author | Eloy Robillard <eloy.robillard@gmail.com> | 2024-03-18 12:19:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-18 11:19:27 +0000 |
commit | 7ee76d708934adeef06479f7757beb22c6c01d14 (patch) | |
tree | c03ad4227e8a71939bb5efddffe57fa21a75b043 /ext/js/comm | |
parent | 4fe881d68d4c1182bee2e78a559c2064aaf48b0d (diff) |
Add an option to allow both viewing and adding duplicates (#693)
* Detect duplicates when checking if can add note
* Display the stacked add buttons
Diffstat (limited to 'ext/js/comm')
-rw-r--r-- | ext/js/comm/anki-connect.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/ext/js/comm/anki-connect.js b/ext/js/comm/anki-connect.js index 0bf38bda..a763f394 100644 --- a/ext/js/comm/anki-connect.js +++ b/ext/js/comm/anki-connect.js @@ -140,6 +140,17 @@ export class AnkiConnect { } /** + * @param {import('anki').Note[]} notes + * @returns {Promise<({ canAdd: true } | { canAdd: false, error: string })[]>} + */ + async canAddNotesWithErrorDetail(notes) { + if (!this._enabled) { return []; } + await this._checkVersion(); + const result = await this._invoke('canAddNotesWithErrorDetail', {notes}); + return this._normalizeCanAddNotesWithErrorDetailArray(result, notes.length); + } + + /** * @param {import('anki').NoteId[]} noteIds * @returns {Promise<(?import('anki').NoteInfo)[]>} */ @@ -581,6 +592,52 @@ export class AnkiConnect { /** * @param {unknown} result + * @param {number} expectedCount + * @returns {import('anki-connect.js').CanAddResult[]} + * @throws {Error} + */ + _normalizeCanAddNotesWithErrorDetailArray(result, expectedCount) { + if (!Array.isArray(result)) { + throw this._createUnexpectedResultError('array', result, ''); + } + if (expectedCount !== result.length) { + throw this._createError(`Unexpected result array size: expected ${expectedCount}, received ${result.length}`, result); + } + /** @type {import('anki-connect.js').CanAddResult[]} */ + const result2 = []; + for (let i = 0; i < expectedCount; ++i) { + const item = /** @type {unknown} */ (result[i]); + if (item === null || typeof item !== 'object') { + throw this._createError(`Unexpected result type at index ${i}: expected object, received ${this._getTypeName(item)}`, result); + } + + const {canAdd, error} = /** @type {{[key: string]: unknown}} */ (item); + if (typeof canAdd !== 'boolean') { + throw this._createError(`Unexpected result type at index ${i}, field canAdd: expected boolean, received ${this._getTypeName(canAdd)}`, result); + } + + if (canAdd && typeof error !== 'undefined') { + throw this._createError(`Unexpected result type at index ${i}, field error: expected undefined, received ${this._getTypeName(error)}`, result); + } + if (!canAdd && typeof error !== 'string') { + throw this._createError(`Unexpected result type at index ${i}, field error: expected string, received ${this._getTypeName(error)}`, result); + } + + if (canAdd) { + result2.push({canAdd}); + } else if (typeof error === 'string') { + const item2 = { + canAdd, + error + }; + result2.push(item2); + } + } + return result2; + } + + /** + * @param {unknown} result * @returns {(?import('anki').NoteInfo)[]} * @throws {Error} */ |