trilium/docs/frontend_api/entities_note_short.js.html

886 lines
26 KiB
HTML
Raw Normal View History

2018-08-30 02:44:35 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: entities/note_short.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: entities/note_short.js</h1>
<section>
<article>
2018-12-23 05:28:49 +08:00
<pre class="prettyprint source linenums"><code>import server from '../services/server.js';
2020-10-01 04:48:30 +08:00
import noteAttributeCache from "../services/note_attribute_cache.js";
import ws from "../services/ws.js";
import options from "../services/options.js";
2021-04-17 05:01:56 +08:00
import froca from "../services/froca.js";
import protectedSessionHolder from "../services/protected_session_holder.js";
import cssClassManager from "../services/css_class_manager.js";
2018-12-23 05:28:49 +08:00
const LABEL = 'label';
const RELATION = 'relation';
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",
"relation-map": "bx bx-map-alt",
2021-09-30 18:26:13 +08:00
"book": "bx bx-book",
"note-map": "bx bx-map-alt",
"mermaid": "bx bx-selection",
"canvas": "bx bx-pen",
2022-11-30 23:57:51 +08:00
"web-view": "bx bx-globe-alt",
"shortcut": "bx bx-link",
"doc": "bx bxs-file-doc"
};
2018-12-23 05:28:49 +08:00
/**
2020-10-01 04:48:30 +08:00
* FIXME: since there's no "full note" anymore we can rename this to Note
*
2021-04-17 04:57:37 +08:00
* This note's representation is used in note tree and is kept in Froca.
2018-08-30 02:44:35 +08:00
*/
class NoteShort {
2019-10-26 16:00:26 +08:00
/**
2021-04-17 04:57:37 +08:00
* @param {Froca} froca
2019-10-26 16:00:26 +08:00
* @param {Object.&lt;string, Object>} row
*/
2021-04-17 04:57:37 +08:00
constructor(froca, row) {
this.froca = froca;
2020-02-02 18:44:08 +08:00
/** @type {string[]} */
this.attributes = [];
/** @type {string[]} */
this.targetRelations = [];
/** @type {string[]} */
this.parents = [];
/** @type {string[]} */
this.children = [];
/** @type {Object.&lt;string, string>} */
this.parentToBranch = {};
/** @type {Object.&lt;string, string>} */
this.childToBranch = {};
this.update(row);
}
update(row) {
/** @type {string} */
2018-08-30 02:44:35 +08:00
this.noteId = row.noteId;
/** @type {string} */
2018-08-30 02:44:35 +08:00
this.title = row.title;
/** @type {boolean} */
2020-03-08 18:41:42 +08:00
this.isProtected = !!row.isProtected;
/**
* one of 'text', 'code', 'file' or 'render'
* @type {string}
*/
2018-08-30 02:44:35 +08:00
this.type = row.type;
/**
* content-type, e.g. "application/json"
* @type {string}
*/
2018-08-30 02:44:35 +08:00
this.mime = row.mime;
2019-10-26 16:00:26 +08:00
}
addParent(parentNoteId, branchId) {
2020-10-01 04:48:30 +08:00
if (parentNoteId === 'none') {
return;
}
2019-10-26 16:00:26 +08:00
if (!this.parents.includes(parentNoteId)) {
this.parents.push(parentNoteId);
}
this.parentToBranch[parentNoteId] = branchId;
}
addChild(childNoteId, branchId, sort = true) {
if (!(childNoteId in this.childToBranch)) {
2019-10-26 16:00:26 +08:00
this.children.push(childNoteId);
}
this.childToBranch[childNoteId] = branchId;
if (sort) {
this.sortChildren();
}
2020-10-01 04:48:30 +08:00
}
sortChildren() {
2019-10-26 16:00:26 +08:00
const branchIdPos = {};
for (const branchId of Object.values(this.childToBranch)) {
2021-04-17 04:57:37 +08:00
branchIdPos[branchId] = this.froca.getBranch(branchId).notePosition;
2019-10-26 16:00:26 +08:00
}
this.children.sort((a, b) => branchIdPos[this.childToBranch[a]] &lt; branchIdPos[this.childToBranch[b]] ? -1 : 1);
2018-08-30 02:44:35 +08:00
}
/** @returns {boolean} */
isJson() {
return this.mime === "application/json";
}
async getContent() {
2021-04-17 04:57:37 +08:00
// we're not caching content since these objects are in froca and as such pretty long lived
const note = await server.get("notes/" + this.noteId);
return note.content;
}
async getJsonContent() {
const content = await this.getContent();
try {
return JSON.parse(content);
}
catch (e) {
console.log(`Cannot parse content of note '${this.noteId}': `, e.message);
return null;
}
}
2022-01-06 03:31:21 +08:00
/**
* @returns {string[]}
*/
getParentBranchIds() {
2020-03-08 18:41:42 +08:00
return Object.values(this.parentToBranch);
}
2022-01-06 03:31:21 +08:00
/**
* @returns {string[]}
* @deprecated use getParentBranchIds() instead
*/
getBranchIds() {
return this.getParentBranchIds();
}
/**
* @returns {Branch[]}
*/
getParentBranches() {
2019-10-26 16:00:26 +08:00
const branchIds = Object.values(this.parentToBranch);
2018-08-30 02:44:35 +08:00
2021-04-17 04:57:37 +08:00
return this.froca.getBranches(branchIds);
2018-08-30 02:44:35 +08:00
}
2022-01-06 03:31:21 +08:00
/**
* @returns {Branch[]}
* @deprecated use getParentBranches() instead
*/
getBranches() {
return this.getParentBranches();
}
2018-08-30 02:44:35 +08:00
/** @returns {boolean} */
hasChildren() {
2019-10-26 16:00:26 +08:00
return this.children.length > 0;
2018-08-30 02:44:35 +08:00
}
/** @returns {Branch[]} */
getChildBranches() {
// don't use Object.values() to guarantee order
const branchIds = this.children.map(childNoteId => this.childToBranch[childNoteId]);
2018-08-30 02:44:35 +08:00
2021-04-17 04:57:37 +08:00
return this.froca.getBranches(branchIds);
2018-08-30 02:44:35 +08:00
}
/** @returns {string[]} */
getParentNoteIds() {
2019-10-26 16:00:26 +08:00
return this.parents;
2018-08-30 02:44:35 +08:00
}
/** @returns {NoteShort[]} */
getParentNotes() {
2021-04-17 04:57:37 +08:00
return this.froca.getNotesFromCache(this.parents);
2018-08-30 02:44:35 +08:00
}
2021-04-08 04:01:52 +08:00
// will sort the parents so that non-search &amp; non-archived are first and archived at the end
// this is done so that non-search &amp; non-archived paths are always explored as first when looking for note path
resortParents() {
this.parents.sort((aNoteId, bNoteId) => {
const aBranchId = this.parentToBranch[aNoteId];
if (aBranchId &amp;&amp; aBranchId.startsWith('virt-')) {
return 1;
}
2021-04-17 04:57:37 +08:00
const aNote = this.froca.getNoteFromCache([aNoteId]);
2021-04-08 04:01:52 +08:00
if (aNote.hasLabel('archived')) {
return 1;
}
return -1;
});
}
2018-08-30 02:44:35 +08:00
/** @returns {string[]} */
getChildNoteIds() {
2019-10-26 16:00:26 +08:00
return this.children;
2018-08-30 02:44:35 +08:00
}
/** @returns {Promise&lt;NoteShort[]>} */
async getChildNotes() {
2021-04-17 04:57:37 +08:00
return await this.froca.getNotes(this.children);
2018-08-30 02:44:35 +08:00
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
* @returns {Attribute[]} all note's attributes, including inherited ones
*/
getOwnedAttributes(type, name) {
const attrs = this.attributes
2021-04-17 04:57:37 +08:00
.map(attributeId => this.froca.attributes[attributeId])
2020-10-01 04:48:30 +08:00
.filter(Boolean); // filter out nulls;
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
return this.__filterAttrs(attrs, type, name);
2020-02-02 18:44:08 +08:00
}
2018-12-23 05:28:49 +08:00
/**
2019-12-04 05:53:17 +08:00
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
* @returns {Attribute[]} all note's attributes, including inherited ones
2018-12-23 05:28:49 +08:00
*/
getAttributes(type, name) {
2020-10-01 04:48:30 +08:00
return this.__filterAttrs(this.__getCachedAttributes([]), type, name);
}
2018-12-23 05:28:49 +08:00
2020-10-01 04:48:30 +08:00
__getCachedAttributes(path) {
// 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-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
if (!(this.noteId in noteAttributeCache.attributes)) {
const newPath = [...path, this.noteId];
const attrArrs = [ this.getOwnedAttributes() ];
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
if (this.noteId !== 'root') {
for (const parentNote of this.getParentNotes()) {
2021-09-30 18:26:13 +08:00
// these virtual parent-child relationships are also loaded into froca
2020-10-01 04:48:30 +08:00
if (parentNote.type !== 'search') {
attrArrs.push(parentNote.__getInheritableAttributes(newPath));
}
}
2020-02-02 18:44:08 +08:00
}
2020-10-01 04:48:30 +08:00
for (const templateAttr of attrArrs.flat().filter(attr => attr.type === 'relation' &amp;&amp; attr.name === 'template')) {
2021-04-17 04:57:37 +08:00
const templateNote = this.froca.notes[templateAttr.value];
2020-10-01 04:48:30 +08:00
if (templateNote &amp;&amp; templateNote.noteId !== this.noteId) {
attrArrs.push(
templateNote.__getCachedAttributes(newPath)
// template attr is used as a marker for templates, but it's not meant to be inherited
.filter(attr => !(attr.type === 'label' &amp;&amp; (attr.name === 'template' || attr.name === 'workspacetemplate')))
);
2020-04-08 01:19:20 +08:00
}
2020-02-02 18:44:08 +08:00
}
2020-10-01 04:48:30 +08:00
noteAttributeCache.attributes[this.noteId] = [];
const addedAttributeIds = new Set();
for (const attr of attrArrs.flat()) {
if (!addedAttributeIds.has(attr.attributeId)) {
addedAttributeIds.add(attr.attributeId);
2020-02-02 18:44:08 +08:00
2020-10-01 04:48:30 +08:00
noteAttributeCache.attributes[this.noteId].push(attr);
}
}
}
return noteAttributeCache.attributes[this.noteId];
2020-02-02 18:44:08 +08:00
}
2021-09-30 18:26:13 +08:00
isRoot() {
return this.noted
}
2021-04-08 04:01:52 +08:00
getAllNotePaths(encounteredNoteIds = null) {
if (this.noteId === 'root') {
return [['root']];
}
if (!encounteredNoteIds) {
encounteredNoteIds = new Set();
}
encounteredNoteIds.add(this.noteId);
const parentNotes = this.getParentNotes();
let paths;
if (parentNotes.length === 1) { // optimization for the most common case
if (encounteredNoteIds.has(parentNotes[0].noteId)) {
return [];
}
else {
paths = parentNotes[0].getAllNotePaths(encounteredNoteIds);
}
}
else {
paths = [];
for (const parentNote of parentNotes) {
if (encounteredNoteIds.has(parentNote.noteId)) {
continue;
}
const newSet = new Set(encounteredNoteIds);
paths.push(...parentNote.getAllNotePaths(newSet));
}
}
for (const path of paths) {
path.push(this.noteId);
}
return paths;
}
getSortedNotePaths(hoistedNotePath = 'root') {
const notePaths = this.getAllNotePaths().map(path => ({
notePath: path,
isInHoistedSubTree: path.includes(hoistedNotePath),
2021-04-17 04:57:37 +08:00
isArchived: path.find(noteId => froca.notes[noteId].hasLabel('archived')),
2021-09-30 18:26:13 +08:00
isSearch: path.find(noteId => froca.notes[noteId].type === 'search'),
isHidden: path.includes("hidden")
2021-04-08 04:01:52 +08:00
}));
notePaths.sort((a, b) => {
if (a.isInHoistedSubTree !== b.isInHoistedSubTree) {
return a.isInHoistedSubTree ? -1 : 1;
} else if (a.isSearch !== b.isSearch) {
return a.isSearch ? 1 : -1;
} else if (a.isArchived !== b.isArchived) {
return a.isArchived ? 1 : -1;
} else {
return a.notePath.length - b.notePath.length;
}
});
return notePaths;
}
2020-02-02 18:44:08 +08:00
__filterAttrs(attributes, type, name) {
2020-10-01 04:48:30 +08:00
if (!type &amp;&amp; !name) {
return attributes;
} else if (type &amp;&amp; name) {
2020-02-02 18:44:08 +08:00
return attributes.filter(attr => attr.type === type &amp;&amp; attr.name === name);
} else if (type) {
return attributes.filter(attr => attr.type === type);
} else if (name) {
return attributes.filter(attr => attr.name === name);
2018-12-23 05:28:49 +08:00
}
}
2020-10-01 04:48:30 +08:00
__getInheritableAttributes(path) {
const attrs = this.__getCachedAttributes(path);
2020-02-02 18:44:08 +08:00
return attrs.filter(attr => attr.isInheritable);
}
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
*/
getOwnedLabels(name) {
return this.getOwnedAttributes(LABEL, name);
}
2018-12-23 05:28:49 +08:00
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
2018-12-23 05:28:49 +08:00
*/
getLabels(name) {
return this.getAttributes(LABEL, name);
2018-12-23 05:28:49 +08:00
}
getIcon() {
const iconClassLabels = this.getLabels('iconClass');
const workspaceIconClass = this.getWorkspaceIconClass();
if (iconClassLabels.length > 0) {
2021-04-08 04:01:52 +08:00
return iconClassLabels[0].value;
}
else if (workspaceIconClass) {
return workspaceIconClass;
}
else if (this.noteId === 'root') {
return "bx bx-chevrons-right";
}
2022-01-06 03:31:21 +08:00
if (this.noteId === 'share') {
return "bx bx-share-alt";
}
else if (this.type === 'text') {
if (this.isFolder()) {
return "bx bx-folder";
}
else {
return "bx bx-note";
}
}
else if (this.type === 'code' &amp;&amp; this.mime.startsWith('text/x-sql')) {
return "bx bx-data";
}
else {
return NOTE_TYPE_ICONS[this.type];
}
}
getColorClass() {
const color = this.getLabelValue("color");
return cssClassManager.createClassForColor(color);
}
isFolder() {
return this.type === 'search'
|| this.getFilteredChildBranches().length > 0;
}
getFilteredChildBranches() {
let childBranches = this.getChildBranches();
if (!childBranches) {
ws.logError(`No children for ${parentNote}. This shouldn't happen.`);
return;
}
if (options.is("hideIncludedImages_main")) {
const imageLinks = this.getRelations('imageLink');
// image is already visible in the parent note so no need to display it separately in the book
childBranches = childBranches.filter(branch => !imageLinks.find(rel => rel.value === branch.noteId));
}
// 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
// note may appear as folder but not contain any children when all of them are archived
return childBranches;
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
*/
getOwnedRelations(name) {
return this.getOwnedAttributes(RELATION, name);
}
2018-12-23 05:28:49 +08:00
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
2018-12-23 05:28:49 +08:00
*/
getRelations(name) {
return this.getAttributes(RELATION, name);
2018-12-23 05:28:49 +08:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {boolean} true if note has an attribute with given type and name (including inherited)
2018-12-23 05:28:49 +08:00
*/
hasAttribute(type, name) {
return !!this.getAttribute(type, name);
2018-12-23 05:28:49 +08:00
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {boolean} true if note has an attribute with given type and name (including inherited)
*/
hasOwnedAttribute(type, name) {
return !!this.getOwnedAttribute(type, name);
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
*/
getOwnedAttribute(type, name) {
const attributes = this.getOwnedAttributes(type, name);
return attributes.length > 0 ? attributes[0] : 0;
}
2018-12-23 05:28:49 +08:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
2018-12-23 05:28:49 +08:00
*/
getAttribute(type, name) {
const attributes = this.getAttributes(type, name);
2018-12-23 05:28:49 +08:00
return attributes.length > 0 ? attributes[0] : null;
2020-02-02 18:44:08 +08:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {string} attribute value of given type and name or null if no such attribute exists.
*/
getOwnedAttributeValue(type, name) {
const attr = this.getOwnedAttribute(type, name);
return attr ? attr.value : null;
2018-12-23 05:28:49 +08:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {string} attribute value of given type and name or null if no such attribute exists.
2018-12-23 05:28:49 +08:00
*/
getAttributeValue(type, name) {
const attr = this.getAttribute(type, name);
2018-12-23 05:28:49 +08:00
return attr ? attr.value : null;
}
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (excluding inherited)
*/
hasOwnedLabel(name) { return this.hasOwnedAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (including inherited)
2018-12-23 05:28:49 +08:00
*/
hasLabel(name) { return this.hasAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - relation name
* @returns {boolean} true if relation exists (excluding inherited)
*/
hasOwnedRelation(name) { return this.hasOwnedAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - relation name
* @returns {boolean} true if relation exists (including inherited)
2018-12-23 05:28:49 +08:00
*/
hasRelation(name) { return this.hasAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - label name
* @returns {Attribute} label if it exists, null otherwise
*/
getOwnedLabel(name) { return this.getOwnedAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - label name
* @returns {Attribute} label if it exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getLabel(name) { return this.getAttribute(LABEL, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - relation name
* @returns {Attribute} relation if it exists, null otherwise
*/
getOwnedRelation(name) { return this.getOwnedAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - relation name
* @returns {Attribute} relation if it exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getRelation(name) { return this.getAttribute(RELATION, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - label name
* @returns {string} label value if label exists, null otherwise
*/
getOwnedLabelValue(name) { return this.getOwnedAttributeValue(LABEL, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - label name
* @returns {string} label value if label exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getLabelValue(name) { return this.getAttributeValue(LABEL, name); }
2018-12-23 05:28:49 +08:00
2020-02-02 18:44:08 +08:00
/**
* @param {string} name - relation name
* @returns {string} relation value if relation exists, null otherwise
*/
getOwnedRelationValue(name) { return this.getOwnedAttributeValue(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name - relation name
* @returns {string} relation value if relation exists, null otherwise
2018-12-23 05:28:49 +08:00
*/
getRelationValue(name) { return this.getAttributeValue(RELATION, name); }
2018-12-23 05:28:49 +08:00
/**
* @param {string} name
* @returns {Promise&lt;NoteShort>|null} target note of the relation or null (if target is empty or note was not found)
2018-12-23 05:28:49 +08:00
*/
async getRelationTarget(name) {
const targets = await this.getRelationTargets(name);
2018-12-23 05:28:49 +08:00
return targets.length > 0 ? targets[0] : null;
}
/**
* @param {string} [name] - relation name to filter
* @returns {Promise&lt;NoteShort[]>}
*/
async getRelationTargets(name) {
const relations = this.getRelations(name);
const targets = [];
for (const relation of relations) {
2021-04-17 04:57:37 +08:00
targets.push(await this.froca.getNote(relation.value));
}
return targets;
2018-12-23 05:28:49 +08:00
}
2020-10-01 04:48:30 +08:00
/**
* @returns {NoteShort[]}
*/
getTemplateNotes() {
const relations = this.getRelations('template');
2021-04-17 04:57:37 +08:00
return relations.map(rel => this.froca.notes[rel.value]);
2020-10-01 04:48:30 +08:00
}
getPromotedDefinitionAttributes() {
if (this.hasLabel('hidePromotedAttributes')) {
return [];
}
const promotedAttrs = this.getAttributes()
.filter(attr => attr.isDefinition())
.filter(attr => {
const def = attr.getDefinition();
return def &amp;&amp; def.isPromoted;
});
// attrs are not resorted if position changes after initial load
promotedAttrs.sort((a, b) => a.position &lt; b.position ? -1 : 1);
return promotedAttrs;
}
2022-01-06 03:31:21 +08:00
hasAncestor(ancestorNoteId, visitedNoteIds = null) {
if (this.noteId === ancestorNoteId) {
2020-10-01 04:48:30 +08:00
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);
2020-10-01 04:48:30 +08:00
for (const templateNote of this.getTemplateNotes()) {
2022-01-06 03:31:21 +08:00
if (templateNote.hasAncestor(ancestorNoteId, visitedNoteIds)) {
2020-10-01 04:48:30 +08:00
return true;
}
}
for (const parentNote of this.getParentNotes()) {
2022-01-06 03:31:21 +08:00
if (parentNote.hasAncestor(ancestorNoteId, visitedNoteIds)) {
2020-10-01 04:48:30 +08:00
return true;
}
}
return false;
}
2018-12-23 05:28:49 +08:00
/**
* @deprecated NOOP
2018-12-23 05:28:49 +08:00
*/
invalidateAttributeCache() {}
2018-12-23 05:28:49 +08:00
/**
* Get relations which target this note
*
2020-02-02 18:44:08 +08:00
* @returns {Attribute[]}
*/
2020-02-02 18:44:08 +08:00
getTargetRelations() {
return this.targetRelations
2021-04-17 04:57:37 +08:00
.map(attributeId => this.froca.attributes[attributeId]);
}
2020-10-01 04:48:30 +08:00
/**
* Get relations which target this note
*
* @returns {NoteShort[]}
*/
async getTargetRelationSourceNotes() {
const targetRelations = this.getTargetRelations();
2021-04-17 04:57:37 +08:00
return await this.froca.getNotes(targetRelations.map(tr => tr.noteId));
2020-10-01 04:48:30 +08:00
}
/**
* Return note complement which is most importantly note's content
*
* @return {Promise&lt;NoteComplement>}
*/
async getNoteComplement() {
2021-04-17 04:57:37 +08:00
return await this.froca.getNoteComplement(this.noteId);
2020-10-01 04:48:30 +08:00
}
toString() {
2018-08-30 02:44:35 +08:00
return `Note(noteId=${this.noteId}, title=${this.title})`;
}
get dto() {
const dto = Object.assign({}, this);
2021-04-17 04:57:37 +08:00
delete dto.froca;
2018-08-30 02:44:35 +08:00
return dto;
}
2020-03-08 18:41:42 +08:00
getCssClass() {
const labels = this.getLabels('cssClass');
2020-03-08 18:41:42 +08:00
return labels.map(l => l.value).join(' ');
}
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 : "";
}
2021-09-30 18:26:13 +08:00
/** @returns {boolean} true if this note is JavaScript (code or attachment) */
isJavaScript() {
return (this.type === "code" || this.type === "file")
&amp;&amp; (this.mime.startsWith("application/javascript")
|| this.mime === "application/x-javascript"
|| this.mime === "text/javascript");
}
/** @returns {boolean} true if this note is HTML */
isHtml() {
return (this.type === "code" || this.type === "file" || this.type === "render") &amp;&amp; this.mime === "text/html";
}
/** @returns {string|null} JS script environment - either "frontend" or "backend" */
getScriptEnv() {
if (this.isHtml() || (this.isJavaScript() &amp;&amp; this.mime.endsWith('env=frontend'))) {
return "frontend";
}
if (this.type === 'render') {
return "frontend";
}
if (this.isJavaScript() &amp;&amp; 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-30 23:57:51 +08:00
return await bundleService.getAndExecuteBundle(this.noteId);
2021-09-30 18:26:13 +08:00
}
else if (env === "backend") {
2022-11-30 23:57:51 +08:00
return await server.post('script/run/' + this.noteId);
2021-09-30 18:26:13 +08:00
}
else {
throw new Error(`Unrecognized env type ${env} for note ${this.noteId}`);
}
}
2022-01-06 03:31:21 +08:00
isShared() {
for (const parentNoteId of this.parents) {
if (parentNoteId === 'root' || parentNoteId === 'none') {
continue;
}
const parentNote = froca.notes[parentNoteId];
2022-01-15 02:53:59 +08:00
if (!parentNote || parentNote.type === 'search') {
2022-01-06 03:31:21 +08:00
continue;
}
if (parentNote.noteId === 'share' || parentNote.isShared()) {
return true;
}
}
return false;
}
isContentAvailable() {
return !this.isProtected || protectedSessionHolder.isProtectedSessionAvailable()
}
2022-11-30 23:57:51 +08:00
isLaunchBarConfig() {
return this.type === 'shortcut' || this.noteId.startsWith("lb_");
}
2018-08-30 02:44:35 +08:00
}
2020-10-01 04:48:30 +08:00
export default NoteShort;
</code></pre>
2018-08-30 02:44:35 +08:00
</article>
</section>
</div>
<nav>
2022-11-08 04:26:13 +08:00
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Attribute.html">Attribute</a></li><li><a href="Branch.html">Branch</a></li><li><a href="FrontendScriptApi.html">FrontendScriptApi</a></li><li><a href="NoteComplement.html">NoteComplement</a></li><li><a href="NoteShort.html">NoteShort</a></li><li><a href="module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#doRenderBody">doRenderBody</a></li></ul>
2018-08-30 02:44:35 +08:00
</nav>
<br class="clear">
<footer>
2022-11-08 04:26:13 +08:00
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a>
2018-08-30 02:44:35 +08:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>