2018-03-27 11:18:50 +08:00
|
|
|
import utils from "./utils.js";
|
|
|
|
import treeCache from "./tree_cache.js";
|
2019-08-27 02:21:43 +08:00
|
|
|
import ws from "./ws.js";
|
2018-12-12 04:53:56 +08:00
|
|
|
import hoistedNoteService from "./hoisted_note.js";
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2019-03-19 06:03:41 +08:00
|
|
|
async function prepareTree() {
|
2018-12-12 04:53:56 +08:00
|
|
|
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
|
2018-12-13 03:39:56 +08:00
|
|
|
|
|
|
|
let hoistedBranch;
|
|
|
|
|
|
|
|
if (hoistedNoteId === 'root') {
|
2019-10-26 15:58:00 +08:00
|
|
|
hoistedBranch = treeCache.getBranch('root');
|
2018-12-13 03:39:56 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const hoistedNote = await treeCache.getNote(hoistedNoteId);
|
|
|
|
hoistedBranch = (await hoistedNote.getBranches())[0];
|
|
|
|
}
|
2018-12-12 04:53:56 +08:00
|
|
|
|
|
|
|
return [ await prepareNode(hoistedBranch) ];
|
2018-03-27 11:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function prepareBranch(note) {
|
|
|
|
if (note.type === 'search') {
|
|
|
|
return await prepareSearchBranch(note);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return await prepareRealBranch(note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-02 03:11:11 +08:00
|
|
|
const NOTE_TYPE_ICONS = {
|
2019-11-02 19:17:00 +08:00
|
|
|
"file": "bx bx-file",
|
|
|
|
"image": "bx bx-image",
|
|
|
|
"code": "bx bx-code",
|
|
|
|
"render": "bx bx-extension",
|
|
|
|
"search": "bx bx-file-find",
|
|
|
|
"relation-map": "bx bx-map-alt",
|
|
|
|
"book": "bx bx-book"
|
2019-10-02 03:11:11 +08:00
|
|
|
};
|
|
|
|
|
2018-12-13 03:54:58 +08:00
|
|
|
async function getIcon(note) {
|
|
|
|
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
|
|
|
|
|
2018-11-09 18:57:52 +08:00
|
|
|
if (note.noteId === 'root') {
|
2019-11-02 19:17:00 +08:00
|
|
|
return "bx bx-chevrons-right";
|
2018-11-09 18:57:52 +08:00
|
|
|
}
|
2018-12-13 03:54:58 +08:00
|
|
|
else if (note.noteId === hoistedNoteId) {
|
2019-11-02 19:17:00 +08:00
|
|
|
return "bx bxs-arrow-from-bottom";
|
2018-12-13 03:54:58 +08:00
|
|
|
}
|
2018-11-09 18:57:52 +08:00
|
|
|
else if (note.type === 'text') {
|
|
|
|
if (note.hasChildren()) {
|
2019-11-02 19:17:00 +08:00
|
|
|
return "bx bx-folder";
|
2018-11-09 18:57:52 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-11-02 19:17:00 +08:00
|
|
|
return "bx bx-note";
|
2018-11-09 18:57:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 03:11:11 +08:00
|
|
|
else {
|
|
|
|
return NOTE_TYPE_ICONS[note.type];
|
2018-11-09 18:57:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 04:16:34 +08:00
|
|
|
async function prepareNode(branch) {
|
|
|
|
const note = await branch.getNote();
|
|
|
|
const title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title;
|
2018-12-13 03:54:58 +08:00
|
|
|
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
|
2018-05-27 04:16:34 +08:00
|
|
|
|
|
|
|
const node = {
|
|
|
|
noteId: note.noteId,
|
|
|
|
parentNoteId: branch.parentNoteId,
|
|
|
|
branchId: branch.branchId,
|
|
|
|
isProtected: note.isProtected,
|
2019-11-05 05:41:06 +08:00
|
|
|
noteType: note.type,
|
2018-05-27 04:16:34 +08:00
|
|
|
title: utils.escapeHtml(title),
|
|
|
|
extraClasses: await getExtraClasses(note),
|
2018-12-13 03:54:58 +08:00
|
|
|
icon: await getIcon(note),
|
2018-05-27 04:16:34 +08:00
|
|
|
refKey: note.noteId,
|
2019-03-19 04:59:53 +08:00
|
|
|
expanded: branch.isExpanded || hoistedNoteId === note.noteId,
|
2019-06-29 03:50:15 +08:00
|
|
|
lazy: true,
|
|
|
|
key: utils.randomString(12) // this should prevent some "duplicate key" errors
|
2018-05-27 04:16:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (note.hasChildren() || note.type === 'search') {
|
|
|
|
node.folder = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-03-27 11:18:50 +08:00
|
|
|
async function prepareRealBranch(parentNote) {
|
|
|
|
utils.assertArguments(parentNote);
|
|
|
|
|
|
|
|
const childBranches = await parentNote.getChildBranches();
|
|
|
|
|
|
|
|
if (!childBranches) {
|
2019-08-27 02:21:43 +08:00
|
|
|
ws.logError(`No children for ${parentNote}. This shouldn't happen.`);
|
2018-03-27 11:18:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const noteList = [];
|
|
|
|
|
|
|
|
for (const branch of childBranches) {
|
2018-05-27 04:16:34 +08:00
|
|
|
const node = await prepareNode(branch);
|
2018-03-27 11:18:50 +08:00
|
|
|
|
|
|
|
noteList.push(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return noteList;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function prepareSearchBranch(note) {
|
2019-11-05 03:20:21 +08:00
|
|
|
await treeCache.reloadNotes([note.noteId]);
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2019-10-28 02:17:32 +08:00
|
|
|
const newNote = await treeCache.getNote(note.noteId);
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2019-10-28 02:17:32 +08:00
|
|
|
return await prepareRealBranch(newNote);
|
2018-03-27 11:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getExtraClasses(note) {
|
|
|
|
utils.assertArguments(note);
|
|
|
|
|
|
|
|
const extraClasses = [];
|
|
|
|
|
|
|
|
if (note.isProtected) {
|
|
|
|
extraClasses.push("protected");
|
|
|
|
}
|
|
|
|
|
2018-04-17 11:34:56 +08:00
|
|
|
if (note.getParentNoteIds().length > 1) {
|
2018-03-27 11:18:50 +08:00
|
|
|
extraClasses.push("multiple-parents");
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:59:31 +08:00
|
|
|
if (note.cssClass) {
|
|
|
|
extraClasses.push(note.cssClass);
|
|
|
|
}
|
|
|
|
|
2019-01-29 04:42:37 +08:00
|
|
|
extraClasses.push(utils.getNoteTypeClass(note.type));
|
2018-03-27 11:18:50 +08:00
|
|
|
|
2019-01-14 03:14:33 +08:00
|
|
|
if (note.mime) { // some notes should not have mime type (e.g. render)
|
2019-01-29 04:42:37 +08:00
|
|
|
extraClasses.push(utils.getMimeTypeClass(note.mime));
|
2019-01-14 03:14:33 +08:00
|
|
|
}
|
|
|
|
|
2019-10-16 01:16:44 +08:00
|
|
|
if (note.archived) {
|
|
|
|
extraClasses.push("archived");
|
|
|
|
}
|
|
|
|
|
2018-03-27 11:18:50 +08:00
|
|
|
return extraClasses.join(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
prepareTree,
|
|
|
|
prepareBranch,
|
2018-11-14 15:42:00 +08:00
|
|
|
getExtraClasses,
|
|
|
|
getIcon
|
2018-03-27 11:18:50 +08:00
|
|
|
}
|