From 7ac1c843a92cbefd0a625f06b5093217b585f7cf Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 7 Mar 2020 15:20:45 -0500 Subject: Use dependency injection for apiTemplateRender --- ext/bg/js/anki-note-builder.js | 7 +++---- ext/bg/js/api.js | 4 ---- ext/bg/js/backend.js | 8 ++++++-- ext/bg/js/settings/anki-templates.js | 4 ++-- 4 files changed, 11 insertions(+), 12 deletions(-) (limited to 'ext/bg/js') diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js index f7555280..be39ff43 100644 --- a/ext/bg/js/anki-note-builder.js +++ b/ext/bg/js/anki-note-builder.js @@ -16,10 +16,9 @@ * along with this program. If not, see . */ -/*global apiTemplateRender*/ - class AnkiNoteBuilder { - constructor() { + constructor({renderTemplate}) { + this._renderTemplate = renderTemplate; this._markers = new Set([ 'audio', 'character', @@ -100,7 +99,7 @@ class AnkiNoteBuilder { } data.marker = marker; try { - return await apiTemplateRender(templates, data); + return await this._renderTemplate(templates, data); } catch (e) { if (errors) { errors.push(e); } return `{${marker}-render-error}`; diff --git a/ext/bg/js/api.js b/ext/bg/js/api.js index 4e5d81db..9a05023e 100644 --- a/ext/bg/js/api.js +++ b/ext/bg/js/api.js @@ -17,10 +17,6 @@ */ -function apiTemplateRender(template, data) { - return _apiInvoke('templateRender', {data, template}); -} - function _apiInvoke(action, params={}) { const data = {action, params}; return new Promise((resolve, reject) => { diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js index 929281da..6e5235ed 100644 --- a/ext/bg/js/backend.js +++ b/ext/bg/js/backend.js @@ -31,7 +31,7 @@ class Backend { this.anki = new AnkiNull(); this.mecab = new Mecab(); this.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)}); - this.ankiNoteBuilder = new AnkiNoteBuilder(); + this.ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: this._renderTemplate.bind(this)}); this.options = null; this.optionsSchema = null; this.defaultAnkiFieldTemplates = null; @@ -507,7 +507,7 @@ class Backend { } async _onApiTemplateRender({template, data}) { - return handlebarsRenderDynamic(template, data); + return this._renderTemplate(template, data); } async _onApiCommandExec({command, params}) { @@ -811,6 +811,10 @@ class Backend { definition.screenshotFileName = filename; } + async _renderTemplate(template, data) { + return handlebarsRenderDynamic(template, data); + } + static _getTabUrl(tab) { return new Promise((resolve) => { chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => { diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js index 32a990f9..b1665048 100644 --- a/ext/bg/js/settings/anki-templates.js +++ b/ext/bg/js/settings/anki-templates.js @@ -18,7 +18,7 @@ /*global getOptionsContext, getOptionsMutable, settingsSaveOptions ankiGetFieldMarkers, ankiGetFieldMarkersHtml -apiOptionsGet, apiTermsFind, apiGetDefaultAnkiFieldTemplates, +apiOptionsGet, apiTermsFind, apiGetDefaultAnkiFieldTemplates, apiTemplateRender AnkiNoteBuilder*/ function onAnkiFieldTemplatesReset(e) { @@ -93,7 +93,7 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i const options = await apiOptionsGet(optionsContext); let templates = options.anki.fieldTemplates; if (typeof templates !== 'string') { templates = await apiGetDefaultAnkiFieldTemplates(); } - const ankiNoteBuilder = new AnkiNoteBuilder(); + const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: apiTemplateRender}); result = await ankiNoteBuilder.formatField(field, definition, mode, options, templates, exceptions); } } catch (e) { -- cgit v1.2.3