Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
176 lines (144 loc) · 5.83 KB
/
Copy pathscript.js
File metadata and controls
176 lines (144 loc) · 5.83 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
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
// DOM elements to interact with
const createNote = document.getElementById('createNote');
const deleteNote = document.getElementById('deleteNote');
const notesList = document.getElementById('notesList');
const focusedNote = document.getElementById('focusedNote');
let currentNote = undefined, currentUUID = undefined;
// displays last edited note OR empty note if no notes have been created
const uuidList = getUUIDList();
if (uuidList && uuidList[0]) {
focusNote(uuidList[0]);
} else {
const uuid = crypto.randomUUID();
localStorage.setItem(uuid, JSON.stringify({"time":1730769988217,"blocks":[{"id":"Ux8d0IhMH9","type":"paragraph","data":{"text":"<b>Demo Note</b>"}},{"id":"9O_ROcctlg","type":"paragraph","data":{"text":"This is the <i>iNotes</i> app, where you can write notes about anything!"}},{"id":"_bNC8nJB3_","type":"paragraph","data":{"text":"In <i>iNotes</i>, you can <b>bold</b> words by clicking Command+B and <i>italicize </i>words by clicking Command+I. You can also <i>bold and italize</i> words/phrases by clicking both of the above shortcuts."}},{"id":"0T2q6iKlZP","type":"paragraph","data":{"text":"To create your own note, click the \"Create Note\" button in the top left."}},{"id":"c-yU7nFQBb","type":"paragraph","data":{"text":"If you want, you can delete this note by clicking the \"Delete Note\" button in the top left. That will automatically delete this note and create a new note for you."}},{"id":"uK68Lr6D3_","type":"paragraph","data":{"text":"<b>All of your notes will be saved in your browser, and loaded back once you reload the page.</b>"}}],"version":"2.30.6"}));
focusNote(uuid);
}
createNote.addEventListener('click', createEmptyNote);
// deletes the focused note
deleteNote.addEventListener('click', () => {
if (currentUUID == undefined) return;
const uuidList = getUUIDList();
if (uuidList == undefined) return;
if (uuidList.length == 0) return;
if (uuidList.length == 1) {
localStorage.removeItem(uuidList[0]);
uuidList.pop();
localStorage.setItem('uuidList', JSON.stringify(uuidList));
createEmptyNote();
return;
}
const uuidIndex = uuidList.indexOf(currentUUID);
localStorage.removeItem(uuidList[uuidIndex]);
uuidList.splice(uuidIndex, 1); // remove uuid at index uuidIndex
localStorage.setItem('uuidList', JSON.stringify(uuidList));
if (uuidIndex == 0) {
focusNote(uuidList[uuidIndex]);
} else {
focusNote(uuidList[uuidIndex-1]);
}
});
// creates an empty note with random uuid
function createEmptyNote() {
focusNote(crypto.randomUUID());
}
// focuses note with uuid
function focusNote(uuid) {
const uuidList = getUUIDList();
if (uuidList) {
if (!uuidList.includes(uuid)) {
uuidList.push(uuid);
localStorage.setItem('uuidList', JSON.stringify(uuidList));
}
} else {
localStorage.setItem('uuidList', JSON.stringify([uuid]));
}
const note = document.createElement('div');
note.classList.add('note');
note.setAttribute('id', uuid);
if (focusedNote.children.length > 0)
focusedNote.removeChild(focusedNote.children[0]);
focusedNote.appendChild(note);
currentNote = new EditorJS({
holder: uuid,
data: localStorage.getItem(uuid) ? JSON.parse(localStorage.getItem(uuid)) : undefined,
autofocus: true,
placeholder: 'Start writing whatever you want!',
onChange: (api, event) => {
// console.log(api, event);
currentNote.save().then((outputData) => {
localStorage[uuid] = JSON.stringify(outputData);
renderNotesList();
});
}
});
currentUUID = uuid;
renderNotesList();
}
// re-renders notes list
function renderNotesList() {
const uuidList = getUUIDList();
notesList.textContent = '';
const recentNotesTitle = document.createElement('span');
recentNotesTitle.classList.add('recentNotesTitle');
recentNotesTitle.textContent = 'Recent Notes: '
notesList.appendChild(recentNotesTitle);
for (const uuid of uuidList) {
let selectorText = 'Empty Note';
if (localStorage[uuid] != undefined) {
let noteContent = JSON.parse(localStorage[uuid]);
if (noteContent.blocks &&
noteContent.blocks[0] &&
noteContent.blocks[0].data &&
noteContent.blocks[0].data.text) {
selectorText = sanitize(noteContent.blocks[0].data.text);
selectorText = truncate(selectorText, 20);
}
}
const noteSelector = document.createElement('button');
noteSelector.textContent = selectorText;
noteSelector.classList.add('noteSelector');
noteSelector.onclick = () => {
focusNote(uuid);
}
if (uuid == currentUUID) {
noteSelector.classList.add('selected');
} else {
noteSelector.classList.remove('selected');
}
notesList.appendChild(noteSelector);
}
const spacer = document.createElement('span')
spacer.innerHTML = ' ';
notesList.appendChild(spacer);
}
// gets and sorts UUID list
function getUUIDList() {
let uuidList = JSON.parse(localStorage.getItem('uuidList'));
if (!uuidList) return undefined;
uuidList = uuidList
.map(e => {
const timestamp = localStorage[e] ? JSON.parse(localStorage[e]).time : (new Date().getTime());
return {
uuid: e,
timestamp: timestamp
}
})
.sort((a, b) => b.timestamp - a.timestamp)
.map(e => e.uuid);
return uuidList;
}
// truncates string to numOfChars, with trailing dots if the string is too long.
function truncate(string, numOfChars) {
if (string.length <= numOfChars) {
return string;
}
if (numOfChars <= 3) {
return string.substring(0, numOfChars);
}
return string.substring(0, numOfChars-3) + '...';
}
function sanitize(string) {
return string.replace(/ /gi, '') // remove non-breaking spaces from display
.replace(/<b>/gi, '').replace(/<\/b>/gi, '') // remove bold (<b>)
.replace(/<i>/gi, '').replace(/<\/i>/gi, ''); // remove italics (<i>)
}