Browse Source

Note deletion feature added

master
Christian Müller 10 years ago
parent
commit
8ccf7d5a71
  1. 14
      package.json
  2. 3
      resources/edit.html
  3. 1
      resources/public/index.html
  4. 15
      resources/public/js/publishing.js
  5. 10
      server.js
  6. 17
      src/storage.js

14
package.json

@ -23,15 +23,15 @@
}, },
"homepage": "https://github.com/chmllr/NoteHub", "homepage": "https://github.com/chmllr/NoteHub",
"dependencies": { "dependencies": {
"body-parser": "^1.14.1", "body-parser": "^1.15.0",
"express": "^4.13.3", "express": "^4.13.4",
"lru-cache": "^2.6.5", "lru-cache": "^4.0.0",
"marked": "^0.3.5", "marked": "^0.3.5",
"md5": "^2.0.0", "md5": "^2.1.0",
"sequelize": "^3.8.0", "sequelize": "^3.19.3",
"sqlite3": "^3.1.0" "sqlite3": "*"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^1.7.3" "nodemon": "^1.9.1"
} }
} }

3
resources/edit.html

@ -22,7 +22,8 @@
<textarea id="note" name="note">%CONTENT%</textarea> <textarea id="note" name="note">%CONTENT%</textarea>
<fieldset id="input-elems"> <fieldset id="input-elems">
<input class="ui-elem" id="plain-password" name="plain-password" placeholder="Password for editing" type="text">&nbsp; <input class="ui-elem" id="plain-password" name="plain-password" placeholder="Password for editing" type="text">&nbsp;
<input class="button ui-elem" id="publish-button" type="submit" value="Publish"> <input class="button ui-elem" id="publish-button" name="button" type="submit" value="Publish">
<input class="button ui-elem" id="delete-button" name="button" type="submit" value="Delete">
<span id="tableau"> <span id="tableau">
0 words 0 words
</span> </span>

1
resources/public/index.html

@ -18,6 +18,7 @@
<article class="bottom-space"> <article class="bottom-space">
<h2>Changelog</h2> <h2>Changelog</h2>
<ul> <ul>
<li><strong>2016-03</strong>: Note deletion feature added.</li>
<li><strong>2015-10</strong>: NoteHub rewritten in Node.js.</li> <li><strong>2015-10</strong>: NoteHub rewritten in Node.js.</li>
<li><strong>2015-10</strong>: NoteHub API and note styling discontinued due to low adoption by the user base.</li> <li><strong>2015-10</strong>: NoteHub API and note styling discontinued due to low adoption by the user base.</li>
<li><strong>2014-09</strong>: text size setting added</li> <li><strong>2014-09</strong>: text size setting added</li>

15
resources/public/js/publishing.js

@ -4,8 +4,7 @@ var $ = function(id) {
var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)"); var iosDetected = navigator.userAgent.match("(iPad|iPod|iPhone)");
var timer = null; var timer = null;
var timerDelay = iosDetected ? 800 : 400; var timerDelay = iosDetected ? 800 : 400;
var $note, $action, $preview, $plain_password, var $note, $action, $preview, $plain_password, $tableau;
updatePreview, $tableau;
var backendTimer; var backendTimer;
function md2html(input) { function md2html(input) {
@ -25,7 +24,7 @@ function onLoad() {
$preview = $("draft"); $preview = $("draft");
$tableau = $("tableau"); $tableau = $("tableau");
$plain_password = $("plain-password"); $plain_password = $("plain-password");
updatePreview = function() { var updatePreview = function() {
clearTimeout(timer); clearTimeout(timer);
var content = $note.value; var content = $note.value;
var delay = Math.min(timerDelay, timerDelay * (content.length / 400)); var delay = Math.min(timerDelay, timerDelay * (content.length / 400));
@ -36,6 +35,7 @@ function onLoad() {
}; };
if ($action == "UPDATE") updatePreview(); if ($action == "UPDATE") updatePreview();
else { else {
$("delete-button").style.display = "none";
$note.value = ""; $note.value = "";
var draft = localStorage.getItem("draft"); var draft = localStorage.getItem("draft");
if (draft) { if (draft) {
@ -44,14 +44,13 @@ function onLoad() {
} }
} }
$note.onkeyup = updatePreview; $note.onkeyup = updatePreview;
$("publish-button").onclick = function(e) { $("delete-button").onclick = $("publish-button").onclick = function(e) {
localStorage.removeItem("draft"); localStorage.removeItem("draft");
self.onbeforeunload = null;; self.onbeforeunload = null;
if ($plain_password.value != "") $("password").value = md5($plain_password.value); if ($plain_password.value != "") $("password").value = md5($plain_password.value);
$plain_password.value = null; $plain_password.value = null;
$("signature").value = md5($("session").value + $("signature").value = md5($("session").value + $note.value.replace(/[\n\r]/g, ""));
$note.value.replace(/[\n\r]/g, "")); };
}
if (iosDetected) $note.className += " ui-border"; if (iosDetected) $note.className += " ui-border";
else $note.focus(); else $note.focus();
self.onbeforeunload = saveDraft; self.onbeforeunload = saveDraft;

10
server.js

@ -61,9 +61,17 @@ app.post('/note', function (req, res) {
storage.addNote(note, password).then(goToNote); storage.addNote(note, password).then(goToNote);
else { else {
CACHE.del(id); CACHE.del(id);
if (body.button == "Delete") {
log("deleting note", id);
storage.deleteNote(id, password).then(
() => sendResponse(res, 200, "Note deleted"),
error => sendResponse(res, 403, error.message));
} else {
log("updating note", id);
storage.updateNote(id, password, note).then(goToNote, storage.updateNote(id, password, note).then(goToNote,
error => sendResponse(res, 403, error.message)); error => sendResponse(res, 403, error.message));
} }
}
}); });
app.get("/:year/:month/:day/:title", function(req, res) { app.get("/:year/:month/:day/:title", function(req, res) {
@ -145,7 +153,7 @@ var sendResponse = (res, code, message) => {
var notFound = res => sendResponse(res, 404, "Not found"); var notFound = res => sendResponse(res, 404, "Not found");
var server = app.listen(process.env.PORT || 3000, function() { var server = app.listen(process.env.PORT || 3000, function() {
log('NoteHub server listening on port %s', server.address().port); log('NoteHub server listening on port', server.address().port);
}); });
setInterval(() => { setInterval(() => {

17
src/storage.js

@ -45,11 +45,18 @@ module.exports.addNote = (note, password) => getFreeId().then(id => Note.create(
password: password password: password
})); }));
module.exports.updateNote = (id, password, text) => Note.findById(id).then(note => { var passwordCheck = (note, password, callback) => (!note || note.password !== password)
if (!note || note.password !== password) return new Promise((resolve, reject) => { ? new Promise((resolve, reject) => reject({ message: "Password is wrong" }))
reject({ message: "Password is wrong" }); : callback();
});
module.exports.updateNote = (id, password, text) =>
Note.findById(id).then(note =>
passwordCheck(note, password, () => {
note.text = text; note.text = text;
note.edited = new Date(); note.edited = new Date();
return note.save(); return note.save();
}); }));
module.exports.deleteNote = (id, password) =>
Note.findById(id).then(note =>
passwordCheck(note, password, () => note.destroy()));
Loading…
Cancel
Save