diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index 15735413..d4100eb8 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -82,12 +82,7 @@ const commandline_state = { keyEvents: new Array(), refresh_completions, state, - keysFromContentProcess: [], - consumedKeyCount: 0, - clInputFocused: false, - clInputNormalEventsReceived: false, - clInputInitialValue: "", - contentProcessKeysReceivedAfterClInputFocused: 0 + initialClInputValue: "", } // first theming of commandline iframe @@ -168,55 +163,38 @@ const noblur = () => setTimeout(() => commandline_state.clInput.focus(), 0) /** @hidden **/ export function focus() { - logger.debug("Called focus()") commandline_state.clInput.focus() commandline_state.clInput.removeEventListener("blur", noblur) commandline_state.clInput.addEventListener("blur", noblur) - commandline_state.clInputFocused = true - Messaging.messageOwnTab("cl_input_focused", "unused") - const keysFromContentProcess = commandline_state.keysFromContentProcess; - if (keysFromContentProcess.length !== 0) { - logger.debug("Consuming " + JSON.stringify(keysFromContentProcess.slice(commandline_state.consumedKeyCount))); - for (let keyIndex = commandline_state.consumedKeyCount; keyIndex < keysFromContentProcess.length; keyIndex++) { - const key = keysFromContentProcess[keyIndex]; - commandline_state.clInput.value += key - clInputValueChanged() - commandline_state.consumedKeyCount++; - } - } -} - -export function bufferUntilClInputFocused(bufferedClInputKeys) { - logger.debug("Command line process received content process keys: " + bufferedClInputKeys) - if (bufferedClInputKeys.length < commandline_state.keysFromContentProcess.length) - return - commandline_state.keysFromContentProcess = bufferedClInputKeys; - const keysFromContentProcess = commandline_state.keysFromContentProcess; - if (commandline_state.clInputFocused) { - for (let keyIndex = commandline_state.consumedKeyCount; keyIndex < keysFromContentProcess.length; keyIndex++) { - const key = keysFromContentProcess[keyIndex]; - logger.debug("Dispatching received keydown event " + key + " since clInputFocused is true," + - " clInputNormalEventsReceived = " + commandline_state.clInputNormalEventsReceived + - " contentProcessKeysReceivedAfterClInputFocused = " + commandline_state.contentProcessKeysReceivedAfterClInputFocused + - " clInputInitialValue = " + commandline_state.clInputInitialValue) - if (!commandline_state.clInputNormalEventsReceived) { - // Insert at the end. - commandline_state.clInput.value += key + logger.debug("Called focus()") + Messaging.messageOwnTab("buffered_page_keys", "").then((bufferedPageKeys : string[]) => { + let clInputStillFocused = window.document.activeElement === commandline_state.clInput; + logger.debug("buffered_page_keys response received", bufferedPageKeys, + "clInputStillFocused = " + clInputStillFocused) + if (!clInputStillFocused) + return + if (bufferedPageKeys.length !== 0) { + const currentClInputValue = commandline_state.clInput.value; + // Native events are assumed to be characters added at the end of clInput. + const clInputNativeEventKeysReceived = commandline_state.initialClInputValue.length > currentClInputValue.length + logger.debug("Consuming buffered page keys", bufferedPageKeys, + "initialClInputValue = " + commandline_state.initialClInputValue, + "currentClInputValue = " + currentClInputValue); + if (clInputNativeEventKeysReceived) { + // Insert page keys at the end. + commandline_state.clInput.value += bufferedPageKeys.join("") } else { - // Normal keys are assumed to always be at the end. - let normalKeyCount = commandline_state.contentProcessKeysReceivedAfterClInputFocused; - let initialCommandLength = commandline_state.clInputInitialValue.length; - // Insert received key at the beginning. + let initialClInputValueLength = commandline_state.initialClInputValue.length; + // Insert page keys just after the command. commandline_state.clInput.value = - commandline_state.clInput.value.substring(0, initialCommandLength + normalKeyCount) - + key - + commandline_state.clInput.value.substring(initialCommandLength + normalKeyCount) - commandline_state.contentProcessKeysReceivedAfterClInputFocused++; + currentClInputValue.substring(0, initialClInputValueLength) + + bufferedPageKeys.join("") + + currentClInputValue.substring(initialClInputValueLength) } - commandline_state.consumedKeyCount++; + // Update completion. clInputValueChanged() } - } + }) } /** @hidden **/ @@ -241,7 +219,6 @@ commandline_state.clInput.addEventListener( function (keyevent: KeyboardEvent) { if (!keyevent.isTrusted) return logger.info("Called keydown event listener") - commandline_state.clInputNormalEventsReceived = true commandline_state.keyEvents.push(minimalKeyFromKeyboardEvent(keyevent)) const response = keyParser(commandline_state.keyEvents) if (response.isMatch) { @@ -407,7 +384,7 @@ export function fillcmdline( // 3. Stop forwarding key events from content process to command line process. if (trailspace) commandline_state.clInput.value = newcommand + " " else commandline_state.clInput.value = newcommand - commandline_state.clInputInitialValue = commandline_state.clInput.value + commandline_state.initialClInputValue = commandline_state.clInput.value commandline_state.isVisible = true let result = Promise.resolve([]) // Focus is lost for some reason. diff --git a/src/content/controller_content.ts b/src/content/controller_content.ts index 7be50df1..abd28261 100644 --- a/src/content/controller_content.ts +++ b/src/content/controller_content.ts @@ -193,9 +193,9 @@ function* ParserController() { if (response.exstr) { exstr = response.exstr if (exstr.startsWith("fillcmdline ")) { // Ugh. That's ugly. I needed a way to know if this command is going to open the cmdline. - logger.debug("Setting clInputFocused to false") - clInputFocused = false - bufferedClInputKeys = [] + logger.debug("Setting mustBufferPageKeysForClInput to true") + mustBufferPageKeysForClInput = true + bufferedPageKeys = [] } break } else { @@ -217,25 +217,27 @@ function* ParserController() { } } } -Messaging.addListener("cl_input_focused", () => { - logger.debug("Callback cl_input_focused") - clInputFocused = true +Messaging.addListener("buffered_page_keys", (message, sender, sendResponse) => { + logger.debug("buffered_page_keys request received, responding with", bufferedPageKeys) + // At this point, clInput is focused and the page will not get any more keyboard events. + sendResponse(Promise.resolve(bufferedPageKeys)) + mustBufferPageKeysForClInput = false + bufferedPageKeys = [] }) -let clInputFocused: boolean = true -let bufferedClInputKeys: string[] = [] +let mustBufferPageKeysForClInput = false +let bufferedPageKeys: string[] = [] export const generator = ParserController() // var rather than let stops weirdness in repl. generator.next() /** Feed keys to the ParserController */ export function acceptKey(keyevent: KeyboardEvent) { - logger.debug("clInputFocused = " + clInputFocused) - if (!clInputFocused) { + logger.debug("mustBufferPageKeysForClInput = " + mustBufferPageKeysForClInput) + if (mustBufferPageKeysForClInput) { let isCharacterKey = keyevent.key.length == 1 && !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey; if (isCharacterKey) { - bufferedClInputKeys.push(keyevent.key); - logger.debug("Sending keyboardEvent for buffering ", bufferedClInputKeys) - Messaging.messageOwnTab("commandline_frame", "bufferUntilClInputFocused", [ bufferedClInputKeys ]); + bufferedPageKeys.push(keyevent.key); + logger.debug("Buffering page keys for clInput", bufferedPageKeys) } keyevent.preventDefault() keyevent.stopImmediatePropagation() diff --git a/src/lib/commandline_cmds.ts b/src/lib/commandline_cmds.ts index 643084b0..754e5f2b 100644 --- a/src/lib/commandline_cmds.ts +++ b/src/lib/commandline_cmds.ts @@ -138,13 +138,6 @@ export function getCommandlineFns(cmdline_state: { ) cmdline_state.activeCompletions = undefined cmdline_state.isVisible = false - console.debug("Called hide_and_clear()") - cmdline_state.keysFromContentProcess = [] - cmdline_state.consumedKeyCount = 0 - cmdline_state.clInputFocused = false - cmdline_state.clInputNormalEventsReceived = false - cmdline_state.clInputInitialValue = "" - cmdline_state.contentProcessKeysReceivedAfterClInputFocused = 0 }, /**