maciejpedzi.ch/assets/js/script.js

108 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-11-27 23:12:38 +01:00
const input = document.querySelector('#cmd');
const cmdHistoryElement = document.querySelector('#cmd-history');
const today = new Date();
2020-12-31 11:56:56 +01:00
const currentYear = today.getFullYear();
2021-02-16 18:06:27 +01:00
const inputHistory = localStorage.getItem('inputHistory')
2021-02-17 09:05:53 +01:00
? JSON.parse(localStorage.getItem('inputHistory'))
2021-02-16 18:06:27 +01:00
: [];
2020-11-27 23:12:38 +01:00
let cmdIndex = 0;
2021-02-16 18:06:27 +01:00
2021-01-01 12:17:06 +01:00
document.querySelector('#current-year').textContent = currentYear;
2020-11-27 23:12:38 +01:00
input.focus();
window.addEventListener('keypress', (evt) => {
2021-02-16 18:06:27 +01:00
const inputText = input.value.trim();
const split = inputText.split(/ +/);
const command = split[0];
2020-11-27 23:12:38 +01:00
2021-01-17 19:45:35 +01:00
if (evt.key === 'Enter' && command.length > 0) {
2021-02-16 18:06:27 +01:00
const args = split.slice(1);
2021-01-17 19:45:35 +01:00
const cmdToSave = document.createElement('p');
const output = document.createElement('p');
2021-02-17 09:05:53 +01:00
2021-02-16 18:06:27 +01:00
cmdToSave.textContent = `> ${inputText}`;
2021-01-17 19:45:35 +01:00
cmdHistoryElement.append(cmdToSave);
2020-11-27 23:12:38 +01:00
2021-01-17 19:45:35 +01:00
switch (command.toLowerCase()) {
default:
output.textContent = `ERROR: Unknown command '${command}'`;
break;
case 'about':
const timeDiff = today.getTime() - new Date('2005-05-08').getTime();
const age = Math.floor(timeDiff / (3600 * 24 * 365 * 1000));
2020-12-31 11:56:56 +01:00
2021-01-17 19:45:35 +01:00
output.textContent = `Maciej Pedzich is a ${age}-year-old high school student from Kielce, Poland.
2020-11-27 23:12:38 +01:00
He makes web applications using Vue.js, Node.js, Express and MongoDB/PostgreSQL, but he likes experimenting with other solutions too.
He believes that by being creative and cooperating with others, you can achieve success.
2020-12-31 11:56:56 +01:00
When not coding, he is probably watching an F1 race, or playing retro video games.`;
2021-01-17 19:45:35 +01:00
break;
2021-02-16 18:06:27 +01:00
case 'bgcolor':
2021-02-17 09:05:53 +01:00
const [bgcolor] = args;
2021-02-16 18:06:27 +01:00
document.body.style.backgroundColor = bgcolor;
break;
2021-01-17 19:45:35 +01:00
case 'cls':
cmdHistoryElement.textContent = '';
break;
2021-02-16 18:06:27 +01:00
case 'color':
2021-02-17 09:05:53 +01:00
const [color] = args;
2021-02-16 18:06:27 +01:00
document.body.style.color = color;
input.style.color = color;
break;
2021-01-17 19:45:35 +01:00
case 'contact':
output.innerHTML = `Email address:
2020-11-27 23:12:38 +01:00
<a href="mailto:contact@maciejpedzi.ch">contact@maciejpedzi.ch</a>`;
2021-01-17 19:45:35 +01:00
break;
case 'github':
2021-02-17 09:05:53 +01:00
output.innerHTML = `If a new tab didn't show up, go <a href="https://github.com/maciejpedzich">here</a>`;
2021-01-17 19:45:35 +01:00
window.open('https://github.com/maciejpedzich');
break;
case "'help'":
output.textContent = 'Without the quotes, dummy.';
break;
case 'help':
output.innerHTML = `<p>about - shows everything you need to know about Maciej</p>
2021-02-16 18:06:27 +01:00
<p>bgcolor [color] - sets background color to given [color]</p>
<p>cls - clears screen</p>
<p>color [color] - sets text color to given [color]</p>
2021-01-05 22:13:31 +01:00
<p>contact - displays contact information</p>
2021-02-16 18:06:27 +01:00
<p>github - opens Maciej's Github profile page</p>
2020-11-27 23:12:38 +01:00
<p>help - displays a list of available commands</p>
2020-12-13 18:01:59 +01:00
<p>skills - presents a set of current skills</p>
2021-01-05 22:13:31 +01:00
<p>If on desktop/laptop, use up and down arrows to retype commands</p>`;
2021-01-17 19:45:35 +01:00
break;
case 'skills':
output.innerHTML = `<p>Frontend: HTML, CSS, JavaScript, TypeScript, Vue.js</p>
2020-11-27 23:12:38 +01:00
<p>Backend: JavaScript, TypeScript, Node.js, Express</p>
<p>Database: MongoDB, PostgreSQL</p>
2021-01-05 22:13:31 +01:00
<p>Tooling: Git, Visual Studio Code, Bash, Windows PowerShell, Postman</p>
2021-02-16 18:06:27 +01:00
<p>Hosting/Deployment: Netlify, Heroku, Amazon Web Services, MongoDB Atlas</p>
<p>Looking to learn: Nuxt.js, GraphQL, Vim</p>`;
2021-01-17 19:45:35 +01:00
break;
}
2020-11-27 23:12:38 +01:00
2021-02-16 18:06:27 +01:00
inputHistory.unshift(inputText);
2021-02-17 09:05:53 +01:00
localStorage.setItem(
'inputHistory',
JSON.stringify(inputHistory.slice(0, 10))
);
2021-02-16 18:06:27 +01:00
2021-01-17 19:45:35 +01:00
cmdHistoryElement.append(output);
input.value = '';
input.scrollIntoView();
}
2020-12-13 18:01:59 +01:00
});
window.addEventListener('keydown', (evt) => {
2021-02-16 18:06:27 +01:00
if (inputHistory.length > 0) {
2021-01-17 19:45:35 +01:00
if (evt.key === 'ArrowUp') {
2021-02-16 18:06:27 +01:00
cmdIndex - 1 >= 0 ? cmdIndex-- : (cmdIndex = inputHistory.length - 1);
input.value = inputHistory[cmdIndex];
2021-01-17 19:45:35 +01:00
} else if (evt.key === 'ArrowDown') {
2021-02-16 18:06:27 +01:00
cmdIndex + 1 < inputHistory.length ? cmdIndex++ : (cmdIndex = 0);
input.value = inputHistory[cmdIndex];
2021-01-17 19:45:35 +01:00
}
}
2020-11-27 23:12:38 +01:00
});
2021-02-16 18:06:27 +01:00
input.addEventListener('blur', (evt) => input.focus());