mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
creating new note, moving note from tree and into tree
This commit is contained in:
parent
4f7bb4f5d8
commit
cfd948bd5b
64
app.py
64
app.py
@ -3,6 +3,10 @@ import base64
|
|||||||
from flask import Flask, request, send_from_directory
|
from flask import Flask, request, send_from_directory
|
||||||
from flask_restful import Resource, Api
|
from flask_restful import Resource, Api
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
import time
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
def dict_factory(cursor, row):
|
def dict_factory(cursor, row):
|
||||||
d = {}
|
d = {}
|
||||||
@ -31,7 +35,8 @@ def insert(tablename, rec):
|
|||||||
keys = ','.join(rec.keys())
|
keys = ','.join(rec.keys())
|
||||||
question_marks = ','.join(list('?'*len(rec)))
|
question_marks = ','.join(list('?'*len(rec)))
|
||||||
values = tuple(rec.values())
|
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):
|
def delete(tablename, note_id):
|
||||||
execute("DELETE FROM " + tablename + " WHERE note_id = ?", [note_id])
|
execute("DELETE FROM " + tablename + " WHERE note_id = ?", [note_id])
|
||||||
@ -39,6 +44,7 @@ def delete(tablename, note_id):
|
|||||||
def execute(sql, params=[]):
|
def execute(sql, params=[]):
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
cursor.execute(sql, params)
|
cursor.execute(sql, params)
|
||||||
|
return cursor
|
||||||
|
|
||||||
def getResults(sql, params=[]):
|
def getResults(sql, params=[]):
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
@ -95,6 +101,62 @@ class Notes(Resource):
|
|||||||
|
|
||||||
api.add_resource(Notes, '/notes/<string:note_id>')
|
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):
|
class Tree(Resource):
|
||||||
def get(self):
|
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")
|
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")
|
||||||
|
@ -105,7 +105,7 @@
|
|||||||
|
|
||||||
let curTag = curContent.substr(0, endOfTag + 1);
|
let curTag = curContent.substr(0, endOfTag + 1);
|
||||||
|
|
||||||
console.log(contents);
|
//console.log(contents);
|
||||||
|
|
||||||
for (tagId in tags) {
|
for (tagId in tags) {
|
||||||
let tag = tags[tagId];
|
let tag = tags[tagId];
|
||||||
@ -134,21 +134,21 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (curTag.substr(0, 4) == "<img") {
|
if (curTag.substr(0, 4) == "<img") {
|
||||||
console.log("Found img tag");
|
//console.log("Found img tag");
|
||||||
|
|
||||||
let dataImagePos = curTag.indexOf('data:image/');
|
let dataImagePos = curTag.indexOf('data:image/');
|
||||||
|
|
||||||
if (dataImagePos != -1) {
|
if (dataImagePos != -1) {
|
||||||
let imageType = curTag.substr(dataImagePos + 11, 3);
|
let imageType = curTag.substr(dataImagePos + 11, 3);
|
||||||
|
|
||||||
console.log("image type: " + imageType);
|
//console.log("image type: " + imageType);
|
||||||
|
|
||||||
let dataStart = curTag.substr(dataImagePos + 22);
|
let dataStart = curTag.substr(dataImagePos + 22);
|
||||||
|
|
||||||
let endOfDataPos = dataStart.indexOf('"');
|
let endOfDataPos = dataStart.indexOf('"');
|
||||||
|
|
||||||
if (endOfDataPos != -1) {
|
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);
|
let imageData = dataStart.substr(0, endOfDataPos);
|
||||||
|
|
||||||
@ -161,7 +161,7 @@
|
|||||||
|
|
||||||
contents = contents.substr(0, index) + contents.substr(index + curTag.length);
|
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;
|
found = true;
|
||||||
}
|
}
|
||||||
@ -178,7 +178,7 @@
|
|||||||
lnk_text: match[2]
|
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);
|
contents = contents.substr(0, index) + match[2] + contents.substr(index + match[0].length);
|
||||||
|
|
||||||
@ -232,9 +232,7 @@
|
|||||||
$(function(){
|
$(function(){
|
||||||
$.get(baseUrl + 'tree').then(notes => {
|
$.get(baseUrl + 'tree').then(notes => {
|
||||||
function copyTitle(notes) {
|
function copyTitle(notes) {
|
||||||
for (key in notes) {
|
for (let note of notes) {
|
||||||
var note = notes[key];
|
|
||||||
|
|
||||||
note.title = note.note_title;
|
note.title = note.note_title;
|
||||||
note.key = note.note_id;
|
note.key = note.note_id;
|
||||||
|
|
||||||
@ -258,30 +256,52 @@
|
|||||||
hotkeys: {
|
hotkeys: {
|
||||||
keydown: {
|
keydown: {
|
||||||
"insert": function(node) {
|
"insert": function(node) {
|
||||||
node.appendSibling({
|
let parentKey = (node.getParent() == null || node.getParent().key == "root_1") ? "root" : node.getParent().key;
|
||||||
"title": "New!"
|
|
||||||
}).setActive(true);
|
createNote(parentKey);
|
||||||
},
|
},
|
||||||
"shift+insert": function(node) {
|
"shift+insert": function(node) {
|
||||||
node.addChildren({
|
createNote(node.key);
|
||||||
"title": "New!"
|
|
||||||
}).setActive(true);
|
|
||||||
},
|
},
|
||||||
"del": function(node) {
|
"del": function(node) {
|
||||||
node.remove();
|
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) {
|
"shift+left": function(node) {
|
||||||
if (node.getParent() != null) {
|
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) {
|
"shift+right": function(node) {
|
||||||
let prevSibling = node.getPrevSibling();
|
let prevSibling = node.getPrevSibling();
|
||||||
|
|
||||||
if (prevSibling != null) {
|
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;
|
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) {
|
function loadNote(noteId) {
|
||||||
$.get(baseUrl + 'notes/' + noteId).then(function(note) {
|
$.get(baseUrl + 'notes/' + noteId).then(function(note) {
|
||||||
globalNote = note;
|
globalNote = note;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user