-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
78 lines (73 loc) · 2.71 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Splitter Thing!</title>
<style>
.box{
margin: 15px 15px 15px 15px;
}
.copier{
margin-left: 15px;
width: 100px;
}
#original {
margin: 15px;
}
</style>
</head>
<body>
<div>Splittable Text:</div>
<textarea id="original" cols="80"></textarea>
<br>
<label>Number of Characters: <input id="chars" type="number" value="280" min="20" max="2000"></label>
<br>
<button id="split" onclick="split()">Split!</button>
<div id="boxes">
</div>
<script>
function make_box(contents){
let new_box=document.createElement('div')
new_box.className="box"
let box_container=document.getElementById('boxes');
let cntnts= contents.trim()
new_box_contents=`<button class="copier">Copy</button><span class="contents">${cntnts}</span>`
new_box.innerHTML=new_box_contents;
box_container.appendChild(new_box)
}
function split(){
let box_container=document.getElementById('boxes');
box_container.innerHTML="";
let sauce=document.getElementById('original').value;
let max_box_chars=parseInt(document.getElementById('chars').value);
if (max_box_chars==NaN) {
return;
}
let sauce_size=sauce.length;
let idx=0
for(;idx<sauce_size;){
let start=idx;
let curboxchars=0
// fill a box
while(idx<sauce_size && curboxchars<max_box_chars){
idx++;
curboxchars++;
}
let end=idx;
if(idx<sauce_size){ // if we're not done with the text...
// make the cut sensible! if we ain't at the end of a word, roll back to closest space
while(curboxchars>0 && sauce[idx]!=' '){
idx--;
curboxchars--;
}
if(curboxchars==0){ // no spaces in the WHOLE box? Caveman break word!
idx=end;
}
}
make_box(sauce.substring(start,idx+1))//substring includes start but excludes end!
}
document.querySelectorAll(".copier").forEach(function(c){c.onclick=function(e){navigator.clipboard.writeText(e.target.parentElement.querySelector(".contents").innerText)}})
}
</script>
</body>
</html>