diff --git a/src/becca/entities/note.js b/src/becca/entities/note.js index abaf9f60a..91e089f07 100644 --- a/src/becca/entities/note.js +++ b/src/becca/entities/note.js @@ -374,7 +374,8 @@ class Note extends AbstractEntity { return this.__attributeCache.filter(attr => attr.name === name); } else { - return this.__attributeCache.slice(); + // a bit unsafe to return the original array, but defensive copy would be costly + return this.__attributeCache; } } @@ -456,7 +457,7 @@ class Note extends AbstractEntity { * @param [value] * @returns {boolean} */ - hasAttribute(type, name, value) { + hasAttribute(type, name, value = null) { return !!this.getAttributes().find(attr => attr.type === type && attr.name === name @@ -648,12 +649,12 @@ class Note extends AbstractEntity { } /** - * @param {string} [type] - (optional) attribute type to filter - * @param {string} [name] - (optional) attribute name to filter - * @param {string} [value] - (optional) attribute value to filter + * @param {string|null} [type] - (optional) attribute type to filter + * @param {string|null} [name] - (optional) attribute name to filter + * @param {string|null} [value] - (optional) attribute value to filter * @returns {Attribute[]} note's "owned" attributes - excluding inherited ones */ - getOwnedAttributes(type, name, value) { + getOwnedAttributes(type = null, name = null, value = null) { // it's a common mistake to include # or ~ into attribute name if (name && ["#", "~"].includes(name[0])) { name = name.substr(1); @@ -681,7 +682,7 @@ class Note extends AbstractEntity { * * This method can be significantly faster than the getAttribute() */ - getOwnedAttribute(type, name, value) { + getOwnedAttribute(type, name, value = null) { const attrs = this.getOwnedAttributes(type, name, value); return attrs.length > 0 ? attrs[0] : null; diff --git a/src/public/app/entities/note_short.js b/src/public/app/entities/note_short.js index fb1c96798..7faa6cb02 100644 --- a/src/public/app/entities/note_short.js +++ b/src/public/app/entities/note_short.js @@ -289,7 +289,7 @@ class NoteShort { } isRoot() { - return this.noted + return this.noteId === 'root'; } getAllNotePaths(encounteredNoteIds = null) { diff --git a/src/services/handlers.js b/src/services/handlers.js index dceb913c2..3e2b69a60 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -46,6 +46,8 @@ eventService.subscribe([ eventService.ENTITY_CHANGED, eventService.ENTITY_DELETE if (entity.type === 'label' && ['sorted', 'sortDirection', 'sortFoldersFirst'].includes(entity.name)) { handleSortedAttribute(entity); + } else if (entity.type === 'label') { + handleMaybeSortingLabel(entity); } } else if (entityName === 'notes') { @@ -94,6 +96,9 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) => else if (entity.type === 'label' && entity.name === 'sorted') { handleSortedAttribute(entity); } + else if (entity.type === 'label') { + handleMaybeSortingLabel(entity); + } } else if (entityName === 'branches') { runAttachedRelations(entity.getNote(), 'runOnBranchCreation', entity); @@ -138,6 +143,27 @@ function handleSortedAttribute(entity) { } } +function handleMaybeSortingLabel(entity) { + // check if this label is used for sorting, if yes force re-sort + const note = becca.notes[entity.noteId]; + + // this will not work on deleted notes, but in that case we don't really need to re-sort + if (note) { + for (const parentNote of note.getParentNotes()) { + console.log("PAR", parentNote.noteId); + const sorted = parentNote.getLabelValue("sorted"); + + console.log("sorted", sorted); + + if (sorted?.includes(entity.name)) { // hacky check if the sorting is affected by this label + console.log("RESIRT!"); + + treeService.sortNotesIfNeeded(parentNote.noteId); + } + } + } +} + eventService.subscribe(eventService.ENTITY_CHANGED, ({ entityName, entity }) => { processInverseRelations(entityName, entity, (definition, note, targetNote) => { // we need to make sure that also target's inverse attribute exists and if not, then create it diff --git a/src/services/tree.js b/src/services/tree.js index 218777183..d6e96b861 100644 --- a/src/services/tree.js +++ b/src/services/tree.js @@ -175,19 +175,25 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder } let position = 10; + let someBranchUpdated = false; for (const note of notes) { const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId); - sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", - [position, branch.branchId]); + if (branch.notePosition !== position) { + sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", + [position, branch.branchId]); - becca.branches[branch.branchId].notePosition = position; + branch.notePosition = position; + someBranchUpdated = true; + } position += 10; } - entityChangesService.addNoteReorderingEntityChange(parentNoteId); + if (someBranchUpdated) { + entityChangesService.addNoteReorderingEntityChange(parentNoteId); + } }); }