Skip to content

Commit bf002c0

Browse files
megalodon2710jeffreyMiodec
authored
feat(funbox): add layout mirror (@megalodon2710) (monkeytypegame#6463)
### Description Added layout mirror item in the funbox with functionality. Tweaked how active funbox items are detected in the commandline (names included in other names no long appear as active if the item they're included in is active). ### Checks - [ ] Adding quotes? - [ ] Make sure to include translations for the quotes in the description (or another comment) so we can verify their content. - [ ] Adding a language or a theme? - [ ] If is a language, did you edit `_list.json`, `_groups.json` and add `languages.json`? - [ ] If is a theme, did you add the theme.css? - Also please add a screenshot of the theme, it would be extra awesome if you do so! - [X] Check if any open issues are related to this PR; if so, be sure to tag them below. - [X] Make sure the PR title follows the Conventional Commits standard. (https://www.conventionalcommits.org for more info) - [X] Make sure to include your GitHub username prefixed with @ inside parentheses at the end of the PR title. <!-- label(optional scope): pull request title (@your_github_username) --> <!-- I know I know they seem boring but please do them, they help us and you will find out it also helps you.--> Closes monkeytypegame#5573 <!-- the issue(s) your PR resolves if any (delete if that is not the case) --> <!-- please also reference any issues and or PRs related to your pull request --> <!-- Also remove it if you are not following any issues. --> <!-- pro tip: you can mention an issue, PR, or discussion on GitHub by referencing its hash number e.g: [monkeytypegame#1234](monkeytypegame#1234) --> <!-- pro tip: you can press . (dot or period) in the code tab of any GitHub repo to get access to GitHub's VS Code web editor Enjoy! :) --> --------- Co-authored-by: jeffrey <[email protected]> Co-authored-by: Miodec <[email protected]>
1 parent 6acaeb4 commit bf002c0

File tree

10 files changed

+95
-9
lines changed

10 files changed

+95
-9
lines changed

frontend/src/ts/commandline/commandline.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,14 @@ async function showCommands(): Promise<void> {
352352
configIcon = `<i class="fas fa-fw"></i>`;
353353
}
354354
} else if (configKey !== undefined) {
355-
const valueIsIncluded =
356-
command.configValueMode === "include" &&
357-
(
355+
let isActive;
356+
357+
if (command.configValueMode === "funbox") {
358+
isActive = (Config[configKey] as string)
359+
.split("#")
360+
.includes(command.configValue as string);
361+
} else if (command.configValueMode === "include") {
362+
isActive = (
358363
Config[configKey] as (
359364
| string
360365
| number
@@ -363,8 +368,11 @@ async function showCommands(): Promise<void> {
363368
| undefined
364369
)[]
365370
).includes(command.configValue);
366-
const valueIsTheSame = Config[configKey] === command.configValue;
367-
if (valueIsIncluded || valueIsTheSame) {
371+
} else {
372+
isActive = Config[configKey] === command.configValue;
373+
}
374+
375+
if (isActive) {
368376
firstActive = firstActive ?? index;
369377
configIcon = `<i class="fas fa-fw fa-check"></i>`;
370378
} else {

frontend/src/ts/commandline/lists/funbox.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ for (const funbox of getAllFunboxes()) {
3333
sticky: true,
3434
alias: funbox.alias,
3535
configValue: funbox.name,
36-
configValueMode: "include",
36+
//todo remove funbox mode once Config.funbox is changed to an array
37+
configValueMode: "funbox",
3738
exec: (): void => {
3839
Funbox.toggleFunbox(funbox.name);
3940
ManualRestart.set();

frontend/src/ts/commandline/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type Command = {
2525
defaultValue?: () => string;
2626
configKey?: keyof Config;
2727
configValue?: string | number | boolean | number[];
28-
configValueMode?: "include";
28+
configValueMode?: "include" | "funbox";
2929
exec?: (options: CommandExecOptions) => void;
3030
hover?: () => void;
3131
available?: () => boolean;

frontend/src/ts/controllers/input-controller.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
findSingleActiveFunboxWithFunction,
4040
getActiveFunboxesWithFunction,
4141
isFunboxActiveWithProperty,
42+
getActiveFunboxNames,
4243
} from "../test/funbox/list";
4344

4445
let dontInsertSpace = false;
@@ -1128,14 +1129,21 @@ $(document).on("keydown", async (event) => {
11281129
Config.oppositeShiftMode === "keymap" &&
11291130
Config.keymapLayout !== "overrideSync"
11301131
) {
1131-
const keymapLayout = await JSONData.getLayout(Config.keymapLayout).catch(
1132+
let keymapLayout = await JSONData.getLayout(Config.keymapLayout).catch(
11321133
() => undefined
11331134
);
1135+
11341136
if (keymapLayout === undefined) {
11351137
Notifications.add("Failed to load keymap layout", -1);
11361138

11371139
return;
11381140
}
1141+
1142+
const funbox = getActiveFunboxNames().includes("layout_mirror");
1143+
if (funbox) {
1144+
keymapLayout = KeyConverter.mirrorLayoutKeys(keymapLayout);
1145+
}
1146+
11391147
const keycode = KeyConverter.layoutKeyToKeycode(event.key, keymapLayout);
11401148

11411149
correctShiftUsed =

frontend/src/ts/elements/keymap.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { capsState } from "../test/caps-warning";
1313
import * as ShiftTracker from "../test/shift-tracker";
1414
import * as AltTracker from "../test/alt-tracker";
1515
import * as KeyConverter from "../utils/key-converter";
16+
import { getActiveFunboxNames } from "../test/funbox/list";
1617

1718
const stenoKeys: JSONData.Layout = {
1819
keymapShowTopRow: true,
@@ -393,6 +394,11 @@ export async function refresh(
393394
layoutData = stenoKeys;
394395
}
395396

397+
const funbox = getActiveFunboxNames().includes("layout_mirror");
398+
if (funbox) {
399+
layoutData = KeyConverter.mirrorLayoutKeys(layoutData);
400+
}
401+
396402
const isISO = layoutData.type === "iso";
397403

398404
let keymapElement = "";

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ const list: Partial<Record<FunboxName, FunboxFunctions>> = {
326326
return Strings.capitalizeFirstLetterOfEachWord(word);
327327
},
328328
},
329+
layout_mirror: {
330+
applyConfig(): void {
331+
let layout = Config.layout;
332+
if (Config.layout === "default") {
333+
layout = "qwerty";
334+
}
335+
UpdateConfig.setLayout(layout, true);
336+
UpdateConfig.setKeymapLayout("overrideSync", true);
337+
},
338+
rememberSettings(): void {
339+
save("keymapMode", Config.keymapMode, UpdateConfig.setKeymapMode);
340+
save("layout", Config.layout, UpdateConfig.setLayout);
341+
},
342+
},
329343
layoutfluid: {
330344
applyConfig(): void {
331345
const layout = Config.customLayoutfluid.split("#")[0] ?? "qwerty";

frontend/src/ts/test/layout-emulator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import * as Misc from "../utils/misc";
33
import * as JSONData from "../utils/json-data";
44
import { capsState } from "./caps-warning";
55
import * as Notifications from "../elements/notifications";
6+
import * as KeyConverter from "../utils/key-converter";
7+
8+
import { getActiveFunboxNames } from "./funbox/list";
69

710
let isAltGrPressed = false;
811
const isPunctuationPattern = /\p{P}/u;
@@ -41,6 +44,11 @@ export async function getCharFromEvent(
4144
return null;
4245
}
4346

47+
const funbox = getActiveFunboxNames().includes("layout_mirror");
48+
if (funbox) {
49+
layout = KeyConverter.mirrorLayoutKeys(layout);
50+
}
51+
4452
let keyEventCodes: string[] = [];
4553

4654
if (layout.type === "ansi") {

frontend/src/ts/utils/key-converter.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,36 @@ export function keycodeToKeyboardSide(keycode: Keycode): {
280280

281281
return { leftSide: left, rightSide: right };
282282
}
283+
284+
/**
285+
* Returns a copy of the given layout with the rows mirrored
286+
* @param layout Layout object from our JSON data (e.g., `layouts["qwerty"]`)
287+
* @returns layout Layout object from our JSON data (e.g., `layouts["qwerty"]`)
288+
*/
289+
export function mirrorLayoutKeys(layout: JSONData.Layout): JSONData.Layout {
290+
const reverse_index = [11, 10, 10, 10, 10];
291+
const mirror_keys: JSONData.Keys = {
292+
row1: [
293+
...[...layout.keys.row1.slice(0, reverse_index[0])].reverse(),
294+
...layout.keys.row1.slice(reverse_index[0]),
295+
],
296+
row2: [
297+
...[...layout.keys.row2.slice(0, reverse_index[1])].reverse(),
298+
...layout.keys.row2.slice(reverse_index[1]),
299+
],
300+
row3: [
301+
...[...layout.keys.row3.slice(0, reverse_index[2])].reverse(),
302+
...layout.keys.row3.slice(reverse_index[2]),
303+
],
304+
row4: [
305+
...[...layout.keys.row4.slice(0, reverse_index[3])].reverse(),
306+
...layout.keys.row4.slice(reverse_index[3]),
307+
],
308+
row5: [
309+
...[...layout.keys.row5.slice(0, reverse_index[4])].reverse(),
310+
...layout.keys.row5.slice(reverse_index[4]),
311+
],
312+
};
313+
const layoutCopy: JSONData.Layout = { ...layout, keys: mirror_keys };
314+
return layoutCopy;
315+
}

packages/funbox/src/list.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const list: Record<FunboxName, FunboxMetadata> = {
6262
},
6363
frontendFunctions: ["applyConfig", "rememberSettings"],
6464
},
65-
6665
tts: {
6766
canGetPb: true,
6867
difficultyLevel: 1,
@@ -130,6 +129,14 @@ const list: Record<FunboxName, FunboxMetadata> = {
130129
frontendFunctions: ["alterText"],
131130
name: "capitals",
132131
},
132+
layout_mirror: {
133+
description: "Mirror the keyboard layout",
134+
canGetPb: true,
135+
difficultyLevel: 1,
136+
properties: ["changesLayout"],
137+
frontendFunctions: ["applyConfig", "rememberSettings"],
138+
name: "layout_mirror",
139+
},
133140
layoutfluid: {
134141
description:
135142
"Switch between layouts specified below proportionately to the length of the test.",

packages/funbox/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export type FunboxName =
1010
| "arrows"
1111
| "rAnDoMcAsE"
1212
| "capitals"
13+
| "layout_mirror"
1314
| "layoutfluid"
1415
| "earthquake"
1516
| "space_balls"

0 commit comments

Comments
 (0)