label update should trigger parent resort, fixes #3366

This commit is contained in:
zadam 2022-12-14 23:44:26 +01:00
parent c4b0c44919
commit 31b3af4129
4 changed files with 45 additions and 12 deletions

View File

@ -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;

View File

@ -289,7 +289,7 @@ class NoteShort {
}
isRoot() {
return this.noted
return this.noteId === 'root';
}
getAllNotePaths(encounteredNoteIds = null) {

View File

@ -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

View File

@ -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);
}
});
}