-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
101 lines (84 loc) · 3.08 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
97
98
99
100
101
// transpose
document.getElementById("transpose").addEventListener("click", function() {
console.log('transpose');
const t = inputText.value;
const columns = getColumns(t);
inputText.value = columns.join('\n');
});
// Rotate clockwise
document.getElementById("rotateClockwise").addEventListener("click", function() {
console.log('transpose');
const t = inputText.value;
const lines = t.split('\n').filter(line => line.trim() !== '');
const columns = getColumns(t);
console.log(columns[0])
let res = []
for (let col = 0; col < columns.length; col++){
newLine = columns[col].split('').reverse().join('');
res.push(newLine);
}
inputText.value = res.join('\n');
});
// Rotate counterclockwise
document.getElementById("rotateCounterclockwise").addEventListener("click", function() {
console.log('transpose');
const t = inputText.value;
const lines = t.split('\n').filter(line => line.trim() !== '');
const columns = getColumns(t);
console.log(columns[0])
let res = []
for (let col = columns.length - 1; col >= 0; col--){
newLine = columns[col];
res.push(newLine);
}
inputText.value = res.join('\n');
});
// Flip horizontally
document.getElementById("flipH").addEventListener("click", function() {
console.log('flipH!');
const t = inputText.value;
const lines = t.split('\n');
const flippedLines = lines.map(line => {return line.split('').reverse().join('')}); // flip each line
const flippedText = flippedLines.join('\n')
inputText.value = flippedText;
});
// Flip vertically
document.getElementById("flipV").addEventListener("click", function() {
const t = inputText.value;
const lines = t.split('\n');
const flippedLines = lines.reverse();
inputText.value = flippedLines.join('\n');
});
// Copy to clipboard
document.getElementById("copyToClipboard").addEventListener("click", function() {
let t = document.getElementById("inputText").value;
const button = document.getElementById("copyToClipboard");
navigator.clipboard.writeText(t)
.then(() => {
// Change the text and color of the button
button.textContent = "Copied!"; // Change the button text
button.style.backgroundColor = "#4C50AF"; // Change the button color (green)
})
.catch(err => {
// Change the text and color of the button
button.textContent = "Failed to copy: " + err; // Change the button text
button.style.backgroundColor = "#AF4C50"; // Change the button color (green)
})
// After 1 second, reset the button text and color
setTimeout(() => {
button.textContent = "Copy to clipboard"; // Reset text
button.style.backgroundColor = ""; // Reset the background color (default)
}, 1000); // Wait for 1 seconds
});
function getColumns(t){
const lines = t.split('\n').filter(line => line.trim() !== '');
const maxLength = Math.max(...lines.map(line => line.length));
let columnsArray = [];
for (let i = 0; i < maxLength; i++){
let column = lines.map(line => line[i] || '.').join('');
columnsArray.push(column);
}
return columnsArray;
//const columns = columnsArray.join('\n');
//return columns;
}