summaryrefslogtreecommitdiff
path: root/ext/js/pages/settings
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2022-05-29 21:24:41 -0400
committerGitHub <noreply@github.com>2022-05-29 21:24:41 -0400
commit331a2e62941e04a4d50a21faefed663a92ddc00a (patch)
tree1212a1e7cd57ea2331fab2101afdc325cb3a4766 /ext/js/pages/settings
parentf3024c50186344aa6a6b09500ea02540463ce5c9 (diff)
Add support for guiEditNote to view notes (#2143)
* Add AnkiConnect.guiEditNote * Update _onApiNoteView to first try guiEditNote * Add setting * Update noteView API * Use setting * Return which mode was used * Update DisplayGenerator * Handle errors in DisplayAnki * Update docs * Add isErrorUnsupportedAction function * Add an allowFallback option to noteView * Disambiguate * Simplify now that preferredMode isn't used * Update settings info * Implement test buttons * Update styles * Update status visibility * Wrap layout * Update description * Update date
Diffstat (limited to 'ext/js/pages/settings')
-rw-r--r--ext/js/pages/settings/anki-controller.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js
index daf09143..d03fa535 100644
--- a/ext/js/pages/settings/anki-controller.js
+++ b/ext/js/pages/settings/anki-controller.js
@@ -71,6 +71,12 @@ class AnkiController {
input.addEventListener('change', this._onAnkiCardPrimaryTypeRadioChange.bind(this), false);
}
+ const testAnkiNoteViewerButtons = document.querySelectorAll('.test-anki-note-viewer-button');
+ const onTestAnkiNoteViewerButtonClick = this._onTestAnkiNoteViewerButtonClick.bind(this);
+ for (const button of testAnkiNoteViewerButtons) {
+ button.addEventListener('click', onTestAnkiNoteViewerButtonClick, false);
+ }
+
document.querySelector('#anki-error-log').addEventListener('click', this._onAnkiErrorLogLinkClick.bind(this));
const options = await this._settingsController.getOptions();
@@ -192,6 +198,10 @@ class AnkiController {
console.log({error: this._ankiError});
}
+ _onTestAnkiNoteViewerButtonClick(e) {
+ this._testAnkiNoteViewerSafe(e.currentTarget.dataset.mode);
+ }
+
_setAnkiCardPrimaryType(ankiCardType, ankiCardMenu) {
if (this._ankiCardPrimary === null) { return; }
this._ankiCardPrimary.dataset.ankiCardType = ankiCardType;
@@ -336,6 +346,54 @@ class AnkiController {
const stringComparer = this._stringComparer;
array.sort((a, b) => stringComparer.compare(a, b));
}
+
+ async _testAnkiNoteViewerSafe(mode) {
+ this._setAnkiNoteViewerStatus(false, null);
+ try {
+ await this._testAnkiNoteViewer(mode);
+ } catch (e) {
+ this._setAnkiNoteViewerStatus(true, e);
+ return;
+ }
+ this._setAnkiNoteViewerStatus(true, null);
+ }
+
+ async _testAnkiNoteViewer(mode) {
+ const queries = [
+ '"よむ" deck:current',
+ '"よむ"',
+ 'deck:current',
+ ''
+ ];
+
+ let noteId = null;
+ for (const query of queries) {
+ const notes = await yomichan.api.findAnkiNotes(query);
+ if (notes.length > 0) {
+ noteId = notes[0];
+ break;
+ }
+ }
+
+ if (noteId === null) {
+ throw new Error('Could not find a note to test with');
+ }
+
+ await yomichan.api.noteView(noteId, mode, false);
+ }
+
+ _setAnkiNoteViewerStatus(visible, error) {
+ const node = document.querySelector('#test-anki-note-viewer-results');
+ if (visible) {
+ const success = (error === null);
+ node.textContent = success ? 'Success!' : error.message;
+ node.dataset.success = `${success}`;
+ } else {
+ node.textContent = '';
+ delete node.dataset.success;
+ }
+ node.hidden = !visible;
+ }
}
class AnkiCardController {