Skip to content

Instantly share code, notes, and snippets.

@dirkk0
Last active March 27, 2023 08:01
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 dirkk0/583c9633ea817f6a028939c4dc6dd729 to your computer and use it in GitHub Desktop.
Save dirkk0/583c9633ea817f6a028939c4dc6dd729 to your computer and use it in GitHub Desktop.
webview-node text editor example
<html>
<style>
#text {
width: 100%;
height: 90%;
}
</style>
<body>
<button onclick="doCmd('load')">load</button>
<button onclick="doCmd('save', getText());">save</button>
<button onclick="doCmd('quit');">quit </button>
<div id="text" contenteditable>type something here ...</div>
</body>
<script>
'use strict';
function setText(t) {
document.querySelector("#text").innerText = t
}
function getText() {
return document.querySelector("#text").innerText
}
doCmd("load")
</script>
</html>
'use strict';
const { Webview } = require('webview-nodejs');
const fs = require("fs");
let w = null
let txt = ""
let path = "texteditor.txt"
function main() {
let html = fs.readFileSync("texteditor.html", { encoding: "utf8", flag: "r" });
w = new Webview(true);
w.title("Minimal Text Editor");
w.size(600, 400, 3);
w.html(html);
w.bind("doCmd", (w, c, t) => {
console.log("cmd:", c);
if (c == "quit") w.terminate();
if (c == "load") {
txt = fs.readFileSync(path, { encoding: "utf8", flag: "r" });
txt = JSON.stringify(txt)
console.log(txt)
w.eval(`setText(${txt})`);
}
if (c == "save") fs.writeFileSync(path, t, { encoding: "utf8" });
});
w.show();
};
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment