-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
547 lines (468 loc) · 17.3 KB
/
script.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
var files = [];
var fileList = [];
//example
if (localStorage.getItem("fileList") === null) {
console.log('new user')
var exampleFileList = JSON.stringify(['example'])
localStorage.setItem('fileList',exampleFileList)
localStorage.setItem('example','welcome to noted, this is an example file!')
}
window.onload = function(){
console.log('haha funny joke') //debugging at its finiest
loadFileList();
loadFiles()
showSplashScreen()
}
function loadFileList(){
fileList = JSON.parse(localStorage.getItem('fileList'))
for(let item of fileList){
console.log(item)
var lclstrg = localStorage.getItem(item)
var objectified = {name:item, content:lclstrg};
files.push(objectified)
//fileList.push(objectified.name)
console.log(files)
}
}
function loadFiles(){
var scroll = document.getElementById('files').scrollTop
clearFiles()
/* var files = [
{
name: "shopping list",
content: "buy stuff"
},
{
name: "other",
content: "other stuff"
},
{
name: "friends",
content: "example"
},
]; */
//get array somehow idk
for(let item of files){
console.log(item)
var sidebar = document.getElementById('sidebar')
var fileDiv = document.createElement("div");
var fileContainer = document.getElementById('files')
fileDiv.addEventListener('click', function(){
loadFile(item)
})
fileDiv.addEventListener("contextmenu", e => {
e.preventDefault();
createContextMenu(e.pageX,e.pageY, item)
});
fileDiv.className = "file"
fileDiv.title = item.name
fileDiv.id = "file_"+item.name
var a = document.createElement("a");
a.className = 'asdf-container'
fileDiv.appendChild(a);
var textnode = document.createTextNode(item.name); // Create a text node
a.appendChild(textnode);
var deleteBtn = document.createElement("button")
deleteBtn.className = "Btn"
deleteBtn.innerHTML = '<i class="material-icons fix-button">delete</i>'
deleteBtn.title = 'Delete this note'
deleteBtn.addEventListener('click', function(e){
if(e.shiftKey){
removeFile(item.name, true)
} else {
askRemoveFile(item.name)
}
})
fileDiv.appendChild(deleteBtn)
var editBtn = document.createElement("button")
editBtn.className = "Btn"
editBtn.innerHTML = '<i class="material-icons fix-button">edit</i>'
editBtn.title = 'Rename note'
editBtn.addEventListener('click', function(){
askRenameFile(item.name)
})
fileDiv.appendChild(editBtn)
/* var saveBtn = document.createElement("button")
saveBtn.className = "Btn"
saveBtn.innerHTML = '<i class="material-icons fix-button">save</i>'
saveBtn.title = 'Save note as text file'
saveBtn.addEventListener('click', function(){
var itemContent = localStorage.getItem(item.name)
saveTextAsFile(itemContent,item.name) //change later to use item.content because this is a terrible solution that im only doing so i can go watch home alone
})
fileDiv.appendChild(saveBtn) */
fileContainer.appendChild(fileDiv)
}
createTools()
document.getElementById('files').scrollTop = scroll
}
function loadFile(item){//note the not s in loadFile
loadFiles()
var splashtext = document.getElementById('splashtext')
splashtext.classList.add('hidden')
var fileContent = document.getElementById('contents')
var fileName = document.getElementById('title')
fileContent.classList.remove('hidden')
fileName.innerText = item.name
//fileContent.setAttribute('contenteditable', true);
//fileContent.innerText = item.content
fileContent.value = localStorage.getItem(item.name)
var selectionDiv = document.getElementById('file_'+item.name)
if(selectionDiv == null){
console.log('pfft doesnt exist u idot')
showSplashScreen()
} else{
document.getElementById('file_'+item.name).classList.add('selected')
}
autoExpand(fileContent);
}
function save(){
var fileContent = document.getElementById('contents')
var fileName = document.getElementById('title')
localStorage.setItem(fileName.innerText, fileContent.value)
}
function createFile(fileName, fileContent){
if(fileName == 'fileList' || fileName == "" || fileList.includes(fileName) || fileName == "notedllama"){//todo also remember to add check for used filename
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'File name error (You should never see this screen)',
footer: '<a class="llama" href="https://i.etsystatic.com/14058045/r/il/d17ec2/1488837902/il_570xN.1488837902_c9os.jpg" target="_blank">picture of llama to cheer you up</a>'
})
} else{
var newFileName = fileName
localStorage.setItem(newFileName, fileContent)
var oldFileList = JSON.parse(localStorage.getItem('fileList'))
var joined = [newFileName, ...oldFileList]
localStorage.setItem('fileList', JSON.stringify(joined))
//fileList = JSON.parse(localStorage.getItem('fileList'))
files.push({name:newFileName, content:fileContent})
fileList.push(newFileName)
console.log(files)
loadFiles()
loadFile({name:newFileName, content:fileContent})
}
}
function clearFiles(){//this one clears the sidebar of files
document.getElementById('sidebar').innerHTML = ` <div onclick="showSplashScreen()" class="noselect">
<h1 class="logo"><i class="material-icons">description</i> noted</h1>
</div><hr class="top-hr">`
var fileContainer = document.createElement('div')
fileContainer.id = 'files'
fileContainer.className = 'files'
document.getElementById('sidebar').appendChild(fileContainer)
}
function createTools(){
var sidebar = document.getElementById('sidebar')
var toolbox = document.createElement('div')
toolbox.className = "toolbox"
var createButton = document.createElement('button')
createButton.innerHTML = '<i class="material-icons">add</i>'
createButton.title = 'Create new note'
createButton.addEventListener('click', function(){
askFileName()
})
createButton.className = "toolboxbutton addbutton"
var clearButton = document.createElement('button')
clearButton.innerHTML = '<i class="material-icons">delete_sweep</i>'
clearButton.title = 'Delete all notes'
clearButton.addEventListener('click', function(){
clearAll()
})
clearButton.className = "toolboxbutton delete-allbutton"
var uploadButton = document.createElement('button')
uploadButton.innerHTML = '<i class="material-icons">publish</i>'
uploadButton.title = 'Upload note from computer'
uploadButton.addEventListener('click', function(){
uploadFile()
})
uploadButton.className = "toolboxbutton uploadbutton"
toolbox.appendChild(createButton)
toolbox.appendChild(uploadButton)
toolbox.appendChild(clearButton)
sidebar.appendChild(toolbox)
}
async function askFileName(){
Swal.fire({
title: "File name",
text: "What do you want to name this awesome file?",
input: 'text',
showCancelButton: true,
inputValidator: (value) => {
if (!value) {
return 'You need to name it something!'
}
if(value == 'fileList'){
return 'Sorry, that name is reserved'
}
if(fileList.includes(value)){
return 'Sorry, that name is taken. (Deja vu?)'
}
if(value == 'test'){
return '<a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ" class="llama">Sorry, that name is reserved</a>'
}
}
}).then((result) => {
if (result.value) {
createFile(result.value, 'Nothing... yet')
}
});
}
async function askRemoveFile(name){
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete ' + escapeHtml(name) + '!'
}).then((result) => {
if (result.value) {
removeFile(name, true)
}
})
}
function removeFile(name, showToast){
localStorage.removeItem(name)
var whats = JSON.parse(localStorage.getItem('fileList'))
var index = whats.indexOf(name)
whats.splice(index, 1)
localStorage.setItem('fileList', JSON.stringify(whats))
var index2 = fileList.indexOf(name)
fileList.splice(index2, 1)
var removeIndex = files.map(function(item) { return item.name; })
.indexOf(name);
~removeIndex && files.splice(removeIndex, 1);
//clearFiles()
//loadFileList()
loadFiles()
showSplashScreen()
if(showToast){
swal.fire({
title: escapeHtml(name) +' was deleted',
showCancelButton: false,
showConfirmButton: false,
timer: 1500,
toast: true,
position: "top-right",
icon: "success",
})
}
}
function showSplashScreen(){
loadFiles()
var fileContent = document.getElementById('contents')
var fileName = document.getElementById('title')
fileName.innerText = 'noted text editor by jeffalo'
fileContent.classList.add('hidden')
var splashtext = document.getElementById('splashtext')
splashtext.classList.remove('hidden')
splashtext.innerHTML = 'select a document or create one with the panel on the left. <br><br> suggestions, feedback or issues? check out the <a class="llama" target="_blank" href="https://github.com/JeffaloBob/noted">GitHub repo.</a>'
//fileContent.setAttribute('contenteditable', false);
}
function renameFile(oldName, newName){ //wish me good luck 😅
var oldcontent = localStorage.getItem(oldName)
//localStorage.removeItem(oldName)
//now the actual thing is renamed, so now we have to chnage in in the dicitonary
var oldList = JSON.parse(localStorage.getItem('fileList'))
//var index = oldList.indexOf(oldName)
//oldList[index] = newName
//localStorage.setItem('fileList', JSON.stringify(oldList))
//var index2 = fileList.indexOf(oldName)
//fileList[index2] = newName
localStorage.setItem(newName, oldcontent)
files.push({name:newName, content:oldcontent})
oldList.push(newName)
localStorage.setItem('fileList', JSON.stringify(oldList))
removeFile(oldName, false)
loadFiles()
fileList.push(newName)
loadFile({name:newName, content:oldcontent})
swal.fire({
title: escapeHtml(oldName) +' was renamed to '+escapeHtml(newName),
showCancelButton: false,
showConfirmButton: false,
timer: 1500,
toast: true,
position: "top-right",
icon: "success",
})
}
async function askRenameFile(oldName){
Swal.fire({
title: "New file name",
text: "What do you want to rename this awesome file to?",
input: 'text',
inputValue: oldName,
showCancelButton: true,
inputValidator: (value) => {
if(value == oldName){
return 'You have to actually... you know... change the name?'
}
if (!value) {
return 'You need to name it something!'
}
if(value == 'fileList'){
return 'Sorry, that name is reserved'
}
if(fileList.includes(value)){
return 'Sorry, that name is taken. (Deja vu?)'
}
}
}).then((result) => {
if (result.value) {
renameFile(oldName, result.value)
}
});
}
var autoExpand = function (field) {
// Reset field height
field.style.height = 'inherit';
// Get the computed styles for the element
var computed = window.getComputedStyle(field);
// Calculate the height
var height = parseInt(computed.getPropertyValue('border-top-width'), 10)
+ parseInt(computed.getPropertyValue('padding-top'), 10)
+ field.scrollHeight
+ parseInt(computed.getPropertyValue('padding-bottom'), 10)
+ parseInt(computed.getPropertyValue('border-bottom-width'), 10);
field.style.height = height + 'px';
};
document.addEventListener('input', function (event) {
if (event.target.tagName.toLowerCase() !== 'textarea') return;
autoExpand(event.target);
}, false);
async function clearAll(){
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete everything!'
}).then((result) => {
if (result.value) {
var fileListClear = JSON.parse(localStorage.getItem('fileList'))
for(let file of fileListClear){
removeFile(file, false)
}
loadFiles()
showSplashScreen()
swal.fire({
title: 'Notes cleared',
showCancelButton: false,
showConfirmButton: false,
timer: 1500,
toast: true,
position: "top-right",
icon: "success",
})
}
})
}
function saveTextAsFile(textToWrite, fileNameToSaveAs)
{
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
async function uploadFile(){
const { value: file } = await Swal.fire({
title: 'Select text file',
input: 'file',
inputAttributes: {
'accept': 'text/*',
'aria-label': 'Upload a text file',
'id':'fileUploader'
}
})
if (file) {
const reader = new FileReader()
console.log(fileUploader.value)
reader.onload = (e) => {
console.log(reader.result)
var randomstring = makeid(10)
if(fileList.includes(file.name)){
createFile(file.name + " - " +randomstring, reader.result)
} else{
createFile(file.name, reader.result)
}
}
reader.readAsText(file)
}
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function createContextMenu(x,y, file){
console.log(file)
var old_element = document.getElementById("context-menu");
var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);
var contextDiv = document.getElementById('context-menu')
contextDiv.style.top = y+"px"
contextDiv.style.left = x+"px"
contextDiv.style.display = 'block'
var menuTitle = document.getElementById('menu-title')
var renameBtn = document.getElementById('renameBtn')
var deleteBtn = document.getElementById('deleteBtn')
var downloadBtn = document.getElementById('downloadBtn')
menuTitle.innerText = file.name
renameBtn.addEventListener('click', e=> {
askRenameFile(file.name)
})
deleteBtn.addEventListener('click', e=> {
if(e.shiftKey){
removeFile(file.name, true)
} else {
askRemoveFile(file.name)
}
})
downloadBtn.addEventListener('click', e=> {
var itemContent = localStorage.getItem(file.name)
saveTextAsFile(itemContent,file.name) //change later to use item.content because this is a terrible solution that im only doing so i can go watch home alone
})
window.addEventListener("click", e => {
//console.log('a')
var contextDiv = document.getElementById('context-menu')
contextDiv.style.display = 'none'
});
}
window.addEventListener("click", e => {
//console.log('a')
var contextDiv = document.getElementById('context-menu')
contextDiv.style.display = 'none'
});