From 50f4288ffc5b8553467e6d482e4fb629235f7144 Mon Sep 17 00:00:00 2001 From: petoncle Date: Mon, 30 Oct 2023 09:58:55 +0100 Subject: [PATCH] Extract methods bufferPageKeyForClInput and consumeBufferedPageKeys --- src/commandline_frame.ts | 15 ++++++++------- src/content/controller_content.ts | 11 +++++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index 31644f8c..e1c7da32 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -55,6 +55,7 @@ import state, * as State from "@src/state" import * as R from "ramda" import { MinimalKey, minimalKeyFromKeyboardEvent } from "@src/lib/keyseq" import { TabGroupCompletionSource } from "@src/completions/TabGroup" +import {ownTabId} from "@src/lib/webext"; /** @hidden **/ const logger = new Logger("cmdline") @@ -163,11 +164,7 @@ const noblur = () => setTimeout(() => commandline_state.clInput.focus(), 0) /** @hidden **/ export function focus() { - commandline_state.clInput.focus() - commandline_state.clInput.removeEventListener("blur", noblur) - commandline_state.clInput.addEventListener("blur", noblur) - logger.debug("commandline_frame clInput focus()") - Messaging.messageOwnTab("buffered_page_keys", "").then((bufferedPageKeys : string[]) => { + function consumeBufferedPageKeys(bufferedPageKeys: string[]) { const clInputStillFocused = window.document.activeElement === commandline_state.clInput; logger.debug("buffered_page_keys response received", bufferedPageKeys, "clInputStillFocused = " + clInputStillFocused) @@ -181,7 +178,6 @@ export function focus() { "currentClInputValue = " + currentClInputValue); // Native events are assumed to be character keydowns events, // i.e. characters appended at the end of clInput. - // Insert page keys just after the command. commandline_state.clInput.value = initialClInputValue + bufferedPageKeys.join("") @@ -189,7 +185,12 @@ export function focus() { // Update completion. clInputValueChanged() } - }) + } + logger.debug("commandline_frame clInput focus()") + commandline_state.clInput.focus() + commandline_state.clInput.removeEventListener("blur", noblur) + commandline_state.clInput.addEventListener("blur", noblur) + Messaging.messageTab(ownTabId(), "buffered_page_keys").then(consumeBufferedPageKeys) } /** @hidden **/ diff --git a/src/content/controller_content.ts b/src/content/controller_content.ts index b2bdfa2f..198bbf54 100644 --- a/src/content/controller_content.ts +++ b/src/content/controller_content.ts @@ -232,10 +232,9 @@ function* ParserController() { export const generator = ParserController() // var rather than let stops weirdness in repl. generator.next() -/** Feed keys to the ParserController */ +/** Feed keys to the ParserController, unless they should be buffered to be later fed to clInput */ export function acceptKey(keyevent: KeyboardEvent) { - logger.debug("controller_content mustBufferPageKeysForClInput = " + mustBufferPageKeysForClInput) - if (mustBufferPageKeysForClInput) { + function bufferPageKeyForClInput(keyevent: KeyboardEvent) { let isCharacterKey = keyevent.key.length == 1 && !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey; if (isCharacterKey) { @@ -245,6 +244,10 @@ export function acceptKey(keyevent: KeyboardEvent) { keyevent.preventDefault() keyevent.stopImmediatePropagation() canceller.push(keyevent) - } else + } + logger.debug("controller_content mustBufferPageKeysForClInput = " + mustBufferPageKeysForClInput) + if (mustBufferPageKeysForClInput) + bufferPageKeyForClInput(keyevent) + else return generator.next(keyevent) }