mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
allow specifiyng sorted attribute (including off for overriding), #2142
This commit is contained in:
parent
6e300c7cf5
commit
5cfd1c030d
@ -132,6 +132,10 @@ class Note extends AbstractEntity {
|
|||||||
return this.children;
|
return this.children;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hasChildren() {
|
||||||
|
return this.children && this.children.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
getChildBranches() {
|
getChildBranches() {
|
||||||
return this.children.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
|
return this.children.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
|
||||||
}
|
}
|
||||||
|
@ -96,12 +96,10 @@ function moveBranchBeforeNote(req) {
|
|||||||
branchToMove.markAsDeleted();
|
branchToMove.markAsDeleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentNote.hasLabel('sorted')) {
|
treeService.sortNotesIfNeeded(parentNote.noteId);
|
||||||
treeService.sortNotesByTitle(parentNote.noteId, false, false);
|
|
||||||
}
|
// if sorting is not needed then still the ordering might have changed above manually
|
||||||
else {
|
|
||||||
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
@ -147,12 +145,10 @@ function moveBranchAfterNote(req) {
|
|||||||
branchToMove.markAsDeleted();
|
branchToMove.markAsDeleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentNote.hasLabel('sorted')) {
|
treeService.sortNotesIfNeeded(parentNote.noteId);
|
||||||
treeService.sortNotesByTitle(parentNote.noteId, false, false);
|
|
||||||
}
|
// if sorting is not needed then still the ordering might have changed above manually
|
||||||
else {
|
|
||||||
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true };
|
return { success: true };
|
||||||
}
|
}
|
||||||
|
@ -97,12 +97,7 @@ function sortChildNotes(req) {
|
|||||||
|
|
||||||
const reverse = sortDirection === 'desc';
|
const reverse = sortDirection === 'desc';
|
||||||
|
|
||||||
if (sortBy === 'title') {
|
|
||||||
treeService.sortNotesByTitle(noteId, false, reverse);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
treeService.sortNotes(noteId, sortBy, reverse);
|
treeService.sortNotes(noteId, sortBy, reverse);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function protectNote(req) {
|
function protectNote(req) {
|
||||||
|
@ -352,7 +352,7 @@ function BackendScriptApi(currentNote, apiParams) {
|
|||||||
* @method
|
* @method
|
||||||
* @param {string} parentNoteId - this note's child notes will be sorted
|
* @param {string} parentNoteId - this note's child notes will be sorted
|
||||||
*/
|
*/
|
||||||
this.sortNotesByTitle = treeService.sortNotesByTitle;
|
this.sortNotesByTitle = parentNoteId => treeService.sortNotes(parentNoteId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method finds note by its noteId and prefix and either sets it to the given parentNoteId
|
* This method finds note by its noteId and prefix and either sets it to the given parentNoteId
|
||||||
|
@ -30,7 +30,7 @@ eventService.subscribe(eventService.NOTE_TITLE_CHANGED, note => {
|
|||||||
|
|
||||||
for (const parentNote of noteFromCache.parents) {
|
for (const parentNote of noteFromCache.parents) {
|
||||||
if (parentNote.hasLabel("sorted")) {
|
if (parentNote.hasLabel("sorted")) {
|
||||||
treeService.sortNotesByTitle(parentNote.noteId);
|
treeService.sortNotesIfNeeded(parentNote.noteId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,14 +83,14 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (entity.type === 'label' && entity.name === 'sorted') {
|
else if (entity.type === 'label' && entity.name === 'sorted') {
|
||||||
treeService.sortNotesByTitle(entity.noteId);
|
treeService.sortNotesIfNeeded(entity.noteId);
|
||||||
|
|
||||||
if (entity.isInheritable) {
|
if (entity.isInheritable) {
|
||||||
const note = becca.notes[entity.noteId];
|
const note = becca.notes[entity.noteId];
|
||||||
|
|
||||||
if (note) {
|
if (note) {
|
||||||
for (const noteId of note.getSubtreeNoteIds()) {
|
for (const noteId of note.getSubtreeNoteIds()) {
|
||||||
treeService.sortNotesByTitle(noteId);
|
treeService.sortNotesIfNeeded(noteId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -469,9 +469,9 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
noteService.scanForLinks(becca.getNote(noteId));
|
noteService.scanForLinks(becca.getNote(noteId));
|
||||||
|
|
||||||
if (!metaFile) {
|
if (!metaFile) {
|
||||||
// if there's no meta file then the notes are created based on the order in that tar file but that
|
// if there's no meta file then the notes are created based on the order in that zip file but that
|
||||||
// is usually quite random so we sort the notes in the way they would appear in the file manager
|
// is usually quite random so we sort the notes in the way they would appear in the file manager
|
||||||
treeService.sortNotesByTitle(noteId, true);
|
treeService.sortNotes(noteId, 'title', false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
taskContext.increaseProgressCount();
|
taskContext.increaseProgressCount();
|
||||||
|
@ -108,58 +108,32 @@ function loadSubtreeNoteIds(parentNoteId, subtreeNoteIds) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortNotesByTitle(parentNoteId, foldersFirst = false, reverse = false) {
|
function sortNotes(parentNoteId, sortBy = 'title', reverse = false, foldersFirst = false) {
|
||||||
sql.transactional(() => {
|
if (!sortBy) {
|
||||||
const notes = sql.getRows(
|
sortBy = 'title';
|
||||||
`SELECT branches.branchId, notes.noteId, title, isProtected,
|
|
||||||
CASE WHEN COUNT(childBranches.noteId) > 0 THEN 1 ELSE 0 END AS hasChildren
|
|
||||||
FROM notes
|
|
||||||
JOIN branches ON branches.noteId = notes.noteId
|
|
||||||
LEFT JOIN branches childBranches ON childBranches.parentNoteId = notes.noteId AND childBranches.isDeleted = 0
|
|
||||||
WHERE branches.isDeleted = 0 AND branches.parentNoteId = ?
|
|
||||||
GROUP BY notes.noteId`, [parentNoteId]);
|
|
||||||
|
|
||||||
protectedSessionService.decryptNotes(notes);
|
|
||||||
|
|
||||||
notes.sort((a, b) => {
|
|
||||||
if (foldersFirst && ((a.hasChildren && !b.hasChildren) || (!a.hasChildren && b.hasChildren))) {
|
|
||||||
// exactly one note of the two is a directory so the sorting will be done based on this status
|
|
||||||
return a.hasChildren ? -1 : 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (reverse) {
|
|
||||||
notes.reverse();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let position = 10;
|
|
||||||
|
|
||||||
for (const note of notes) {
|
|
||||||
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
|
|
||||||
[position, note.branchId]);
|
|
||||||
|
|
||||||
if (note.branchId in becca.branches) {
|
|
||||||
becca.branches[note.branchId].notePosition = position;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
log.info(`Branch "${note.branchId}" was not found in becca.`);
|
|
||||||
}
|
|
||||||
|
|
||||||
position += 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function sortNotes(parentNoteId, sortBy, reverse = false) {
|
|
||||||
sql.transactional(() => {
|
sql.transactional(() => {
|
||||||
const notes = becca.getNote(parentNoteId).getChildNotes();
|
const notes = becca.getNote(parentNoteId).getChildNotes();
|
||||||
|
|
||||||
notes.sort((a, b) => a[sortBy] < b[sortBy] ? -1 : 1);
|
const normalize = obj => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj;
|
||||||
|
|
||||||
|
notes.sort((a, b) => {
|
||||||
|
if (foldersFirst) {
|
||||||
|
const aHasChildren = a.hasChildren();
|
||||||
|
const bHasChildren = b.hasChildren();
|
||||||
|
|
||||||
|
if ((aHasChildren && !bHasChildren) || (!aHasChildren && bHasChildren)) {
|
||||||
|
// exactly one note of the two is a directory so the sorting will be done based on this status
|
||||||
|
return aHasChildren ? -1 : 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let aEl = normalize(a[sortBy]);
|
||||||
|
let bEl = normalize(b[sortBy]);
|
||||||
|
|
||||||
|
return aEl < bEl ? -1 : 1;
|
||||||
|
});
|
||||||
|
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
notes.reverse();
|
notes.reverse();
|
||||||
@ -182,6 +156,22 @@ function sortNotes(parentNoteId, sortBy, reverse = false) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sortNotesIfNeeded(parentNoteId) {
|
||||||
|
const parentNote = becca.getNote(parentNoteId);
|
||||||
|
|
||||||
|
if (!parentNote) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortedLabel = parentNote.getLabel('sorted');
|
||||||
|
|
||||||
|
if (!sortedLabel || sortedLabel.value === 'off') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sortNotes(parentNoteId, sortedLabel.value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated - this will be removed in the future
|
* @deprecated - this will be removed in the future
|
||||||
*/
|
*/
|
||||||
@ -233,7 +223,7 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
getNotes,
|
getNotes,
|
||||||
validateParentChild,
|
validateParentChild,
|
||||||
sortNotesByTitle,
|
|
||||||
sortNotes,
|
sortNotes,
|
||||||
|
sortNotesIfNeeded,
|
||||||
setNoteToParent
|
setNoteToParent
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user