mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
refactoring and check for existing clone in a parent
This commit is contained in:
parent
45a293e25a
commit
5b2c18dab7
@ -27,12 +27,17 @@ const treeChanges = (function() {
|
|||||||
|
|
||||||
// beware that first arg is noteId and second is noteTreeId!
|
// beware that first arg is noteId and second is noteTreeId!
|
||||||
async function cloneNoteAfter(noteId, afterNoteTreeId) {
|
async function cloneNoteAfter(noteId, afterNoteTreeId) {
|
||||||
await $.ajax({
|
const resp = await $.ajax({
|
||||||
url: baseApiUrl + 'notes/' + noteId + '/cloneAfter/' + afterNoteTreeId,
|
url: baseApiUrl + 'notes/' + noteId + '/cloneAfter/' + afterNoteTreeId,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
error: () => showError("Error cloning note.")
|
error: () => showError("Error cloning note.")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!resp.success) {
|
||||||
|
alert(resp.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await noteTree.reload();
|
await noteTree.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,12 +59,17 @@ const treeChanges = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function cloneNoteTo(childNoteId, parentNoteId) {
|
async function cloneNoteTo(childNoteId, parentNoteId) {
|
||||||
await $.ajax({
|
const resp = await $.ajax({
|
||||||
url: baseApiUrl + 'notes/' + childNoteId + '/cloneTo/' + parentNoteId,
|
url: baseApiUrl + 'notes/' + childNoteId + '/cloneTo/' + parentNoteId,
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
error: () => showError("Error cloning note.")
|
error: () => showError("Error cloning note.")
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!resp.success) {
|
||||||
|
alert(resp.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await noteTree.reload();
|
await noteTree.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,31 +92,37 @@ router.put('/:childNoteId/cloneTo/:parentNoteId', auth.checkApiAuth, async (req,
|
|||||||
|
|
||||||
const existing = await sql.getSingleValue('SELECT * FROM notes_tree WHERE note_id = ? AND note_pid = ?', [childNoteId, parentNoteId]);
|
const existing = await sql.getSingleValue('SELECT * FROM notes_tree WHERE note_id = ? AND note_pid = ?', [childNoteId, parentNoteId]);
|
||||||
|
|
||||||
if (!existing) {
|
if (existing && !existing.is_deleted) {
|
||||||
|
res.send({
|
||||||
|
success: false,
|
||||||
|
message: 'This note already exists in target parent note.'
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
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]);
|
||||||
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
||||||
|
|
||||||
const noteTreeId = utils.newNoteTreeId();
|
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
await sync_table.addNoteTreeSync(noteTreeId);
|
const noteTree = {
|
||||||
|
'note_tree_id': utils.newNoteTreeId(),
|
||||||
await sql.insert("notes_tree", {
|
|
||||||
'note_tree_id': noteTreeId,
|
|
||||||
'note_id': childNoteId,
|
'note_id': childNoteId,
|
||||||
'note_pid': parentNoteId,
|
'note_pid': parentNoteId,
|
||||||
'note_pos': newNotePos,
|
'note_pos': newNotePos,
|
||||||
'is_expanded': 0,
|
'is_expanded': 0,
|
||||||
'date_modified': utils.nowTimestamp(),
|
'date_modified': utils.nowTimestamp(),
|
||||||
'is_deleted': 0
|
'is_deleted': 0
|
||||||
});
|
};
|
||||||
});
|
|
||||||
}
|
|
||||||
else if (existing && existing.is_deleted) {
|
|
||||||
await sql.execute("UPDATE notes_tree SET is_deleted = 0 WHERE note_tree_id = ?", [existing.note_tree_id]);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.send({});
|
await sql.replace("notes_tree", noteTree);
|
||||||
|
|
||||||
|
await sync_table.addNoteTreeSync(noteTree.note_tree_id);
|
||||||
|
|
||||||
|
res.send({
|
||||||
|
success: true
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put('/:noteId/cloneAfter/:afterNoteTreeId', async (req, res, next) => {
|
router.put('/:noteId/cloneAfter/:afterNoteTreeId', async (req, res, next) => {
|
||||||
@ -125,34 +131,47 @@ router.put('/:noteId/cloneAfter/:afterNoteTreeId', async (req, res, next) => {
|
|||||||
|
|
||||||
const afterNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]);
|
const afterNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]);
|
||||||
|
|
||||||
if (afterNote) {
|
if (!afterNote) {
|
||||||
|
res.status(500).send("After note " + afterNoteTreeId + " doesn't exist.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const existing = await sql.getSingleValue('SELECT * FROM notes_tree WHERE note_id = ? AND note_pid = ?', [noteId, afterNote.note_pid]);
|
||||||
|
|
||||||
|
if (existing && !existing.is_deleted) {
|
||||||
|
res.send({
|
||||||
|
success: false,
|
||||||
|
message: 'This note already exists in target parent note.'
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await sql.doInTransaction(async () => {
|
await sql.doInTransaction(async () => {
|
||||||
// we don't change date_modified so other changes are prioritized in case of conflict
|
// we don't change date_modified so other changes are prioritized in case of conflict
|
||||||
await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0",
|
await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0",
|
||||||
[afterNote.note_pid, afterNote.note_pos]);
|
[afterNote.note_pid, afterNote.note_pos]);
|
||||||
|
|
||||||
const noteTreeId = utils.newNoteTreeId();
|
const noteTree = {
|
||||||
|
'note_tree_id': utils.newNoteTreeId(),
|
||||||
await sql.insert("notes_tree", {
|
|
||||||
'note_tree_id': noteTreeId,
|
|
||||||
'note_id': noteId,
|
'note_id': noteId,
|
||||||
'note_pid': afterNote.note_pid,
|
'note_pid': afterNote.note_pid,
|
||||||
'note_pos': afterNote.note_pos + 1,
|
'note_pos': afterNote.note_pos + 1,
|
||||||
'is_expanded': 0,
|
'is_expanded': 0,
|
||||||
'date_modified': utils.nowTimestamp(),
|
'date_modified': utils.nowTimestamp(),
|
||||||
'is_deleted': 0
|
'is_deleted': 0
|
||||||
});
|
};
|
||||||
|
|
||||||
await sync_table.addNoteTreeSync(noteTreeId);
|
await sql.replace("notes_tree", noteTree);
|
||||||
|
|
||||||
|
await sync_table.addNoteTreeSync(noteTree.note_tree_id);
|
||||||
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({
|
||||||
}
|
success: true
|
||||||
else {
|
});
|
||||||
res.status(500).send("After note " + afterNoteTreeId + " doesn't exist.");
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.put('/:noteTreeId/expanded/:expanded', async (req, res, next) => {
|
router.put('/:noteTreeId/expanded/:expanded', async (req, res, next) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user