Skip to content

Commit

Permalink
feat: add max line width
Browse files Browse the repository at this point in the history
controls how many characters per line (estimated) to display during the test
literally bullied into adding it back, hope you love me again
  • Loading branch information
Miodec committed Apr 26, 2024
1 parent 06ed908 commit 36df36b
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend/src/api/schemas/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const CONFIG_SCHEMA = joi.object({
britishEnglish: joi.boolean(),
lazyMode: joi.boolean(),
showAverage: joi.string().valid("off", "speed", "acc", "both"),
maxLineWidth: joi.number().min(20).max(1000),
});

export default CONFIG_SCHEMA;
22 changes: 22 additions & 0 deletions frontend/src/html/pages/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,28 @@
<button data-config-value="true">on</button>
</div>
</div>
<div class="section" data-config-name="maxLineWidth">
<div class="groupTitle">
<i class="fas fa-text-width"></i>
<span>max line width</span>
</div>
<div class="text">
Change the maximum width of the typing test, measured in characters.
</div>
<div class="inputs">
<div class="inputAndButton">
<input
type="number"
placeholder="max line width"
class="input"
min="20"
/>
<button class="save no-auto-handle">
<i class="fas fa-save fa-fw"></i>
</button>
</div>
</div>
</div>
<div class="section" data-config-name="fontSize">
<div class="groupTitle">
<i class="fas fa-font"></i>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/ts/commandline/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import BailOutCommands from "./lists/bail-out";
import ResultSavingCommands from "./lists/result-saving";
import NavigationCommands from "./lists/navigation";
import FontSizeCommands from "./lists/font-size";
import MaxLineWidthCommands from "./lists/max-line-width";
import ResultScreenCommands from "./lists/result-screen";
import CustomBackgroundSizeCommands from "./lists/background-size";
import CustomBackgroundFilterCommands from "./lists/background-filter";
Expand Down Expand Up @@ -283,6 +284,7 @@ export const commands: MonkeyTypes.CommandsSubgroup = {
...TypingSpeedUnitCommands,
...AlwaysShowDecimalCommands,
...StartGraphsAtZeroCommands,
...MaxLineWidthCommands,
...FontSizeCommands,
...FontFamilyCommands,
...KeymapModeCommands,
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/ts/commandline/lists/max-line-width.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Config, * as UpdateConfig from "../../config";

const commands: MonkeyTypes.Command[] = [
{
id: "changeMaxLineWidth",
display: "Max line width...",
icon: "fa-text-width",
alias: "page",
input: true,
defaultValue: (): string => {
return Config.maxLineWidth.toString();
},
exec: ({ input }): void => {
if (input === undefined || input === "") return;
UpdateConfig.setMaxLineWidth(parseFloat(input));
},
},
];
export default commands;
26 changes: 26 additions & 0 deletions frontend/src/ts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,32 @@ export function setFontSize(fontSize: number, nosave?: boolean): boolean {
return true;
}

export function setMaxLineWidth(
maxLineWidth: number,
nosave?: boolean
): boolean {
if (!isConfigValueValid("max line width", maxLineWidth, ["number"])) {
return false;
}

if (maxLineWidth < 20) {
maxLineWidth = 20;
}
if (maxLineWidth > 1000) {
maxLineWidth = 1000;
}

config.maxLineWidth = maxLineWidth;

saveToLocalStorage("maxLineWidth", nosave);
ConfigEvent.dispatch("maxLineWidth", config.maxLineWidth, nosave);

// trigger a resize event to update the layout - handled in ui.ts:108
$(window).trigger("resize");

return true;
}

export function setCustomBackground(value: string, nosave?: boolean): boolean {
if (!isConfigValueValid("custom background", value, ["string"])) return false;

Expand Down
1 change: 1 addition & 0 deletions frontend/src/ts/constants/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ export default {
lazyMode: false,
showAverage: "off",
tapeMode: "off",
maxLineWidth: 70,
} as SharedTypes.Config;
62 changes: 62 additions & 0 deletions frontend/src/ts/pages/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ async function initGroups(): Promise<void> {
UpdateConfig.setFontSize,
"button"
) as SettingsGroup<SharedTypes.ConfigValue>;
groups["maxLineWidth"] = new SettingsGroup(
"maxLineWidth",
UpdateConfig.setMaxLineWidth,
"button"
) as SettingsGroup<SharedTypes.ConfigValue>;
groups["caretStyle"] = new SettingsGroup(
"caretStyle",
UpdateConfig.setCaretStyle,
Expand Down Expand Up @@ -657,6 +662,10 @@ async function fillSettingsPage(): Promise<void> {
Config.fontSize
);

$(".pageSettings .section[data-config-name='maxLineWidth'] input").val(
Config.maxLineWidth
);

$(".pageSettings .section[data-config-name='customLayoutfluid'] input").val(
Config.customLayoutfluid.replace(/#/g, " ")
);
Expand Down Expand Up @@ -1212,6 +1221,59 @@ $(
}
});

$(
".pageSettings .section[data-config-name='maxLineWidth'] .inputAndButton button.save"
).on("click", () => {
const didConfigSave = UpdateConfig.setMaxLineWidth(
parseFloat(
$(
".pageSettings .section[data-config-name='maxLineWidth'] .inputAndButton input"
).val() as string
)
);
if (didConfigSave) {
Notifications.add("Saved", 1, {
duration: 1,
});
}
});

$(
".pageSettings .section[data-config-name='maxLineWidth'] .inputAndButton input"
).on("focusout", () => {
const didConfigSave = UpdateConfig.setMaxLineWidth(
parseFloat(
$(
".pageSettings .section[data-config-name='maxLineWidth'] .inputAndButton input"
).val() as string
)
);
if (didConfigSave) {
Notifications.add("Saved", 1, {
duration: 1,
});
}
});

$(
".pageSettings .section[data-config-name='maxLineWidth'] .inputAndButton input"
).on("keypress", (e) => {
if (e.key === "Enter") {
const didConfigSave = UpdateConfig.setMaxLineWidth(
parseFloat(
$(
".pageSettings .section[data-config-name='maxLineWidth'] .inputAndButton input"
).val() as string
)
);
if (didConfigSave) {
Notifications.add("Saved", 1, {
duration: 1,
});
}
}
});

$(
".pageSettings .section[data-config-name='customLayoutfluid'] .inputAndButton button.save"
).on("click", () => {
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/ts/test/test-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ ConfigEvent.subscribe((key, value) => {
if (key === "tapeMode") {
if (value === "off") {
$("#typingTest").css({
"max-width": "70ch%",
"max-width": Config.maxLineWidth + "ch",
});
$("#miniTimerAndLiveWpm").css({
"grid-column": "full-width-padding",
Expand All @@ -1545,4 +1545,11 @@ ConfigEvent.subscribe((key, value) => {
});
}
}
if (key === "maxLineWidth") {
if (Config.tapeMode === "off") {
$("#typingTest").css({
"max-width": Config.maxLineWidth + "ch",
});
}
}
});
1 change: 1 addition & 0 deletions shared-types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ declare namespace SharedTypes {
lazyMode: boolean;
showAverage: SharedTypes.Config.ShowAverage;
tapeMode: SharedTypes.Config.TapeMode;
maxLineWidth: number;
}

type ConfigValue = Config[keyof Config];
Expand Down

0 comments on commit 36df36b

Please sign in to comment.