-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (52 loc) · 1.66 KB
/
index.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
const CS_CONSOLE_CHARACTER_LIMIT = 255
document.getElementById("char-limit").value = CS_CONSOLE_CHARACTER_LIMIT
function copyToClipboard (text) {
navigator.clipboard.writeText(text)
}
function renderCodeBlock (line) {
const commandList = document.getElementById("command-list")
const commandBlock = document.createElement('li')
commandBlock.classList.add('command-block')
const commandDiv = document.createElement('div')
commandDiv.style.flex = 1
commandDiv.appendChild(
document.createTextNode(line)
)
commandBlock.appendChild(commandDiv)
const copyButton = document.createElement('button')
copyButton.innerText = 'Copy'
copyButton.addEventListener('click', () => {
commandBlock.style['background-color'] = '#4CAF50'
copyToClipboard(line)
})
commandBlock.appendChild(
document.createElement('div').appendChild(copyButton)
)
commandList.appendChild(commandBlock)
}
function convertToOneLiners () {
const cfg = document.getElementById('cs-cfg').value
// Remove old code blocks
const commandList = document.getElementById("command-list")
commandList.innerHTML = ""
if (!cfg) {
return
}
const cfgLines = cfg
.replace(/\/\/.*/g, '') // remove comments
.split('\n')
.filter(line => line) // remove empty lines
.map(line => line.trim()) // remove spaces
const charLimit = document.getElementById("char-limit").value
let line = ""
for (let i = 0; i < cfgLines.length + 1; i++) {
if (!cfgLines[i] || (line + cfgLines[i] + "; ").length > charLimit) {
renderCodeBlock(line)
if (cfgLines[i]) {
line = cfgLines[i] + "; "
}
} else {
line += cfgLines[i] + "; "
}
}
}