Skip to content

Commit 545f08c

Browse files
committed
fix: localize default custom text
1 parent 4b92621 commit 545f08c

5 files changed

Lines changed: 120 additions & 6 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
async function importCustomText(): Promise<
4+
typeof import("../../src/ts/test/custom-text")
5+
> {
6+
vi.resetModules();
7+
window.localStorage.clear();
8+
return await import("../../src/ts/test/custom-text");
9+
}
10+
11+
describe("custom text", () => {
12+
beforeEach(() => {
13+
window.localStorage.clear();
14+
});
15+
16+
it("uses language words when custom text is untouched default", async () => {
17+
const CustomText = await importCustomText();
18+
19+
expect(CustomText.getEffectiveText(["uno", "dos", "tres"])).toEqual([
20+
"uno",
21+
"dos",
22+
"tres",
23+
]);
24+
});
25+
26+
it("keeps user custom text instead of language words", async () => {
27+
const CustomText = await importCustomText();
28+
29+
CustomText.setText(["custom", "words"]);
30+
31+
expect(CustomText.getEffectiveText(["uno", "dos", "tres"])).toEqual([
32+
"custom",
33+
"words",
34+
]);
35+
});
36+
37+
it("keeps explicitly set default text instead of language words", async () => {
38+
const CustomText = await importCustomText();
39+
40+
CustomText.setText([
41+
"The",
42+
"quick",
43+
"brown",
44+
"fox",
45+
"jumps",
46+
"over",
47+
"the",
48+
"lazy",
49+
"dog",
50+
]);
51+
52+
expect(CustomText.getEffectiveText(["uno", "dos", "tres"])).toEqual([
53+
"The",
54+
"quick",
55+
"brown",
56+
"fox",
57+
"jumps",
58+
"over",
59+
"the",
60+
"lazy",
61+
"dog",
62+
]);
63+
});
64+
65+
it("returns stored text for UI", async () => {
66+
const CustomText = await importCustomText();
67+
68+
expect(CustomText.getStoredText()).toEqual([
69+
"The",
70+
"quick",
71+
"brown",
72+
"fox",
73+
"jumps",
74+
"over",
75+
"the",
76+
"lazy",
77+
"dog",
78+
]);
79+
});
80+
});

frontend/src/ts/components/modals/CustomTextModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export function CustomTextModal(): JSXElement {
244244
if (
245245
mode === "repeat" &&
246246
CustomText.getLimitMode() !== "time" &&
247-
CustomText.getLimitValue() === CustomText.getText().length
247+
CustomText.getLimitValue() === CustomText.getStoredText().length
248248
) {
249249
mode = "simple";
250250
}
@@ -264,7 +264,7 @@ export function CustomTextModal(): JSXElement {
264264
}
265265
}
266266

267-
const text = CustomText.getText()
267+
const text = CustomText.getStoredText()
268268
.join(pipeDelimiter ? "|" : " ")
269269
.replace(/^ +/gm, "");
270270

frontend/src/ts/test/custom-text.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,28 @@ const customTextLongLS = new LocalStorageWithSchema({
2727
fallback: {},
2828
});
2929

30+
const userEditedSettingsText = new LocalStorageWithSchema({
31+
key: "customTextSettingsUserEdited",
32+
schema: z.boolean(),
33+
fallback: false,
34+
});
35+
3036
type CustomTextLimit = z.infer<typeof CustomTextSettingsSchema>["limit"];
3137

38+
const defaultCustomText = [
39+
"The",
40+
"quick",
41+
"brown",
42+
"fox",
43+
"jumps",
44+
"over",
45+
"the",
46+
"lazy",
47+
"dog",
48+
];
49+
3250
const defaultCustomTextSettings: CustomTextSettings = {
33-
text: ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"],
51+
text: defaultCustomText,
3452
mode: "repeat",
3553
limit: { value: 9, mode: "word" },
3654
pipeDelimiter: false,
@@ -57,12 +75,28 @@ const customTextSettings = new LocalStorageWithSchema({
5775
},
5876
});
5977

60-
export function getText(): string[] {
78+
function isDefaultCustomText(text: string[]): boolean {
79+
return (
80+
text.length === defaultCustomText.length &&
81+
text.every((word, index) => word === defaultCustomText[index])
82+
);
83+
}
84+
85+
export function getStoredText(): string[] {
6186
return customTextSettings.get().text;
6287
}
6388

89+
export function getEffectiveText(languageWords: string[]): string[] {
90+
const text = customTextSettings.get().text;
91+
if (!userEditedSettingsText.get() && isDefaultCustomText(text)) {
92+
return languageWords.slice(0, text.length);
93+
}
94+
return text;
95+
}
96+
6497
export function setText(txt: string[]): void {
6598
const currentSettings = customTextSettings.get();
99+
userEditedSettingsText.set(true);
66100
customTextSettings.set({
67101
...currentSettings,
68102
text: txt,

frontend/src/ts/test/test-logic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ async function init(): Promise<boolean> {
536536
},
537537
customText: {
538538
...CustomText.getData(),
539-
text: `${CustomText.getText().length} words`,
539+
text: `${CustomText.getStoredText().length} words`,
540540
},
541541
mode: Config.mode,
542542
mode2: Misc.getMode2(Config, null),

frontend/src/ts/test/words-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ export async function generateWords(
638638

639639
let wordList = language.words;
640640
if (Config.mode === "custom") {
641-
wordList = CustomText.getText();
641+
wordList = CustomText.getEffectiveText(language.words);
642642
} else if (Config.mode === "quote") {
643643
wordList = await getQuoteWordList(language, wordOrder);
644644
} else if (Config.mode === "zen") {

0 commit comments

Comments
 (0)