widgets: add html property to allow inserting custom content in widgets (label, value, etc) and style it with the css property.
This commit is contained in:
parent
7fd59ae71c
commit
bee2e6e5be
4 changed files with 69 additions and 19 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 <a href="https://openstagecontrol.ammd.net/docs/customization/css-tips/">documentation</a>.',
|
||||
'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`<div class="html"></div>`
|
||||
newHtml.innerHTML = extraHtml
|
||||
morph(this.extraHtml, newHtml)
|
||||
}
|
||||
|
||||
} else if (extraHtml) {
|
||||
var newHtml = html`<div class="html"></div>`
|
||||
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',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
' <img>: 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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue