From e7a1496fcbcfc11028d885ae50488e577009548d Mon Sep 17 00:00:00 2001 From: jahorton Date: Mon, 23 Mar 2020 11:16:20 +0700 Subject: [PATCH] refactor(web/engine): more PR review addressing --- web/source/text/defaultOutput.ts | 17 +++++++++++++---- web/source/text/processor.ts | 14 ++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/web/source/text/defaultOutput.ts b/web/source/text/defaultOutput.ts index 77b9d12150..7f5bf3d157 100644 --- a/web/source/text/defaultOutput.ts +++ b/web/source/text/defaultOutput.ts @@ -4,6 +4,12 @@ /// namespace com.keyman.text { + export enum EmulationKeystrokes { + Space = ' ', + Enter = '\n', + Backspace = '\b' + } + /** * Defines a collection of static library functions that define KeymanWeb's default (implied) keyboard rule behaviors. */ @@ -141,18 +147,21 @@ namespace com.keyman.text { * Codes matched here generally have default implementations when in a browser but require emulation * for 'synthetic' `OutputTarget`s like `Mock`s, which have no default text handling. */ - public static forSpecialEmulation(Lkc: KeyEvent): string { + public static forSpecialEmulation(Lkc: KeyEvent): EmulationKeystrokes { let code = DefaultOutput.codeForEvent(Lkc); switch(code) { case Codes.keyCodes['K_BKSP']: - return '\b'; + return EmulationKeystrokes.Backspace; case Codes.keyCodes['K_ENTER']: - return '\n'; + return EmulationKeystrokes.Enter; + // (Probably) only here for legacy reasons; it's always been handled alongside the other two. case Codes.keyCodes['K_SPACE']: - return ' '; + return EmulationKeystrokes.Space; // case Codes.keyCodes['K_DEL']: // return '\u007f'; // 127, ASCII / Unicode control code for DEL. + default: + return null; } } diff --git a/web/source/text/processor.ts b/web/source/text/processor.ts index 42f071b106..2e5222c8af 100644 --- a/web/source/text/processor.ts +++ b/web/source/text/processor.ts @@ -53,6 +53,7 @@ namespace com.keyman.text { let matched = false; var char = ''; + var special: EmulationKeystrokes; if(usingOSK || outputTarget.isSynthetic) { matched = true; // All the conditions below result in matches until the final else, which restores the expected default // if no match occurs. @@ -63,22 +64,23 @@ namespace com.keyman.text { // We'd rather let the browser handle these keys, but we're using emulated keystrokes, forcing KMW // to emulate default behavior here. - } else if(char = DefaultOutput.forSpecialEmulation(Lkc)) { - switch(char) { - case '\b': + } else if(special = DefaultOutput.forSpecialEmulation(Lkc)) { + switch(special) { + case EmulationKeystrokes.Backspace: keyman.interface.defaultBackspace(outputTarget); break; - case '\n': + case EmulationKeystrokes.Enter: outputTarget.handleNewlineAtCaret(); break; - case ' ': + case EmulationKeystrokes.Space: keyman.interface.output(0, outputTarget, ' '); break; // case '\u007f': // K_DEL // // For (possible) future implementation. // // Would recommend (conceptually) equaling K_RIGHT + K_BKSP, the former of which would technically be a 'command'. default: - ruleBehavior.errorLog = "Unexpected 'special emulation' character (\\u" + char.kmwCharCodeAt(0).toString(16) + ") went unhandled!"; + // In case we extend the allowed set, but forget to implement its handling case above. + ruleBehavior.errorLog = "Unexpected 'special emulation' character (\\u" + (special as String).kmwCharCodeAt(0).toString(16) + ") went unhandled!"; } } else { // Back to the standard default, pending normal matching.