-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
78 lines (77 loc) · 2.77 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PAD</title>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.min.js"></script>
<!-- Uncomment to use Radisk/IndexedDB instead of localStorage:
<script src="https://cdn.jsdelivr.net/npm/gun/lib/radix.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/lib/radisk.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/lib/store.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/lib/rindexed.min.js"></script>
-->
<style>
textarea {
border: none;
font-size: 1rem;
inset: 0;
margin: 0;
outline: none;
padding: 1rem;
position: absolute;
resize: none;
}
textarea:read-only {
background-color: rgba(255, 0, 0, .15);
}
</style>
</head>
<body>
<textarea id="textarea" dir="auto" placeholder="Type here."></textarea>
<script>
const peers = ['https://gun-manhattan.herokuapp.com/gun', 'https://try.axe.eco/gun', 'https://test.era.eco/gun', 'https://peer.wallie.io/gun']
const gun = Gun(peers)
const pad = gun.get(location.hash.slice(1) || 'default')
if (!navigator.onLine)
textarea.readOnly = true
addEventListener('offline', () => textarea.readOnly = true)
addEventListener('online', () => {
gun.opt(peers)
pad.get('heartbeat').put('heartbeat')
textarea.removeAttribute('readonly')
})
textarea.addEventListener('input', () => pad.put({content: textarea.value}))
pad.on(data => {
const text = textarea.value
const new_text = data.content
if (new_text != text) {
const start = textarea.selectionStart
const end = textarea.selectionEnd
textarea.value = new_text
const delta = new_text.length - text.length
if (start < text.length && text.slice(start) == new_text.slice(start + delta))
textarea.setSelectionRange(start + delta, end + delta)
else if (text.slice(0, end) == new_text.slice(0, end))
textarea.setSelectionRange(start, end)
else if (end > start)
if (text.slice(0, start) == new_text.slice(0, start)) {
if (text.slice(end) == new_text.slice(end + delta))
textarea.setSelectionRange(start, end + delta)
else {
let i = start
while (text[i] == new_text[i])
i++
textarea.setSelectionRange(start, i)
}
} else if (text.slice(end) == new_text.slice(end + delta)) {
let i = end
while (text[i - 1] == new_text[i - 1 + delta])
i--
textarea.setSelectionRange(i + delta, end + delta)
}
}
})
</script>
</body>
</html>