You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
4.4 KiB
JavaScript
155 lines
4.4 KiB
JavaScript
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
class Notes {
|
|
|
|
constructor() {
|
|
this.notes = [];
|
|
}
|
|
|
|
get(noteid) {
|
|
for (var i = 0; i < this.notes.length; i++) {
|
|
if (this.notes[i].noteid == noteid) {
|
|
return this.notes[i];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
getAll() {
|
|
return this.notes;
|
|
}
|
|
|
|
set(note) {
|
|
for (var i = 0; i < this.notes.length; i++) {
|
|
if (this.notes[i].noteid == note.noteid) {
|
|
// Refresh HTML rendering
|
|
note.html = marked(note.content);
|
|
|
|
this.notes[i] = note;
|
|
this.save();
|
|
return;
|
|
}
|
|
}
|
|
this.notes.push(note);
|
|
this.save();
|
|
}
|
|
|
|
del(noteid, success, error) {
|
|
var newnotearr = [];
|
|
for (var i = 0; i < this.notes.length; i++) {
|
|
if (this.notes[i].noteid != noteid) {
|
|
newnotearr.push(this.notes[i]);
|
|
}
|
|
}
|
|
this.notes = newnotearr;
|
|
this.save();
|
|
if (typeof success == 'function') {
|
|
success();
|
|
}
|
|
}
|
|
|
|
add(note, success, error) {
|
|
var noteid = null;
|
|
do {
|
|
noteid = Math.floor(Math.random() * (9999999999 - 1000000000) + 1000000000);
|
|
console.log("Generating random note ID: " + noteid);
|
|
} while (this.get(noteid) != null);
|
|
note["noteid"] = noteid;
|
|
this.notes.push(note);
|
|
this.save();
|
|
if (typeof success == 'function') {
|
|
success(note);
|
|
}
|
|
}
|
|
|
|
fix(note) {
|
|
if (typeof note.noteid !== 'number') {
|
|
note.noteid = null;
|
|
}
|
|
// Set background color
|
|
if (typeof note.color !== 'string') {
|
|
note.color = "FFF59D";
|
|
}
|
|
// Set text color based on background
|
|
if (typeof note.textcolor !== 'string') {
|
|
var r = parseInt(note.color.substring(0, 2), 16);
|
|
var g = parseInt(note.color.substring(2, 4), 16);
|
|
var b = parseInt(note.color.substring(4, 6), 16);
|
|
var contrast = Math.sqrt(
|
|
r * r * 0.241 +
|
|
g * g * 0.691 +
|
|
b * b * 0.068
|
|
);
|
|
if (contrast > 130) {
|
|
note.textcolor = "000000";
|
|
} else {
|
|
note.textcolor = "FFFFFF";
|
|
}
|
|
}
|
|
// Just in case
|
|
if (typeof note.content !== 'string') {
|
|
note.content = "";
|
|
}
|
|
// Set title
|
|
if (typeof note.title !== 'string') {
|
|
note.title = note.content.split('\n')[0].replace(/[#\-]+/gi, "").trim();
|
|
}
|
|
if (typeof note.modified !== 'number') {
|
|
note.modified = Math.round((new Date()).getTime() / 1000);
|
|
}
|
|
// Render Markdown to HTML
|
|
if (typeof note.html !== 'string') {
|
|
note.html = marked(note.content);
|
|
}
|
|
// Save
|
|
return note;
|
|
}
|
|
|
|
fixAll() {
|
|
for (var i = 0; i < this.notes.length; i++) {
|
|
this.notes[i] = this.fix(this.notes[i]);
|
|
}
|
|
this.save();
|
|
}
|
|
|
|
/**
|
|
* Sync notes with the storage backend.
|
|
*
|
|
* @param {type} success
|
|
* @param {type} error
|
|
* @returns {undefined}
|
|
*/
|
|
sync(success, error) {
|
|
if (localStorage.getItem("notes") !== null) { // There is localstorage
|
|
var storage = JSON.parse(localStorage.getItem("notes"));
|
|
|
|
console.log("Memory copy:", this.notes);
|
|
console.log("Storage copy:", storage);
|
|
var delta = getDelta(this.notes, storage);
|
|
console.log("Comparison: ", delta);
|
|
|
|
var notes = delta.noChange;
|
|
notes = notes.concat(delta.addedRemote);
|
|
notes = notes.concat(delta.changedRemote);
|
|
notes = notes.concat(delta.addedLocal);
|
|
notes = notes.concat(delta.changedLocal);
|
|
// If localStorage is missing something, we still want it
|
|
notes = notes.concat(delta.deletedRemote);
|
|
|
|
this.notes = notes;
|
|
this.fixAll();
|
|
}
|
|
this.save();
|
|
if (typeof success == 'function') {
|
|
success(this.notes);
|
|
}
|
|
}
|
|
|
|
save() {
|
|
localStorage.setItem("notes", JSON.stringify(this.notes));
|
|
}
|
|
} |