forked from jerryshueh/simple-noter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
95 lines (81 loc) · 3.98 KB
/
index.html
File metadata and controls
95 lines (81 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- Taken from https://gist.github.com/SimonPadbury/fc55c054c0480550c3be592111778763 -->
<link rel="stylesheet" href="./styles/stickystyle.css">
<link href="https://fonts.googleapis.com/css?family=Indie+Flower&display=swap" rel="stylesheet">
<title>Simple Noter</title>
</head>
<body>
<!-- This is a comment -->
<h1>simple-note</h1>
<p><sub>To make notes, just type in the space below. Press Ctrl + x to save, or Ctrl + s to save on disk!</sub></p>
<p><sub>
Font is Indie Flower, taken from Google Fonts.
<br>
Sticky note CSS is taken from Github user <a href="https://gist.github.com/SimonPadbury">SimonPadbury's</a> <a href="https://gist.github.com/SimonPadbury/fc55c054c0480550c3be592111778763">gist.</a>
</sub></p>
<br>
<div id="content"></div>
<script src="https://unpkg.com/filer/dist/filer.min.js"></script>
<script src="https://unpkg.com/hotkeys-js/dist/hotkeys.min.js"></script>
<script src="https://unpkg.com/file-saver@2.0.2/src/FileSaver.js"></script>
<script>
const fs = new Filer.FileSystem();
var nString = "Nothing is saved yet, so just type here!"; //init
var contentDiv = document.getElementById("content");
var noteDiv = document.createElement("div");
noteDiv.setAttribute("contenteditable", true);
noteDiv.id = "note";
noteDiv.className = "sticky";
contentDiv.appendChild(noteDiv);
hotkeys.filter = function(event) {
var tagName = (event.target || event.srcElement).tagName;
return !(tagName.isContentEditable || tagName == "INPUT" || tagName == "SELECT" || tagName == "TEXTAREA");
}
window.addEventListener("DOMContentLoaded", (event) => {
console.log("DOM fully loaded and parsed");
fs.readFile("/note", 'utf8', function(err, data) {
if(err) {
console.log("Document not found/ could not be read. New file will be created upon save.");
noteDiv.setAttribute("aria-placeholder", nString);
}
if(data) {
noteDiv.textContent = data;
console.log("Document loaded.");
}
});
});
hotkeys("ctrl+x,ctrl+s", function(event, handler) {
event.preventDefault();
const newNString = document.getElementById("note").textContent;
switch (handler.key) {
case "ctrl+x":
saveNote(newNString);
alert("Note saved!");
break;
case "ctrl+s":
saveOnDisk(newNString)
}
});
function saveNote(content) {
fs.writeFile("/note", content, function (err) {
if (err) throw err;
else console.log("Document saved: " + content);
});
}
function saveOnDisk(content) {
if (!content) alert("Your note is empty. Type something before saving it on disk!");
else {
const fileName = prompt("Please enter the name of the file where you want to store your note", "simple-note.txt");
// If fileName is empty or the user clicks on "cancel", nothing will be done
if (fileName) {
const file = new File([content], fileName, {type: "text/plain;charset=utf-8"});
saveAs(file);
}
}
}
</script>
</body>
</html>