diff --git a/src/client/ui/ui-code-editor.js b/src/client/ui/ui-code-editor.js new file mode 100644 index 00000000..75157b92 --- /dev/null +++ b/src/client/ui/ui-code-editor.js @@ -0,0 +1,275 @@ +var UiModal = require('./ui-modal'), + locales = require('../locales'), + html = require('nanohtml'), + raw = require('nanohtml/raw'), + {icon} = require('../ui/utils'), + ace = require('brace'), + ScriptVm = require('../widgets/scripts/script-vm'), + scriptGlobals = ScriptVm.globals, + editors = {}, editorModes = { + javascript: require('brace/mode/javascript') + } + +class CodeEditor { + + constructor(parent, name, language, syntaxChecker) { + + this.parent = parent + this.name = name + this.language = language + this.syntaxChecker = syntaxChecker + + this.field = null + this.input = null + this.widget = null + this.container = html`
` + this.helpBtn = html`
${locales('editor_ace_help')}
` + this.fsBtn = html`
${raw(icon('expand'))}
` + + this.helpBtn.addEventListener('fast-click', (e)=>{ + this.help() + }) + this.fsBtn.addEventListener('fast-click', (e)=>{ + this.fullscreen() + }) + + this.closeKey = ((e)=>{ + // this handler may remain dangling if closing with ctrl+enter + // we ignore this as it has no effect + if (e.keyCode !== 27) return + this.field.classList.remove('fullscreen') + this.editor.setOptions({maxLines: 30}) + this.editor.fullscreen = false + document.removeEventListener('keydown', this.closeKey) + this.field.removeEventListener('fast-click', this.closeClick) + this.editor.blur() + this.parent.parentContainer.classList.remove('editor-breakout') + }).bind(this) + + this.closeClick = ((e)=>{ + if (e.target === this.field) { + this.closeKey({keyCode: 27}) + } + }).bind(this) + + this.editor = ace.edit(this.container) + + if (editorModes[language]) this.editor.getSession().setMode('ace/mode/' + language) + + this.editor.setOptions({ + autoScrollEditorIntoView: true, + fixedWidthGutter: true, + tabSize: 2, + maxLines: 30, + dragEnabled: false, + useWorker: syntaxChecker !== false + }) + + this.editor.$blockScrolling = Infinity + this.editor.commands.bindKeys({ + 'ctrl-l': null, 'ctrl-f': null, 'ctrl-h': null, 'ctrl-,': null, + }) + this.editor.renderer.setScrollMargin(4, 4, 0, 4) + this.editor.element = this.container + this.editor.element.classList.add('ace_dark') + this.editor.textarea = DOM.get(this.container, '.ace_text-input')[0] + this.editor.textarea.name = name + this.editor.textarea._ace = true + this.editor.setHighlightActiveLine(false) + this.editor.setHighlightGutterLine(false) + + if (language === 'javascript' && syntaxChecker !== false) { + this.editor.getSession().$worker.send('setOptions', [{ + asi: true, // no semicolon + esversion: 6, + strict: 'implied', + node: false, + browser: true, + validthis: true, + undef: true, + globals: {} + }]) + } + + + this.middledown = false + this.editor.on('mousedown', function(e) { + if (e.domEvent.button === 1) this.middledown = true + }) + this.editor.on('mouseup', function(e) { + if (e.domEvent.button === 1) this.middledown = false + }) + + + } + + bind(widget, field) { + + this.widget = widget + this.field = field + this.input = DOM.get(field, 'textarea')[0] + + var editor = this.editor, + input = this.input + + editor.fullscreen = false + 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') + this.editor.on('focus', (e)=>{ + if (this.middledown) { + e.preventDefault() + return + } + this.parent.focusedInput = this.input + this.editor.setHighlightActiveLine(true) + this.editor.setHighlightGutterLine(true) + this.editor.removeAllListeners('blur') + this.editor.removeAllListeners('change') + this.editor.on('change', (e)=>{ + this.input.value = this.editor.getValue() + // this.parent.focusedInput = this.input + this.editor.dirty = true + }) + + this.editor.on('blur', (e)=>{ + this.editor.setHighlightActiveLine(false) + this.editor.setHighlightGutterLine(false) + this.editor.selection.setRange({start:0,end:0}) + if (this.editor.fullscreen) { + e.preventDefault() + return + } + this.input.value = this.editor.getValue() + this.parent.focusedInput = this.input + if (this.editor.dirty) { + this.editor.dirty = false + this.parent.onChange() + } + this.parent.focusedInput = null + + }) + + }) + + + editor.setOptions({maxLines: 30}) + editor.dirty = false + + if (this.language === 'javascript') { + let globals = {} + if (this.name === 'onEvent') { + if (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 === 'onValue') { + globals = {id: true, value: true, touch: true} + } else if (this.name === 'onDraw') { + globals = {value: true, height: true, width: true, ctx: true, cssVars: true} + } else if (this.name === 'onTouch') { + globals = this.widget.getProp('type') === 'canvas' ? + {value: true, height: true, width: true, event: true} : + {value: true, event: true, handle: true} + } + globals.locals = true + globals.console = true + globals.JS = true + globals.this = false + + if (this.syntaxChecker !== false) editor.getSession().$worker.send('changeOptions', [{ + globals: {...scriptGlobals, ...globals} + }]) + } + + field.appendChild(editor.element) + field.appendChild(this.helpBtn) + field.appendChild(this.fsBtn) + + setTimeout(()=>{ + editor.getSession().getUndoManager().reset() + editor.resize() + }) + + + } + + fullscreen() { + + this.editor.fullscreen = this.field.classList.toggle('fullscreen') + this.editor.setOptions({maxLines: this.editor.fullscreen ? 0 : 30}) + window.dispatchEvent(new Event('resize')) + this.field.style.setProperty('--prefix', this.editor.fullscreen ? '"' + this.widget.getProp('id') + '."' : '') + + if (this.editor.fullscreen) { + this.parent.parentContainer.classList.add('editor-breakout') + this.editor.focus() + document.addEventListener('keydown', this.closeKey) + this.field.addEventListener('fast-click', this.closeClick) + } else { + this.parent.parentContainer.classList.remove('editor-breakout') + document.removeEventListener('keydown', this.closeKey) + this.field.removeEventListener('fast-click', this.closeClick) + } + } + + help() { + + var keybindings = [] + var commandMap = {} + + this.editor.keyBinding.$handlers.forEach(function(handler) { + var ckb = handler.commandKeyBinding + for (var i in ckb) { + var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase() }) + var commands = ckb[i] + if (!Array.isArray(commands)) + commands = [commands] + commands.forEach(function(command) { + if (typeof command != 'string') + command = command.name + if (commandMap[command]) { + commandMap[command].key += '|' + key + } else { + commandMap[command] = {key: key, command: command} + keybindings.push(commandMap[command]) + } + }) + } + }) + + var modal = new UiModal({closable: true, title: html`${locales('editor_ace_help_title')}`, html: true, content: html` +
+ + + + + + + + + ${keybindings.map(k=>html``)} +
ShortcutCommand
Ctrl-Entersave
${k.key}${k.command}
+
+ `}) + + + this.parent.helpModalOpened = true + + modal.on('close', ()=>{ + this.parent.helpModalOpened = false + }) + + } + +} + +module.exports = function(parent, name, language, syntaxChecker) { + if (!editors[name]) editors[name] = new CodeEditor(parent, name, language, syntaxChecker) + return editors[name] +} diff --git a/src/client/ui/ui-inspector-field.js b/src/client/ui/ui-inspector-field.js index ed993c99..60766098 100644 --- a/src/client/ui/ui-inspector-field.js +++ b/src/client/ui/ui-inspector-field.js @@ -1,58 +1,9 @@ var UiWidget = require('./ui-widget'), - UiModal = require('./ui-modal'), {categories} = require('../widgets/'), - locales = require('../locales'), html = require('nanohtml'), raw = require('nanohtml/raw'), {icon} = require('../ui/utils'), - chroma = require('chroma-js'), - ace = require('brace'), - ScriptVm = require('../widgets/scripts/script-vm'), - scriptGlobals = ScriptVm.globals, - editors = {}, editorModes = { - javascript: require('brace/mode/javascript') - } - -function createEditor(name, language, syntaxChecker) { - var el = html`
` - var editor = editors[name] = ace.edit(el) - if (editorModes[language]) editor.getSession().setMode('ace/mode/' + language) - editor.setOptions({ - autoScrollEditorIntoView: true, - fixedWidthGutter: true, - tabSize: 2, - maxLines: 30, - dragEnabled: false, - useWorker: syntaxChecker !== false - }) - - editor.$blockScrolling = Infinity - editor.commands.bindKeys({ - 'ctrl-l': null, 'ctrl-f': null, 'ctrl-h': null, 'ctrl-,': null, - }) - editor.renderer.setScrollMargin(4, 4, 0, 4) - editor.element = el - editor.element.classList.add('ace_dark') - 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' && syntaxChecker !== false) { - editor.getSession().$worker.send('setOptions', [{ - asi: true, // no semicolon - esversion: 6, - strict: 'implied', - node: false, - browser: true, - validthis: true, - undef: true, - globals: {} - }]) - } -} - - + chroma = require('chroma-js') class UiInspectorField extends UiWidget { @@ -180,185 +131,57 @@ class UiInspectorField extends UiWidget { } if (this.default.editor) { - this.container.classList.add('has-editor') - if (!editors[this.name]) createEditor(this.name, this.default.editor, this.default.syntaxChecker) - let editor = editors[this.name] - editor.fullscreen = false - 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.removeAllListeners('mousedown') - editor.removeAllListeners('mouseup') - editor.setOptions({maxLines: 30}) - editor.dirty = false - editor.middledown = false - editor.on("mousedown", function (e) { - if (e.domEvent.button === 1) editor.middledown = true - }) - editor.on("mouseup", function (e) { - if (e.domEvent.button === 1) editor.middledown = false - }) - editor.on('focus', (e)=>{ - if (editor.middledown) return - this.parent.focusedInput = input - editor.setHighlightActiveLine(true) - editor.setHighlightGutterLine(true) - editor.removeAllListeners('blur') - editor.removeAllListeners('change') - editor.on('change', (e)=>{ - input.value = editor.getValue() - editor.dirty = true - }) - editor.on('blur', (e)=>{ - editor.setHighlightActiveLine(false) - editor.setHighlightGutterLine(false) - editor.selection.setRange({start:0,end:0}) - if (editor.fullscreen) { - e.preventDefault() - return - } - input.value = editor.getValue() - this.parent.focusedInput = input - if (editor.dirty) { - editor.dirty = false - this.parent.onChange() - } - }) - }) - if (this.default.editor === 'javascript') { - let globals = {} - if (this.name === 'onEvent') { - if (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 === 'onValue') { - globals = {id: true, value: true, touch: true} - } else if (this.name === 'onDraw') { - globals = {value: true, height: true, width: true, ctx: true, cssVars: true} - } else if (this.name === 'onTouch') { - globals = this.widget.getProp('type') === 'canvas' ? - {value: true, height: true, width: true, event: true} : - {value: true, event: true, handle: true} - } - globals.locals = true - globals.console = true - globals.JS = true - globals.this = false - if (this.default.syntaxChecker !== false) editor.getSession().$worker.send('changeOptions', [{ - globals: {...scriptGlobals, ...globals} - }]) - } - - this.container.appendChild(editor.element) - setTimeout(()=>{ - editor.getSession().getUndoManager().reset() - editor.resize() - }) - - var help = this.container.appendChild(html`
${locales('editor_ace_help')}
`) - help.addEventListener('fast-click', (e)=>{ - this.aceHelp(editor) - }) - - var closeKey = (e)=>{ - // this handler may remain dangling if closing with ctrl+enter - // we ignore this as it has no effect - if (e.keyCode !== 27) return - this.container.classList.remove('fullscreen') - editor.setOptions({maxLines: 30}) - editor.fullscreen = false - document.removeEventListener('keydown', closeKey) - this.container.removeEventListener('fast-click', closeClick) - editor.blur() - this.parent.parentContainer.classList.remove('editor-breakout') - } - var closeClick = (e)=>{ - if (e.target === this.container) { - closeKey({keyCode: 27}) - } - } - var fullscreenBtn = this.container.appendChild(html`
${raw(icon('expand'))}
`) - fullscreenBtn.addEventListener('fast-click', (e)=>{ - - // update dom reference because we might break them with morph in ui-inspector.js - this.container = e.target.closest('osc-inspector-field') - this.label = DOM.get(this.container, 'label')[0] - - editor.fullscreen = this.container.classList.toggle('fullscreen') - editor.setOptions({maxLines: editor.fullscreen ? 0 : 30}) - window.dispatchEvent(new Event('resize')) - this.label.style.setProperty('--prefix', editor.fullscreen ? '"' + this.widget.getProp('id') + '."' : '') - if (editor.fullscreen) { - this.parent.parentContainer.classList.add('editor-breakout') - editor.focus() - document.addEventListener('keydown', closeKey) - this.container.addEventListener('fast-click', closeClick) - } else { - this.parent.parentContainer.classList.remove('editor-breakout') - document.removeEventListener('keydown', closeKey) - this.container.removeEventListener('fast-click', closeClick) - } - }) + // var help = this.container.appendChild(html`
${locales('editor_ace_help')}
`) + // help.addEventListener('fast-click', (e)=>{ + // this.aceHelp(editor) + // }) + // + // var closeKey = (e)=>{ + // // this handler may remain dangling if closing with ctrl+enter + // // we ignore this as it has no effect + // if (e.keyCode !== 27) return + // this.container.classList.remove('fullscreen') + // editor.setOptions({maxLines: 30}) + // editor.fullscreen = false + // document.removeEventListener('keydown', closeKey) + // this.container.removeEventListener('fast-click', closeClick) + // editor.blur() + // this.parent.parentContainer.classList.remove('editor-breakout') + // } + // var closeClick = (e)=>{ + // if (e.target === this.container) { + // closeKey({keyCode: 27}) + // } + // } + // var fullscreenBtn = this.container.appendChild(html`
${raw(icon('expand'))}
`) + // fullscreenBtn.addEventListener('fast-click', (e)=>{ + // + // // update dom reference because we might break them with morph in ui-inspector.js + // this.container = e.target.closest('osc-inspector-field') + // this.label = DOM.get(this.container, 'label')[0] + // + // editor.fullscreen = this.container.classList.toggle('fullscreen') + // editor.setOptions({maxLines: editor.fullscreen ? 0 : 30}) + // window.dispatchEvent(new Event('resize')) + // this.label.style.setProperty('--prefix', editor.fullscreen ? '"' + this.widget.getProp('id') + '."' : '') + // if (editor.fullscreen) { + // this.parent.parentContainer.classList.add('editor-breakout') + // editor.focus() + // document.addEventListener('keydown', closeKey) + // this.container.addEventListener('fast-click', closeClick) + // } else { + // this.parent.parentContainer.classList.remove('editor-breakout') + // document.removeEventListener('keydown', closeKey) + // this.container.removeEventListener('fast-click', closeClick) + // } + // }) } } - aceHelp(editor) { - - var keybindings = [] - var commandMap = {} - editor.keyBinding.$handlers.forEach(function(handler) { - var ckb = handler.commandKeyBinding - for (var i in ckb) { - var key = i.replace(/(^|-)\w/g, function(x) { return x.toUpperCase() }) - var commands = ckb[i] - if (!Array.isArray(commands)) - commands = [commands] - commands.forEach(function(command) { - if (typeof command != 'string') - command = command.name - if (commandMap[command]) { - commandMap[command].key += '|' + key - } else { - commandMap[command] = {key: key, command: command} - keybindings.push(commandMap[command]) - } - }) - } - }) - - var modal = new UiModal({closable: true, title: html`${locales('editor_ace_help_title')}`, html: true, content: html` -
- - - - - - - - - ${keybindings.map(k=>html``)} -
ShortcutCommand
Ctrl-Entersave
${k.key}${k.command}
-
- `}) - - - this.parent.helpModalOpened = true - modal.on('close', ()=>{ - this.parent.helpModalOpened = false - }) - - } - } diff --git a/src/client/ui/ui-inspector.js b/src/client/ui/ui-inspector.js index 7a91fe63..20d349ab 100644 --- a/src/client/ui/ui-inspector.js +++ b/src/client/ui/ui-inspector.js @@ -6,9 +6,8 @@ var UiWidget = require('./ui-widget'), {defaults} = require('../widgets/'), html = require('nanohtml'), raw = require('nanohtml/raw'), - fastdom = require('fastdom') - - + fastdom = require('fastdom'), + getCodeEditor = require('./ui-code-editor') class UiInspector extends UiWidget { @@ -38,7 +37,7 @@ class UiInspector extends UiWidget { } }) - this.parentContainer = DOM.get('osc-panel-container.right osc-panel-inner')[0] + this.parentContainer = DOM.get('osc-panel-container.right')[0] this.colorPicker = new UiColorPicker() this.colorPicker.on('change', (event)=>{ @@ -106,6 +105,7 @@ class UiInspector extends UiWidget { this.mounted = false this.clearTimeout = null this.colorPicker.close() + this.parentContainer.classList.remove('editor-breakout') }) } @@ -121,7 +121,8 @@ class UiInspector extends UiWidget { var widget = widgets[0], props = defaults[widget.props.type], - tabIndex = 1000 + tabIndex = 1000, + codeEditorFields = [] this.widget = widget @@ -171,6 +172,11 @@ class UiInspector extends UiWidget { tabIndex: tabIndex++ }).container if (first) field.classList.add('first-child') + + if (def.editor) { + codeEditorFields.push([propName, def.editor, def.syntaxChecker, widget]) + field.classList.add('has-editor') + } } if (field) { @@ -194,6 +200,15 @@ class UiInspector extends UiWidget { this.container.appendChild(content) } + if (codeEditorFields.length) { + let fields = DOM.get(this.container, '.has-editor') + for (var i in codeEditorFields) { + let [propName, language, syntax, widget] = codeEditorFields[i] + let codeEditor = getCodeEditor(this, propName, language, syntax) + codeEditor.bind(widget, fields[i]) + } + } + this.mounted = true this.lock = false @@ -258,7 +273,6 @@ class UiInspector extends UiWidget { event.preventDefault() DOM.dispatchEvent(event.target, 'change') - this.parentContainer.classList.remove('editor-breakout') } else if (event.keyCode === 27) { diff --git a/src/scss/ui/inspector.scss b/src/scss/ui/inspector.scss index c4ee0c3d..a5ef4fad 100644 --- a/src/scss/ui/inspector.scss +++ b/src/scss/ui/inspector.scss @@ -4,8 +4,13 @@ $label-width: 94rem; $padding: 8rem; $radius: 3rem; -osc-panel-container.right osc-panel-inner.editor-breakout { - contain: initial +osc-panel-container.right.editor-breakout { + osc-panel-inner { + contain: initial; + } + .resize-handle { + display: none; + } } #osc-inspector {