-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (85 loc) · 2.2 KB
/
script.js
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
const result = document.getElementById('carta-gerada');
const inputLetter = document.getElementById('carta-texto');
const button = document.getElementById('criar-carta');
const counter = document.getElementById('carta-contador');
const hiddenSection = document.getElementById('word-counter');
const hint = document.getElementById('hint');
let tick = 0;
const style = [
'medium',
'big',
'reallybig',
'newspaper',
'magazine1',
'magazine2',
'rotateleft',
'rotateright',
'skewleft',
'skewright',
];
function wordGen(word) {
const nWord = document.createElement('span');
nWord.innerText = word;
nWord.className = style[Math.floor(Math.random() * style.length)];
result.appendChild(nWord);
}
function refreshStyle(event) {
const selected = event.target;
if (tick === style.length) {
tick = 0;
} else {
selected.className = style[tick];
tick += 1;
}
}
function autoSave() {
localStorage.setItem('mystery', inputLetter.value);
}
function hideHint() {
hint.style.display = 'none';
}
function showHint() {
if (inputLetter.value.length > 0) {
hint.style.display = 'block';
setTimeout(hideHint, 3000);
}
}
function renameButton() {
button.innerText = 'Create';
}
function createLetter() {
hiddenSection.style.display = 'flex';
result.innerHTML = '';
let word = inputLetter.value.split(' ');
word = word.filter((i) => i !== '');
counter.innerText = word.length;
if (word.length === 0) {
renameButton();
result.innerText = 'por favor, digite o conteúdo da carta.';
} else {
button.innerText = 'Refresh';
}
word.forEach(wordGen);
document.querySelectorAll('span').forEach((element) => {
element.addEventListener('click', refreshStyle);
});
autoSave();
}
function syncSave() {
const localList = localStorage.getItem('mystery');
if (localList) {
inputLetter.value = localList;
createLetter();
button.innerText = 'Refresh';
}
}
function inputEnter(event) {
if (event.key === 'Enter') {
createLetter();
}
}
inputLetter.addEventListener('input', renameButton);
inputLetter.addEventListener('keyup', inputEnter);
button.addEventListener('click', createLetter);
button.addEventListener('dblclick', showHint);
window.onload = syncSave;