support for note path

This commit is contained in:
azivner 2017-11-19 08:47:22 -05:00
parent cc3c9d6428
commit b22eb2db1e
7 changed files with 86 additions and 55 deletions

View File

@ -28,7 +28,7 @@ const jumpToNote = (function() {
const noteId = link.getNodeIdFromLabel(val); const noteId = link.getNodeIdFromLabel(val);
if (noteId) { if (noteId) {
treeUtils.activateNode(noteId); noteTree.activateNode(noteId);
dialogEl.dialog('close'); dialogEl.dialog('close');
} }

View File

@ -18,9 +18,6 @@ const recentNotes = (function() {
function addRecentNote(noteTreeId) { function addRecentNote(noteTreeId) {
setTimeout(() => { setTimeout(() => {
console.log("note tree: " + noteTreeId);
console.log("current note tree: " + noteTree.getCurrentNoteTreeId());
// 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 (noteTreeId === noteTree.getCurrentNoteTreeId()) { if (noteTreeId === noteTree.getCurrentNoteTreeId()) {
$.ajax({ $.ajax({
@ -86,7 +83,7 @@ const recentNotes = (function() {
function setActiveNoteBasedOnRecentNotes() { function setActiveNoteBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes(); const noteId = getSelectedNoteIdFromRecentNotes();
treeUtils.activateNode(noteId); noteTree.activateNode(noteId);
dialogEl.dialog('close'); dialogEl.dialog('close');
} }

View File

@ -2,7 +2,7 @@
const link = (function() { const link = (function() {
function getNoteIdFromLink(url) { function getNoteIdFromLink(url) {
const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url); const noteIdMatch = /app#([A-Za-z0-9]+)$/.exec(url);
if (noteIdMatch === null) { if (noteIdMatch === null) {
return null; return null;
@ -13,7 +13,7 @@ const link = (function() {
} }
function getNodeIdFromLabel(label) { function getNodeIdFromLabel(label) {
const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label); const noteIdMatch = / \(([A-Za-z0-9]+)\)/.exec(label);
if (noteIdMatch !== null) { if (noteIdMatch !== null) {
return noteIdMatch[1]; return noteIdMatch[1];
@ -41,7 +41,7 @@ const link = (function() {
} }
if (noteId) { if (noteId) {
treeUtils.activateNode(noteId); noteTree.activateNode(noteId);
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise // this is quite ugly hack, but it seems like we can't close the tooltip otherwise
$("[role='tooltip']").remove(); $("[role='tooltip']").remove();

View File

@ -7,7 +7,8 @@ const noteTree = (function() {
let treeLoadTime = null; let treeLoadTime = null;
let clipboardNoteTreeId = null; let clipboardNoteTreeId = null;
let notesMap = {}; let notesMap = {};
let parentToNotes = {}; let parentToChildren = {};
let childToParents = {};
let counter = 1; let counter = 1;
let noteTreeIdToKey = {}; let noteTreeIdToKey = {};
@ -34,7 +35,8 @@ const noteTree = (function() {
} }
function prepareNoteTree(notes, notesParent) { function prepareNoteTree(notes, notesParent) {
parentToNotes = {}; parentToChildren = {};
childToParents = {};
notesMap = {}; notesMap = {};
for (const note of notes) { for (const note of notes) {
@ -42,16 +44,22 @@ const noteTree = (function() {
} }
for (const np of notesParent) { for (const np of notesParent) {
if (!parentToNotes[np.parent_id]) { if (!parentToChildren[np.parent_id]) {
parentToNotes[np.parent_id] = []; parentToChildren[np.parent_id] = [];
} }
parentToNotes[np.parent_id].push(np.child_id); parentToChildren[np.parent_id].push(np.child_id);
if (!childToParents[np.child_id]) {
childToParents[np.child_id] = [];
}
childToParents[np.child_id].push(np.parent_id);
} }
glob.allNoteIds = Object.keys(notesMap); glob.allNoteIds = Object.keys(notesMap);
return prepareNoteTreeInner(parentToNotes['root']); return prepareNoteTreeInner(parentToChildren['root']);
} }
function prepareNoteTreeInner(noteTreeIds) { function prepareNoteTreeInner(noteTreeIds) {
@ -71,11 +79,11 @@ const noteTree = (function() {
noteTreeIdToKey[noteTreeId] = note.key; noteTreeIdToKey[noteTreeId] = note.key;
if (parentToNotes[noteTreeId] && parentToNotes[noteTreeId].length > 0) { if (parentToChildren[noteTreeId] && parentToChildren[noteTreeId].length > 0) {
note.folder = true; note.folder = true;
if (note.expanded) { if (note.expanded) {
note.children = prepareNoteTreeInner(parentToNotes[noteTreeId], notesMap, parentToNotes); note.children = prepareNoteTreeInner(parentToChildren[noteTreeId], notesMap, parentToChildren);
} }
else { else {
note.lazy = true; note.lazy = true;
@ -88,6 +96,36 @@ const noteTree = (function() {
return noteList; return noteList;
} }
async function activateNode(notePath) {
const path = notePath.split("/").reverse();
if (!notesMap[path[0]]) {
console.log("Requested note doesn't exist.");
return;
}
const effectivePath = [];
for (const noteTreeId of path) {
effectivePath.push(noteTreeId);
}
const runPath = effectivePath.reverse();
for (let i = 0; i < runPath.length; i++) {
const noteTreeId = runPath[i];
const node = treeUtils.getNodeByNoteTreeId(noteTreeId);
if (i < runPath.length - 1) {
await node.setExpanded();
}
else {
await node.setActive();
}
}
}
function setExpandedToServer(noteTreeId, isExpanded) { function setExpandedToServer(noteTreeId, isExpanded) {
const expandedNum = isExpanded ? 1 : 0; const expandedNum = isExpanded ? 1 : 0;
@ -150,7 +188,7 @@ const noteTree = (function() {
activate: (event, data) => { activate: (event, data) => {
const node = data.node.data; const node = data.node.data;
document.location.hash = node.note_tree_id; document.location.hash = treeUtils.getNotePath(data.node);
recentNotes.addRecentNote(node.note_tree_id); recentNotes.addRecentNote(node.note_tree_id);
@ -163,13 +201,9 @@ const noteTree = (function() {
setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false); setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false);
}, },
init: (event, data) => { init: (event, data) => {
// if (startNoteTreeId) { if (startNoteTreeId) {
// treeUtils.activateNode(startNoteTreeId); activateNode(startNoteTreeId);
// } }
showAppIfHidden();
}, },
hotkeys: { hotkeys: {
keydown: keybindings keydown: keybindings
@ -237,8 +271,8 @@ const noteTree = (function() {
const node = data.node.data; const node = data.node.data;
const noteTreeId = node.note_tree_id; const noteTreeId = node.note_tree_id;
if (parentToNotes[noteTreeId]) { if (parentToChildren[noteTreeId]) {
data.result = prepareNoteTreeInner(parentToNotes[noteTreeId]); data.result = prepareNoteTreeInner(parentToChildren[noteTreeId]);
} }
else { else {
console.log("No children for " + noteTreeId + ". This shouldn't happen."); console.log("No children for " + noteTreeId + ". This shouldn't happen.");
@ -372,5 +406,6 @@ const noteTree = (function() {
setCurrentNoteTreeBasedOnProtectedStatus, setCurrentNoteTreeBasedOnProtectedStatus,
getCurrentNode, getCurrentNode,
getCurrentNoteTreeId, getCurrentNoteTreeId,
activateNode
}; };
})(); })();

View File

@ -21,31 +21,6 @@ const treeUtils = (function() {
return getNodeByKey(key); return getNodeByKey(key);
} }
async function activateNode(noteTreeIdToActivate) {
const noteTreeIdPath = [ noteTreeIdToActivate ];
let note = noteTree.getByNoteId(noteTreeIdToActivate);
while (note) {
if (note.note_pid !== 'root') {
noteTreeIdPath.push(note.note_pid);
}
note = noteTree.getByNoteId(note.note_pid);
}
for (const noteTreeId of noteTreeIdPath.reverse()) {
const node = treeUtils.getNodeByNoteTreeId(noteTreeId);
if (noteTreeId !== noteTreeIdToActivate) {
await node.setExpanded();
}
else {
await node.setActive();
}
}
}
function getNoteTitle(noteId) { function getNoteTitle(noteId) {
const note = treeUtils.getNodeByKey(noteId); const note = treeUtils.getNodeByKey(noteId);
if (!note) { if (!note) {
@ -79,13 +54,27 @@ const treeUtils = (function() {
return path.reverse().join(" > "); return path.reverse().join(" > ");
} }
function getNotePath(node) {
const path = [];
while (node) {
if (node.data.note_tree_id) {
path.push(node.data.note_tree_id);
}
node = node.getParent();
}
return path.reverse().join("/");
}
return { return {
getParentNoteTreeId, getParentNoteTreeId,
getParentProtectedStatus, getParentProtectedStatus,
getNodeByKey, getNodeByKey,
getNodeByNoteTreeId, getNodeByNoteTreeId,
activateNode,
getNoteTitle, getNoteTitle,
getFullName getFullName,
getNotePath
}; };
})(); })();

View File

@ -8,7 +8,7 @@ const sync_table = require('./sync_table');
async function createNewNote(parentNoteId, note, browserId) { async function createNewNote(parentNoteId, note, browserId) {
const noteId = utils.newNoteId(); const noteId = utils.newNoteId();
const noteTreeId = utils.newNoteId(); const noteTreeId = utils.newNoteTreeId();
let newNotePos = 0; let newNotePos = 0;
@ -156,7 +156,7 @@ async function updateNote(noteId, newNote, ctx) {
await sql.doInTransaction(async () => { await sql.doInTransaction(async () => {
if (!existingNoteHistoryId) { if (!existingNoteHistoryId) {
const newNoteHistoryId = utils.randomString(16); const newNoteHistoryId = utils.newNoteHistoryId();
await sql.execute("insert into notes_history (note_history_id, note_id, note_title, note_text, is_protected, date_modified_from, date_modified_to) " + await sql.execute("insert into notes_history (note_history_id, note_id, note_title, note_text, is_protected, date_modified_from, date_modified_to) " +
"values (?, ?, ?, ?, ?, ?, ?)", [ "values (?, ?, ?, ?, ?, ?, ?)", [

View File

@ -6,6 +6,14 @@ function newNoteId() {
return randomString(12); return randomString(12);
} }
function newNoteTreeId() {
return randomString(8);
}
function newNoteHistoryId() {
return randomString(12);
}
const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; const ALPHA_NUMERIC = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function randomString(length) { function randomString(length) {
@ -63,6 +71,8 @@ module.exports = {
randomString, randomString,
nowTimestamp, nowTimestamp,
newNoteId, newNoteId,
newNoteTreeId,
newNoteHistoryId,
toBase64, toBase64,
fromBase64, fromBase64,
hmac, hmac,