Skip to content

Commit 02a4938

Browse files
localstorage syncing - #4
1 parent 73797d2 commit 02a4938

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/script.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,25 @@ function generate_id() {
4444
return Math.floor(Math.random() * 1e16).toString(16)
4545
}
4646

47-
groups = {};
47+
if (localStorage.getItem('powernote_data-notes') == null) {
48+
notes = [];
49+
// Load test data
50+
notes = testdata.split('\n');
51+
} else {
52+
notes = JSON.parse(localStorage.getItem('powernote_data-notes'));
53+
}
54+
groups = [];
4855
event_chain = [];
4956

57+
notes.forEach((note) => {
58+
$('#notes-panel').append(load_note(note));
59+
})
60+
61+
function sync() {
62+
notes = note_elements_to_array($('li'));
63+
localStorage.setItem('powernote_data-notes', JSON.stringify(notes));
64+
}
65+
5066
// sortable.on($('#notes-panel')[0],'onEnd',(e)=>{console.log(e)})
5167
// Sortable.utils.on($('#notes-panel').children()[0],'onStart',(events)=>{console.log(true)})
5268

@@ -146,6 +162,7 @@ $('#delete-notes-button').click(() => {
146162
$('li.list-group-item.selected').remove();
147163
update_buttons();
148164
write_to_text();
165+
sync();
149166
});
150167
// Merge multiple notes into one
151168
$('#merge-notes-button').click(() => {
@@ -161,6 +178,7 @@ $('#merge-notes-button').click(() => {
161178
}
162179

163180
update_buttons();
181+
sync();
164182
});
165183
// Unmerge (split by newline)
166184
$('#unmerge-notes-button').click(() => {
@@ -178,6 +196,7 @@ $('#unmerge-notes-button').click(() => {
178196
}
179197

180198
update_buttons();
199+
sync();
181200
});
182201
// Select all
183202
$('#select-notes-button').click(() => {
@@ -187,6 +206,7 @@ $('#select-notes-button').click(() => {
187206
});
188207
$('li').each((index) => Sortable.utils.select($('li')[index]))
189208
update_buttons();
209+
sync();
190210
});
191211
// Deselect all
192212
$('#deselect-notes-button').click(() => {
@@ -196,6 +216,7 @@ $('#deselect-notes-button').click(() => {
196216
});
197217
$('li').each((index) => Sortable.utils.deselect($('li')[index]))
198218
update_buttons();
219+
sync();
199220
});
200221
// Alphabetical sort
201222
$('#sort_az-notes-button').click(() => {
@@ -206,6 +227,7 @@ $('#sort_az-notes-button').click(() => {
206227
});
207228
sorted_list = note_elements_to_array(selection).sort();
208229
replace_note_elements(selection, sorted_list);
230+
sync();
209231
});
210232
// Reverse alphabetical sort
211233
$('#sort_za-notes-button').click(() => {
@@ -216,6 +238,7 @@ $('#sort_za-notes-button').click(() => {
216238
});
217239
sorted_list = note_elements_to_array(selection).sort().reverse();
218240
replace_note_elements(selection, sorted_list);
241+
sync();
219242
});
220243
// Short to long sort
221244
$('#sort_sl-notes-button').click(() => {
@@ -226,6 +249,7 @@ $('#sort_sl-notes-button').click(() => {
226249
});
227250
sorted_list = note_elements_to_array(selection).sort((a, b) => a.length - b.length);
228251
replace_note_elements(selection, sorted_list);
252+
sync();
229253
});
230254
// Short to long sort
231255
$('#sort_ls-notes-button').click(() => {
@@ -236,6 +260,7 @@ $('#sort_ls-notes-button').click(() => {
236260
});
237261
sorted_list = note_elements_to_array(selection).sort((a, b) => a.length - b.length).reverse();
238262
replace_note_elements(selection, sorted_list);
263+
sync();
239264
});
240265
// Reverse alphabetical sort
241266
$('#number-notes-button').click(() => {
@@ -249,25 +274,22 @@ $('#number-notes-button').click(() => {
249274
list[i] = (i + 1) + '. ' + list[i];
250275
}
251276
replace_note_elements(selection, list);
277+
sync();
252278
});
253279

254-
// Load test data
255-
notes = testdata.split('\n');
256-
notes.forEach((note) => {
257-
$('#notes-panel').append(load_note(note));
258-
})
259-
260280
// Convert list of note elements to text field
261281
function write_to_text() {
262282
$('#text-panel').val(note_elements_to_array($('li')).join('\n'));
263283
update_textarea();
284+
sync();
264285
}
265286

266287
function update_textarea() {
267288
t = $('#text-panel')[0];
268289
// Adapted from https://stackoverflow.com/a/1430925
269290
t.style.height = "";
270291
t.style.height = t.scrollHeight + 3 + "px";
292+
sync();
271293
}
272294

273295
// Create sortable instance for notes list
@@ -278,20 +300,24 @@ sortable = Sortable.create($('#notes-panel')[0], {
278300
onStart: function(evt) {
279301
record_event(evt);
280302
write_to_text();
303+
sync();
281304
},
282305
onSort: function(evt) {
283306
record_event(evt);
284307
write_to_text();
308+
sync();
285309
},
286310
onSelect: function(evt) {
287311
record_event(evt);
288312
write_to_text();
289313
update_buttons();
314+
sync();
290315
},
291316
onDeselect: function(evt) {
292317
record_event(evt);
293318
write_to_text();
294319
update_buttons();
320+
sync();
295321
}
296322
});
297323

0 commit comments

Comments
 (0)