mirror of
https://github.com/zadam/trilium.git
synced 2024-09-21 08:06:03 +08:00
fix recent notes issues
This commit is contained in:
parent
aee60c444f
commit
0e69f0c079
|
@ -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
|
||||
};
|
|
@ -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('/')) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
};
|
Loading…
Reference in a new issue