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

@ -30,6 +30,7 @@
"babelify": "10.0.0",
"balanced-match": "1.0.0",
"bonjour": "github:jean-emmanuel/bonjour",
"brace": "^0.11.1",
"browserify": "17.0.0",
"chokidar": "3.5.1",
"chroma-js": "2.1.1",

View file

@ -36,7 +36,11 @@ var handleClick = function(event) {
if (event.type !== 'fast-right-click') {
if (editor.inspector.focusedInput) editor.inspector.onChange()
if (editor.inspector.focusedInput) {
editor.inspector.onChange()
if (!document.body.contains(eventData.target)) return
}
if (targetWidget.getProp('type') === 'tab') {
targetWidget.parent.setValue(targetWidget.parent.children.indexOf(targetWidget), {sync: true, send: true})

View file

@ -104,7 +104,7 @@ var SessionManager = class SessionManager extends EventEmitter {
if (path) this.setSessionPath(path)
if (editor.inspector.focusedInput) {
editor.inspector.focusedInput.blur()
editor.inspector.onChange()
}
if (!this.sessionPath) return this.saveAs()

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)

View file

@ -4,7 +4,8 @@ var widgetManager = require('../../managers/widgets'),
{deepCopy} = require('../../utils'),
{urlParser} = require('../utils'),
Vm = require('../vm'),
toolbar
toolbar,
scriptGlobals = {empty:true}
class ScriptVm extends Vm {
@ -493,6 +494,15 @@ class ScriptVm extends Vm {
this.sanitize(this.sandbox.contentWindow[imports])
}
if (scriptGlobals.empty){
delete scriptGlobals.empty
for (var k in this.sandbox.contentWindow) {
if (window[k] === undefined) scriptGlobals[k] = true
}
ScriptVm.globals = scriptGlobals
console.log(Object.keys(scriptGlobals).length)
}
}
}

View file

@ -56,6 +56,11 @@
</osc-workspace>
<div id="notifications"></div>
<div id="editors" style="display:none">
<div id="editor-script"></div>
<div id="editor-touch"></div>
<div id="editor-draw"></div>
</div>
</body>
</html>

View file

@ -19,7 +19,7 @@ osc-inspector-field {
--widget-padding: -1rem;
label {
> label {
width: 90rem;
// order: 35;
text-align: right;
@ -31,7 +31,10 @@ osc-inspector-field {
// background: rgba(white, 0.1);
}
textarea, input {
.ace_editor {
flex:1;
}
> textarea, > input {
font-family: $font-mono;
line-height: $field-height - 8rem;
@ -76,6 +79,145 @@ osc-inspector-field {
}
osc-inspector-field .ace_editor {
background-color: var(--color-background);
color: var(--color-text);
font-size: inherit;
background: var(--color-background);
line-height: 1.3;
.ace_gutter {
background: #282c34;
color: #6a6f7a
}
.ace_print-margin {
display: none;
}
.ace_cursor {
color: #528bff
}
.ace_marker-layer .ace_selection {
background: #3d4350
}
.ace_multiselect .ace_selection.ace_start {
box-shadow: 0 0 3px 0 #282c34;
border-radius: 2px
}
.ace_marker-layer .ace_step {
background: #c6dbae
}
.ace_marker-layer .ace_bracket {
margin: -1px 0 0 -1px;
border: 1px solid #747369
}
.ace_marker-layer .ace_active-line {
background: rgba(76, 87, 103, .19)
}
.ace_gutter-active-line {
background-color: rgba(76, 87, 103, .19)
}
.ace_marker-layer .ace_selected-word {
border: 1px solid #3d4350
}
.ace_fold {
background-color: #61afef;
border-color: #abb2bf
}
.ace_keyword {
color: #c678dd
}
.ace_keyword.ace_operator {
color: #c678dd
}
.ace_keyword.ace_other.ace_unit {
color: #d19a66
}
.ace_constant.ace_language {
color: #d19a66
}
.ace_constant.ace_numeric {
color: #d19a66
}
.ace_constant.ace_character {
color: #56b6c2
}
.ace_constant.ace_other {
color: #56b6c2
}
.ace_support.ace_function {
color: #61afef
}
.ace_support.ace_constant {
color: #d19a66
}
.ace_support.ace_class {
color: #e5c07b
}
.ace_support.ace_type {
color: #e5c07b
}
.ace_storage {
color: #c678dd
}
.ace_storage.ace_type {
color: #c678dd
}
.ace_invalid {
color: #fff;
background-color: #f2777a
}
.ace_invalid.ace_deprecated {
color: #272b33;
background-color: #d27b53
}
.ace_string {
color: #98c379
}
.ace_string.ace_regexp {
color: #e06c75
}
.ace_comment {
font-style: italic;
color: #5c6370
}
.ace_variable {
color: #e06c75
}
.ace_variable.ace_parameter {
color: #d19a66
}
.ace_meta.ace_tag {
color: #e06c75
}
.ace_entity.ace_other.ace_attribute-name {
color: #e06c75
}
.ace_entity.ace_name.ace_function {
color: #61afef
}
.ace_entity.ace_name.ace_tag {
color: #e06c75
}
.ace_markup.ace_heading {
color: #98c379
}
.ace_indent-guide {
background: none;
border-right: 1rem solid rgba(white, 0.05)
}
.ace_tooltip {
left:$padding!important
}
// .ace_search {
// position: fixed;
// top: var(--nav-height);
// margin-top: $padding;
// right: $padding * 2;
// border-radius: 3rem;
// }
}
osc-inspector-category {