Skip to content

Commit

Permalink
fix: ligature support
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Aug 7, 2024
1 parent 4673be4 commit 20716e5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/hydrogen
22
8 changes: 4 additions & 4 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ exports[`parseMSKLC should parse a layout 1`] = `
"KeyA": "å",
"KeyB": "",
"KeyC": "č",
"KeyD": "%%",
"KeyD": "",
"KeyE": "ě",
"KeyF": "đ",
"KeyG": "",
Expand All @@ -202,7 +202,7 @@ exports[`parseMSKLC should parse a layout 1`] = `
"KeyQ": "ś",
"KeyR": "ŕ",
"KeyS": "š",
"KeyT": "%%",
"KeyT": "",
"KeyU": "ų",
"KeyV": "ć",
"KeyW": "ę",
Expand Down Expand Up @@ -238,7 +238,7 @@ exports[`parseMSKLC should parse a layout 1`] = `
"KeyA": "Å",
"KeyB": "",
"KeyC": "Č",
"KeyD": "%%",
"KeyD": "",
"KeyE": "Ě",
"KeyF": "Đ",
"KeyG": "",
Expand All @@ -254,7 +254,7 @@ exports[`parseMSKLC should parse a layout 1`] = `
"KeyQ": "Ś",
"KeyR": "Ŕ",
"KeyS": "Š",
"KeyT": "%%",
"KeyT": "",
"KeyU": "Ų",
"KeyV": "Ć",
"KeyW": "Ę",
Expand Down
46 changes: 32 additions & 14 deletions src/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { scMap } from './scMap';
import { scMap, vkMap } from './maps';

export type Layout = {
name: string;
Expand All @@ -11,6 +11,14 @@ export type Layout = {
states: Record<number, Record<string, string>>;
};

const BASE = 0;
const SHIFT = 1;
const CTRL = 2;
/** @deprecated */
const ALT = 4;
const ALT_GR = ALT | CTRL;
const SHIFT_ALT_GR = SHIFT | ALT_GR;

type Handler = (this: Partial<Layout>, args: string[]) => void;

type Handlers = {
Expand All @@ -27,8 +35,18 @@ const noop: Handler = () => {
/* noop */
};

const parseLigature: Handler = () => {
// TODO:
const parseLigature: Handler = function ([vk, _1, index, ...chars]) {
const state = [BASE, SHIFT, CTRL, ALT_GR, SHIFT_ALT_GR][+index];
if (state === undefined) {
return;
}

const code = vkMap.get(vk);
if (code === undefined) {
return;
}

this.states![state][code] = chars.map(parseChar).join('');
};

const parseLayout: Handler = function (args) {
Expand All @@ -44,17 +62,17 @@ const parseLayout: Handler = function (args) {
}

this.states ??= {};
this.states[0] ??= {};
this.states[1] ??= {};
this.states[2] ??= {};
this.states[6] ??= {};
this.states[7] ??= {};

this.states[0][code] = parseChar(s0);
this.states[1][code] = parseChar(s1);
this.states[2][code] = parseChar(s2);
this.states[6][code] = parseChar(s6);
this.states[7][code] = parseChar(s7);
this.states[BASE] ??= {};
this.states[SHIFT] ??= {};
this.states[CTRL] ??= {};
this.states[ALT_GR] ??= {};
this.states[SHIFT_ALT_GR] ??= {};

this.states[BASE][code] = parseChar(s0);
this.states[SHIFT][code] = parseChar(s1);
this.states[CTRL][code] = parseChar(s2);
this.states[ALT_GR][code] = parseChar(s6);
this.states[SHIFT_ALT_GR][code] = parseChar(s7);
};

function parseChar(ch: string): string {
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Layout, reset, handle } from './handlers';

export default function parseMSKLC(layout: string) {
export default function parseMSKLC(layout: string): Layout {
reset();

const result: Partial<Layout> = {};
Expand All @@ -9,5 +9,5 @@ export default function parseMSKLC(layout: string) {
handle(result, line);
}

return result;
return result as Layout;
}
17 changes: 11 additions & 6 deletions src/scMap.ts → src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ const MAPPING = [
[['Decimal'], 110, 'DECIMAL', 83],
] as const;

export const scMap = new Map(
MAPPING.flatMap((v) => v[0].map((x) => [x, ...v.slice(1)])).map((v) => [
v[3] as number,
v[0] as string,
]),
);
function createMap<K, V>(keyIndex: number, valueIndex: number) {
return new Map<K, V>(
MAPPING.flatMap((v) => v[0].map((x) => [x, ...v.slice(1)])).map((v) => [
v[keyIndex] as K,
v[valueIndex] as V,
]),
);
}

export const scMap = createMap<number, string>(3, 0);
export const vkMap = createMap<string, number>(2, 0);

0 comments on commit 20716e5

Please sign in to comment.