Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
creating new note, moving note from tree and into tree
azivnercfd948bd5bc4
diff --git a/app.py b/app.pyindex 59e5f218de..2768406d56 100644--- a/app.py+++ b/app.py@@ -3,6 +3,10 @@ import base64from flask import Flask, request, send_from_directoryfrom flask_restful import Resource, Apifrom flask_cors import CORS+import time+import math+import random+import stringdef 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.lastrowiddef 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 cursordef getResults(sql, params=[]):cursor = conn.cursor()@@ -95,6 +101,62 @@ class Notes(Resource):api.add_resource(Notes, '/notes/<string:note_id>')+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/<string:parent_note_id>/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/<string:note_id>/moveAfter/<string:after_note_id>')++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/<string:note_id>/moveTo/<string:parent_id>')+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.pycindex 0eb1fc716a..a62df2ebc6 100644Binary files a/app.pyc and b/app.pyc differdiff --git a/demo.ncdb b/demo.ncdbindex dd86e507a2..f17f68024d 100644Binary files a/demo.ncdb and b/demo.ncdb differdiff --git a/frontend/index.html b/frontend/index.htmlindex 529cebe764..637ecdd4d5 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) == "<img") {- console.log("Found img tag");+ //console.log("Found img tag");let dataImagePos = curTag.indexOf('data:image/');if (dataImagePos != -1) {let imageType = curTag.substr(dataImagePos + 11, 3);- console.log("image type: " + imageType);+ //console.log("image type: " + imageType);let dataStart = curTag.substr(dataImagePos + 22);let endOfDataPos = dataStart.indexOf('"');if (endOfDataPos != -1) {- console.log("Found the end of image data");+ //console.log("Found the end of image data");let imageData = dataStart.substr(0, endOfDataPos);@@ -161,7 +161,7 @@contents = contents.substr(0, index) + contents.substr(index + curTag.length);- console.log("Parsed image: " + imageData.substr(0, 100));+ //console.log("Parsed image: " + imageData.substr(0, 100));found = true;}@@ -178,7 +178,7 @@lnk_text: match[2]});- console.log("Found link with text: " + match[2] + ", targetting: " + match[1]);+ //console.log("Found link with text: " + match[2] + ", targetting: " + match[1]);contents = contents.substr(0, index) + match[2] + contents.substr(index + match[0].length);@@ -232,9 +232,7 @@$(function(){$.get(baseUrl + 'tree').then(notes => {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;