Skip to content

Commit

Permalink
Merge pull request #12278 from keymanapp/fix/developer/12056-find-las…
Browse files Browse the repository at this point in the history
…t-key-in-ldml-keybag-for-kvk

fix(developer): find last matching key in LDML key bag when building KVK
  • Loading branch information
mcdurdin authored Aug 26, 2024
2 parents f9b0dc5 + 6353f92 commit 89d65ad
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion developer/src/kmc-ldml/src/compiler/visual-keyboard-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { LDMLKeyboard, CompilerCallbacks } from "@keymanapp/developer-utils";
import { KeysCompiler } from "./keys.js";
import { LdmlCompilerMessages } from "./ldml-compiler-messages.js";

// This is a partial polyfill for findLast, so not polluting Array.prototype
// https://medium.com/@stheodorejohn/findlast-method-polyfill-in-javascript-bridging-browser-gaps-c3baf6aabae1
// TODO: remove and replace with Array.prototype.findLast when it is
// well-supported
function findLast(arr: any, callback: any) {
if (!arr) {
return undefined;
}
const len = arr.length >>> 0;
for (let i = len - 1; i >= 0; i--) {
if (callback(arr[i], i, arr)) {
return arr[i];
}
}
return undefined;
}

export class LdmlKeyboardVisualKeyboardCompiler {
public constructor(private callbacks: CompilerCallbacks) {
}
Expand Down Expand Up @@ -57,7 +74,8 @@ export class LdmlKeyboardVisualKeyboardCompiler {
const keyId = key;
x++;

let keydef = source.keyboard3.keys?.key?.find(x => x.id == key);
//@ts-ignore
let keydef = findLast(source.keyboard3.keys?.key, x => x.id == key);

if (!keydef) {
this.callbacks.reportMessage(
Expand Down

0 comments on commit 89d65ad

Please sign in to comment.