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 treeService from '../services/tree.js';
import messagingService from '../services/messaging.js';
import server from '../services/server.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 $dialog = $("#recent-notes-dialog");
const $searchInput = $('#recent-notes-search-input'); 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) { function addRecentNote(branchId, notePath) {
setTimeout(async () => { setTimeout(async () => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds // we include the note into recent list only if the user stayed on the note at least 5 seconds
if (notePath && notePath === treeService.getCurrentNotePath()) { if (notePath && notePath === treeService.getCurrentNotePath()) {
const result = await server.put('recent-notes/' + branchId + '/' + encodeURIComponent(notePath)); const result = await server.put('recent-notes/' + branchId + '/' + encodeURIComponent(notePath));
list = result.map(r => r.notePath);
} }
}, 1500); }, 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() { async function showDialog() {
glob.activeDialog = $dialog; glob.activeDialog = $dialog;
@ -54,16 +25,17 @@ async function showDialog() {
$searchInput.val(''); $searchInput.val('');
// remove the current note const result = await server.get('recent-notes');
const recNotes = list.filter(note => note !== treeService.getCurrentNotePath());
const items = [];
for (const notePath of recNotes) { // remove the current note
items.push({ const recNotes = result.filter(note => note.notePath !== treeService.getCurrentNotePath());
label: await getNoteTitle(notePath),
value: notePath const items = recNotes.map(rn => {
return {
label: rn.title,
value: rn.notePath
};
}); });
}
$searchInput.autocomplete({ $searchInput.autocomplete({
source: items, 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 { export default {
showDialog, showDialog,
addRecentNote, addRecentNote
reload
}; };

View File

@ -52,6 +52,15 @@ async function getNotePathTitle(notePath) {
const titlePath = []; 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'; let parentNoteId = 'root';
for (const noteId of notePath.split('/')) { for (const noteId of notePath.split('/')) {

View File

@ -1,12 +1,12 @@
"use strict"; "use strict";
const repository = require('../../services/repository'); const repository = require('../../services/repository');
const dateUtils = require('../../services/date_utils');
const optionService = require('../../services/options'); const optionService = require('../../services/options');
const RecentNote = require('../../entities/recent_note'); const RecentNote = require('../../entities/recent_note');
const noteCacheService = require('../../services/note_cache');
async function getRecentNotes() { async function getRecentNotes() {
return await repository.getEntities(` const recentNotes = await repository.getEntities(`
SELECT SELECT
recent_notes.* recent_notes.*
FROM FROM
@ -18,6 +18,12 @@ async function getRecentNotes() {
ORDER BY ORDER BY
dateCreated DESC dateCreated DESC
LIMIT 200`); LIMIT 200`);
for (const rn of recentNotes) {
rn.title = noteCacheService.getNoteTitleForPath(rn.notePath.split('/'));
}
return recentNotes;
} }
async function addRecentNote(req) { async function addRecentNote(req) {

View File

@ -161,6 +161,15 @@ function getNoteTitle(noteId, parentNoteId) {
function getNoteTitleForPath(path) { function getNoteTitleForPath(path) {
const titles = []; const titles = [];
if (path[0] === 'root') {
if (path.length === 1) {
return getNoteTitle('root');
}
else {
path = path.slice(1);
}
}
let parentNoteId = 'root'; let parentNoteId = 'root';
for (const noteId of path) { for (const noteId of path) {
@ -279,5 +288,6 @@ sqlInit.dbReady.then(() => utils.stopWatch("Autocomplete load", load));
module.exports = { module.exports = {
findNotes, findNotes,
getNotePath getNotePath,
getNoteTitleForPath
}; };