From 5769c80d12730a4f0be9c8f98caf10c15fcb4a14 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Wed, 12 Jan 2022 12:09:24 +0700 Subject: [PATCH 1/4] fix(web): Fix layers for embedded longpress keys --- .../KMEA/app/src/main/assets/keyboard.html | 3 +- web/source/kmwembedded.ts | 30 ++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/android/KMEA/app/src/main/assets/keyboard.html b/android/KMEA/app/src/main/assets/keyboard.html index d0d4f93cf0..e938cb9558 100644 --- a/android/KMEA/app/src/main/assets/keyboard.html +++ b/android/KMEA/app/src/main/assets/keyboard.html @@ -248,7 +248,8 @@ var keyPos = x.toString() + ',' + y.toString(); for(i=0; i 0) ? + keyName.substring(0, functionalLayerSeparatorIndex).lastIndexOf('-') : + keyName.lastIndexOf('-'); if (separatorIndex > 0) { + displayLayer = keyName.substring(0, separatorIndex); keyName = keyName.substring(separatorIndex+1); } + if(displayLayer == 'undefined') { + displayLayer=keymanweb.core.keyboardProcessor.layerId; + } + + // Determine the "functional" layer from the coreID + let layer = displayLayer; + separatorIndex = originalKeyName.lastIndexOf('+'); + if (separatorIndex > 0) { + layer = originalKeyName.substring(separatorIndex+1); + } // Note: this assumes Lelem is properly attached and has an element interface. // Currently true in the Android and iOS apps. @@ -384,6 +401,11 @@ namespace com.keyman.text { } else { console.warn("No base key exists for the subkey being executed: '" + origArg + "'"); } + + // Now that we've checked if key is found, split "functional" layer from keyName + if (separatorIndex > 0) { + keyName = keyName.substring(0, separatorIndex); + } }; /** From 1eeb5b1da92f228839679850718eefa2914fce97 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Wed, 12 Jan 2022 14:58:14 +0700 Subject: [PATCH 2/4] fix(web): Use regex to determine layers --- web/source/kmwembedded.ts | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/web/source/kmwembedded.ts b/web/source/kmwembedded.ts index 981522ecdc..53cbcd017a 100644 --- a/web/source/kmwembedded.ts +++ b/web/source/kmwembedded.ts @@ -365,27 +365,24 @@ namespace com.keyman.text { // Changes for Build 353 to resolve KMEI popup key issues keyName=keyName.replace('popup-',''); //remove popup prefix if present (unlikely) - // Determine display layer (and ignore functional layer if it exists). - // Can't just split on '-' because some layers like ctrl-shift contain it. - let originalKeyName = keyName; let displayLayer = keymanweb.core.keyboardProcessor.layerId; - let functionalLayerSeparatorIndex = keyName.indexOf('+'); - let separatorIndex = (functionalLayerSeparatorIndex > 0) ? - keyName.substring(0, functionalLayerSeparatorIndex).lastIndexOf('-') : - keyName.lastIndexOf('-'); - if (separatorIndex > 0) { - displayLayer = keyName.substring(0, separatorIndex); - keyName = keyName.substring(separatorIndex+1); - } - if(displayLayer == 'undefined') { - displayLayer=keymanweb.core.keyboardProcessor.layerId; + let functionalLayer = ''; + + // Regex for 'display layer'-'virtual key name'+'optional functional layer' + // Can't just split on '-' because some layers like ctrl-shift contain it. + // Virtual key can be T_, U_, K_, or ISO 9995 + let rx = new RegExp("(.*)-([TKU]_[^+]*|[A-E]\\d\\d)\\+?(.*)?"); + let matches = keyName.match(rx); + displayLayer = matches[1]; + keyName = matches[2]; + if (matches[3]) { + // Optional function layer + functionalLayer = matches[3] + keyName += '+' + functionalLayer; } - // Determine the "functional" layer from the coreID - let layer = displayLayer; - separatorIndex = originalKeyName.lastIndexOf('+'); - if (separatorIndex > 0) { - layer = originalKeyName.substring(separatorIndex+1); + if(displayLayer == 'undefined') { + displayLayer=keymanweb.core.keyboardProcessor.layerId; } // Note: this assumes Lelem is properly attached and has an element interface. @@ -401,11 +398,6 @@ namespace com.keyman.text { } else { console.warn("No base key exists for the subkey being executed: '" + origArg + "'"); } - - // Now that we've checked if key is found, split "functional" layer from keyName - if (separatorIndex > 0) { - keyName = keyName.substring(0, separatorIndex); - } }; /** From 77ac5f112294f7551cbdc13fd0e503fa82d037d6 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Thu, 13 Jan 2022 13:00:40 +0700 Subject: [PATCH 3/4] chore(web): Add some sanity checking --- web/source/kmwembedded.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/web/source/kmwembedded.ts b/web/source/kmwembedded.ts index 53cbcd017a..9714a85549 100644 --- a/web/source/kmwembedded.ts +++ b/web/source/kmwembedded.ts @@ -366,25 +366,23 @@ namespace com.keyman.text { keyName=keyName.replace('popup-',''); //remove popup prefix if present (unlikely) let displayLayer = keymanweb.core.keyboardProcessor.layerId; - let functionalLayer = ''; // Regex for 'display layer'-'virtual key name'+'optional functional layer' // Can't just split on '-' because some layers like ctrl-shift contain it. // Virtual key can be T_, U_, K_, or ISO 9995 let rx = new RegExp("(.*)-([TKU]_[^+]*|[A-E]\\d\\d)\\+?(.*)?"); let matches = keyName.match(rx); + if (matches == null) { + return false; + } displayLayer = matches[1]; keyName = matches[2]; + let functionalLayer = ''; if (matches[3]) { - // Optional function layer - functionalLayer = matches[3] + functionalLayer = matches[3]; keyName += '+' + functionalLayer; } - if(displayLayer == 'undefined') { - displayLayer=keymanweb.core.keyboardProcessor.layerId; - } - // Note: this assumes Lelem is properly attached and has an element interface. // Currently true in the Android and iOS apps. var Lelem=keymanweb.domManager.lastActiveElement; From f11fab54c222b4b329dc852c996c8dbff5cdf55c Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Mon, 17 Jan 2022 09:54:50 +0700 Subject: [PATCH 4/4] fix(web): Incorporate review comments Fix regex and optimize code and remove unused displayLayer --- web/source/kmwembedded.ts | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/web/source/kmwembedded.ts b/web/source/kmwembedded.ts index 9714a85549..f0f0c68cb1 100644 --- a/web/source/kmwembedded.ts +++ b/web/source/kmwembedded.ts @@ -365,24 +365,18 @@ namespace com.keyman.text { // Changes for Build 353 to resolve KMEI popup key issues keyName=keyName.replace('popup-',''); //remove popup prefix if present (unlikely) - let displayLayer = keymanweb.core.keyboardProcessor.layerId; - // Regex for 'display layer'-'virtual key name'+'optional functional layer' // Can't just split on '-' because some layers like ctrl-shift contain it. - // Virtual key can be T_, U_, K_, or ISO 9995 - let rx = new RegExp("(.*)-([TKU]_[^+]*|[A-E]\\d\\d)\\+?(.*)?"); - let matches = keyName.match(rx); + // Virtual key name starts with T_, K_, or U_ + // matches[1]: displayLayer (not used) + // matches[2]: keyId + // matches[3]: optional functionalLayer + let matches = keyName.match(/^(.+)-([TKU]_[^+]+)\+?(.+)?$/); if (matches == null) { return false; } - displayLayer = matches[1]; - keyName = matches[2]; - let functionalLayer = ''; - if (matches[3]) { - functionalLayer = matches[3]; - keyName += '+' + functionalLayer; - } - + keyName = matches[2] + (matches[3] ? '+' + matches[3] : ''); + // Note: this assumes Lelem is properly attached and has an element interface. // Currently true in the Android and iOS apps. var Lelem=keymanweb.domManager.lastActiveElement;