diff --git a/CHANGELOG.md b/CHANGELOG.md index 54255156..4184e622 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 1.6.0 + +- widgets + - all: add `html` property to allow inserting custom content in widgets (label, value, etc) and style it with the `css` property. + ## 1.5.4 - bug fixes diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index bb5f9de1..b449085a 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -10,6 +10,9 @@ var EventEmitter = require('../../events/event-emitter'), OscReceiver = require('./osc-receiver'), {deepCopy, deepEqual, isJSON} = require('../../utils'), html = require('nanohtml'), + raw = require('nanohtml/raw'), + morph = require('nanomorph'), + sanitizeHtml = require('sanitize-html'), updateWidget = ()=>{}, Script @@ -75,6 +78,17 @@ class Widget extends EventEmitter { alphaFillOn: {type: 'number', value: 'auto', help: 'Fill color opacity (on).'}, lineWidth: {type: 'number', value: 'auto', help: 'Stroke width.'}, padding: {type: 'number', value: 'auto', help: 'Inner spacing.'}, + html: {type: 'string', value: '', help: [ + 'Custom html content to be inserted in the widget (before the widget\'s content).', + 'The code is automatically wrapped in <div class="html"></div>', + 'Allowed HTML tags:', + ' h1-6, blockquote, p, a, ul, ol, nl, li,', + ' b, i, strong, em, strike, code, hr, br, div,', + ' table, thead, img, caption, tbody, tr, th, td, pre', + 'Allowed attributes:', + ' <*>: class, style, title', + ' <img>: src, width, height', + ]}, css: {type: 'string', value: '', help: [ 'CSS rules. See documentation.', 'Available css variables:', @@ -226,6 +240,9 @@ class Widget extends EventEmitter { this.setCssVariables() this.setVisibility() + this.extraHtml = null + if (this.getProp('html')) this.updateHtml() + } contains(widget) { @@ -800,6 +817,11 @@ class Widget extends EventEmitter { this.container.classList.toggle('no-interaction', !this.getProp('interaction')) return + case 'html': + this.updateHtml() + resize.check(this.container) + return + case 'css': { this.setContainerStyles(['css']) var re = /width|height|display|margin|padding|flex/ @@ -988,6 +1010,35 @@ class Widget extends EventEmitter { } + updateHtml(){ + + var extraHtml = this.getProp('html') ? sanitizeHtml(this.getProp('html'), { + allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img', 'h1', 'h2']).filter(x=>x!=='iframe'), + allowedAttributes: { + '*': [ 'title', 'class', 'style', 'type'], + 'img': [ 'src' , 'title', 'class', 'style', 'width', 'height'] + } + }) : null + + if (this.extraHtml) { + if (!extraHtml) { + this.container.removeChild(this.extraHtml) + this.extraHtml = null + } else { + var newHtml = html`
` + newHtml.innerHTML = extraHtml + morph(this.extraHtml, newHtml) + } + + } else if (extraHtml) { + var newHtml = html`` + newHtml.innerHTML = extraHtml + this.container.insertBefore(newHtml, this.widget) + this.extraHtml = newHtml + } + + } + isVisible() { return this.getProp('visible') && this.parent.isVisible() @@ -1065,6 +1116,7 @@ Widget.dynamicProps = [ 'alphaFillOn', 'padding', 'lineWidth', + 'html', 'css', 'value', diff --git a/src/client/widgets/frames/html.js b/src/client/widgets/frames/html.js index 81bb5ccd..9bd74ae7 100644 --- a/src/client/widgets/frames/html.js +++ b/src/client/widgets/frames/html.js @@ -24,17 +24,6 @@ class Html extends StaticProperties(Widget, {bypass: true}) { static defaults() { return super.defaults().extend({ - class_specific: { - html: {type: 'string', value: '', help: [ - 'Allowed HTML tags:', - ' h1-6, blockquote, p, a, ul, ol, nl, li,', - ' b, i, strong, em, strike, code, hr, br, div,', - ' table, thead, img, caption, tbody, tr, th, td, pre', - 'Allowed attributes:', - ' <*>: class, style, title', - '