inspector: code editor for script/touch/draw properties

This commit is contained in:
jean-emmanuel 2021-12-13 12:40:09 +01:00
parent 0a1b30416f
commit 1058cb551c
8 changed files with 273 additions and 12 deletions

View file

@ -3,7 +3,45 @@ var UiWidget = require('./ui-widget'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
{icon} = require('../ui/utils'),
chroma = require('chroma-js')
chroma = require('chroma-js'),
ace = require('brace'),
ScriptVm = require('../widgets/scripts/script-vm'),
vm = new ScriptVm(), scriptGlobals = ScriptVm.globals,
editors = {}
require('brace/mode/html')
require('brace/mode/javascript')
require('brace/theme/solarized_dark')
// 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].setTheme('ace/theme/solarized_dark')
editors[name].setOption('autoScrollEditorIntoView', true)
editors[name].setOption('maxLines', 80)
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].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: {}
}])
}
class UiInspectorField extends UiWidget {
@ -65,7 +103,7 @@ class UiInspectorField extends UiWidget {
JSON.stringify(this.value, null, ' ').replace(/\n\s\s\s\s/g, ' ').replace(/\n\s\s(\}|\])/g, ' $1') : this.value
var input = html`<textarea tabIndex="${this.tabIndex}" class="no-keybinding" name="${this.name}" rows="${stringValue.split('\n').length}"></textarea>`
let input = html`<textarea tabIndex="${this.tabIndex}" class="no-keybinding" name="${this.name}" rows="${stringValue.split('\n').length}"></textarea>`
if (this.name === 'id' && this.widget.props.type === 'root') input.disabled = true
@ -128,6 +166,58 @@ class UiInspectorField extends UiWidget {
}
if (['script', 'touch', 'draw'].includes(this.name)) {
let editor = editors[this.name]
input.style.display = 'none'
input._ace_input = editor.textarea
editor.setValue(input.value)
editor.textarea.osc_input = input
editor.selection.setRange({start:0,end:0})
editor.gotoLine(0)
editor.removeAllListeners('focus')
editor.on('focus', ()=>{
this.parent.focusedInput = input
editor.setHighlightActiveLine(true)
editor.setHighlightGutterLine(true)
editor.removeAllListeners('blur')
editor.on('blur', (e)=>{
if (input.value !== editor.getValue()) {
input.value = editor.getValue()
this.parent.focusedInput = input
this.parent.onChange()
}
editor.setHighlightActiveLine(false)
editor.setHighlightGutterLine(false)
})
})
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}
}
globals.locals = true
globals.console = true
globals.JS = true
globals.this = false
editor.getSession().$worker.send("changeOptions", [{
globals: {...scriptGlobals, ...globals}
}])
this.container.appendChild(editor.element)
setTimeout(()=>{
editor.resize()
})
}
}

View file

@ -204,12 +204,17 @@ class UiInspector extends UiWidget {
input.blur()
var str = input.value.replace(/[‟“”]/g, '"').replace(/[‘’]/g, '\'') // convert invalid quote characters
var v = str
if (document.activeElement === input._ace_input) input._ace_input.blur()
var value = input.value,
str = value.replace(/[‟“”]/g, '"').replace(/[‘’]/g, '\''), // convert invalid quote characters
v
try {
v = JSON.parseFlex(str)
} catch(err) {}
} catch(err) {
v = str
}
this.trigger('update', {
propName: input.name,
@ -225,7 +230,10 @@ class UiInspector extends UiWidget {
if (event.target.tagName === 'TEXTAREA') {
if (event.keyCode === 13 && !event.shiftKey) {
if (event.keyCode === 13 &&
((!event.shiftKey && !event.target._ace) ||
(event.shiftKey && event.target._ace))
) {
event.preventDefault()
DOM.dispatchEvent(event.target, 'change')
@ -244,10 +252,11 @@ class UiInspector extends UiWidget {
onInput(event) {
if (event.target._ace) return
this.focusedInput = event.target
// text autoheight
this.focusedInput.setAttribute('rows',0)
this.focusedInput.setAttribute('rows', this.focusedInput.value.split('\n').length)