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