Skip to content

Commit 3ddb3c6

Browse files
authored
refactor(config): cleanup handling for fontSize, tapeMargin and maxLineWidth (@fehmer) (monkeytypegame#6775)
merge this after monkeytypegame#6751 and the command builder pr - remove configOverride - add handling to config migration
1 parent be1774e commit 3ddb3c6

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

frontend/__tests__/root/config.spec.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,9 +1042,6 @@ describe("Config", () => {
10421042
it("setFontSize", () => {
10431043
expect(Config.setFontSize(1)).toBe(true);
10441044

1045-
//gets converted
1046-
expect(Config.setFontSize(-1)).toBe(true);
1047-
10481045
expect(Config.setFontSize(0)).toBe(false);
10491046
expect(Config.setFontSize("5" as any)).toBe(false);
10501047
expect(Config.setFontSize("invalid" as any)).toBe(false);
@@ -1053,11 +1050,6 @@ describe("Config", () => {
10531050
expect(Config.setMaxLineWidth(0)).toBe(true);
10541051
expect(Config.setMaxLineWidth(50)).toBe(true);
10551052
expect(Config.setMaxLineWidth(50.5)).toBe(true);
1056-
1057-
//gets converted
1058-
expect(Config.setMaxLineWidth(10)).toBe(true);
1059-
expect(Config.setMaxLineWidth(10_000)).toBe(true);
1060-
expect(Config.setMaxLineWidth("invalid" as any)).toBe(false);
10611053
});
10621054
it("setCustomBackground", () => {
10631055
expect(Config.setCustomBackground("http://example.com/test.png")).toBe(

frontend/__tests__/utils/config.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,35 @@ describe("config.ts", () => {
209209
given: { fontSize: 15 },
210210
expected: { fontSize: 15 },
211211
},
212+
{
213+
given: { fontSize: -0.5 },
214+
expected: { fontSize: 1 },
215+
},
216+
{
217+
given: { tapeMargin: 9.5 },
218+
expected: { tapeMargin: 10 },
219+
},
220+
{
221+
given: { tapeMargin: 25 },
222+
expected: { tapeMargin: 25 },
223+
},
224+
{
225+
given: { tapeMargin: 90.5 },
226+
expected: { tapeMargin: 90 },
227+
},
228+
{
229+
given: { maxLineWidth: 0 },
230+
expected: { maxLineWidth: 0 },
231+
},
232+
233+
{
234+
given: { maxLineWidth: 19 },
235+
expected: { maxLineWidth: 20 },
236+
},
237+
{
238+
given: { maxLineWidth: 1001 },
239+
expected: { maxLineWidth: 1000 },
240+
},
212241
])(`$given`, ({ given, expected }) => {
213242
const description = `given: ${JSON.stringify(
214243
given

frontend/src/ts/config-metadata.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -500,16 +500,6 @@ export const configMetadata: ConfigMetadataObject = {
500500
icon: "fa-tape",
501501
displayString: "tape margin",
502502
changeRequiresRestart: false,
503-
overrideValue: ({ value }) => {
504-
//TODO move to migration after settings validation
505-
if (value < 10) {
506-
value = 10;
507-
}
508-
if (value > 90) {
509-
value = 90;
510-
}
511-
return value;
512-
},
513503
},
514504
smoothLineScroll: {
515505
icon: "fa-align-left",
@@ -548,29 +538,12 @@ export const configMetadata: ConfigMetadataObject = {
548538
changeRequiresRestart: false,
549539
triggerResize: true,
550540
displayString: "max line width",
551-
overrideValue: ({ value }) => {
552-
//TODO move to migration after settings validation
553-
if (value < 20 && value !== 0) {
554-
value = 20;
555-
}
556-
if (value > 1000) {
557-
value = 1000;
558-
}
559-
return value;
560-
},
561541
},
562542
fontSize: {
563543
icon: "fa-font",
564544
changeRequiresRestart: false,
565545
triggerResize: true,
566546
displayString: "font size",
567-
overrideValue: ({ value }) => {
568-
//TODO move to migration after settings validation
569-
if (value < 0) {
570-
value = 1;
571-
}
572-
return value;
573-
},
574547
},
575548
fontFamily: {
576549
icon: "fa-font",

frontend/src/ts/utils/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ export function replaceLegacyValues(
154154
}
155155

156156
configObj.fontSize = newValue;
157+
} else if (configObj.fontSize !== undefined && configObj.fontSize < 0) {
158+
configObj.fontSize = 1;
157159
}
158160

159161
if (
@@ -198,5 +200,21 @@ export function replaceLegacyValues(
198200
}
199201
}
200202

203+
if (configObj.tapeMargin !== undefined) {
204+
if (configObj.tapeMargin < 10) {
205+
configObj.tapeMargin = 10;
206+
} else if (configObj.tapeMargin > 90) {
207+
configObj.tapeMargin = 90;
208+
}
209+
}
210+
211+
if (configObj.maxLineWidth !== undefined) {
212+
if (configObj.maxLineWidth < 20 && configObj.maxLineWidth !== 0) {
213+
configObj.maxLineWidth = 20;
214+
} else if (configObj.maxLineWidth > 1000) {
215+
configObj.maxLineWidth = 1000;
216+
}
217+
}
218+
201219
return configObj;
202220
}

0 commit comments

Comments
 (0)