diff --git a/CHANGELOG.md b/CHANGELOG.md index b59ef75c..88715bc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - widgets - encoder: expose angle in script (as `locals.angle`) + - new textarea widget (multi line input) ## 1.9.14 diff --git a/src/client/widgets/basics/textarea.js b/src/client/widgets/basics/textarea.js new file mode 100644 index 00000000..cb11a304 --- /dev/null +++ b/src/client/widgets/basics/textarea.js @@ -0,0 +1,113 @@ +var Widget = require('../common/widget'), + {deepCopy} = require('../../utils'), + html = require('nanohtml') + +class TextArea extends Widget { + + static description() { + + return 'Text area (multi line input).' + + } + + static defaults() { + + return super.defaults().extend({ + style: { + // _separator_input_style: 'Textarea style', + }, + class_specific: { + + }, + }) + + } + + constructor(options) { + + super({...options, html: html` + + + + `}) + + this.value = '' + this.stringValue = '' + this.focused = false + this.validation = null + this.input = DOM.get(this.widget, 'textarea')[0] + + if (this.getProp('align') === 'left') this.widget.classList.add('left') + if (this.getProp('align') === 'right') this.widget.classList.add('right') + + + if (this.getProp('interaction')) { + + this.input.setAttribute('tabindex', 0) + this.input.addEventListener('focus', this.focus.bind(this)) + this.input.addEventListener('blur', (e)=>{ + this.blur(true) + }) + + this.input.addEventListener('keydown', (e)=>{ + if (e.keyCode === 13 && !e.shiftKey) { + // enter + e.preventDefault() + this.blur() + } + else if (e.keyCode === 27) { + // esc + this.blur(false) + this.input.value = this.stringValue + } + }) + + } + + } + + focus() { + + if (this.focused) return + this.focused = true + + } + + blur(change=true) { + + if (!this.focused) return + this.focused = false + + this.input.blur() + + if (change) this.inputChange() + + } + + inputChange() { + + this.setValue(this.input.value, {sync:true, send:true}) + + } + + + setValue(v, options={} ) { + + this.value = String(v) + + if (this.value === '' || this.value === null) this.value = this.getProp('default') + + if (this.input.value !== this.value) this.input.value = this.value + + if (options.send && !options.fromSync) this.sendValue() + if (options.sync) this.changed(options) + + } + +} + +TextArea.dynamicProps = TextArea.prototype.constructor.dynamicProps + .filter(x=>x !== 'interaction') + + +module.exports = TextArea diff --git a/src/client/widgets/index.js b/src/client/widgets/index.js index 99716eda..d42b66c5 100644 --- a/src/client/widgets/index.js +++ b/src/client/widgets/index.js @@ -6,6 +6,7 @@ module.exports.widgets = { dropdown: require('./basics/dropdown'), menu: require('./basics/menu'), input: require('./basics/input'), + textarea: require('./basics/textarea'), file: require('./basics/file'), // containers @@ -54,7 +55,7 @@ module.exports.widgets = { } module.exports.categories = { - 'Basics':['button', 'switch', 'dropdown', 'menu', 'input', 'file'], + 'Basics':['button', 'switch', 'dropdown', 'menu', 'input', 'textarea', 'file'], 'Containers':['panel', 'modal', 'clone', 'fragment', 'matrix', 'keyboard', 'patchbay'], 'Frames':['frame', 'svg', 'html', 'image'], 'Graphs':['plot','eq','visualizer'], diff --git a/src/scss/widgets/basics.scss b/src/scss/widgets/basics.scss index 1ef2d586..0ca35bb3 100644 --- a/src/scss/widgets/basics.scss +++ b/src/scss/widgets/basics.scss @@ -629,11 +629,47 @@ } +.textarea-container { + + --alpha-fill-off: 0.05; + --widget-padding: 2rem; + + inner { + position: relative; + @include pseudo-stroke(); + @include pseudo-fill(0.05); + justify-content: center; + align-items: center; + display: flex; + padding: var(--widget-padding); + text-align: center; + &.right { + text-align: right; + } + &.left { + text-align: left; + } + padding: var(--widget-padding); + + + } + textarea { + background: none; + width: 100%; + height: 100%; + resize: none; + padding: var(--widget-padding); + } +} + .editor-enabled { + .textarea-container textarea, .input-container inner { // prevent input focus when selecting pointer-events: none; } + .clone-container.editing .textarea-container textarea, + .textarea-container.editing textarea, .clone-container.editing .input-container inner, .input-container.editing inner { // prevent input focus when selecting