1+ // transpose
2+ document . getElementById ( "transpose" ) . addEventListener ( "click" , function ( ) {
3+ console . log ( 'transpose' ) ;
4+ const t = inputText . value ;
5+ const columns = getColumns ( t ) ;
6+
7+ inputText . value = columns . join ( '\n' ) ;
8+ } ) ;
9+
10+
11+ // Rotate clockwise
12+ document . getElementById ( "rotateClockwise" ) . addEventListener ( "click" , function ( ) {
13+ console . log ( 'transpose' ) ;
14+ const t = inputText . value ;
15+ const lines = t . split ( '\n' ) . filter ( line => line . trim ( ) !== '' ) ;
16+ const columns = getColumns ( t ) ;
17+ console . log ( columns [ 0 ] )
18+
19+ let res = [ ]
20+ for ( let col = 0 ; col < columns . length ; col ++ ) {
21+ newLine = columns [ col ] . split ( '' ) . reverse ( ) . join ( '' ) ;
22+ res . push ( newLine ) ;
23+ }
24+ inputText . value = res . join ( '\n' ) ;
25+ } ) ;
26+
27+ // Rotate counterclockwise
28+ document . getElementById ( "rotateCounterclockwise" ) . addEventListener ( "click" , function ( ) {
29+ console . log ( 'transpose' ) ;
30+ const t = inputText . value ;
31+ const lines = t . split ( '\n' ) . filter ( line => line . trim ( ) !== '' ) ;
32+ const columns = getColumns ( t ) ;
33+ console . log ( columns [ 0 ] )
34+
35+ let res = [ ]
36+ for ( let col = columns . length - 1 ; col >= 0 ; col -- ) {
37+ newLine = columns [ col ] ;
38+ res . push ( newLine ) ;
39+ }
40+ inputText . value = res . join ( '\n' ) ;
41+ } ) ;
42+
43+ // Flip horizontally
44+ document . getElementById ( "flipH" ) . addEventListener ( "click" , function ( ) {
45+ console . log ( 'flipH!' ) ;
46+ const t = inputText . value ;
47+ const lines = t . split ( '\n' ) ;
48+ const flippedLines = lines . map ( line => { return line . split ( '' ) . reverse ( ) . join ( '' ) } ) ; // flip each line
49+ const flippedText = flippedLines . join ( '\n' )
50+ inputText . value = flippedText ;
51+ } ) ;
52+
53+ // Flip vertically
54+ document . getElementById ( "flipV" ) . addEventListener ( "click" , function ( ) {
55+ const t = inputText . value ;
56+ const lines = t . split ( '\n' ) ;
57+ const flippedLines = lines . reverse ( ) ;
58+ inputText . value = flippedLines . join ( '\n' ) ;
59+
60+ } ) ;
61+
62+ // Copy to clipboard
63+ document . getElementById ( "copyToClipboard" ) . addEventListener ( "click" , function ( ) {
64+ let t = document . getElementById ( "inputText" ) . value ;
65+ const button = document . getElementById ( "copyToClipboard" ) ;
66+ navigator . clipboard . writeText ( t )
67+ . then ( ( ) => {
68+ // Change the text and color of the button
69+ button . textContent = "Copied!" ; // Change the button text
70+ button . style . backgroundColor = "#4C50AF" ; // Change the button color (green)
71+
72+ } )
73+ . catch ( err => {
74+ // Change the text and color of the button
75+ button . textContent = "Failed to copy: " + err ; // Change the button text
76+ button . style . backgroundColor = "#AF4C50" ; // Change the button color (green)
77+
78+ } )
79+
80+ // After 1 second, reset the button text and color
81+ setTimeout ( ( ) => {
82+ button . textContent = "Copy to clipboard" ; // Reset text
83+ button . style . backgroundColor = "" ; // Reset the background color (default)
84+ } , 1000 ) ; // Wait for 1 seconds
85+ } ) ;
86+
87+ function getColumns ( t ) {
88+
89+ const lines = t . split ( '\n' ) . filter ( line => line . trim ( ) !== '' ) ;
90+ const maxLength = Math . max ( ...lines . map ( line => line . length ) ) ;
91+
92+ let columnsArray = [ ] ;
93+
94+ for ( let i = 0 ; i < maxLength ; i ++ ) {
95+ let column = lines . map ( line => line [ i ] || '.' ) . join ( '' ) ;
96+ columnsArray . push ( column ) ;
97+ }
98+ return columnsArray ;
99+ //const columns = columnsArray.join('\n');
100+ //return columns;
101+ }
0 commit comments