trilium/src/routes/api/tree_changes.js

116 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-01-14 07:02:41 +08:00
"use strict";
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const sync_table = require('../../services/sync_table');
const tree = require('../../services/tree');
const notes = require('../../services/notes');
2018-04-01 11:08:22 +08:00
const repository = require('../../services/repository');
2018-01-14 07:02:41 +08:00
/**
* Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique
* for not deleted note trees. There may be multiple deleted note-parent note relationships.
*/
2018-03-31 00:57:22 +08:00
async function moveBranchToParent(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
2018-01-14 07:02:41 +08:00
const parentNoteId = req.params.parentNoteId;
2018-03-25 09:39:15 +08:00
const noteToMove = await tree.getBranch(branchId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
const validationResult = await tree.validateParentChild(parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) {
return [400, validationResult];
2018-01-14 07:02:41 +08:00
}
2018-03-25 09:39:15 +08:00
const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentNoteId]);
2018-01-14 07:02:41 +08:00
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
const now = utils.nowDate();
2018-03-31 00:57:22 +08:00
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[parentNoteId, newNotePos, now, branchId]);
2018-01-14 07:02:41 +08:00
await sync_table.addBranchSync(branchId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
return { success: true };
}
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
async function moveBranchBeforeNote(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
const beforeBranchId = req.params.beforeBranchId;
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
const noteToMove = await tree.getBranch(branchId);
const beforeNote = await tree.getBranch(beforeBranchId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
const validationResult = await tree.validateParentChild(beforeNote.parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) {
return [400, validationResult];
2018-01-14 07:02:41 +08:00
}
2018-03-31 00:57:22 +08:00
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition >= ? AND isDeleted = 0",
[beforeNote.parentNoteId, beforeNote.notePosition]);
2018-01-14 07:02:41 +08:00
await sync_table.addNoteReorderingSync(beforeNote.parentNoteId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[beforeNote.parentNoteId, beforeNote.notePosition, utils.nowDate(), branchId]);
2018-01-14 07:02:41 +08:00
await sync_table.addBranchSync(branchId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
return { success: true };
}
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
async function moveBranchAfterNote(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
const afterBranchId = req.params.afterBranchId;
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
const noteToMove = await tree.getBranch(branchId);
const afterNote = await tree.getBranch(afterBranchId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteToMove.noteId, branchId);
if (!validationResult.success) {
return [400, validationResult];
2018-01-14 07:02:41 +08:00
}
2018-03-31 00:57:22 +08:00
// we don't change dateModified so other changes are prioritized in case of conflict
// also we would have to sync all those modified note trees otherwise hash checks would fail
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
2018-01-14 07:02:41 +08:00
await sync_table.addNoteReorderingSync(afterNote.parentNoteId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
await sql.execute("UPDATE branches SET parentNoteId = ?, notePosition = ?, dateModified = ? WHERE branchId = ?",
[afterNote.parentNoteId, afterNote.notePosition + 1, utils.nowDate(), branchId]);
2018-01-14 07:02:41 +08:00
await sync_table.addBranchSync(branchId);
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
return { success: true };
}
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
async function setExpanded(req) {
2018-03-25 09:39:15 +08:00
const branchId = req.params.branchId;
2018-01-14 07:02:41 +08:00
const expanded = req.params.expanded;
2018-03-31 00:57:22 +08:00
await sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
// we don't sync expanded label
}
async function deleteBranch(req) {
2018-04-01 11:08:22 +08:00
const branch = await repository.getBranch(req.params.branchId);
await notes.deleteNote(branch);
2018-03-31 00:57:22 +08:00
}
module.exports = {
moveBranchToParent,
moveBranchBeforeNote,
moveBranchAfterNote,
setExpanded,
deleteBranch
};