Last active
September 7, 2019 23:55
-
-
Save mizchi/217498744594975e8b94c37d096a561d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "regenerator-runtime/runtime"; | |
const tx = document.createElement("textarea") as HTMLTextAreaElement; | |
async function writeFile(handler: any, text: string): Promise<void> { | |
const writer = await handler.createWriter(); | |
await writer.truncate(0); | |
await writer.write(0, text); | |
await writer.close(); | |
} | |
// mount open button | |
const btn = document.createElement("button"); | |
btn.textContent = "Open file and edit!"; | |
document.body.appendChild(btn); | |
btn.addEventListener("click", async (ev: any) => { | |
let handler: any = null; | |
try { | |
// @ts-ignore | |
handler = await window.chooseFileSystemEntries({ | |
type: "saveFile", | |
accepts: [ | |
{ | |
description: "Text file", | |
extensions: ["txt"], | |
mimeTypes: ["text/plain"] | |
} | |
] | |
}); | |
} catch (err) { | |
alert("Please select file"); | |
return; | |
} | |
// @ts-ignore | |
tx.style = "width: 80vw; height: 80vh"; | |
document.body.appendChild(tx); | |
tx.addEventListener("change", async (ev: any) => { | |
await writeFile(handler, ev.target.value); | |
}); | |
btn.remove(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment