-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ui.ts
139 lines (127 loc) · 4.28 KB
/
ui.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import Config from "./config";
import * as Caret from "./test/caret";
import * as Notifications from "./elements/notifications";
import * as CustomText from "./test/custom-text";
import * as TestState from "./test/test-state";
import * as ConfigEvent from "./observables/config-event";
import { debounce, throttle } from "throttle-debounce";
import * as TestUI from "./test/test-ui";
import { get as getActivePage } from "./states/active-page";
import { isDevEnvironment } from "./utils/misc";
let isPreviewingFont = false;
export function previewFontFamily(font: string): void {
document.documentElement.style.setProperty(
"--font",
'"' + font.replace(/_/g, " ") + '", "Roboto Mono", "Vazirmatn"'
);
isPreviewingFont = true;
}
export function clearFontPreview(): void {
if (!isPreviewingFont) return;
previewFontFamily(Config.fontFamily);
isPreviewingFont = false;
}
function updateKeytips(): void {
const modifierKey = window.navigator.userAgent.toLowerCase().includes("mac")
? "cmd"
: "ctrl";
if (Config.quickRestart === "esc") {
$(".pageSettings .tip").html(`
tip: You can also change all these settings quickly using the
command line (<key>${modifierKey}</key>+<key>shift</key>+<key>p</key>)`);
} else {
$(".pageSettings .tip").html(`
tip: You can also change all these settings quickly using the
command line (<key>esc</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key>)`);
}
if (Config.quickRestart === "esc") {
$("footer .keyTips").html(`
<key>esc</key> - restart test<br>
<key>tab</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key> - command line`);
} else if (Config.quickRestart === "tab") {
$("footer .keyTips").html(`
<key>tab</key> - restart test<br>
<key>esc</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key> - command line`);
} else if (Config.quickRestart === "enter") {
$("footer .keyTips").html(`
<key>enter</key> - restart test<br>
<key>esc</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key> - command line`);
} else {
$("footer .keyTips").html(`
<key>tab</key> + <key>enter</key> - restart test<br>
<key>esc</key> or <key>${modifierKey}</key>+<key>shift</key>+<key>p</key> - command line`);
}
}
if (isDevEnvironment()) {
window.onerror = function (error): void {
Notifications.add(JSON.stringify(error), -1);
};
$("header #logo .top").text("localhost");
$("head title").text($("head title").text() + " (localhost)");
$("body").append(
`<div class="devIndicator tl">local</div><div class="devIndicator br">local</div>`
);
}
//stop space scrolling
window.addEventListener("keydown", function (e) {
if (e.code === "Space" && e.target === document.body) {
e.preventDefault();
}
});
window.addEventListener("beforeunload", (event) => {
// Cancel the event as stated by the standard.
if (
(Config.mode === "words" && Config.words < 1000) ||
(Config.mode === "time" && Config.time < 3600) ||
Config.mode === "quote" ||
(Config.mode === "custom" &&
CustomText.isWordRandom &&
CustomText.word < 1000) ||
(Config.mode === "custom" &&
CustomText.isTimeRandom &&
CustomText.time < 1000) ||
(Config.mode === "custom" &&
!CustomText.isWordRandom &&
CustomText.text.length < 1000)
) {
//ignore
} else {
if (TestState.isActive) {
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = "";
}
}
});
const debouncedEvent = debounce(250, () => {
void Caret.updatePosition();
if (getActivePage() === "test" && !TestUI.resultVisible) {
if (Config.tapeMode !== "off") {
TestUI.scrollTape();
} else {
const word =
document.querySelectorAll<HTMLElement>("#words .word")[
TestUI.currentWordElementIndex - 1
];
if (word) {
const currentTop: number = Math.floor(word.offsetTop);
TestUI.lineJump(currentTop);
}
}
}
setTimeout(() => {
if ($("#wordsInput").is(":focus")) {
Caret.show();
}
}, 250);
});
const throttledEvent = throttle(250, () => {
Caret.hide();
});
$(window).on("resize", () => {
throttledEvent();
debouncedEvent();
});
ConfigEvent.subscribe((eventKey) => {
if (eventKey === "quickRestart") updateKeytips();
});