/
wiki.coffee
137 lines (119 loc) · 4.18 KB
/
wiki.coffee
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
showdown = new Showdown.converter()
currentPage = null
pageHistoryModified = false
pageHistory = null
window.save = (editor) ->
data = currentPage
$("#page").html showdown.makeHtml data
document.title = data.substr 0, data.indexOf("\n")
$("#page a").click ->
if this.href and this.href.indexOf("static/") < 0
event.preventDefault()
location.hash = "##{this.href.replace(location.href.replace(/#.*$/g, ''), '')}"
$.post "savePage.do?" + location.hash.replace(/^#/, ''), data
window.addImg = (editor) ->
$("#fileEl").change (e) ->
uploadFiles this.files, editor
$("#fileEl").click()
window.addLink = (editor) ->
url = prompt("Please enter the Link's URL:")
editor.getDoc().replaceSelection "[#{editor.getDoc().getSelection() or "Link name"}](#{url})", true
editor.focus()
window.duplicate = (from) ->
name = prompt "Please enter the duplicate's name:"
$.get "duplicate.do?from=#{from}&to=#{name}", ->
location.hash = "##{name}"
window.viewHistory = (editor) ->
uploadFiles = (files, cm) ->
for file in files
fr = new FileReader()
fr.onload = ((file) ->
return (event) ->
if event.loaded >= event.total
xhr = new XMLHttpRequest()
xhr.open "POST", "upload.do?" + file.name.replace(/[^a-zA-Z0-9_.-]/g, '-')
xhr.upload.addEventListener "progress", (e) ->
if e.lengthComputable
$("div.CodeMirror-gutter.CodeMirror-linenumbers").addClass("loading").css "height: #{Math.round(e.loaded * 100 / e.total)}%;"
, false
xhr.onreadystatechange = (e) ->
if xhr.readyState is 4 and xhr.status is 200
$("div.CodeMirror-gutter.CodeMirror-linenumbers").removeClass "loading"
cm.getDoc().replaceSelection "![#{cm.getDoc().getSelection() or file.name}](#{xhr.responseText.replace(/^\s+|\s+$/g, '')})", true
cm.focus()
xhr.setRequestHeader "Content-Type", file.type
xhr.send event.target.result.substr(event.target.result.indexOf(",") + 1)
)(file)
fr.readAsDataURL file
window.editPage = ->
form = document.createElement("form")
ta = document.createElement("textarea")
ta.id = "code"
$(form).append "<div class='toolbar'><button class='save'></button><button class='addImg'></button><button class='addLink'></button><button class='viewHistory'></button></div>"
form.appendChild ta
$("#page").html form.outerHTML
$("#code").val currentPage
editor = CodeMirror.fromTextArea $("#code")[0],
mode: 'markdown'
lineNumbers: true
autofocus: true
lineWrapping: true
theme: "default"
extraKeys: "Enter": "newlineAndIndentContinueMarkdownList"
editor.on "change", (doc, changed) ->
currentPage = doc.getValue()
editor.on "drop", (cm, event) ->
event.preventDefault()
uploadFiles event.dataTransfer.files, cm
$(".toolbar button").click (e) ->
e.preventDefault()
window[this.className](editor, editor)
updateBreadcrumbs = ->
i = 0
# clear previous breadcrumbs
$("#breadcrumbs").html ""
while i < pageHistory.length
entry = pageHistory[i]
link = document.createElement "a"
link.innerHTML = entry[1]
link.href = entry[0]
link.onclick = ((id) ->
return (event) ->
pageHistory = pageHistory.slice 0, id
pageHistoryModified = true
)(i)
$("#breadcrumbs").append link
i++
hashChange = ->
$("body").addClass "loading"
path = location.hash.replace /^#/, ''
frags = path.split /\//
$.get "getPage.do?#{path}", (data) ->
$("body").removeClass "loading"
title = data.substr 0, data.indexOf("\n")
if not pageHistory
pageHistory = []
else if not pageHistoryModified
if not pageHistory.some((el) -> el[0] is currentHash)
pageHistory.push [""+currentHash, ""+document.title]
pageHistoryModified = false
currentPage = data
$("#page").html showdown.makeHtml data
do updateBreadcrumbs
document.title = title
window.currentHash = location.hash
$("#page a").click (event) ->
if this.href and this.href.indexOf("static/") < 0
event.preventDefault()
location.hash = "##{this.href.replace(location.href.replace(/#.*$/g, ''), '')}"
input = document.createElement("input")
input.type = "file"
input.id = "fileEl"
input.multiple = true
input.accept = "image/*"
document.body.appendChild input
input.style.display = "none"
setInterval ->
if window.currentHash != location.hash
do hashChange
, 250