From d23e9f1bc43bd895cf5c8974cb561d66282173f8 Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 6 Oct 2019 09:49:47 +0200 Subject: [PATCH] tar import will sort notes if there is no meta file --- src/public/stylesheets/style.css | 2 +- src/services/import/tar.js | 7 +++++++ src/services/tree.js | 23 ++++++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.css index 6c5d7733d..f30af6751 100644 --- a/src/public/stylesheets/style.css +++ b/src/public/stylesheets/style.css @@ -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; diff --git a/src/services/import/tar.js b/src/services/import/tar.js index c61cf4d33..05d92d505 100644 --- a/src/services/import/tar.js +++ b/src/services/import/tar.js @@ -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(); } diff --git a/src/services/tree.js b/src/services/tree.js index 97bd5002a..7caebde9c 100644 --- a/src/services/tree.js +++ b/src/services/tree.js @@ -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;