Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix anki template field marker menu #725

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ext/js/dom/html-template-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,28 @@ export class HtmlTemplateCollection {
* @throws {Error}
*/
instantiate(name) {
const template = this._templates.get(name);
if (typeof template === 'undefined') { throw new Error(`Failed to find template: ${name}`); }
const {firstElementChild} = template.content;
const {firstElementChild} = this.getTemplateContent(name);
if (firstElementChild === null) { throw new Error(`Failed to find template content element: ${name}`); }
return /** @type {T} */ (document.importNode(firstElementChild, true));
}

/**
* @param {string} name
* @returns {DocumentFragment}
* @throws {Error}
*/
instantiateFragment(name) {
return document.importNode(this.getTemplateContent(name), true);
}

/**
* @param {string} name
* @returns {DocumentFragment}
* @throws {Error}
*/
getTemplateContent(name) {
const template = this._templates.get(name);
if (typeof template === 'undefined') { throw new Error(`Failed to find template: ${name}`); }
const {content} = template;
return document.importNode(content, true);
return template.content;
}

/**
Expand Down
27 changes: 17 additions & 10 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,32 @@ 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) {
markers.push(...getStandardFieldMarkers(type));
}
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');
Expand Down
5 changes: 5 additions & 0 deletions ext/js/pages/settings/settings-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
Loading