-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
94 lines (74 loc) · 2.09 KB
/
app.js
File metadata and controls
94 lines (74 loc) · 2.09 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
function getUrlB64Content () {
return window.location.hash
.replace('#edit:', '#')
.substr(1)
.replace(/_/g, '/')
}
function getUrlContent () {
var binaryString = atob(getUrlB64Content())
if (binaryString) {
return pako.inflate(binaryString, {to: 'string'})
}
return ''
}
function loadEditor () {
var submitEl = document.querySelectorAll('#submit')[0]
editorPanelEl.style.display = 'block'
if (window.location.hash.substr(0, 6) === '#edit:') {
simplemde.value(getUrlContent())
}
submitEl.addEventListener('click', function () {
var binaryString = pako.deflate(simplemde.value(), {to: 'string'})
var b64 = window.btoa(binaryString)
var fullUrl = window.location.href.split('#')[0] + '#' + b64.replace(/\//g, '_')
window.location = fullUrl
})
}
function hideEditor () {
editorPanelEl.style.display = 'none'
}
function loadContent () {
contentPanelEl.style.display = 'block'
var content = document.querySelectorAll('.content')[0]
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
sanitize: true
})
content.innerHTML = marked(getUrlContent())
var editEl = document.querySelectorAll('.edit-link')[0]
editEl.addEventListener('click', function (e) {
e.preventDefault()
var fullUrl = window.location.href.split('#')[0] + '#edit:' + getUrlB64Content().replace(/\//g, '_')
window.location = fullUrl
})
}
function hideContent () {
contentPanelEl.style.display = 'none'
}
function hashChanged () {
if (window.location.hash.length < 2 || window.location.hash.substr(0, 6) === '#edit:') {
hideContent()
loadEditor()
} else {
loadContent()
hideEditor()
}
}
var simplemde = new SimpleMDE({
element: document.getElementById('editor'),
autofocus: true,
spellChecker: false,
hideIcons: ['guide'],
showIcons: ['code']
})
var editorPanelEl = document.querySelectorAll('.editor-panel')[0]
var contentPanelEl = document.querySelectorAll('.content-panel')[0]
if (window.location.hash) {
hashChanged()
} else {
hideContent()
loadEditor()
}
window.onhashchange = hashChanged