-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
312 lines (266 loc) · 9.48 KB
/
index.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const marked = require('marked')
const escape = require('escape-html')
const formsEl = document.getElementById('forms-container')
const submitBtn = document.getElementById('submit')
let messageEl = document.getElementById('message')
let formCount = 0 // the number of file forms that are currently rendered
// render one form by default
appendForm()
detectUserAgent()
submitBtn.addEventListener('click', createGist)
document.getElementById('add-file').addEventListener('click', appendForm)
async function createGist () {
var fileForms = document.querySelectorAll('form')
var paths = []
// basic check to see if files have content
// TODO: this is not correct - imagine if user has 2 forms, content in one but
// not the first.
if (fileForms.length && fileForms[0].content.value) {
var archive = await DatArchive.create({
title: document.querySelector('input[name="title"]').value || '',
description: document.querySelector('input[name="description"]').value || ''
})
// Figure out which files need to be added
var fileForms = document.querySelectorAll('form')
for (var i = 0; i < fileForms.length; i++) {
var form = fileForms[i]
var path = form.path.value
var content = form.content.value
if (path && content) {
await archive.writeFile(path, content)
paths.push(path)
}
}
await archive.commit()
// if the user didn't add an index.html, generate a preview page
if (paths.indexOf('index.html') === -1) await createPreviewPage(archive)
window.location = archive.url
} else renderMessage('Try adding some files!', 'info', true)
}
async function createPreviewPage (archive) {
let previewHTML, filesListHTML, filesListItemsHTML = ''
// files are only ever added to top-level directory, so recursive readdir
// is not necessary
const files = await archive.readdir('/')
const info = await archive.getInfo()
const styles = `
<style>
*{box-sizing:border-box;}
html{padding:50px 10px;}
body{
font-size:16px;
margin:auto;
max-width:700px;
font-family:BlinkMacSystemFont,'Helvetica Neue',sans-serif;
line-height:1.4;
color:#111;
}
a{color:#0b51de;}
ul{list-style:none;padding-left:0;}
.file-preview{
font-size:.9rem;
width:100%;
position:relative;
display:block;
margin-bottom:1.5rem;
}
.preview {
border-radius:3px 3px 0 0;
border:1px solid #9e9e9e;
padding:.3rem .4rem;
font-family:Consolas, Monaco, 'Lucida Console', monospace;
font-size:.8rem;
margin:.5rem 0 1rem 0;
max-height:200px;
overflow-x:hidden;
overflow-y:hidden;
white-space:pre-wrap;
}
.preview.markdown{
font-family:BlinkMacSystemFont,'Helvetica Neue',sans-serif;
}
.preview.more-lines{padding-bottom:24px;max-height:235px;}
.preview.more-lines.expanded{max-height:none;}
.preview-toggle-expand{
position:absolute;
bottom:0;
left:0;
padding:.1rem 0.4rem;
font-size:.8rem;
color:#9e9e9e;
border:1px solid #9e9e9e;
border-radius:0 0 3px 3px;
width:100%;
background:#f4f6ff;
cursor:pointer;
text-decoration:none;
font-family:Consolas,Monaco,'Lucida Console', monospace;
}
.preview-toggle-expand.hidden{visibility:hidden;}
.preview-toggle-expand:hover{color:#525252;}
</style>
`
const js = `
<script>
var toggles = document.querySelectorAll('.preview-toggle-expand')
toggles.forEach(function (toggle) {
toggle.addEventListener('click', toggleExpand)
})
function toggleExpand (e) {
var previewId = e.target.dataset.preview
var preview = document.getElementById(previewId)
var collapseSelector = '[data-preview="' + previewId + '"].collapse'
var collapseBtn = document.querySelector(collapseSelector)
var expandSelector = '[data-preview="' + previewId + '"].expand'
var expandBtn = document.querySelector(expandSelector)
collapseBtn.classList.toggle('hidden')
expandBtn.classList.toggle('hidden')
preview.classList.toggle('expanded')
}
</script>
`
for (let path of files) {
// Don't include the dat.json in share page
if (path !== 'dat.json') {
filesListItemsHTML += await generateFilePreview(archive, path)
}
}
filesListHTML = `<ul>${filesListItemsHTML}</ul>`
previewHTML = `
<html lang="en">
<head>
<meta charset="utf-8">
${styles}
</head>
<body>
<h1>${info.title || '<em>Untitled</em>'}</h1>
<p>${info.description || ''}</p>
<main>${filesListHTML}</main>
${js}
</body>
</html>
`
await archive.writeFile('/index.html', previewHTML)
return
// TODOrender a share thing if is owner
}
async function generateFilePreview (archive, path) {
const file = await archive.readFile(path)
const isMarkdown = path.endsWith('.md')
let previewNote = ''
let lines = file.split('\n')
if (lines.length > 10) {
previewNote = `
<span data-preview="preview-${path.replace('.', '')}" class="preview-toggle-expand expand">
+ ${lines.length - 10} more lines...
</span>
<span data-preview="preview-${path.replace('.', '')}" class="preview-toggle-expand collapse hidden">
- Less
</span>`
}
return `
<li class="file-preview">
<a href=${path}>${path}</a>
<pre id="preview-${path}" class="preview ${previewNote ? 'more-lines' : ''} ${isMarkdown ? 'markdown' : ''}">${isMarkdown ? marked(file) : escape(file.trim())}</pre>
${previewNote}
</li>`
}
function appendForm () {
formCount += 1
let form = document.createElement('form')
form.id = 'add-file-form-' + formCount
form.classList.add('file-form')
var removeBtn = ''
if (formCount !== 1) {
var removeBtn = `
<button type="button" class="remove" data-form=${form.id}>
<span>Remove</span>
<img src="/img/trash.png"/>
</button>`
}
var textarea = `<textarea name="content" data-form=${form.id}></textarea>`
var markdownPreview = `<p id="markdown-preview-${form.id}" class="markdown-preview hidden"></p>`
const formContent = `
<!-- TODO allow user to do any kind of file -->
<div class="header">
<input autofocus data-form=${form.id} name="path" placeholder="Filename including extension"/>
<button type="button" id="preview-markdown-btn-${form.id}" data-form=${form.id} class="preview-markdown-btn markdown"></button>
<button type="button" id="edit-btn-${form.id}" data-form=${form.id} class="edit-btn"></button>
${removeBtn}
</div>
${textarea}
${markdownPreview}
`
form.innerHTML = formContent
formsEl.appendChild(form)
var previewMarkdownBtn = document.getElementById(`preview-markdown-btn-${form.id}`)
var editBtn = document.getElementById(`edit-btn-${form.id}`)
previewMarkdownBtn.addEventListener('click', previewMarkdown)
editBtn.addEventListener('click', showTextarea)
try {
var pathInput = document.querySelector(`input[data-form=${form.id}`)
var removeBtn = document.querySelector(`button.remove[data-form=${form.id}`)
pathInput.addEventListener('keyup', function (e) {
if (e.target.value.endsWith('.md')) {
renderPreviewMarkdownBtn(form.id)
} else {
editBtn.innerHTML = ''
previewMarkdownBtn.innerHTML = ''
}
})
removeBtn.addEventListener('click', removeForm)
} catch (_) {}
}
function removeForm (e) {
const form = document.getElementById(e.target.parentElement.dataset.form)
formsEl.removeChild(form)
}
function renderMessage (msg, type, timeout) {
messageEl.innerText = msg
if (type) messageEl.classList.add(type)
if (timeout) {
window.setTimeout(function () {
messageEl.classList.remove('error')
messageEl.innerText = ''
}, 4000)
}
}
function renderPreviewMarkdownBtn (id) {
var previewMarkdownBtn = document.getElementById(`preview-markdown-btn-${id}`)
var editBtn = document.getElementById(`edit-btn-${id}`)
previewMarkdownBtn.innerHTML = '<span>Preview</span><img src="/img/eye.png"/>'
}
function previewMarkdown (e) {
var previewBtn = e.target.parentElement
var previewEl = document.getElementById(`markdown-preview-${previewBtn.dataset.form}`)
var form = document.getElementById(previewBtn.dataset.form)
var textarea = form.content
var markdown = textarea.value
var editBtn = document.getElementById(`edit-btn-${form.id}`)
previewEl.innerHTML = marked(markdown)
textarea.classList.add('hidden')
previewEl.classList.remove('hidden')
editBtn.innerHTML = '<span>Edit</span><img src="/img/pencil.png"/>'
previewBtn.innerHTML = ''
}
function showTextarea (e) {
var editBtn = e.target.parentElement // TODO
var form = document.getElementById(editBtn.dataset.form)
var textarea = form.content
var previewEl = document.getElementById(`markdown-preview-${editBtn.dataset.form}`)
editBtn.innerHTML = ''
renderPreviewMarkdownBtn(editBtn.dataset.form)
previewEl.classList.add('hidden')
textarea.classList.remove('hidden')
textarea.focus()
}
function detectUserAgent () {
if (window.DatArchive) {
// enable form
document.querySelectorAll('[disabled]').forEach(function (el) {
el.disabled = false
})
} else {
renderMessage('Sorry, PasteDat only works in the Beaker browser.', 'error', false)
}
}