From 0e69f0c079ddb820ba00330863f89707ef0721cd Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 6 Jun 2018 22:38:36 -0400 Subject: [PATCH] fix recent notes issues --- .../javascripts/dialogs/recent_notes.js | 61 ++++--------------- src/public/javascripts/services/tree_utils.js | 9 +++ src/routes/api/recent_notes.js | 10 ++- src/services/note_cache.js | 12 +++- 4 files changed, 39 insertions(+), 53 deletions(-) diff --git a/src/public/javascripts/dialogs/recent_notes.js b/src/public/javascripts/dialogs/recent_notes.js index a60a692f1..99b386924 100644 --- a/src/public/javascripts/dialogs/recent_notes.js +++ b/src/public/javascripts/dialogs/recent_notes.js @@ -1,47 +1,18 @@ import treeService from '../services/tree.js'; -import messagingService from '../services/messaging.js'; import server from '../services/server.js'; -import utils from "../services/utils.js"; -import treeUtils from "../services/tree_utils.js"; const $dialog = $("#recent-notes-dialog"); const $searchInput = $('#recent-notes-search-input'); -// list of recent note paths -let list = []; - -async function reload() { - const result = await server.get('recent-notes'); - - list = result.map(r => r.notePath); -} - function addRecentNote(branchId, notePath) { setTimeout(async () => { // we include the note into recent list only if the user stayed on the note at least 5 seconds if (notePath && notePath === treeService.getCurrentNotePath()) { const result = await server.put('recent-notes/' + branchId + '/' + encodeURIComponent(notePath)); - - list = result.map(r => r.notePath); } }, 1500); } -async function getNoteTitle(notePath) { - let noteTitle; - - try { - noteTitle = await treeUtils.getNotePathTitle(notePath); - } - catch (e) { - noteTitle = "[error - can't find note title]"; - - messagingService.logError("Could not find title for notePath=" + notePath + ", stack=" + e.stack); - } - - return noteTitle; -} - async function showDialog() { glob.activeDialog = $dialog; @@ -54,16 +25,17 @@ async function showDialog() { $searchInput.val(''); - // remove the current note - const recNotes = list.filter(note => note !== treeService.getCurrentNotePath()); - const items = []; + const result = await server.get('recent-notes'); - for (const notePath of recNotes) { - items.push({ - label: await getNoteTitle(notePath), - value: notePath - }); - } + // remove the current note + const recNotes = result.filter(note => note.notePath !== treeService.getCurrentNotePath()); + + const items = recNotes.map(rn => { + return { + label: rn.title, + value: rn.notePath + }; + }); $searchInput.autocomplete({ source: items, @@ -96,18 +68,7 @@ async function showDialog() { }); } -setTimeout(reload, 100); - -messagingService.subscribeToMessages(syncData => { - if (syncData.some(sync => sync.entityName === 'recent_notes')) { - console.log(utils.now(), "Reloading recent notes because of background changes"); - - reload(); - } -}); - export default { showDialog, - addRecentNote, - reload + addRecentNote }; \ No newline at end of file diff --git a/src/public/javascripts/services/tree_utils.js b/src/public/javascripts/services/tree_utils.js index 5e8686be8..548c9fcc5 100644 --- a/src/public/javascripts/services/tree_utils.js +++ b/src/public/javascripts/services/tree_utils.js @@ -52,6 +52,15 @@ async function getNotePathTitle(notePath) { const titlePath = []; + if (notePath.startsWith('root/')) { + notePath = notePath.substr(5); + } + + // special case when we want just root's title + if (notePath === 'root') { + return await getNoteTitle(notePath); + } + let parentNoteId = 'root'; for (const noteId of notePath.split('/')) { diff --git a/src/routes/api/recent_notes.js b/src/routes/api/recent_notes.js index 14d779f5b..90adc708d 100644 --- a/src/routes/api/recent_notes.js +++ b/src/routes/api/recent_notes.js @@ -1,12 +1,12 @@ "use strict"; const repository = require('../../services/repository'); -const dateUtils = require('../../services/date_utils'); const optionService = require('../../services/options'); const RecentNote = require('../../entities/recent_note'); +const noteCacheService = require('../../services/note_cache'); async function getRecentNotes() { - return await repository.getEntities(` + const recentNotes = await repository.getEntities(` SELECT recent_notes.* FROM @@ -18,6 +18,12 @@ async function getRecentNotes() { ORDER BY dateCreated DESC LIMIT 200`); + + for (const rn of recentNotes) { + rn.title = noteCacheService.getNoteTitleForPath(rn.notePath.split('/')); + } + + return recentNotes; } async function addRecentNote(req) { diff --git a/src/services/note_cache.js b/src/services/note_cache.js index 34223834b..29e916a4f 100644 --- a/src/services/note_cache.js +++ b/src/services/note_cache.js @@ -161,6 +161,15 @@ function getNoteTitle(noteId, parentNoteId) { function getNoteTitleForPath(path) { const titles = []; + if (path[0] === 'root') { + if (path.length === 1) { + return getNoteTitle('root'); + } + else { + path = path.slice(1); + } + } + let parentNoteId = 'root'; for (const noteId of path) { @@ -279,5 +288,6 @@ sqlInit.dbReady.then(() => utils.stopWatch("Autocomplete load", load)); module.exports = { findNotes, - getNotePath + getNotePath, + getNoteTitleForPath }; \ No newline at end of file