Skip to content

Commit dea95a2

Browse files
authored
feat(input): add 'code unindent on backspace' for code languages (@notTamion) (monkeytypegame#5991)
1 parent 23948f0 commit dea95a2

File tree

9 files changed

+100
-0
lines changed

9 files changed

+100
-0
lines changed

frontend/__tests__/root/config.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ describe("Config", () => {
145145
expect(Config.setSmoothCaret("medium")).toBe(true);
146146
expect(Config.setSmoothCaret("invalid" as any)).toBe(false);
147147
});
148+
it("setCodeUnindentOnBackspace", () => {
149+
testBoolean(Config.setCodeUnindentOnBackspace);
150+
});
148151
it("setQuickRestartMode", () => {
149152
expect(Config.setQuickRestartMode("off")).toBe(true);
150153
expect(Config.setQuickRestartMode("tab")).toBe(true);

frontend/src/html/pages/settings.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,20 @@
504504
<select></select>
505505
</div>
506506
</div>
507+
<div class="section" data-config-name="codeUnindentOnBackspace">
508+
<div class="groupTitle">
509+
<i class="fas fa-code"></i>
510+
<span>code unindent on backspace</span>
511+
</div>
512+
<div class="text">
513+
Automatically go back to the previous line when deleting line leading
514+
tab characters. Only works in code languages.
515+
</div>
516+
<div class="buttons">
517+
<button data-config-value="false">off</button>
518+
<button data-config-value="true">on</button>
519+
</div>
520+
</div>
507521
<div class="sectionSpacer"></div>
508522
</div>
509523

frontend/src/ts/commandline/lists.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import ResultScreenCommands from "./lists/result-screen";
6969
import CustomBackgroundSizeCommands from "./lists/background-size";
7070
import CustomBackgroundFilterCommands from "./lists/background-filter";
7171
import AddOrRemoveThemeToFavorite from "./lists/add-or-remove-theme-to-favorites";
72+
import CodeUnindentOnBackspace from "./lists/code-unindent-on-backspace";
7273

7374
import TagsCommands from "./lists/tags";
7475
import CustomThemesListCommands from "./lists/custom-themes-list";
@@ -264,6 +265,7 @@ export const commands: CommandsSubgroup = {
264265
...HideExtraLettersCommands,
265266
...LazyModeCommands,
266267
...LayoutsCommands,
268+
...CodeUnindentOnBackspace,
267269

268270
//sound
269271
...SoundVolumeCommands,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as UpdateConfig from "../../config";
2+
import { Command, CommandsSubgroup } from "../types";
3+
4+
const subgroup: CommandsSubgroup = {
5+
title: "Code unindent on backspace...",
6+
configKey: "codeUnindentOnBackspace",
7+
list: [
8+
{
9+
id: "setCodeUnindentOnBackspaceOff",
10+
display: "off",
11+
configValue: false,
12+
exec: (): void => {
13+
UpdateConfig.setCodeUnindentOnBackspace(false);
14+
},
15+
},
16+
{
17+
id: "changeCodeUnindentOnBackspaceOn",
18+
display: "on",
19+
configValue: true,
20+
exec: (): void => {
21+
UpdateConfig.setCodeUnindentOnBackspace(true);
22+
},
23+
},
24+
],
25+
};
26+
27+
const commands: Command[] = [
28+
{
29+
id: "changeCodeUnindentOnBackspace",
30+
display: "Code unindent on backspace...",
31+
icon: "fa-code",
32+
subgroup,
33+
},
34+
];
35+
36+
export default commands;

frontend/src/ts/config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,24 @@ export function setSmoothCaret(
11801180
return true;
11811181
}
11821182

1183+
export function setCodeUnindentOnBackspace(
1184+
mode: boolean,
1185+
nosave?: boolean
1186+
): boolean {
1187+
if (!isConfigValueValidBoolean("code unindent on backspace", mode)) {
1188+
return false;
1189+
}
1190+
config.codeUnindentOnBackspace = mode;
1191+
1192+
saveToLocalStorage("codeUnindentOnBackspace", nosave);
1193+
ConfigEvent.dispatch(
1194+
"codeUnindentOnBackspace",
1195+
config.codeUnindentOnBackspace,
1196+
nosave
1197+
);
1198+
return true;
1199+
}
1200+
11831201
export function setStartGraphsAtZero(mode: boolean, nosave?: boolean): boolean {
11841202
if (!isConfigValueValidBoolean("start graphs at zero", mode)) {
11851203
return false;
@@ -1993,6 +2011,7 @@ export async function apply(
19932011
setKeymapSize(configObj.keymapSize, true);
19942012
setFontFamily(configObj.fontFamily, true);
19952013
setSmoothCaret(configObj.smoothCaret, true);
2014+
setCodeUnindentOnBackspace(configObj.codeUnindentOnBackspace, true);
19962015
setSmoothLineScroll(configObj.smoothLineScroll, true);
19972016
setAlwaysShowDecimalPlaces(configObj.alwaysShowDecimalPlaces, true);
19982017
setAlwaysShowWordsHistory(configObj.alwaysShowWordsHistory, true);

frontend/src/ts/constants/default-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const obj = {
2525
favThemes: [],
2626
showKeyTips: true,
2727
smoothCaret: "medium",
28+
codeUnindentOnBackspace: false,
2829
quickRestart: "off",
2930
punctuation: false,
3031
numbers: false,

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,25 @@ $(document).on("keydown", async (event) => {
10461046
event.preventDefault();
10471047
return;
10481048
}
1049+
1050+
// if the user backspaces the indentation in a code language we need to empty
1051+
// the current word so the user is set back to the end of the last line
1052+
if (
1053+
Config.codeUnindentOnBackspace &&
1054+
TestInput.input.current.length > 0 &&
1055+
/^\t*$/.test(TestInput.input.current) &&
1056+
Config.language.startsWith("code") &&
1057+
isCharCorrect(
1058+
TestInput.input.current.slice(-1),
1059+
TestInput.input.current.length - 1
1060+
) &&
1061+
(TestInput.input.history[TestWords.words.currentIndex - 1] !=
1062+
TestWords.words.get(TestWords.words.currentIndex - 1) ||
1063+
Config.freedomMode)
1064+
) {
1065+
TestInput.input.current = "";
1066+
await TestUI.updateActiveWordLetters();
1067+
}
10491068
}
10501069

10511070
if (event.key === "Backspace" && TestInput.input.current.length === 0) {

frontend/src/ts/pages/settings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ async function initGroups(): Promise<void> {
3737
UpdateConfig.setSmoothCaret,
3838
"button"
3939
) as SettingsGroup<ConfigValue>;
40+
groups["codeUnindentOnBackspace"] = new SettingsGroup(
41+
"codeUnindentOnBackspace",
42+
UpdateConfig.setCodeUnindentOnBackspace,
43+
"button"
44+
) as SettingsGroup<ConfigValue>;
4045
groups["difficulty"] = new SettingsGroup(
4146
"difficulty",
4247
UpdateConfig.setDifficulty,

packages/contracts/src/schemas/configs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export const ConfigSchema = z
298298
favThemes: FavThemesSchema,
299299
showKeyTips: z.boolean(),
300300
smoothCaret: SmoothCaretSchema,
301+
codeUnindentOnBackspace: z.boolean(),
301302
quickRestart: QuickRestartSchema,
302303
punctuation: z.boolean(),
303304
numbers: z.boolean(),

0 commit comments

Comments
 (0)