Skip to content

Commit 0808e6a

Browse files
authored
Add copy button to templates by @tanjz12
Copy button
2 parents 3bf11ef + 5c2aa3f commit 0808e6a

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

www/index.html.tmpl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
background-color: #f66;
1414
border-color: #a94442;
1515
}
16+
17+
.copied {
18+
color:#888;
19+
}
20+
21+
.fresh {
22+
color:#000;
23+
}
24+
25+
.notready {
26+
color:#000;
27+
}
1628
</style>
1729
</head>
1830
<body>
@@ -31,6 +43,7 @@
3143
<h2 style="margin-top: 0;" id="title"></h2>
3244
<div id="variables"></div>
3345
<textarea id="text" class="form-control" style="height: 400px;"></textarea>
46+
<button style="margin-top: 20px;" id="button" class="fresh" onclick="copyText()">Copy Text</button>
3447
</div>
3548
</div>
3649
</div>
@@ -75,6 +88,7 @@
7588
values[v] = $(this).val();
7689
// If the field is empty, show it as errored
7790
$(this).toggleClass('error', values[v] === "");
91+
refreshButton();
7892
updateText();
7993
})
8094
)
@@ -92,6 +106,7 @@
92106
if (!(v in values) || values[v] === "") {
93107
$('#text').bind('mousedown', preventSelection);
94108
$('#text').prop('readonly', true);
109+
buttonNotReady();
95110
}
96111

97112
// fuck JavaScript
@@ -119,6 +134,7 @@
119134

120135
$.get('/templates/' + encodeURI(page), function(newText) {
121136
text = newText;
137+
refreshButton();
122138
updateText();
123139
});
124140
}
@@ -142,10 +158,42 @@
142158
});
143159

144160
$(window).on('hashchange', updatePage);
161+
refreshButton();
145162
updateText();
146163
updatePage();
147164
});
165+
166+
const copy_button = document.getElementById("button");
167+
168+
window.copyText = () => {
169+
const copy_button = document.getElementById("button");
170+
// Copy text to clipboard onclick.
171+
if (copy_button.className == 'notready') {
172+
buttonNotReady();
173+
}
174+
else {
175+
const copyText = document.getElementById("text");
176+
const title = document.getElementById("title");
177+
navigator.clipboard.writeText(copyText.value);
178+
179+
// Button text change from "Copy Text" to "Copied", and text turns grey.
180+
copy_button.innerHTML = 'Copied';
181+
copy_button.className = 'copied';
182+
}
183+
}
184+
185+
window.refreshButton = () => {
186+
copy_button.innerHTML = 'Copy Text';
187+
copy_button.className = 'fresh';
188+
}
189+
190+
window.buttonNotReady = () => {
191+
copy_button.innerHTML = 'Not all fields are filled';
192+
copy_button.className = 'notready';
193+
}
148194
})();
195+
196+
149197
</script>
150198
</body>
151199
</html>

0 commit comments

Comments
 (0)