Skip to content

Commit

Permalink
Create index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
eyaler authored Jun 23, 2024
1 parent 59322b4 commit 35e6db5
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,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']
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>

0 comments on commit 35e6db5

Please sign in to comment.