Skip to content

Commit

Permalink
refactor(web): work around global in interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ermshiperete committed Aug 12, 2024
1 parent c09c895 commit bba694c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
19 changes: 5 additions & 14 deletions common/web/keyboard-processor/src/text/keyEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,14 @@ import { type DeviceSpec } from "@keymanapp/web-utils";
import Codes from './codes.js';
import { ActiveKeyBase } from "../index.js";

interface DefaultRulesInterface {
export interface DefaultRulesInterface {
forAny(Lkc: KeyEvent, isMnemonic: boolean): string;
}

export class BASE_DEFAULT_RULES {
private static _instance: DefaultRulesInterface;
let defaultRules: DefaultRulesInterface;

// Prevent direct instantiation.
private constructor() { }

public static forAny(Lkc: KeyEvent, isMnemonic: boolean): string {
return this._instance.forAny(Lkc, isMnemonic);
}

public static set instance(value: DefaultRulesInterface) {
this._instance = value;
}
export function setDefaultRules(rules: DefaultRulesInterface) {
defaultRules = rules;
}

// Represents a probability distribution over a keyboard's keys.
Expand Down Expand Up @@ -168,7 +159,7 @@ export default class KeyEvent implements KeyEventSpec {
// the actual keyname instead.
mappingEvent.kName = 'K_xxxx';
mappingEvent.Lmodifiers = (shifted ? 0x10 : 0); // mnemonic lookups only exist for default & shift layers.
var mappedChar: string = BASE_DEFAULT_RULES.forAny(mappingEvent, true);
var mappedChar: string = defaultRules.forAny(mappingEvent, true);

/* First, save a backup of the original code. This one won't needlessly trigger keyboard
* rules, but allows us to replicate/emulate commands after rule processing if needed.
Expand Down
12 changes: 4 additions & 8 deletions web/src/engine/js-processor/src/defaultRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
*/

import { ModifierKeyConstants } from '@keymanapp/common-types';
import { BASE_DEFAULT_RULES, Codes, type KeyEvent, type OutputTarget } from "@keymanapp/keyboard-processor";
import { Codes, type KeyEvent, type OutputTarget, setDefaultRules } from "@keymanapp/keyboard-processor";
import RuleBehavior from "./ruleBehavior.js";

export enum EmulationKeystrokes {
Enter = '\n',
Backspace = '\b'
}


/**
* Defines a collection of static library functions that define KeymanWeb's default (implied) keyboard rule behaviors.
*/
export default class DefaultRules {
public constructor() {
setDefaultRules(this);
}

codeForEvent(Lkc: KeyEvent) {
Expand All @@ -28,7 +30,7 @@ export default class DefaultRules {
* Serves as a default keycode lookup table. This may be referenced safely by mnemonic handling without fear of side-effects.
* Also used by Processor.defaultRuleBehavior to generate output after filtering for special cases.
*/
public forAny(Lkc: KeyEvent, isMnemonic: boolean, ruleBehavior?: RuleBehavior) {
public forAny(Lkc: KeyEvent, isMnemonic: boolean, ruleBehavior?: RuleBehavior): string {
var char = '';

// A pretty simple table of lookups, corresponding VERY closely to the original defaultKeyOutput.
Expand Down Expand Up @@ -217,9 +219,3 @@ export default class DefaultRules {
return null;
}
}

/**
* A simple instance of the standard 'default rules' for keystroke processing from the
* DefaultRules base class.
*/
BASE_DEFAULT_RULES.instance = new DefaultRules();

0 comments on commit bba694c

Please sign in to comment.