Support for HTML values in widget hbs templates

This commit is contained in:
Robin Ward 2017-09-29 09:55:53 -04:00
parent f6c484881b
commit 0caf6a0f7d
4 changed files with 18 additions and 4 deletions

View File

@ -99,7 +99,6 @@ export default Ember.Component.extend(AddArchetypeClass, Scrolling, {
// this happens after route exit, stuff could have trickled in // this happens after route exit, stuff could have trickled in
this.appEvents.trigger('header:hide-topic'); this.appEvents.trigger('header:hide-topic');
this.appEvents.off('post:highlight'); this.appEvents.off('post:highlight');
}, },
@observes('Discourse.hasFocus') @observes('Discourse.hasFocus')

View File

@ -4,7 +4,7 @@ export default class RawHtml {
} }
init() { init() {
const $html = $(this.html); const $html = $.parseHTML(this.html);
this.decorate($html); this.decorate($html);
return $html[0]; return $html[0];
} }
@ -20,3 +20,8 @@ export default class RawHtml {
} }
RawHtml.prototype.type = 'Widget'; RawHtml.prototype.type = 'Widget';
// TODO: Improve how helpers are registered for vdom compliation
if (typeof Discourse !== "undefined") {
Discourse.__widget_helpers.rawHtml = RawHtml;
}

View File

@ -64,7 +64,12 @@ function mustacheValue(node, state) {
return `__iN("${icon}")`; return `__iN("${icon}")`;
break; break;
default: default:
return `${resolve(path)}`; if (node.escaped) {
return `${resolve(path)}`;
} else {
state.helpersUsed.rawHtml = true;
return `new __rH({ html: '<span>' + ${resolve(path)} + '</span>'})`;
}
break; break;
} }
} }
@ -180,7 +185,10 @@ function compile(template) {
let imports = ''; let imports = '';
if (compiler.state.helpersUsed.iconNode) { if (compiler.state.helpersUsed.iconNode) {
imports = "var __iN = Discourse.__widget_helpers.iconNode; "; imports += "var __iN = Discourse.__widget_helpers.iconNode; ";
}
if (compiler.state.helpersUsed.rawHtml) {
imports += "var __rH = Discourse.__widget_helpers.rawHtml; ";
} }
return `function(attrs, state) { ${imports}var _r = [];\n${code}\nreturn _r; }`; return `function(attrs, state) { ${imports}var _r = [];\n${code}\nreturn _r; }`;

View File

@ -1,5 +1,7 @@
template = <<~HBS template = <<~HBS
{{attach widget="widget-name" attrs=attrs}} {{attach widget="widget-name" attrs=attrs}}
{{a}}
{{{htmlValue}}}
{{#if state.category}} {{#if state.category}}
{{attach widget="category-display" attrs=(hash category=state.category)}} {{attach widget="category-display" attrs=(hash category=state.category)}}
{{/if}} {{/if}}