Skip to content

Commit

Permalink
fixes to stable sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Nov 3, 2023
1 parent 45ed436 commit f37f47c
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/becca/entities/bnote.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class BNote extends AbstractBeccaEntity {
this.utcDateCreated = utcDateCreated || dateUtils.utcNowDateTime();
/** @type {string} */
this.utcDateModified = utcDateModified;
/**
/**
* set during the deletion operation, before it is completed (removed from becca completely)
* @type {boolean}
*/
Expand Down Expand Up @@ -756,7 +756,7 @@ class BNote extends AbstractBeccaEntity {
} else if (a.parentNote?.isHiddenCompletely()) {
return 1;
} else {
return -1;
return 0;
}
});

Expand All @@ -776,7 +776,7 @@ class BNote extends AbstractBeccaEntity {
const aBranch = becca.getBranchFromChildAndParent(a.noteId, this.noteId);
const bBranch = becca.getBranchFromChildAndParent(b.noteId, this.noteId);

return aBranch?.notePosition < bBranch?.notePosition ? -1 : 1;
return (aBranch?.notePosition - bBranch?.notePosition) || 0;
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/etapi/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function register(router) {
zipImportService.importZip(taskContext, req.body, note).then(importedNote => {
res.status(201).json({
note: mappers.mapNoteToPojo(importedNote),
branch: mappers.mapBranchToPojo(importedNote.getBranches()[0]),
branch: mappers.mapBranchToPojo(importedNote.getParentBranches()[0]),
});
}); // we need better error handling here, async errors won't be properly processed.
});
Expand Down
4 changes: 2 additions & 2 deletions src/public/app/entities/fnote.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class FNote {
branchIdPos[branchId] = this.froca.getBranch(branchId).notePosition;
}

this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] < branchIdPos[this.childToBranch[b]] ? -1 : 1);
this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] - branchIdPos[this.childToBranch[b]]);
}

/** @returns {boolean} */
Expand Down Expand Up @@ -228,7 +228,7 @@ class FNote {
return 1;
}

return -1;
return 0;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ export default class AttributeEditorWidget extends NoteContextAwareWidget {

async renderOwnedAttributes(ownedAttributes, saved) {
// attrs are not resorted if position changes after the initial load
ownedAttributes.sort((a, b) => a.position < b.position ? -1 : 1);
ownedAttributes.sort((a, b) => a.position - b.position);

let htmlAttrs = (await attributeRenderer.renderAttributes(ownedAttributes, true)).html();

Expand Down
8 changes: 4 additions & 4 deletions src/public/app/widgets/basic_widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Component from "../components/component.js";

/**
* This is the base widget for all other widgets.
*
*
* For information on using widgets, see the tutorial {@tutorial widget_basics}.
*/
class BasicWidget extends Component {
Expand Down Expand Up @@ -33,7 +33,7 @@ class BasicWidget extends Component {
}
}

this.children.sort((a, b) => a.position - b.position < 0 ? -1 : 1);
this.children.sort((a, b) => a.position - b.position);

return this;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ class BasicWidget extends Component {

/**
* Accepts a string of CSS to add with the widget.
* @param {string} block
* @param {string} block
* @returns {this} for chaining
*/
cssBlock(block) {
Expand Down Expand Up @@ -124,7 +124,7 @@ class BasicWidget extends Component {

/**
* Method used for rendering the widget.
*
*
* Your class should override this method.
* @returns {JQuery<HTMLElement>} Your widget.
*/
Expand Down
15 changes: 9 additions & 6 deletions src/public/app/widgets/note_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,6 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
let parentsOfAddedNodes = [];

const allBranchRows = loadResults.getBranchRows();
// TODO: this flag is suspicious - why does it matter that all branches in a particular update are deleted?
const allBranchesDeleted = allBranchRows.every(branchRow => !!branchRow.isDeleted);

for (const branchRow of allBranchRows) {
Expand Down Expand Up @@ -1190,12 +1189,16 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {
continue;
}

const found = (parentNode.getChildren() || []).find(child => child.data.noteId === branchRow.noteId);
if (!found) {
const note = await froca.getNote(branchRow.noteId);
const frocaBranch = froca.getBranch(branchRow.branchId);
const foundNode = (parentNode.getChildren() || []).find(child => child.data.noteId === branchRow.noteId);
if (foundNode) {
// the branch already exists in the tree
if (branchRow.isExpanded !== foundNode.isExpanded()) {
noteIdsToReload.add(frocaBranch.noteId);
}
} else {
// make sure it's loaded
const note = await froca.getNote(branchRow.noteId);
const frocaBranch = froca.getBranch(branchRow.branchId);

// we're forcing lazy since it's not clear if the whole required subtree is in froca
parentNode.addChildren([this.prepareNode(frocaBranch, true)]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class InheritedAttributesWidget extends NoteContextAwareWidget {

attrs.sort((a, b) => {
if (a.noteId === b.noteId) {
return a.position < b.position ? -1 : 1;
return a.position - b.position;
} else {
// inherited attributes should stay grouped: https://github.com/zadam/trilium/issues/3761
return a.noteId < b.noteId ? -1 : 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class PromotedAttributesWidget extends NoteContextAwareWidget {
// attrs are not resorted if position changes after the initial load
// promoted attrs are sorted primarily by order of definitions, but with multi-valued promoted attrs
// the order of attributes is important as well
ownedAttributes.sort((a, b) => a.position < b.position ? -1 : 1);
ownedAttributes.sort((a, b) => a.position - b.position);

if (promotedDefAttrs.length === 0) {
this.toggleInt(false);
Expand Down
2 changes: 1 addition & 1 deletion src/services/import/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const markdownService = require("./markdown");
* @param {TaskContext} taskContext
* @param {Buffer} fileBuffer
* @param {BNote} importRootNote
* @returns {Promise<*>}
* @returns {Promise<BNote>}
*/
async function importZip(taskContext, fileBuffer, importRootNote) {
/** @type {Object.<string, string>} maps from original noteId (in ZIP file) to newly generated noteId */
Expand Down

0 comments on commit f37f47c

Please sign in to comment.