image saving

This commit is contained in:
nate smith 2024-03-09 16:01:36 -08:00
parent dfc1ce2589
commit 2d6a417028
4 changed files with 55 additions and 11 deletions

20
assets/html2canvas.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -326,17 +326,17 @@ class ThemeToggler extends HTMLAnchorElement {
class PoemSaver extends HTMLFormElement { class PoemSaver extends HTMLFormElement {
// TODO oops, you can't copy an image on ff; disable copy if image selected
connectedCallback() { connectedCallback() {
this.addEventListener("submit", (e) => { this.addEventListener("submit", (e) => {
e.preventDefault(); e.preventDefault();
const includeSources = this.querySelector("input[name=sources]").checked; const fd = new FormData(e.target);
const saveType = this.querySelector("input[name=type]").value; const includeSources = fd.get("sources") == "on";
const saveType = fd.get("type");
const text = this.toText(includeSources); const text = this.toText(includeSources);
if (e.submitter.innerText == "copy") { if (e.submitter.innerText == "copy") {
if (saveType == "text") { if (saveType == "text") {
this.copyText(text); this.copyText(text);
} else {
this.copyImage(text);
} }
} else { } else {
if (saveType == "text") { if (saveType == "text") {
@ -365,15 +365,14 @@ class PoemSaver extends HTMLFormElement {
navigator.clipboard.writeText(text); navigator.clipboard.writeText(text);
} }
copyImage(text) { fname() {
// TODO return `trunkless-poem-${Math.trunc(Date.now()/1000)}`
} }
saveText(text) { saveText(text) {
const blob = new Blob([text], {type: "text/plain"}); const blob = new Blob([text], {type: "text/plain"});
const fname = `trunkless-poem-${Math.trunc(Date.now()/1000)}.txt`
const dlink = document.createElement("a"); const dlink = document.createElement("a");
dlink.download = fname; dlink.download = this.fname() + ".txt";
dlink.href = window.URL.createObjectURL(blob); dlink.href = window.URL.createObjectURL(blob);
dlink.addEventListener("click", (e)=>{e.target.remove()}); dlink.addEventListener("click", (e)=>{e.target.remove()});
dlink.style.display = "none"; dlink.style.display = "none";
@ -382,8 +381,31 @@ class PoemSaver extends HTMLFormElement {
} }
saveImage(text) { saveImage(text) {
// TODO const toSave = document.createElement("p");
console.log(text); toSave.setAttribute("id", "toSave");
toSave.style.color = "black";
toSave.style["font-size"] = "75%";
toSave.style["max-width"] = "500px";
toSave.innerText = text;
$("body").append(toSave);
html2canvas(document.querySelector("#toSave")).then((canvas) => {
canvas.toBlob((blob) => {
// can't use this because ff doesn't have support by default for
// clipboard.write()
// let data = [new ClipboardItem({ [blob.type]: blob })];
// navigator.clipboard.write(data).then(() => {
// copyImgButton.classList.add("rainbow");
// }, console.log);
const downloadUrl = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = this.fname() + ".png";
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(downloadUrl);
toSave.remove();
});
});
} }
} }

View File

@ -51,6 +51,7 @@ func main() {
r.StaticFile("/bg_light.gif", "./assets/bg_light.gif") r.StaticFile("/bg_light.gif", "./assets/bg_light.gif")
r.StaticFile("/bg_dark.gif", "./assets/bg_dark.gif") r.StaticFile("/bg_dark.gif", "./assets/bg_dark.gif")
r.StaticFile("/main.js", "./assets/main.js") r.StaticFile("/main.js", "./assets/main.js")
r.StaticFile("/html2canvas.min.js", "./assets/html2canvas.min.js")
randMax := big.NewInt(maxID) randMax := big.NewInt(maxID)

View File

@ -50,6 +50,7 @@ span.linetext {
<span class="linetext"></span> <span class="linetext"></span>
<p is="source-text"></p> <p is="source-text"></p>
</template> </template>
<script src="/html2canvas.min.js"></script>
<script src="/main.js"></script> <script src="/main.js"></script>
<h1 style="display:block;margin:0">Trunkless</h1> <h1 style="display:block;margin:0">Trunkless</h1>
<p style="margin-top:0;font-size:80%"> <p style="margin-top:0;font-size:80%">
@ -60,7 +61,7 @@ span.linetext {
<button is="poem-resetter"></button> <button is="poem-resetter"></button>
<form is="poem-saver" style="border: 1px solid grey; display:inline; padding:4px;"> <form is="poem-saver" style="border: 1px solid grey; display:inline; padding:4px;">
<input name="type" value="text" type="radio" checked="true"/>as text <input name="type" value="text" type="radio" checked />as text
<input name="type" value="image" type="radio"/>as image <input name="type" value="image" type="radio"/>as image
<input name="sources" type="checkbox"/>include sources <input name="sources" type="checkbox"/>include sources
<button type="submit">copy</button> <button type="submit">copy</button>