-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
171 lines (153 loc) · 4.39 KB
/
index.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Krushn's Notepad</title>
<!-- pwa manifest -->
<link rel="manifest" href="manifest.json" />
<meta name="theme-color" content="#ffffff" />
<!-- icons -->
<link
rel="shortcut icon"
href="icons/mipmap-xxxhdpi/notepad.png"
type="image/png" />
<link rel="apple-touch-icon" href="icons/mipmap-xxxhdpi/notepad.png" />
<style>
@import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300&display=swap');
body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: #fff;
margin: 0;
}
#header {
background-color: #f5f5f5;
border-bottom: 1px solid #e5e5e5;
padding-bottom: 10px;
padding-top: 10px;
padding-left: 24px;
padding-right: 24px;
display: flex;
flex-direction: row;
max-width: 100vw;
}
#header h1 {
margin: 0;
font-size: 18px;
line-height: 1.33333333;
display: inline-block;
flex-grow: 1;
}
#header #status {
text-align: right;
margin-top: 0px;
margin-right: 10px;
display: inline-block;
}
#notebody {
box-sizing: border-box;
width: 100%;
height: calc(100vh - 45px);
overflow-y: scroll;
padding: 24px;
max-width: 100vw;
font-family: 'Roboto Mono', monospace;
outline: none;
}
</style>
</head>
<body>
<main id="app">
<div id="header">
<h1>Krushn's Notepad</h1>
<span id="status">
<span v-if="savingNote">saving...</span>
{{characterCount}}
</span>
</div>
<div
id="notebody"
contenteditable
v-html="note"
@input.sync="saveNote"></div>
</main>
<!-- vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- localforage -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/localforage.min.js"></script>
<script>
function debounce(fn, delay) {
var timeoutID = null
return function () {
clearTimeout(timeoutID)
var args = arguments
var that = this
timeoutID = setTimeout(function () {
fn.apply(that, args)
}, delay)
}
}
var app = new Vue({
el: '#app',
data: {
note: '',
savedNote: '',
savingNote: false,
},
mounted: async function () {
this.savedNote = await localforage.getItem('note')
this.note = this.savedNote
fetch('/api/note')
.then((response) => response.json())
.then((data) => {
const serverNote = data.body
if (serverNote !== this.savedNote) {
this.note = serverNote
this.savedNote = serverNote
localforage.setItem('note', serverNote)
}
})
},
methods: {
saveNote: debounce(async function (e) {
this.savingNote = true
await localforage.setItem('note', e.target.innerHTML)
fetch('/api/note', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ body: e.target.innerHTML }),
})
this.savedNote = e.target.innerHTML
this.savingNote = false
}, 500),
},
computed: {
characterCount: function () {
return this.savedNote.length
},
},
})
</script>
<!-- pwa -->
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
.register('/sw.js')
.then((registration) => {
console.log('SW registered: ', registration)
})
.catch((registrationError) => {
console.log('SW registration failed: ', registrationError)
})
})
}
</script>
</body>
</html>