trilium/src/services/tree.js

132 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-01-14 07:02:41 +08:00
"use strict";
const sql = require('./sql');
const repository = require('./repository');
const Branch = require('../entities/branch');
const syncTableService = require('./sync_table');
const protectedSessionService = require('./protected_session');
2018-01-14 07:02:41 +08:00
2018-03-31 00:57:22 +08:00
async function validateParentChild(parentNoteId, childNoteId, branchId = null) {
2018-03-25 09:39:15 +08:00
const existing = await getExistingBranch(parentNoteId, childNoteId);
2018-01-14 07:02:41 +08:00
2018-03-25 09:39:15 +08:00
if (existing && (branchId === null || existing.branchId !== branchId)) {
2018-03-31 00:57:22 +08:00
return {
2018-01-14 07:02:41 +08:00
success: false,
message: 'This note already exists in the target.'
2018-03-31 00:57:22 +08:00
};
2018-01-14 07:02:41 +08:00
}
if (!await checkTreeCycle(parentNoteId, childNoteId)) {
2018-03-31 00:57:22 +08:00
return {
2018-01-14 07:02:41 +08:00
success: false,
message: 'Moving note here would create cycle.'
2018-03-31 00:57:22 +08:00
};
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-25 09:39:15 +08:00
async function getExistingBranch(parentNoteId, childNoteId) {
return await sql.getRow('SELECT * FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
2018-01-14 07:02:41 +08:00
}
/**
* Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases.
*/
async function checkTreeCycle(parentNoteId, childNoteId) {
const subTreeNoteIds = [];
// we'll load the whole sub tree - because the cycle can start in one of the notes in the sub tree
await loadSubTreeNoteIds(childNoteId, subTreeNoteIds);
async function checkTreeCycleInner(parentNoteId) {
if (parentNoteId === 'root') {
return true;
}
if (subTreeNoteIds.includes(parentNoteId)) {
// while towards the root of the tree we encountered noteId which is already present in the subtree
// joining parentNoteId with childNoteId would then clearly create a cycle
return false;
}
2018-03-25 09:39:15 +08:00
const parentNoteIds = await sql.getColumn("SELECT DISTINCT parentNoteId FROM branches WHERE noteId = ? AND isDeleted = 0", [parentNoteId]);
2018-01-14 07:02:41 +08:00
for (const pid of parentNoteIds) {
if (!await checkTreeCycleInner(pid)) {
return false;
}
}
return true;
}
return await checkTreeCycleInner(parentNoteId);
}
2018-03-25 09:39:15 +08:00
async function getBranch(branchId) {
return sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId]);
2018-01-14 07:02:41 +08:00
}
async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
subTreeNoteIds.push(parentNoteId);
2018-03-25 09:39:15 +08:00
const children = await sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
2018-01-14 07:02:41 +08:00
for (const childNoteId of children) {
await loadSubTreeNoteIds(childNoteId, subTreeNoteIds);
}
}
async function sortNotesAlphabetically(parentNoteId) {
await sql.transactional(async () => {
2018-03-25 09:39:15 +08:00
const notes = await sql.getRows(`SELECT branchId, noteId, title, isProtected
FROM notes JOIN branches USING(noteId)
WHERE branches.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]);
protectedSessionService.decryptNotes(notes);
2018-01-29 08:30:14 +08:00
notes.sort((a, b) => a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1);
let position = 1;
for (const note of notes) {
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
[position, note.branchId]);
position++;
}
await syncTableService.addNoteReorderingSync(parentNoteId);
});
}
async function setNoteToParent(noteId, prefix, parentNoteId) {
// case where there might be more such branches is ignored. It's expected there should be just one
const branch = await repository.getEntity("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
if (branch) {
if (!parentNoteId) {
branch.isDeleted = true;
}
else {
branch.parentNoteId = parentNoteId;
}
await branch.save();
}
else if (parentNoteId) {
await new Branch({
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix
}).save();
}
}
2018-01-14 07:02:41 +08:00
module.exports = {
validateParentChild,
2018-03-25 09:39:15 +08:00
getBranch,
sortNotesAlphabetically,
setNoteToParent
2018-01-14 07:02:41 +08:00
};