diff --git a/app.py b/app.py index 59e5f218d..2768406d5 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,10 @@ import base64 from flask import Flask, request, send_from_directory from flask_restful import Resource, Api from flask_cors import CORS +import time +import math +import random +import string def dict_factory(cursor, row): d = {} @@ -31,7 +35,8 @@ def insert(tablename, rec): keys = ','.join(rec.keys()) question_marks = ','.join(list('?'*len(rec))) values = tuple(rec.values()) - execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values) + cursor = execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values) + return cursor.lastrowid def delete(tablename, note_id): execute("DELETE FROM " + tablename + " WHERE note_id = ?", [note_id]) @@ -39,6 +44,7 @@ def delete(tablename, note_id): def execute(sql, params=[]): cursor = conn.cursor() cursor.execute(sql, params) + return cursor def getResults(sql, params=[]): cursor = conn.cursor() @@ -95,6 +101,62 @@ class Notes(Resource): api.add_resource(Notes, '/notes/') +class NotesChildren(Resource): + def post(self, parent_note_id): + note = request.get_json(force=True) + + noteId = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(22)) + + now = math.floor(time.time()); + + insert("notes", { + 'note_id': noteId, + 'note_title': note['note_title'], + 'note_text': '', + 'note_clone_id': '', + 'date_created': now, + 'date_modified': now, + 'icon_info': 'pencil', + 'is_finished': 0 + }) + + if parent_note_id == "root": + parent_note_id = "" + + insert("notes_tree", { + 'note_id': noteId, + 'note_pid': parent_note_id, + 'note_pos': 0, + 'is_expanded': 0 + }) + + conn.commit() + + return { + 'note_id': noteId + } + +api.add_resource(NotesChildren, '/notes//children') + +class MoveAfterNote(Resource): + def put(self, note_id, after_note_id): + after_note = getSingleResult("select * from notes_tree where note_id = ?", [after_note_id]) + + if after_note <> None: + execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [after_note['note_pid'], after_note['note_pos'] + 1, note_id]) + + conn.commit() + +api.add_resource(MoveAfterNote, '/notes//moveAfter/') + +class MoveToNote(Resource): + def put(self, note_id, parent_id): + execute("update notes_tree set note_pid = ? where note_id = ?", [parent_id, note_id]) + + conn.commit() + +api.add_resource(MoveToNote, '/notes//moveTo/') + class Tree(Resource): def get(self): notes = getResults("select notes_tree.*, notes.note_title from notes_tree join notes on notes.note_id = notes_tree.note_id order by note_pid, note_pos") diff --git a/app.pyc b/app.pyc index 0eb1fc716..a62df2ebc 100644 Binary files a/app.pyc and b/app.pyc differ diff --git a/demo.ncdb b/demo.ncdb index dd86e507a..f17f68024 100644 Binary files a/demo.ncdb and b/demo.ncdb differ diff --git a/frontend/index.html b/frontend/index.html index 529cebe76..637ecdd4d 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -105,7 +105,7 @@ let curTag = curContent.substr(0, endOfTag + 1); - console.log(contents); + //console.log(contents); for (tagId in tags) { let tag = tags[tagId]; @@ -134,21 +134,21 @@ } if (curTag.substr(0, 4) == " { function copyTitle(notes) { - for (key in notes) { - var note = notes[key]; - + for (let note of notes) { note.title = note.note_title; note.key = note.note_id; @@ -258,30 +256,52 @@ hotkeys: { keydown: { "insert": function(node) { - node.appendSibling({ - "title": "New!" - }).setActive(true); + let parentKey = (node.getParent() == null || node.getParent().key == "root_1") ? "root" : node.getParent().key; + + createNote(parentKey); }, "shift+insert": function(node) { - node.addChildren({ - "title": "New!" - }).setActive(true); + createNote(node.key); }, "del": function(node) { node.remove(); }, + "shift+up": function(node) { + if (node.getPrevSibling() != null) { + node.moveTo(node.getPrevSibling(), 'before'); + } + }, + "shift+down": function(node) { + if (node.getNextSibling() != null) { + node.moveTo(node.getNextSibling(), 'after'); + } + }, "shift+left": function(node) { if (node.getParent() != null) { - node.moveTo(node.getParent(), 'after'); + $.ajax({ + url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getParent().key, + type: 'PUT', + contentType: "application/json", + success: function(result) { + node.moveTo(node.getParent(), 'after'); + } + }); } }, "shift+right": function(node) { let prevSibling = node.getPrevSibling(); if (prevSibling != null) { - node.moveTo(prevSibling); + $.ajax({ + url: baseUrl + 'notes/' + node.key + '/moveTo/' + prevSibling.key, + type: 'PUT', + contentType: "application/json", + success: function(result) { + node.moveTo(prevSibling); - prevSibling.setExpanded(true); + prevSibling.setExpanded(true); + } + }); } } } @@ -292,6 +312,41 @@ var globalNote; + function setParent(noteId, newParentKey, successCallback) { + let newNoteName = "new note"; + + $.ajax({ + url: baseUrl + 'notes/' + nodeId + '/setParent/' + newParentKey, + type: 'PUT', + contentType: "application/json", + success: function(result) { + successCallback(); + } + }); + } + + function createNote(parentKey) { + let newNoteName = "new note"; + + $.ajax({ + url: baseUrl + 'notes/' + parentKey + '/children' , + type: 'POST', + data: JSON.stringify({ + note_title: newNoteName + }), + contentType: "application/json", + success: function(result) { + node.appendSibling({ + "title": newNoteName, + "key": result.note_id, + "note_id": result.note_id + }).setActive(true); + + message("Created!"); + } + }); + } + function loadNote(noteId) { $.get(baseUrl + 'notes/' + noteId).then(function(note) { globalNote = note;