Skip to content

Commit b36bc9f

Browse files
authored
refactor: move layout names to contracts (@fehmer) (#6495)
1 parent ab9cef0 commit b36bc9f

File tree

10 files changed

+257
-250
lines changed

10 files changed

+257
-250
lines changed

docs/LAYOUTS.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ For iso the number of keys need to be exactly thirteen for `row1`, twelve for `r
4646

4747

4848

49-
In addition to the layout file you need to add your layout to the `frontend/src/ts/constants/layouts.ts` file. Just append your layout name (without the `.json`) at the __end__ of the array. Remember to add a comma like this:
49+
In addition to the layout file you need to add your layout to the `packages/contracts/src/schemas/layouts.ts` file. Just append your layout name (without the `.json`) at the __end__ of the `LayoutNameSchema`. Remember to add a comma like this:
5050

5151
```ts
52-
"ergopti",
53-
"sword",
54-
"YOUR_LAYOUT",
55-
];
52+
export const LayoutNameSchema = z.enum([
53+
"qwerty",
54+
"dvorak",
55+
"colemak",
56+
...
57+
"your_layout_name",
58+
]);
5659
```
5760

5861
### Committing Layouts

frontend/__tests__/constants/layouts.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { readdirSync } from "fs";
22
import { LayoutsList } from "../../src/ts/constants/layouts";
3+
import { LayoutName } from "@monkeytype/contracts/schemas/layouts";
34

45
describe("layouts", () => {
56
it("should not have duplicates", () => {
@@ -32,7 +33,7 @@ describe("layouts", () => {
3233
});
3334

3435
function listLayoutFiles() {
35-
return readdirSync(import.meta.dirname + "/../../static/layouts").map((it) =>
36-
it.substring(0, it.length - 5)
36+
return readdirSync(import.meta.dirname + "/../../static/layouts").map(
37+
(it) => it.substring(0, it.length - 5) as LayoutName
3738
);
3839
}

frontend/__tests__/root/config.spec.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,15 @@ describe("Config", () => {
439439
});
440440
it("setKeymapLayout", () => {
441441
expect(Config.setKeymapLayout("overrideSync")).toBe(true);
442-
expect(Config.setKeymapLayout("override_sync")).toBe(true);
443-
expect(Config.setKeymapLayout("override sync")).toBe(false);
444-
expect(Config.setKeymapLayout("override-sync!")).toBe(false);
445-
expect(Config.setKeymapLayout(stringOfLength(50))).toBe(true);
446-
447-
expect(Config.setKeymapLayout(stringOfLength(51))).toBe(false);
442+
expect(Config.setKeymapLayout("override_sync" as any)).toBe(false);
443+
expect(Config.setKeymapLayout("override sync" as any)).toBe(false);
444+
expect(Config.setKeymapLayout("override-sync!" as any)).toBe(false);
448445
});
449446
it("setLayout", () => {
450447
expect(Config.setLayout("semimak")).toBe(true);
451-
expect(Config.setLayout("semi_mak")).toBe(true);
452-
expect(Config.setLayout(stringOfLength(50))).toBe(true);
453-
454-
expect(Config.setLayout("semi mak")).toBe(false);
455-
expect(Config.setLayout("semi-mak")).toBe(true);
456-
expect(Config.setLayout(stringOfLength(51))).toBe(false);
448+
expect(Config.setLayout("default")).toBe(true);
449+
expect(Config.setLayout("semi_mak" as any)).toBe(false);
450+
expect(Config.setLayout("overrideSync" as any)).toBe(false);
457451
});
458452
it("setFontSize", () => {
459453
expect(Config.setFontSize(1)).toBe(true);

frontend/src/ts/commandline/lists/keymap-layouts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { KeymapLayout } from "@monkeytype/contracts/schemas/configs";
12
import * as UpdateConfig from "../../config";
23
import { LayoutsList } from "../../constants/layouts";
34
import * as TestLogic from "../../test/test-logic";
@@ -23,7 +24,7 @@ const subgroup: CommandsSubgroup = {
2324
display: layout.replace(/_/g, " "),
2425
configValue: layout,
2526
exec: (): void => {
26-
UpdateConfig.setKeymapLayout(layout);
27+
UpdateConfig.setKeymapLayout(layout as KeymapLayout);
2728
TestLogic.restart();
2829
},
2930
})),

frontend/src/ts/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { migrateConfig } from "./utils/config";
3131
import { roundTo1 } from "@monkeytype/util/numbers";
3232
import { getDefaultConfig } from "./constants/default-config";
3333
import { LayoutsList } from "./constants/layouts";
34+
import { LayoutName } from "@monkeytype/contracts/schemas/layouts";
3435

3536
const configLS = new LocalStorageWithSchema({
3637
key: "config",
@@ -1884,7 +1885,7 @@ export function setCustomLayoutfluid(
18841885

18851886
const invalidLayouts = trimmed
18861887
.split(/[# ]+/) //can be space or hash
1887-
.filter((it) => !LayoutsList.includes(it));
1888+
.filter((it) => !LayoutsList.includes(it as LayoutName));
18881889

18891890
if (invalidLayouts.length !== 0) {
18901891
notifyInvalid(

frontend/src/ts/constants/layouts.ts

Lines changed: 3 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -1,214 +1,3 @@
1-
export const LayoutsList:string[] = [
2-
"qwerty",
3-
"dvorak",
4-
"colemak",
5-
"colemak_wide",
6-
"colemak_dh",
7-
"colemak_dh_iso",
8-
"colemak_dh_wide",
9-
"colemak_dh_iso_wide",
10-
"colemak_dhk",
11-
"colemak_dh_matrix",
12-
"colemak_dhk_iso",
13-
"colemak_dhv",
14-
"qwertz",
15-
"swiss_german",
16-
"swiss_french",
17-
"workman",
18-
"prog_workman",
19-
"turkish_q",
20-
"turkish_f",
21-
"turkish_e",
22-
"MTGAP_ASRT",
23-
"norman",
24-
"halmak",
25-
"QGMLWB",
26-
"QGMLWY",
27-
"qwpr",
28-
"uk_qwerty",
29-
"spanish_qwerty",
30-
"italian_qwerty",
31-
"latam_qwerty",
32-
"prog_dvorak",
33-
"prog_dvorak_prime",
34-
"german_dvorak",
35-
"german_dvorak_imp",
36-
"spanish_dvorak",
37-
"swedish_colemak",
38-
"swedish_dvorak",
39-
"dvorak_L",
40-
"dvorak_R",
41-
"dvorak_fr",
42-
"azerty",
43-
"bepo",
44-
"bepo_AFNOR",
45-
"alpha",
46-
"handsdown",
47-
"hungarian",
48-
"handsdown_alt",
49-
"handsdown_promethium",
50-
"typehack",
51-
"MTGAP",
52-
"MTGAP_full",
53-
"ina",
54-
"soul",
55-
"niro",
56-
"mongolian",
57-
"JCUKEN",
58-
"Diktor",
59-
"Diktor_VoronovMod",
60-
"Redaktor",
61-
"JUIYAF",
62-
"Zubachev",
63-
"ISRT",
64-
"ISRT_Angle",
65-
"colemak_Qix",
66-
"colemak_Qi",
67-
"colemaQ",
68-
"colemaQ_F",
69-
"engram",
70-
"engrammer",
71-
"semimak",
72-
"semimak_jq",
73-
"semimak_jqc",
74-
"canary",
75-
"canary_matrix",
76-
"japanese_hiragana",
77-
"boo",
78-
"boo_mangle",
79-
"APT",
80-
"APT_angle",
81-
"middlemak",
82-
"middlemak-nh",
83-
"hindi_inscript",
84-
"thai_kedmanee",
85-
"thai_pattachote",
86-
"thai_manoonchai",
87-
"persian_standard",
88-
"persian_farsi",
89-
"arabic_101",
90-
"arabic_102",
91-
"arabic_mac",
92-
"hebrew",
93-
"urdu_phonetic",
94-
"brasileiro_nativo",
95-
"Foalmak",
96-
"quartz",
97-
"arensito",
98-
"ARTS",
99-
"beakl_15",
100-
"beakl_19",
101-
"beakl_19_bis",
102-
"capewell_dvorak",
103-
"colman",
104-
"heart",
105-
"klauser",
106-
"oneproduct",
107-
"pine",
108-
"real",
109-
"rolll",
110-
"stndc",
111-
"three",
112-
"uciea",
113-
"asset",
114-
"dwarf",
115-
"flaw",
116-
"whorf",
117-
"whorf6",
118-
"whorfmax",
119-
"whorfmax_ortho",
120-
"sertain",
121-
"ctgap",
122-
"octa8",
123-
"polish_programmers",
124-
"bulgarian",
125-
"bulgarian_phonetic_traditional",
126-
"belarusian",
127-
"ukrainian",
128-
"russian",
129-
"neo",
130-
"bone",
131-
"AdNW",
132-
"mine",
133-
"noted",
134-
"koy",
135-
"3l",
136-
"korean",
137-
"ekverto_b",
138-
"nerps",
139-
"sturdy_angle_ansi",
140-
"sturdy_angle_iso",
141-
"sturdy_ortho",
142-
"ABNT2",
143-
"HiYou",
144-
"xenia",
145-
"xenia_alt",
146-
"burmese",
147-
"gallium",
148-
"gallium_angle",
149-
"gallium_v2",
150-
"gallium_v2_matrix",
151-
"maya",
152-
"gallaya_angle_ansi",
153-
"gallaya_angle_iso",
154-
"gallaya_matrix",
155-
"nila",
156-
"minimak_4k",
157-
"minimak_8k",
158-
"minimak_12k",
159-
"optimot",
160-
"norwegian_qwerty",
161-
"portuguese_pt_qwerty_iso",
162-
"portuguese_pt_qwerty_ansi",
163-
"swedish_qwerty",
164-
"danish_qwerty",
165-
"noctum",
166-
"graphite",
167-
"graphite_angle",
168-
"graphite_angle_vc",
169-
"graphite_angle_kp",
170-
"graphite_matrix",
171-
"macedonian",
172-
"UGJRMV",
173-
"pashto",
174-
"ORNATE",
175-
"estonian",
176-
"stronk",
177-
"dhorf",
178-
"recurva",
179-
"seht-drai",
180-
"ints",
181-
"rollla",
182-
"wreathy",
183-
"saiga",
184-
"saiga-e",
185-
"krai",
186-
"mir",
187-
"ergol",
188-
"cascade",
189-
"vylet",
190-
"hyperroll",
191-
"romak",
192-
"scythe",
193-
"inqwerted",
194-
"rain",
195-
"night",
196-
"whix2",
197-
"haruka",
198-
"kuntum",
199-
"anishtro",
200-
"Kuntem",
201-
"BEAKL_Zi",
202-
"snorkle",
203-
"MALTRON",
204-
"PRSTEN",
205-
"RSTHD",
206-
"dusk",
207-
"zenith",
208-
"focal",
209-
"panini",
210-
"panini_wide",
211-
"ergopti",
212-
"sword",
213-
"opy",
214-
];
1+
import { LayoutName, LayoutNameSchema } from "@monkeytype/contracts/schemas/layouts";
2+
3+
export const LayoutsList:LayoutName[] = LayoutNameSchema._def.values;

frontend/src/ts/test/funbox/funbox-functions.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as WeakSpot from "../weak-spot";
2323
import * as IPAddresses from "../../utils/ip-addresses";
2424
import * as TestState from "../test-state";
2525
import { WordGenError } from "../../utils/word-gen-error";
26+
import { KeymapLayout, Layout } from "@monkeytype/contracts/schemas/configs";
2627

2728
export type FunboxFunctions = {
2829
getWord?: (wordset?: Wordset, wordIndex?: number) => string;
@@ -345,8 +346,8 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
345346
applyConfig(): void {
346347
const layout = Config.customLayoutfluid.split("#")[0] ?? "qwerty";
347348

348-
UpdateConfig.setLayout(layout, true);
349-
UpdateConfig.setKeymapLayout(layout, true);
349+
UpdateConfig.setLayout(layout as Layout, true);
350+
UpdateConfig.setKeymapLayout(layout as KeymapLayout, true);
350351
},
351352
rememberSettings(): void {
352353
save("keymapMode", Config.keymapMode, UpdateConfig.setKeymapMode);
@@ -357,8 +358,8 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
357358
if (Config.mode !== "time") {
358359
// here I need to check if Config.customLayoutFluid exists because of my
359360
// scuffed solution of returning whenever value is undefined in the setCustomLayoutfluid function
360-
const layouts: string[] = Config.customLayoutfluid
361-
? Config.customLayoutfluid.split("#")
361+
const layouts: Layout[] = Config.customLayoutfluid
362+
? (Config.customLayoutfluid.split("#") as Layout[])
362363
: ["qwerty", "dvorak", "colemak"];
363364
const outOf: number = TestWords.words.length;
364365
const wordsPerLayout = Math.floor(outOf / layouts.length);
@@ -379,8 +380,8 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
379380
LayoutfluidFunboxTimer.hide();
380381
}
381382
if (mod === wordsPerLayout) {
382-
UpdateConfig.setLayout(layouts[index] as string);
383-
UpdateConfig.setKeymapLayout(layouts[index] as string);
383+
UpdateConfig.setLayout(layouts[index] as Layout);
384+
UpdateConfig.setKeymapLayout(layouts[index] as KeymapLayout);
384385
if (mod > 3) {
385386
LayoutfluidFunboxTimer.hide();
386387
}

frontend/src/ts/test/test-timer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as TestState from "./test-state";
1616
import * as Time from "../states/time";
1717
import * as TimerEvent from "../observables/timer-event";
1818
import * as LayoutfluidFunboxTimer from "../test/funbox/layoutfluid-funbox-timer";
19+
import { KeymapLayout, Layout } from "@monkeytype/contracts/schemas/configs";
1920

2021
type TimerStats = {
2122
dateNow: number;
@@ -118,8 +119,8 @@ function layoutfluid(): void {
118119

119120
if (Config.layout !== layout && layout !== undefined) {
120121
LayoutfluidFunboxTimer.hide();
121-
UpdateConfig.setLayout(layout, true);
122-
UpdateConfig.setKeymapLayout(layout, true);
122+
UpdateConfig.setLayout(layout as Layout, true);
123+
UpdateConfig.setKeymapLayout(layout as KeymapLayout, true);
123124
}
124125
}
125126
if (timerDebug) console.timeEnd("layoutfluid");

0 commit comments

Comments
 (0)