trilium/src/public/javascripts/services/tree.js

568 lines
16 KiB
JavaScript
Raw Normal View History

import contextMenuService from './context_menu.js';
import dragAndDropSetup from './drag_and_drop.js';
import linkService from './link.js';
import messagingService from './messaging.js';
import noteDetailService from './note_detail.js';
import protectedSessionHolder from './protected_session_holder.js';
2018-04-02 08:33:10 +08:00
import treeChangesService from './branches.js';
import treeUtils from './tree_utils.js';
import utils from './utils.js';
import server from './server.js';
import recentNotesDialog from '../dialogs/recent_notes.js';
import treeCache from './tree_cache.js';
2018-03-26 09:29:35 +08:00
import infoService from "./info.js";
2018-03-27 11:18:50 +08:00
import treeBuilder from "./tree_builder.js";
import treeKeyBindings from "./tree_keybindings.js";
2018-03-26 11:25:17 +08:00
import Branch from '../entities/branch.js';
2018-04-01 10:23:40 +08:00
import NoteShort from '../entities/note_short.js';
const $tree = $("#tree");
const $parentList = $("#parent-list");
const $parentListList = $("#parent-list-inner");
const $createTopLevelNoteButton = $("#create-top-level-note-button");
const $collapseTreeButton = $("#collapse-tree-button");
const $scrollToCurrentNoteButton = $("#scroll-to-current-note-button");
let startNotePath = null;
2018-03-13 08:00:19 +08:00
// note that if you want to access data like noteId or isProtected, you need to go into "data" property
function getCurrentNode() {
return $tree.fancytree("getActiveNode");
}
2017-11-24 09:12:39 +08:00
function getCurrentNotePath() {
const node = getCurrentNode();
return treeUtils.getNotePath(node);
}
2017-11-28 23:17:30 +08:00
async function getNodesByBranchId(branchId) {
utils.assertArguments(branchId);
2017-11-28 23:17:30 +08:00
const branch = await treeCache.getBranch(branchId);
return getNodesByNoteId(branch.noteId).filter(node => node.data.branchId === branchId);
}
2017-11-24 09:12:39 +08:00
function getNodesByNoteId(noteId) {
utils.assertArguments(noteId);
const list = getTree().getNodesByRef(noteId);
return list ? list : []; // if no nodes with this refKey are found, fancy tree returns null
}
2017-11-28 23:17:30 +08:00
async function setPrefix(branchId, prefix) {
utils.assertArguments(branchId);
2017-11-28 23:17:30 +08:00
const branch = await treeCache.getBranch(branchId);
2018-03-13 11:14:09 +08:00
branch.prefix = prefix;
for (const node of await getNodesByBranchId(branchId)) {
await setNodeTitleWithPrefix(node);
}
}
2018-03-13 11:14:09 +08:00
async function setNodeTitleWithPrefix(node) {
const noteTitle = await treeUtils.getNoteTitle(node.data.noteId);
const branch = await treeCache.getBranch(node.data.branchId);
2018-03-13 11:14:09 +08:00
const prefix = branch.prefix;
2017-11-28 23:17:30 +08:00
const title = (prefix ? (prefix + " - ") : "") + noteTitle;
node.setTitle(utils.escapeHtml(title));
}
2018-03-25 22:06:14 +08:00
async function expandToNote(notePath, expandOpts) {
utils.assertArguments(notePath);
const runPath = await getRunPath(notePath);
2017-12-04 06:46:56 +08:00
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
2017-12-04 06:46:56 +08:00
let parentNoteId = 'root';
2017-12-04 06:46:56 +08:00
for (const childNoteId of runPath) {
const node = getNodesByNoteId(childNoteId).find(node => node.data.parentNoteId === parentNoteId);
2017-12-04 06:46:56 +08:00
if (childNoteId === noteId) {
return node;
2017-12-04 06:46:56 +08:00
}
else {
await node.setExpanded(true, expandOpts);
}
parentNoteId = childNoteId;
}
}
async function activateNode(notePath) {
utils.assertArguments(notePath);
const node = await expandToNote(notePath);
await node.setActive();
clearSelectedNodes();
}
2017-12-04 06:46:56 +08:00
/**
* Accepts notePath and tries to resolve it. Part of the path might not be valid because of note moving (which causes
* path change) or other corruption, in that case this will try to get some other valid path to the correct note.
*/
async function getRunPath(notePath) {
utils.assertArguments(notePath);
const path = notePath.split("/").reverse();
path.push('root');
2017-11-19 21:47:22 +08:00
const effectivePath = [];
let childNoteId = null;
let i = 0;
while (true) {
if (i >= path.length) {
break;
}
const parentNoteId = path[i++];
if (childNoteId !== null) {
const child = await treeCache.getNote(childNoteId);
const parents = await child.getParentNotes();
if (!parents) {
messagingService.logError("No parents found for " + childNoteId);
return;
}
2017-11-27 10:00:42 +08:00
if (!parents.some(p => p.noteId === parentNoteId)) {
console.log(utils.now(), "Did not find parent " + parentNoteId + " for child " + childNoteId);
if (parents.length > 0) {
console.log(utils.now(), "Available parents:", parents);
const someNotePath = await getSomeNotePath(parents[0]);
if (someNotePath) { // in case it's root the path may be empty
const pathToRoot = someNotePath.split("/").reverse();
for (const noteId of pathToRoot) {
effectivePath.push(noteId);
}
}
2017-11-19 21:47:22 +08:00
break;
}
else {
messagingService.logError("No parents, can't activate node.");
return;
}
2017-11-19 21:47:22 +08:00
}
}
2017-12-04 06:46:56 +08:00
if (parentNoteId === 'root') {
break;
}
else {
effectivePath.push(parentNoteId);
childNoteId = parentNoteId;
}
2017-11-19 21:47:22 +08:00
}
return effectivePath.reverse();
}
async function showParentList(noteId, node) {
utils.assertArguments(noteId, node);
const note = await treeCache.getNote(noteId);
const parents = await note.getParentNotes();
if (!parents.length) {
2018-03-26 09:29:35 +08:00
infoService.throwError("Can't find parents for noteId=" + noteId);
}
if (parents.length <= 1) {
$parentList.hide();
}
else {
$parentList.show();
$parentListList.empty();
for (const parentNote of parents) {
const parentNotePath = await getSomeNotePath(parentNote);
// this is to avoid having root notes leading '/'
const notePath = parentNotePath ? (parentNotePath + '/' + noteId) : noteId;
const title = await treeUtils.getNotePathTitle(notePath);
let item;
if (node.getParent().data.noteId === parentNote.noteId) {
item = $("<span/>").attr("title", "Current note").append(title);
}
else {
item = linkService.createNoteLink(notePath, title);
}
$parentListList.append($("<li/>").append(item));
}
}
}
async function getSomeNotePath(note) {
utils.assertArguments(note);
const path = [];
let cur = note;
while (cur.noteId !== 'root') {
path.push(cur.noteId);
const parents = await cur.getParentNotes();
if (!parents.length) {
2018-03-26 09:29:35 +08:00
infoService.throwError("Can't find parents for " + cur);
}
cur = parents[0];
}
return path.reverse().join('/');
}
2017-11-05 07:28:49 +08:00
async function setExpandedToServer(branchId, isExpanded) {
utils.assertArguments(branchId);
2017-11-05 07:28:49 +08:00
const expandedNum = isExpanded ? 1 : 0;
2018-04-02 08:33:10 +08:00
await server.put('branches/' + branchId + '/expanded/' + expandedNum);
}
function setCurrentNotePathToHash(node) {
utils.assertArguments(node);
const currentNotePath = treeUtils.getNotePath(node);
const currentBranchId = node.data.branchId;
document.location.hash = currentNotePath;
recentNotesDialog.addRecentNote(currentBranchId, currentNotePath);
}
function getSelectedNodes(stopOnParents = false) {
return getTree().getSelectedNodes(stopOnParents);
}
function clearSelectedNodes() {
for (const selectedNode of getSelectedNodes()) {
selectedNode.setSelected(false);
}
const currentNode = getCurrentNode();
if (currentNode) {
currentNode.setSelected(true);
}
}
2017-11-05 07:28:49 +08:00
async function treeInitialized() {
const noteId = treeUtils.getNoteIdFromNotePath(startNotePath);
2018-03-26 10:37:02 +08:00
if (!await treeCache.getNote(noteId)) {
// note doesn't exist so don't try to activate it
startNotePath = null;
}
if (startNotePath) {
await activateNode(startNotePath);
// looks like this this doesn't work when triggered immediatelly after activating node
// so waiting a second helps
setTimeout(scrollToCurrentNote, 1000);
}
}
function initFancyTree(branch) {
utils.assertArguments(branch);
$tree.fancytree({
autoScroll: true,
keyboard: false, // we takover keyboard handling in the hotkeys plugin
extensions: ["hotkeys", "filter", "dnd", "clones"],
source: branch,
scrollParent: $("#tree"),
click: (event, data) => {
const targetType = data.targetType;
const node = data.node;
if (targetType === 'title' || targetType === 'icon') {
if (!event.ctrlKey) {
node.setActive();
node.setSelected(true);
clearSelectedNodes();
}
else {
node.setSelected(!node.isSelected());
2018-01-04 11:49:53 +08:00
}
return false;
}
},
activate: (event, data) => {
const node = data.node.data;
setCurrentNotePathToHash(data.node);
noteDetailService.switchToNote(node.noteId);
showParentList(node.noteId, data.node);
},
expand: (event, data) => setExpandedToServer(data.node.data.branchId, true),
collapse: (event, data) => setExpandedToServer(data.node.data.branchId, false),
init: (event, data) => treeInitialized(), // don't collapse to short form
hotkeys: {
keydown: treeKeyBindings
},
filter: {
autoApply: true, // Re-apply last filter if lazy data is loaded
autoExpand: true, // Expand all branches that contain matches while filtered
counter: false, // Show a badge with number of matching child nodes near parent icons
fuzzy: false, // Match single characters in order, e.g. 'fb' will match 'FooBar'
hideExpandedCounter: true, // Hide counter badge if parent is expanded
hideExpanders: false, // Hide expanders if all child nodes are hidden by filter
highlight: true, // Highlight matches by wrapping inside <mark> tags
leavesOnly: false, // Match end nodes only
nodata: true, // Display a 'no data' status node if result is empty
mode: "hide" // Grayout unmatched nodes (pass "hide" to remove unmatched node instead)
},
dnd: dragAndDropSetup,
lazyLoad: function(event, data) {
const noteId = data.node.data.noteId;
2018-03-27 11:18:50 +08:00
data.result = treeCache.getNote(noteId).then(note => treeBuilder.prepareBranch(note));
},
clones: {
highlightActiveClones: true
}
});
2017-11-05 07:28:49 +08:00
$tree.contextmenu(contextMenuService.contextMenuOptions);
}
function getTree() {
return $tree.fancytree('getTree');
}
2018-03-13 11:14:09 +08:00
async function reload() {
const notes = await loadTree();
2018-03-13 11:14:09 +08:00
// this will also reload the note content
await getTree().reload(notes);
}
function getNotePathFromAddress() {
return document.location.hash.substr(1); // strip initial #
}
async function loadTree() {
const resp = await server.get('tree');
2018-04-03 09:47:46 +08:00
startNotePath = resp.startNotePath;
2018-03-13 11:14:09 +08:00
if (document.location.hash) {
startNotePath = getNotePathFromAddress();
}
2018-03-27 11:18:50 +08:00
return await treeBuilder.prepareTree(resp.notes, resp.branches);
}
function collapseTree(node = null) {
if (!node) {
node = $tree.fancytree("getRootNode");
}
node.setExpanded(false);
2017-11-05 07:28:49 +08:00
node.visit(node => node.setExpanded(false));
}
2017-11-05 07:28:49 +08:00
function scrollToCurrentNote() {
const node = getCurrentNode();
if (node) {
node.makeVisible({scrollIntoView: true});
node.setFocus();
2017-11-05 07:28:49 +08:00
}
}
2017-11-05 07:28:49 +08:00
function setBranchBackgroundBasedOnProtectedStatus(noteId) {
getNodesByNoteId(noteId).map(node => node.toggleClass("protected", !!node.data.isProtected));
}
2017-11-05 07:28:49 +08:00
function setProtected(noteId, isProtected) {
getNodesByNoteId(noteId).map(node => node.data.isProtected = isProtected);
2017-11-05 07:28:49 +08:00
setBranchBackgroundBasedOnProtectedStatus(noteId);
}
2017-11-05 07:28:49 +08:00
async function setNoteTitle(noteId, title) {
utils.assertArguments(noteId);
2018-03-26 10:37:02 +08:00
const note = await treeCache.getNote(noteId);
note.title = title;
for (const clone of getNodesByNoteId(noteId)) {
await setNodeTitleWithPrefix(clone);
}
}
async function createNewTopLevelNote() {
const rootNode = $tree.fancytree("getRootNode");
await createNote(rootNode, "root", "into");
}
async function createNote(node, parentNoteId, target, isProtected) {
utils.assertArguments(node, parentNoteId, target);
// if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted
// but this is quite weird since user doesn't see WHERE the note is being created so it shouldn't occur often
if (!isProtected || !protectedSessionHolder.isProtectedSessionAvailable()) {
isProtected = false;
}
const newNoteName = "new note";
2018-03-25 12:20:55 +08:00
2018-04-01 23:42:12 +08:00
const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
title: newNoteName,
target: target,
target_branchId: node.data.branchId,
isProtected: isProtected
});
2018-03-25 12:20:55 +08:00
2018-04-01 23:42:12 +08:00
const noteEntity = new NoteShort(treeCache, note);
const branchEntity = new Branch(treeCache, branch);
2018-04-01 23:42:12 +08:00
treeCache.add(noteEntity, branchEntity);
noteDetailService.newNoteCreated();
2018-01-17 12:22:13 +08:00
const newNode = {
title: newNoteName,
2018-04-01 23:42:12 +08:00
noteId: branchEntity.noteId,
parentNoteId: parentNoteId,
2018-04-01 23:42:12 +08:00
refKey: branchEntity.noteId,
branchId: branchEntity.branchId,
isProtected: isProtected,
2018-04-01 23:42:12 +08:00
extraClasses: await treeBuilder.getExtraClasses(noteEntity)
};
if (target === 'after') {
await node.appendSibling(newNode).setActive(true);
}
else if (target === 'into') {
if (!node.getChildren() && node.isFolder()) {
await node.setExpanded();
}
2018-01-17 12:22:13 +08:00
else {
node.addChildren(newNode);
2018-01-17 12:22:13 +08:00
}
await node.getLastChild().setActive(true);
node.folder = true;
node.renderTitle();
}
else {
2018-03-26 09:29:35 +08:00
infoService.throwError("Unrecognized target: " + target);
}
clearSelectedNodes(); // to unmark previously active node
2018-03-26 09:29:35 +08:00
infoService.showMessage("Created!");
}
async function sortAlphabetically(noteId) {
await server.put('notes/' + noteId + '/sort');
await reload();
}
2017-12-19 12:41:13 +08:00
async function showTree() {
const tree = await loadTree();
initFancyTree(tree);
}
messagingService.subscribeToMessages(syncData => {
if (syncData.some(sync => sync.entityName === 'branches')
|| syncData.some(sync => sync.entityName === 'notes')) {
console.log(utils.now(), "Reloading tree because of background changes");
reload();
}
});
2018-03-26 10:37:02 +08:00
utils.bindShortcut('ctrl+o', () => {
const node = getCurrentNode();
const parentNoteId = node.data.parentNoteId;
const isProtected = treeUtils.getParentProtectedStatus(node);
2017-12-19 12:41:13 +08:00
createNote(node, parentNoteId, 'after', isProtected);
});
2017-12-19 12:41:13 +08:00
2018-03-26 10:37:02 +08:00
utils.bindShortcut('ctrl+p', () => {
const node = getCurrentNode();
2017-12-19 12:41:13 +08:00
createNote(node, node.data.noteId, 'into', node.data.isProtected);
});
2018-03-26 10:37:02 +08:00
utils.bindShortcut('ctrl+del', () => {
const node = getCurrentNode();
treeChangesService.deleteNodes([node]);
});
2018-03-26 10:37:02 +08:00
utils.bindShortcut('ctrl+.', scrollToCurrentNote);
$(window).bind('hashchange', function() {
const notePath = getNotePathFromAddress();
if (getCurrentNotePath() !== notePath) {
console.log("Switching to " + notePath + " because of hash change");
activateNode(notePath);
}
});
2018-03-27 10:11:45 +08:00
utils.bindShortcut('alt+c', () => collapseTree()); // don't use shortened form since collapseTree() accepts argument
$createTopLevelNoteButton.click(createNewTopLevelNote);
$collapseTreeButton.click(collapseTree);
$scrollToCurrentNoteButton.click(scrollToCurrentNote);
export default {
reload,
collapseTree,
scrollToCurrentNote,
setBranchBackgroundBasedOnProtectedStatus,
setProtected,
expandToNote,
activateNode,
getCurrentNode,
getCurrentNotePath,
setCurrentNotePathToHash,
setNoteTitle,
setPrefix,
createNewTopLevelNote,
createNote,
getSelectedNodes,
clearSelectedNodes,
sortAlphabetically,
showTree
};