From 001d91172a571026f159a0d31f08f0c50e2b0848 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 27 Aug 2017 10:30:32 -0400 Subject: [PATCH] better handling of renames in recent notes list (but hacky and not perfect) --- static/js/init.js | 14 ++++++++++---- static/js/note.js | 15 ++++++++------- static/js/tree.js | 8 ++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/static/js/init.js b/static/js/init.js index 7e571acb8..08077a490 100644 --- a/static/js/init.js +++ b/static/js/init.js @@ -26,12 +26,18 @@ $(document).bind('keydown', 'alt+q', function() { recentNotesSelectBox.find('option').remove(); // remove the current note - let recNotes = recentNotes.filter(note => note.noteId !== globalNote.detail.note_id); + let recNotes = recentNotes.filter(note => note !== globalNote.detail.note_id); + + $.each(recNotes, function(key, valueNoteId) { + let noteTitle = globalNoteNames[valueNoteId]; + + if (!noteTitle) { + return; + } - $.each(recNotes, function(key, value) { let option = $("") - .attr("value", value.noteId) - .text(value.noteTitle); + .attr("value", valueNoteId) + .text(noteTitle); // select the first one (most recent one) by default if (key === 0) { diff --git a/static/js/note.js b/static/js/note.js index cdbfc0627..a83ae8cc1 100644 --- a/static/js/note.js +++ b/static/js/note.js @@ -42,6 +42,8 @@ function saveNoteIfChanged(callback) { note.detail.note_title = title; + globalNoteNames[note.detail.note_id] = title; + $.ajax({ url: baseUrl + 'notes/' + note.detail.note_id, type: 'PUT', @@ -123,6 +125,8 @@ function createNote(node, parentKey, target) { "note_id": result.note_id }; + globalNoteNames[result.note_id] = newNoteName; + newNoteCreated = true; if (target === 'after') { @@ -164,27 +168,24 @@ function loadNote(noteId) { $(window).resize(); // to trigger resizing of editor - addRecentNote(noteId, note.detail.note_id, note.detail.note_title); + addRecentNote(noteId, note.detail.note_id); noteChangeDisabled = false; }); } -function addRecentNote(noteTreeId, noteContentId, noteTitle) { +function addRecentNote(noteTreeId, noteContentId) { const origDate = new Date(); setTimeout(function() { // we include the note into recent list only if the user stayed on the note at least 5 seconds if (noteTreeId === globalNote.detail.note_id || noteContentId === globalNote.detail.note_id) { // if it's already there, remove the note - recentNotes = recentNotes.filter(note => note.noteId !== noteTreeId); + recentNotes = recentNotes.filter(note => note !== noteTreeId); console.log("added after " + (new Date().getTime() - origDate.getTime())); - recentNotes.unshift({ - noteId: noteTreeId, - noteTitle: noteTitle - }); + recentNotes.unshift(noteTreeId); } }, 1500); } diff --git a/static/js/tree.js b/static/js/tree.js index 6008a2773..f399de444 100644 --- a/static/js/tree.js +++ b/static/js/tree.js @@ -18,6 +18,11 @@ const keybindings = { node.getParent().renderTitle(); } + delete globalNoteNames[node.key]; + + // remove from recent notes + recentNotes = recentNotes.filter(note => note !== node.key); + node.remove(); } }); @@ -89,6 +94,7 @@ const keybindings = { } }; +const globalNoteNames = {}; $(function(){ $.get(baseUrl + 'tree').then(resp => { @@ -101,6 +107,8 @@ $(function(){ function copyTitle(notes) { for (let note of notes) { + globalNoteNames[note.note_id] = note.note_title; + note.title = note.note_title; if (note.is_clone) {