Use dependency injection for apiTemplateRender
This commit is contained in:
parent
69cce49b0d
commit
7ac1c843a9
@ -16,10 +16,9 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*global apiTemplateRender*/
|
|
||||||
|
|
||||||
class AnkiNoteBuilder {
|
class AnkiNoteBuilder {
|
||||||
constructor() {
|
constructor({renderTemplate}) {
|
||||||
|
this._renderTemplate = renderTemplate;
|
||||||
this._markers = new Set([
|
this._markers = new Set([
|
||||||
'audio',
|
'audio',
|
||||||
'character',
|
'character',
|
||||||
@ -100,7 +99,7 @@ class AnkiNoteBuilder {
|
|||||||
}
|
}
|
||||||
data.marker = marker;
|
data.marker = marker;
|
||||||
try {
|
try {
|
||||||
return await apiTemplateRender(templates, data);
|
return await this._renderTemplate(templates, data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (errors) { errors.push(e); }
|
if (errors) { errors.push(e); }
|
||||||
return `{${marker}-render-error}`;
|
return `{${marker}-render-error}`;
|
||||||
|
@ -17,10 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function apiTemplateRender(template, data) {
|
|
||||||
return _apiInvoke('templateRender', {data, template});
|
|
||||||
}
|
|
||||||
|
|
||||||
function _apiInvoke(action, params={}) {
|
function _apiInvoke(action, params={}) {
|
||||||
const data = {action, params};
|
const data = {action, params};
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@ -31,7 +31,7 @@ class Backend {
|
|||||||
this.anki = new AnkiNull();
|
this.anki = new AnkiNull();
|
||||||
this.mecab = new Mecab();
|
this.mecab = new Mecab();
|
||||||
this.clipboardMonitor = new ClipboardMonitor({getClipboard: this._onApiClipboardGet.bind(this)});
|
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.options = null;
|
||||||
this.optionsSchema = null;
|
this.optionsSchema = null;
|
||||||
this.defaultAnkiFieldTemplates = null;
|
this.defaultAnkiFieldTemplates = null;
|
||||||
@ -507,7 +507,7 @@ class Backend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _onApiTemplateRender({template, data}) {
|
async _onApiTemplateRender({template, data}) {
|
||||||
return handlebarsRenderDynamic(template, data);
|
return this._renderTemplate(template, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
async _onApiCommandExec({command, params}) {
|
async _onApiCommandExec({command, params}) {
|
||||||
@ -811,6 +811,10 @@ class Backend {
|
|||||||
definition.screenshotFileName = filename;
|
definition.screenshotFileName = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _renderTemplate(template, data) {
|
||||||
|
return handlebarsRenderDynamic(template, data);
|
||||||
|
}
|
||||||
|
|
||||||
static _getTabUrl(tab) {
|
static _getTabUrl(tab) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => {
|
chrome.tabs.sendMessage(tab.id, {action: 'getUrl'}, {frameId: 0}, (response) => {
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
/*global getOptionsContext, getOptionsMutable, settingsSaveOptions
|
/*global getOptionsContext, getOptionsMutable, settingsSaveOptions
|
||||||
ankiGetFieldMarkers, ankiGetFieldMarkersHtml
|
ankiGetFieldMarkers, ankiGetFieldMarkersHtml
|
||||||
apiOptionsGet, apiTermsFind, apiGetDefaultAnkiFieldTemplates,
|
apiOptionsGet, apiTermsFind, apiGetDefaultAnkiFieldTemplates, apiTemplateRender
|
||||||
AnkiNoteBuilder*/
|
AnkiNoteBuilder*/
|
||||||
|
|
||||||
function onAnkiFieldTemplatesReset(e) {
|
function onAnkiFieldTemplatesReset(e) {
|
||||||
@ -93,7 +93,7 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i
|
|||||||
const options = await apiOptionsGet(optionsContext);
|
const options = await apiOptionsGet(optionsContext);
|
||||||
let templates = options.anki.fieldTemplates;
|
let templates = options.anki.fieldTemplates;
|
||||||
if (typeof templates !== 'string') { templates = await apiGetDefaultAnkiFieldTemplates(); }
|
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);
|
result = await ankiNoteBuilder.formatField(field, definition, mode, options, templates, exceptions);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user