mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-27 10:01:37 +08:00
client-ts: Port services/tree.js
This commit is contained in:
parent
cbdb8b8b18
commit
e872e88376
@ -4,10 +4,18 @@ import froca from './froca.js';
|
|||||||
import hoistedNoteService from '../services/hoisted_note.js';
|
import hoistedNoteService from '../services/hoisted_note.js';
|
||||||
import appContext from "../components/app_context.js";
|
import appContext from "../components/app_context.js";
|
||||||
|
|
||||||
|
export interface Node {
|
||||||
|
getParent(): Node;
|
||||||
|
data: {
|
||||||
|
noteId?: string;
|
||||||
|
isProtected?: boolean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {string|null}
|
* @returns {string|null}
|
||||||
*/
|
*/
|
||||||
async function resolveNotePath(notePath, hoistedNoteId = 'root') {
|
async function resolveNotePath(notePath: string, hoistedNoteId = 'root') {
|
||||||
const runPath = await resolveNotePathToSegments(notePath, hoistedNoteId);
|
const runPath = await resolveNotePathToSegments(notePath, hoistedNoteId);
|
||||||
|
|
||||||
return runPath ? runPath.join("/") : null;
|
return runPath ? runPath.join("/") : null;
|
||||||
@ -17,10 +25,8 @@ async function resolveNotePath(notePath, hoistedNoteId = 'root') {
|
|||||||
* Accepts notePath which might or might not be valid and returns an existing path as close to the original
|
* Accepts notePath which might or might not be valid and returns an existing path as close to the original
|
||||||
* notePath as possible. Part of the path might not be valid because of note moving (which causes
|
* notePath as possible. Part of the path might not be valid because of note moving (which causes
|
||||||
* path change) or other corruption, in that case, this will try to get some other valid path to the correct note.
|
* path change) or other corruption, in that case, this will try to get some other valid path to the correct note.
|
||||||
*
|
|
||||||
* @returns {Promise<string[]>}
|
|
||||||
*/
|
*/
|
||||||
async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logErrors = true) {
|
async function resolveNotePathToSegments(notePath: string, hoistedNoteId = 'root', logErrors = true) {
|
||||||
utils.assertArguments(notePath);
|
utils.assertArguments(notePath);
|
||||||
|
|
||||||
// we might get notePath with the params suffix, remove it if present
|
// we might get notePath with the params suffix, remove it if present
|
||||||
@ -103,8 +109,14 @@ async function resolveNotePathToSegments(notePath, hoistedNoteId = 'root', logEr
|
|||||||
return effectivePathSegments;
|
return effectivePathSegments;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const note = await froca.getNote(getNoteIdFromUrl(notePath));
|
const noteId = getNoteIdFromUrl(notePath);
|
||||||
|
if (!noteId) {
|
||||||
|
throw new Error(`Unable to find note with ID: ${noteId}.`);
|
||||||
|
}
|
||||||
|
const note = await froca.getNote(noteId);
|
||||||
|
if (!note) {
|
||||||
|
throw new Error(`Unable to find note: ${notePath}.`);
|
||||||
|
}
|
||||||
const bestNotePath = note.getBestNotePath(hoistedNoteId);
|
const bestNotePath = note.getBestNotePath(hoistedNoteId);
|
||||||
|
|
||||||
if (!bestNotePath) {
|
if (!bestNotePath) {
|
||||||
@ -128,11 +140,11 @@ ws.subscribeToMessages(message => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function getParentProtectedStatus(node) {
|
function getParentProtectedStatus(node: Node) {
|
||||||
return hoistedNoteService.isHoistedNode(node) ? false : node.getParent().data.isProtected;
|
return hoistedNoteService.isHoistedNode(node) ? false : node.getParent().data.isProtected;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNoteIdFromUrl(urlOrNotePath) {
|
function getNoteIdFromUrl(urlOrNotePath: string) {
|
||||||
if (!urlOrNotePath) {
|
if (!urlOrNotePath) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -143,13 +155,16 @@ function getNoteIdFromUrl(urlOrNotePath) {
|
|||||||
return segments[segments.length - 1];
|
return segments[segments.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getBranchIdFromUrl(urlOrNotePath) {
|
async function getBranchIdFromUrl(urlOrNotePath: string) {
|
||||||
const {noteId, parentNoteId} = getNoteIdAndParentIdFromUrl(urlOrNotePath);
|
const {noteId, parentNoteId} = getNoteIdAndParentIdFromUrl(urlOrNotePath);
|
||||||
|
if (!parentNoteId) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return await froca.getBranchId(parentNoteId, noteId);
|
return await froca.getBranchId(parentNoteId, noteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNoteIdAndParentIdFromUrl(urlOrNotePath) {
|
function getNoteIdAndParentIdFromUrl(urlOrNotePath: string) {
|
||||||
if (!urlOrNotePath) {
|
if (!urlOrNotePath) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@ -182,7 +197,7 @@ function getNoteIdAndParentIdFromUrl(urlOrNotePath) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNotePath(node) {
|
function getNotePath(node: Node) {
|
||||||
if (!node) {
|
if (!node) {
|
||||||
logError("Node is null");
|
logError("Node is null");
|
||||||
return "";
|
return "";
|
||||||
@ -201,7 +216,7 @@ function getNotePath(node) {
|
|||||||
return path.reverse().join("/");
|
return path.reverse().join("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNoteTitle(noteId, parentNoteId = null) {
|
async function getNoteTitle(noteId: string, parentNoteId: string | null = null) {
|
||||||
utils.assertArguments(noteId);
|
utils.assertArguments(noteId);
|
||||||
|
|
||||||
const note = await froca.getNote(noteId);
|
const note = await froca.getNote(noteId);
|
||||||
@ -226,7 +241,7 @@ async function getNoteTitle(noteId, parentNoteId = null) {
|
|||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNotePathTitleComponents(notePath) {
|
async function getNotePathTitleComponents(notePath: string) {
|
||||||
const titleComponents = [];
|
const titleComponents = [];
|
||||||
|
|
||||||
if (notePath.startsWith('root/')) {
|
if (notePath.startsWith('root/')) {
|
||||||
@ -249,7 +264,7 @@ async function getNotePathTitleComponents(notePath) {
|
|||||||
return titleComponents;
|
return titleComponents;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNotePathTitle(notePath) {
|
async function getNotePathTitle(notePath: string) {
|
||||||
utils.assertArguments(notePath);
|
utils.assertArguments(notePath);
|
||||||
|
|
||||||
const titlePath = await getNotePathTitleComponents(notePath);
|
const titlePath = await getNotePathTitleComponents(notePath);
|
||||||
@ -257,7 +272,7 @@ async function getNotePathTitle(notePath) {
|
|||||||
return titlePath.join(' / ');
|
return titlePath.join(' / ');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNoteTitleWithPathAsSuffix(notePath) {
|
async function getNoteTitleWithPathAsSuffix(notePath: string) {
|
||||||
utils.assertArguments(notePath);
|
utils.assertArguments(notePath);
|
||||||
|
|
||||||
const titleComponents = await getNotePathTitleComponents(notePath);
|
const titleComponents = await getNotePathTitleComponents(notePath);
|
||||||
@ -280,7 +295,7 @@ async function getNoteTitleWithPathAsSuffix(notePath) {
|
|||||||
return $titleWithPath;
|
return $titleWithPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNotePathInHiddenSubtree(notePath) {
|
function isNotePathInHiddenSubtree(notePath: string) {
|
||||||
return notePath?.includes("root/_hidden");
|
return notePath?.includes("root/_hidden");
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user