diff --git a/src/entities/branch.js b/src/entities/branch.js index c24698a8a..b4d509eee 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -27,6 +27,13 @@ class Branch extends Entity { // notePosition is not part of hash because it would produce a lot of updates in case of reordering static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "prefix"]; } + constructor(row = {}) { + super(row); + + // used to detect move in note tree + this.origParentNoteId = this.parentNoteId; + } + async getNote() { return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); } diff --git a/src/services/note_cache.js b/src/services/note_cache.js index 970799b7d..d7ecb194e 100644 --- a/src/services/note_cache.js +++ b/src/services/note_cache.js @@ -260,20 +260,19 @@ eventService.subscribe(eventService.ENTITY_CHANGED, async ({entityName, entity}) else if (entityName === 'branches') { const branch = entity; - if (childToParent[branch.noteId]) { - childToParent[branch.noteId] = childToParent[branch.noteId].filter(noteId => noteId !== branch.parentNoteId) - } + // first we remove records for original placement (if they exist) + childToParent[branch.noteId] = childToParent[branch.noteId] || []; + childToParent[branch.noteId] = childToParent[branch.noteId].filter(noteId => noteId !== branch.origParentNoteId); - if (branch.isDeleted) { - delete prefixes[branch.noteId + '-' + branch.parentNoteId]; - delete childParentToBranchId[branch.noteId + '-' + branch.parentNoteId]; - } - else { + delete prefixes[branch.noteId + '-' + branch.origParentNoteId]; + delete childParentToBranchId[branch.noteId + '-' + branch.origParentNoteId]; + + if (!branch.isDeleted) { + // ... and then we create new records if (branch.prefix) { prefixes[branch.noteId + '-' + branch.parentNoteId] = branch.prefix; } - childToParent[branch.noteId] = childToParent[branch.noteId] || []; childToParent[branch.noteId].push(branch.parentNoteId); childParentToBranchId[branch.noteId + '-' + branch.parentNoteId] = branch.branchId; } diff --git a/src/services/repository.js b/src/services/repository.js index 61ed885e4..2bd1ca453 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -68,9 +68,11 @@ async function updateEntity(entity) { const clone = Object.assign({}, entity); + // transient properties not supposed to be persisted delete clone.jsonContent; delete clone.isOwned; delete clone.isChanged; + delete clone.origParentNoteId; delete clone.__attributeCache; for (const key in clone) {