monkeytype/public/js/commandline.js

981 lines
22 KiB
JavaScript
Raw Normal View History

function capitalizeFirstLetter(str) {
2020-07-03 08:35:45 +08:00
return str.charAt(0).toUpperCase() + str.slice(1);
}
let commands = {
2020-07-03 08:35:45 +08:00
title: "",
list: [
{
id: "togglePunctuation",
display: "Toggle punctuation",
exec: () => {
togglePunctuation();
restartTest();
},
},
{
id: "changeMode",
display: "Change mode...",
subgroup: true,
exec: () => {
currentCommands.push(commandsMode);
showCommandLine();
},
},
{
id: "changeTimeConfig",
display: "Change time config...",
subgroup: true,
exec: () => {
currentCommands.push(commandsTimeConfig);
showCommandLine();
},
},
{
id: "changeWordCount",
display: "Change word count...",
subgroup: true,
exec: () => {
currentCommands.push(commandsWordCount);
showCommandLine();
},
},
{
visible: false,
id: "changeTags",
display: "Change tags...",
subgroup: true,
exec: () => {
updateCommandsTagsList();
currentCommands.push(commandsTags);
showCommandLine();
},
},
{
id: "toggleSmoothCaret",
display: "Toggle smooth caret",
exec: () => {
toggleSmoothCaret();
},
},
{
id: "toggleQuickTab",
display: "Toggle quick tab mode",
exec: () => {
toggleQuickTabMode();
},
},
{
id: "toggleShowLiveWpm",
display: "Toggle live wpm display",
exec: () => {
config.showLiveWpm = !config.showLiveWpm;
saveConfigToCookie();
},
},
{
id: "toggleTimerBar",
display: "Toggle timer display",
exec: () => {
config.showTimerBar = !config.showTimerBar;
saveConfigToCookie();
},
},
{
id: "toggleKeyTips",
display: "Toggle keybind tips",
exec: () => {
toggleKeyTips();
},
},
{
id: "toggleFreedom",
display: "Toggle freedom mode",
exec: () => {
toggleFreedomMode();
},
},
{
id: "toggleMaxConfidence",
display: "Toggle max confidence",
exec: () => {
toggleMaxConfidence();
},
},
{
id: "toggleBlindMode",
display: "Toggle blind mode",
exec: () => {
toggleBlindMode();
},
},
2020-07-15 05:01:05 +08:00
{
id: "toggleStopOnError",
display: "Toggle stop on error",
exec: () => {
toggleStopOnError();
},
},
2020-07-03 08:35:45 +08:00
{
id: "toggleQuickEnd",
display: "Toggle quick end",
exec: () => {
toggleQuickEnd();
},
},
{
id: "toggleFlipTestColors",
display: "Toggle flip test colors",
exec: () => {
toggleFlipTestColors();
},
},
{
id: "toggleShowAllLines",
display: "Toggle show all lines",
exec: () => {
toggleShowAllLines();
},
},
2020-07-03 08:35:45 +08:00
{
id: "toggleColorfulMode",
display: "Toggle colorful mode",
exec: () => {
toggleColorfulMode();
},
},
{
id: "toggleRandomTheme",
display: "Toggle random theme",
exec: () => {
toggleRandomTheme();
},
},
2020-07-03 08:35:45 +08:00
{
id: "changeDifficulty",
display: "Change difficulty...",
subgroup: true,
exec: () => {
currentCommands.push(commandsDifficulty);
showCommandLine();
},
},
{
id: "changeCaretStyle",
display: "Change caret style...",
subgroup: true,
exec: () => {
currentCommands.push(commandsCaretStyle);
showCommandLine();
},
},
{
id: "changeTimerStyle",
2020-07-13 23:00:27 +08:00
display: "Change timer/progress style...",
2020-07-03 08:35:45 +08:00
subgroup: true,
exec: () => {
currentCommands.push(commandsTimerStyle);
showCommandLine();
},
},
2020-07-12 07:45:52 +08:00
{
id: "changeTimerColor",
2020-07-13 23:00:27 +08:00
display: "Change timer/progress color...",
2020-07-12 07:45:52 +08:00
subgroup: true,
exec: () => {
currentCommands.push(commandsTimerColor);
showCommandLine();
},
},
{
id: "changeTimerOpacity",
2020-07-13 23:00:27 +08:00
display: "Change timer/progress opacity...",
subgroup: true,
exec: () => {
currentCommands.push(commandsTimerOpacity);
showCommandLine();
},
},
2020-07-03 08:35:45 +08:00
{
id: "changeTheme",
display: "Change theme...",
subgroup: true,
exec: () => {
currentCommands.push(commandsThemes);
showCommandLine();
},
},
{
id: "changeLanguage",
display: "Change language...",
subgroup: true,
exec: () => {
currentCommands.push(commandsLanguages);
showCommandLine();
},
},
{
id: "changeLayout",
display: "Change layout...",
subgroup: true,
exec: () => {
currentCommands.push(commandsLayouts);
showCommandLine();
},
},
{
id: "changeFontSize",
display: "Change font size...",
subgroup: true,
exec: () => {
currentCommands.push(commandsFontSize);
showCommandLine();
},
},
{
id: "joinDiscord",
display: "Join the Discord server",
exec: () => {
window.open("https://discord.gg/yENzqcB");
},
},
],
};
let commandsDifficulty = {
2020-07-03 08:35:45 +08:00
title: "Change difficulty...",
list: [
{
id: "setDifficultyNormal",
display: "Normal",
exec: () => {
setDifficulty("normal");
},
},
{
id: "setDifficultyExpert",
display: "Expert",
exec: () => {
setDifficulty("expert");
},
},
{
id: "setDifficultyMaster",
display: "Master",
exec: () => {
setDifficulty("master");
},
},
],
};
2020-05-29 12:32:38 +08:00
let commandsCaretStyle = {
2020-07-03 08:35:45 +08:00
title: "Change caret...",
list: [
{
id: "setCaretStyleDefault",
display: "line",
exec: () => {
setCaretStyle("default");
},
},
{
id: "setCaretStyleBlock",
display: "block",
exec: () => {
setCaretStyle("block");
},
},
{
id: "setCaretStyleOutline",
display: "outline-block",
exec: () => {
setCaretStyle("outline");
},
},
{
id: "setCaretStyleUnderline",
display: "underline",
exec: () => {
setCaretStyle("underline");
},
},
],
};
2020-06-16 06:13:40 +08:00
let commandsTimerStyle = {
2020-07-13 23:00:27 +08:00
title: "Change timer/progress style...",
2020-07-03 08:35:45 +08:00
list: [
{
id: "setTimerStyleBar",
display: "bar",
exec: () => {
setTimerStyle("bar");
},
},
{
id: "setTimerStyleText",
display: "text",
exec: () => {
setTimerStyle("text");
},
},
],
};
2020-06-16 06:13:40 +08:00
2020-07-12 07:45:52 +08:00
let commandsTimerColor = {
2020-07-13 23:00:27 +08:00
title: "Change timer/progress color...",
2020-07-12 07:45:52 +08:00
list: [
{
id: "setTimerColorBlack",
display: "black",
exec: () => {
setTimerColor("bar");
},
},
{
id: "setTimerColorSub",
display: "sub",
exec: () => {
setTimerColor("sub");
},
},
{
id: "setTimerColorText",
display: "text",
exec: () => {
setTimerColor("text");
},
},
{
id: "setTimerColorMain",
display: "main",
exec: () => {
setTimerColor("main");
},
},
],
};
let commandsTimerOpacity = {
title: "Change timer opacity...",
list: [
{
id: "setTimerOpacity.25",
display: ".25",
exec: () => {
2020-07-13 23:00:27 +08:00
setTimerOpacity(0.25);
},
},
{
id: "setTimerOpacity.5",
display: ".5",
exec: () => {
2020-07-13 23:00:27 +08:00
setTimerOpacity(0.5);
},
},
{
id: "setTimerOpacity.75",
display: ".75",
exec: () => {
2020-07-13 23:00:27 +08:00
setTimerOpacity(0.75);
},
},
{
id: "setTimerOpacity1",
display: "1",
exec: () => {
2020-07-13 09:17:23 +08:00
setTimerOpacity(1);
},
},
],
};
let commandsWordCount = {
2020-07-03 08:35:45 +08:00
title: "Change word count...",
list: [
{
id: "changeWordCount10",
display: "10",
exec: () => {
changeWordCount("10");
restartTest();
},
},
{
id: "changeWordCount25",
display: "25",
exec: () => {
changeWordCount("25");
restartTest();
},
},
{
id: "changeWordCount50",
display: "50",
exec: () => {
changeWordCount("50");
restartTest();
},
},
{
id: "changeWordCount100",
display: "100",
exec: () => {
changeWordCount("100");
restartTest();
},
},
{
id: "changeWordCount200",
display: "200",
exec: () => {
changeWordCount("200");
restartTest();
},
},
],
};
let commandsMode = {
2020-07-03 08:35:45 +08:00
title: "Change mode...",
list: [
{
id: "changeModeTime",
display: "time",
exec: () => {
changeMode("time");
restartTest();
},
},
{
id: "changeModeWords",
display: "words",
exec: () => {
changeMode("words");
restartTest();
},
},
{
id: "changeModeQuote",
display: "quote",
exec: () => {
changeMode("quote");
restartTest();
},
},
{
id: "changeModeCustom",
display: "custom",
exec: () => {
changeMode("custom");
restartTest();
},
},
],
};
let commandsTimeConfig = {
2020-07-03 08:35:45 +08:00
title: "Change time config...",
list: [
{
id: "changeTimeConfig15",
display: "15",
exec: () => {
changeTimeConfig("15");
restartTest();
},
},
{
id: "changeTimeConfig30",
display: "30",
exec: () => {
changeTimeConfig("30");
restartTest();
},
},
{
id: "changeTimeConfig60",
display: "60",
exec: () => {
changeTimeConfig("60");
restartTest();
},
},
{
id: "changeTimeConfig120",
display: "120",
exec: () => {
changeTimeConfig("120");
restartTest();
},
},
],
};
let commandsFontSize = {
2020-07-03 08:35:45 +08:00
title: "Change font size...",
list: [
{
id: "changeFontSize1",
display: "1x",
exec: () => {
changeFontSize(1);
restartTest();
},
},
{
id: "changeFontSize125",
display: "1.25x",
exec: () => {
changeFontSize(125);
restartTest();
},
},
{
id: "changeFontSize15",
display: "1.5x",
exec: () => {
changeFontSize(15);
restartTest();
},
},
{
id: "changeFontSize2",
display: "2x",
exec: () => {
changeFontSize(2);
restartTest();
},
},
],
};
let commandsTags = {
2020-07-03 08:35:45 +08:00
title: "Change tags...",
list: [],
};
2020-07-03 07:01:46 +08:00
function updateCommandsTagsList() {
2020-07-03 08:35:45 +08:00
if (dbSnapshot.tags.length > 0) {
commandsTags.list = [];
dbSnapshot.tags.forEach((tag) => {
let dis = tag.name;
if (tag.active === true) {
dis = '<i class="fas fa-check-square"></i>' + dis;
} else {
dis = '<i class="fas fa-square"></i>' + dis;
}
commandsTags.list.push({
id: "toggleTag" + tag.id,
display: dis,
sticky: true,
exec: () => {
toggleTag(tag.id);
updateTestModesNotice();
let txt = tag.name;
if (tag.active === true) {
txt = '<i class="fas fa-check-square"></i>' + txt;
} else {
txt = '<i class="fas fa-square"></i>' + txt;
}
$(
`#commandLine .suggestions .entry[command='toggleTag${tag.id}']`
).html(txt);
},
});
});
commands.list[4].visible = true;
}
}
2020-05-15 05:30:25 +08:00
let themesList;
2020-07-03 07:01:46 +08:00
$.getJSON("themes/list.json", function (data) {
2020-07-03 08:35:45 +08:00
commandsThemes.list = [];
themesList = data.sort(function (a, b) {
(nameA = a.name.toLowerCase()), (nameB = b.name.toLowerCase());
if (nameA < nameB) return -1;
if (nameA > nameB) return 1;
return 0;
});
data.forEach((theme) => {
commandsThemes.list.push({
id: "changeTheme" + capitalizeFirstLetter(theme.name),
display: theme.name.replace("_", " "),
hover: () => {
previewTheme(theme.name);
},
exec: () => {
setTheme(theme.name);
},
});
});
});
let commandsThemes = {
2020-07-03 08:35:45 +08:00
title: "Change theme...",
list: [
{
id: "couldnotload",
display: "Could not load the themes list :(",
},
],
};
2020-05-16 21:57:01 +08:00
let commandsLanguages = {
2020-07-03 08:35:45 +08:00
title: "Change language...",
list: [
{
id: "couldnotload",
display: "Could not load the languages list :(",
},
],
2020-05-16 21:57:01 +08:00
};
if (Object.keys(words).length > 0) {
2020-07-03 08:35:45 +08:00
commandsLanguages.list = [];
Object.keys(words).forEach((language) => {
if (language === "english_10k") return;
commandsLanguages.list.push({
id: "changeLanguage" + capitalizeFirstLetter(language),
display: language.replace("_", " "),
exec: () => {
changeLanguage(language);
restartTest();
saveConfigToCookie();
},
});
if (language === "english_expanded") {
commandsLanguages.list.push({
id: "changeLanguageEnglish10k",
display: "english 10k",
exec: () => {
changeLanguage("english_10k");
restartTest();
saveConfigToCookie();
},
});
}
});
2020-06-04 00:10:14 +08:00
}
let commandsLayouts = {
2020-07-03 08:35:45 +08:00
title: "Change layout...",
list: [
{
id: "couldnotload",
display: "Could not load the layouts list :(",
},
],
2020-06-04 00:10:14 +08:00
};
if (Object.keys(layouts).length > 0) {
2020-07-03 08:35:45 +08:00
commandsLayouts.list = [];
Object.keys(layouts).forEach((layout) => {
commandsLayouts.list.push({
id: "changeLayout" + capitalizeFirstLetter(layout),
display: layout.replace("_", " "),
exec: () => {
changeLayout(layout);
restartTest();
saveConfigToCookie();
},
});
});
2020-05-16 21:57:01 +08:00
}
$("#commandLine input").keyup((e) => {
2020-07-03 08:35:45 +08:00
if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 13) return;
updateSuggestedCommands();
});
2020-07-03 08:35:45 +08:00
$(document).ready((e) => {
$(document).keydown((event) => {
//escape
if (event.keyCode == 27) {
if ($("#commandLineWrapper").hasClass("hidden")) {
currentCommands = [commands];
showCommandLine();
} else {
if (currentCommands.length > 1) {
currentCommands.pop();
showCommandLine();
} else {
hideCommandLine();
}
2020-07-03 08:35:45 +08:00
if (config.customTheme === true) {
setCustomTheme();
} else {
setTheme(config.theme);
}
2020-07-03 08:35:45 +08:00
}
}
2020-07-03 08:35:45 +08:00
});
});
2020-07-03 08:35:45 +08:00
$("#commandInput textarea").keydown((e) => {
if (e.keyCode == 13 && e.shiftKey) {
//enter
e.preventDefault();
let command = $("#commandInput textarea").attr("command");
let value = $("#commandInput textarea").val();
let list = currentCommands[currentCommands.length - 1];
$.each(list.list, (i, obj) => {
if (obj.id == command) {
obj.exec(value);
if (obj.subgroup !== null && obj.subgroup !== undefined) {
subgroup = obj.subgroup;
}
}
});
try {
2020-07-03 08:35:45 +08:00
firebase.analytics().logEvent("usedCommandLine", {
command: command,
});
} catch (e) {
console.log("Analytics unavailable");
}
hideCommandLine();
}
return;
});
2020-07-03 08:35:45 +08:00
$("#commandLineWrapper #commandLine .suggestions").on("mouseover", (e) => {
$("#commandLineWrapper #commandLine .suggestions .entry").removeClass(
"active"
);
let hoverId = $(e.target).attr("command");
try {
let list = currentCommands[currentCommands.length - 1];
$.each(list.list, (index, obj) => {
if (obj.id == hoverId) {
obj.hover();
}
});
} catch (e) {}
});
2020-07-03 08:35:45 +08:00
$("#commandLineWrapper #commandLine .suggestions").click((e) => {
triggerCommand($(e.target).attr("command"));
});
2020-07-03 08:35:45 +08:00
$("#commandLineWrapper").click((e) => {
if ($(e.target).attr("id") === "commandLineWrapper") {
hideCommandLine();
}
});
$(document).keydown((e) => {
2020-07-03 08:35:45 +08:00
if (!$("#commandLineWrapper").hasClass("hidden")) {
$("#commandLine input").focus();
if (e.keyCode == 13) {
//enter
e.preventDefault();
let command = $(".suggestions .entry.activeKeyboard").attr("command");
triggerCommand(command);
return;
}
if (e.keyCode == 38 || e.keyCode == 40) {
$("#commandLineWrapper #commandLine .suggestions .entry").unbind(
"mouseenter mouseleave"
);
let entries = $(".suggestions .entry");
let activenum = -1;
let hoverId;
$.each(entries, (index, obj) => {
if ($(obj).hasClass("activeKeyboard")) activenum = index;
});
if (e.keyCode == 38) {
entries.removeClass("activeKeyboard");
if (activenum == 0) {
$(entries[entries.length - 1]).addClass("activeKeyboard");
hoverId = $(entries[entries.length - 1]).attr("command");
} else {
$(entries[--activenum]).addClass("activeKeyboard");
hoverId = $(entries[activenum]).attr("command");
}
2020-07-03 08:35:45 +08:00
}
if (e.keyCode == 40) {
entries.removeClass("activeKeyboard");
if (activenum + 1 == entries.length) {
$(entries[0]).addClass("activeKeyboard");
hoverId = $(entries[0]).attr("command");
} else {
$(entries[++activenum]).addClass("activeKeyboard");
hoverId = $(entries[activenum]).attr("command");
2020-05-12 08:33:49 +08:00
}
2020-07-03 08:35:45 +08:00
}
let scroll =
Math.abs(
$(".suggestions").offset().top -
$(".entry.activeKeyboard").offset().top -
$(".suggestions").scrollTop()
) -
$(".suggestions").outerHeight() / 2 +
$($(".entry")[0]).outerHeight();
$(".suggestions").scrollTop(scroll);
console.log(`scrolling to ${scroll}`);
try {
let list = currentCommands[currentCommands.length - 1];
$.each(list.list, (index, obj) => {
if (obj.id == hoverId) {
obj.hover();
}
});
} catch (e) {}
return false;
}
2020-07-03 08:35:45 +08:00
}
});
let currentCommands = [commands];
2020-07-03 07:01:46 +08:00
function triggerCommand(command) {
2020-07-03 08:35:45 +08:00
let subgroup = false;
let input = false;
let list = currentCommands[currentCommands.length - 1];
let sticky = false;
$.each(list.list, (i, obj) => {
if (obj.id == command) {
if (obj.input) {
input = true;
showCommandInput(obj.id, obj.display);
} else {
obj.exec();
if (obj.subgroup !== null && obj.subgroup !== undefined) {
subgroup = obj.subgroup;
}
2020-07-03 08:35:45 +08:00
if (obj.sticky === true) {
sticky = true;
}
2020-07-03 08:35:45 +08:00
}
}
2020-07-03 08:35:45 +08:00
});
if (!subgroup && !input && !sticky) {
try {
firebase.analytics().logEvent("usedCommandLine", {
command: command,
});
} catch (e) {
console.log("Analytics unavailable");
}
hideCommandLine();
}
}
function hideCommandLine() {
2020-07-03 08:35:45 +08:00
$("#commandLineWrapper")
.stop(true, true)
.css("opacity", 1)
.animate(
{
opacity: 0,
},
100,
() => {
$("#commandLineWrapper").addClass("hidden");
}
);
focusWords();
2020-07-03 07:01:46 +08:00
}
function showCommandLine() {
2020-07-03 08:35:45 +08:00
$("#commandLine").removeClass("hidden");
$("#commandInput").addClass("hidden");
if ($("#commandLineWrapper").hasClass("hidden")) {
$("#commandLineWrapper")
.stop(true, true)
.css("opacity", 0)
.removeClass("hidden")
.animate(
{
opacity: 1,
},
100
);
}
$("#commandLine input").val("");
updateSuggestedCommands();
$("#commandLine input").focus();
2020-07-03 07:01:46 +08:00
}
function showCommandInput(command, placeholder) {
2020-07-03 08:35:45 +08:00
$("#commandLineWrapper").removeClass("hidden");
$("#commandLine").addClass("hidden");
$("#commandInput").removeClass("hidden");
$("#commandInput textarea").attr("placeholder", placeholder);
$("#commandInput textarea").val("");
$("#commandInput textarea").focus();
$("#commandInput textarea").attr("command", "");
$("#commandInput textarea").attr("command", command);
2020-07-03 07:01:46 +08:00
}
function updateSuggestedCommands() {
2020-07-03 08:35:45 +08:00
let inputVal = $("#commandLine input").val().toLowerCase().split(" ");
let list = currentCommands[currentCommands.length - 1];
if (inputVal[0] == "") {
$.each(list.list, (index, obj) => {
if (obj.visible !== false) obj.found = true;
});
} else {
$.each(list.list, (index, obj) => {
let foundcount = 0;
$.each(inputVal, (index2, obj2) => {
if (obj2 == "") return;
let re = new RegExp("\\b" + obj2, "g");
let res = obj.display.toLowerCase().match(re);
if (res != null && res.length > 0) {
foundcount++;
} else {
foundcount--;
}
});
if (foundcount > 0) {
if (obj.visible !== false) obj.found = true;
} else {
obj.found = false;
}
});
}
displayFoundCommands();
2020-07-03 07:01:46 +08:00
}
function displayFoundCommands() {
2020-07-03 08:35:45 +08:00
$("#commandLine .suggestions").empty();
let list = currentCommands[currentCommands.length - 1];
$.each(list.list, (index, obj) => {
if (obj.found) {
$("#commandLine .suggestions").append(
'<div class="entry" command="' + obj.id + '">' + obj.display + "</div>"
);
}
});
if ($("#commandLine .suggestions .entry").length == 0) {
$("#commandLine .separator").css({ height: 0, margin: 0 });
} else {
$("#commandLine .separator").css({
height: "1px",
"margin-bottom": ".5rem",
});
}
let entries = $("#commandLine .suggestions .entry");
if (entries.length > 0) {
$(entries[0]).addClass("activeKeyboard");
try {
$.each(list.list, (index, obj) => {
2020-07-03 07:01:46 +08:00
if (obj.found) {
2020-07-03 08:35:45 +08:00
obj.hover();
return false;
}
2020-07-03 08:35:45 +08:00
});
} catch (e) {}
}
$("#commandLine .listTitle").remove();
// if(currentCommands.title != ''){
// $("#commandLine .suggestions").before("<div class='listTitle'>"+currentCommands.title+"</div>");
// }
2020-07-03 07:01:46 +08:00
}