Skip to content

Commit

Permalink
Merge branch 'custom' into merges
Browse files Browse the repository at this point in the history
I used unix2dos to resolve conflicts
  • Loading branch information
SeerLite committed Dec 18, 2020
2 parents 4d7f934 + 4b154b8 commit fae2208
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ available modes:

- `wordcount`
- `time`
- `custom`

## punctuation

Expand All @@ -100,4 +101,4 @@ acc: total number of characters (including spaces) of words you got right divide

## support

- <a href="https://www.paypal.me/briano1905" target="_blank">PayPal</a>
- <a href="https://www.paypal.me/briano1905" target="_blank">PayPal</a>
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ <h2 id="header">typings</h2>
<text> / </text>
<span id="tc-240" onclick="setTimeCount(240)">240</span>
</span>
<span id="custom" style="border-bottom: 2px solid;">
CUSTOM
</span>
</div>
<div id="right-wing">WPM: XX / ACC: XX</div>
</div>
Expand Down
86 changes: 59 additions & 27 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ let timer;
let timerActive = false;
let punctuation = false;

// Initialize global constants
const PUNCREGEX = /!:;'",.\/?!@#$%^&*()_}{\[\]\-s\+\=/;

// Initialize favicon canvas
canvas.width = 64;
canvas.height = 64;
Expand All @@ -38,50 +41,50 @@ getCookie('typingMode') === '' ? setTypingMode('wordcount') : setTypingMode(getC
getCookie('punctuation') === '' ? setPunctuation('false') : setPunctuation(getCookie('punctuation'));

// Find a list of words and display it to textDisplay
function setText(e) {
e = e || window.event;
var keepWordList = e && e.shiftKey;

function setText() {
// Reset
if (!keepWordList) {
wordList = [];
}
wordList = [];
currentWord = 0;
correctKeys = 0;
inputField.value = '';
timerActive = false;
clearTimeout(timer);
textDisplay.style.display = 'block';
inputField.className = '';

switch (typingMode) {
case 'custom':
const helpText = "Paste your custom text and click redo!"
wordList = (inputField.value === 'custom' || inputField.value === '') ? helpText.split(" "): betterSplit(inputField.value);
punctuation = false;
textDisplay.style.maxHeight = '3.2rem';
textDisplay.innerHTML = '';
inputField.value='';
break;

case 'wordcount':
inputField.value = '';
textDisplay.style.height = 'auto';
textDisplay.innerHTML = '';
if (!keepWordList) {
wordList = [];
while (wordList.length < wordCount) {
const randomWord = randomWords[Math.floor(Math.random() * randomWords.length)];
if (wordList[wordList.length - 1] !== randomWord || wordList[wordList.length - 1] === undefined || getCookie('language') === 'dots') {
wordList.push(randomWord);
}
wordList = [];
while (wordList.length < wordCount) {
const randomWord = randomWords[Math.floor(Math.random() * randomWords.length)];
if (wordList[wordList.length - 1] !== randomWord || wordList[wordList.length - 1] === undefined || getCookie('language') === 'dots') {
wordList.push(randomWord);
}
}
break;

case 'time':
inputField.value = '';
textDisplay.style.height = '3.2rem';
document.querySelector(`#tc-${timeCount}`).innerHTML = timeCount;
textDisplay.innerHTML = '';
if (!keepWordList) {
wordList = [];
for (i = 0; i < 500; i++) {
let n = Math.floor(Math.random() * randomWords.length);
wordList.push(randomWords[n]);
}
wordList = [];
for (i = 0; i < 500; i++) {
let n = Math.floor(Math.random() * randomWords.length);
wordList.push(randomWords[n]);
}
}

if (punctuation) addPunctuations();
showText();
inputField.focus();
Expand Down Expand Up @@ -132,13 +135,15 @@ function showText() {
inputField.addEventListener('keydown', e => {
// Add wrong class to input field
switch (typingMode) {
case 'custom':
case 'wordcount':
if (currentWord < wordList.length) inputFieldClass();
case 'time':
if (timerActive) inputFieldClass();
}
function inputFieldClass() {
if (e.key >= 'a' && e.key <= 'z' || (e.key === `'` || e.key === ',' || e.key === '.' || e.key === ';')) {
var puncRegex = new RegExp('[' + PUNCREGEX.source + ']');
if (e.key >= 'a' && e.key <= 'z' || puncRegex.test(e.key)) {
let inputWordSlice = inputField.value + e.key;
let currentWordSlice = wordList[currentWord].slice(0, inputWordSlice.length);
inputField.className = inputWordSlice === currentWordSlice ? '' : 'wrong';
Expand All @@ -154,6 +159,7 @@ inputField.addEventListener('keydown', e => {
// If it is the first character entered
if (currentWord === 0 && inputField.value === '') {
switch (typingMode) {
case 'custom':
case 'wordcount':
startDate = Date.now();
break;
Expand Down Expand Up @@ -187,7 +193,7 @@ inputField.addEventListener('keydown', e => {

if (inputField.value !== '') {
// Scroll down text when reach new line
if (typingMode === 'time') {
if (typingMode === 'time' || typingMode === 'custom') {
const currentWordPosition = textDisplay.childNodes[currentWord].getBoundingClientRect();
const nextWordPosition = textDisplay.childNodes[currentWord + 1].getBoundingClientRect();
if (currentWordPosition.top < nextWordPosition.top) {
Expand Down Expand Up @@ -228,6 +234,7 @@ inputField.addEventListener('keydown', e => {
function showResult() {
let words, minute, acc;
switch (typingMode) {
case 'custom':
case 'wordcount':
words = correctKeys / 5;
minute = (Date.now() - startDate) / 1000 / 60;
Expand Down Expand Up @@ -276,8 +283,6 @@ document.addEventListener('keydown', e => {
hideThemeCenter();
inputField.focus();
}
} else if (e.key === 'Escape') {
setText(e);
}
});

Expand Down Expand Up @@ -347,13 +352,23 @@ function setTypingMode(_mode) {
setCookie('typingMode', mode, 90);
document.querySelector('#word-count').style.display = 'inline';
document.querySelector('#time-count').style.display = 'none';
document.querySelector('#custom').style.display = 'none';
setText();
break;
case 'time':
typingMode = mode;
setCookie('typingMode', mode, 90);
document.querySelector('#word-count').style.display = 'none';
document.querySelector('#time-count').style.display = 'inline';
document.querySelector('#custom').style.display = 'none';
setText();
break;
case 'custom':
typingMode = mode;
setCookie('typingMode', mode, 90);
document.querySelector('#word-count').style.display = 'none';
document.querySelector('#time-count').style.display = 'none';
document.querySelector('#custom').style.display = 'inline';
setText();
break;
default:
Expand Down Expand Up @@ -514,8 +529,25 @@ function hideThemeCenter() {
document.getElementById('command-center').classList.remove('hidden');
}

function betterSplit(str) {
// Regex tester for:
// Normal a-z, A-Z
// punjabi,
// CJK,
// Russian
// TODO: Add support for remaining European languages
// Cleaner way to do this???
const regex1 = "[\u0A00-\u0A7F\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f\u3131-\uD79D]+|[\u0400-\u04FFa-zA-Z0-9";
const regex2 = "]+\'*[a-z]*";


const regex = new RegExp(regex1 + PUNCREGEX.source + regex2, "g");
let array = [...str.match(regex)];
return array;
}

document.querySelector('body').addEventListener('transitionend', function () {
setFavicon();
});

setFavicon();
setFavicon();

0 comments on commit fae2208

Please sign in to comment.