server-ts: Port services/tree

This commit is contained in:
Elian Doran 2024-02-18 11:47:32 +02:00
parent d8d729342d
commit 0d4fb42731
No known key found for this signature in database
10 changed files with 43 additions and 35 deletions

View File

@ -267,17 +267,19 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
};
}
createClone(parentNoteId: string, notePosition: number) {
createClone(parentNoteId: string, notePosition?: number) {
const existingBranch = this.becca.getBranchFromChildAndParent(this.noteId, parentNoteId);
if (existingBranch) {
existingBranch.notePosition = notePosition;
if (notePosition) {
existingBranch.notePosition = notePosition;
}
return existingBranch;
} else {
return new BBranch({
noteId: this.noteId,
parentNoteId: parentNoteId,
notePosition: notePosition,
notePosition: notePosition || null,
prefix: this.prefix,
isExpanded: this.isExpanded
});

View File

@ -80,7 +80,7 @@ export interface BranchRow {
noteId: string;
parentNoteId: string;
prefix?: string | null;
notePosition: number | null;
notePosition?: number | null;
isExpanded?: boolean;
isDeleted?: boolean;
utcDateModified?: string;

View File

@ -3,7 +3,7 @@
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const entityChangesService = require('../../services/entity_changes');
const treeService = require('../../services/tree.js');
const treeService = require('../../services/tree');
const eraseService = require('../../services/erase');
const becca = require('../../becca/becca');
const TaskContext = require('../../services/task_context');

View File

@ -2,7 +2,7 @@
const noteService = require('../../services/notes');
const eraseService = require('../../services/erase');
const treeService = require('../../services/tree.js');
const treeService = require('../../services/tree');
const sql = require('../../services/sql');
const utils = require('../../services/utils');
const log = require('../../services/log');

View File

@ -4,7 +4,7 @@ const sql = require('./sql');
const utils = require('./utils');
const attributeService = require('./attributes');
const dateNoteService = require('./date_notes.js');
const treeService = require('./tree.js');
const treeService = require('./tree');
const config = require('./config');
const axios = require('axios');
const dayjs = require('dayjs');

View File

@ -1,4 +1,4 @@
const treeService = require('./tree.js');
const treeService = require('./tree');
const sql = require('./sql');
function moveBranchToNote(branchToMove, targetParentNoteId) {

View File

@ -2,7 +2,7 @@
const sql = require('./sql');
const eventChangesService = require('./entity_changes');
const treeService = require('./tree.js');
const treeService = require('./tree');
const BBranch = require('../becca/entities/bbranch');
const becca = require('../becca/becca');
const log = require('./log');

View File

@ -1,6 +1,6 @@
const eventService = require('./events');
const scriptService = require('./script.js');
const treeService = require('./tree.js');
const treeService = require('./tree');
const noteService = require('./notes');
const becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute');

View File

@ -9,7 +9,7 @@ const BBranch = require('../../becca/entities/bbranch');
const path = require('path');
const protectedSessionService = require('../protected_session');
const mimeService = require('./mime.js');
const treeService = require('../tree.js');
const treeService = require('../tree');
const yauzl = require("yauzl");
const htmlSanitizer = require('../html_sanitizer');
const becca = require('../../becca/becca');

View File

@ -1,12 +1,13 @@
"use strict";
const sql = require('./sql');
const log = require('./log');
const BBranch = require('../becca/entities/bbranch');
const entityChangesService = require('./entity_changes');
const becca = require('../becca/becca');
import sql = require('./sql');
import log = require('./log');
import BBranch = require('../becca/entities/bbranch');
import entityChangesService = require('./entity_changes');
import becca = require('../becca/becca');
import BNote = require('../becca/entities/bnote');
function validateParentChild(parentNoteId, childNoteId, branchId = null) {
function validateParentChild(parentNoteId: string, childNoteId: string, branchId: string | null = null) {
if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) {
return { branch: null, success: false, message: `Cannot change this note's location.` };
}
@ -25,7 +26,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
return {
branch: existingBranch,
success: false,
message: `Note "${childNote.title}" note already exists in the "${parentNote.title}".`
message: `Note "${childNote?.title}" note already exists in the "${parentNote?.title}".`
};
}
@ -37,7 +38,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
};
}
if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId).type === 'launcher') {
if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId)?.type === 'launcher') {
return {
branch: null,
success: false,
@ -51,7 +52,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
/**
* Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases.
*/
function wouldAddingBranchCreateCycle(parentNoteId, childNoteId) {
function wouldAddingBranchCreateCycle(parentNoteId: string, childNoteId: string) {
if (parentNoteId === childNoteId) {
return true;
}
@ -70,20 +71,22 @@ function wouldAddingBranchCreateCycle(parentNoteId, childNoteId) {
return parentAncestorNoteIds.some(parentAncestorNoteId => childSubtreeNoteIds.has(parentAncestorNoteId));
}
function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false, sortNatural = false, sortLocale) {
function sortNotes(parentNoteId: string, customSortBy: string = 'title', reverse = false, foldersFirst = false, sortNatural = false, _sortLocale?: string | null) {
if (!customSortBy) {
customSortBy = 'title';
}
if (!sortLocale) {
// sortLocale can not be empty string or null value, default value must be set to undefined.
sortLocale = undefined;
}
// sortLocale can not be empty string or null value, default value must be set to undefined.
const sortLocale = (_sortLocale || undefined);
sql.transactional(() => {
const notes = becca.getNote(parentNoteId).getChildNotes();
const note = becca.getNote(parentNoteId);
if (!note) {
throw new Error("Unable to find note");
}
const normalize = obj => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj;
const notes = note.getChildNotes();
const normalize = (obj: any) => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj;
notes.sort((a, b) => {
if (foldersFirst) {
@ -96,7 +99,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
}
}
function fetchValue(note, key) {
function fetchValue(note: BNote, key: string) {
let rawValue;
if (key === 'title') {
@ -105,14 +108,14 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
rawValue = prefix ? `${prefix} - ${note.title}` : note.title;
} else {
rawValue = ['dateCreated', 'dateModified'].includes(key)
? note[key]
? (note as any)[key]
: note.getLabelValue(key);
}
return normalize(rawValue);
}
function compare(a, b) {
function compare(a: string, b: string) {
if (!sortNatural) {
// alphabetical sort
return b === null || b === undefined || a < b ? -1 : 1;
@ -160,6 +163,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
for (const note of notes) {
const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId);
if (!branch) { continue; }
if (branch.noteId === '_hidden') {
position = 999_999_999;
@ -182,9 +186,8 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
});
}
function sortNotesIfNeeded(parentNoteId) {
function sortNotesIfNeeded(parentNoteId: string) {
const parentNote = becca.getNote(parentNoteId);
if (!parentNote) {
return;
}
@ -206,7 +209,7 @@ function sortNotesIfNeeded(parentNoteId) {
/**
* @deprecated this will be removed in the future
*/
function setNoteToParent(noteId, prefix, parentNoteId) {
function setNoteToParent(noteId: string, prefix: string, parentNoteId: string) {
const parentNote = becca.getNote(parentNoteId);
if (parentNoteId && !parentNote) {
@ -215,7 +218,7 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
}
// case where there might be more such branches is ignored. It's expected there should be just one
const branchId = sql.getValue("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
const branchId = sql.getValue<string>("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
const branch = becca.getBranch(branchId);
if (branch) {
@ -233,12 +236,15 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
}
else if (parentNoteId) {
const note = becca.getNote(noteId);
if (!note) {
throw new Error(`Cannot find note '${noteId}.`);
}
if (note.isDeleted) {
throw new Error(`Cannot create a branch for '${noteId}' which is deleted.`);
}
const branchId = sql.getValue('SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]);
const branchId = sql.getValue<string>('SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]);
const branch = becca.getBranch(branchId);
if (branch) {