trilium/src/public/javascripts/note_tree.js

924 lines
27 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 07:28:49 +08:00
const noteTree = (function() {
2018-02-15 12:31:20 +08:00
const $tree = $("#tree");
const $parentList = $("#parent-list");
const $parentListList = $("#parent-list-inner");
let instanceName = null; // should have better place
let startNotePath = null;
2017-11-24 09:12:39 +08:00
let notesTreeMap = {};
2017-11-19 21:47:22 +08:00
let parentToChildren = {};
let childToParents = {};
let parentChildToNoteTreeId = {};
2018-03-13 08:00:19 +08:00
let noteIdToNote = {};
let hiddenInAutocomplete = {};
2018-03-13 08:00:19 +08:00
function getNote(noteId) {
const note = noteIdToNote[noteId];
if (!note) {
throwError("Can't find title for noteId='" + noteId + "'");
}
return note;
}
function getNoteTreeId(parentNoteId, childNoteId) {
assertArguments(parentNoteId, childNoteId);
const key = parentNoteId + "-" + childNoteId;
// this can return undefined and client code should deal with it somehow
return parentChildToNoteTreeId[key];
}
function getNoteTitle(noteId, parentNoteId = null) {
assertArguments(noteId);
2018-03-13 08:00:19 +08:00
let title = getNote(noteId).title;
2017-11-20 01:06:48 +08:00
if (parentNoteId !== null) {
const noteTreeId = getNoteTreeId(parentNoteId, noteId);
if (noteTreeId) {
const noteTree = notesTreeMap[noteTreeId];
if (noteTree.prefix) {
title = noteTree.prefix + ' - ' + title;
}
}
}
2017-11-20 01:06:48 +08:00
return title;
}
2018-01-29 08:30:14 +08:00
// note that if you want to access data like noteId or isProtected, you need to go into "data" property
2017-11-24 09:12:39 +08:00
function getCurrentNode() {
2018-02-15 12:31:20 +08:00
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
function getNodesByNoteTreeId(noteTreeId) {
assertArguments(noteTreeId);
2017-11-28 23:17:30 +08:00
const noteTree = notesTreeMap[noteTreeId];
2018-01-29 08:30:14 +08:00
return getNodesByNoteId(noteTree.noteId).filter(node => node.data.noteTreeId === noteTreeId);
2017-11-28 23:17:30 +08:00
}
function getNodesByNoteId(noteId) {
assertArguments(noteId);
2017-12-13 11:26:40 +08:00
const list = getTree().getNodesByRef(noteId);
return list ? list : []; // if no nodes with this refKey are found, fancy tree returns null
2017-11-24 09:12:39 +08:00
}
2017-11-28 23:17:30 +08:00
function setPrefix(noteTreeId, prefix) {
assertArguments(noteTreeId);
2017-11-28 23:17:30 +08:00
notesTreeMap[noteTreeId].prefix = prefix;
getNodesByNoteTreeId(noteTreeId).map(node => {
node.data.prefix = prefix;
treeUtils.setNodeTitleWithPrefix(node);
});
}
function removeParentChildRelation(parentNoteId, childNoteId) {
assertArguments(parentNoteId, childNoteId);
const key = parentNoteId + "-" + childNoteId;
delete parentChildToNoteTreeId[key];
parentToChildren[parentNoteId] = parentToChildren[parentNoteId].filter(noteId => noteId !== childNoteId);
childToParents[childNoteId] = childToParents[childNoteId].filter(noteId => noteId !== parentNoteId);
}
function setParentChildRelation(noteTreeId, parentNoteId, childNoteId) {
assertArguments(noteTreeId, parentNoteId, childNoteId);
const key = parentNoteId + "-" + childNoteId;
parentChildToNoteTreeId[key] = noteTreeId;
if (!parentToChildren[parentNoteId]) {
parentToChildren[parentNoteId] = [];
}
parentToChildren[parentNoteId].push(childNoteId);
if (!childToParents[childNoteId]) {
childToParents[childNoteId] = [];
}
childToParents[childNoteId].push(parentNoteId);
}
function prepareNoteTree(notes) {
assertArguments(notes);
2017-11-19 21:47:22 +08:00
parentToChildren = {};
childToParents = {};
2017-11-24 09:12:39 +08:00
notesTreeMap = {};
for (const note of notes) {
2018-01-29 08:30:14 +08:00
notesTreeMap[note.noteTreeId] = note;
2018-03-13 08:00:19 +08:00
noteIdToNote[note.noteId] = {
title: note.title,
isProtected: note.isProtected,
type: note.type,
mime: note.mime
};
2017-11-20 01:06:48 +08:00
2018-03-13 08:00:19 +08:00
delete note.title; // this should not be used. Use noteIdToNote instead
2018-01-29 08:30:14 +08:00
setParentChildRelation(note.noteTreeId, note.parentNoteId, note.noteId);
}
return prepareNoteTreeInner('root');
2017-11-18 10:31:54 +08:00
}
2017-11-24 09:12:39 +08:00
function getExtraClasses(note) {
assertArguments(note);
const extraClasses = [];
2017-11-24 09:12:39 +08:00
2018-01-29 08:30:14 +08:00
if (note.isProtected) {
extraClasses.push("protected");
2017-11-24 09:12:39 +08:00
}
2018-01-29 08:30:14 +08:00
if (childToParents[note.noteId].length > 1) {
extraClasses.push("multiple-parents");
2017-11-24 09:12:39 +08:00
}
2018-03-11 22:49:22 +08:00
extraClasses.push(note.type);
return extraClasses.join(" ");
2017-11-24 09:12:39 +08:00
}
function prepareNoteTreeInner(parentNoteId) {
assertArguments(parentNoteId);
const childNoteIds = parentToChildren[parentNoteId];
2017-11-20 05:43:49 +08:00
if (!childNoteIds) {
2017-12-02 11:28:22 +08:00
messaging.logError("No children for " + parentNoteId + ". This shouldn't happen.");
2017-11-20 05:43:49 +08:00
return;
}
2017-11-18 10:31:54 +08:00
const noteList = [];
2017-11-24 09:12:39 +08:00
for (const noteId of childNoteIds) {
const noteTreeId = getNoteTreeId(parentNoteId, noteId);
const noteTree = notesTreeMap[noteTreeId];
2018-03-13 08:00:19 +08:00
const title = (noteTree.prefix ? (noteTree.prefix + " - ") : "") + getNote(noteTree.noteId).title;
2017-11-24 09:12:39 +08:00
const node = {
2018-01-29 08:30:14 +08:00
noteId: noteTree.noteId,
parentNoteId: noteTree.parentNoteId,
noteTreeId: noteTree.noteTreeId,
isProtected: noteTree.isProtected,
2017-11-27 11:40:14 +08:00
prefix: noteTree.prefix,
title: escapeHtml(title),
2017-11-24 09:12:39 +08:00
extraClasses: getExtraClasses(noteTree),
2018-01-29 08:30:14 +08:00
refKey: noteTree.noteId,
expanded: noteTree.isExpanded
2017-11-24 09:12:39 +08:00
};
if (parentToChildren[noteId] && parentToChildren[noteId].length > 0) {
node.folder = true;
2017-11-18 10:31:54 +08:00
if (node.expanded) {
2017-11-24 09:12:39 +08:00
node.children = prepareNoteTreeInner(noteId);
2017-11-18 10:31:54 +08:00
}
else {
node.lazy = true;
2017-11-18 10:31:54 +08:00
}
2017-11-05 07:28:49 +08:00
}
2017-11-18 10:31:54 +08:00
noteList.push(node);
2017-11-05 07:28:49 +08:00
}
2017-11-18 10:31:54 +08:00
return noteList;
2017-11-05 07:28:49 +08:00
}
async function expandToNote(notePath, expandOpts) {
assertArguments(notePath);
2017-12-04 06:46:56 +08:00
const runPath = getRunPath(notePath);
2017-12-04 06:46:56 +08:00
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
let parentNoteId = 'root';
for (const childNoteId of runPath) {
2018-01-29 08:30:14 +08:00
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);
2017-12-04 06:46:56 +08:00
}
parentNoteId = childNoteId;
}
}
async function activateNode(notePath) {
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.
*/
function getRunPath(notePath) {
assertArguments(notePath);
2017-11-19 21:47:22 +08:00
const path = notePath.split("/").reverse();
2017-12-04 06:46:56 +08:00
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 parents = childToParents[childNoteId];
2017-11-27 10:00:42 +08:00
if (!parents) {
2017-12-02 11:28:22 +08:00
messaging.logError("No parents found for " + childNoteId);
2017-11-27 10:00:42 +08:00
return;
}
if (!parents.includes(parentNoteId)) {
console.log(now(), "Did not find parent " + parentNoteId + " for child " + childNoteId);
if (parents.length > 0) {
console.log(now(), "Available parents:", parents);
const someNotePath = 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);
}
}
break;
}
else {
2017-12-02 11:28:22 +08:00
messaging.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;
2017-11-19 21:47:22 +08:00
}
else {
2017-12-04 06:46:56 +08:00
effectivePath.push(parentNoteId);
childNoteId = parentNoteId;
2017-11-19 21:47:22 +08:00
}
}
2017-12-04 06:46:56 +08:00
return effectivePath.reverse();
2017-11-19 21:47:22 +08:00
}
function showParentList(noteId, node) {
assertArguments(noteId, node);
const parents = childToParents[noteId];
if (!parents) {
throwError("Can't find parents for noteId=" + noteId);
}
if (parents.length <= 1) {
2018-02-15 12:31:20 +08:00
$parentList.hide();
}
else {
2018-02-15 12:31:20 +08:00
$parentList.show();
$parentListList.empty();
for (const parentNoteId of parents) {
const parentNotePath = getSomeNotePath(parentNoteId);
// this is to avoid having root notes leading '/'
const notePath = parentNotePath ? (parentNotePath + '/' + noteId) : noteId;
const title = getNotePathTitle(notePath);
let item;
2018-01-29 08:30:14 +08:00
if (node.getParent().data.noteId === parentNoteId) {
item = $("<span/>").attr("title", "Current note").append(title);
}
else {
item = link.createNoteLink(notePath, title);
}
2018-02-15 12:31:20 +08:00
$parentListList.append($("<li/>").append(item));
}
}
}
function getNotePathTitle(notePath) {
assertArguments(notePath);
const titlePath = [];
let parentNoteId = 'root';
for (const noteId of notePath.split('/')) {
titlePath.push(getNoteTitle(noteId, parentNoteId));
parentNoteId = noteId;
}
return titlePath.join(' / ');
}
function getSomeNotePath(noteId) {
assertArguments(noteId);
const path = [];
let cur = noteId;
while (cur !== 'root') {
path.push(cur);
if (!childToParents[cur]) {
throwError("Can't find parents for " + cur);
}
cur = childToParents[cur][0];
}
return path.reverse().join('/');
}
async function setExpandedToServer(noteTreeId, isExpanded) {
assertArguments(noteTreeId);
const expandedNum = isExpanded ? 1 : 0;
2017-11-05 07:28:49 +08:00
2018-01-14 07:02:41 +08:00
await server.put('tree/' + noteTreeId + '/expanded/' + expandedNum);
2017-11-05 07:28:49 +08:00
}
function setCurrentNotePathToHash(node) {
assertArguments(node);
const currentNotePath = treeUtils.getNotePath(node);
2018-01-29 08:30:14 +08:00
const currentNoteTreeId = node.data.noteTreeId;
document.location.hash = currentNotePath;
recentNotes.addRecentNote(currentNoteTreeId, currentNotePath);
}
2018-01-15 10:39:21 +08:00
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);
}
}
function initFancyTree(noteTree) {
assertArguments(noteTree);
2017-11-05 07:28:49 +08:00
const keybindings = {
"del": node => {
2018-01-15 10:39:21 +08:00
treeChanges.deleteNodes(getSelectedNodes(true));
2017-11-05 07:28:49 +08:00
},
"ctrl+up": node => {
2017-11-05 07:28:49 +08:00
const beforeNode = node.getPrevSibling();
if (beforeNode !== null) {
treeChanges.moveBeforeNode([node], beforeNode);
2017-11-05 07:28:49 +08:00
}
return false;
2017-11-05 07:28:49 +08:00
},
"ctrl+down": node => {
2017-11-05 07:28:49 +08:00
let afterNode = node.getNextSibling();
if (afterNode !== null) {
treeChanges.moveAfterNode([node], afterNode);
2017-11-05 07:28:49 +08:00
}
return false;
2017-11-05 07:28:49 +08:00
},
"ctrl+left": node => {
2017-11-29 04:17:11 +08:00
treeChanges.moveNodeUpInHierarchy(node);
return false;
2017-11-05 07:28:49 +08:00
},
"ctrl+right": node => {
2017-11-05 07:28:49 +08:00
let toNode = node.getPrevSibling();
if (toNode !== null) {
treeChanges.moveToNode([node], toNode);
2017-11-05 07:28:49 +08:00
}
return false;
},
"shift+up": node => {
node.navigate($.ui.keyCode.UP, true).then(() => {
const currentNode = getCurrentNode();
if (currentNode.isSelected()) {
node.setSelected(false);
}
currentNode.setSelected(true);
});
return false;
},
"shift+down": node => {
node.navigate($.ui.keyCode.DOWN, true).then(() => {
const currentNode = getCurrentNode();
if (currentNode.isSelected()) {
node.setSelected(false);
}
currentNode.setSelected(true);
});
return false;
},
"f2": node => {
2017-11-28 23:17:30 +08:00
editTreePrefix.showDialog(node);
},
"alt+-": node => {
collapseTree(node);
},
"alt+s": node => {
2018-01-29 08:30:14 +08:00
sortAlphabetically(node.data.noteId);
return false;
},
"ctrl+a": node => {
for (const child of node.getParent().getChildren()) {
child.setSelected(true);
}
return false;
},
"ctrl+c": () => {
contextMenu.copy(getSelectedNodes());
2017-12-28 10:12:54 +08:00
return false;
},
"ctrl+x": () => {
contextMenu.cut(getSelectedNodes());
2017-12-28 10:12:54 +08:00
return false;
},
"ctrl+v": node => {
contextMenu.pasteInto(node);
return false;
},
"return": node => {
noteEditor.focus();
return false;
},
2018-01-04 11:49:53 +08:00
"backspace": node => {
if (!isTopLevelNode(node)) {
node.getParent().setActive().then(() => clearSelectedNodes());
}
},
// code below shouldn't be necessary normally, however there's some problem with interaction with context menu plugin
// after opening context menu, standard shortcuts don't work, but they are detected here
// so we essentially takeover the standard handling with our implementation.
"left": node => {
node.navigate($.ui.keyCode.LEFT, true).then(() => clearSelectedNodes());
return false;
},
"right": node => {
node.navigate($.ui.keyCode.RIGHT, true).then(() => clearSelectedNodes());
return false;
},
"up": node => {
node.navigate($.ui.keyCode.UP, true).then(() => clearSelectedNodes());
return false;
},
"down": node => {
node.navigate($.ui.keyCode.DOWN, true).then(() => clearSelectedNodes());
return false;
2017-11-05 07:28:49 +08:00
}
};
2018-02-15 12:31:20 +08:00
$tree.fancytree({
2017-11-05 07:28:49 +08:00
autoScroll: true,
keyboard: false, // we takover keyboard handling in the hotkeys plugin
extensions: ["hotkeys", "filter", "dnd", "clones"],
source: noteTree,
2017-11-05 07:28:49 +08:00
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());
}
return false;
}
},
2017-11-05 07:28:49 +08:00
activate: (event, data) => {
const node = data.node.data;
setCurrentNotePathToHash(data.node);
2018-01-29 08:30:14 +08:00
noteEditor.switchToNote(node.noteId);
2018-01-29 08:30:14 +08:00
showParentList(node.noteId, data.node);
2017-11-05 07:28:49 +08:00
},
expand: (event, data) => {
2018-01-29 08:30:14 +08:00
setExpandedToServer(data.node.data.noteTreeId, true);
2017-11-05 07:28:49 +08:00
},
collapse: (event, data) => {
2018-01-29 08:30:14 +08:00
setExpandedToServer(data.node.data.noteTreeId, false);
2017-11-05 07:28:49 +08:00
},
init: (event, data) => {
const noteId = treeUtils.getNoteIdFromNotePath(startNotePath);
2018-03-13 08:00:19 +08:00
if (noteIdToNote[noteId] === undefined) {
// note doesn't exist so don't try to activate it
startNotePath = null;
}
if (startNotePath) {
activateNode(startNotePath);
// looks like this this doesn't work when triggered immediatelly after activating node
// so waiting a second helps
setTimeout(scrollToCurrentNote, 1000);
2017-11-19 21:47:22 +08:00
}
2017-11-05 07:28:49 +08:00
},
hotkeys: {
keydown: keybindings
},
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,
2017-11-18 10:31:54 +08:00
lazyLoad: function(event, data){
const node = data.node.data;
2017-11-18 10:31:54 +08:00
2018-01-29 08:30:14 +08:00
data.result = prepareNoteTreeInner(node.noteId);
},
clones: {
highlightActiveClones: true
2017-11-05 07:28:49 +08:00
}
});
2018-02-15 12:31:20 +08:00
$tree.contextmenu(contextMenu.contextMenuSettings);
2017-11-05 07:28:49 +08:00
}
function getTree() {
2018-02-15 12:31:20 +08:00
return $tree.fancytree('getTree');
}
async function reload() {
const notes = await loadTree();
// 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');
startNotePath = resp.start_note_path;
instanceName = resp.instanceName;
2017-11-05 07:28:49 +08:00
if (document.location.hash) {
startNotePath = getNotePathFromAddress();
}
2017-11-05 07:28:49 +08:00
hiddenInAutocomplete = {};
for (const noteId of resp.hiddenInAutocomplete) {
hiddenInAutocomplete[noteId] = true;
}
return prepareNoteTree(resp.notes);
2017-11-05 07:28:49 +08:00
}
$(() => loadTree().then(noteTree => initFancyTree(noteTree)));
2017-11-05 07:28:49 +08:00
function collapseTree(node = null) {
if (!node) {
2018-02-15 12:31:20 +08:00
node = $tree.fancytree("getRootNode");
}
node.setExpanded(false);
node.visit(node => node.setExpanded(false));
2017-11-05 07:28:49 +08:00
}
2017-12-27 00:00:04 +08:00
$(document).bind('keydown', 'alt+c', () => collapseTree()); // don't use shortened form since collapseTree() accepts argument
2017-11-05 07:28:49 +08:00
function scrollToCurrentNote() {
const node = getCurrentNode();
2017-11-05 07:28:49 +08:00
if (node) {
node.makeVisible({scrollIntoView: true});
node.setFocus();
}
}
function setNoteTreeBackgroundBasedOnProtectedStatus(noteId) {
2018-01-29 08:30:14 +08:00
getNodesByNoteId(noteId).map(node => node.toggleClass("protected", !!node.data.isProtected));
}
function setProtected(noteId, isProtected) {
2018-01-29 08:30:14 +08:00
getNodesByNoteId(noteId).map(node => node.data.isProtected = isProtected);
setNoteTreeBackgroundBasedOnProtectedStatus(noteId);
}
2017-11-20 08:39:39 +08:00
function getAutocompleteItems(parentNoteId, notePath, titlePath) {
if (!parentNoteId) {
parentNoteId = 'root';
}
if (!parentToChildren[parentNoteId]) {
return [];
}
if (!notePath) {
notePath = '';
}
if (!titlePath) {
titlePath = '';
}
2018-02-25 11:44:45 +08:00
// https://github.com/zadam/trilium/issues/46
// unfortunately not easy to implement because we don't have an easy access to note's isProtected property
2017-11-20 08:39:39 +08:00
const autocompleteItems = [];
for (const childNoteId of parentToChildren[parentNoteId]) {
if (hiddenInAutocomplete[childNoteId]) {
continue;
}
2017-11-20 08:39:39 +08:00
const childNotePath = (notePath ? (notePath + '/') : '') + childNoteId;
const childTitlePath = (titlePath ? (titlePath + ' / ') : '') + getNoteTitle(childNoteId, parentNoteId);
2017-11-20 08:39:39 +08:00
autocompleteItems.push({
2017-11-20 09:36:13 +08:00
value: childTitlePath + ' (' + childNotePath + ')',
2017-11-20 08:39:39 +08:00
label: childTitlePath
});
const childItems = getAutocompleteItems(childNoteId, childNotePath, childTitlePath);
for (const childItem of childItems) {
autocompleteItems.push(childItem);
}
}
return autocompleteItems;
}
function setNoteTitle(noteId, title) {
assertArguments(noteId);
2018-03-13 08:00:19 +08:00
getNote(noteId).title = title;
getNodesByNoteId(noteId).map(clone => treeUtils.setNodeTitleWithPrefix(clone));
}
async function createNewTopLevelNote() {
2018-02-15 12:31:20 +08:00
const rootNode = $tree.fancytree("getRootNode");
await createNote(rootNode, "root", "into");
}
async function createNote(node, parentNoteId, target, isProtected) {
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 || !protected_session.isProtectedSessionAvailable()) {
isProtected = false;
}
const newNoteName = "new note";
const result = await server.post('notes/' + parentNoteId + '/children', {
2018-01-29 08:30:14 +08:00
title: newNoteName,
target: target,
2018-01-29 08:30:14 +08:00
target_noteTreeId: node.data.noteTreeId,
isProtected: isProtected
});
2018-01-29 08:30:14 +08:00
setParentChildRelation(result.noteTreeId, parentNoteId, result.noteId);
2018-01-29 08:30:14 +08:00
notesTreeMap[result.noteTreeId] = result;
2018-03-13 08:00:19 +08:00
noteIdToNote[result.noteId] = {
title: result.title,
isProtected: result.isProtected,
type: result.type,
mime: result.mime
};
noteEditor.newNoteCreated();
const newNode = {
title: newNoteName,
2018-01-29 08:30:14 +08:00
noteId: result.noteId,
parentNoteId: parentNoteId,
refKey: result.noteId,
noteTreeId: result.noteTreeId,
isProtected: isProtected,
extraClasses: getExtraClasses(result.note)
};
if (target === 'after') {
await node.appendSibling(newNode).setActive(true);
}
2018-01-17 12:22:13 +08:00
else if (target === 'into') {
if (!node.getChildren() && node.isFolder()) {
await node.setExpanded();
}
else {
node.addChildren(newNode);
}
await node.getLastChild().setActive(true);
node.folder = true;
node.renderTitle();
}
2018-01-17 12:22:13 +08:00
else {
throwError("Unrecognized target: " + target);
}
clearSelectedNodes(); // to unmark previously active node
showMessage("Created!");
}
async function sortAlphabetically(noteId) {
await server.put('notes/' + noteId + '/sort');
await reload();
}
function noteExists(noteId) {
return !!childToParents[noteId];
}
function getInstanceName() {
return instanceName;
}
2017-12-19 12:41:13 +08:00
$(document).bind('keydown', 'ctrl+o', e => {
const node = getCurrentNode();
2018-01-29 08:30:14 +08:00
const parentNoteId = node.data.parentNoteId;
2017-12-19 12:41:13 +08:00
const isProtected = treeUtils.getParentProtectedStatus(node);
createNote(node, parentNoteId, 'after', isProtected);
e.preventDefault();
});
$(document).bind('keydown', 'ctrl+p', e => {
const node = getCurrentNode();
2018-01-29 08:30:14 +08:00
createNote(node, node.data.noteId, 'into', node.data.isProtected);
2017-12-19 12:41:13 +08:00
e.preventDefault();
});
$(document).bind('keydown', 'ctrl+del', e => {
const node = getCurrentNode();
2018-01-15 10:39:21 +08:00
treeChanges.deleteNodes([node]);
2017-12-19 12:41:13 +08:00
e.preventDefault();
});
$(document).bind('keydown', 'ctrl+.', scrollToCurrentNote);
$(window).bind('hashchange', function() {
const notePath = getNotePathFromAddress();
if (getCurrentNotePath() !== notePath) {
console.log("Switching to " + notePath + " because of hash change");
activateNode(notePath);
}
});
if (isElectron()) {
$(document).bind('keydown', 'alt+left', e => {
window.history.back();
e.preventDefault();
});
$(document).bind('keydown', 'alt+right', e => {
window.history.forward();
e.preventDefault();
});
}
2017-11-05 07:28:49 +08:00
return {
reload,
2017-11-05 07:28:49 +08:00
collapseTree,
scrollToCurrentNote,
setNoteTreeBackgroundBasedOnProtectedStatus,
setProtected,
getCurrentNode,
expandToNote,
2017-11-20 01:06:48 +08:00
activateNode,
getCurrentNotePath,
getNoteTitle,
2017-11-20 08:39:39 +08:00
setCurrentNotePathToHash,
getAutocompleteItems,
setNoteTitle,
createNewTopLevelNote,
2017-11-28 23:17:30 +08:00
createNote,
setPrefix,
getNotePathTitle,
removeParentChildRelation,
setParentChildRelation,
getSelectedNodes,
sortAlphabetically,
noteExists,
getInstanceName
2017-11-05 07:28:49 +08:00
};
})();