tar import will sort notes if there is no meta file

This commit is contained in:
zadam 2019-10-06 09:49:47 +02:00
parent 516277a478
commit d23e9f1bc4
3 changed files with 26 additions and 6 deletions

View File

@ -840,7 +840,7 @@ a.external:not(.no-arrow):after, a[href^="http://"]:not(.no-arrow):after, a[href
}
.note-book-auto-message {
background-color: var(--more-accented-background-color);
background-color: var(--accented-background-color);
text-align: center;
width: 100%;
border-radius: 10px;

View File

@ -14,6 +14,7 @@ const commonmark = require('commonmark');
const ImportContext = require('../import_context');
const protectedSessionService = require('../protected_session');
const mimeService = require("./mime");
const treeService = require("../tree");
/**
* @param {ImportContext} importContext
@ -426,6 +427,12 @@ async function importTar(importContext, fileBuffer, importRootNote) {
for (const noteId in createdNoteIds) { // now the noteIds are unique
await noteService.scanForLinks(noteId);
if (!metaFile) {
// if there's no meta file then the notes are created based on the order in that tar file but that
// is usually quite random so we sort the notes in the way they would appear in the file manager
await treeService.sortNotesAlphabetically(noteId, true);
}
importContext.increaseProgressCount();
}

View File

@ -87,15 +87,28 @@ async function loadSubtreeNoteIds(parentNoteId, subtreeNoteIds) {
}
}
async function sortNotesAlphabetically(parentNoteId) {
async function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
await sql.transactional(async () => {
const notes = await sql.getRows(`SELECT branchId, noteId, title, isProtected
FROM notes JOIN branches USING(noteId)
WHERE branches.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]);
const notes = await sql.getRows(
`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) => a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1);
notes.sort((a, b) => {
if (directoriesFirst && ((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;
}
});
let position = 1;