-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.html
131 lines (116 loc) · 3.29 KB
/
wiki.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
<body>
<script src="https://invisible-college.github.io/diffsync/diffsync.js"></script>
<script src="https://invisible.college/js/marked.min.js"></script>
<link rel="stylesheet" href="https://invisible.college/css/github-markdown.css">
<script>
var output = document.createElement('div')
output.className = 'pad'
output.style.maxWidth = '900px'
document.body.append(output)
var bottom_pad = document.createElement('div')
bottom_pad.style.height = '50vh'
bottom_pad.style.display = 'none'
document.body.append(bottom_pad)
var t = document.createElement('textarea')
t.style.position = 'fixed'
t.style.bottom = '0px'
t.style.right = '0px'
t.style.width = '100%'
t.style.height = '50vh'
t.style.display = 'none'
t.style.fontSize = '15px'
t.style.fontFamily = 'helvetica, monaco, lucida grande, avenir'
document.body.append(t)
var edit = document.createElement('div')
edit.style.position = 'fixed'
edit.style.bottom = '0px'
edit.style.right = '0px'
edit.style.padding = '30px'
edit.style.cursor = 'pointer'
edit.style.textDecoration = 'underline'
edit.style.backgroundColor = 'rgba(255, 255, 255, .5)'
edit.onclick = toggle_editor
edit.innerText = 'edit'
document.body.append(edit)
var timer
var render_delay = 100
function update_markdown_later() {
if (timer) clearTimeout(timer)
timer = setTimeout(update_markdown, render_delay)
}
function update_markdown() {
timer = null
output.innerHTML = marked(t.value, {sanitize: !is_safe})
document.body.className = 'nopad'
}
update_markdown()
var is_safe = window.location.href.match(/^https?:\/\/wiki\./)
var channel = is_safe ? '/wiki' + window.location.pathname : window.location.pathname
var ds = diffsync.create_client({
ws_url : 'wss://invisible.college:60607',
channel : channel,
get_text : function () {
return t.value
},
get_range : function () {
return [t.selectionStart, t.selectionEnd]
},
on_text : function (text, range) {
t.value = text
if (t.style.display != 'none') t.setSelectionRange(range[0], range[1])
update_markdown_later()
},
on_range : null
})
var old_text
t.onkeyup = function () {
if (old_text === t.value) return
old_text = t.value
ds.on_change()
update_markdown_later()
}
t.onpaste = function () {
setTimeout(function () {
ds.on_change()
update_markdown_later()
}, 0)
}
var vert = true, editing = false
function render () {
t.style.display = editing ? null : 'none'
bottom_pad.style.display = (editing && vert) ? null : 'none'
if (vert) {
t.style.width = '100vw'
t.style.height = '50vh'
output.style.width = null
} else {
t.style.width = '45vw'
t.style.height = '100vh'
output.style.width = editing ? '55vw' : null
}
}
function toggle_editor () {
editing = !editing
render()
if (editing) setTimeout(function () {t.focus()}, 10)
}
document.body.onkeydown = function (e) {
var mods = 0
for (k in {ctrlKey:1, shiftKey:1, altKey:1, metaKey:1})
if (e[k]) mods += 1
if (mods >= 2 && e.keyCode == 32) {
e.stopPropagation()
toggle_editor()
}
}
window.onresize = function () {
var w = window.innerWidth, h = window.innerHeight
if (w < 1200 !== vert) {
vert = !vert
render()
}
}
onresize()
render()
</script>
</body>