-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (158 loc) · 6.19 KB
/
main.js
File metadata and controls
195 lines (158 loc) · 6.19 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
(function () {
var notes_container = document.querySelector('.notes-container');
var localstorage = Boolean(localStorage);
var NOTES = {
lastNoteID: -1,
notes: {},
save: function (id, value) {
if (value.markdown === '') return;
this.notes[id] = value;
localstorage && localStorage.setItem('pocket-notes', JSON.stringify(NOTES));
},
delete: function (id) {
delete this.notes[id];
NOTES.lastNoteID -= 1;
localstorage && localStorage.setItem('pocket-notes', JSON.stringify(NOTES));
}
}
function getDate() {
var now = new Date().toUTCString().split(' ');
now.pop();
now.pop();
now = now.join(' ');
return now;
}
function formatDate(dateString) {
return '<span style="margin:10px 0; padding-bottom:7.5px;border-bottom:1px solid;">' + dateString + '</span>'
}
function template(data) {
var colors = ['blue', 'yellow', 'green', 'pink', 'light', 'dark'];
var id = data.id || ("pocket-notes-" + (++NOTES.lastNoteID));
var markdown = data.value ? data.value.markdown : '';
var html = `<div class="notes-component" id="${id}" data-color="${colors[Math.floor(Math.random() * (5 - 0 + 1)) + 0]}" data-state="view">
<div class="notes-header">
<i class="fa fa-plus new-note"></i>
<input type="text" placeholder="Title" value="Pocket Note" />
<i class="fa fa-pen edit"></i>
<i class="fa fa-trash delete"></i>
<i class="fa fa-ellipsis-v more"></i>
<i class="fa fa-save save"></i>
<i class="fa fa-times cancel"></i>
<section class="color-palette">
<div data-color="green"></div>
<div data-color="blue"></div>
<div data-color="yellow"></div>
<div data-color="pink"></div>
<div data-color="light"></div>
<div data-color="dark"></div>
<div class="set-color"><i class="fa fa-check"></i></div>
</section>
</div>
<div class="notes-content">
<textarea class="markdown" wrap="soft" placeholder="Welcome to Pocket Note!...">${markdown}</textarea>
<div class="html"></div>
</div>
</div>`;
var tempDOM = document.createElement('div');
tempDOM.innerHTML = html;
return tempDOM.firstElementChild;
};
function createNote(data = {}) {
var notes_component = template(data);
var textarea = notes_component.querySelector('.markdown');
var domElement = notes_component.querySelector('.html');
domElement.querySelectorAll('pre code').forEach((block) => {
hljs.highlightBlock(block);
});
var id = notes_component.id;
var new_note = notes_component.querySelector('.new-note');
var edit = notes_component.querySelector('.edit');
var _delete = notes_component.querySelector('.delete');
var more = notes_component.querySelector('.more');
var save = notes_component.querySelector('.save');
var cancel = notes_component.querySelector('.cancel');
var color_palette = notes_component.querySelector('.color-palette');
var colors = notes_component.querySelectorAll('.color-palette > div:not(.set-color)');
var set_color = notes_component.querySelector('.set-color');
var previous_value = textarea.value.trim();
var markdown = textarea.value.trim();
var html = marked(markdown);
var now = data.value ? data.value.createdOn : getDate();
domElement.innerHTML = formatDate(now) + html;
new_note.addEventListener('click', createNote);
edit.addEventListener('click', function (e) {
previous_value = textarea.value.trim();
notes_component.setAttribute('data-state', 'edit');
textarea.focus();
})
save.addEventListener('click', function (e) {
notes_component.setAttribute('data-state', 'view');
markdown = textarea.value.trim();
html = marked(markdown);
now = getDate();
domElement.innerHTML = formatDate(now) + html;
var value = {
markdown: markdown,
createdOn: now
}
NOTES.save(id, value);
domElement.querySelectorAll('pre code').forEach((block) => {
hljs.highlightBlock(block);
});
})
cancel.addEventListener('click', function (e) {
notes_component.setAttribute('data-state', 'view');
textarea.value = previous_value;
})
_delete.addEventListener('click', function (e) {
NOTES.delete(id);
notes_container.removeChild(notes_component);
if (notes_container.childElementCount === 0) {
createNote();
}
})
more.addEventListener('click', function () {
color_palette.classList.add('active');
});
set_color.addEventListener('click', function () {
color_palette.classList.remove('active');
});
colors.forEach(function (color) {
color.addEventListener('click', function (e) {
var selected = e.target.attributes[0].value;
notes_component.setAttribute('data-color', selected);
})
})
notes_container.prepend(notes_component);
return notes_component;
}
window.onload = function () {
var wh = window.innerHeight;
var header = window.getComputedStyle(document.querySelector('header')).height;
var footer = window.getComputedStyle(document.querySelector('footer')).height;
notes_container.style.height = wh - (parseInt(header) + parseInt(footer)) + 'px';
if (!localstorage) {
alert("Notes can't be saved in your browser.");
createNote();
}
else {
console.log('localstorage available');
var data = localstorage ? JSON.parse(window.localStorage.getItem('pocket-notes')) : null;
if (data) {
NOTES.lastNoteID = data.lastNoteID;
NOTES.notes = data.notes;
Object.keys(NOTES.notes).forEach(function (note) {
createNote({
id: note,
value: NOTES.notes[note]
});
})
if (Object.keys(NOTES.notes).length === 0) {
createNote();
}
} else {
createNote();
}
}
};
})()