2018-12-22 22:16:32 +01:00
|
|
|
import server from '../services/server.js';
|
2020-05-02 18:19:41 +02:00
|
|
|
import noteAttributeCache from "../services/note_attribute_cache.js";
|
2021-02-13 20:07:08 +01:00
|
|
|
import ws from "../services/ws.js";
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from "../services/froca.js";
|
2022-07-28 23:59:41 +02:00
|
|
|
import protectedSessionHolder from "../services/protected_session_holder.js";
|
2022-09-25 14:19:30 +02:00
|
|
|
import cssClassManager from "../services/css_class_manager.js";
|
2024-07-25 20:36:15 +03:00
|
|
|
import { Froca } from '../services/froca-interface.js';
|
|
|
|
import FAttachment from './fattachment.js';
|
|
|
|
import FAttribute, { AttributeType } from './fattribute.js';
|
2018-12-22 22:16:32 +01:00
|
|
|
|
|
|
|
const LABEL = 'label';
|
|
|
|
const RELATION = 'relation';
|
|
|
|
|
2020-10-14 23:14:04 +02:00
|
|
|
const NOTE_TYPE_ICONS = {
|
|
|
|
"file": "bx bx-file",
|
|
|
|
"image": "bx bx-image",
|
|
|
|
"code": "bx bx-code",
|
|
|
|
"render": "bx bx-extension",
|
|
|
|
"search": "bx bx-file-find",
|
2024-09-09 20:30:35 +03:00
|
|
|
"relationMap": "bx bxs-network-chart",
|
2021-09-20 22:19:47 +02:00
|
|
|
"book": "bx bx-book",
|
2024-09-09 20:30:35 +03:00
|
|
|
"noteMap": "bx bxs-network-chart",
|
2021-11-25 13:47:56 +01:00
|
|
|
"mermaid": "bx bx-selection",
|
2022-05-28 22:19:29 +02:00
|
|
|
"canvas": "bx bx-pen",
|
2022-12-06 23:01:42 +01:00
|
|
|
"webView": "bx bx-globe-alt",
|
2022-12-01 10:16:57 +01:00
|
|
|
"launcher": "bx bx-link",
|
2022-12-06 16:23:30 +01:00
|
|
|
"doc": "bx bxs-file-doc",
|
2024-09-01 14:16:45 +03:00
|
|
|
"contentWidget": "bx bxs-widget",
|
|
|
|
"mindMap": "bx bx-sitemap"
|
2020-10-14 23:14:04 +02:00
|
|
|
};
|
|
|
|
|
2023-08-21 04:15:53 -04:00
|
|
|
/**
|
|
|
|
* There are many different Note types, some of which are entirely opaque to the
|
|
|
|
* end user. Those types should be used only for checking against, they are
|
|
|
|
* not for direct use.
|
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
type NoteType = "file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code";
|
|
|
|
|
|
|
|
interface NotePathRecord {
|
|
|
|
isArchived: boolean;
|
|
|
|
isInHoistedSubTree: boolean;
|
|
|
|
isSearch: boolean;
|
|
|
|
notePath: string[];
|
|
|
|
isHidden: boolean;
|
|
|
|
}
|
2023-08-21 04:15:53 -04:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
export interface FNoteRow {
|
|
|
|
noteId: string;
|
|
|
|
title: string;
|
|
|
|
isProtected: boolean;
|
|
|
|
type: NoteType;
|
|
|
|
mime: string;
|
|
|
|
blobId: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NoteMetaData {
|
|
|
|
dateCreated: string;
|
|
|
|
utcDateCreated: string;
|
|
|
|
dateModified: string;
|
|
|
|
utcDateModified: string;
|
|
|
|
}
|
2023-08-21 04:15:53 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Note is the main node and concept in Trilium.
|
|
|
|
*/
|
2023-01-03 13:35:10 +01:00
|
|
|
class FNote {
|
2024-07-25 20:36:15 +03:00
|
|
|
|
|
|
|
private froca: Froca;
|
|
|
|
|
|
|
|
noteId!: string;
|
|
|
|
title!: string;
|
|
|
|
isProtected!: boolean;
|
|
|
|
type!: NoteType;
|
2019-10-26 09:51:08 +02:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* content-type, e.g. "application/json"
|
2019-10-26 09:51:08 +02:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
mime!: string;
|
|
|
|
// the main use case to keep this is to detect content change which should trigger refresh
|
|
|
|
blobId!: string;
|
2020-01-29 22:32:22 +01:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
attributes: string[];
|
|
|
|
targetRelations: string[];
|
|
|
|
parents: string[];
|
|
|
|
children: string[];
|
2020-01-30 22:38:31 +01:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
parentToBranch: Record<string, string>;
|
|
|
|
childToBranch: Record<string, string>;
|
|
|
|
attachments: FAttachment[] | null;
|
|
|
|
|
|
|
|
// Managed by Froca.
|
|
|
|
searchResultsLoaded?: boolean;
|
|
|
|
highlightedTokens?: unknown;
|
2020-01-30 22:38:31 +01:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
constructor(froca: Froca, row: FNoteRow) {
|
|
|
|
this.froca = froca;
|
|
|
|
this.attributes = [];
|
|
|
|
this.targetRelations = [];
|
2020-01-30 22:38:31 +01:00
|
|
|
this.parents = [];
|
|
|
|
this.children = [];
|
|
|
|
|
|
|
|
this.parentToBranch = {};
|
|
|
|
this.childToBranch = {};
|
|
|
|
|
2023-04-03 23:47:24 +02:00
|
|
|
this.attachments = null; // lazy loaded
|
|
|
|
|
2020-01-31 20:52:31 +01:00
|
|
|
this.update(row);
|
2020-01-29 22:32:22 +01:00
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
update(row: FNoteRow) {
|
2018-03-25 12:29:00 -04:00
|
|
|
this.noteId = row.noteId;
|
|
|
|
this.title = row.title;
|
2020-03-08 09:38:49 +01:00
|
|
|
this.isProtected = !!row.isProtected;
|
2018-03-25 12:29:00 -04:00
|
|
|
this.type = row.type;
|
2024-07-25 20:36:15 +03:00
|
|
|
|
2018-03-25 12:29:00 -04:00
|
|
|
this.mime = row.mime;
|
2023-11-13 23:53:14 +01:00
|
|
|
|
|
|
|
this.blobId = row.blobId;
|
2019-10-26 09:51:08 +02:00
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
addParent(parentNoteId: string, branchId: string, sort = true) {
|
2020-08-28 14:29:20 +02:00
|
|
|
if (parentNoteId === 'none') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-26 09:51:08 +02:00
|
|
|
if (!this.parents.includes(parentNoteId)) {
|
|
|
|
this.parents.push(parentNoteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parentToBranch[parentNoteId] = branchId;
|
2023-02-28 23:23:17 +01:00
|
|
|
|
|
|
|
if (sort) {
|
|
|
|
this.sortParents();
|
|
|
|
}
|
2019-10-26 09:51:08 +02:00
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
addChild(childNoteId: string, branchId: string, sort = true) {
|
2020-12-14 22:12:26 +01:00
|
|
|
if (!(childNoteId in this.childToBranch)) {
|
2019-10-26 09:51:08 +02:00
|
|
|
this.children.push(childNoteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.childToBranch[childNoteId] = branchId;
|
|
|
|
|
2020-12-10 16:10:10 +01:00
|
|
|
if (sort) {
|
|
|
|
this.sortChildren();
|
|
|
|
}
|
2020-09-14 22:48:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sortChildren() {
|
2024-07-25 20:36:15 +03:00
|
|
|
const branchIdPos: Record<string, number> = {};
|
2019-10-26 09:51:08 +02:00
|
|
|
|
|
|
|
for (const branchId of Object.values(this.childToBranch)) {
|
2024-07-25 20:36:15 +03:00
|
|
|
const notePosition = this.froca.getBranch(branchId)?.notePosition;
|
|
|
|
if (notePosition) {
|
|
|
|
branchIdPos[branchId] = notePosition;
|
|
|
|
}
|
2019-10-26 09:51:08 +02:00
|
|
|
}
|
|
|
|
|
2023-11-03 01:11:47 +01:00
|
|
|
this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] - branchIdPos[this.childToBranch[b]]);
|
2018-03-25 12:29:00 -04:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:25:17 -04:00
|
|
|
isJson() {
|
|
|
|
return this.mime === "application/json";
|
|
|
|
}
|
|
|
|
|
2019-09-06 23:36:08 +02:00
|
|
|
async getContent() {
|
2023-09-05 22:02:26 +02:00
|
|
|
const blob = await this.getBlob();
|
2019-09-06 23:36:08 +02:00
|
|
|
|
2023-09-05 22:02:26 +02:00
|
|
|
return blob?.content;
|
2019-09-06 23:36:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async getJsonContent() {
|
|
|
|
const content = await this.getContent();
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
if (typeof content !== "string") {
|
|
|
|
console.log(`Unknown note content for '${this.noteId}'.`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-09-06 23:36:08 +02:00
|
|
|
try {
|
|
|
|
return JSON.parse(content);
|
|
|
|
}
|
2024-07-25 20:36:15 +03:00
|
|
|
catch (e: any) {
|
2022-04-19 23:36:21 +02:00
|
|
|
console.log(`Cannot parse content of note '${this.noteId}': `, e.message);
|
2019-09-06 23:36:08 +02:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:30:47 +01:00
|
|
|
getParentBranchIds() {
|
2020-02-03 20:07:34 +01:00
|
|
|
return Object.values(this.parentToBranch);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:30:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated use getParentBranchIds() instead
|
|
|
|
*/
|
|
|
|
getBranchIds() {
|
|
|
|
return this.getParentBranchIds();
|
|
|
|
}
|
|
|
|
|
|
|
|
getParentBranches() {
|
2019-10-26 09:51:08 +02:00
|
|
|
const branchIds = Object.values(this.parentToBranch);
|
2018-03-25 12:29:00 -04:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
return this.froca.getBranches(branchIds);
|
2018-03-25 12:29:00 -04:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:30:47 +01:00
|
|
|
/**
|
|
|
|
* @deprecated use getParentBranches() instead
|
|
|
|
*/
|
|
|
|
getBranches() {
|
|
|
|
return this.getParentBranches();
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:40:18 -04:00
|
|
|
hasChildren() {
|
2019-10-26 09:51:08 +02:00
|
|
|
return this.children.length > 0;
|
2018-04-16 20:40:18 -04:00
|
|
|
}
|
|
|
|
|
2020-03-18 22:35:54 +01:00
|
|
|
getChildBranches() {
|
2019-10-27 22:39:38 +01:00
|
|
|
// don't use Object.values() to guarantee order
|
|
|
|
const branchIds = this.children.map(childNoteId => this.childToBranch[childNoteId]);
|
2018-03-25 12:29:00 -04:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
return this.froca.getBranches(branchIds);
|
2018-03-25 12:29:00 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 23:34:56 -04:00
|
|
|
getParentNoteIds() {
|
2019-10-26 09:51:08 +02:00
|
|
|
return this.parents;
|
2018-04-16 20:40:18 -04:00
|
|
|
}
|
|
|
|
|
2020-03-18 22:35:54 +01:00
|
|
|
getParentNotes() {
|
2021-04-16 22:57:37 +02:00
|
|
|
return this.froca.getNotesFromCache(this.parents);
|
2018-04-16 23:34:56 -04:00
|
|
|
}
|
|
|
|
|
2021-02-24 22:38:26 +01:00
|
|
|
// will sort the parents so that non-search & non-archived are first and archived at the end
|
2023-05-05 23:41:11 +02:00
|
|
|
// this is done so that non-search & non-archived paths are always explored as first when looking for a note path
|
2023-02-28 23:23:17 +01:00
|
|
|
sortParents() {
|
2021-02-24 22:38:26 +01:00
|
|
|
this.parents.sort((aNoteId, bNoteId) => {
|
|
|
|
const aBranchId = this.parentToBranch[aNoteId];
|
|
|
|
|
|
|
|
if (aBranchId && aBranchId.startsWith('virt-')) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-02-28 23:23:17 +01:00
|
|
|
const aNote = this.froca.getNoteFromCache(aNoteId);
|
2021-02-24 22:38:26 +01:00
|
|
|
|
2023-01-27 16:57:23 +01:00
|
|
|
if (aNote.isArchived || aNote.isHiddenCompletely()) {
|
2021-02-24 22:38:26 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-11-03 08:58:57 +01:00
|
|
|
return aNoteId < bNoteId ? -1 : 1;
|
2021-02-24 22:38:26 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:57:23 +01:00
|
|
|
get isArchived() {
|
|
|
|
return this.hasAttribute('label', 'archived');
|
|
|
|
}
|
|
|
|
|
2018-04-16 23:34:56 -04:00
|
|
|
getChildNoteIds() {
|
2019-10-26 09:51:08 +02:00
|
|
|
return this.children;
|
2018-03-25 12:29:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async getChildNotes() {
|
2021-04-16 22:57:37 +02:00
|
|
|
return await this.froca.getNotes(this.children);
|
2018-03-25 12:29:00 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 23:47:24 +02:00
|
|
|
async getAttachments() {
|
|
|
|
if (!this.attachments) {
|
2023-05-29 00:19:54 +02:00
|
|
|
this.attachments = await this.froca.getAttachmentsForNote(this.noteId);
|
2023-04-03 23:47:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.attachments;
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
async getAttachmentsByRole(role: string) {
|
2023-09-08 21:53:57 +02:00
|
|
|
return (await this.getAttachments())
|
|
|
|
.filter(attachment => attachment.role === role);
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
async getAttachmentById(attachmentId: string) {
|
2023-04-03 23:47:24 +02:00
|
|
|
const attachments = await this.getAttachments();
|
|
|
|
|
|
|
|
return attachments.find(att => att.attachmentId === attachmentId);
|
|
|
|
}
|
|
|
|
|
2023-05-02 22:46:39 +02:00
|
|
|
isEligibleForConversionToAttachment() {
|
|
|
|
if (this.type !== 'image' || !this.isContentAvailable() || this.hasChildren() || this.getParentBranches().length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetRelations = this.getTargetRelations().filter(relation => relation.name === 'imageLink');
|
|
|
|
|
2023-05-04 22:16:18 +02:00
|
|
|
if (targetRelations.length > 1) {
|
2023-05-02 22:46:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentNote = this.getParentNotes()[0]; // at this point note can have only one parent
|
2023-05-04 22:16:18 +02:00
|
|
|
const referencingNote = targetRelations[0]?.getNote();
|
2023-05-02 22:46:39 +02:00
|
|
|
|
2023-05-04 22:16:18 +02:00
|
|
|
if (referencingNote && referencingNote !== parentNote) {
|
|
|
|
return false;
|
|
|
|
} else if (parentNote.type !== 'text' || !parentNote.isContentAvailable()) {
|
2023-05-02 22:46:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [type] - attribute type to filter
|
|
|
|
* @param [name] - attribute name to filter
|
|
|
|
* @returns all note's attributes, including inherited ones
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedAttributes(type?: AttributeType, name?: string) {
|
2020-01-25 13:27:23 +01:00
|
|
|
const attrs = this.attributes
|
2021-04-16 22:57:37 +02:00
|
|
|
.map(attributeId => this.froca.attributes[attributeId])
|
2020-05-02 18:19:41 +02:00
|
|
|
.filter(Boolean); // filter out nulls;
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2020-05-02 18:19:41 +02:00
|
|
|
return this.__filterAttrs(attrs, type, name);
|
2020-01-25 13:27:23 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [type] - attribute type to filter
|
|
|
|
* @param [name] - attribute name to filter
|
|
|
|
* @returns all note's attributes, including inherited ones
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getAttributes(type?: AttributeType, name?: string) {
|
2020-06-04 12:27:41 +02:00
|
|
|
return this.__filterAttrs(this.__getCachedAttributes([]), type, name);
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
__getCachedAttributes(path: string[]): FAttribute[] {
|
2020-06-04 12:27:41 +02:00
|
|
|
// notes/clones cannot form tree cycles, it is possible to create attribute inheritance cycle via templates
|
|
|
|
// when template instance is a parent of template itself
|
|
|
|
if (path.includes(this.noteId)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-06-27 00:40:35 +02:00
|
|
|
if (!(this.noteId in noteAttributeCache.attributes)) {
|
2020-06-04 12:27:41 +02:00
|
|
|
const newPath = [...path, this.noteId];
|
2020-06-27 00:40:35 +02:00
|
|
|
const attrArrs = [ this.getOwnedAttributes() ];
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2023-01-17 22:14:53 +01:00
|
|
|
// inheritable attrs on root are typically not intended to be applied to hidden subtree #3537
|
|
|
|
if (this.noteId !== 'root' && this.noteId !== '_hidden') {
|
2020-05-02 18:19:41 +02:00
|
|
|
for (const parentNote of this.getParentNotes()) {
|
2021-04-16 22:57:37 +02:00
|
|
|
// these virtual parent-child relationships are also loaded into froca
|
2020-05-02 18:19:41 +02:00
|
|
|
if (parentNote.type !== 'search') {
|
2020-06-04 12:27:41 +02:00
|
|
|
attrArrs.push(parentNote.__getInheritableAttributes(newPath));
|
2020-05-02 18:19:41 +02:00
|
|
|
}
|
2020-03-29 20:37:40 +02:00
|
|
|
}
|
2020-01-25 13:27:23 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 20:31:55 +01:00
|
|
|
for (const templateAttr of attrArrs.flat().filter(attr => attr.type === 'relation' && ['template', 'inherit'].includes(attr.name))) {
|
2021-04-16 22:57:37 +02:00
|
|
|
const templateNote = this.froca.notes[templateAttr.value];
|
2020-06-27 00:40:35 +02:00
|
|
|
|
|
|
|
if (templateNote && templateNote.noteId !== this.noteId) {
|
2022-06-19 11:36:29 +02:00
|
|
|
attrArrs.push(
|
|
|
|
templateNote.__getCachedAttributes(newPath)
|
|
|
|
// template attr is used as a marker for templates, but it's not meant to be inherited
|
2022-09-16 23:03:02 +02:00
|
|
|
.filter(attr => !(attr.type === 'label' && (attr.name === 'template' || attr.name === 'workspacetemplate')))
|
2022-06-19 11:36:29 +02:00
|
|
|
);
|
2020-06-27 00:40:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:34:18 +02:00
|
|
|
noteAttributeCache.attributes[this.noteId] = [];
|
|
|
|
const addedAttributeIds = new Set();
|
|
|
|
|
|
|
|
for (const attr of attrArrs.flat()) {
|
|
|
|
if (!addedAttributeIds.has(attr.attributeId)) {
|
|
|
|
addedAttributeIds.add(attr.attributeId);
|
|
|
|
|
|
|
|
noteAttributeCache.attributes[this.noteId].push(attr);
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 18:19:41 +02:00
|
|
|
}
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2020-06-04 12:27:41 +02:00
|
|
|
return noteAttributeCache.attributes[this.noteId];
|
2020-01-25 13:27:23 +01:00
|
|
|
}
|
|
|
|
|
2021-07-20 13:29:11 +02:00
|
|
|
isRoot() {
|
2022-12-14 23:44:26 +01:00
|
|
|
return this.noteId === 'root';
|
2021-07-20 13:29:11 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
/**
|
|
|
|
* Gives all possible note paths leading to this note. Paths containing search note are ignored (could form cycles)
|
|
|
|
*
|
2024-07-25 20:36:15 +03:00
|
|
|
* @returns array of notePaths (each represented by array of noteIds constituting the particular note path)
|
2023-04-15 00:06:13 +02:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getAllNotePaths(): string[][] {
|
2021-03-06 20:23:29 +01:00
|
|
|
if (this.noteId === 'root') {
|
|
|
|
return [['root']];
|
|
|
|
}
|
|
|
|
|
2023-05-23 09:30:55 +08:00
|
|
|
const parentNotes = this.getParentNotes().filter(note => note.type !== 'search');
|
2021-03-06 20:23:29 +01:00
|
|
|
|
2023-05-05 23:17:23 +02:00
|
|
|
const notePaths = parentNotes.length === 1
|
2023-05-05 23:41:11 +02:00
|
|
|
? parentNotes[0].getAllNotePaths() // optimization for the most common case
|
2023-05-05 23:17:23 +02:00
|
|
|
: parentNotes.flatMap(parentNote => parentNote.getAllNotePaths());
|
2021-03-06 20:23:29 +01:00
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
for (const notePath of notePaths) {
|
|
|
|
notePath.push(this.noteId);
|
2021-03-06 20:23:29 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
return notePaths;
|
2021-03-06 20:23:29 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
getSortedNotePathRecords(hoistedNoteId = 'root') {
|
|
|
|
const isHoistedRoot = hoistedNoteId === 'root';
|
|
|
|
|
2021-03-08 00:04:43 +01:00
|
|
|
const notePaths = this.getAllNotePaths().map(path => ({
|
|
|
|
notePath: path,
|
2023-04-15 00:06:13 +02:00
|
|
|
isInHoistedSubTree: isHoistedRoot || path.includes(hoistedNoteId),
|
|
|
|
isArchived: path.some(noteId => froca.notes[noteId].isArchived),
|
2023-06-29 23:32:19 +02:00
|
|
|
isSearch: path.find(noteId => froca.notes[noteId].type === 'search'),
|
2022-12-21 16:11:00 +01:00
|
|
|
isHidden: path.includes('_hidden')
|
2021-03-08 00:04:43 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
notePaths.sort((a, b) => {
|
|
|
|
if (a.isInHoistedSubTree !== b.isInHoistedSubTree) {
|
|
|
|
return a.isInHoistedSubTree ? -1 : 1;
|
|
|
|
} else if (a.isArchived !== b.isArchived) {
|
|
|
|
return a.isArchived ? 1 : -1;
|
2022-12-16 16:00:49 +01:00
|
|
|
} else if (a.isHidden !== b.isHidden) {
|
|
|
|
return a.isHidden ? 1 : -1;
|
2023-06-29 23:32:19 +02:00
|
|
|
} else if (a.isSearch !== b.isSearch) {
|
|
|
|
return a.isSearch ? 1 : -1;
|
2021-03-08 00:04:43 +01:00
|
|
|
} else {
|
|
|
|
return a.notePath.length - b.notePath.length;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-08 22:04:52 +01:00
|
|
|
return notePaths;
|
2021-03-08 00:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-15 00:06:13 +02:00
|
|
|
/**
|
2023-05-05 23:41:11 +02:00
|
|
|
* Returns the note path considered to be the "best"
|
2023-04-15 00:06:13 +02:00
|
|
|
*
|
|
|
|
* @param {string} [hoistedNoteId='root']
|
|
|
|
* @return {string[]} array of noteIds constituting the particular note path
|
|
|
|
*/
|
|
|
|
getBestNotePath(hoistedNoteId = 'root') {
|
|
|
|
return this.getSortedNotePathRecords(hoistedNoteId)[0]?.notePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-05 23:41:11 +02:00
|
|
|
* Returns the note path considered to be the "best"
|
2023-04-15 00:06:13 +02:00
|
|
|
*
|
|
|
|
* @param {string} [hoistedNoteId='root']
|
|
|
|
* @return {string} serialized note path (e.g. 'root/a1h315/js725h')
|
|
|
|
*/
|
|
|
|
getBestNotePathString(hoistedNoteId = 'root') {
|
|
|
|
const notePath = this.getBestNotePath(hoistedNoteId);
|
|
|
|
|
|
|
|
return notePath?.join("/");
|
|
|
|
}
|
|
|
|
|
2023-01-13 10:09:41 +01:00
|
|
|
/**
|
|
|
|
* @return boolean - true if there's no non-hidden path, note is not cloned to the visible tree
|
|
|
|
*/
|
|
|
|
isHiddenCompletely() {
|
2023-07-15 09:50:07 +02:00
|
|
|
if (this.noteId === '_hidden') {
|
|
|
|
return true;
|
|
|
|
} else if (this.noteId === 'root') {
|
2023-01-27 16:57:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const parentNote of this.getParentNotes()) {
|
|
|
|
if (parentNote.noteId === 'root') {
|
|
|
|
return false;
|
2023-05-30 23:54:11 +02:00
|
|
|
} else if (parentNote.noteId === '_hidden' || parentNote.type === 'search') {
|
2023-01-27 16:57:23 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentNote.isHiddenCompletely()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2023-01-13 10:09:41 +01:00
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
/**
|
2023-04-15 00:06:13 +02:00
|
|
|
* @private
|
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
__filterAttrs(attributes: FAttribute[], type?: AttributeType, name?: string): FAttribute[] {
|
2022-12-17 21:46:51 +01:00
|
|
|
this.__validateTypeName(type, name);
|
|
|
|
|
2020-05-02 18:19:41 +02:00
|
|
|
if (!type && !name) {
|
|
|
|
return attributes;
|
|
|
|
} else if (type && name) {
|
2023-01-27 16:59:59 +01:00
|
|
|
return attributes.filter(attr => attr.name === name && attr.type === type);
|
2020-01-25 13:27:23 +01:00
|
|
|
} else if (type) {
|
|
|
|
return attributes.filter(attr => attr.type === type);
|
|
|
|
} else if (name) {
|
|
|
|
return attributes.filter(attr => attr.name === name);
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
2024-07-25 20:36:15 +03:00
|
|
|
|
|
|
|
return [];
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
__getInheritableAttributes(path: string[]) {
|
2020-06-04 12:27:41 +02:00
|
|
|
const attrs = this.__getCachedAttributes(path);
|
2020-01-25 13:27:23 +01:00
|
|
|
|
|
|
|
return attrs.filter(attr => attr.isInheritable);
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
__validateTypeName(type?: string, name?: string) {
|
2022-12-17 21:46:51 +01:00
|
|
|
if (type && type !== 'label' && type !== 'relation') {
|
|
|
|
throw new Error(`Unrecognized attribute type '${type}'. Only 'label' and 'relation' are possible values.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
const firstLetter = name.charAt(0);
|
|
|
|
if (firstLetter === '#' || firstLetter === '~') {
|
|
|
|
throw new Error(`Detect '#' or '~' in the attribute's name. In the API, attribute names should be set without these characters.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [name] - label name to filter
|
|
|
|
* @returns all note's labels (attributes with type label), including inherited ones
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedLabels(name: string) {
|
2020-01-25 13:27:23 +01:00
|
|
|
return this.getOwnedAttributes(LABEL, name);
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [name] - label name to filter
|
|
|
|
* @returns all note's labels (attributes with type label), including inherited ones
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getLabels(name: string) {
|
2020-03-18 22:35:54 +01:00
|
|
|
return this.getAttributes(LABEL, name);
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
2021-02-13 20:07:08 +01:00
|
|
|
getIcon() {
|
2020-11-25 20:25:55 +01:00
|
|
|
const iconClassLabels = this.getLabels('iconClass');
|
|
|
|
const workspaceIconClass = this.getWorkspaceIconClass();
|
2020-10-14 23:14:04 +02:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
if (iconClassLabels && iconClassLabels.length > 0) {
|
2021-03-25 19:46:10 +01:00
|
|
|
return iconClassLabels[0].value;
|
2020-11-25 20:25:55 +01:00
|
|
|
}
|
|
|
|
else if (workspaceIconClass) {
|
|
|
|
return workspaceIconClass;
|
2020-10-14 23:14:04 +02:00
|
|
|
}
|
|
|
|
else if (this.noteId === 'root') {
|
2024-09-09 20:30:35 +03:00
|
|
|
return "bx bx-home-alt-2";
|
2020-10-14 23:14:04 +02:00
|
|
|
}
|
2022-12-21 16:11:00 +01:00
|
|
|
if (this.noteId === '_share') {
|
2021-12-22 16:02:36 +01:00
|
|
|
return "bx bx-share-alt";
|
|
|
|
}
|
2020-10-14 23:14:04 +02:00
|
|
|
else if (this.type === 'text') {
|
2021-02-13 20:07:08 +01:00
|
|
|
if (this.isFolder()) {
|
2020-10-14 23:14:04 +02:00
|
|
|
return "bx bx-folder";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "bx bx-note";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (this.type === 'code' && this.mime.startsWith('text/x-sql')) {
|
|
|
|
return "bx bx-data";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NOTE_TYPE_ICONS[this.type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 14:19:30 +02:00
|
|
|
getColorClass() {
|
|
|
|
const color = this.getLabelValue("color");
|
|
|
|
return cssClassManager.createClassForColor(color);
|
|
|
|
}
|
|
|
|
|
2021-02-13 20:07:08 +01:00
|
|
|
isFolder() {
|
|
|
|
return this.type === 'search'
|
|
|
|
|| this.getFilteredChildBranches().length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
getFilteredChildBranches() {
|
|
|
|
let childBranches = this.getChildBranches();
|
|
|
|
|
|
|
|
if (!childBranches) {
|
2023-01-09 23:15:02 +01:00
|
|
|
ws.logError(`No children for '${this.noteId}'. This shouldn't happen.`);
|
2024-07-25 20:36:15 +03:00
|
|
|
return [];
|
2021-02-13 20:07:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// we're not checking hideArchivedNotes since that would mean we need to lazy load the child notes
|
|
|
|
// which would seriously slow down everything.
|
|
|
|
// we check this flag only once user chooses to expand the parent. This has the negative consequence that
|
2023-05-05 23:41:11 +02:00
|
|
|
// note may appear as a folder but not contain any children when all of them are archived
|
2021-02-13 20:07:08 +01:00
|
|
|
|
|
|
|
return childBranches;
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [name] - relation name to filter
|
|
|
|
* @returns all note's relations (attributes with type relation), including inherited ones
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedRelations(name: string) {
|
2020-01-25 13:27:23 +01:00
|
|
|
return this.getOwnedAttributes(RELATION, name);
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [name] - relation name to filter
|
|
|
|
* @returns all note's relations (attributes with type relation), including inherited ones
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getRelations(name: string) {
|
2020-03-18 22:35:54 +01:00
|
|
|
return this.getAttributes(RELATION, name);
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param type - attribute type (label, relation, etc.)
|
|
|
|
* @param name - attribute name
|
|
|
|
* @returns true if note has an attribute with given type and name (including inherited)
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
hasAttribute(type: AttributeType, name: string) {
|
2023-04-15 00:06:13 +02:00
|
|
|
const attributes = this.getAttributes();
|
|
|
|
|
|
|
|
return attributes.some(attr => attr.name === name && attr.type === type);
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param type - attribute type (label, relation, etc.)
|
|
|
|
* @param name - attribute name
|
|
|
|
* @returns true if note has an attribute with given type and name (including inherited)
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
hasOwnedAttribute(type: AttributeType, name: string) {
|
2020-01-25 13:27:23 +01:00
|
|
|
return !!this.getOwnedAttribute(type, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param type - attribute type (label, relation, etc.)
|
|
|
|
* @param name - attribute name
|
|
|
|
* @returns attribute of the given type and name. If there are more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedAttribute(type: AttributeType, name: string) {
|
2023-01-27 16:57:23 +01:00
|
|
|
const attributes = this.getOwnedAttributes();
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2023-01-27 16:57:23 +01:00
|
|
|
return attributes.find(attr => attr.name === name && attr.type === type);
|
2020-01-25 13:27:23 +01:00
|
|
|
}
|
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param type - attribute type (label, relation, etc.)
|
|
|
|
* @param name - attribute name
|
|
|
|
* @returns attribute of the given type and name. If there are more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getAttribute(type: AttributeType, name: string) {
|
2023-01-27 16:57:23 +01:00
|
|
|
const attributes = this.getAttributes();
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2023-01-27 16:57:23 +01:00
|
|
|
return attributes.find(attr => attr.name === name && attr.type === type);
|
2020-01-25 13:27:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param type - attribute type (label, relation, etc.)
|
|
|
|
* @param name - attribute name
|
|
|
|
* @returns attribute value of the given type and name or null if no such attribute exists.
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedAttributeValue(type: AttributeType, name: string) {
|
2020-01-25 13:27:23 +01:00
|
|
|
const attr = this.getOwnedAttribute(type, name);
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
return attr ? attr.value : null;
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param type - attribute type (label, relation, etc.)
|
|
|
|
* @param name - attribute name
|
|
|
|
* @returns attribute value of the given type and name or null if no such attribute exists.
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getAttributeValue(type: AttributeType, name: string) {
|
2020-03-18 22:35:54 +01:00
|
|
|
const attr = this.getAttribute(type, name);
|
2018-12-22 22:16:32 +01:00
|
|
|
|
|
|
|
return attr ? attr.value : null;
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns true if label exists (excluding inherited)
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
hasOwnedLabel(name: string) {
|
|
|
|
return this.hasOwnedAttribute(LABEL, name);
|
|
|
|
}
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns true if label exists (including inherited)
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
hasLabel(name: string) { return this.hasAttribute(LABEL, name); }
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2023-04-03 21:08:32 +02:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns true if label exists (including inherited) and does not have "false" value.
|
2023-04-03 21:08:32 +02:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
isLabelTruthy(name: string) {
|
2023-04-03 21:08:32 +02:00
|
|
|
const label = this.getLabel(name);
|
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return label && label.value !== 'false';
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - relation name
|
|
|
|
* @returns true if relation exists (excluding inherited)
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
hasOwnedRelation(name: string) { return this.hasOwnedAttribute(RELATION, name); }
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - relation name
|
|
|
|
* @returns true if relation exists (including inherited)
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
hasRelation(name: string) { return this.hasAttribute(RELATION, name); }
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns label if it exists, null otherwise
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedLabel(name: string) { return this.getOwnedAttribute(LABEL, name); }
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns label if it exists, null otherwise
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getLabel(name: string) { return this.getAttribute(LABEL, name); }
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - relation name
|
|
|
|
* @returns relation if it exists, null otherwise
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedRelation(name: string) { return this.getOwnedAttribute(RELATION, name); }
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - relation name
|
|
|
|
* @returns relation if it exists, null otherwise
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getRelation(name: string) { return this.getAttribute(RELATION, name); }
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns label value if label exists, null otherwise
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedLabelValue(name: string) { return this.getOwnedAttributeValue(LABEL, name); }
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - label name
|
|
|
|
* @returns label value if label exists, null otherwise
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getLabelValue(name: string) { return this.getAttributeValue(LABEL, name); }
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2020-01-25 13:27:23 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - relation name
|
|
|
|
* @returns relation value if relation exists, null otherwise
|
2020-01-25 13:27:23 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getOwnedRelationValue(name: string) { return this.getOwnedAttributeValue(RELATION, name); }
|
2020-01-25 13:27:23 +01:00
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name - relation name
|
|
|
|
* @returns relation value if relation exists, null otherwise
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
getRelationValue(name: string) { return this.getAttributeValue(RELATION, name); }
|
2018-12-22 22:16:32 +01:00
|
|
|
|
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param name
|
|
|
|
* @returns target note of the relation or null (if target is empty or note was not found)
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
async getRelationTarget(name: string) {
|
2019-08-17 11:28:36 +02:00
|
|
|
const targets = await this.getRelationTargets(name);
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2019-08-17 11:28:36 +02:00
|
|
|
return targets.length > 0 ? targets[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-25 20:36:15 +03:00
|
|
|
* @param [name] - relation name to filter
|
2019-08-17 11:28:36 +02:00
|
|
|
*/
|
2024-07-25 20:36:15 +03:00
|
|
|
async getRelationTargets(name: string) {
|
2020-03-18 22:42:29 +01:00
|
|
|
const relations = this.getRelations(name);
|
2019-08-17 11:28:36 +02:00
|
|
|
const targets = [];
|
|
|
|
|
|
|
|
for (const relation of relations) {
|
2021-04-16 22:57:37 +02:00
|
|
|
targets.push(await this.froca.getNote(relation.value));
|
2019-08-17 11:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return targets;
|
2018-12-22 22:16:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-06 20:31:55 +01:00
|
|
|
getNotesToInheritAttributesFrom() {
|
|
|
|
const relations = [
|
|
|
|
...this.getRelations('template'),
|
|
|
|
...this.getRelations('inherit')
|
|
|
|
];
|
2020-06-09 22:59:22 +02:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
return relations.map(rel => this.froca.notes[rel.value]);
|
2020-06-09 22:59:22 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 21:41:02 +01:00
|
|
|
getPromotedDefinitionAttributes() {
|
2023-06-29 00:14:12 +02:00
|
|
|
if (this.isLabelTruthy('hidePromotedAttributes')) {
|
2021-01-23 21:41:02 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2022-06-19 12:18:13 +02:00
|
|
|
const promotedAttrs = this.getAttributes()
|
2021-01-23 21:41:02 +01:00
|
|
|
.filter(attr => attr.isDefinition())
|
|
|
|
.filter(attr => {
|
|
|
|
const def = attr.getDefinition();
|
|
|
|
|
|
|
|
return def && def.isPromoted;
|
|
|
|
});
|
2022-06-19 12:18:13 +02:00
|
|
|
|
2023-05-05 23:41:11 +02:00
|
|
|
// attrs are not resorted if position changes after the initial load
|
2023-03-23 23:47:28 +01:00
|
|
|
promotedAttrs.sort((a, b) => {
|
|
|
|
if (a.noteId === b.noteId) {
|
|
|
|
return a.position < b.position ? -1 : 1;
|
|
|
|
} else {
|
|
|
|
// inherited promoted attributes should stay grouped: https://github.com/zadam/trilium/issues/3761
|
|
|
|
return a.noteId < b.noteId ? -1 : 1;
|
|
|
|
}
|
|
|
|
});
|
2022-06-19 12:18:13 +02:00
|
|
|
|
|
|
|
return promotedAttrs;
|
2021-01-23 21:41:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
hasAncestor(ancestorNoteId: string, followTemplates = false, visitedNoteIds: Set<string> | null = null) {
|
2021-12-20 17:30:47 +01:00
|
|
|
if (this.noteId === ancestorNoteId) {
|
2020-06-09 22:59:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-09 20:15:14 +01:00
|
|
|
if (!visitedNoteIds) {
|
|
|
|
visitedNoteIds = new Set();
|
|
|
|
} else if (visitedNoteIds.has(this.noteId)) {
|
|
|
|
// to avoid infinite cycle when template is descendent of the instance
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
visitedNoteIds.add(this.noteId);
|
|
|
|
|
2023-02-01 22:55:31 +01:00
|
|
|
if (followTemplates) {
|
|
|
|
for (const templateNote of this.getNotesToInheritAttributesFrom()) {
|
|
|
|
if (templateNote.hasAncestor(ancestorNoteId, followTemplates, visitedNoteIds)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-09 22:59:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const parentNote of this.getParentNotes()) {
|
2023-02-01 22:55:31 +01:00
|
|
|
if (parentNote.hasAncestor(ancestorNoteId, followTemplates, visitedNoteIds)) {
|
2020-06-09 22:59:22 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-23 15:32:11 +01:00
|
|
|
isInHiddenSubtree() {
|
|
|
|
return this.noteId === '_hidden' || this.hasAncestor('_hidden');
|
|
|
|
}
|
|
|
|
|
2018-12-22 22:16:32 +01:00
|
|
|
/**
|
2021-10-27 22:28:33 +02:00
|
|
|
* @deprecated NOOP
|
2018-12-22 22:16:32 +01:00
|
|
|
*/
|
2021-10-27 22:28:33 +02:00
|
|
|
invalidateAttributeCache() {}
|
2018-12-22 22:16:32 +01:00
|
|
|
|
2019-08-19 20:12:00 +02:00
|
|
|
/**
|
2019-08-19 20:59:40 +02:00
|
|
|
* Get relations which target this note
|
2019-08-19 20:12:00 +02:00
|
|
|
*/
|
2020-01-25 13:27:23 +01:00
|
|
|
getTargetRelations() {
|
|
|
|
return this.targetRelations
|
2021-04-16 22:57:37 +02:00
|
|
|
.map(attributeId => this.froca.attributes[attributeId]);
|
2019-08-19 20:12:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:45:26 +02:00
|
|
|
/**
|
|
|
|
* Get relations which target this note
|
|
|
|
*/
|
|
|
|
async getTargetRelationSourceNotes() {
|
|
|
|
const targetRelations = this.getTargetRelations();
|
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
return await this.froca.getNotes(targetRelations.map(tr => tr.noteId));
|
2020-09-05 22:45:26 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 22:21:51 +02:00
|
|
|
/**
|
|
|
|
* @deprecated use getBlob() instead
|
|
|
|
*/
|
2023-05-05 16:37:39 +02:00
|
|
|
async getNoteComplement() {
|
2023-05-05 22:21:51 +02:00
|
|
|
return this.getBlob();
|
2023-05-05 16:37:39 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 22:27:15 +02:00
|
|
|
async getBlob() {
|
|
|
|
return await this.froca.getBlob('notes', this.noteId);
|
2020-06-14 14:30:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:37:12 +02:00
|
|
|
toString() {
|
2018-03-25 12:29:00 -04:00
|
|
|
return `Note(noteId=${this.noteId}, title=${this.title})`;
|
|
|
|
}
|
2018-04-08 08:21:49 -04:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
get dto(): Omit<FNote, "froca"> {
|
|
|
|
const dto = Object.assign({}, this) as any;
|
2021-04-16 22:57:37 +02:00
|
|
|
delete dto.froca;
|
2018-04-08 08:21:49 -04:00
|
|
|
|
|
|
|
return dto;
|
|
|
|
}
|
2020-02-25 09:40:49 +01:00
|
|
|
|
2020-03-18 22:42:29 +01:00
|
|
|
getCssClass() {
|
|
|
|
const labels = this.getLabels('cssClass');
|
2020-02-25 09:40:49 +01:00
|
|
|
return labels.map(l => l.value).join(' ');
|
|
|
|
}
|
2020-11-24 23:24:05 +01:00
|
|
|
|
2020-11-25 20:25:55 +01:00
|
|
|
getWorkspaceIconClass() {
|
|
|
|
const labels = this.getLabels('workspaceIconClass');
|
|
|
|
return labels.length > 0 ? labels[0].value : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
getWorkspaceTabBackgroundColor() {
|
|
|
|
const labels = this.getLabels('workspaceTabBackgroundColor');
|
|
|
|
return labels.length > 0 ? labels[0].value : "";
|
2020-11-24 23:24:05 +01:00
|
|
|
}
|
2021-05-07 21:23:10 +02:00
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
/** @returns true if this note is JavaScript (code or file) */
|
2021-05-07 21:23:10 +02:00
|
|
|
isJavaScript() {
|
2022-12-17 21:46:51 +01:00
|
|
|
return (this.type === "code" || this.type === "file" || this.type === 'launcher')
|
2021-05-07 21:23:10 +02:00
|
|
|
&& (this.mime.startsWith("application/javascript")
|
|
|
|
|| this.mime === "application/x-javascript"
|
|
|
|
|| this.mime === "text/javascript");
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
/** @returns true if this note is HTML */
|
2021-05-07 21:23:10 +02:00
|
|
|
isHtml() {
|
|
|
|
return (this.type === "code" || this.type === "file" || this.type === "render") && this.mime === "text/html";
|
|
|
|
}
|
|
|
|
|
2024-07-25 20:36:15 +03:00
|
|
|
/** @returns JS script environment - either "frontend" or "backend" */
|
2021-05-07 21:23:10 +02:00
|
|
|
getScriptEnv() {
|
|
|
|
if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) {
|
|
|
|
return "frontend";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.type === 'render') {
|
|
|
|
return "frontend";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
|
|
|
|
return "backend";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async executeScript() {
|
|
|
|
if (!this.isJavaScript()) {
|
|
|
|
throw new Error(`Note ${this.noteId} is of type ${this.type} and mime ${this.mime} and thus cannot be executed`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const env = this.getScriptEnv();
|
|
|
|
|
|
|
|
if (env === "frontend") {
|
|
|
|
const bundleService = (await import("../services/bundle.js")).default;
|
2022-11-27 23:43:25 +01:00
|
|
|
return await bundleService.getAndExecuteBundle(this.noteId);
|
2024-07-25 20:36:15 +03:00
|
|
|
} else if (env === "backend") {
|
|
|
|
await server.post(`script/run/${this.noteId}`);
|
|
|
|
} else {
|
2021-05-07 21:23:10 +02:00
|
|
|
throw new Error(`Unrecognized env type ${env} for note ${this.noteId}`);
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 15:01:54 +01:00
|
|
|
|
|
|
|
isShared() {
|
|
|
|
for (const parentNoteId of this.parents) {
|
|
|
|
if (parentNoteId === 'root' || parentNoteId === 'none') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentNote = froca.notes[parentNoteId];
|
|
|
|
|
2022-01-12 19:46:40 +01:00
|
|
|
if (!parentNote || parentNote.type === 'search') {
|
2021-12-22 15:01:54 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-21 16:11:00 +01:00
|
|
|
if (parentNote.noteId === '_share' || parentNote.isShared()) {
|
2021-12-22 15:01:54 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-28 23:59:41 +02:00
|
|
|
|
|
|
|
isContentAvailable() {
|
|
|
|
return !this.isProtected || protectedSessionHolder.isProtectedSessionAvailable()
|
|
|
|
}
|
2022-08-05 19:15:28 +02:00
|
|
|
|
|
|
|
isLaunchBarConfig() {
|
2022-12-21 16:11:00 +01:00
|
|
|
return this.type === 'launcher' || ['_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(this.noteId);
|
2022-08-05 19:15:28 +02:00
|
|
|
}
|
2022-12-08 15:29:14 +01:00
|
|
|
|
|
|
|
isOptions() {
|
2023-01-14 23:01:02 +01:00
|
|
|
return this.noteId.startsWith("_options");
|
2022-12-08 15:29:14 +01:00
|
|
|
}
|
2023-09-06 22:54:31 +02:00
|
|
|
|
2023-09-06 23:05:03 +02:00
|
|
|
/**
|
|
|
|
* Provides note's date metadata.
|
|
|
|
*/
|
2023-09-06 22:54:31 +02:00
|
|
|
async getMetadata() {
|
2024-07-25 20:36:15 +03:00
|
|
|
return await server.get<NoteMetaData>(`notes/${this.noteId}/metadata`);
|
2023-09-06 22:54:31 +02:00
|
|
|
}
|
2018-03-25 12:29:00 -04:00
|
|
|
}
|
|
|
|
|
2023-01-03 13:35:10 +01:00
|
|
|
export default FNote;
|