From 80913fa1db8ec0527d495bd8844cd9deaf55ab41 Mon Sep 17 00:00:00 2001 From: jahorton Date: Fri, 17 Apr 2020 09:21:45 +0700 Subject: [PATCH] fix(web/engine): better shorthand API management --- .../src/text/kbdInterface.ts | 74 ++++++++++--------- .../tests/cases/basic-init.js | 3 +- web/source/kmwapi.ts | 5 ++ 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/common/core/web/keyboard-processor/src/text/kbdInterface.ts b/common/core/web/keyboard-processor/src/text/kbdInterface.ts index ab576660a8..bb81bc46b5 100644 --- a/common/core/web/keyboard-processor/src/text/kbdInterface.ts +++ b/common/core/web/keyboard-processor/src/text/kbdInterface.ts @@ -958,39 +958,47 @@ namespace com.keyman.text { return behavior; } + + // Publishes the KeyboardInterface's shorthand API names. + // Note that this may need to be called multiple times; a keyboard-processor consumer + // may extend or overwrite some of the callbacks after this method's initial call. + static __publishShorthandAPI() { + // Keyboard callbacks + let prototype = this.prototype; + + var exportKBCallback = function(miniName: string, longName: string) { + prototype[miniName] = prototype[longName]; + } + + exportKBCallback('KSF', 'saveFocus'); + exportKBCallback('KBR', 'beepReset'); + exportKBCallback('KT', 'insertText'); + exportKBCallback('KR', 'registerKeyboard'); + exportKBCallback('KRS', 'registerStub'); + exportKBCallback('KC', 'context'); + exportKBCallback('KN', 'nul'); + exportKBCallback('KCM', 'contextMatch'); + exportKBCallback('KFCM', 'fullContextMatch'); + exportKBCallback('KIK', 'isKeypress'); + exportKBCallback('KKM', 'keyMatch'); + exportKBCallback('KSM', 'stateMatch'); + exportKBCallback('KKI', 'keyInformation'); + exportKBCallback('KDM', 'deadkeyMatch'); + exportKBCallback('KB', 'beep'); + exportKBCallback('KA', 'any'); + exportKBCallback('KDC', 'deleteContext'); + exportKBCallback('KO', 'output'); + exportKBCallback('KDO', 'deadkeyOutput'); + exportKBCallback('KIO', 'indexOutput'); + exportKBCallback('KIFS', 'ifStore'); + exportKBCallback('KSETS', 'setStore'); + exportKBCallback('KLOAD', 'loadStore'); + exportKBCallback('KSAVE', 'saveStore'); + } } - // Keyboard callbacks -(function() { - let prototype = KeyboardInterface.prototype; - - var exportKBCallback = function(miniName: string, longName: string) { - prototype[miniName] = prototype[longName]; - } - - exportKBCallback('KSF', 'saveFocus'); - exportKBCallback('KBR', 'beepReset'); - exportKBCallback('KT', 'insertText'); - exportKBCallback('KR', 'registerKeyboard'); - exportKBCallback('KRS', 'registerStub'); - exportKBCallback('KC', 'context'); - exportKBCallback('KN', 'nul'); - exportKBCallback('KCM', 'contextMatch'); - exportKBCallback('KFCM', 'fullContextMatch'); - exportKBCallback('KIK', 'isKeypress'); - exportKBCallback('KKM', 'keyMatch'); - exportKBCallback('KSM', 'stateMatch'); - exportKBCallback('KKI', 'keyInformation'); - exportKBCallback('KDM', 'deadkeyMatch'); - exportKBCallback('KB', 'beep'); - exportKBCallback('KA', 'any'); - exportKBCallback('KDC', 'deleteContext'); - exportKBCallback('KO', 'output'); - exportKBCallback('KDO', 'deadkeyOutput'); - exportKBCallback('KIO', 'indexOutput'); - exportKBCallback('KIFS', 'ifStore'); - exportKBCallback('KSETS', 'setStore'); - exportKBCallback('KLOAD', 'loadStore'); - exportKBCallback('KSAVE', 'saveStore'); -}()); + (function() { + // This will be the only call within the keyboard-processor module. + KeyboardInterface.__publishShorthandAPI(); + }()); } \ No newline at end of file diff --git a/common/core/web/keyboard-processor/tests/cases/basic-init.js b/common/core/web/keyboard-processor/tests/cases/basic-init.js index b87768e62b..3b480a1547 100644 --- a/common/core/web/keyboard-processor/tests/cases/basic-init.js +++ b/common/core/web/keyboard-processor/tests/cases/basic-init.js @@ -12,8 +12,7 @@ global.keyman = {}; // So that keyboard-based checks against the global `keyman` // Initialize supplementary plane string extensions String.kmwEnableSupplementaryPlane(false); -// Test the top-level LMLayer interface. -// Note: these tests can only be run after BOTH stages of compilation are completed. +// Test the KeyboardProcessor interface. describe('KeyboardProcessor', function() { describe('[[constructor]]', function () { it('should initialize without errors', function () { diff --git a/web/source/kmwapi.ts b/web/source/kmwapi.ts index 01010574e6..d00d277d9d 100644 --- a/web/source/kmwapi.ts +++ b/web/source/kmwapi.ts @@ -20,4 +20,9 @@ publishAPI("_GetAbsoluteY", "getAbsoluteY"); publishAPI("_GetAbsolute", "getAbsolute"); publishAPI("toNzString", "nzString"); +}()); + +(function() { + // DOM-aware KeymanWeb overwrites some of the API functions. + com.keyman.text.KeyboardInterface.__publishShorthandAPI(); }()); \ No newline at end of file