From bffd87fbd5014dd9e19030161071f07b762ebdbc Mon Sep 17 00:00:00 2001 From: jahorton Date: Sat, 21 Mar 2020 22:50:14 +0700 Subject: [PATCH 1/3] refactor(web/engine): headless-friendly keymapping --- web/source/kmwbase.ts | 4 --- web/source/text/kbdInterface.ts | 5 ++-- .../{kmwkeymaps.ts => text/keyMapping.ts} | 28 ++++++++++++------- web/source/text/processor.ts | 23 +++++++-------- 4 files changed, 30 insertions(+), 30 deletions(-) rename web/source/{kmwkeymaps.ts => text/keyMapping.ts} (85%) diff --git a/web/source/kmwbase.ts b/web/source/kmwbase.ts index 174dacc1d6..fac37a5dd4 100644 --- a/web/source/kmwbase.ts +++ b/web/source/kmwbase.ts @@ -16,8 +16,6 @@ /// // Defines keyboard data & management classes. /// -// Defines built-in keymapping. -/// // Defines KMW's hotkey management object. /// // Defines the ui management code that tracks UI activation and such. @@ -72,7 +70,6 @@ namespace com.keyman { domManager: DOMManager; hotkeyManager: HotkeyManager; uiManager: UIManager; - keyMapManager: KeyMapManager; textProcessor: text.Processor; modelManager: text.prediction.ModelManager; @@ -118,7 +115,6 @@ namespace com.keyman { this.domManager = new DOMManager(this); this.hotkeyManager = new HotkeyManager(this); this.uiManager = new UIManager(this); - this.keyMapManager = new KeyMapManager(); this.textProcessor = new text.Processor(); // Used by the embedded apps. this['interface'] = this.textProcessor.keyboardInterface; diff --git a/web/source/text/kbdInterface.ts b/web/source/text/kbdInterface.ts index 7e5f0fdd6a..a1af899f45 100644 --- a/web/source/text/kbdInterface.ts +++ b/web/source/text/kbdInterface.ts @@ -562,12 +562,11 @@ namespace com.keyman.text { * @return {boolean} true if keypress event * Description Test if event as a keypress event */ - isKeypress(e: KeyEvent):boolean { - let keyman = com.keyman.singleton; + isKeypress(e: KeyEvent): boolean { if(this.activeKeyboard.isMnemonic) { // I1380 - support KIK for positional layouts return !e.LisVirtualKey; // will now return true for U_xxxx keys, but not for T_xxxx keys } else { - return keyman.keyMapManager._USKeyCodeToCharCode(e) ? true : false; // I1380 - support KIK for positional layouts + return KeyMapping._USKeyCodeToCharCode(e) ? true : false; // I1380 - support KIK for positional layouts } } diff --git a/web/source/kmwkeymaps.ts b/web/source/text/keyMapping.ts similarity index 85% rename from web/source/kmwkeymaps.ts rename to web/source/text/keyMapping.ts index a81faaea54..9ecb711627 100644 --- a/web/source/kmwkeymaps.ts +++ b/web/source/text/keyMapping.ts @@ -45,17 +45,17 @@ namespace com.keyman { } } - export class KeyMapManager { - browserMap: BrowserKeyMaps = new BrowserKeyMaps(); - languageMap: LanguageKeyMaps = new LanguageKeyMaps(); + export class KeyMapping { + static readonly browserMap: BrowserKeyMaps = new BrowserKeyMaps(); + static readonly languageMap: LanguageKeyMaps = new LanguageKeyMaps(); - _usCharCodes: KeyMap[]; + private static _usCharCodes: KeyMap[]; - constructor() { - this._usCodeInit(); + private constructor() { + // Do not construct this class. } - _usCodeInit() { + private static _usCodeInit() { var s0=new KeyMap(),s1=new KeyMap(); s0['k192'] = 96; @@ -154,7 +154,7 @@ namespace com.keyman { s1['k190'] = 62; s1['k191'] = 63; - this._usCharCodes = [s0,s1]; + KeyMapping._usCharCodes = [s0,s1]; } /** @@ -164,8 +164,16 @@ namespace com.keyman { * @return {number} Character code * Description Translate keyboard codes to standard US layout codes */ - _USKeyCodeToCharCode(Levent: com.keyman.text.KeyEvent) { - return this._usCharCodes[Levent.Lmodifiers & 0x10 ? 1 : 0]['k'+Levent.Lcode]; + static _USKeyCodeToCharCode(Levent: com.keyman.text.KeyEvent) { + return KeyMapping.usCharCodes[Levent.Lmodifiers & 0x10 ? 1 : 0]['k'+Levent.Lcode]; }; + + public static get usCharCodes() { + if(!KeyMapping._usCharCodes) { + KeyMapping._usCodeInit(); + } + + return KeyMapping._usCharCodes; + } } } \ No newline at end of file diff --git a/web/source/text/processor.ts b/web/source/text/processor.ts index face73c9da..4db4fdabdc 100644 --- a/web/source/text/processor.ts +++ b/web/source/text/processor.ts @@ -6,6 +6,8 @@ /// // Defines the keyboard wrapper object. /// +// Defines built-in keymapping. +/// namespace com.keyman.text { export class LegacyKeyEvent { @@ -221,7 +223,7 @@ namespace com.keyman.text { // Support version 1.0 KeymanWeb keyboards that do not define positional vs mnemonic if(!activeKeyboard.definesPositionalOrMnemonic) { - Lkc.Lcode=keyman.keyMapManager._USKeyCodeToCharCode(Lkc); + Lkc.Lcode = KeyMapping._USKeyCodeToCharCode(Lkc); Lkc.LisVirtualKey=false; } @@ -259,13 +261,12 @@ namespace com.keyman.text { processKeystroke(keyEvent: KeyEvent, outputTarget: OutputTarget, fromOSK: boolean): RuleBehavior { let keyman = com.keyman.singleton; - let keyMapManager = keyman.keyMapManager; if(!keyman.isEmbedded && !fromOSK && keyman.util.device.browser == 'firefox') { // I1466 - Convert the - keycode on mnemonic as well as positional layouts // FireFox, Mozilla Suite - if(keyMapManager.browserMap.FF['k'+keyEvent.Lcode]) { - keyEvent.Lcode=keyMapManager.browserMap.FF['k'+keyEvent.Lcode]; + if(KeyMapping.browserMap.FF['k'+keyEvent.Lcode]) { + keyEvent.Lcode=KeyMapping.browserMap.FF['k'+keyEvent.Lcode]; } } @@ -306,6 +307,7 @@ namespace com.keyman.text { */ processKeyEvent(keyEvent: KeyEvent, e?: osk.KeyElement | boolean): boolean { let keyman = com.keyman.singleton; + let formFactor = keyman.util.device.formFactor; // Determine the current target for text output and create a "mock" backup // of its current, pre-input state. @@ -318,9 +320,6 @@ namespace com.keyman.text { e = null as osk.KeyElement; // Cast is necessary for TS type-checking later in the method. } - let formFactor = keyman.util.device.formFactor; - let keyMapManager = keyman.keyMapManager; - this.swallowKeypress = false; if(fromOSK && !keyman.isEmbedded) { @@ -355,8 +354,8 @@ namespace com.keyman.text { if(!keyman.isEmbedded && !fromOSK && keyman.util.device.browser == 'firefox') { // I1466 - Convert the - keycode on mnemonic as well as positional layouts // FireFox, Mozilla Suite - if(keyMapManager.browserMap.FF['k'+keyEvent.Lcode]) { - keyEvent.Lcode=keyMapManager.browserMap.FF['k'+keyEvent.Lcode]; + if(KeyMapping.browserMap.FF['k'+keyEvent.Lcode]) { + keyEvent.Lcode = KeyMapping.browserMap.FF['k'+keyEvent.Lcode]; } } //else //{ @@ -1161,14 +1160,12 @@ namespace com.keyman.text { var LisVirtualKeyCode = (typeof e.charCode != 'undefined' && e.charCode != null && (e.charCode == 0 || (s.Lmodifiers & 0x6F) != 0)); s.LisVirtualKey = LisVirtualKeyCode || e.type != 'keypress'; - let keyMapManager = keyman.keyMapManager; - // Other minor physical-keyboard adjustments if(activeKeyboard && !activeKeyboard.isMnemonic) { // Positional Layout /* 13/03/2007 MCD: Swedish: Start mapping of keystroke to US keyboard */ - var Lbase=keyMapManager.languageMap[com.keyman.osk.Layouts._BaseLayout]; + var Lbase = KeyMapping.languageMap[com.keyman.osk.Layouts._BaseLayout]; if(Lbase && Lbase['k'+s.Lcode]) { s.Lcode=Lbase['k'+s.Lcode]; } @@ -1177,7 +1174,7 @@ namespace com.keyman.text { if(!activeKeyboard.definesPositionalOrMnemonic && !(s.Lmodifiers & 0x60)) { // Support version 1.0 KeymanWeb keyboards that do not define positional vs mnemonic s = { - Lcode: keyMapManager._USKeyCodeToCharCode(s), + Lcode: KeyMapping._USKeyCodeToCharCode(s), Ltarg: s.Ltarg, Lmodifiers: 0, LisVirtualKey: false, From 1b1a79fae67e7fd17bb7bf47e9c251ddf8cb15d8 Mon Sep 17 00:00:00 2001 From: jahorton Date: Sat, 21 Mar 2020 23:11:51 +0700 Subject: [PATCH 2/3] refactor(web/engine): base layout now set on Processor --- web/source/keymanweb.ts | 9 --------- web/source/kmwbase.ts | 15 +++++++++++++-- web/source/osk/defaultLayouts.ts | 6 ------ web/source/text/processor.ts | 7 +++++-- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/web/source/keymanweb.ts b/web/source/keymanweb.ts index 4c5d793d7b..bb5783815d 100644 --- a/web/source/keymanweb.ts +++ b/web/source/keymanweb.ts @@ -107,15 +107,6 @@ if(!window['keyman']['initialized']) { keymanweb.delayedInit(); - // I732 START - Support for European underlying keyboards #1 - if(typeof(window['KeymanWeb_BaseLayout']) !== 'undefined') - com.keyman.osk.Layouts._BaseLayout = window['KeymanWeb_BaseLayout']; - else - com.keyman.osk.Layouts._BaseLayout = 'us'; - - - keymanweb._BrowserIsSafari = (navigator.userAgent.indexOf('AppleWebKit') >= 0); // I732 END - Support for European underlying keyboards #1 - //TODO: find all references to next three routines and disambiguate!! // Complete page initialization only after the page is fully loaded, including any embedded fonts diff --git a/web/source/kmwbase.ts b/web/source/kmwbase.ts index fac37a5dd4..61b6d363e0 100644 --- a/web/source/kmwbase.ts +++ b/web/source/kmwbase.ts @@ -53,7 +53,8 @@ namespace com.keyman { initialized: number; // Signals the initialization state of the KeymanWeb system. isHeadless = false; // Indicates that KMW lacks any access to the DOM. Nothing yet implemented for '= true'. - 'build' = 300; // TS needs this to be defined within the class. + 'build' = 300; // TS needs this to be defined within the class. + _BrowserIsSafari: boolean; // A legacy browser-check variable. // Used as placeholders during initialization. // The corresponding class properties should be dropped after a refactor; @@ -115,7 +116,17 @@ namespace com.keyman { this.domManager = new DOMManager(this); this.hotkeyManager = new HotkeyManager(this); this.uiManager = new UIManager(this); - this.textProcessor = new text.Processor(); + + // I732 START - Support for European underlying keyboards #1 + var baseLayout: string; + if(typeof(window['KeymanWeb_BaseLayout']) !== 'undefined') { + baseLayout = window['KeymanWeb_BaseLayout']; + } else { + baseLayout = 'us'; + } + this._BrowserIsSafari = (navigator.userAgent.indexOf('AppleWebKit') >= 0); // I732 END - Support for European underlying keyboards #1 + + this.textProcessor = new text.Processor(baseLayout); // Used by the embedded apps. this['interface'] = this.textProcessor.keyboardInterface; diff --git a/web/source/osk/defaultLayouts.ts b/web/source/osk/defaultLayouts.ts index 5c24fd7654..4880de8e2e 100644 --- a/web/source/osk/defaultLayouts.ts +++ b/web/source/osk/defaultLayouts.ts @@ -101,12 +101,6 @@ namespace com.keyman.osk { 'leftctrl-shift': '*LCtrlShift*', 'rightctrl-shift': '*RCtrlShift*' }; - - static _BaseLayout: string = 'us'; // default BaseLayout - static _BaseLayoutEuro: {[code: string]: string} = { - 'se': '\u00a71234567890+´~~~QWERTYUIOP\u00c5\u00a8\'~~~ASDFGHJKL\u00d6\u00c4~~~~~ Date: Wed, 25 Mar 2020 09:27:21 +0700 Subject: [PATCH 3/3] docs(web/engine): preserves old definitions as comment with keymapping --- web/source/text/keyMapping.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/web/source/text/keyMapping.ts b/web/source/text/keyMapping.ts index 9ecb711627..cb45620394 100644 --- a/web/source/text/keyMapping.ts +++ b/web/source/text/keyMapping.ts @@ -24,6 +24,11 @@ namespace com.keyman { class LanguageKeyMaps { [languageCode: string]: KeyMap; + // // Here are some old legacy definitions that were no longer referenced but are likely related: + // static _BaseLayoutEuro: {[code: string]: string} = { + // 'se': '\u00a71234567890+´~~~QWERTYUIOP\u00c5\u00a8\'~~~ASDFGHJKL\u00d6\u00c4~~~~~