diff --git a/migrations/0040__fix_note_pid.sql b/migrations/0040__fix_note_pid.sql new file mode 100644 index 000000000..f829e6902 --- /dev/null +++ b/migrations/0040__fix_note_pid.sql @@ -0,0 +1,6 @@ +UPDATE + notes_tree +SET + note_pid = (SELECT parent.note_id FROM notes_tree parent WHERE notes_tree.note_pid = parent.note_tree_id) +WHERE + note_pid != 'root' \ No newline at end of file diff --git a/migrations/0040__notes_parent.sql b/migrations/0040__notes_parent.sql deleted file mode 100644 index 21b5c7b55..000000000 --- a/migrations/0040__notes_parent.sql +++ /dev/null @@ -1,28 +0,0 @@ -CREATE TABLE [notes_parent] ( - [parent_id] VARCHAR(30) NOT NULL, - [child_id] VARCHAR(30) NOT NULL, - date_created INTEGER NOT NULL DEFAULT 0, - PRIMARY KEY (parent_id, child_id) -); - -INSERT INTO notes_parent (parent_id, child_id, date_created) - SELECT note_pid, note_tree_id, date_modified FROM notes_tree; - -CREATE TABLE [notes_tree_mig] ( - [note_tree_id] VARCHAR(30) PRIMARY KEY NOT NULL, - [note_id] VARCHAR(30) UNIQUE NOT NULL, - [note_pos] INTEGER NOT NULL, - [is_expanded] BOOLEAN NULL , - date_modified INTEGER NOT NULL DEFAULT 0, - is_deleted INTEGER NOT NULL DEFAULT 0 -); - -INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted) - SELECT note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted FROM notes_tree; - -DROP TABLE notes_tree; -ALTER TABLE notes_tree_mig RENAME TO notes_tree; - -CREATE INDEX `IDX_notes_tree_note_id` ON `notes_tree` ( - `note_id` -); \ No newline at end of file diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index e7d6c55ce..474c389db 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -11,6 +11,7 @@ const noteTree = (function() { let childToParents = {}; let counter = 1; let noteTreeIdToKey = {}; + let parentChildToNoteTreeId = {}; function getNoteTreeIdFromKey(key) { const node = treeUtils.getNodeByKey(key); @@ -34,38 +35,54 @@ const noteTree = (function() { clipboardNoteTreeId = cbNoteId; } - function prepareNoteTree(notes, notesParent) { + function getNoteTreeId(parentNoteId, childNoteId) { + const key = parentNoteId + "-" + childNoteId; + + const noteTreeId = parentChildToNoteTreeId[key]; + + if (!noteTreeId) { + throw new Error("Can't find note tree id for parent=" + parentNoteId + ", child=" + childNoteId); + } + + return noteTreeId; + } + + function prepareNoteTree(notes) { parentToChildren = {}; childToParents = {}; notesMap = {}; for (const note of notes) { notesMap[note.note_tree_id] = note; - } - for (const np of notesParent) { - if (!parentToChildren[np.parent_id]) { - parentToChildren[np.parent_id] = []; + const key = note.note_pid + "-" + note.note_id; + + parentChildToNoteTreeId[key] = note.note_tree_id; + + if (!parentToChildren[note.note_pid]) { + parentToChildren[note.note_pid] = []; } - parentToChildren[np.parent_id].push(np.child_id); + parentToChildren[note.note_pid].push(note.note_id); - if (!childToParents[np.child_id]) { - childToParents[np.child_id] = []; + if (!childToParents[note.note_id]) { + childToParents[note.note_id] = []; } - childToParents[np.child_id].push(np.parent_id); + childToParents[note.note_id].push(note.note_pid); } glob.allNoteIds = Object.keys(notesMap); - return prepareNoteTreeInner(parentToChildren['root']); + return prepareNoteTreeInner('root'); } - function prepareNoteTreeInner(noteTreeIds) { + function prepareNoteTreeInner(parentNoteId) { + const childNoteIds = parentToChildren[parentNoteId]; const noteList = []; - for (const noteTreeId of noteTreeIds) { + for (const childNoteId of childNoteIds) { + const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); const note = notesMap[noteTreeId]; note.title = note.note_title; @@ -79,11 +96,11 @@ const noteTree = (function() { noteTreeIdToKey[noteTreeId] = note.key; - if (parentToChildren[noteTreeId] && parentToChildren[noteTreeId].length > 0) { + if (parentToChildren[note.note_id] && parentToChildren[note.note_id].length > 0) { note.folder = true; if (note.expanded) { - note.children = prepareNoteTreeInner(parentToChildren[noteTreeId], notesMap, parentToChildren); + note.children = prepareNoteTreeInner(note.note_id); } else { note.lazy = true; @@ -99,21 +116,40 @@ const noteTree = (function() { async function activateNode(notePath) { const path = notePath.split("/").reverse(); - if (!notesMap[path[0]]) { - console.log("Requested note doesn't exist."); - return; - } - const effectivePath = []; + let childNoteId = null; - for (const noteTreeId of path) { - effectivePath.push(noteTreeId); + for (const parentNoteId of path) { + if (childNoteId !== null) { + const parents = childToParents[childNoteId]; + + if (!parents.includes(parentNoteId)) { + console.log("Did not find parent " + parentNoteId + " for child " + childNoteId); + + if (parents.length > 0) { + childNoteId = parents[0]; + effectivePath.push(childNoteId); + + console.log("Choosing parent " + childNoteId + " instead."); + continue; + } + else { + console.log("No parents, can't activate node."); + return; + } + } + } + + effectivePath.push(parentNoteId); + childNoteId = parentNoteId; } const runPath = effectivePath.reverse(); + let parentNoteId = 'root'; for (let i = 0; i < runPath.length; i++) { - const noteTreeId = runPath[i]; + const childNoteId = runPath[i]; + const noteTreeId = getNoteTreeId(parentNoteId, childNoteId); const node = treeUtils.getNodeByNoteTreeId(noteTreeId); @@ -123,6 +159,8 @@ const noteTree = (function() { else { await node.setActive(); } + + parentNoteId = childNoteId; } } @@ -201,6 +239,7 @@ const noteTree = (function() { setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false); }, init: (event, data) => { + showAppIfHidden(); if (startNoteTreeId) { activateNode(startNoteTreeId); } diff --git a/public/javascripts/tree_utils.js b/public/javascripts/tree_utils.js index 1d0646add..3edb1d54d 100644 --- a/public/javascripts/tree_utils.js +++ b/public/javascripts/tree_utils.js @@ -58,8 +58,8 @@ const treeUtils = (function() { const path = []; while (node) { - if (node.data.note_tree_id) { - path.push(node.data.note_tree_id); + if (node.data.note_id) { + path.push(node.data.note_id); } node = node.getParent(); diff --git a/routes/api/tree.js b/routes/api/tree.js index d5a78f37c..3e59b7dfd 100644 --- a/routes/api/tree.js +++ b/routes/api/tree.js @@ -21,13 +21,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { + "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 " + "order by note_pos"); - const notes_parent = await sql.getResults("" + - "select parent_id, child_id " + - "from notes_parent " + - "join notes_tree as child on child.note_tree_id = notes_parent.child_id " + - "left join notes_tree as parent on parent.note_tree_id = notes_parent.parent_id " + - "where child.is_deleted = 0 and (parent.is_deleted = 0 or notes_parent.parent_id = 'root')"); - const dataKey = protected_session.getDataKey(req); for (const note of notes) { @@ -38,7 +31,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { res.send({ notes: notes, - notes_parent: notes_parent, start_note_tree_id: await options.getOption('start_note_tree_id'), tree_load_time: utils.nowTimestamp() });