From 3defca2a7c5074d5d19c6d63abd4ca48f7f80700 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 29 Oct 2022 11:05:07 +0200 Subject: [PATCH] keyboard: wip cursor attempt --- src/client/ui/ui-keyboard.js | 74 +++++++++++++++++++++++++++++------- src/scss/ui/keyboard.scss | 36 +++++++++++++++++- 2 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/client/ui/ui-keyboard.js b/src/client/ui/ui-keyboard.js index fb1417bd..dc3cbaa5 100644 --- a/src/client/ui/ui-keyboard.js +++ b/src/client/ui/ui-keyboard.js @@ -3,7 +3,8 @@ var UiWidget = require('./ui-widget'), locales = require('../locales'), layout = locales('keyboard_layout'), html = require('nanohtml'), - raw = require('nanohtml/raw') + raw = require('nanohtml/raw'), + morph = require('nanomorph') const numericLayout = [ '{sep} + 7 8 9', @@ -42,9 +43,9 @@ class OscKeybard extends UiWidget { this.value = null this.visible = false - this.display = html`` + this.display = html`
` this.container.appendChild(html`
${this.display}
`) - + this.cursor = html`|` for (var k in layout) { layout[k] = layout[k].map((r, i)=>(r + ' ' + (numericLayout[i]||'')).trim().split(' ')) @@ -160,7 +161,6 @@ class OscKeybard extends UiWidget { this.hide() this.input = null this.value = null - this.updateDisplay() } }, {capture: true}) @@ -177,7 +177,18 @@ class OscKeybard extends UiWidget { document.addEventListener('change', (e)=>{ // hide keyboard when leaving input focus if (e.target === this.input) { - this.updateDisplay() + setTimeout(()=>{ + this.updateDisplay() + }) + } + }) + + document.addEventListener('keydown', (e)=>{ + // hide keyboard when leaving input focus + if (e.target === this.input) { + setTimeout(()=>{ + this.updateCursor() + }) } }) @@ -211,6 +222,7 @@ class OscKeybard extends UiWidget { } 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) editLength = -1 @@ -222,15 +234,17 @@ class OscKeybard extends UiWidget { str = ' ' break case '{left}': - this.input.selectionStart = this.input.selectionEnd = this.input.selectionStart - 1 + this.input.selectionStart = this.input.selectionEnd = Math.max(this.input.selectionStart - 1, 0) + this.updateCursor() return case '{right}': this.input.selectionStart = this.input.selectionEnd = this.input.selectionStart + 1 + this.updateCursor() return case '{up}': case '{down}': - if (this.display.value.includes('\n')) { - var chars = this.display.value.split('\n').map(x=>x.length + 1), + if (this.input.value.includes('\n')) { + var chars = this.input.value.split('\n').map(x=>x.length + 1), line = 0, charCount = 0, newPos = 0, @@ -252,11 +266,11 @@ class OscKeybard extends UiWidget { this.input.selectionStart = this.input.selectionEnd = newPos + posAtLine - return } else { this.input.selectionStart = - this.input.selectionEnd = key === '{up}' ? 0 : this.display.value.length + this.input.selectionEnd = key === '{up}' ? 0 : this.input.value.length } + this.updateCursor() return default: str = key @@ -300,12 +314,44 @@ class OscKeybard extends UiWidget { } updateDisplay() { + var value = '' if (this.input) { - this.display.value = this.input.osc_input ? this.input.osc_input.value :this.input.value - - } else { - this.display.value = '' + value = this.input.osc_input ? this.input.osc_input.value :this.input.value } + + var content = html`
` + for (var i in value) { + content.appendChild(html` + ${value[i]} + `) + } + + morph(this.display, content) + this.updateCursor() + } + + updateCursor() { + + if (!this.input) return + + var pos = this.input.selectionStart + this.display.classList.remove('first-char-caret') + DOM.get(this.display, '.caret').map(x=>x.classList.remove('caret')) + var caretChar = DOM.get(this.display, 'span:nth-child(' + pos + ')')[0] + + if (caretChar) { + caretChar.classList.add('caret') + caretChar.scrollIntoView() + } + else this.display.classList.add('first-char-caret') + + // 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(){ diff --git a/src/scss/ui/keyboard.scss b/src/scss/ui/keyboard.scss index e8afb366..3615a912 100644 --- a/src/scss/ui/keyboard.scss +++ b/src/scss/ui/keyboard.scss @@ -25,10 +25,15 @@ osc-keyboard { from {opacity: 0} to {opacity: 1} } + @keyframes caret-blink { + to {visibility: hidden} + } + animation: keyboard-in 0.25s forwards; - textarea { + .textarea { height: auto; + overflow: auto; width: 100%; background: transparent; padding: 14rem 20rem 4rem; @@ -37,6 +42,35 @@ osc-keyboard { vertical-align: middle; font: inherit; margin-top:var(---spacing); + position: relative; + pointer-events: auto; + min-height: 100%; + span { + margin: 0 0.25rem; + } + .caret { + position: relative; + &:after { + content: ""; + height: 10rem; + box-shadow: 0 0 0 1rem var(--color-accent); + position: absolute; + animation: caret-blink 1.2s steps(2, start) infinite; + } + + } + &.first-char-caret { + &:before { + content: ""; + height: 10rem; + position: absolute; + box-shadow: 0 0 0 1rem var(--color-accent); + animation: caret-blink 1.2s steps(2, start) infinite; + } + } + .newline { + display: block; + } } .row {