diff --git a/src/client/ui/ui-inspector-field.js b/src/client/ui/ui-inspector-field.js index f03115d6..516729fc 100644 --- a/src/client/ui/ui-inspector-field.js +++ b/src/client/ui/ui-inspector-field.js @@ -7,39 +7,39 @@ var UiWidget = require('./ui-widget'), ace = require('brace'), ScriptVm = require('../widgets/scripts/script-vm'), scriptGlobals = ScriptVm.globals, - editors = {} + editors = {}, editorModes = { + javascript: require('brace/mode/javascript') + } -// require('brace/mode/html') -require('brace/mode/javascript') -// require('brace/ext/searchbox') -// require('brace/ext/keybinding_menu') - -for (var name of ['script', 'draw', 'touch']) { - editors[name] = ace.edit('editor-' + name) - editors[name].getSession().setMode('ace/mode/javascript') - editors[name].setOption('autoScrollEditorIntoView', true) - editors[name].setOption('maxLines', 40) - editors[name].setOption('blockScrolling', Infinity) - editors[name].commands.bindKeys({'ctrl-l':null, 'ctrl-f': null,'cmd-l':null, 'cmd-f': null}) - editors[name].renderer.setScrollMargin(4, 4, 0, 4) - editors[name].element = DOM.get('#editor-' + name)[0] - editors[name].element.classList.add('ace_dark') - editors[name].$mouseHandler.setOption('dragEnabled', false) - editors[name].textarea = DOM.get('#editor-' + name + ' .ace_text-input')[0] - editors[name].textarea.name = name - editors[name].textarea._ace = true - editors[name].setHighlightActiveLine(false) - editors[name].setHighlightGutterLine(false) - editors[name].getSession().$worker.send('setOptions', [{ - asi: true, // no semicolon - esversion: 6, - strict: 'implied', - node: false, - browser: true, - validthis: true, - undef: true, - globals: {} - }]) +function createEditor(name, language) { + var el = html`
` + var editor = editors[name] = ace.edit(el) + if (editorModes[language]) editor.getSession().setMode('ace/mode/' + language) + editor.setOption('autoScrollEditorIntoView', true) + editor.setOption('maxLines', 40) + editor.setOption('blockScrolling', Infinity) + editor.commands.bindKeys({'ctrl-l':null, 'ctrl-f': null,'cmd-l':null, 'cmd-f': null}) + editor.renderer.setScrollMargin(4, 4, 0, 4) + editor.element = el + editor.element.classList.add('ace_dark') + editor.$mouseHandler.setOption('dragEnabled', false) + editor.textarea = DOM.get(el, '.ace_text-input')[0] + editor.textarea.name = name + editor.textarea._ace = true + editor.setHighlightActiveLine(false) + editor.setHighlightGutterLine(false) + if (language === 'javascript') { + editor.getSession().$worker.send('setOptions', [{ + asi: true, // no semicolon + esversion: 6, + strict: 'implied', + node: false, + browser: true, + validthis: true, + undef: true, + globals: {} + }]) + } } @@ -166,7 +166,9 @@ class UiInspectorField extends UiWidget { } - if (['script', 'touch', 'draw'].includes(this.name)) { + if (this.default.editor) { + this.container.classList.add('has-editor') + if (!editors[this.name]) createEditor(this.name, this.default.editor) let editor = editors[this.name] input.style.display = 'none' input._ace_input = editor.textarea @@ -193,26 +195,28 @@ class UiInspectorField extends UiWidget { }) }) - let globals = {} - if (this.name === 'script') { - if (this.widget.type !== 'script' || this.widget.getProp('event') === 'value') { - globals = {id: true, value: true, touch: true} - } else if (this.widget.getProp('event') === 'keyboard') { - globals = {type: true, key: true, code: true, ctrl: true, shift: true, alt: true, meta: true} + if (this.default.editor === 'javascript') { + let globals = {} + if (this.name === 'script') { + if (this.widget.type !== 'script' || this.widget.getProp('event') === 'value') { + globals = {id: true, value: true, touch: true} + } else if (this.widget.getProp('event') === 'keyboard') { + globals = {type: true, key: true, code: true, ctrl: true, shift: true, alt: true, meta: true} + } + } else if (this.name === 'draw') { + globals = {value: true, height: true, width: true, ctx: true, cssVars: true} + } else if (this.name === 'touch') { + globals = {value: true, height: true, width: true, event: true} } - } else if (this.name === 'draw') { - globals = {value: true, height: true, width: true, ctx: true, cssVars: true} - } else if (this.name === 'touch') { - globals = {value: true, height: true, width: true, event: true} - } - globals.locals = true - globals.console = true - globals.JS = true - globals.this = false + globals.locals = true + globals.console = true + globals.JS = true + globals.this = false - editor.getSession().$worker.send('changeOptions', [{ - globals: {...scriptGlobals, ...globals} - }]) + editor.getSession().$worker.send('changeOptions', [{ + globals: {...scriptGlobals, ...globals} + }]) + } this.container.appendChild(editor.element) setTimeout(()=>{ diff --git a/src/client/ui/ui-inspector.js b/src/client/ui/ui-inspector.js index fae161b6..305ac083 100644 --- a/src/client/ui/ui-inspector.js +++ b/src/client/ui/ui-inspector.js @@ -148,13 +148,15 @@ class UiInspector extends UiWidget { if (field) field.classList.add('last-child') field = html`
${props[categoryName][propName]}
` } else { + var def = defaults[widget.props.type][categoryName][propName] + if (field && def.editor) field.classList.add('last-child') var first = !field || field.classList.contains('separator') field = new UiInspectorField({ parent: this, widget: widget, name: propName, value: widget.props[propName], - default: defaults[widget.props.type][categoryName][propName], + default: def, tabIndex: tabIndex++ }).container if (first) field.classList.add('first-child') @@ -238,7 +240,7 @@ class UiInspector extends UiWidget { if (event.keyCode === 13 && ((!event.shiftKey && !event.target._ace) || - (event.shiftKey && event.target._ace)) + (event.ctrlKey && event.target._ace)) ) { event.preventDefault() diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index 8cd72758..246ce54e 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -137,7 +137,7 @@ class Widget extends EventEmitter { bypass: {type: 'boolean', value: false, help: 'Set to `true` to prevent the widget from sending any osc message'} }, scripting: { - script: {type: 'script', value: '', help: ['Script executed whenever the widget\'s value updates. See documentation.']}, + script: {type: 'script', value: '', editor: 'javascript', help: ['Script executed whenever the widget\'s value updates. See documentation.']}, } } diff --git a/src/client/widgets/pads/canvas.js b/src/client/widgets/pads/canvas.js index 02fa3d9c..9ee644c6 100644 --- a/src/client/widgets/pads/canvas.js +++ b/src/client/widgets/pads/canvas.js @@ -26,10 +26,10 @@ class CanvasWidget extends Canvas { 'If set to `true`, `draw` will be called at each frame, otherwise it will be called only when the widget is touched and when it receives a value.', 'Can be a number between 1 and 60 to specify the framerate (default: 30 fps).' ]}, - touch: {type: 'string', value: '', help: [ + touch: {type: 'string', value: '', editor:'javascript', help: [ 'Script executed when the widget is touched. See documentation.', ]}, - draw: {type: 'string', value: '', help: [ + draw: {type: 'string', value: '', editor:'javascript', help: [ 'Script executed when the widget is redrawn. See documentation.' ]}, } diff --git a/src/client/widgets/scripts/script.js b/src/client/widgets/scripts/script.js index ffaa0f51..3eee8bc5 100644 --- a/src/client/widgets/scripts/script.js +++ b/src/client/widgets/scripts/script.js @@ -14,28 +14,26 @@ class Script extends Widget { static defaults() { - return super.defaults().extend({ - widget: { - visible: null, - interaction: null - }, - geometry: null, - style: null, - scripting: { - event: {type: 'string', value: 'value', choices: ['value', 'keyboard', 'once'], help: 'Define which events trigger the script\'s execution.'}, + var defaults = super.defaults() + delete defaults.widget.visible + delete defaults.widget.interaction + delete defaults.geometry + delete defaults.style + defaults.scripting = { + event: {type: 'string', value: 'value', choices: ['value', 'keyboard', 'once'], help: 'Define which events trigger the script\'s execution.'}, + script: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives the defined event. See documentation.'}, - _separator: 'event: keyboard', + _separator: 'event: keyboard', - keyBinding: {type: 'string|array', value: '', help: [ - 'Key combo `string` or `array` of strings (see KeyboardJS documentation).', - 'If the editor is enabled, some keys / combos will not work.' - ]}, - keyRepeat: {type: 'boolean', value: true, help: 'Set to `false` to prevent keydown repeats when holding the key combo pressed'}, - keyType: {type: 'string', value: 'keydown', choices: ['keydown', 'keyup', 'both'], help: 'Determines which key event trigger the script\'s execution'}, + keyBinding: {type: 'string|array', value: '', help: [ + 'Key combo `string` or `array` of strings (see KeyboardJS documentation).', + 'If the editor is enabled, some keys / combos will not work.' + ]}, + keyRepeat: {type: 'boolean', value: true, help: 'Set to `false` to prevent keydown repeats when holding the key combo pressed'}, + keyType: {type: 'string', value: 'keydown', choices: ['keydown', 'keyup', 'both'], help: 'Determines which key event trigger the script\'s execution'}, - script: {type: 'script', value: '', help: 'Script executed whenever the widget receives the defined event. See documentation.'}, - } - }) + } + return defaults } diff --git a/src/html/client.html b/src/html/client.html index d6813fc4..7d359459 100644 --- a/src/html/client.html +++ b/src/html/client.html @@ -56,11 +56,6 @@
- diff --git a/src/scss/ui/inspector.scss b/src/scss/ui/inspector.scss index 9b0221ec..00c997b8 100644 --- a/src/scss/ui/inspector.scss +++ b/src/scss/ui/inspector.scss @@ -41,8 +41,18 @@ osc-inspector-field { border-right: none; } + &.has-editor { + flex-direction: column;height: auto; + > label { + border:1rem solid var(--color-background); + border-bottom : none; + width: 100%; + } + &:after { + content:none; + } + } .ace_editor { - flex:1; min-height: $field-height; } > textarea, > input { @@ -91,7 +101,7 @@ osc-inspector-field { } osc-inspector-field .ace_editor { - background-color: var(--color-background); + color: var(--color-text); font-size: inherit; background: var(--color-background); @@ -237,7 +247,7 @@ osc-inspector-field .ace_editor { border-right: 1rem solid rgba(white, 0.05) } .ace_tooltip { - left:$padding!important + left:$padding!important; } // .ace_search { // position: fixed; @@ -366,7 +376,6 @@ osc-inspector-category { } } - osc-inspector-checkbox { --widget-padding: 0;