Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom text mode #68

Closed
wants to merge 12 commits into from
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ available modes:

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

## punctuation

Expand All @@ -92,4 +93,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 @@ -47,6 +47,9 @@ <h2 id="header">typings</h2>
<text> / </text>
<span id="tc-240" onclick="setTimeCount(240)">240</span>
</span>
<span id="custom">
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
<span style="border-bottom: 2px solid;">CUSTOM</span>
</span>
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
</div>
<div id="right-wing">WPM: XX / ACC: XX</div>
</div>
Expand Down
50 changes: 42 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,23 @@ function setText() {
wordList = [];
currentWord = 0;
correctKeys = 0;
inputField.value = '';
timerActive = false;
clearTimeout(timer);
textDisplay.style.display = 'block';
inputField.className = '';

switch (typingMode) {
case 'custom':
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
const helpText = "Paste your custom text and click redo!"
wordList = (inputField.value === 'custom' || inputField.value === '') ? helpText.split(" "): betterSplit(inputField.value);
punctuation = false;
textDisplay.style.height = '3.2rem';
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
textDisplay.innerHTML = '';
inputField.value='';
break;

case 'wordcount':
inputField.value = '';
textDisplay.style.height = 'auto';
textDisplay.innerHTML = '';
wordList = [];
Expand All @@ -51,6 +60,7 @@ function setText() {
break;

case 'time':
inputField.value = '';
textDisplay.style.height = '3.2rem';
document.querySelector(`#tc-${timeCount}`).innerHTML = timeCount;
textDisplay.innerHTML = '';
Expand All @@ -60,7 +70,6 @@ function setText() {
wordList.push(randomWords[n]);
}
}

if (punctuation) addPunctuations();
showText();
inputField.focus();
Expand Down Expand Up @@ -111,13 +120,15 @@ function showText() {
inputField.addEventListener('keydown', e => {
// Add wrong class to input field
switch (typingMode) {
case 'custom':
case 'wordcount':
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
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 === ';')) {
const puncRegex = /[!:;'",.\/?!@#$%^&*()_}{\[\]-]/;
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -133,6 +144,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 @@ -166,7 +178,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 @@ -207,6 +219,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 @@ -310,16 +323,26 @@ function setTypingMode(_mode) {
typingMode = mode;
setCookie('typingMode', mode, 90);
document.querySelector('#word-count').style.display = 'inline';
document.querySelector('#time-count').style.display = 'none';
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('#time-count').style.display = 'inline';
document.querySelector('#custom').style.display = 'none';
setText();
break;
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:
console.error(`mode ${mode} is undefine`);
}
Expand Down Expand Up @@ -445,4 +468,15 @@ 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 regex = /[\u0A00-\u0A7F\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f\u3131-\uD79D]+|[\u0400-\u04FFa-zA-Z0-9!:;'",.\/?!@#$%^&*()_}{\[\]-]+\'*[a-z]*/g;
andybui01 marked this conversation as resolved.
Show resolved Hide resolved
let array = [...str.match(regex)];
return array;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure you have a newline at the end of your file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not "reformat" the whole file! Configure your editor to use the original code style.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yh, I'll fix that, while changing the tab indents to spaces I must've inadvertently reformated the entire file.