1- const tagsEl = document . getElementById ( 'tags' )
2- const textarea = document . getElementById ( 'textarea' )
1+ const tagsEl = document . getElementById ( 'tags' ) ;
2+ const textarea = document . getElementById ( 'textarea' ) ;
33
4- textarea . focus ( )
4+ textarea . focus ( ) ;
55
66const 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-
18- const pickRandomTag = ( ) => {
19- const tags = document . querySelectorAll ( '.tag' )
20- return tags [ Math . floor ( Math . random ( ) * tags . length ) ]
21- }
22-
23- const highlightTag = ( tag ) => {
24- tag . classList . add ( 'highlight' )
25- }
26-
27- const unHighlightTag = ( tag ) => {
28- tag . classList . remove ( 'highlight' )
29- }
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+ } ;
3029
3130const randomSelect = ( ) => {
32- const times = 30
33- const interval = setInterval ( ( ) => {
34- const randomTag = pickRandomTag ( )
35- highlightTag ( randomTag )
36-
37- setTimeout ( ( ) => {
38- unHighlightTag ( randomTag )
39- } , 100 )
40- } , 100 )
41-
42- setTimeout ( ( ) => {
43- clearInterval ( interval )
44-
45- setTimeout ( ( ) => {
46- const randomTag = pickRandomTag ( )
47-
48- highlightTag ( randomTag )
49- } , 100 )
50- } , times * 100 )
51- }
52-
53- textarea . addEventListener ( 'keyup' , ( e ) => {
54- createTags ( e . target . value )
55- if ( e . key === 'Enter' ) {
56- setTimeout ( ( ) => {
57- e . target . value = ''
58- } , 10 )
59- randomSelect ( )
60- }
61- } )
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