summaryrefslogtreecommitdiff
path: root/ext/js/pages
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-02-24 23:30:58 -0500
committerGitHub <noreply@github.com>2024-02-25 04:30:58 +0000
commit0e005f2ca6a3d9e572ed18b51c720d8bea907118 (patch)
tree697931975a5caf68e848f63f4a18cc4a4fe75372 /ext/js/pages
parent9b5de0d2d4cb224751c57bdae6558a046351c2f4 (diff)
Fix anki template field marker menu (#725)
* Fix HTML collection * Expose templates * Fix menu setup * Warn
Diffstat (limited to 'ext/js/pages')
-rw-r--r--ext/js/pages/settings/anki-controller.js27
-rw-r--r--ext/js/pages/settings/settings-controller.js5
2 files changed, 22 insertions, 10 deletions
diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js
index edd40516..d166d3f0 100644
--- a/ext/js/pages/settings/anki-controller.js
+++ b/ext/js/pages/settings/anki-controller.js
@@ -281,15 +281,25 @@ export class AnkiController {
/** */
_setupFieldMenus() {
- /** @type {[types: import('dictionary').DictionaryEntryType[], selector: string][]} */
+ /** @type {[types: import('dictionary').DictionaryEntryType[], templateName: string][]} */
const fieldMenuTargets = [
- [['term'], '#anki-card-terms-field-menu-template'],
- [['kanji'], '#anki-card-kanji-field-menu-template'],
- [['term', 'kanji'], '#anki-card-all-field-menu-template']
+ [['term'], 'anki-card-terms-field-menu'],
+ [['kanji'], 'anki-card-kanji-field-menu'],
+ [['term', 'kanji'], 'anki-card-all-field-menu']
];
- for (const [types, selector] of fieldMenuTargets) {
- const element = /** @type {HTMLTemplateElement} */ (document.querySelector(selector));
- if (element === null) { continue; }
+ const {templates} = this._settingsController;
+ for (const [types, templateName] of fieldMenuTargets) {
+ const templateContent = templates.getTemplateContent(templateName);
+ if (templateContent === null) {
+ log.warn(new Error(`Failed to set up menu "${templateName}": element not found`));
+ continue;
+ }
+
+ const container = templateContent.querySelector('.popup-menu-body');
+ if (container === null) {
+ log.warn(new Error(`Failed to set up menu "${templateName}": body not found`));
+ return;
+ }
let markers = [];
for (const type of types) {
@@ -297,9 +307,6 @@ export class AnkiController {
}
markers = [...new Set(markers)];
- const container = element.content.querySelector('.popup-menu-body');
- if (container === null) { return; }
-
const fragment = document.createDocumentFragment();
for (const marker of markers) {
const option = document.createElement('button');
diff --git a/ext/js/pages/settings/settings-controller.js b/ext/js/pages/settings/settings-controller.js
index 282479de..a9f3a026 100644
--- a/ext/js/pages/settings/settings-controller.js
+++ b/ext/js/pages/settings/settings-controller.js
@@ -66,6 +66,11 @@ export class SettingsController extends EventDispatcher {
this._setProfileIndex(value, true);
}
+ /** @type {HtmlTemplateCollection} */
+ get templates() {
+ return this._templates;
+ }
+
/** */
async prepare() {
await this._templates.loadFromFiles(['/templates-settings.html']);