forked from breck7/ScrollHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollHubEditor.js
582 lines (491 loc) · 18.1 KB
/
scrollHubEditor.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
const getBaseUrlForFolder = (folderName, hostname, protocol) => {
if (hostname === "localhost") return "http://localhost/" + folderName
if (!folderName.includes(".")) return protocol + "//" + hostname + "/" + folderName
// now it might be a custom domain, serve it as if it is
// of course, sometimes it would not be
return protocol + "//" + folderName
}
// todo: before unload warn about unsaved changes
class EditorApp {
constructor() {
this.folderName = ""
this.previewIFrame = null
this.scrollParser = new HandParsersProgram(AppConstants.parsers).compileAndReturnRootParser()
this.initCodeMirror("custom")
window.addEventListener("resize", () => this.codeMirrorInstance.setSize(this.width, 490))
}
getEditorMode(fileName) {
const extension = fileName ? fileName.split(".").pop().toLowerCase() : ""
const modeMap = {
html: "htmlmixed",
htm: "htmlmixed",
js: "javascript",
css: "css",
scroll: "custom",
parsers: "custom"
}
return modeMap[extension] || null
}
initCodeMirror(mode) {
const textarea = document.getElementById("fileEditor")
if (this.codeMirrorInstance) this.codeMirrorInstance.toTextArea()
if (mode === "custom") {
// Use custom scroll parser mode with its autocomplete
this.codeMirrorInstance = new ParsersCodeMirrorMode(mode, () => this.scrollParser, undefined, CodeMirror).register().fromTextAreaWithAutocomplete(textarea, {
lineWrapping: true,
lineNumbers: false,
mode
})
} else {
// Use standard CodeMirror with appropriate mode and hint addons
this.codeMirrorInstance = CodeMirror.fromTextArea(textarea, {
lineWrapping: true,
lineNumbers: false,
mode
})
}
this.codeMirrorInstance.setSize(this.width, 490)
}
mode = "custom"
updateEditorMode(fileName) {
const mode = this.getEditorMode(fileName)
if (mode === this.mode) return
const currentContent = this.codeMirrorInstance.getValue()
this.initCodeMirror(mode)
this.codeMirrorInstance.setValue(currentContent)
this.mode = mode
}
_scrollProgram
get scrollProgram() {
const { scrollParser } = this
this._scrollProgram = new scrollParser(this.bufferValue)
this._scrollProgram.setFile({ filename: this.fileName })
return this._scrollProgram
}
get bufferValue() {
return this.codeMirrorInstance.getValue().replace(/\r/g, "")
}
get width() {
const bodyWidth = document.body.clientWidth
let computedWidth
if (bodyWidth < 1000) {
// Calculate width as body width minus 250
computedWidth = bodyWidth - 270
} else {
// Set width to 784 when body width is 1000 or more
computedWidth = 784
}
// Ensure the width does not exceed 784 and is not less than 100
computedWidth = Math.max(100, Math.min(784, computedWidth))
return computedWidth
}
showError(message) {
console.error(message)
this.fileListEl.innerHTML = `<span style="color:red;">${message}</span>`
}
get filePath() {
return `${this.folderName}/${this.fileName}`
}
async main() {
this.bindFileButtons()
this.bindFileListListeners()
this.bindFileDrop()
this.bindKeyboardShortcuts()
const urlParams = new URL(window.location).searchParams
this.openFolder(urlParams.get("folderName") || window.location.hostname)
let fileName = urlParams.get("fileName") || ""
if (fileName.startsWith("/")) this.setFileNameInUrl(fileName.replace(/^\/+/, "")) // strip leading slashes
if (!fileName) {
let buffer = urlParams.get("buffer")
if (buffer) {
this.fetchAndDisplayFileList()
buffer = buffer.replace(/TODAYS_DATE/g, new Date().toLocaleDateString("en-US"))
this.setFileContent(decodeURIComponent(buffer))
} else {
await this.fetchAndDisplayFileList()
this.autoOpen()
}
} else {
this.fetchAndDisplayFileList()
this.openFile(fileName)
}
return this
}
setFileNameInUrl(fileName) {
const url = new URL(window.location)
const urlParams = url.searchParams
urlParams.set("fileName", fileName)
window.history.replaceState(null, "", url)
}
autoOpen() {
const { filenames } = this
this.openFile(filenames.includes("index.scroll") ? "index.scroll" : filenames[0])
}
async openFolder(folderName) {
this.folderName = folderName
this.updateFooterLinks()
}
async openFile(fileName) {
const { folderName } = this
this.fileName = fileName
const filePath = `${folderName}/${fileName}`
const response = await fetch(`/read.htm?folderName=${folderName}&filePath=${encodeURIComponent(filePath)}`)
const content = await response.text()
// Update editor mode before setting content
this.updateEditorMode(fileName)
this.setFileContent(content)
this.setFileNameInUrl(fileName)
this.updatePreviewIFrame()
this.updateVisitLink()
if (!this.files) await this.fetchAndDisplayFileList()
else this.renderFileList()
}
bindFileDrop() {
const dropZone = document.getElementById("editForm")
dropZone.addEventListener("dragover", this.handleDragOver.bind(this))
dropZone.addEventListener("dragleave", this.handleDragLeave.bind(this))
dropZone.addEventListener("drop", this.handleDrop.bind(this))
}
get permalink() {
const dir = this.rootUrl.replace(/\/$/, "") + "/"
const { fileName } = this
return dir + (fileName.endsWith(".scroll") ? this.scrollProgram.permalink : fileName)
}
updateVisitLink() {
document.getElementById("folderNameLink").innerHTML = this.permalink
document.getElementById("folderNameLink").href = this.permalink
}
showSpinner(message, style) {
document.querySelector("#spinner").innerHTML = `<span${style}>${message}</span>`
document.querySelector("#spinner").style.display = "block"
}
showError(message) {
this.showSpinner(message, ` style="color:red;"`)
}
hideSpinner() {
document.querySelector("#spinner").style.display = "none"
}
async saveFile() {
if (!this.fileName) {
let fileName = prompt("Enter a filename", "untitled")
if (!fileName) return ""
this.fileName = this.sanitizeFileName(fileName)
}
this.showSpinner("Publishing...")
const formData = new FormData()
const { filePath } = this
formData.append("filePath", filePath)
formData.append("folderName", this.folderName)
const sentData = this.bufferValue
formData.append("content", sentData)
try {
const response = await fetch("/write.htm", {
method: "POST",
body: formData
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(errorText || "Network response was not ok")
}
const newVersion = await response.text()
console.log(`'${filePath}' saved`)
this.hideSpinner()
if (this.bufferValue === sentData && newVersion !== sentData) {
// Todo: figure out how to restore cursor position.
// this.codeMirrorInstance.setValue(newVersion)
}
this.updatePreviewIFrame()
} catch (error) {
this.showError(error.message.split(".")[0])
console.error("Error saving file:", error.message)
throw error // Re-throw the error if you want calling code to handle it
}
}
async saveCommand() {
await this.saveFile()
await this.fetchAndDisplayFileList()
}
bindKeyboardShortcuts() {
const that = this
const keyboardShortcuts = {
"command+s": () => {
that.saveCommand()
},
"ctrl+n": () => {
that.createFileCommand()
}
}
// note: I do not rememeber why we do any of this stopCallback stuff but it seems hard won knowledge ;)
Mousetrap._originalStopCallback = Mousetrap.prototype.stopCallback
Mousetrap.prototype.stopCallback = async function (evt, element, shortcut) {
if (shortcut === "command+s") {
// save. refresh preview
that.saveCommand()
evt.preventDefault()
return true
} else if (keyboardShortcuts[shortcut]) {
keyboardShortcuts[shortcut]()
evt.preventDefault()
return true
}
if (Mousetrap._pause) return true
return Mousetrap._originalStopCallback.call(this, evt, element)
}
Object.keys(keyboardShortcuts).forEach(key => {
Mousetrap.bind(key, function (evt) {
keyboardShortcuts[key]()
// todo: handle the below when we need to
if (evt.preventDefault) evt.preventDefault()
return false
})
})
}
handleDragOver(event) {
event.preventDefault()
event.stopPropagation()
event.currentTarget.classList.add("drag-over")
}
handleDragLeave(event) {
event.preventDefault()
event.stopPropagation()
event.currentTarget.classList.remove("drag-over")
}
handleDrop(event) {
event.preventDefault()
event.stopPropagation()
event.currentTarget.classList.remove("drag-over")
const files = event.dataTransfer.files
if (files.length > 0) this.uploadFiles(files)
}
// New method to handle multiple file uploads
uploadFiles(files) {
this.showSpinner("Uploading...")
const uploadPromises = Array.from(files).map(file => this.uploadFile(file))
Promise.all(uploadPromises)
.then(() => {
console.log("All files uploaded successfully")
this.fetchAndDisplayFileList()
this.hideSpinner()
})
.catch(error => {
console.error("Error uploading files:", error)
// todo: show error to user
alert("Error uploading files:" + error)
})
}
// Modified uploadFile method to return a Promise
async uploadFile(file) {
const formData = new FormData()
formData.append("file", file)
formData.append("folderName", this.folderName)
try {
const response = await fetch("/upload.htm", {
method: "POST",
body: formData
})
if (!response.ok) {
const errorText = await response.text()
throw new Error(errorText || "Network response was not ok")
}
const data = await response.text()
console.log("File uploaded successfully:", data)
return data
} catch (error) {
console.error("Error uploading file:", error.message)
throw error // Re-throw the error if you want calling code to handle it
}
}
get rootUrl() {
return getBaseUrlForFolder(this.folderName, window.location.hostname, window.location.protocol)
}
updatePreviewIFrame() {
const { rootUrl, folderName } = this
this.previewIFrame = document.getElementById("previewIFrame")
this.previewIFrame.src = this.permalink
this.updateVisitLink()
document.title = `Editing ${folderName}`
}
updateFooterLinks() {
const { folderName } = this
document.getElementById("gitClone").innerHTML =
`<a class="folderActionLink" href="/globe.html?folderName=${folderName}">live traffic</a> · <a class="folderActionLink" href="/diff.htm/${folderName}">history</a> · <a class="folderActionLink" href="${folderName}.zip">download</a> · <a class="folderActionLink" href="#" onclick="window.app.duplicate()">duplicate</a> · <a href="#" class="folderActionLink" onclick="window.app.renameFolder()">rename</a> · <a href="#" class="folderActionLink" onclick="window.app.deleteFolder()">delete</a>`
}
async renameFolder() {
const newFolderName = prompt(`Rename ${this.folderName} to:`)
if (!newFolderName) return
const response = await fetch("/mv.htm", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `oldFolderName=${this.folderName}&newFolderName=${newFolderName}`
})
const result = await response.text()
window.location.href = `/edit.html?folderName=${newFolderName}`
}
async deleteFolder() {
const userInput = prompt(`To delete this entire folder, please type the folder name: ${this.folderName}`)
if (userInput !== this.folderName) return
try {
const response = await fetch("/trash.htm", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `folderName=${encodeURIComponent(this.folderName)}`
})
const data = await response.text()
console.log(data)
window.location.href = "/" // Redirect to home page after deletion
} catch (error) {
console.error("Error:", error)
}
}
async duplicate() {
this.showSpinner("Copying folder...")
const response = await fetch("/clone.htm", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `folderName=${this.folderName}&redirect=false`
})
const result = await response.text()
this.hideSpinner()
console.log(result)
window.location.href = `/edit.html?folderName=${result}`
}
async fetchAndDisplayFileList() {
const { folderName } = this
try {
const response = await fetch(`/ls.json?folderName=${folderName}`)
if (!response.ok) throw new Error(await response.text())
this.files = await response.json()
this.renderFileList()
} catch (error) {
console.error("There was a problem with the fetch operation:", error.message)
}
}
get filenames() {
return Object.keys(this.files)
}
renderFileList() {
const { files, filenames, folderName } = this
const currentFileName = this.fileName
const scrollFiles = filenames.filter(file => file.endsWith(".scroll") || file.endsWith(".parsers"))
const sorted = scrollFiles.concat(filenames.filter(file => !file.endsWith(".scroll") && !file.endsWith(".parsers")))
const fileLinks = sorted.map(file => {
const stats = files[file]
const selected = currentFileName === file ? "selectedFile" : ""
const isScrollFile = file.endsWith(".scroll") || file.endsWith(".parsers")
const isTrackedByGit = stats.versioned ? "" : " untracked"
return `<a class="${isScrollFile ? "" : "nonScrollFile"} ${selected} ${isTrackedByGit}" href="edit.html?folderName=${folderName}&fileName=${encodeURIComponent(file)}">${file}</a>`
})
this.fileListEl.innerHTML = fileLinks.join("<br>")
}
bindFileListListeners() {
this.fileListEl.addEventListener("click", event => {
if (event.metaKey || event.ctrlKey) return true
const link = event.target.closest("a")
if (!link) return true
event.preventDefault()
if (event.shiftKey) this.maybeRenameFilePrompt(link.textContent, event)
else this.openFile(link.textContent)
})
}
get fileListEl() {
return document.getElementById("fileList")
}
maybeRenameFilePrompt(oldFileName, event) {
const newFileName = prompt(`Enter new name for "${oldFileName}":`, oldFileName)
if (newFileName && newFileName !== oldFileName) {
this.performFileRename(oldFileName, this.sanitizeFileName(newFileName))
}
}
async performFileRename(oldFileName, newFileName) {
const { folderName } = this
try {
this.showSpinner("Renaming..")
const response = await fetch("/rename.htm", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `folderName=${encodeURIComponent(folderName)}&oldFileName=${encodeURIComponent(oldFileName)}&newFileName=${encodeURIComponent(newFileName)}`
})
if (!response.ok) throw new Error(await response.text())
this.hideSpinner()
console.log(`File renamed from ${oldFileName} to ${newFileName}`)
this.fetchAndDisplayFileList()
// If the renamed file was the current file, open the new file
if (this.fileName === oldFileName) this.openFile(newFileName)
} catch (error) {
console.error(error)
alert("Rename error:" + error)
}
}
sanitizeFileName(fileName) {
fileName = fileName.replace(/[^a-zA-Z0-9\.\_\-]/g, "")
return fileName.includes(".") ? fileName : fileName + ".scroll"
}
async createFileCommand() {
let fileName = prompt("Enter a filename", "untitled")
if (!fileName) return ""
const newFileName = this.sanitizeFileName(fileName)
const { folderName } = this
const filePath = `${folderName}/${newFileName}`
this.showSpinner("Creating file...")
const response = await fetch("/write.htm", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: `content=&folderName=${encodeURIComponent(folderName)}&filePath=${encodeURIComponent(filePath)}`
})
await response.text()
delete this.files
await this.openFile(newFileName)
this.hideSpinner()
}
setFileContent(value) {
document.getElementById("fileEditor").value = value.replace(/\r/g, "")
this.codeMirrorInstance.setValue(value)
const lines = value.split("\n")
const lastLine = lines.pop()
if (lines.length < 24) {
// if its a small file, put user right in editing experience
this.codeMirrorInstance.setCursor({ line: lines.length, ch: lastLine.length })
this.codeMirrorInstance.focus()
}
}
bindFileButtons() {
const that = this
document.querySelector(".renameLink").addEventListener("click", async e => {
const oldFileName = that.fileName
const newFileName = prompt(`Enter new name for "${oldFileName}":`, oldFileName)
if (newFileName && newFileName !== oldFileName) {
this.performFileRename(oldFileName, this.sanitizeFileName(newFileName))
}
})
document.querySelector(".deleteLink").addEventListener("click", async e => {
e.preventDefault()
const { fileName, folderName } = this
const userInput = prompt(`To delete this file, please type the file name: ${fileName}`)
if (!userInput || userInput !== fileName) return
const filePath = `${folderName}/${fileName}`
this.showSpinner("Deleting...")
const response = await fetch(`/delete.htm?filePath=${encodeURIComponent(filePath)}`, {
method: "POST"
})
const data = await response.text()
await this.fetchAndDisplayFileList()
this.autoOpen()
this.hideSpinner()
})
}
}
// Initialize the app when the DOM is fully loaded
document.addEventListener("DOMContentLoaded", () => {
window.app = new EditorApp()
window.app.main()
})