aboutsummaryrefslogtreecommitdiff
path: root/ext/js/templates
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-03-31 20:07:11 -0400
committerGitHub <noreply@github.com>2021-03-31 20:07:11 -0400
commit5d7309ed5474a9fb67b9cae1b1176bc10bde6115 (patch)
tree3dd2f58e8b365298d5a75a3a360899803b67e0b8 /ext/js/templates
parentda612bbdd7c5ac15ed64497666f6415c525c823f (diff)
Log Anki data (#1579)
* Remove unused modifierPost * Add _getModifier * Add _getModifiedData * Add getModifiedData * Add getRenderingData * Update logging to also log anki note data * Fix dangling comma
Diffstat (limited to 'ext/js/templates')
-rw-r--r--ext/js/templates/template-renderer-frame-api.js12
-rw-r--r--ext/js/templates/template-renderer-proxy.js5
-rw-r--r--ext/js/templates/template-renderer.js42
3 files changed, 40 insertions, 19 deletions
diff --git a/ext/js/templates/template-renderer-frame-api.js b/ext/js/templates/template-renderer-frame-api.js
index 4936a2af..6eebc199 100644
--- a/ext/js/templates/template-renderer-frame-api.js
+++ b/ext/js/templates/template-renderer-frame-api.js
@@ -19,7 +19,8 @@ class TemplateRendererFrameApi {
constructor(templateRenderer) {
this._templateRenderer = templateRenderer;
this._windowMessageHandlers = new Map([
- ['render', {async: true, handler: this._onRender.bind(this)}]
+ ['render', {async: true, handler: this._onRender.bind(this)}],
+ ['getModifiedData', {async: true, handler: this._onGetModifiedData.bind(this)}]
]);
}
@@ -57,6 +58,11 @@ class TemplateRendererFrameApi {
return await this._templateRenderer.render(template, data, type);
}
+ async _onGetModifiedData({data, type}) {
+ const result = await this._templateRenderer.getModifiedData(data, type);
+ return this._clone(result);
+ }
+
_errorToJson(error) {
try {
if (error !== null && typeof error === 'object') {
@@ -75,4 +81,8 @@ class TemplateRendererFrameApi {
hasValue: true
};
}
+
+ _clone(value) {
+ return JSON.parse(JSON.stringify(value));
+ }
}
diff --git a/ext/js/templates/template-renderer-proxy.js b/ext/js/templates/template-renderer-proxy.js
index 6a49832b..aba45e6c 100644
--- a/ext/js/templates/template-renderer-proxy.js
+++ b/ext/js/templates/template-renderer-proxy.js
@@ -30,6 +30,11 @@ class TemplateRendererProxy {
return await this._invoke('render', {template, data, type});
}
+ async getModifiedData(data, type) {
+ await this._prepareFrame();
+ return await this._invoke('getModifiedData', {data, type});
+ }
+
// Private
async _prepareFrame() {
diff --git a/ext/js/templates/template-renderer.js b/ext/js/templates/template-renderer.js
index f72740d5..a7a4a842 100644
--- a/ext/js/templates/template-renderer.js
+++ b/ext/js/templates/template-renderer.js
@@ -29,8 +29,8 @@ class TemplateRenderer {
this._dataTypes = new Map();
}
- registerDataType(name, {modifier=null, modifierPost=null}) {
- this._dataTypes.set(name, {modifier, modifierPost});
+ registerDataType(name, {modifier=null}) {
+ this._dataTypes.set(name, {modifier});
}
async render(template, data, type) {
@@ -47,32 +47,38 @@ class TemplateRenderer {
cache.set(template, instance);
}
- let modifier = null;
- let modifierPost = null;
- if (typeof type === 'string') {
- const typeInfo = this._dataTypes.get(type);
- if (typeof typeInfo !== 'undefined') {
- ({modifier, modifierPost} = typeInfo);
- }
- }
-
try {
- if (typeof modifier === 'function') {
- data = modifier(data);
- }
-
+ data = this._getModifiedData(data, type);
this._stateStack = [new Map()];
return instance(data).trim();
} finally {
this._stateStack = null;
+ }
+ }
- if (typeof modifierPost === 'function') {
- modifierPost(data);
+ async getModifiedData(data, type) {
+ return this._getModifiedData(data, type);
+ }
+
+ // Private
+
+ _getModifier(type) {
+ if (typeof type === 'string') {
+ const typeInfo = this._dataTypes.get(type);
+ if (typeof typeInfo !== 'undefined') {
+ return typeInfo.modifier;
}
}
+ return null;
}
- // Private
+ _getModifiedData(data, type) {
+ const modifier = this._getModifier(type);
+ if (typeof modifier === 'function') {
+ data = modifier(data);
+ }
+ return data;
+ }
_updateCacheSize(maxSize) {
const cache = this._cache;