mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
recovery if note path changes, plus change of note path after note move
This commit is contained in:
parent
14787e0283
commit
e992087720
@ -139,15 +139,24 @@ const noteTree = (function() {
|
|||||||
|
|
||||||
const effectivePath = [];
|
const effectivePath = [];
|
||||||
let childNoteId = null;
|
let childNoteId = null;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const parentNoteId = i < path.length ? path[i] : null;
|
||||||
|
i++;
|
||||||
|
|
||||||
for (const parentNoteId of path) {
|
|
||||||
if (childNoteId !== null) {
|
if (childNoteId !== null) {
|
||||||
const parents = childToParents[childNoteId];
|
const parents = childToParents[childNoteId];
|
||||||
|
|
||||||
if (!parents.includes(parentNoteId)) {
|
if (parentNoteId === null || !parents.includes(parentNoteId)) {
|
||||||
console.log("Did not find parent " + parentNoteId + " for child " + childNoteId);
|
console.log("Did not find parent " + parentNoteId + " for child " + childNoteId);
|
||||||
|
|
||||||
if (parents.length > 0) {
|
if (parents.length > 0) {
|
||||||
|
if (parents[0] === 'root') {
|
||||||
|
console.log("Reached root.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
childNoteId = parents[0];
|
childNoteId = parents[0];
|
||||||
effectivePath.push(childNoteId);
|
effectivePath.push(childNoteId);
|
||||||
|
|
||||||
@ -196,6 +205,14 @@ const noteTree = (function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setCurrentNotePathToHash(node) {
|
||||||
|
const currentNotePath = treeUtils.getNotePath(node);
|
||||||
|
|
||||||
|
document.location.hash = currentNotePath;
|
||||||
|
|
||||||
|
recentNotes.addRecentNote(currentNotePath);
|
||||||
|
}
|
||||||
|
|
||||||
function initFancyTree(noteTree) {
|
function initFancyTree(noteTree) {
|
||||||
const keybindings = {
|
const keybindings = {
|
||||||
"insert": node => {
|
"insert": node => {
|
||||||
@ -246,11 +263,8 @@ const noteTree = (function() {
|
|||||||
scrollParent: $("#tree"),
|
scrollParent: $("#tree"),
|
||||||
activate: (event, data) => {
|
activate: (event, data) => {
|
||||||
const node = data.node.data;
|
const node = data.node.data;
|
||||||
const currentNotePath = treeUtils.getNotePath(data.node);
|
|
||||||
|
|
||||||
document.location.hash = currentNotePath;
|
setCurrentNotePathToHash(data.node);
|
||||||
|
|
||||||
recentNotes.addRecentNote(currentNotePath);
|
|
||||||
|
|
||||||
noteEditor.switchToNote(node.note_id);
|
noteEditor.switchToNote(node.note_id);
|
||||||
},
|
},
|
||||||
@ -471,6 +485,7 @@ const noteTree = (function() {
|
|||||||
getCurrentNoteTreeId,
|
getCurrentNoteTreeId,
|
||||||
activateNode,
|
activateNode,
|
||||||
getCurrentNotePath,
|
getCurrentNotePath,
|
||||||
getNoteTitle
|
getNoteTitle,
|
||||||
|
setCurrentNotePathToHash
|
||||||
};
|
};
|
||||||
})();
|
})();
|
@ -3,29 +3,33 @@
|
|||||||
const treeChanges = (function() {
|
const treeChanges = (function() {
|
||||||
function moveBeforeNode(node, beforeNode) {
|
function moveBeforeNode(node, beforeNode) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'notes/' + node.key + '/moveBefore/' + beforeNode.key,
|
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveBefore/' + beforeNode.data.note_tree_id,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
success: () => {
|
success: () => {
|
||||||
node.moveTo(beforeNode, 'before');
|
node.moveTo(beforeNode, 'before');
|
||||||
|
|
||||||
|
noteTree.setCurrentNotePathToHash(node);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveAfterNode(node, afterNode) {
|
function moveAfterNode(node, afterNode) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'notes/' + node.key + '/moveAfter/' + afterNode.key,
|
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + afterNode.data.note_tree_id,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
success: () => {
|
success: () => {
|
||||||
node.moveTo(afterNode, 'after');
|
node.moveTo(afterNode, 'after');
|
||||||
|
|
||||||
|
noteTree.setCurrentNotePathToHash(node);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function moveToNode(node, toNode) {
|
function moveToNode(node, toNode) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'notes/' + node.key + '/moveTo/' + toNode.key,
|
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveTo/' + toNode.data.note_id,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
success: () => {
|
success: () => {
|
||||||
@ -35,6 +39,8 @@ const treeChanges = (function() {
|
|||||||
|
|
||||||
toNode.folder = true;
|
toNode.folder = true;
|
||||||
toNode.renderTitle();
|
toNode.renderTitle();
|
||||||
|
|
||||||
|
noteTree.setCurrentNotePathToHash(node);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -63,6 +69,8 @@ const treeChanges = (function() {
|
|||||||
|
|
||||||
// activate next element after this one is deleted so we don't lose focus
|
// activate next element after this one is deleted so we don't lose focus
|
||||||
next.setActive();
|
next.setActive();
|
||||||
|
|
||||||
|
noteTree.setCurrentNotePathToHash(next);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -71,7 +79,7 @@ const treeChanges = (function() {
|
|||||||
function moveNodeUp(node) {
|
function moveNodeUp(node) {
|
||||||
if (node.getParent() !== null) {
|
if (node.getParent() !== null) {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: baseApiUrl + 'notes/' + node.key + '/moveAfter/' + node.getParent().key,
|
url: baseApiUrl + 'notes/' + node.data.note_tree_id + '/moveAfter/' + node.getParent().data.note_tree_id,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
contentType: "application/json",
|
contentType: "application/json",
|
||||||
success: () => {
|
success: () => {
|
||||||
@ -81,6 +89,8 @@ const treeChanges = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
node.moveTo(node.getParent(), 'after');
|
node.moveTo(node.getParent(), 'after');
|
||||||
|
|
||||||
|
noteTree.setCurrentNotePathToHash(node);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -8,37 +8,31 @@ const audit_category = require('../../services/audit_category');
|
|||||||
const auth = require('../../services/auth');
|
const auth = require('../../services/auth');
|
||||||
const sync_table = require('../../services/sync_table');
|
const sync_table = require('../../services/sync_table');
|
||||||
|
|
||||||
router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next) => {
|
router.put('/:noteTreeId/moveTo/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {
|
||||||
let noteId = req.params.noteId;
|
const noteTreeId = req.params.noteTreeId;
|
||||||
let parentId = req.params.parentId;
|
const parentNoteId = req.params.parentNoteId;
|
||||||
|
|
||||||
const row = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ? and is_deleted = 0', [parentId]);
|
const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]);
|
||||||
const maxNotePos = row.max_note_pos;
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
||||||
let newNotePos = 0;
|
|
||||||
|
|
||||||
if (maxNotePos === null) // no children yet
|
|
||||||
newNotePos = 0;
|
|
||||||
else
|
|
||||||
newNotePos = maxNotePos + 1;
|
|
||||||
|
|
||||||
const now = utils.nowTimestamp();
|
const now = utils.nowTimestamp();
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
|
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_tree_id = ?",
|
||||||
[parentId, newNotePos, now, noteId]);
|
[parentNoteId, newNotePos, now, noteTreeId]);
|
||||||
|
|
||||||
await sync_table.addNoteTreeSync(noteId);
|
await sync_table.addNoteTreeSync(noteTreeId);
|
||||||
await sql.addAudit(audit_category.CHANGE_PARENT, utils.browserId(req), noteId, null, parentId);
|
await sql.addAudit(audit_category.CHANGE_PARENT, utils.browserId(req), null, null, parentNoteId);
|
||||||
});
|
});
|
||||||
|
|
||||||
res.send({});
|
res.send({});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => {
|
router.put('/:noteTreeId/moveBefore/:beforeNoteTreeId', async (req, res, next) => {
|
||||||
let noteId = req.params.noteId;
|
const noteTreeId = req.params.noteTreeId;
|
||||||
let beforeNoteId = req.params.beforeNoteId;
|
const beforeNoteTreeId = req.params.beforeNoteTreeId;
|
||||||
|
|
||||||
const beforeNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [beforeNoteId]);
|
const beforeNote = await sql.getSingleResult("select * from notes_tree where note_tree_id = ?", [beforeNoteTreeId]);
|
||||||
|
|
||||||
if (beforeNote) {
|
if (beforeNote) {
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
@ -48,23 +42,26 @@ router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => {
|
|||||||
|
|
||||||
const now = utils.nowTimestamp();
|
const now = utils.nowTimestamp();
|
||||||
|
|
||||||
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
|
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_tree_id = ?",
|
||||||
[beforeNote.note_pid, beforeNote.note_pos, now, noteId]);
|
[beforeNote.note_pid, beforeNote.note_pos, now, noteTreeId]);
|
||||||
|
|
||||||
await sync_table.addNoteTreeSync(noteId);
|
await sync_table.addNoteTreeSync(noteTreeId);
|
||||||
await sync_table.addNoteReorderingSync(beforeNote.note_pid);
|
await sync_table.addNoteReorderingSync(beforeNote.note_pid);
|
||||||
await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), beforeNote.note_pid);
|
await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), beforeNote.note_pid);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
res.send({});
|
res.send({});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.status(500).send("Before note " + beforeNoteTreeId + " doesn't exist.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => {
|
router.put('/:noteTreeId/moveAfter/:afterNoteTreeId', async (req, res, next) => {
|
||||||
let noteId = req.params.noteId;
|
const noteTreeId = req.params.noteTreeId;
|
||||||
let afterNoteId = req.params.afterNoteId;
|
const afterNoteTreeId = req.params.afterNoteTreeId;
|
||||||
|
|
||||||
const afterNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [afterNoteId]);
|
const afterNote = await sql.getSingleResult("select * from notes_tree where note_tree_id = ?", [afterNoteTreeId]);
|
||||||
|
|
||||||
if (afterNote) {
|
if (afterNote) {
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
@ -74,24 +71,27 @@ router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => {
|
|||||||
|
|
||||||
const now = utils.nowTimestamp();
|
const now = utils.nowTimestamp();
|
||||||
|
|
||||||
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_id = ?",
|
await sql.execute("update notes_tree set note_pid = ?, note_pos = ?, date_modified = ? where note_tree_id = ?",
|
||||||
[afterNote.note_pid, afterNote.note_pos + 1, now, noteId]);
|
[afterNote.note_pid, afterNote.note_pos + 1, now, noteTreeId]);
|
||||||
|
|
||||||
await sync_table.addNoteTreeSync(noteId);
|
await sync_table.addNoteTreeSync(noteTreeId);
|
||||||
await sync_table.addNoteReorderingSync(afterNote.note_pid);
|
await sync_table.addNoteReorderingSync(afterNote.note_pid);
|
||||||
await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), afterNote.note_pid);
|
await sql.addAudit(audit_category.CHANGE_POSITION, utils.browserId(req), afterNote.note_pid);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
res.send({});
|
res.send({});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.status(500).send("After note " + afterNoteTreeId + " doesn't exist.");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put('/:noteId/expanded/:expanded', async (req, res, next) => {
|
router.put('/:noteTreeId/expanded/:expanded', async (req, res, next) => {
|
||||||
const noteId = req.params.noteId;
|
const noteTreeId = req.params.noteTreeId;
|
||||||
const expanded = req.params.expanded;
|
const expanded = req.params.expanded;
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
await sql.execute("update notes_tree set is_expanded = ? where note_id = ?", [expanded, noteId]);
|
await sql.execute("update notes_tree set is_expanded = ? where note_tree_id = ?", [expanded, noteTreeId]);
|
||||||
});
|
});
|
||||||
|
|
||||||
res.send({});
|
res.send({});
|
||||||
|
@ -15,19 +15,15 @@ async function createNewNote(parentNoteId, note, browserId) {
|
|||||||
if (note.target === 'into') {
|
if (note.target === 'into') {
|
||||||
const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]);
|
const maxNotePos = await sql.getSingleValue('select max(note_pos) from notes_tree where note_pid = ? and is_deleted = 0', [parentNoteId]);
|
||||||
|
|
||||||
if (maxNotePos === null) // no children yet
|
newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
||||||
newNotePos = 0;
|
|
||||||
else
|
|
||||||
newNotePos = maxNotePos + 1
|
|
||||||
}
|
}
|
||||||
else if (note.target === 'after') {
|
else if (note.target === 'after') {
|
||||||
const afterNote = await sql.getSingleResult('select note_pos from notes_tree where note_id = ?', [note.target_note_id]);
|
const afterNote = await sql.getSingleResult('select note_pos from notes_tree where note_id = ?', [note.target_note_id]);
|
||||||
|
|
||||||
newNotePos = afterNote.note_pos + 1;
|
newNotePos = afterNote.note_pos + 1;
|
||||||
|
|
||||||
const now = utils.nowTimestamp();
|
await sql.execute('update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_pid = ? and note_pos > ? and is_deleted = 0',
|
||||||
|
[utils.nowTimestamp(), parentNoteId, afterNote.note_pos]);
|
||||||
await sql.execute('update notes_tree set note_pos = note_pos + 1, date_modified = ? where note_pid = ? and note_pos > ? and is_deleted = 0', [now, parentNoteId, afterNote['note_pos']]);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new Error('Unknown target: ' + note.target);
|
throw new Error('Unknown target: ' + note.target);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user