1013 lines
30 KiB
TypeScript
Raw Normal View History

2025-01-09 18:07:02 +02:00
import server from "../services/server.js";
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";
import protectedSessionHolder from "../services/protected_session_holder.js";
2022-09-25 14:19:30 +02:00
import cssClassManager from "../services/css_class_manager.js";
import type { Froca } from "../services/froca-interface.js";
import type FAttachment from "./fattachment.js";
import type { default as FAttribute, AttributeType } from "./fattribute.js";
2025-01-09 18:07:02 +02:00
import utils from "../services/utils.js";
2025-01-09 18:07:02 +02:00
const LABEL = "label";
const RELATION = "relation";
const NOTE_TYPE_ICONS = {
2025-01-09 18:07:02 +02:00
file: "bx bx-file",
image: "bx bx-image",
code: "bx bx-code",
render: "bx bx-extension",
search: "bx bx-file-find",
relationMap: "bx bxs-network-chart",
book: "bx bx-book",
noteMap: "bx bxs-network-chart",
mermaid: "bx bx-selection",
canvas: "bx bx-pen",
webView: "bx bx-globe-alt",
launcher: "bx bx-link",
doc: "bx bxs-file-doc",
contentWidget: "bx bxs-widget",
2025-01-20 18:45:56 +02:00
mindMap: "bx bx-sitemap",
geoMap: "bx bx-map-alt"
};
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.
*/
export type NoteType = "file" | "image" | "search" | "noteMap" | "launcher" | "doc" | "contentWidget" | "text" | "relationMap" | "render" | "canvas" | "mermaid" | "book" | "webView" | "code" | "mindMap" | "geoMap";
2024-07-25 20:36:15 +03:00
2025-01-19 20:53:52 +02:00
export interface NotePathRecord {
2024-07-25 20:36:15 +03:00
isArchived: boolean;
isInHoistedSubTree: boolean;
2025-01-19 20:53:52 +02:00
isSearch?: boolean;
2024-07-25 20:36:15 +03:00
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.
*/
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?: string[];
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) {
this.noteId = row.noteId;
this.title = row.title;
2020-03-08 09:38:49 +01:00
this.isProtected = !!row.isProtected;
this.type = row.type;
this.mime = row.mime;
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) {
2025-01-09 18:07:02 +02:00
if (parentNoteId === "none") {
2020-08-28 14:29:20 +02:00
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;
if (sort) {
this.sortChildren();
}
}
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 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);
2025-01-09 18:07:02 +02: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() {
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);
2021-04-16 22:57:37 +02:00
return this.froca.getBranches(branchIds);
}
2021-12-20 17:30:47 +01:00
/**
* @deprecated use getParentBranches() instead
*/
getBranches() {
return this.getParentBranches();
}
hasChildren() {
2019-10-26 09:51:08 +02:00
return this.children.length > 0;
}
getChildBranches() {
2019-10-27 22:39:38 +01:00
// don't use Object.values() to guarantee order
2025-01-09 18:07:02 +02:00
const branchIds = this.children.map((childNoteId) => this.childToBranch[childNoteId]);
2021-04-16 22:57:37 +02:00
return this.froca.getBranches(branchIds);
}
2018-04-16 23:34:56 -04:00
getParentNoteIds() {
2019-10-26 09:51:08 +02:00
return this.parents;
}
getParentNotes() {
2021-04-16 22:57:37 +02:00
return this.froca.getNotesFromCache(this.parents);
2018-04-16 23:34:56 -04: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() {
this.parents.sort((aNoteId, bNoteId) => {
const aBranchId = this.parentToBranch[aNoteId];
2025-01-09 18:07:02 +02:00
if (aBranchId && aBranchId.startsWith("virt-")) {
return 1;
}
2023-02-28 23:23:17 +01:00
const aNote = this.froca.getNoteFromCache(aNoteId);
2023-01-27 16:57:23 +01:00
if (aNote.isArchived || aNote.isHiddenCompletely()) {
return 1;
}
return aNoteId < bNoteId ? -1 : 1;
});
}
2023-01-27 16:57:23 +01:00
get isArchived() {
2025-01-09 18:07:02 +02:00
return this.hasAttribute("label", "archived");
2023-01-27 16:57:23 +01:00
}
2018-04-16 23:34:56 -04:00
getChildNoteIds() {
2019-10-26 09:51:08 +02:00
return this.children;
}
async getChildNotes() {
2021-04-16 22:57:37 +02:00
return await this.froca.getNotes(this.children);
}
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) {
2025-01-09 18:07:02 +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();
2025-01-09 18:07:02 +02:00
return attachments.find((att) => att.attachmentId === attachmentId);
2023-04-03 23:47:24 +02:00
}
isEligibleForConversionToAttachment() {
2025-01-09 18:07:02 +02:00
if (this.type !== "image" || !this.isContentAvailable() || this.hasChildren() || this.getParentBranches().length !== 1) {
return false;
}
2025-01-09 18:07:02 +02:00
const targetRelations = this.getTargetRelations().filter((relation) => relation.name === "imageLink");
2023-05-04 22:16:18 +02:00
if (targetRelations.length > 1) {
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-04 22:16:18 +02:00
if (referencingNote && referencingNote !== parentNote) {
return false;
2025-01-09 18:07:02 +02:00
} else if (parentNote.type !== "text" || !parentNote.isContentAvailable()) {
return false;
}
return true;
}
/**
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
*/
2024-07-25 20:36:15 +03:00
getOwnedAttributes(type?: AttributeType, name?: string) {
2025-01-09 18:07:02 +02:00
const attrs = this.attributes.map((attributeId) => this.froca.attributes[attributeId]).filter(Boolean); // filter out nulls;
return this.__filterAttrs(attrs, type, name);
}
/**
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
*/
2024-07-25 20:36:15 +03:00
getAttributes(type?: AttributeType, name?: string) {
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[] {
// 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)) {
console.log("Forming a path");
return [];
}
2020-06-27 00:40:35 +02:00
if (!(this.noteId in noteAttributeCache.attributes)) {
const newPath = [...path, this.noteId];
2025-01-09 18:07:02 +02:00
const attrArrs = [this.getOwnedAttributes()];
// inheritable attrs on root are typically not intended to be applied to hidden subtree #3537
2025-01-09 18:07:02 +02:00
if (this.noteId !== "root" && this.noteId !== "_hidden") {
for (const parentNote of this.getParentNotes()) {
2021-04-16 22:57:37 +02:00
// these virtual parent-child relationships are also loaded into froca
2025-01-09 18:07:02 +02:00
if (parentNote.type !== "search") {
attrArrs.push(parentNote.__getInheritableAttributes(newPath));
}
2020-03-29 20:37:40 +02:00
}
}
2025-01-09 18:07:02 +02: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) {
attrArrs.push(
2025-01-09 18:07:02 +02:00
templateNote
.__getCachedAttributes(newPath)
// template attr is used as a marker for templates, but it's not meant to be inherited
2025-01-09 18:07:02 +02:00
.filter((attr) => !(attr.type === "label" && (attr.name === "template" || attr.name === "workspacetemplate")))
);
2020-06-27 00:40:35 +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);
}
}
}
return noteAttributeCache.attributes[this.noteId];
}
2021-07-20 13:29:11 +02:00
isRoot() {
2025-01-09 18:07:02 +02: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[][] {
2025-01-09 18:07:02 +02:00
if (this.noteId === "root") {
return [["root"]];
}
2025-01-09 18:07:02 +02:00
const parentNotes = this.getParentNotes().filter((note) => note.type !== "search");
2025-01-09 18:07:02 +02:00
const notePaths =
parentNotes.length === 1
? parentNotes[0].getAllNotePaths() // optimization for the most common case
: parentNotes.flatMap((parentNote) => parentNote.getAllNotePaths());
2023-04-15 00:06:13 +02:00
for (const notePath of notePaths) {
notePath.push(this.noteId);
}
2023-04-15 00:06:13 +02:00
return notePaths;
}
2025-01-19 20:53:52 +02:00
getSortedNotePathRecords(hoistedNoteId = "root"): NotePathRecord[] {
2025-01-09 18:07:02 +02:00
const isHoistedRoot = hoistedNoteId === "root";
2023-04-15 00:06:13 +02:00
2025-01-19 20:53:52 +02:00
const notePaths: NotePathRecord[] = this.getAllNotePaths().map((path) => ({
notePath: path,
2023-04-15 00:06:13 +02:00
isInHoistedSubTree: isHoistedRoot || path.includes(hoistedNoteId),
2025-01-09 18:07:02 +02:00
isArchived: path.some((noteId) => froca.notes[noteId].isArchived),
2025-01-19 20:53:52 +02:00
isSearch: path.some((noteId) => froca.notes[noteId].type === "search"),
2025-01-09 18:07:02 +02:00
isHidden: path.includes("_hidden")
}));
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;
} else {
return a.notePath.length - b.notePath.length;
}
});
return notePaths;
}
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
*/
2025-01-09 18:07:02 +02:00
getBestNotePath(hoistedNoteId = "root") {
2023-04-15 00:06:13 +02:00
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')
*/
2025-01-09 18:07:02 +02:00
getBestNotePathString(hoistedNoteId = "root") {
2023-04-15 00:06:13 +02:00
const notePath = this.getBestNotePath(hoistedNoteId);
return notePath?.join("/");
}
/**
* @return boolean - true if there's no non-hidden path, note is not cloned to the visible tree
*/
isHiddenCompletely() {
2025-01-09 18:07:02 +02:00
if (this.noteId === "_hidden") {
return true;
2025-01-09 18:07:02 +02:00
} else if (this.noteId === "root") {
2023-01-27 16:57:23 +01:00
return false;
}
for (const parentNote of this.getParentNotes()) {
2025-01-09 18:07:02 +02:00
if (parentNote.noteId === "root") {
2023-01-27 16:57:23 +01:00
return false;
2025-01-09 18:07:02 +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-04-15 00:06:13 +02:00
* @private
*/
2024-07-25 20:36:15 +03:00
__filterAttrs(attributes: FAttribute[], type?: AttributeType, name?: string): FAttribute[] {
this.__validateTypeName(type, name);
if (!type && !name) {
return attributes;
} else if (type && name) {
2025-01-09 18:07:02 +02:00
return attributes.filter((attr) => attr.name === name && attr.type === type);
} else if (type) {
2025-01-09 18:07:02 +02:00
return attributes.filter((attr) => attr.type === type);
} else if (name) {
2025-01-09 18:07:02 +02:00
return attributes.filter((attr) => attr.name === name);
}
2024-07-25 20:36:15 +03:00
return [];
}
2024-07-25 20:36:15 +03:00
__getInheritableAttributes(path: string[]) {
const attrs = this.__getCachedAttributes(path);
2025-01-09 18:07:02 +02:00
return attrs.filter((attr) => attr.isInheritable);
}
2024-07-25 20:36:15 +03:00
__validateTypeName(type?: string, name?: string) {
2025-01-09 18:07:02 +02: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);
2025-01-09 18:07:02 +02:00
if (firstLetter === "#" || firstLetter === "~") {
throw new Error(`Detect '#' or '~' in the attribute's name. In the API, attribute names should be set without these characters.`);
}
}
}
/**
2025-01-18 01:14:47 +02:00
* @param name - label name to filter
2024-07-25 20:36:15 +03:00
* @returns all note's labels (attributes with type label), including inherited ones
*/
2025-01-18 01:14:47 +02:00
getOwnedLabels(name?: string) {
return this.getOwnedAttributes(LABEL, name);
}
/**
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
*/
2024-07-25 20:36:15 +03:00
getLabels(name: string) {
return this.getAttributes(LABEL, name);
}
2021-02-13 20:07:08 +01:00
getIcon() {
2025-01-09 18:07:02 +02:00
const iconClassLabels = this.getLabels("iconClass");
const workspaceIconClass = this.getWorkspaceIconClass();
2024-07-25 20:36:15 +03:00
if (iconClassLabels && iconClassLabels.length > 0) {
return iconClassLabels[0].value;
2025-01-09 18:07:02 +02:00
} else if (workspaceIconClass) {
return workspaceIconClass;
2025-01-09 18:07:02 +02:00
} else if (this.noteId === "root") {
return "bx bx-home-alt-2";
}
2025-01-09 18:07:02 +02:00
if (this.noteId === "_share") {
2021-12-22 16:02:36 +01:00
return "bx bx-share-alt";
2025-01-09 18:07:02 +02:00
} else if (this.type === "text") {
2021-02-13 20:07:08 +01:00
if (this.isFolder()) {
return "bx bx-folder";
2025-01-09 18:07:02 +02:00
} else {
return "bx bx-note";
}
2025-01-09 18:07:02 +02:00
} else if (this.type === "code" && this.mime.startsWith("text/x-sql")) {
return "bx bx-data";
2025-01-09 18:07:02 +02:00
} 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() {
2025-01-09 18:07:02 +02:00
return this.type === "search" || this.getFilteredChildBranches().length > 0;
2021-02-13 20:07:08 +01:00
}
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;
}
/**
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
*/
2024-07-25 20:36:15 +03:00
getOwnedRelations(name: string) {
return this.getOwnedAttributes(RELATION, name);
}
/**
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
*/
getRelations(name?: string) {
return this.getAttributes(RELATION, name);
}
/**
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)
*/
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();
2025-01-09 18:07:02 +02:00
return attributes.some((attr) => attr.name === name && attr.type === type);
}
/**
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)
*/
2024-07-25 20:36:15 +03:00
hasOwnedAttribute(type: AttributeType, name: string) {
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.
*/
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();
2025-01-09 18:07:02 +02:00
return attributes.find((attr) => attr.name === name && attr.type === type);
}
/**
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.
*/
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();
2025-01-09 18:07:02 +02:00
return attributes.find((attr) => attr.name === name && attr.type === type);
}
/**
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.
*/
2024-07-25 20:36:15 +03:00
getOwnedAttributeValue(type: AttributeType, name: string) {
const attr = this.getOwnedAttribute(type, name);
return attr ? attr.value : null;
}
/**
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.
*/
2024-07-25 20:36:15 +03:00
getAttributeValue(type: AttributeType, name: string) {
const attr = this.getAttribute(type, name);
return attr ? attr.value : null;
}
/**
2024-07-25 20:36:15 +03:00
* @param name - label name
* @returns true if label exists (excluding inherited)
*/
2024-07-25 20:36:15 +03:00
hasOwnedLabel(name: string) {
return this.hasOwnedAttribute(LABEL, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - label name
* @returns true if label exists (including inherited)
*/
2025-01-09 18:07:02 +02:00
hasLabel(name: string) {
return this.hasAttribute(LABEL, name);
}
/**
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.
*/
2024-07-25 20:36:15 +03:00
isLabelTruthy(name: string) {
const label = this.getLabel(name);
if (!label) {
return false;
}
2025-01-09 18:07:02 +02:00
return label && label.value !== "false";
}
/**
2024-07-25 20:36:15 +03:00
* @param name - relation name
* @returns true if relation exists (excluding inherited)
*/
2025-01-09 18:07:02 +02:00
hasOwnedRelation(name: string) {
return this.hasOwnedAttribute(RELATION, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - relation name
* @returns true if relation exists (including inherited)
*/
2025-01-09 18:07:02 +02:00
hasRelation(name: string) {
return this.hasAttribute(RELATION, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - label name
* @returns label if it exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getOwnedLabel(name: string) {
return this.getOwnedAttribute(LABEL, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - label name
* @returns label if it exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getLabel(name: string) {
return this.getAttribute(LABEL, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - relation name
* @returns relation if it exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getOwnedRelation(name: string) {
return this.getOwnedAttribute(RELATION, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - relation name
* @returns relation if it exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getRelation(name: string) {
return this.getAttribute(RELATION, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - label name
* @returns label value if label exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getOwnedLabelValue(name: string) {
return this.getOwnedAttributeValue(LABEL, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - label name
* @returns label value if label exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getLabelValue(name: string) {
return this.getAttributeValue(LABEL, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - relation name
* @returns relation value if relation exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getOwnedRelationValue(name: string) {
return this.getOwnedAttributeValue(RELATION, name);
}
/**
2024-07-25 20:36:15 +03:00
* @param name - relation name
* @returns relation value if relation exists, null otherwise
*/
2025-01-09 18:07:02 +02:00
getRelationValue(name: string) {
return this.getAttributeValue(RELATION, name);
}
/**
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)
*/
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);
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) {
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;
}
2023-01-06 20:31:55 +01:00
getNotesToInheritAttributesFrom() {
2025-01-09 18:07:02 +02:00
const relations = [...this.getRelations("template"), ...this.getRelations("inherit")];
2025-01-09 18:07:02 +02:00
return relations.map((rel) => this.froca.notes[rel.value]);
}
getPromotedDefinitionAttributes() {
2025-01-09 18:07:02 +02:00
if (this.isLabelTruthy("hidePromotedAttributes")) {
return [];
}
2022-06-19 12:18:13 +02:00
const promotedAttrs = this.getAttributes()
2025-01-09 18:07:02 +02: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
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;
}
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) {
return true;
}
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;
}
}
}
for (const parentNote of this.getParentNotes()) {
2023-02-01 22:55:31 +01:00
if (parentNote.hasAncestor(ancestorNoteId, followTemplates, visitedNoteIds)) {
return true;
}
}
return false;
}
isInHiddenSubtree() {
2025-01-09 18:07:02 +02:00
return this.noteId === "_hidden" || this.hasAncestor("_hidden");
}
/**
2021-10-27 22:28:33 +02:00
* @deprecated NOOP
*/
2021-10-27 22:28:33 +02:00
invalidateAttributeCache() {}
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
*/
getTargetRelations() {
2025-01-09 18:07:02 +02:00
return this.targetRelations.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();
2025-01-09 18:07:02 +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
}
async getBlob() {
2025-01-09 18:07:02 +02:00
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() {
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
getCssClass() {
2025-01-09 18:07:02 +02:00
const labels = this.getLabels("cssClass");
return labels.map((l) => l.value).join(" ");
2020-02-25 09:40:49 +01:00
}
2020-11-24 23:24:05 +01:00
getWorkspaceIconClass() {
2025-01-09 18:07:02 +02:00
const labels = this.getLabels("workspaceIconClass");
return labels.length > 0 ? labels[0].value : "";
}
getWorkspaceTabBackgroundColor() {
2025-01-09 18:07:02 +02:00
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() {
2025-01-09 18:07:02 +02:00
return (
(this.type === "code" || this.type === "file" || this.type === "launcher") &&
(this.mime.startsWith("application/javascript") || this.mime === "application/x-javascript" || this.mime === "text/javascript")
);
2021-05-07 21:23:10 +02:00
}
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() {
2025-01-09 18:07:02 +02:00
if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith("env=frontend"))) {
2021-05-07 21:23:10 +02:00
return "frontend";
}
2025-01-09 18:07:02 +02:00
if (this.type === "render") {
2021-05-07 21:23:10 +02:00
return "frontend";
}
2025-01-09 18:07:02 +02:00
if (this.isJavaScript() && this.mime.endsWith("env=backend")) {
2021-05-07 21:23:10 +02:00
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) {
2025-01-09 18:07:02 +02:00
if (parentNoteId === "root" || parentNoteId === "none") {
2021-12-22 15:01:54 +01:00
continue;
}
const parentNote = froca.notes[parentNoteId];
2025-01-09 18:07:02 +02:00
if (!parentNote || parentNote.type === "search") {
2021-12-22 15:01:54 +01:00
continue;
}
2025-01-09 18:07:02 +02:00
if (parentNote.noteId === "_share" || parentNote.isShared()) {
2021-12-22 15:01:54 +01:00
return true;
}
}
return false;
}
isContentAvailable() {
2025-01-09 18:07:02 +02:00
return !this.isProtected || protectedSessionHolder.isProtectedSessionAvailable();
}
2022-08-05 19:15:28 +02:00
isLaunchBarConfig() {
2025-01-09 18:07:02 +02:00
return this.type === "launcher" || utils.isLaunchBarConfig(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 23:05:03 +02:00
/**
* Provides note's date metadata.
*/
async getMetadata() {
2024-07-25 20:36:15 +03:00
return await server.get<NoteMetaData>(`notes/${this.noteId}/metadata`);
}
}
export default FNote;