From 9cdda89ef8acc85c808ba84db360dbdd0440f104 Mon Sep 17 00:00:00 2001 From: bordun Date: Mon, 26 Oct 2020 17:15:42 +0100 Subject: [PATCH] widgets: add `html` property to allow inserting custom content in widgets (label, value, etc) and style it with the `css` property. --- CHANGELOG.md | 5 +++ src/client/widgets/common/widget.js | 52 +++++++++++++++++++++++++++++ src/client/widgets/frames/html.js | 23 +++---------- src/scss/widgets.scss | 8 ++++- 4 files changed, 69 insertions(+), 19 deletions(-) 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', - '  : src, width, height', - ]}, - }, osc: { decimals: null, typeTags: null, @@ -56,11 +45,13 @@ class Html extends StaticProperties(Widget, {bypass: true}) { if (!this.getProp('border')) this.container.classList.add('noborder') this.frame = DOM.get(this.widget, '.frame')[0] - if (this.getProp('html') !== '') this.updateHtml() + if (this.getProp('html') !== '') this.updateHtmlLegacy() } - updateHtml(){ + updateHtml(){} + + updateHtmlLegacy(){ var newHtml = this.frame.cloneNode(false) @@ -77,7 +68,7 @@ class Html extends StaticProperties(Widget, {bypass: true}) { switch (propName) { case 'html': - this.updateHtml() + this.updateHtmlLegacy() return } @@ -87,8 +78,4 @@ class Html extends StaticProperties(Widget, {bypass: true}) { } -Html.dynamicProps = Html.prototype.constructor.dynamicProps.concat( - 'html' -) - module.exports = Html diff --git a/src/scss/widgets.scss b/src/scss/widgets.scss index 98fade9d..967bbfa9 100644 --- a/src/scss/widgets.scss +++ b/src/scss/widgets.scss @@ -5,7 +5,7 @@ vertical-align:top; display: inline-flex; - flex-direction: row; + flex-direction: column; align-items: stretch; max-width: 100%; @@ -70,6 +70,12 @@ } } + .html { + text-align: center; + line-height: 30rem; + min-height: 0; + } + }