|
| 1 | +const tagsElements = document.getElementById("tags"); |
| 2 | +const textarea = document.getElementById("textarea"); |
| 3 | + |
| 4 | +const createTags = (input) => { |
| 5 | + const tags = input |
| 6 | + .split(",") |
| 7 | + .filter((tag) => tag.trim() !== "") |
| 8 | + .map((tag) => tag.trim()); |
| 9 | + tagsElements.innerHTML = ""; |
| 10 | + tags.forEach((tag) => { |
| 11 | + const tagElement = document.createElement("span"); |
| 12 | + tagElement.classList.add("tag"); |
| 13 | + tagElement.innerText = tag; |
| 14 | + tagsElements.appendChild(tagElement); |
| 15 | + }); |
| 16 | +}; |
| 17 | + |
| 18 | +const pickRandomTag = () => { |
| 19 | + const tags = document.querySelectorAll(".tag"); |
| 20 | + return tags[Math.floor(Math.random() * tags.length)]; |
| 21 | +}; |
| 22 | + |
| 23 | +const highlightTag = (tag) => tag.classList.add("highlight"); |
| 24 | + |
| 25 | +const unHighlightTag = (tag) => tag.classList.remove("highlight"); |
| 26 | + |
| 27 | +const randomSelect = () => { |
| 28 | + const times = 30; |
| 29 | + const interval = setInterval(() => { |
| 30 | + const randomTag = pickRandomTag(); |
| 31 | + highlightTag(randomTag); |
| 32 | + setTimeout(() => { |
| 33 | + unHighlightTag(randomTag); |
| 34 | + }, 100); |
| 35 | + }, 100); |
| 36 | + |
| 37 | + setTimeout(() => { |
| 38 | + clearInterval(interval); |
| 39 | + setTimeout(() => { |
| 40 | + const randomTag = pickRandomTag(); |
| 41 | + highlightTag(randomTag); |
| 42 | + }, 100); |
| 43 | + }, times * 100); |
| 44 | +}; |
| 45 | + |
| 46 | +textarea.focus(); |
| 47 | +textarea.addEventListener("keyup", (e) => { |
| 48 | + createTags(e.target.value); |
| 49 | + if (e.key === "Enter") { |
| 50 | + setTimeout(() => (e.target.value = ""), 10); |
| 51 | + randomSelect(); |
| 52 | + } |
| 53 | +}); |
0 commit comments