Skip to content

Commit 7bebf7a

Browse files
committedJun 11, 2017
fixes to the positioning after creating notes and moving them around
1 parent cfd948b commit 7bebf7a

File tree

9 files changed

+531
-701
lines changed

9 files changed

+531
-701
lines changed
 

‎.pylintrc

+425
Large diffs are not rendered by default.

‎LICENSE

-661
This file was deleted.

‎TODO

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- drag and drop for notes (currently only keyboard)
2+
- authentication with credentials in the config file (password hashed and salted)
3+
- sync with sync server
4+
- remembering open and closed folders
5+
- link to individual notes (and changing the URL to current one, probably through # anchor)
6+
- when loading the app, open last viewed note

‎app.py

+60-6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ def put(self, note_id):
9999

100100
return {}
101101

102+
def delete(self, note_id):
103+
children = getResults("select note_id from notes_tree where note_pid = ?", [note_id])
104+
105+
for child in children:
106+
self.delete(child['note_id'])
107+
108+
delete("notes_tree", note_id)
109+
delete("notes", note_id)
110+
111+
conn.commit()
112+
102113
api.add_resource(Notes, '/notes/<string:note_id>')
103114

104115
class NotesChildren(Resource):
@@ -107,7 +118,29 @@ def post(self, parent_note_id):
107118

108119
noteId = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(22))
109120

110-
now = math.floor(time.time());
121+
if parent_note_id == "root":
122+
parent_note_id = ""
123+
124+
new_note_pos = 0
125+
126+
if note['target'] == 'into':
127+
res = getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?', [parent_note_id])
128+
max_note_pos = res['max_note_pos']
129+
130+
if max_note_pos is None: # no children yet
131+
new_note_pos = 0
132+
else:
133+
new_note_pos = max_note_pos + 1
134+
elif note['target'] == 'after':
135+
after_note = getSingleResult('select note_pos from notes_tree where note_id = ?', [note['target_note_id']])
136+
137+
new_note_pos = after_note['note_pos'] + 1
138+
139+
execute('update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?', [parent_note_id, after_note['note_pos']])
140+
else:
141+
raise Exception('Unknown target: ' + note['target'])
142+
143+
now = math.floor(time.time())
111144

112145
insert("notes", {
113146
'note_id': noteId,
@@ -120,13 +153,10 @@ def post(self, parent_note_id):
120153
'is_finished': 0
121154
})
122155

123-
if parent_note_id == "root":
124-
parent_note_id = ""
125-
126156
insert("notes_tree", {
127157
'note_id': noteId,
128158
'note_pid': parent_note_id,
129-
'note_pos': 0,
159+
'note_pos': new_note_pos,
130160
'is_expanded': 0
131161
})
132162

@@ -143,15 +173,39 @@ def put(self, note_id, after_note_id):
143173
after_note = getSingleResult("select * from notes_tree where note_id = ?", [after_note_id])
144174

145175
if after_note <> None:
176+
execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?", [after_note['note_pid'], after_note['note_pos']])
177+
146178
execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [after_note['note_pid'], after_note['note_pos'] + 1, note_id])
147179

148180
conn.commit()
149181

150182
api.add_resource(MoveAfterNote, '/notes/<string:note_id>/moveAfter/<string:after_note_id>')
151183

184+
class MoveBeforeNote(Resource):
185+
def put(self, note_id, before_note_id):
186+
before_note = getSingleResult("select * from notes_tree where note_id = ?", [before_note_id])
187+
188+
if before_note <> None:
189+
execute("update notes_tree set note_pos = note_pos + 1 where note_id = ?", [before_note_id])
190+
191+
execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [before_note['note_pid'], before_note['note_pos'], note_id])
192+
193+
conn.commit()
194+
195+
api.add_resource(MoveBeforeNote, '/notes/<string:note_id>/moveBefore/<string:before_note_id>')
196+
152197
class MoveToNote(Resource):
153198
def put(self, note_id, parent_id):
154-
execute("update notes_tree set note_pid = ? where note_id = ?", [parent_id, note_id])
199+
res = getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?', [parent_id])
200+
max_note_pos = res['max_note_pos']
201+
new_note_pos = 0
202+
203+
if max_note_pos is None: # no children yet
204+
new_note_pos = 0
205+
else:
206+
new_note_pos = max_note_pos + 1
207+
208+
execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [parent_id, new_note_pos, note_id])
155209

156210
conn.commit()
157211

‎app.pyc

1.92 KB
Binary file not shown.

‎demo.ncdb

0 Bytes
Binary file not shown.

‎frontend/.babelrc

-7
This file was deleted.

‎frontend/README.md

-18
This file was deleted.

‎frontend/index.html

+40-9
Original file line numberDiff line numberDiff line change
@@ -258,22 +258,44 @@
258258
"insert": function(node) {
259259
let parentKey = (node.getParent() == null || node.getParent().key == "root_1") ? "root" : node.getParent().key;
260260

261-
createNote(parentKey);
261+
createNote(node, parentKey, 'after');
262262
},
263263
"shift+insert": function(node) {
264-
createNote(node.key);
264+
createNote(node, node.key, 'into');
265265
},
266266
"del": function(node) {
267-
node.remove();
267+
if (confirm('Are you sure you want to delete note "' + node.title + '"?')) {
268+
$.ajax({
269+
url: baseUrl + 'notes/' + node.key,
270+
type: 'DELETE',
271+
success: function(result) {
272+
node.remove();
273+
}
274+
});
275+
}
268276
},
269277
"shift+up": function(node) {
270278
if (node.getPrevSibling() != null) {
271-
node.moveTo(node.getPrevSibling(), 'before');
279+
$.ajax({
280+
url: baseUrl + 'notes/' + node.key + '/moveBefore/' + node.getPrevSibling().key,
281+
type: 'PUT',
282+
contentType: "application/json",
283+
success: function(result) {
284+
node.moveTo(node.getPrevSibling(), 'before');
285+
}
286+
});
272287
}
273288
},
274289
"shift+down": function(node) {
275290
if (node.getNextSibling() != null) {
276-
node.moveTo(node.getNextSibling(), 'after');
291+
$.ajax({
292+
url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getNextSibling().key,
293+
type: 'PUT',
294+
contentType: "application/json",
295+
success: function(result) {
296+
node.moveTo(node.getNextSibling(), 'after');
297+
}
298+
});
277299
}
278300
},
279301
"shift+left": function(node) {
@@ -325,22 +347,31 @@
325347
});
326348
}
327349

328-
function createNote(parentKey) {
350+
function createNote(node, parentKey, target) {
329351
let newNoteName = "new note";
330352

331353
$.ajax({
332354
url: baseUrl + 'notes/' + parentKey + '/children' ,
333355
type: 'POST',
334356
data: JSON.stringify({
335-
note_title: newNoteName
357+
note_title: newNoteName,
358+
target: target,
359+
target_note_id: node.key
336360
}),
337361
contentType: "application/json",
338362
success: function(result) {
339-
node.appendSibling({
363+
let newNode = {
340364
"title": newNoteName,
341365
"key": result.note_id,
342366
"note_id": result.note_id
343-
}).setActive(true);
367+
};
368+
369+
if (target == 'after') {
370+
node.appendSibling(newNode).setActive(true);
371+
}
372+
else {
373+
node.addChildren(newNode).setActive(true);
374+
}
344375

345376
message("Created!");
346377
}

0 commit comments

Comments
 (0)
Please sign in to comment.