editor: proper tab navigation in inspector + keyboard category fold/unfold with enter/space when focused

editor: add mod + f to focus tree search
code editor: fix nasty bug when focusing field using tab key
editor: f2 focuses "html" property when "label" is not present
This commit is contained in:
jean-emmanuel 2025-03-14 03:00:46 +01:00
parent 1c984a57e0
commit 89b8daa35a
7 changed files with 67 additions and 19 deletions

View file

@ -211,7 +211,8 @@ class Editor {
'end',
't',
'f2',
'h'
'h',
'mod + f'
]
for (let c of combos) {
@ -455,7 +456,7 @@ class Editor {
break
case 'f2':
if (!this.selectedWidgets.length) return
if (this.selectedWidgets[0].props.top === undefined) return
if (this.selectedWidgets[0].props.label === undefined && this.selectedWidgets[0].props.html === undefined) return
if (!this.inspector.expandedCategories.includes('style')) {
var header = DOM.get(this.inspector.container, '.category-header[data-name="style"]')[0]
if (header) {
@ -463,16 +464,24 @@ class Editor {
this.inspector.expandedCategories.push('style')
}
}
var input = DOM.get(this.inspector.container, 'textarea[name="label"]')[0]
var labelProp = this.selectedWidgets[0].props.label === undefined ? 'html' : 'label'
var input = DOM.get(this.inspector.container, `textarea[name="${labelProp}"]`)[0]
if (input) {
input.focus()
input.setSelectionRange(0, input.value.length)
input.scrollIntoView({block: 'center'})
if (input._ace_input) {
input._ace_input.focus()
} else {
input.focus()
input.setSelectionRange(0, input.value.length)
input.scrollIntoView({block: 'center'})
}
}
break
case 'h':
document.body.classList.add('editor-hide-selection')
break
case 'mod + f':
this.widgetTree.filter.focus()
break
}
@ -483,7 +492,7 @@ class Editor {
if (
this.inspector.helpModalOpened ||
e && e.target && e.target.tagName.match(/INPUT|TEXTAREA|SELECT/)
(e && e.target && e.target.tagName.match(/INPUT|TEXTAREA|SELECT/))
) return
if (e) e.preventDefault()

View file

@ -14,6 +14,15 @@ var UiModal = require('./ui-modal'),
ace.config.set('basePath', '/client/workers/')
function setCommandEnabled(editor, name, enabled) {
var command = editor.commands.byName[name]
if (!command.bindKeyOriginal)
command.bindKeyOriginal = command.bindKey
command.bindKey = enabled ? command.bindKeyOriginal : null
editor.commands.addCommand(command)
}
class CodeEditor {
constructor(parent, name, language, syntaxChecker) {
@ -119,25 +128,47 @@ class CodeEditor {
editor.fullscreen = false
input.style.display = 'none'
input._ace_input = editor.textarea
input.tabIndex = -1
editor.setValue(input.value)
editor.textarea.osc_input = input
editor.textarea.tabIndex = 0
editor.selection.setRange({start:0,end:0})
if (!this.widget || widget.hash !== this.widget.hash) {
// preserve active line if widget is the same
editor.gotoLine(0)
}
editor.removeAllListeners('focus')
this.editor.on('focus', (e)=>{
this.parent.focusedInput = this.input
this.editor.setHighlightActiveLine(true)
this.editor.setHighlightGutterLine(true)
this.editor.removeAllListeners('blur')
this.editor.removeAllListeners('change')
this.editor.removeAllListeners('changeSelection')
this.editor.on('change', (e)=>{
this.input.value = this.editor.getValue()
// this.parent.focusedInput = this.input
this.editor.dirty = true
})
var pos = this.editor.getCursorPosition()
if (pos.row === undefined || pos.column === undefined) this.editor.gotoLine(0)
this.typing = false
setCommandEnabled(this.editor, "indent", false)
setCommandEnabled(this.editor, "outdent", false)
this.editor.on('changeSelection', ()=>{
if (this.typing) return
let pos = this.editor.getCursorPosition()
if (pos.row || pos.column) {
setCommandEnabled(this.editor, "indent", true)
setCommandEnabled(this.editor, "outdent", true)
}
this.typing = true
})
this.editor.on('blur', (e)=>{
this.editor.setHighlightActiveLine(false)
this.editor.setHighlightGutterLine(false)

View file

@ -30,14 +30,14 @@ class UiInspectorField extends UiWidget {
if (this.widget.props.type == 'tab' || this.widget.props.type == 'root') {
this.container.appendChild(html`
<textarea tabIndex="${this.tabIndex}" class="no-keybinding" name="${this.name}" rows="1" disabled>${this.value}</textarea>
<textarea tabIndex="0" class="no-keybinding" name="${this.name}" rows="1" disabled>${this.value}</textarea>
`)
return
}
this.createInput()
var select = html`<select tabIndex="${this.tabIndex}" class="no-keybinding" name="${this.name}"/>`
var select = html`<select tabIndex="-1" class="no-keybinding" name="${this.name}"/>`
for (let cname in categories) {
@ -64,7 +64,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
let input = html`<textarea tabIndex="${this.tabIndex}" class="no-keybinding" name="${this.name}" rows="${stringValue.split('\n').length}"></textarea>`
let input = html`<textarea tabIndex="0" class="no-keybinding" name="${this.name}" rows="${stringValue.split('\n').length}"></textarea>`
if (this.name === 'id' && this.widget.props.type === 'root') input.disabled = true
@ -80,7 +80,7 @@ class UiInspectorField extends UiWidget {
if (this.default.choices) {
let wrapper = html`<osc-inspector-select></osc-inspector-select>`,
select = wrapper.appendChild(html`<select class="no-keybinding" name="${this.name}"/>`)
select = wrapper.appendChild(html`<select tabIndex="-1" class="no-keybinding" name="${this.name}"/>`)
for (let choice of this.default.choices) {
select.appendChild(html`<option ${choice == this.value ? 'selected' : ''} value="${choice}">${choice}</option>`)
}

View file

@ -185,7 +185,6 @@ class UiInspector extends UiWidget {
var widget = widgets[0],
lock = widgets.some(w=>w.getProp('lock')),
props = defaults[widget.props.type],
tabIndex = 1000,
codeEditorFields = []
this.widget = widget
@ -202,7 +201,7 @@ class UiInspector extends UiWidget {
if (widgets.length > 1 && categoryName === 'class_specific') categoryLabel = 'shared properties'
category.appendChild(html`
<div class="category-header" data-name="${categoryName}">${categoryLabel}${categoryName === 'widget' && widgets.length > 1 ? `s (${widgets.length})` : ''}</div>
<div class="category-header" tabIndex="0" data-name="${categoryName}">${categoryLabel}${categoryName === 'widget' && widgets.length > 1 ? `s (${widgets.length})` : ''}</div>
`)
let notEmpty
@ -235,7 +234,6 @@ class UiInspector extends UiWidget {
name: propName,
value: widget.props[propName],
default: def,
tabIndex: tabIndex++
}).container
if (first) field.classList.add('first-child')
@ -346,6 +344,11 @@ class UiInspector extends UiWidget {
}
} else if ((event.key == 'Enter' || event.key == ' ') && event.target.classList.contains('category-header')) {
event.preventDefault()
DOM.dispatchEvent(event.target, 'fast-click', {})
}
}

View file

@ -464,6 +464,10 @@ osc-inspector-category {
top:1rem;
}
&:focus, &:active {
text-decoration: underline
}
}
.separator {