xP: bind M-h to toggle history, and adjust focus

This commit is contained in:
Přemysl Eric Janouch 2022-09-10 20:38:32 +02:00
parent 0bc2c12eec
commit 95aa89ee97
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 31 additions and 21 deletions

View File

@ -137,7 +137,7 @@ let bufferCurrent = undefined
let bufferLog = undefined
let bufferAutoscroll = true
function resetBufferStats(b) {
function bufferResetStats(b) {
b.newMessages = 0
b.newUnimportantMessages = 0
b.highlighted = false
@ -147,6 +147,29 @@ function bufferActivate(name) {
rpc.send({command: 'BufferActivate', bufferName: name})
}
function bufferToggleLog() {
if (bufferLog) {
setTimeout(() =>
document.getElementById('input')?.focus())
bufferLog = undefined
m.redraw()
return
}
let name = bufferCurrent
rpc.send({
command: 'BufferLog',
bufferName: name,
}).then(resp => {
if (bufferCurrent !== name)
return
bufferLog = rpc.base64decode(resp.log)
m.redraw()
})
}
let connecting = true
rpc.connect().then(result => {
buffers.clear()
@ -175,7 +198,7 @@ rpc.addEventListener('BufferUpdate', event => {
let e = event.detail, b = buffers.get(e.bufferName)
if (b === undefined) {
buffers.set(e.bufferName, (b = {lines: []}))
resetBufferStats(b)
bufferResetStats(b)
}
b.hideUnimportant = e.hideUnimportant
})
@ -206,7 +229,7 @@ rpc.addEventListener('BufferRemove', event => {
rpc.addEventListener('BufferActivate', event => {
let old = buffers.get(bufferCurrent)
if (old !== undefined)
resetBufferStats(old)
bufferResetStats(old)
bufferLast = bufferCurrent
let e = event.detail, b = buffers.get(e.bufferName)
@ -307,26 +330,11 @@ let Toolbar = {
bufferAutoscroll = !bufferAutoscroll
},
toggleLog: () => {
if (bufferLog) {
bufferLog = undefined
return
}
rpc.send({
command: 'BufferLog',
bufferName: bufferCurrent,
}).then(resp => {
bufferLog = rpc.base64decode(resp.log)
m.redraw()
})
},
view: vnode => {
return m('.toolbar', {}, [
m('button', {onclick: Toolbar.toggleAutoscroll},
bufferAutoscroll ? 'Scroll lock' : 'Scroll unlock'),
m('button', {onclick: Toolbar.toggleLog},
m('button', {onclick: event => bufferToggleLog()},
bufferLog === undefined ? 'Show log' : 'Hide log'),
])
},
@ -500,8 +508,8 @@ let Buffer = {
let Log = {
oncreate: vnode => {
if (vnode.dom !== undefined)
vnode.dom.scrollTop = vnode.dom.scrollHeight
vnode.dom.scrollTop = vnode.dom.scrollHeight
vnode.dom.focus()
},
linkify: text => {
@ -629,6 +637,8 @@ document.addEventListener('keydown', event => {
if (event.altKey && event.key == 'Tab') {
if (bufferLast !== undefined)
bufferActivate(bufferLast)
} else if (event.altKey && event.key == 'h') {
bufferToggleLog()
} else if (event.altKey && event.key == 'a') {
for (const [name, b] of buffers)
if (name !== bufferCurrent && b.newMessages) {