-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
121 lines (103 loc) · 3.42 KB
/
index.html
File metadata and controls
121 lines (103 loc) · 3.42 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC&display=swap" rel="stylesheet">
<title>Simple Pad</title>
<style>
* {
box-sizing: border-box;
}
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
font-family: Noto Sans TC, sans-serif;
}
.container {
width: 100%;
height: 100%;
padding: 0;
margin: auto;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
#pad {
flex: 1;
flex-grow: 1;
width: 100%;
overflow: auto;
outline: none;
padding: 1em;
border: 3px solid #c3c3c3;
}
#status {
width: 100%;
padding: .25rem;
display: grid;
grid-template-columns: 150px 1fr minmax(min-content, 100px);
background-color: #e3e3e3;
}
#filename {
grid-column: 1;
}
#counter {
grid-column: 3;
white-space: pre;
text-align: end;
}
</style>
</head>
<body>
<div class="container">
<div id="pad" contenteditable="true"></div>
<div id="status">
<input id="filename" type="text" placeholder="pad.txt">
<div id="counter"></div>
</div>
</div>
<script>
const $ = (selector) => { return document.querySelector(selector); }
function downloadFile(fileName, fileContent, fileType) {
function createBlobUrl() {
return URL.createObjectURL(new Blob([fileContent], { type: fileType }));
}
let downloadElement = document.createElement("a");
let url = createBlobUrl();
downloadElement.download = fileName;
downloadElement.href = url;
downloadElement.style = 'position: absolute; top: 0; right: 0; left: 0; bottom: 0; visibility: hidden;';
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
URL.revokeObjectURL(url);
}
function updateCounter() {
$("#counter").textContent = `length: ${$("#pad").innerText.length}`;
}
$("#pad").addEventListener("input", function(e) {
let el = e.target;
if (el.innerText) localStorage.setItem("pad_content", el.innerText);
else localStorage.removeItem("pad_content");
updateCounter();
});
if (localStorage.getItem("pad_content")) {
$("#pad").innerText = localStorage.getItem("pad_content");
}
updateCounter();
document.body.addEventListener("keydown", (ev) => {
// Ctrl + S => save file
if (ev.ctrlKey && ev.key == "s") {
ev.preventDefault();
downloadFile($("#filename").value || "pad.txt", $("#pad").innerText, "text/plain");
}
});
</script>
</body>
</html>