Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active September 7, 2019 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/217498744594975e8b94c37d096a561d to your computer and use it in GitHub Desktop.
Save mizchi/217498744594975e8b94c37d096a561d to your computer and use it in GitHub Desktop.
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