ui: clean keyboard code, add unselect btn, tidyup style

This commit is contained in:
jean-emmanuel 2022-10-30 14:55:42 +01:00
parent 63e7a4fa5c
commit e52a4c224e
6 changed files with 115 additions and 87 deletions

View file

@ -30,6 +30,8 @@ class UiInspector extends UiWidget {
<div class="btn toolbar-btn" data-action="zoom-out">${raw(icon('magnifying-glass-minus'))}</div>
<div class="btn toolbar-btn" data-action="zoom-reset">${raw(icon('magnifying-glass'))}</div>
<div class="btn toolbar-btn" data-action="zoom-in">${raw(icon('magnifying-glass-plus'))}</div>
<div class="spacer"></div>
<div class="btn toolbar-btn" data-action="reset-selection">${raw(icon('border-none'))}</div>
<div class="btn toolbar-btn" data-action="toggle-grid">${raw(icon('table-cells'))}</div>
</div>
`)
@ -78,9 +80,14 @@ class UiInspector extends UiWidget {
case 'zoom-in':
zoom.setLocalZoom(zoom.localZoom + 0.1)
break
case 'reset-selection':
editor = editor || require('../editor')
editor.unselect()
break
case 'toggle-grid':
editor = editor || require('../editor')
editor.toggleGrid()
break
}
}

View file

@ -135,7 +135,7 @@ class OscKeybard extends UiWidget {
if (!e.shiftKey) {
this.input.selectionEnd = pos
}
this.updateCursor()
this.clickHandler()
}
}
@ -146,7 +146,7 @@ class OscKeybard extends UiWidget {
doubleClick(this.display, ()=>{
this.input.selectionStart = 0
this.input.selectionEnd = this.input.value.length
this.updateCursor()
this.clickHandler()
})
this.on('drag', (e)=>{
@ -166,7 +166,7 @@ class OscKeybard extends UiWidget {
this.input.selectionEnd = pos
}
this.updateCursor()
this.clickHandler()
}
}
@ -191,9 +191,6 @@ class OscKeybard extends UiWidget {
this.checkShift()
this.keyUp(key.getAttribute(this.shift != this.lock ? 'data-shift' : 'data-key'))
}, {element: this.container, multitouch: true})
@ -201,63 +198,19 @@ class OscKeybard extends UiWidget {
if (!VIRTUAL_KEYBOARD) return
// show keyboard when entering input focus
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
if (e.target.osc_input) return
this.show()
this.input = e.target
this.value = this.input.value
setTimeout(()=>{
this.updateDisplay()
this.show(e.target)
})
}
}, {capture: true})
document.addEventListener('blur', (e)=>{
if (!VIRTUAL_KEYBOARD) return
// hide keyboard when leaving input focus
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
this.hide()
this.input = null
this.value = null
}
}, {capture: true})
document.addEventListener('input', (e)=>{
if (!VIRTUAL_KEYBOARD) return
if (e.target === this.input) {
setTimeout(()=>{
this.updateDisplay()
})
}
}, {capture: true})
document.addEventListener('change', (e)=>{
if (!VIRTUAL_KEYBOARD) return
if (e.target === this.input) {
setTimeout(()=>{
this.updateDisplay()
})
}
})
document.addEventListener('keydown', (e)=>{
if (!VIRTUAL_KEYBOARD) return
if (e.target === this.input) {
setTimeout(()=>{
this.updateCursor()
})
}
})
document.addEventListener('select', (e)=>{
if (!VIRTUAL_KEYBOARD) return
if (e.target === this.input) {
setTimeout(()=>{
this.updateCursor()
})
}
})
this.boundSelectHandler = this.selectHandler.bind(this)
this.boundBlurHandler = this.blurHandler.bind(this)
this.boundKeydownHandler = this.keydownHandler.bind(this)
this.boundChangeHandler = this.changeHandler.bind(this)
this.boundInputHandler = this.inputHandler.bind(this)
this.boundClickHandler = this.clickHandler.bind(this)
}
@ -283,13 +236,10 @@ class OscKeybard extends UiWidget {
editLength = 1
break
} else {
this.input.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 13}))
if (this.input) DOM.dispatchEvent(this.input, 'change')
if (this.input) this.input.blur()
DOM.dispatchEvent(this.input, 'change')
return
}
case '{bksp}':
this.input.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 8}))
if (startPos === endPos && startPos === 0) break
this.input.value = this.input.value.substring(0, startPos - 1)
+ this.input.value.substring(endPos, this.input.value.length)
@ -303,11 +253,11 @@ class OscKeybard extends UiWidget {
break
case '{left}':
this.input.selectionStart = this.input.selectionEnd = Math.max(this.input.selectionStart - 1, 0)
this.updateCursor()
this.keydownHandler()
return
case '{right}':
this.input.selectionStart = this.input.selectionEnd = this.input.selectionStart + 1
this.updateCursor()
this.keydownHandler()
return
case '{up}':
case '{down}':
@ -338,7 +288,7 @@ class OscKeybard extends UiWidget {
this.input.selectionStart =
this.input.selectionEnd = key === '{up}' ? 0 : this.input.value.length
}
this.updateCursor()
this.keydownHandler()
return
default:
str = key
@ -357,13 +307,8 @@ class OscKeybard extends UiWidget {
this.input.selectionStart = startPos + editLength
this.input.selectionEnd = startPos + editLength
DOM.dispatchEvent(this.input, 'input')
DOM.dispatchEvent(this.input, 'keydown')
// this.input.dispatchEvent(new KeyboardEvent('keydown'))
}
keyUp(key) {
this.inputHandler()
}
@ -408,7 +353,10 @@ class OscKeybard extends UiWidget {
}
morph(this.display, content)
this.updateCursor()
setTimeout(()=>{
this.updateCursor()
})
}
updateCursor() {
@ -440,27 +388,50 @@ class OscKeybard extends UiWidget {
})
}
// if (this.display.contains(this.cursor)) this.display.removeChild(this.cursor)
// if (pos === 0) this.display.insertBefore(this.cursor)
// this.display.insert
// this.display.childr
// if (this.input) {
// this.display.style.setProperty('--cursor', )
// }
}
show(){
show(input){
if (input.osc_input) return // not compatible with code editor
if (this.visible) return
this.input = input
this.value = this.input.value
this.input.addEventListener('blur', this.boundBlurHandler)
this.input.addEventListener('input', this.boundInputHandler)
this.input.addEventListener('change', this.boundChangeHandler)
this.input.addEventListener('keydown', this.boundKeydownHandler)
this.input.addEventListener('select', this.boundSelectHandler)
this.input.addEventListener('click', this.boundClickHandler)
this.updateDisplay()
this.container.style.display = 'flex'
this.visible = true
this.container.style.display = 'flex'
}
hide(){
this.visible = false
if (!this.visible) return
this.input.removeEventListener('blur', this.boundBlurHandler)
this.input.removeEventListener('input', this.boundInputHandler)
this.input.removeEventListener('change', this.boundChangeHandler)
this.input.removeEventListener('keydown', this.boundKeydownHandler)
this.input.removeEventListener('select', this.boundSelectHandler)
this.input.removeEventListener('click', this.boundClickHandler)
this.input = null
this.value = null
this.container.style.display = 'none'
this.visible = false
for (let k in this.repeatKeys) {
clearTimeout(this.repeatKeys[k][0])
clearInterval(this.repeatKeys[k][1])
@ -471,6 +442,38 @@ class OscKeybard extends UiWidget {
delete this.pressedKeys[k]
}
}
blurHandler(e) {
setTimeout(()=>{
this.hide()
})
}
inputHandler(e) {
setTimeout(()=>{
this.updateDisplay()
})
}
changeHandler(e) {
setTimeout(()=>{
this.hide()
})
}
keydownHandler(e) {
setTimeout(()=>{
this.updateCursor()
})
}
selectHandler(e) {
setTimeout(()=>{
this.updateCursor()
})
}
clickHandler(e) {
setTimeout(()=>{
this.updateCursor()
})
}
}
module.exports = new OscKeybard({element: DOM.get('osc-keyboard')[0]})

View file

@ -46,15 +46,26 @@ osc-panel-container.right.editor-breakout {
align-items: center;
justify-content: center;
display: flex;
&:last-child {
margin-left: auto;
}
i {
pointer-events: none;
}
&[data-action="toggle-grid"] {
color: var(--color-accent)
}
&[data-action="reset-selection"]:after {
content: "";
position: absolute;
border-left: 1.5rem solid;
left: 50%;
top: 50%;
margin-top: -8rem;
margin-left: -1rem;
height: 16rem;
transform: rotate(45deg);
}
}
.spacer {
flex: 1
}
}
@ -63,6 +74,9 @@ osc-panel-container.right.editor-breakout {
overflow: auto;
flex:1;
height: calc(100% - 46rem);
box-shadow: 0 1rem 0 0 inset rgba(white, 0.025);
&::-webkit-scrollbar-thumb, &::-webkit-scrollbar-track-piece {
border-color: var(--color-background)!important;
border-top:0;

View file

@ -66,12 +66,11 @@ osc-keyboard {
width: 100rem;
}
.caret {
position: relative;
&:after {
content: "";
height: 10rem;
box-shadow: 0 0 0 1rem var(--color-accent);
position: absolute;
position: relative;
animation: caret-blink 1.2s steps(2, start) infinite;
pointer-events: none;
}
@ -104,6 +103,8 @@ osc-keyboard {
}
&:first-child {
height: 40rem;
min-height: 40rem;
flex-shrink: 0;
overflow: hidden;
margin-bottom: var(--spacing);
}

View file

@ -24,6 +24,8 @@
padding-left:$padding + 4rem;
padding-right:$padding;
border-top:1rem solid var(--color-background);
box-shadow: 0 1rem 0 0 inset rgba(white, 0.025);
padding-top:1rem;
&::-webkit-scrollbar-thumb, &::-webkit-scrollbar-track-piece {
border-color: var(--color-background)!important;

View file

@ -43,6 +43,7 @@ osc-panel-container {
flex: 1;
z-index: 1;
min-width: 100rem;
background: rgba(black, 0.35)!important
}
&.left, &.right {
@ -371,7 +372,7 @@ osc-panel-content {
left:0;
right:0;
bottom:0;
background: rgba(black, 0.5);
background: rgba(black, 0.35);
}
}