From 1058cb551c6f573517bbe39de0db37429d64b689 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Mon, 13 Dec 2021 12:40:09 +0100 Subject: [PATCH] inspector: code editor for script/touch/draw properties --- package.json | 1 + src/client/editor/context-menu.js | 6 +- src/client/managers/session/index.js | 2 +- src/client/ui/ui-inspector-field.js | 94 ++++++++++++++- src/client/ui/ui-inspector.js | 19 ++- src/client/widgets/scripts/script-vm.js | 12 +- src/html/client.html | 5 + src/scss/ui/inspector.scss | 146 +++++++++++++++++++++++- 8 files changed, 273 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 3dbaeaf7..21706525 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/client/editor/context-menu.js b/src/client/editor/context-menu.js index a1113cb9..21924ef0 100644 --- a/src/client/editor/context-menu.js +++ b/src/client/editor/context-menu.js @@ -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}) diff --git a/src/client/managers/session/index.js b/src/client/managers/session/index.js index c9c2cca7..2978f038 100644 --- a/src/client/managers/session/index.js +++ b/src/client/managers/session/index.js @@ -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() diff --git a/src/client/ui/ui-inspector-field.js b/src/client/ui/ui-inspector-field.js index b3f71970..3ee6b3a1 100644 --- a/src/client/ui/ui-inspector-field.js +++ b/src/client/ui/ui-inspector-field.js @@ -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`` + let input = html`` 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() + }) + } + } diff --git a/src/client/ui/ui-inspector.js b/src/client/ui/ui-inspector.js index f9793ef2..dfc49d4f 100644 --- a/src/client/ui/ui-inspector.js +++ b/src/client/ui/ui-inspector.js @@ -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) diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index 11136af1..17c135f3 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -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) + } + } } diff --git a/src/html/client.html b/src/html/client.html index 7d359459..d6813fc4 100644 --- a/src/html/client.html +++ b/src/html/client.html @@ -56,6 +56,11 @@
+ diff --git a/src/scss/ui/inspector.scss b/src/scss/ui/inspector.scss index 0c9e7e5d..b255bc61 100644 --- a/src/scss/ui/inspector.scss +++ b/src/scss/ui/inspector.scss @@ -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 {