fix recent notes issues

This commit is contained in:
azivner 2018-06-06 22:38:36 -04:00
parent aee60c444f
commit 0e69f0c079
4 changed files with 39 additions and 53 deletions

View file

@ -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
};

View file

@ -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('/')) {

View file

@ -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) {

View file

@ -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
};