chore(nx): move all monorepo-style in subfolder for processing

This commit is contained in:
Elian Doran
2025-04-22 10:06:06 +03:00
parent 2e200eab39
commit 62dbcc0a2e
1469 changed files with 16 additions and 16 deletions

View File

@@ -1,3 +0,0 @@
node_modules
build
!build/.gitkeep

View File

@@ -1,593 +0,0 @@
/* !!!!!! TRILIUM CUSTOM CHANGES !!!!!! */
.printed-content .ck-widget__selection-handle, .printed-content .ck-widget__type-around { /* gets rid of triangles: https://github.com/zadam/trilium/issues/1129 */
display: none;
}
.page-break {
page-break-after: always;
}
.printed-content .page-break:after,
.printed-content .page-break > * {
display: none !important;
}
.ck-content li p {
margin: 0 !important;
}
.admonition {
--accent-color: var(--card-border-color);
border: 1px solid var(--accent-color);
box-shadow: var(--card-box-shadow);
background: var(--card-background-color);
border-radius: 0.5em;
padding: 1em;
margin: 1.25em 0;
position: relative;
overflow: hidden;
}
.admonition p:last-child {
margin-bottom: 0;
}
.admonition p, h2 {
margin-top: 0;
}
.admonition.note { --accent-color: #69c7ff; }
.admonition.tip { --accent-color: #40c025; }
.admonition.important { --accent-color: #9839f7; }
.admonition.caution { --accent-color: #ff2e2e; }
.admonition.warning { --accent-color: #e2aa03; }
/*
* CKEditor 5 (v41.0.0) content styles.
* Generated on Fri, 26 Jan 2024 10:23:49 GMT.
* For more information, check out https://ckeditor.com/docs/ckeditor5/latest/installation/advanced/content-styles.html
*/
:root {
--ck-color-image-caption-background: hsl(0, 0%, 97%);
--ck-color-image-caption-text: hsl(0, 0%, 20%);
--ck-color-mention-background: hsla(341, 100%, 30%, 0.1);
--ck-color-mention-text: hsl(341, 100%, 30%);
--ck-color-selector-caption-background: hsl(0, 0%, 97%);
--ck-color-selector-caption-text: hsl(0, 0%, 20%);
--ck-highlight-marker-blue: hsl(201, 97%, 72%);
--ck-highlight-marker-green: hsl(120, 93%, 68%);
--ck-highlight-marker-pink: hsl(345, 96%, 73%);
--ck-highlight-marker-yellow: hsl(60, 97%, 73%);
--ck-highlight-pen-green: hsl(112, 100%, 27%);
--ck-highlight-pen-red: hsl(0, 85%, 49%);
--ck-image-style-spacing: 1.5em;
--ck-inline-image-style-spacing: calc(var(--ck-image-style-spacing) / 2);
--ck-todo-list-checkmark-size: 16px;
}
/* @ckeditor/ckeditor5-table/theme/tablecolumnresize.css */
.ck-content .table .ck-table-resized {
table-layout: fixed;
}
/* @ckeditor/ckeditor5-table/theme/tablecolumnresize.css */
.ck-content .table table {
overflow: hidden;
}
/* @ckeditor/ckeditor5-table/theme/tablecolumnresize.css */
.ck-content .table td,
.ck-content .table th {
overflow-wrap: break-word;
position: relative;
}
/* @ckeditor/ckeditor5-table/theme/table.css */
.ck-content .table {
margin: 0.9em auto;
display: table;
}
/* @ckeditor/ckeditor5-table/theme/table.css */
.ck-content .table table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
height: 100%;
border: 1px double hsl(0, 0%, 70%);
}
/* @ckeditor/ckeditor5-table/theme/table.css */
.ck-content .table table td,
.ck-content .table table th {
min-width: 2em;
padding: .4em;
border: 1px solid hsl(0, 0%, 75%);
}
/* @ckeditor/ckeditor5-table/theme/table.css */
.ck-content .table table th {
font-weight: bold;
background: hsla(0, 0%, 0%, 5%);
}
/* @ckeditor/ckeditor5-table/theme/table.css */
.ck-content[dir="rtl"] .table th {
text-align: right;
}
/* @ckeditor/ckeditor5-table/theme/table.css */
.ck-content[dir="ltr"] .table th {
text-align: left;
}
/* @ckeditor/ckeditor5-table/theme/tablecaption.css */
.ck-content .table > figcaption {
display: table-caption;
caption-side: top;
word-break: break-word;
text-align: center;
color: var(--ck-color-selector-caption-text);
background-color: var(--ck-color-selector-caption-background);
padding: .6em;
font-size: .75em;
outline-offset: -1px;
}
/* @ckeditor/ckeditor5-page-break/theme/pagebreak.css */
.ck-content .page-break {
position: relative;
clear: both;
padding: 5px 0;
display: flex;
align-items: center;
justify-content: center;
}
/* @ckeditor/ckeditor5-page-break/theme/pagebreak.css */
.ck-content .page-break::after {
content: '';
position: absolute;
border-bottom: 2px dashed hsl(0, 0%, 77%);
width: 100%;
}
/* @ckeditor/ckeditor5-page-break/theme/pagebreak.css */
.ck-content .page-break__label {
position: relative;
z-index: 1;
padding: .3em .6em;
display: block;
text-transform: uppercase;
border: 1px solid hsl(0, 0%, 77%);
border-radius: 2px;
font-family: Helvetica, Arial, Tahoma, Verdana, Sans-Serif;
font-size: 0.75em;
font-weight: bold;
color: hsl(0, 0%, 20%);
background: hsl(0, 0%, 100%);
box-shadow: 2px 2px 1px hsla(0, 0%, 0%, 0.15);
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* @ckeditor/ckeditor5-media-embed/theme/mediaembed.css */
.ck-content .media {
clear: both;
margin: 0.9em 0;
display: block;
min-width: 15em;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list {
list-style: none;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list li {
position: relative;
margin-bottom: 5px;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list li .todo-list {
margin-top: 5px;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label > input {
-webkit-appearance: none;
display: inline-block;
position: relative;
width: var(--ck-todo-list-checkmark-size);
height: var(--ck-todo-list-checkmark-size);
vertical-align: middle;
border: 0;
left: -25px;
margin-right: -15px;
right: 0;
margin-left: 0;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content[dir=rtl] .todo-list .todo-list__label > input {
left: 0;
margin-right: 0;
right: -25px;
margin-left: -15px;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label > input::before {
display: block;
position: absolute;
box-sizing: border-box;
content: '';
width: 100%;
height: 100%;
border: 1px solid hsl(0, 0%, 20%);
border-radius: 2px;
transition: 250ms ease-in-out box-shadow;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label > input::after {
display: block;
position: absolute;
box-sizing: content-box;
pointer-events: none;
content: '';
left: calc( var(--ck-todo-list-checkmark-size) / 3 );
top: calc( var(--ck-todo-list-checkmark-size) / 5.3 );
width: calc( var(--ck-todo-list-checkmark-size) / 5.3 );
height: calc( var(--ck-todo-list-checkmark-size) / 2.6 );
border-style: solid;
border-color: transparent;
border-width: 0 calc( var(--ck-todo-list-checkmark-size) / 8 ) calc( var(--ck-todo-list-checkmark-size) / 8 ) 0;
transform: rotate(45deg);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label > input[checked]::before {
background: hsl(126, 64%, 41%);
border-color: hsl(126, 64%, 41%);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label > input[checked]::after {
border-color: hsl(0, 0%, 100%);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label .todo-list__label__description {
vertical-align: middle;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-content .todo-list .todo-list__label.todo-list__label_without-description input[type=checkbox] {
position: absolute;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > input,
.ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input {
cursor: pointer;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > input:hover::before, .ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input:hover::before {
box-shadow: 0 0 0 5px hsla(0, 0%, 0%, 0.1);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input {
-webkit-appearance: none;
display: inline-block;
position: relative;
width: var(--ck-todo-list-checkmark-size);
height: var(--ck-todo-list-checkmark-size);
vertical-align: middle;
border: 0;
left: -25px;
margin-right: -15px;
right: 0;
margin-left: 0;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content[dir=rtl] .todo-list .todo-list__label > span[contenteditable=false] > input {
left: 0;
margin-right: 0;
right: -25px;
margin-left: -15px;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input::before {
display: block;
position: absolute;
box-sizing: border-box;
content: '';
width: 100%;
height: 100%;
border: 1px solid hsl(0, 0%, 20%);
border-radius: 2px;
transition: 250ms ease-in-out box-shadow;
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input::after {
display: block;
position: absolute;
box-sizing: content-box;
pointer-events: none;
content: '';
left: calc( var(--ck-todo-list-checkmark-size) / 3 );
top: calc( var(--ck-todo-list-checkmark-size) / 5.3 );
width: calc( var(--ck-todo-list-checkmark-size) / 5.3 );
height: calc( var(--ck-todo-list-checkmark-size) / 2.6 );
border-style: solid;
border-color: transparent;
border-width: 0 calc( var(--ck-todo-list-checkmark-size) / 8 ) calc( var(--ck-todo-list-checkmark-size) / 8 ) 0;
transform: rotate(45deg);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input[checked]::before {
background: hsl(126, 64%, 41%);
border-color: hsl(126, 64%, 41%);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label > span[contenteditable=false] > input[checked]::after {
border-color: hsl(0, 0%, 100%);
}
/* @ckeditor/ckeditor5-list/theme/todolist.css */
.ck-editor__editable.ck-content .todo-list .todo-list__label.todo-list__label_without-description input[type=checkbox] {
position: absolute;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ol {
list-style-type: decimal;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ol ol {
list-style-type: lower-latin;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ol ol ol {
list-style-type: lower-roman;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ol ol ol ol {
list-style-type: upper-latin;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ol ol ol ol ol {
list-style-type: upper-roman;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ul {
list-style-type: disc;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ul ul {
list-style-type: circle;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ul ul ul {
list-style-type: square;
}
/* @ckeditor/ckeditor5-list/theme/list.css */
.ck-content ul ul ul ul {
list-style-type: square;
}
/* @ckeditor/ckeditor5-image/theme/image.css */
.ck-content .image {
display: table;
clear: both;
text-align: center;
margin: 0.9em auto;
min-width: 50px;
}
/* @ckeditor/ckeditor5-image/theme/image.css */
.ck-content .image img {
display: block;
margin: 0 auto;
max-width: 100%;
min-width: 100%;
height: auto;
}
/* @ckeditor/ckeditor5-image/theme/image.css */
.ck-content .image-inline {
/*
* Normally, the .image-inline would have "display: inline-block" and "img { width: 100% }" (to follow the wrapper while resizing).;
* Unfortunately, together with "srcset", it gets automatically stretched up to the width of the editing root.
* This strange behavior does not happen with inline-flex.
*/
display: inline-flex;
max-width: 100%;
align-items: flex-start;
}
/* @ckeditor/ckeditor5-image/theme/image.css */
.ck-content .image-inline picture {
display: flex;
}
/* @ckeditor/ckeditor5-image/theme/image.css */
.ck-content .image-inline picture,
.ck-content .image-inline img {
flex-grow: 1;
flex-shrink: 1;
max-width: 100%;
}
/* @ckeditor/ckeditor5-image/theme/imageresize.css */
.ck-content img.image_resized {
height: auto;
}
/* @ckeditor/ckeditor5-image/theme/imageresize.css */
.ck-content .image.image_resized {
max-width: 100%;
display: block;
box-sizing: border-box;
}
/* @ckeditor/ckeditor5-image/theme/imageresize.css */
.ck-content .image.image_resized img {
width: 100%;
}
/* @ckeditor/ckeditor5-image/theme/imageresize.css */
.ck-content .image.image_resized > figcaption {
display: block;
}
/* @ckeditor/ckeditor5-image/theme/imagecaption.css */
.ck-content .image > figcaption {
display: table-caption;
caption-side: bottom;
word-break: break-word;
color: var(--ck-color-image-caption-text);
background-color: var(--ck-color-image-caption-background);
padding: .6em;
font-size: .75em;
outline-offset: -1px;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-block-align-left,
.ck-content .image-style-block-align-right {
max-width: calc(100% - var(--ck-image-style-spacing));
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-align-left,
.ck-content .image-style-align-right {
clear: none;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-side {
float: right;
margin-left: var(--ck-image-style-spacing);
max-width: 50%;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-align-left {
float: left;
margin-right: var(--ck-image-style-spacing);
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-align-center {
margin-left: auto;
margin-right: auto;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-align-right {
float: right;
margin-left: var(--ck-image-style-spacing);
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-block-align-right {
margin-right: 0;
margin-left: auto;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-style-block-align-left {
margin-left: 0;
margin-right: auto;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content p + .image-style-align-left,
.ck-content p + .image-style-align-right,
.ck-content p + .image-style-side {
margin-top: 0;
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-inline.image-style-align-left,
.ck-content .image-inline.image-style-align-right {
margin-top: var(--ck-inline-image-style-spacing);
margin-bottom: var(--ck-inline-image-style-spacing);
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-inline.image-style-align-left {
margin-right: var(--ck-inline-image-style-spacing);
}
/* @ckeditor/ckeditor5-image/theme/imagestyle.css */
.ck-content .image-inline.image-style-align-right {
margin-left: var(--ck-inline-image-style-spacing);
}
/* @ckeditor/ckeditor5-highlight/theme/highlight.css */
.ck-content .marker-yellow {
background-color: var(--ck-highlight-marker-yellow);
}
/* @ckeditor/ckeditor5-highlight/theme/highlight.css */
.ck-content .marker-green {
background-color: var(--ck-highlight-marker-green);
}
/* @ckeditor/ckeditor5-highlight/theme/highlight.css */
.ck-content .marker-pink {
background-color: var(--ck-highlight-marker-pink);
}
/* @ckeditor/ckeditor5-highlight/theme/highlight.css */
.ck-content .marker-blue {
background-color: var(--ck-highlight-marker-blue);
}
/* @ckeditor/ckeditor5-highlight/theme/highlight.css */
.ck-content .pen-red {
color: var(--ck-highlight-pen-red);
background-color: transparent;
}
/* @ckeditor/ckeditor5-highlight/theme/highlight.css */
.ck-content .pen-green {
color: var(--ck-highlight-pen-green);
background-color: transparent;
}
/* @ckeditor/ckeditor5-block-quote/theme/blockquote.css */
.ck-content blockquote {
overflow: hidden;
padding-right: 1.5em;
padding-left: 1.5em;
margin-left: 0;
margin-right: 0;
font-style: italic;
border-left: solid 5px hsl(0, 0%, 80%);
}
/* @ckeditor/ckeditor5-block-quote/theme/blockquote.css */
.ck-content[dir="rtl"] blockquote {
border-left: 0;
border-right: solid 5px hsl(0, 0%, 80%);
}
/* @ckeditor/ckeditor5-basic-styles/theme/code.css */
.ck-content code {
background-color: hsla(0, 0%, 78%, 0.3);
padding: .15em;
border-radius: 2px;
}
/* @ckeditor/ckeditor5-font/theme/fontsize.css */
.ck-content .text-tiny {
font-size: .7em;
}
/* @ckeditor/ckeditor5-font/theme/fontsize.css */
.ck-content .text-small {
font-size: .85em;
}
/* @ckeditor/ckeditor5-font/theme/fontsize.css */
.ck-content .text-big {
font-size: 1.4em;
}
/* @ckeditor/ckeditor5-font/theme/fontsize.css */
.ck-content .text-huge {
font-size: 1.8em;
}
/* @ckeditor/ckeditor5-mention/theme/mention.css */
.ck-content .mention {
background: var(--ck-color-mention-background);
color: var(--ck-color-mention-text);
}
/* @ckeditor/ckeditor5-horizontal-line/theme/horizontalline.css */
.ck-content hr {
margin: 15px 0;
height: 4px;
background: hsl(0, 0%, 87%);
border: 0;
}
/* @ckeditor/ckeditor5-code-block/theme/codeblock.css */
.ck-content pre {
padding: 1em;
text-align: left;
direction: ltr;
tab-size: 4;
white-space: pre-wrap;
font-style: normal;
min-width: 200px;
border: 0px;
border-radius: 6px;
box-shadow: 1px 1px 6px rgba(0, 0, 0, 0.2);
}
.ck-content pre:not(.hljs) {
color: hsl(0, 0%, 20.8%);
background: hsla(0, 0%, 78%, 0.3);
}
/* @ckeditor/ckeditor5-code-block/theme/codeblock.css */
.ck-content pre code {
background: unset;
padding: 0;
border-radius: 0;
}
@media print {
/* @ckeditor/ckeditor5-page-break/theme/pagebreak.css */
.ck-content .page-break {
padding: 0;
}
/* @ckeditor/ckeditor5-page-break/theme/pagebreak.css */
.ck-content .page-break::after {
display: none;
}
}

View File

@@ -1,49 +0,0 @@
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import { DecoupledEditor as DecoupledEditorBase } from '@ckeditor/ckeditor5-editor-decoupled';
import { Essentials } from '@ckeditor/ckeditor5-essentials';
import { Alignment } from '@ckeditor/ckeditor5-alignment';
import { FontSize, FontFamily, FontColor, FontBackgroundColor } from '@ckeditor/ckeditor5-font';
import { CKFinderUploadAdapter } from '@ckeditor/ckeditor5-adapter-ckfinder';
import { Autoformat } from '@ckeditor/ckeditor5-autoformat';
import { Bold, Italic, Strikethrough, Underline } from '@ckeditor/ckeditor5-basic-styles';
import { BlockQuote } from '@ckeditor/ckeditor5-block-quote';
import { CKBox } from '@ckeditor/ckeditor5-ckbox';
import { CKFinder } from '@ckeditor/ckeditor5-ckfinder';
import { EasyImage } from '@ckeditor/ckeditor5-easy-image';
import { Heading } from '@ckeditor/ckeditor5-heading';
import { Image, ImageCaption, ImageResize, ImageStyle, ImageToolbar, ImageUpload, PictureEditing } from '@ckeditor/ckeditor5-image';
import { Indent, IndentBlock } from '@ckeditor/ckeditor5-indent';
import { Link } from '@ckeditor/ckeditor5-link';
import { List, ListProperties } from '@ckeditor/ckeditor5-list';
import { MediaEmbed } from '@ckeditor/ckeditor5-media-embed';
import { Paragraph } from '@ckeditor/ckeditor5-paragraph';
import { PasteFromOffice } from '@ckeditor/ckeditor5-paste-from-office';
import { Table, TableToolbar } from '@ckeditor/ckeditor5-table';
import { TextTransformation } from '@ckeditor/ckeditor5-typing';
import { CloudServices } from '@ckeditor/ckeditor5-cloud-services';
export default class DecoupledEditor extends DecoupledEditorBase {
static builtinPlugins: (typeof TextTransformation | typeof Essentials | typeof Alignment | typeof FontBackgroundColor | typeof FontColor | typeof FontFamily | typeof FontSize | typeof CKFinderUploadAdapter | typeof Paragraph | typeof Heading | typeof Autoformat | typeof Bold | typeof Italic | typeof Strikethrough | typeof Underline | typeof BlockQuote | typeof Image | typeof ImageCaption | typeof ImageResize | typeof ImageStyle | typeof ImageToolbar | typeof ImageUpload | typeof CloudServices | typeof CKBox | typeof CKFinder | typeof EasyImage | typeof List | typeof ListProperties | typeof Indent | typeof IndentBlock | typeof Link | typeof MediaEmbed | typeof PasteFromOffice | typeof Table | typeof TableToolbar | typeof PictureEditing)[];
static defaultConfig: {
toolbar: {
items: string[];
};
image: {
resizeUnit: "px";
toolbar: string[];
};
table: {
contentToolbar: string[];
};
list: {
properties: {
styles: boolean;
startIndex: boolean;
reversed: boolean;
};
};
language: string;
};
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,51 +0,0 @@
// Source: https://github.com/deathau/cm-editor-syntax-highlight-obsidian/issues/27#issuecomment-1340586596
(() => {
var varsAndArgsRegex = /(%[0-9]|%~\S+|%\S+%)/;
CodeMirror.defineSimpleMode("batch", {
start: [
{ //comment
regex: /(rem|::)(?:\s.*|$)/i,
token: "comment",
sol: true
},
{ //echo
regex: /(@echo|echo)/i,
token: "builtin",
sol: true,
next: "echo"
},
{ //commands
regex: /(?:\s|^)(assoc|aux|break|call|cd|chcp|chdir|choice|cls|cmdextversion|color|com1|com2|com3|com4|com|con|copy|country|ctty|date|defined|del|dir|do|dpath|else|endlocal|erase|errorlevel|exist|exit|for|ftype|goto|if|in|loadfix|loadhigh|lpt|lpt1|lpt2|lpt3|lpt4|md|mkdir|move|not|nul|path|pause|popd|prn|prompt|pushd|rd|rename|ren|rmdir|setlocal|set|shift|start|time|title|type|verify|ver|vol)(?:\s|$)/i,
token: "builtin"
},
{ //variables and arguments
regex: varsAndArgsRegex,
token: "variable-2"
},
{ //label
regex: /\s*:.*/,
token: "string",
sol: true
}
],
echo: [
{ //highlight variables and arguments in echo command
regex: varsAndArgsRegex,
token: "variable-2"
},
{ //go back to start state at end of line
regex: /.$/,
next: "start"
}
]
});
CodeMirror.defineMIME("application/x-bat", "batch");
CodeMirror.modeInfo.push({
ext: [ "bat", "cmd" ],
mime: "application/x-bat",
mode: "batch",
name: "Batch file"
});
})();

View File

@@ -1,74 +0,0 @@
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
async function validatorHtml(text, options) {
const result = /<script[^>]*>([\s\S]+)<\/script>/ig.exec(text);
if (result !== null) {
// preceding code is copied over but any (non-newline) character is replaced with space
// this will preserve line numbers etc.
const prefix = text.substr(0, result.index).replace(/./g, " ");
const js = prefix + result[1];
return await validatorJavaScript(js, options);
}
return [];
}
async function validatorJavaScript(text, options) {
if (glob.isMobile()
|| glob.getActiveContextNote() == null
|| glob.getActiveContextNote().mime === 'application/json') {
// eslint doesn't seem to validate pure JSON well
return [];
}
if (text.length > 20000) {
console.log("Skipping linting because of large size: ", text.length);
return [];
}
const errors = await glob.linter(text, glob.getActiveContextNote().mime);
console.log(errors);
const result = [];
if (errors) {
parseErrors(errors, result);
}
return result;
}
CodeMirror.registerHelper("lint", "javascript", validatorJavaScript);
CodeMirror.registerHelper("lint", "html", validatorHtml);
function parseErrors(errors, output) {
for (const error of errors) {
const startLine = error.line - 1;
const endLine = error.endLine !== undefined ? error.endLine - 1 : startLine;
const startCol = error.column - 1;
const endCol = error.endColumn !== undefined ? error.endColumn - 1 : startCol + 1;
output.push({
message: error.message,
severity: error.severity === 1 ? "warning" : "error",
from: CodeMirror.Pos(startLine, startCol),
to: CodeMirror.Pos(endLine, endCol)
});
}
}
});

View File

@@ -1,204 +0,0 @@
// Source: https://github.com/codemirror/codemirror5/pull/7080/files
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/5/LICENSE
(function (mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function (CodeMirror) {
"use strict";
CodeMirror.defineMode("hcl", function (config) {
var indentUnit = config.indentUnit;
var keywords = {
"resource": true,
"variable": true,
"output": true,
"module": true,
"provider": true,
"data": true,
"locals": true,
"terraform": true,
"if": true,
"else": true,
"for": true,
"foreach": true,
"in": true,
"true": true,
"false": true,
"null": true,
};
var atoms = {
"true": true,
"false": true,
"null": true,
};
var isOperatorChar = /[+\-*&^%:=<>!|\/]/;
var curPunc;
function tokenBase(stream, state) {
var ch = stream.next();
if (ch == '"' || ch == "'" || ch == "`") {
state.tokenize = tokenString(ch);
return state.tokenize(stream, state);
}
if (/[\d\.]/.test(ch)) {
if (ch == ".") {
stream.match(/^[0-9_]+([eE][\-+]?[0-9_]+)?/);
} else {
stream.match(/^[0-9_]*\.?[0-9_]*([eE][\-+]?[0-9_]+)?/);
}
return "number";
}
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
curPunc = ch;
return null;
}
if (ch == "/") {
if (stream.eat("*")) {
state.tokenize = tokenComment;
return tokenComment(stream, state);
}
if (stream.eat("/")) {
stream.skipToEnd();
return "comment";
}
}
if (isOperatorChar.test(ch)) {
stream.eatWhile(isOperatorChar);
return "operator";
}
stream.eatWhile(/[\w\$_\xa1-\uffff]/);
var cur = stream.current();
if (keywords.propertyIsEnumerable(cur)) {
return "keyword";
}
if (atoms.propertyIsEnumerable(cur)) return "atom";
return "variable";
}
function tokenString(quote) {
return function (stream, state) {
var escaped = false,
next,
end = false;
while ((next = stream.next()) != null) {
if (next == quote && !escaped) {
end = true;
break;
}
escaped = !escaped && quote != "`" && next == "\\";
}
if (end || !(escaped || quote == "`"))
state.tokenize = tokenBase;
return "string";
};
}
function tokenComment(stream, state) {
var maybeEnd = false,
ch;
while (ch = stream.next()) {
if (ch == "/" && maybeEnd) {
state.tokenize = tokenBase;
break;
}
maybeEnd = (ch == "*");
}
return "comment";
}
function Context(indented, column, type, align, prev) {
this.indented = indented;
this.column = column;
this.type = type;
this.align = align;
this.prev = prev;
}
function pushContext(state, col, type) {
return state.context = new Context(state.indented, col, type, null, state.context);
}
function popContext(state) {
if (!state.context.prev) return;
var t = state.context.type;
if (t == ")" || t == "]" || t == "}")
state.indented = state.context.indented;
return state.context = state.context.prev;
}
// Interface
return {
startState: function (basecolumn) {
return {
tokenize: null,
context: new Context((basecolumn || 0) - indentUnit, 0, "top", false),
indented: 0,
startOfLine: true
};
},
token: function (stream, state) {
var ctx = state.context;
if (stream.sol()) {
if (ctx.align == null) ctx.align = false;
state.indented = stream.indentation();
state.startOfLine = true;
}
if (stream.eatSpace()) return null;
curPunc = null;
var style = (state.tokenize || tokenBase)(stream, state);
if (style == "comment") return style;
if (ctx.align == null) ctx.align = true;
if (curPunc == "{") pushContext(state, stream.column(), "}");
else if (curPunc == "[") pushContext(state, stream.column(), "]");
else if (curPunc == "(") pushContext(state, stream.column(), ")");
else if (curPunc == "}" && ctx.type == "}") popContext(state);
else if (curPunc == ctx.type) popContext(state);
state.startOfLine = false;
return style;
},
indent: function (state, textAfter) {
if (state.tokenize != tokenBase && state.tokenize != null) return CodeMirror.Pass;
var ctx = state.context, firstChar = textAfter && textAfter.charAt(0);
if (firstChar == "#" || firstChar == ";") return 0;
if (stream.sol()) {
if (ctx.type == "case" && /^(?:case|default)\b/.test(textAfter)) {
state.context.type = "}";
return ctx.indented;
}
var closing = firstChar == ctx.type;
if (ctx.align) return ctx.column + (closing ? 0 : 1);
else return ctx.indented + (closing ? 0 : indentUnit);
}
},
electricChars: "{}):",
closeBrackets: "()[]{}''\"\"``",
fold: "brace",
blockCommentStart: "/*",
blockCommentEnd: "*/",
lineComment: "//"
};
});
CodeMirror.defineMIME("text/x-hcl", "hcl");
CodeMirror.modeInfo.push({
ext: [ "hcl " ],
mime: "text/x-hcl",
mode: "hcl",
name: "Terraform (HCL)"
});
});

View File

@@ -1,83 +0,0 @@
/*
* highlight.js terraform syntax highlighting definition
*
* @see https://github.com/highlightjs/highlight.js
*
* :TODO:
*
* @package: highlightjs-terraform
* @author: Nikos Tsirmirakis <nikos.tsirmirakis@winopsdba.com>
* @since: 2019-03-20
*
* Description: Terraform (HCL) language definition
* Category: scripting
*/
var module = module ? module : {}; // shim for browser use
function hljsDefineTerraform(hljs) {
var NUMBERS = {
className: 'number',
begin: '\\b\\d+(\\.\\d+)?',
relevance: 0
};
var STRINGS = {
className: 'string',
begin: '"',
end: '"',
contains: [{
className: 'variable',
begin: '\\${',
end: '\\}',
relevance: 9,
contains: [{
className: 'string',
begin: '"',
end: '"'
}, {
className: 'meta',
begin: '[A-Za-z_0-9]*' + '\\(',
end: '\\)',
contains: [
NUMBERS, {
className: 'string',
begin: '"',
end: '"',
contains: [{
className: 'variable',
begin: '\\${',
end: '\\}',
contains: [{
className: 'string',
begin: '"',
end: '"',
contains: [{
className: 'variable',
begin: '\\${',
end: '\\}'
}]
}, {
className: 'meta',
begin: '[A-Za-z_0-9]*' + '\\(',
end: '\\)'
}]
}]
},
'self']
}]
}]
};
return {
aliases: ['tf', 'hcl'],
keywords: 'resource variable provider output locals module data terraform|10',
literal: 'false true null',
contains: [
hljs.COMMENT('\\#', '$'),
NUMBERS,
STRINGS
]
}
}
hljs.registerLanguage('terraform', hljsDefineTerraform);

View File

@@ -1,84 +0,0 @@
{
"name": "@triliumnext/client",
"version": "0.0.1",
"description": "JQuery-based client for TriliumNext, used for both web and desktop (via Electron)",
"homepage": "https://github.com/TriliumNext/Notes#readme",
"bugs": {
"url": "https://github.com/TriliumNext/Notes/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/TriliumNext/Notes.git"
},
"license": "AGPL-3.0-only",
"author": {
"name": "TriliumNext Notes Team",
"email": "contact@eliandoran.me",
"url": "https://github.com/TriliumNext/Notes"
},
"copyright": "",
"type": "module",
"main": "index.js",
"scripts": {
"build:webpack": "tsx ../../node_modules/webpack/bin/webpack.js -c webpack.config.ts",
"test": "vitest"
},
"devDependencies": {
"@excalidraw/excalidraw": "0.18.0",
"@fullcalendar/core": "6.1.17",
"@fullcalendar/daygrid": "6.1.17",
"@fullcalendar/interaction": "6.1.17",
"@fullcalendar/list": "6.1.17",
"@fullcalendar/multimonth": "6.1.17",
"@fullcalendar/timegrid": "6.1.17",
"@mermaid-js/layout-elk": "0.1.7",
"@mind-elixir/node-menu": "1.0.5",
"@types/jquery": "3.5.32",
"@types/leaflet-gpx": "1.3.7",
"@types/leaflet": "1.9.17",
"autoprefixer": "10.4.21",
"copy-webpack-plugin": "13.0.0",
"i18next-http-backend": "3.0.2",
"jquery-hotkeys": "0.2.2",
"jquery.fancytree": "2.38.5",
"jquery": "3.7.1",
"jsplumb": "2.15.6",
"knockout": "3.5.1",
"leaflet-gpx": "2.1.2",
"leaflet": "1.9.4",
"mark.js": "8.11.1",
"i18next": "25.0.0",
"mermaid": "11.6.0",
"mind-elixir": "4.5.1",
"mini-css-extract-plugin": "2.9.2",
"panzoom": "9.4.3",
"react-dom": "18.3.1",
"react": "18.3.1",
"split.js": "1.6.5",
"svg-pan-zoom": "3.6.2",
"ts-loader": "9.5.2",
"tsx": "4.19.3",
"vanilla-js-wheel-zoom": "9.0.4",
"webpack-cli": "6.0.1",
"webpack": "5.99.6",
"sass": "1.86.3",
"sass-loader": "16.0.5",
"script-loader": "0.7.2",
"electron": "35.1.5",
"debounce": "2.2.0",
"draggabilly": "3.0.0",
"vitest": "3.1.1",
"@types/bootstrap": "5.2.10",
"bootstrap": "5.3.5",
"force-graph": "1.49.5",
"@popperjs/core": "2.11.8",
"@electron/remote": "2.1.2",
"@types/react": "18.3.20",
"@types/react-dom": "18.3.6",
"css-loader": "7.1.2",
"postcss-loader": "8.1.1",
"eslint-linter-browserify": "9.25.0",
"@eslint/js": "9.25.0",
"happy-dom": "17.4.4"
}
}

View File

@@ -1,58 +0,0 @@
import { beforeAll, vi } from "vitest";
import $ from "jquery";
injectGlobals();
beforeAll(() => {
vi.mock("../services/ws.js", mockWebsocket);
vi.mock("../services/server.js", mockServer);
});
function injectGlobals() {
const uncheckedWindow = window as any;
uncheckedWindow.$ = $;
uncheckedWindow.WebSocket = () => {};
uncheckedWindow.glob = {
isMainWindow: true
};
}
function mockWebsocket() {
return {
default: {
subscribeToMessages(callback: (message: unknown) => void) {
// Do nothing.
}
}
}
}
function mockServer() {
return {
default: {
async get(url: string) {
if (url === "options") {
return {};
}
if (url === "keyboard-actions") {
return [];
}
if (url === "tree") {
return {
branches: [],
notes: [],
attributes: []
}
}
},
async post(url: string, data: object) {
if (url === "tree/load") {
throw new Error(`A module tried to load from the server the following notes: ${((data as any).noteIds || []).join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`)
}
}
}
};
}

View File

@@ -1,3 +0,0 @@
import packageJson from "../package.json" with { type: "json" };
export default `assets/v${packageJson.version}`;

View File

@@ -1,593 +0,0 @@
import froca from "../services/froca.js";
import bundleService from "../services/bundle.js";
import RootCommandExecutor from "./root_command_executor.js";
import Entrypoints, { type SqlExecuteResults } from "./entrypoints.js";
import options from "../services/options.js";
import utils, { hasTouchBar } from "../services/utils.js";
import zoomComponent from "./zoom.js";
import TabManager from "./tab_manager.js";
import Component from "./component.js";
import keyboardActionsService from "../services/keyboard_actions.js";
import linkService, { type ViewScope } from "../services/link.js";
import MobileScreenSwitcherExecutor, { type Screen } from "./mobile_screen_switcher.js";
import MainTreeExecutors from "./main_tree_executors.js";
import toast from "../services/toast.js";
import ShortcutComponent from "./shortcut_component.js";
import { t, initLocale } from "../services/i18n.js";
import type NoteDetailWidget from "../widgets/note_detail.js";
import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
import type { PromptDialogOptions } from "../widgets/dialogs/prompt.js";
import type { ConfirmWithMessageOptions, ConfirmWithTitleOptions } from "../widgets/dialogs/confirm.js";
import type LoadResults from "../services/load_results.js";
import type { Attribute } from "../services/attribute_parser.js";
import type NoteTreeWidget from "../widgets/note_tree.js";
import type { default as NoteContext, GetTextEditorCallback } from "./note_context.js";
import type TypeWidget from "../widgets/type_widgets/type_widget.js";
import type EditableTextTypeWidget from "../widgets/type_widgets/editable_text.js";
import type { NativeImage, TouchBar } from "electron";
import TouchBarComponent from "./touch_bar.js";
interface Layout {
getRootWidget: (appContext: AppContext) => RootWidget;
}
interface RootWidget extends Component {
render: () => JQuery<HTMLElement>;
}
interface BeforeUploadListener extends Component {
beforeUnloadEvent(): boolean;
}
/**
* Base interface for the data/arguments for a given command (see {@link CommandMappings}).
*/
export interface CommandData {
ntxId?: string | null;
}
/**
* Represents a set of commands that are triggered from the context menu, providing information such as the selected note.
*/
export interface ContextMenuCommandData extends CommandData {
node: Fancytree.FancytreeNode;
notePath?: string;
noteId?: string;
selectedOrActiveBranchIds: string[];
selectedOrActiveNoteIds?: string[];
}
export interface NoteCommandData extends CommandData {
notePath?: string | null;
hoistedNoteId?: string | null;
viewScope?: ViewScope;
}
export interface ExecuteCommandData<T> extends CommandData {
resolve: (data: T) => void;
}
export interface NoteSwitchedContext {
noteContext: NoteContext;
notePath: string | null | undefined;
}
/**
* The keys represent the different commands that can be triggered via {@link AppContext#triggerCommand} (first argument), and the values represent the data or arguments definition of the given command. All data for commands must extend {@link CommandData}.
*/
export type CommandMappings = {
"api-log-messages": CommandData;
focusTree: CommandData;
focusOnTitle: CommandData;
focusOnDetail: CommandData;
focusOnSearchDefinition: Required<CommandData>;
searchNotes: CommandData & {
searchString?: string;
ancestorNoteId?: string | null;
};
closeTocCommand: CommandData;
closeHlt: CommandData;
showLaunchBarSubtree: CommandData;
showRevisions: CommandData;
showLlmChat: CommandData;
createAiChat: CommandData;
showOptions: CommandData & {
section: string;
};
showExportDialog: CommandData & {
notePath: string;
defaultType: "single" | "subtree";
};
showDeleteNotesDialog: CommandData & {
branchIdsToDelete: string[];
callback: (value: ResolveOptions) => void;
forceDeleteAllClones: boolean;
};
showConfirmDeleteNoteBoxWithNoteDialog: ConfirmWithTitleOptions;
openedFileUpdated: CommandData & {
entityType: string;
entityId: string;
lastModifiedMs: number;
filePath: string;
};
focusAndSelectTitle: CommandData & {
isNewNote?: boolean;
};
showPromptDialog: PromptDialogOptions;
showInfoDialog: ConfirmWithMessageOptions;
showConfirmDialog: ConfirmWithMessageOptions;
showRecentChanges: CommandData & { ancestorNoteId: string };
showImportDialog: CommandData & { noteId: string };
openNewNoteSplit: NoteCommandData;
openInWindow: NoteCommandData;
openNoteInNewTab: CommandData;
openNoteInNewSplit: CommandData;
openNoteInNewWindow: CommandData;
openAboutDialog: CommandData;
hideFloatingButtons: {};
hideLeftPane: CommandData;
showLeftPane: CommandData;
hoistNote: CommandData & { noteId: string };
leaveProtectedSession: CommandData;
enterProtectedSession: CommandData;
noteContextReorder: CommandData & {
ntxIdsInOrder: string[];
oldMainNtxId?: string | null;
newMainNtxId?: string | null;
};
openInTab: ContextMenuCommandData;
openNoteInSplit: ContextMenuCommandData;
toggleNoteHoisting: ContextMenuCommandData;
insertNoteAfter: ContextMenuCommandData;
insertChildNote: ContextMenuCommandData;
delete: ContextMenuCommandData;
editNoteTitle: {};
protectSubtree: ContextMenuCommandData;
unprotectSubtree: ContextMenuCommandData;
openBulkActionsDialog:
| ContextMenuCommandData
| {
selectedOrActiveNoteIds?: string[];
};
editBranchPrefix: ContextMenuCommandData;
convertNoteToAttachment: ContextMenuCommandData;
duplicateSubtree: ContextMenuCommandData;
expandSubtree: ContextMenuCommandData;
collapseSubtree: ContextMenuCommandData;
sortChildNotes: ContextMenuCommandData;
copyNotePathToClipboard: ContextMenuCommandData;
recentChangesInSubtree: ContextMenuCommandData;
cutNotesToClipboard: ContextMenuCommandData;
copyNotesToClipboard: ContextMenuCommandData;
pasteNotesFromClipboard: ContextMenuCommandData;
pasteNotesAfterFromClipboard: ContextMenuCommandData;
moveNotesTo: ContextMenuCommandData;
cloneNotesTo: ContextMenuCommandData;
deleteNotes: ContextMenuCommandData;
importIntoNote: ContextMenuCommandData;
exportNote: ContextMenuCommandData;
searchInSubtree: ContextMenuCommandData;
moveNoteUp: ContextMenuCommandData;
moveNoteDown: ContextMenuCommandData;
moveNoteUpInHierarchy: ContextMenuCommandData;
moveNoteDownInHierarchy: ContextMenuCommandData;
selectAllNotesInParent: ContextMenuCommandData;
createNoteIntoInbox: CommandData;
addNoteLauncher: ContextMenuCommandData;
addScriptLauncher: ContextMenuCommandData;
addWidgetLauncher: ContextMenuCommandData;
addSpacerLauncher: ContextMenuCommandData;
moveLauncherToVisible: ContextMenuCommandData;
moveLauncherToAvailable: ContextMenuCommandData;
resetLauncher: ContextMenuCommandData;
executeInActiveNoteDetailWidget: CommandData & {
callback: (value: NoteDetailWidget | PromiseLike<NoteDetailWidget>) => void;
};
executeWithTextEditor: CommandData &
ExecuteCommandData<TextEditor> & {
callback?: GetTextEditorCallback;
};
executeWithCodeEditor: CommandData & ExecuteCommandData<CodeMirrorInstance>;
/**
* Called upon when attempting to retrieve the content element of a {@link NoteContext}.
* Generally should not be invoked manually, as it is used by {@link NoteContext.getContentElement}.
*/
executeWithContentElement: CommandData & ExecuteCommandData<JQuery<HTMLElement>>;
executeWithTypeWidget: CommandData & ExecuteCommandData<TypeWidget | null>;
addTextToActiveEditor: CommandData & {
text: string;
};
/** Works only in the electron context menu. */
replaceMisspelling: CommandData;
importMarkdownInline: CommandData;
showPasswordNotSet: CommandData;
showProtectedSessionPasswordDialog: CommandData;
showUploadAttachmentsDialog: CommandData & { noteId: string };
showIncludeNoteDialog: CommandData & { textTypeWidget: EditableTextTypeWidget };
showAddLinkDialog: CommandData & { textTypeWidget: EditableTextTypeWidget, text: string };
closeProtectedSessionPasswordDialog: CommandData;
copyImageReferenceToClipboard: CommandData;
copyImageToClipboard: CommandData;
updateAttributesList: {
attributes: Attribute[];
};
addNewLabel: CommandData;
addNewRelation: CommandData;
addNewLabelDefinition: CommandData;
addNewRelationDefinition: CommandData;
cloneNoteIdsTo: CommandData & {
noteIds: string[];
};
moveBranchIdsTo: CommandData & {
branchIds: string[];
};
/** Sets the active {@link Screen} (e.g. to toggle the tree sidebar). It triggers the {@link EventMappings.activeScreenChanged} event, but only if the provided <em>screen</em> is different than the current one. */
setActiveScreen: CommandData & {
screen: Screen;
};
closeTab: CommandData;
closeToc: CommandData;
closeOtherTabs: CommandData;
closeRightTabs: CommandData;
closeAllTabs: CommandData;
reopenLastTab: CommandData;
moveTabToNewWindow: CommandData;
copyTabToNewWindow: CommandData;
closeActiveTab: CommandData & {
$el: JQuery<HTMLElement>;
};
setZoomFactorAndSave: {
zoomFactor: string;
};
reEvaluateRightPaneVisibility: CommandData;
runActiveNote: CommandData;
scrollContainerToCommand: CommandData & {
position: number;
};
scrollToEnd: CommandData;
closeThisNoteSplit: CommandData;
moveThisNoteSplit: CommandData & { isMovingLeft: boolean };
jumpToNote: CommandData;
// Geomap
deleteFromMap: { noteId: string };
openGeoLocation: { noteId: string; event: JQuery.MouseDownEvent };
toggleZenMode: CommandData;
updateAttributeList: CommandData & { attributes: Attribute[] };
saveAttributes: CommandData;
reloadAttributes: CommandData;
refreshNoteList: CommandData & { noteId: string };
refreshResults: {};
refreshSearchDefinition: {};
geoMapCreateChildNote: CommandData;
buildTouchBar: CommandData & {
TouchBar: typeof TouchBar;
buildIcon(name: string): NativeImage;
};
refreshTouchBar: CommandData;
};
type EventMappings = {
initialRenderComplete: {};
frocaReloaded: {};
protectedSessionStarted: {};
notesReloaded: {
noteIds: string[];
};
refreshIncludedNote: {
noteId: string;
};
apiLogMessages: {
noteId: string;
messages: string[];
};
entitiesReloaded: {
loadResults: LoadResults;
};
addNewLabel: CommandData;
addNewRelation: CommandData;
sqlQueryResults: CommandData & {
results: SqlExecuteResults;
};
readOnlyTemporarilyDisabled: {
noteContext: NoteContext;
};
/** Triggered when the {@link CommandMappings.setActiveScreen} command is invoked. */
activeScreenChanged: {
activeScreen: Screen;
};
activeContextChanged: {
noteContext: NoteContext;
};
beforeNoteSwitch: {
noteContext: NoteContext;
};
beforeNoteContextRemove: {
ntxIds: string[];
};
noteSwitched: NoteSwitchedContext;
noteSwitchedAndActivated: NoteSwitchedContext;
setNoteContext: {
noteContext: NoteContext;
};
reEvaluateHighlightsListWidgetVisibility: {
noteId: string | undefined;
};
reEvaluateTocWidgetVisibility: {
noteId: string | undefined;
};
showHighlightsListWidget: {
noteId: string;
};
showTocWidget: {
noteId: string;
};
showSearchError: {
error: string;
};
searchRefreshed: { ntxId?: string | null };
hoistedNoteChanged: {
noteId: string;
ntxId: string | null;
};
contextsReopened: {
ntxId?: string;
mainNtxId: string | null;
tabPosition: number;
afterNtxId?: string;
};
noteDetailRefreshed: {
ntxId?: string | null;
};
noteContextReorder: {
oldMainNtxId: string;
newMainNtxId: string;
ntxIdsInOrder: string[];
};
newNoteContextCreated: {
noteContext: NoteContext;
};
noteContextRemoved: {
ntxIds: string[];
};
exportSvg: { ntxId: string | null | undefined; };
exportPng: { ntxId: string | null | undefined; };
geoMapCreateChildNote: {
ntxId: string | null | undefined; // TODO: deduplicate ntxId
};
tabReorder: {
ntxIdsInOrder: string[];
};
refreshNoteList: {
noteId: string;
};
noteTypeMimeChanged: { noteId: string };
zenModeChanged: { isEnabled: boolean };
relationMapCreateChildNote: { ntxId: string | null | undefined };
relationMapResetPanZoom: { ntxId: string | null | undefined };
relationMapResetZoomIn: { ntxId: string | null | undefined };
relationMapResetZoomOut: { ntxId: string | null | undefined };
activeNoteChanged: {};
showAddLinkDialog: {
textTypeWidget: EditableTextTypeWidget;
text: string;
};
showIncludeDialog: {
textTypeWidget: EditableTextTypeWidget;
};
openBulkActionsDialog: {
selectedOrActiveNoteIds: string[];
};
cloneNoteIdsTo: {
noteIds: string[];
};
refreshData: { ntxId: string | null | undefined };
};
export type EventListener<T extends EventNames> = {
[key in T as `${key}Event`]: (data: EventData<T>) => void;
};
export type CommandListener<T extends CommandNames> = {
[key in T as `${key}Command`]: (data: CommandListenerData<T>) => void;
};
export type CommandListenerData<T extends CommandNames> = CommandMappings[T];
type CommandAndEventMappings = CommandMappings & EventMappings;
type EventOnlyNames = keyof EventMappings;
export type EventNames = CommandNames | EventOnlyNames;
export type EventData<T extends EventNames> = CommandAndEventMappings[T];
/**
* This type is a discriminated union which contains all the possible commands that can be triggered via {@link AppContext.triggerCommand}.
*/
export type CommandNames = keyof CommandMappings;
type FilterByValueType<T, ValueType> = { [K in keyof T]: T[K] extends ValueType ? K : never }[keyof T];
/**
* Generic which filters {@link CommandNames} to provide only those commands that take in as data the desired implementation of {@link CommandData}. Mostly useful for contextual menu, to enforce consistency in the commands.
*/
export type FilteredCommandNames<T extends CommandData> = keyof Pick<CommandMappings, FilterByValueType<CommandMappings, T>>;
export class AppContext extends Component {
isMainWindow: boolean;
components: Component[];
beforeUnloadListeners: WeakRef<BeforeUploadListener>[];
tabManager!: TabManager;
layout?: Layout;
noteTreeWidget?: NoteTreeWidget;
lastSearchString?: string;
constructor(isMainWindow: boolean) {
super();
this.isMainWindow = isMainWindow;
// non-widget/layout components needed for the application
this.components = [];
this.beforeUnloadListeners = [];
}
/**
* Must be called as soon as possible, before the creation of any components since this method is in charge of initializing the locale. Any attempts to read translation before this method is called will result in `undefined`.
*/
async earlyInit() {
await options.initializedPromise;
await initLocale();
}
setLayout(layout: Layout) {
this.layout = layout;
}
async start() {
this.initComponents();
this.renderWidgets();
await froca.initializedPromise;
this.tabManager.loadTabs();
setTimeout(() => bundleService.executeStartupBundles(), 2000);
}
initComponents() {
this.tabManager = new TabManager();
this.components = [this.tabManager, new RootCommandExecutor(), new Entrypoints(), new MainTreeExecutors(), new ShortcutComponent()];
if (utils.isMobile()) {
this.components.push(new MobileScreenSwitcherExecutor());
}
for (const component of this.components) {
this.child(component);
}
if (utils.isElectron()) {
this.child(zoomComponent);
}
if (hasTouchBar) {
this.child(new TouchBarComponent());
}
}
renderWidgets() {
if (!this.layout) {
throw new Error("Missing layout.");
}
const rootWidget = this.layout.getRootWidget(this);
const $renderedWidget = rootWidget.render();
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
$("body").append($renderedWidget);
$renderedWidget.on("click", "[data-trigger-command]", function () {
if ($(this).hasClass("disabled")) {
return;
}
const commandName = $(this).attr("data-trigger-command");
const $component = $(this).closest(".component");
const component = $component.prop("component");
component.triggerCommand(commandName, { $el: $(this) });
});
this.child(rootWidget);
this.triggerEvent("initialRenderComplete", {});
}
triggerEvent<K extends EventNames>(name: K, data: EventData<K>) {
return this.handleEvent(name, data);
}
triggerCommand<K extends CommandNames>(name: K, _data?: CommandMappings[K]) {
const data = _data || {};
for (const executor of this.components) {
const fun = (executor as any)[`${name}Command`];
if (fun) {
return executor.callMethod(fun, data);
}
}
// this might hint at error, but sometimes this is used by components which are at different places
// in the component tree to communicate with each other
console.debug(`Unhandled command ${name}, converting to event.`);
return this.triggerEvent(name, data as CommandAndEventMappings[K]);
}
getComponentByEl(el: HTMLElement) {
return $(el).closest(".component").prop("component");
}
addBeforeUnloadListener(obj: BeforeUploadListener) {
if (typeof WeakRef !== "function") {
// older browsers don't support WeakRef
return;
}
this.beforeUnloadListeners.push(new WeakRef<BeforeUploadListener>(obj));
}
}
const appContext = new AppContext(window.glob.isMainWindow);
// we should save all outstanding changes before the page/app is closed
$(window).on("beforeunload", () => {
let allSaved = true;
appContext.beforeUnloadListeners = appContext.beforeUnloadListeners.filter((wr) => !!wr.deref());
for (const weakRef of appContext.beforeUnloadListeners) {
const component = weakRef.deref();
if (!component) {
continue;
}
if (!component.beforeUnloadEvent()) {
console.log(`Component ${component.componentId} is not finished saving its state.`);
toast.showMessage(t("app_context.please_wait_for_save"), 10000);
allSaved = false;
}
}
if (!allSaved) {
return "some string";
}
});
$(window).on("hashchange", function () {
const { notePath, ntxId, viewScope, searchString } = linkService.parseNavigationStateFromUrl(window.location.href);
if (notePath || ntxId) {
appContext.tabManager.switchToNoteContext(ntxId, notePath, viewScope);
} else if (searchString) {
appContext.triggerCommand("searchNotes", { searchString });
}
});
export default appContext;

View File

@@ -1,129 +0,0 @@
import utils from "../services/utils.js";
import type { CommandMappings, CommandNames, EventData, EventNames } from "./app_context.js";
/**
* Abstract class for all components in the Trilium's frontend.
*
* Contains also event implementation with following properties:
* - event / command distribution is synchronous which among others mean that events are well-ordered - event
* which was sent out first will also be processed first by the component
* - execution of the event / command is asynchronous - each component executes the event on its own without regard for
* other components.
* - although the execution is async, we are collecting all the promises, and therefore it is possible to wait until the
* event / command is executed in all components - by simply awaiting the `triggerEvent()`.
*/
export class TypedComponent<ChildT extends TypedComponent<ChildT>> {
$widget!: JQuery<HTMLElement>;
componentId: string;
children: ChildT[];
initialized: Promise<void> | null;
parent?: TypedComponent<any>;
_position!: number;
constructor() {
this.componentId = `${this.sanitizedClassName}-${utils.randomString(8)}`;
this.children = [];
this.initialized = null;
}
get sanitizedClassName() {
// webpack mangles names and sometimes uses unsafe characters
return this.constructor.name.replace(/[^A-Z0-9]/gi, "_");
}
get position() {
return this._position;
}
set position(newPosition: number) {
this._position = newPosition;
}
setParent(parent: TypedComponent<any>) {
this.parent = parent;
return this;
}
child(...components: ChildT[]) {
for (const component of components) {
component.setParent(this);
this.children.push(component);
}
return this;
}
handleEvent<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null | undefined {
try {
const callMethodPromise = this.initialized ? this.initialized.then(() => this.callMethod((this as any)[`${name}Event`], data)) : this.callMethod((this as any)[`${name}Event`], data);
const childrenPromise = this.handleEventInChildren(name, data);
// don't create promises if not needed (optimization)
return callMethodPromise && childrenPromise ? Promise.all([callMethodPromise, childrenPromise]) : callMethodPromise || childrenPromise;
} catch (e: any) {
console.error(`Handling of event '${name}' failed in ${this.constructor.name} with error ${e.message} ${e.stack}`);
return null;
}
}
triggerEvent<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown> | undefined | null {
return this.parent?.triggerEvent(name, data);
}
handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>): Promise<unknown[] | unknown> | null {
const promises: Promise<unknown>[] = [];
for (const child of this.children) {
const ret = child.handleEvent(name, data) as Promise<void>;
if (ret) {
promises.push(ret);
}
}
// don't create promises if not needed (optimization)
return promises.length > 0 ? Promise.all(promises) : null;
}
triggerCommand<K extends CommandNames>(name: K, data?: CommandMappings[K]): Promise<unknown> | undefined | null {
const fun = (this as any)[`${name}Command`];
if (fun) {
return this.callMethod(fun, data);
} else {
if (!this.parent) {
throw new Error(`Component "${this.componentId}" does not have a parent attached to propagate a command.`);
}
return this.parent.triggerCommand(name, data);
}
}
callMethod(fun: (arg: unknown) => Promise<unknown>, data: unknown) {
if (typeof fun !== "function") {
return;
}
const startTime = Date.now();
const promise = fun.call(this, data);
const took = Date.now() - startTime;
if (glob.isDev && took > 20) {
// measuring only sync handlers
console.log(`Call to ${fun.name} in ${this.componentId} took ${took}ms`);
}
if (glob.isDev && promise) {
return utils.timeLimit(promise, 20000, `Time limit failed on ${this.constructor.name} with ${fun.name}`);
}
return promise;
}
}
export default class Component extends TypedComponent<Component> {}

View File

@@ -1,233 +0,0 @@
import utils from "../services/utils.js";
import dateNoteService from "../services/date_notes.js";
import protectedSessionHolder from "../services/protected_session_holder.js";
import server from "../services/server.js";
import appContext, { type NoteCommandData } from "./app_context.js";
import Component from "./component.js";
import toastService from "../services/toast.js";
import ws from "../services/ws.js";
import bundleService from "../services/bundle.js";
import froca from "../services/froca.js";
import linkService from "../services/link.js";
import { t } from "../services/i18n.js";
import type FNote from "../entities/fnote.js";
// TODO: Move somewhere else nicer.
export type SqlExecuteResults = string[][][];
// TODO: Deduplicate with server.
interface SqlExecuteResponse {
success: boolean;
error?: string;
results: SqlExecuteResults;
}
// TODO: Deduplicate with server.
interface CreateChildrenResponse {
note: FNote;
}
export default class Entrypoints extends Component {
constructor() {
super();
if (jQuery.hotkeys) {
// hot keys are active also inside inputs and content editables
jQuery.hotkeys.options.filterInputAcceptingElements = false;
jQuery.hotkeys.options.filterContentEditable = false;
jQuery.hotkeys.options.filterTextInputs = false;
}
}
openDevToolsCommand() {
if (utils.isElectron()) {
utils.dynamicRequire("@electron/remote").getCurrentWindow().toggleDevTools();
}
}
async createNoteIntoInboxCommand() {
const inboxNote = await dateNoteService.getInboxNote();
if (!inboxNote) {
console.warn("Missing inbox note.");
return;
}
const { note } = await server.post<CreateChildrenResponse>(`notes/${inboxNote.noteId}/children?target=into`, {
content: "",
type: "text",
isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable()
});
await ws.waitForMaxKnownEntityChangeId();
await appContext.tabManager.openTabWithNoteWithHoisting(note.noteId, { activate: true });
appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true });
}
async toggleNoteHoistingCommand({ noteId = appContext.tabManager.getActiveContextNoteId() }) {
const activeNoteContext = appContext.tabManager.getActiveContext();
if (!activeNoteContext || !noteId) {
return;
}
const noteToHoist = await froca.getNote(noteId);
if (noteToHoist?.noteId === activeNoteContext.hoistedNoteId) {
await activeNoteContext.unhoist();
} else if (noteToHoist?.type !== "search") {
await activeNoteContext.setHoistedNoteId(noteId);
}
}
async hoistNoteCommand({ noteId }: { noteId: string }) {
const noteContext = appContext.tabManager.getActiveContext();
if (!noteContext) {
logError("hoistNoteCommand: noteContext is null");
return;
}
if (noteContext.hoistedNoteId !== noteId) {
await noteContext.setHoistedNoteId(noteId);
}
}
async unhoistCommand() {
const activeNoteContext = appContext.tabManager.getActiveContext();
if (activeNoteContext) {
activeNoteContext.unhoist();
}
}
copyWithoutFormattingCommand() {
utils.copySelectionToClipboard();
}
toggleFullscreenCommand() {
if (utils.isElectron()) {
const win = utils.dynamicRequire("@electron/remote").getCurrentWindow();
if (win.isFullScreenable()) {
win.setFullScreen(!win.isFullScreen());
}
} // outside of electron this is handled by the browser
}
reloadFrontendAppCommand() {
utils.reloadFrontendApp();
}
async logoutCommand() {
await server.post("../logout");
window.location.replace(`/login`);
}
backInNoteHistoryCommand() {
if (utils.isElectron()) {
// standard JS version does not work completely correctly in electron
const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex());
webContents.goToIndex(activeIndex - 1);
} else {
window.history.back();
}
}
forwardInNoteHistoryCommand() {
if (utils.isElectron()) {
// standard JS version does not work completely correctly in electron
const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents();
const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex());
webContents.goToIndex(activeIndex + 1);
} else {
window.history.forward();
}
}
async switchToDesktopVersionCommand() {
utils.setCookie("trilium-device", "desktop");
utils.reloadFrontendApp("Switching to desktop version");
}
async switchToMobileVersionCommand() {
utils.setCookie("trilium-device", "mobile");
utils.reloadFrontendApp("Switching to mobile version");
}
async openInWindowCommand({ notePath, hoistedNoteId, viewScope }: NoteCommandData) {
const extraWindowHash = linkService.calculateHash({ notePath, hoistedNoteId, viewScope });
if (utils.isElectron()) {
const { ipcRenderer } = utils.dynamicRequire("electron");
ipcRenderer.send("create-extra-window", { extraWindowHash });
} else {
const url = `${window.location.protocol}//${window.location.host}${window.location.pathname}?extraWindow=1${extraWindowHash}`;
window.open(url, "", "width=1000,height=800");
}
}
async openNewWindowCommand() {
this.openInWindowCommand({ notePath: "", hoistedNoteId: "root" });
}
async runActiveNoteCommand() {
const noteContext = appContext.tabManager.getActiveContext();
if (!noteContext) {
return;
}
const { ntxId, note } = noteContext;
// ctrl+enter is also used elsewhere, so make sure we're running only when appropriate
if (!note || note.type !== "code") {
return;
}
// TODO: use note.executeScript()
if (note.mime.endsWith("env=frontend")) {
await bundleService.getAndExecuteBundle(note.noteId);
} else if (note.mime.endsWith("env=backend")) {
await server.post(`script/run/${note.noteId}`);
} else if (note.mime === "text/x-sqlite;schema=trilium") {
const resp = await server.post<SqlExecuteResponse>(`sql/execute/${note.noteId}`);
if (!resp.success) {
toastService.showError(t("entrypoints.sql-error", { message: resp.error }));
}
await appContext.triggerEvent("sqlQueryResults", { ntxId: ntxId, results: resp.results });
}
toastService.showMessage(t("entrypoints.note-executed"));
}
hideAllPopups() {
if (utils.isDesktop()) {
$(".aa-input").autocomplete("close");
}
}
noteSwitchedEvent() {
this.hideAllPopups();
}
activeContextChangedEvent() {
this.hideAllPopups();
}
async forceSaveRevisionCommand() {
const noteId = appContext.tabManager.getActiveContextNoteId();
await server.post(`notes/${noteId}/revision`);
toastService.showMessage(t("entrypoints.note-revision-created"));
}
}

View File

@@ -1,8 +0,0 @@
import type { MenuCommandItem } from "../menus/context_menu.js";
import type { CommandNames } from "./app_context.js";
type ListenerReturnType = void | Promise<void>;
export interface SelectMenuItemEventListener<T extends CommandNames> {
selectMenuItemHandler(item: MenuCommandItem<T>): ListenerReturnType;
}

View File

@@ -1,82 +0,0 @@
import appContext, { type EventData } from "./app_context.js";
import noteCreateService from "../services/note_create.js";
import treeService from "../services/tree.js";
import hoistedNoteService from "../services/hoisted_note.js";
import Component from "./component.js";
/**
* This class contains command executors which logically belong to the NoteTree widget, but for better user experience,
* the keyboard shortcuts must be active on the whole screen and not just on the widget itself, so the executors
* must be at the root of the component tree.
*/
export default class MainTreeExecutors extends Component {
/**
* On mobile it will be `undefined`.
*/
get tree() {
return appContext.noteTreeWidget;
}
async cloneNotesToCommand({ selectedOrActiveNoteIds }: EventData<"cloneNotesTo">) {
if (!selectedOrActiveNoteIds && this.tree) {
selectedOrActiveNoteIds = this.tree.getSelectedOrActiveNodes().map((node) => node.data.noteId);
}
if (!selectedOrActiveNoteIds) {
return;
}
this.triggerCommand("cloneNoteIdsTo", { noteIds: selectedOrActiveNoteIds });
}
async moveNotesToCommand({ selectedOrActiveBranchIds }: EventData<"moveNotesTo">) {
if (!selectedOrActiveBranchIds && this.tree) {
selectedOrActiveBranchIds = this.tree.getSelectedOrActiveNodes().map((node) => node.data.branchId);
}
if (!selectedOrActiveBranchIds) {
return;
}
this.triggerCommand("moveBranchIdsTo", { branchIds: selectedOrActiveBranchIds });
}
async createNoteIntoCommand() {
const activeNoteContext = appContext.tabManager.getActiveContext();
if (!activeNoteContext || !activeNoteContext.notePath || !activeNoteContext.note) {
return;
}
await noteCreateService.createNote(activeNoteContext.notePath, {
isProtected: activeNoteContext.note.isProtected,
saveSelection: false
});
}
async createNoteAfterCommand() {
if (!this.tree) {
return;
}
const node = this.tree.getActiveNode();
if (!node) {
return;
}
const parentNotePath = treeService.getNotePath(node.getParent());
const isProtected = treeService.getParentProtectedStatus(node);
if (node.data.noteId === "root" || node.data.noteId === hoistedNoteService.getHoistedNoteId()) {
return;
}
await noteCreateService.createNote(parentNotePath, {
target: "after",
targetBranchId: node.data.branchId,
isProtected: isProtected,
saveSelection: false
});
}
}

View File

@@ -1,15 +0,0 @@
import Component from "./component.js";
import type { CommandListener, CommandListenerData } from "./app_context.js";
export type Screen = "detail" | "tree";
export default class MobileScreenSwitcherExecutor extends Component implements CommandListener<"setActiveScreen"> {
private activeScreen?: Screen;
setActiveScreenCommand({ screen }: CommandListenerData<"setActiveScreen">) {
if (screen !== this.activeScreen) {
this.activeScreen = screen;
this.triggerEvent("activeScreenChanged", { activeScreen: screen });
}
}
}

View File

@@ -1,388 +0,0 @@
import protectedSessionHolder from "../services/protected_session_holder.js";
import server from "../services/server.js";
import utils from "../services/utils.js";
import appContext, { type EventData, type EventListener } from "./app_context.js";
import treeService from "../services/tree.js";
import Component from "./component.js";
import froca from "../services/froca.js";
import hoistedNoteService from "../services/hoisted_note.js";
import options from "../services/options.js";
import type { ViewScope } from "../services/link.js";
import type FNote from "../entities/fnote.js";
import type TypeWidget from "../widgets/type_widgets/type_widget.js";
export interface SetNoteOpts {
triggerSwitchEvent?: unknown;
viewScope?: ViewScope;
}
export type GetTextEditorCallback = (editor: TextEditor) => void;
class NoteContext extends Component implements EventListener<"entitiesReloaded"> {
ntxId: string | null;
hoistedNoteId: string;
mainNtxId: string | null;
notePath?: string | null;
noteId?: string | null;
parentNoteId?: string | null;
viewScope?: ViewScope;
constructor(ntxId: string | null = null, hoistedNoteId: string = "root", mainNtxId: string | null = null) {
super();
this.ntxId = ntxId || NoteContext.generateNtxId();
this.hoistedNoteId = hoistedNoteId;
this.mainNtxId = mainNtxId;
this.resetViewScope();
}
static generateNtxId() {
return utils.randomString(6);
}
setEmpty() {
this.notePath = null;
this.noteId = null;
this.parentNoteId = null;
// hoisted note is kept intentionally
this.triggerEvent("noteSwitched", {
noteContext: this,
notePath: this.notePath
});
this.resetViewScope();
}
isEmpty() {
return !this.noteId;
}
async setNote(inputNotePath: string | undefined, opts: SetNoteOpts = {}) {
opts.triggerSwitchEvent = opts.triggerSwitchEvent !== undefined ? opts.triggerSwitchEvent : true;
opts.viewScope = opts.viewScope || {};
opts.viewScope.viewMode = opts.viewScope.viewMode || "default";
if (!inputNotePath) {
return;
}
const resolvedNotePath = await this.getResolvedNotePath(inputNotePath);
if (!resolvedNotePath) {
return;
}
if (this.notePath === resolvedNotePath && utils.areObjectsEqual(this.viewScope, opts.viewScope)) {
return;
}
await this.triggerEvent("beforeNoteSwitch", { noteContext: this });
utils.closeActiveDialog();
this.notePath = resolvedNotePath;
this.viewScope = opts.viewScope;
({ noteId: this.noteId, parentNoteId: this.parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(resolvedNotePath));
this.saveToRecentNotes(resolvedNotePath);
protectedSessionHolder.touchProtectedSessionIfNecessary(this.note);
if (opts.triggerSwitchEvent) {
await this.triggerEvent("noteSwitched", {
noteContext: this,
notePath: this.notePath
});
}
await this.setHoistedNoteIfNeeded();
if (utils.isMobile()) {
this.triggerCommand("setActiveScreen", { screen: "detail" });
}
}
async setHoistedNoteIfNeeded() {
if (this.hoistedNoteId === "root" && this.notePath?.startsWith("root/_hidden") && !this.note?.isLabelTruthy("keepCurrentHoisting")) {
// hidden subtree displays only when hoisted, so it doesn't make sense to keep root as hoisted note
let hoistedNoteId = "_hidden";
if (this.note?.isLaunchBarConfig()) {
hoistedNoteId = "_lbRoot";
} else if (this.note?.isOptions()) {
hoistedNoteId = "_options";
}
await this.setHoistedNoteId(hoistedNoteId);
}
}
getSubContexts() {
return appContext.tabManager.noteContexts.filter((nc) => nc.ntxId === this.ntxId || nc.mainNtxId === this.ntxId);
}
/**
* A main context represents a tab and also the first split. Further splits are the children contexts of the main context.
* Imagine you have a tab with 3 splits, each showing notes A, B, C (in this order).
* In such a scenario, A context is the main context (also representing the tab as a whole), and B, C are the children
* of context A.
*
* @returns {boolean} true if the context is main (= tab)
*/
isMainContext() {
// if null, then this is a main context
return !this.mainNtxId;
}
/**
* See docs for isMainContext() for better explanation.
*
* @returns {NoteContext}
*/
getMainContext() {
if (this.mainNtxId) {
try {
return appContext.tabManager.getNoteContextById(this.mainNtxId);
} catch (e) {
this.mainNtxId = null;
return this;
}
} else {
return this;
}
}
saveToRecentNotes(resolvedNotePath: string) {
setTimeout(async () => {
// we include the note in the recent list only if the user stayed on the note at least 5 seconds
if (resolvedNotePath && resolvedNotePath === this.notePath) {
await server.post("recent-notes", {
noteId: this.note?.noteId,
notePath: this.notePath
});
utils.reloadTray();
}
}, 5000);
}
async getResolvedNotePath(inputNotePath: string) {
const resolvedNotePath = await treeService.resolveNotePath(inputNotePath, this.hoistedNoteId);
if (!resolvedNotePath) {
logError(`Cannot resolve note path ${inputNotePath}`);
return;
}
if ((await hoistedNoteService.checkNoteAccess(resolvedNotePath, this)) === false) {
return; // note is outside of hoisted subtree and user chose not to unhoist
}
return resolvedNotePath;
}
get note(): FNote | null {
if (!this.noteId || !(this.noteId in froca.notes)) {
return null;
}
return froca.notes[this.noteId];
}
/** @returns {string[]} */
get notePathArray() {
return this.notePath ? this.notePath.split("/") : [];
}
isActive() {
return appContext.tabManager.activeNtxId === this.ntxId;
}
getPojoState() {
if (this.hoistedNoteId !== "root") {
// keeping empty hoisted tab is esp. important for mobile (e.g. opened launcher config)
if (!this.notePath && this.getSubContexts().length === 0) {
return null;
}
}
return {
ntxId: this.ntxId,
mainNtxId: this.mainNtxId,
notePath: this.notePath,
hoistedNoteId: this.hoistedNoteId,
active: this.isActive(),
viewScope: this.viewScope
};
}
async unhoist() {
await this.setHoistedNoteId("root");
}
async setHoistedNoteId(noteIdToHoist: string) {
if (this.hoistedNoteId === noteIdToHoist) {
return;
}
this.hoistedNoteId = noteIdToHoist;
if (!this.notePathArray?.includes(noteIdToHoist)) {
await this.setNote(noteIdToHoist);
}
await this.triggerEvent("hoistedNoteChanged", {
noteId: noteIdToHoist,
ntxId: this.ntxId
});
}
/** @returns {Promise<boolean>} */
async isReadOnly() {
if (this?.viewScope?.readOnlyTemporarilyDisabled) {
return false;
}
// "readOnly" is a state valid only for text/code notes
if (!this.note || (this.note.type !== "text" && this.note.type !== "code")) {
return false;
}
if (this.note.isLabelTruthy("readOnly")) {
return true;
}
if (this.viewScope?.viewMode === "source") {
return true;
}
const blob = await this.note.getBlob();
if (!blob) {
return false;
}
const sizeLimit = this.note.type === "text" ? options.getInt("autoReadonlySizeText") : options.getInt("autoReadonlySizeCode");
return sizeLimit && blob.contentLength > sizeLimit && !this.note.isLabelTruthy("autoReadOnlyDisabled");
}
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (this.noteId && loadResults.isNoteReloaded(this.noteId)) {
const noteRow = loadResults.getEntityRow("notes", this.noteId);
if (noteRow.isDeleted) {
this.noteId = null;
this.notePath = null;
this.triggerEvent("noteSwitched", {
noteContext: this,
notePath: this.notePath
});
}
}
}
hasNoteList() {
return (
this.note &&
["default", "contextual-help"].includes(this.viewScope?.viewMode ?? "") &&
(this.note.hasChildren() || this.note.getLabelValue("viewType") === "calendar") &&
["book", "text", "code"].includes(this.note.type) &&
this.note.mime !== "text/x-sqlite;schema=trilium" &&
!this.note.isLabelTruthy("hideChildrenOverview")
);
}
async getTextEditor(callback?: GetTextEditorCallback) {
return this.timeout<TextEditor>(
new Promise((resolve) =>
appContext.triggerCommand("executeWithTextEditor", {
callback,
resolve,
ntxId: this.ntxId
})
)
);
}
async getCodeEditor() {
return this.timeout(
new Promise<CodeMirrorInstance>((resolve) =>
appContext.triggerCommand("executeWithCodeEditor", {
resolve,
ntxId: this.ntxId
})
)
);
}
/**
* Returns a promise which will retrieve the JQuery element of the content of this note context.
*
* Do note that retrieving the content element needs to be handled by the type widget, which is the one which
* provides the content element by listening to the `executeWithContentElement` event. Not all note types support
* this.
*
* If no content could be determined `null` is returned instead.
*/
async getContentElement() {
return this.timeout<JQuery<HTMLElement>>(
new Promise((resolve) =>
appContext.triggerCommand("executeWithContentElement", {
resolve,
ntxId: this.ntxId
})
)
);
}
async getTypeWidget() {
return this.timeout(
new Promise<TypeWidget | null>((resolve) =>
appContext.triggerCommand("executeWithTypeWidget", {
resolve,
ntxId: this.ntxId
})
)
);
}
timeout<T>(promise: Promise<T | null>) {
return Promise.race([promise, new Promise((res) => setTimeout(() => res(null), 200))]) as Promise<T>;
}
resetViewScope() {
// view scope contains data specific to one note context and one "view".
// it is used to e.g., make read-only note temporarily editable or to hide TOC
// this is reset after navigating to a different note
this.viewScope = {};
}
async getNavigationTitle() {
if (!this.note) {
return null;
}
const { note, viewScope } = this;
const isNormalView = (viewScope?.viewMode === "default" || viewScope?.viewMode === "contextual-help");
let title = (isNormalView ? note.title : `${note.title}: ${viewScope?.viewMode}`);
if (viewScope?.attachmentId) {
// assuming the attachment has been already loaded
const attachment = await note.getAttachmentById(viewScope.attachmentId);
if (attachment) {
title += `: ${attachment.title}`;
}
}
return title;
}
}
export default NoteContext;

View File

@@ -1,263 +0,0 @@
import Component from "./component.js";
import appContext, { type CommandData, type CommandListenerData } from "./app_context.js";
import dateNoteService from "../services/date_notes.js";
import treeService from "../services/tree.js";
import openService from "../services/open.js";
import protectedSessionService from "../services/protected_session.js";
import options from "../services/options.js";
import froca from "../services/froca.js";
import utils from "../services/utils.js";
import LlmChatPanel from "../widgets/llm_chat_panel.js";
import toastService from "../services/toast.js";
import noteCreateService from "../services/note_create.js";
export default class RootCommandExecutor extends Component {
editReadOnlyNoteCommand() {
const noteContext = appContext.tabManager.getActiveContext();
if (noteContext?.viewScope) {
noteContext.viewScope.readOnlyTemporarilyDisabled = true;
appContext.triggerEvent("readOnlyTemporarilyDisabled", { noteContext });
}
}
async showSQLConsoleCommand() {
const sqlConsoleNote = await dateNoteService.createSqlConsole();
if (!sqlConsoleNote) {
return;
}
const noteContext = await appContext.tabManager.openTabWithNoteWithHoisting(sqlConsoleNote.noteId, { activate: true });
appContext.triggerEvent("focusOnDetail", { ntxId: noteContext.ntxId });
}
async searchNotesCommand({ searchString, ancestorNoteId }: CommandListenerData<"searchNotes">) {
const searchNote = await dateNoteService.createSearchNote({ searchString, ancestorNoteId });
if (!searchNote) {
return;
}
// force immediate search
await froca.loadSearchNote(searchNote.noteId);
const noteContext = await appContext.tabManager.openTabWithNoteWithHoisting(searchNote.noteId, {
activate: true
});
appContext.triggerCommand("focusOnSearchDefinition", { ntxId: noteContext.ntxId });
}
async searchInSubtreeCommand({ notePath }: CommandListenerData<"searchInSubtree">) {
const noteId = treeService.getNoteIdFromUrl(notePath);
this.searchNotesCommand({ ancestorNoteId: noteId });
}
openNoteExternallyCommand() {
const noteId = appContext.tabManager.getActiveContextNoteId();
const mime = appContext.tabManager.getActiveContextNoteMime();
if (noteId) {
openService.openNoteExternally(noteId, mime || "");
}
}
openNoteCustomCommand() {
const noteId = appContext.tabManager.getActiveContextNoteId();
const mime = appContext.tabManager.getActiveContextNoteMime();
if (noteId) {
openService.openNoteCustom(noteId, mime || "");
}
}
enterProtectedSessionCommand() {
protectedSessionService.enterProtectedSession();
}
leaveProtectedSessionCommand() {
protectedSessionService.leaveProtectedSession();
}
hideLeftPaneCommand() {
options.save(`leftPaneVisible`, "false");
}
showLeftPaneCommand() {
options.save(`leftPaneVisible`, "true");
}
toggleLeftPaneCommand() {
options.toggle("leftPaneVisible");
}
async showBackendLogCommand() {
await appContext.tabManager.openTabWithNoteWithHoisting("_backendLog", { activate: true });
}
async showHelpCommand() {
await this.showAndHoistSubtree("_help");
}
async showLaunchBarSubtreeCommand() {
const rootNote = utils.isMobile() ? "_lbMobileRoot" : "_lbRoot";
await this.showAndHoistSubtree(rootNote);
this.showLeftPaneCommand();
}
async showShareSubtreeCommand() {
await this.showAndHoistSubtree("_share");
}
async showHiddenSubtreeCommand() {
await this.showAndHoistSubtree("_hidden");
}
async showOptionsCommand({ section }: CommandListenerData<"showOptions">) {
await appContext.tabManager.openContextWithNote(section || "_options", {
activate: true,
hoistedNoteId: "_options"
});
}
async showSQLConsoleHistoryCommand() {
await this.showAndHoistSubtree("_sqlConsole");
}
async showSearchHistoryCommand() {
await this.showAndHoistSubtree("_search");
}
async showAndHoistSubtree(subtreeNoteId: string) {
await appContext.tabManager.openContextWithNote(subtreeNoteId, {
activate: true,
hoistedNoteId: subtreeNoteId
});
}
async showNoteSourceCommand() {
const notePath = appContext.tabManager.getActiveContextNotePath();
if (notePath) {
await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
activate: true,
viewScope: {
viewMode: "source"
}
});
}
}
async showAttachmentsCommand() {
const notePath = appContext.tabManager.getActiveContextNotePath();
if (notePath) {
await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
activate: true,
viewScope: {
viewMode: "attachments"
}
});
}
}
async showAttachmentDetailCommand() {
const notePath = appContext.tabManager.getActiveContextNotePath();
if (notePath) {
await appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
activate: true,
viewScope: {
viewMode: "attachments"
}
});
}
}
toggleTrayCommand() {
if (!utils.isElectron()) return;
const { BrowserWindow } = utils.dynamicRequire("@electron/remote");
const windows = BrowserWindow.getAllWindows() as Electron.BaseWindow[];
const isVisible = windows.every((w) => w.isVisible());
const action = isVisible ? "hide" : "show";
for (const window of windows) window[action]();
}
toggleZenModeCommand() {
const $body = $("body");
$body.toggleClass("zen");
const isEnabled = $body.hasClass("zen");
appContext.triggerEvent("zenModeChanged", { isEnabled });
}
firstTabCommand() {
this.#goToTab(1);
}
secondTabCommand() {
this.#goToTab(2);
}
thirdTabCommand() {
this.#goToTab(3);
}
fourthTabCommand() {
this.#goToTab(4);
}
fifthTabCommand() {
this.#goToTab(5);
}
sixthTabCommand() {
this.#goToTab(6);
}
seventhTabCommand() {
this.#goToTab(7);
}
eigthTabCommand() {
this.#goToTab(8);
}
ninthTabCommand() {
this.#goToTab(9);
}
lastTabCommand() {
this.#goToTab(Number.POSITIVE_INFINITY);
}
#goToTab(tabNumber: number) {
const mainNoteContexts = appContext.tabManager.getMainNoteContexts();
const index = tabNumber === Number.POSITIVE_INFINITY ? mainNoteContexts.length - 1 : tabNumber - 1;
const tab = mainNoteContexts[index];
if (tab) {
appContext.tabManager.activateNoteContext(tab.ntxId);
}
}
async createAiChatCommand() {
try {
// Create a new AI Chat note at the root level
const rootNoteId = "root";
const result = await noteCreateService.createNote(rootNoteId, {
title: "New AI Chat",
type: "aiChat",
content: JSON.stringify({
messages: [],
title: "New AI Chat"
})
});
if (!result.note) {
toastService.showError("Failed to create AI Chat note");
return;
}
await appContext.tabManager.openTabWithNoteWithHoisting(result.note.noteId, {
activate: true
});
toastService.showMessage("Created new AI Chat note");
}
catch (e) {
console.error("Error creating AI Chat note:", e);
toastService.showError("Failed to create AI Chat note: " + (e as Error).message);
}
}
}

View File

@@ -1,44 +0,0 @@
import appContext, { type EventData, type EventListener } from "./app_context.js";
import shortcutService from "../services/shortcuts.js";
import server from "../services/server.js";
import Component from "./component.js";
import froca from "../services/froca.js";
import type { AttributeRow } from "../services/load_results.js";
export default class ShortcutComponent extends Component implements EventListener<"entitiesReloaded"> {
constructor() {
super();
server.get<AttributeRow[]>("keyboard-shortcuts-for-notes").then((shortcutAttributes) => {
for (const attr of shortcutAttributes) {
this.bindNoteShortcutHandler(attr);
}
});
}
bindNoteShortcutHandler(labelOrRow: AttributeRow) {
const handler = () => appContext.tabManager.getActiveContext()?.setNote(labelOrRow.noteId);
const namespace = labelOrRow.attributeId;
if (labelOrRow.isDeleted) {
// only applicable if row
if (namespace) {
shortcutService.removeGlobalShortcut(namespace);
}
} else if (labelOrRow.value) {
shortcutService.bindGlobalShortcut(labelOrRow.value, handler, namespace);
}
}
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
for (const attr of loadResults.getAttributeRows()) {
if (attr.type === "label" && attr.name === "keyboardShortcut" && attr.noteId) {
const note = await froca.getNote(attr.noteId);
// launcher shortcuts are handled specifically
if (note && attr && note.type !== "launcher") {
this.bindNoteShortcutHandler(attr);
}
}
}
}
}

View File

@@ -1,700 +0,0 @@
import Component from "./component.js";
import SpacedUpdate from "../services/spaced_update.js";
import server from "../services/server.js";
import options from "../services/options.js";
import froca from "../services/froca.js";
import treeService from "../services/tree.js";
import NoteContext from "./note_context.js";
import appContext from "./app_context.js";
import Mutex from "../utils/mutex.js";
import linkService from "../services/link.js";
import type { EventData } from "./app_context.js";
import type FNote from "../entities/fnote.js";
interface TabState {
contexts: NoteContext[];
position: number;
}
interface NoteContextState {
ntxId: string;
mainNtxId: string | null;
notePath: string | null;
hoistedNoteId: string;
active: boolean;
viewScope: Record<string, any>;
}
export default class TabManager extends Component {
public children: NoteContext[];
public mutex: Mutex;
public activeNtxId: string | null;
public recentlyClosedTabs: TabState[];
public tabsUpdate: SpacedUpdate;
constructor() {
super();
this.children = [];
this.mutex = new Mutex();
this.activeNtxId = null;
this.recentlyClosedTabs = [];
this.tabsUpdate = new SpacedUpdate(async () => {
if (!appContext.isMainWindow) {
return;
}
const openNoteContexts = this.noteContexts
.map((nc) => nc.getPojoState())
.filter((t) => !!t);
await server.put("options", {
openNoteContexts: JSON.stringify(openNoteContexts)
});
});
appContext.addBeforeUnloadListener(this);
}
get noteContexts(): NoteContext[] {
return this.children;
}
get mainNoteContexts(): NoteContext[] {
return this.noteContexts.filter((nc) => !nc.mainNtxId);
}
async loadTabs() {
try {
const noteContextsToOpen = (appContext.isMainWindow && options.getJson("openNoteContexts")) || [];
// preload all notes at once
await froca.getNotes([...noteContextsToOpen.flatMap((tab: NoteContextState) =>
[treeService.getNoteIdFromUrl(tab.notePath), tab.hoistedNoteId])], true);
const filteredNoteContexts = noteContextsToOpen.filter((openTab: NoteContextState) => {
const noteId = treeService.getNoteIdFromUrl(openTab.notePath);
if (!noteId || !(noteId in froca.notes)) {
// note doesn't exist so don't try to open tab for it
return false;
}
if (!(openTab.hoistedNoteId in froca.notes)) {
openTab.hoistedNoteId = "root";
}
return true;
});
// resolve before opened tabs can change this
const parsedFromUrl = linkService.parseNavigationStateFromUrl(window.location.href);
if (filteredNoteContexts.length === 0) {
parsedFromUrl.ntxId = parsedFromUrl.ntxId || NoteContext.generateNtxId(); // generate already here, so that we later know which one to activate
filteredNoteContexts.push({
notePath: parsedFromUrl.notePath || "root",
ntxId: parsedFromUrl.ntxId,
active: true,
hoistedNoteId: parsedFromUrl.hoistedNoteId || "root",
viewScope: parsedFromUrl.viewScope || {}
});
} else if (!filteredNoteContexts.find((tab: NoteContextState) => tab.active)) {
filteredNoteContexts[0].active = true;
}
await this.tabsUpdate.allowUpdateWithoutChange(async () => {
for (const tab of filteredNoteContexts) {
await this.openContextWithNote(tab.notePath, {
activate: tab.active,
ntxId: tab.ntxId,
mainNtxId: tab.mainNtxId,
hoistedNoteId: tab.hoistedNoteId,
viewScope: tab.viewScope
});
}
});
// if there's a notePath in the URL, make sure it's open and active
// (useful, for e.g., opening clipped notes from clipper or opening link in an extra window)
if (parsedFromUrl.notePath) {
await appContext.tabManager.switchToNoteContext(
parsedFromUrl.ntxId,
parsedFromUrl.notePath,
parsedFromUrl.viewScope,
parsedFromUrl.hoistedNoteId
);
} else if (parsedFromUrl.searchString) {
await appContext.triggerCommand("searchNotes", {
searchString: parsedFromUrl.searchString
});
}
} catch (e: unknown) {
if (e instanceof Error) {
logError(`Loading note contexts '${options.get("openNoteContexts")}' failed: ${e.message} ${e.stack}`);
} else {
logError(`Loading note contexts '${options.get("openNoteContexts")}' failed: ${String(e)}`);
}
// try to recover
await this.openEmptyTab();
}
}
noteSwitchedEvent({ noteContext }: EventData<"noteSwitched">) {
if (noteContext.isActive()) {
this.setCurrentNavigationStateToHash();
}
this.tabsUpdate.scheduleUpdate();
}
setCurrentNavigationStateToHash() {
const calculatedHash = this.calculateHash();
// update if it's the first history entry or there has been a change
if (window.history.length === 0 || calculatedHash !== window.location?.hash) {
// using pushState instead of directly modifying document.location because it does not trigger hashchange
window.history.pushState(null, "", calculatedHash);
}
const activeNoteContext = this.getActiveContext();
this.updateDocumentTitle(activeNoteContext);
this.triggerEvent("activeNoteChanged", {}); // trigger this even in on popstate event
}
calculateHash(): string {
const activeNoteContext = this.getActiveContext();
if (!activeNoteContext) {
return "";
}
return linkService.calculateHash({
notePath: activeNoteContext.notePath,
ntxId: activeNoteContext.ntxId,
hoistedNoteId: activeNoteContext.hoistedNoteId,
viewScope: activeNoteContext.viewScope
});
}
getNoteContexts(): NoteContext[] {
return this.noteContexts;
}
getMainNoteContexts(): NoteContext[] {
return this.noteContexts.filter((nc) => nc.isMainContext());
}
getNoteContextById(ntxId: string | null): NoteContext {
const noteContext = this.noteContexts.find((nc) => nc.ntxId === ntxId);
if (!noteContext) {
throw new Error(`Cannot find noteContext id='${ntxId}'`);
}
return noteContext;
}
getActiveContext(): NoteContext | null {
return this.activeNtxId ? this.getNoteContextById(this.activeNtxId) : null;
}
getActiveMainContext(): NoteContext | null {
return this.activeNtxId ? this.getNoteContextById(this.activeNtxId).getMainContext() : null;
}
getActiveContextNotePath(): string | null {
const activeContext = this.getActiveContext();
return activeContext?.notePath ?? null;
}
getActiveContextNote(): FNote | null {
const activeContext = this.getActiveContext();
return activeContext ? activeContext.note : null;
}
getActiveContextNoteId(): string | null {
const activeNote = this.getActiveContextNote();
return activeNote ? activeNote.noteId : null;
}
getActiveContextNoteType(): string | null {
const activeNote = this.getActiveContextNote();
return activeNote ? activeNote.type : null;
}
getActiveContextNoteMime(): string | null {
const activeNote = this.getActiveContextNote();
return activeNote ? activeNote.mime : null;
}
async switchToNoteContext(
ntxId: string | null,
notePath: string,
viewScope: Record<string, any> = {},
hoistedNoteId: string | null = null
) {
const noteContext = this.noteContexts.find((nc) => nc.ntxId === ntxId) ||
await this.openEmptyTab();
await this.activateNoteContext(noteContext.ntxId);
if (hoistedNoteId) {
await noteContext.setHoistedNoteId(hoistedNoteId);
}
if (notePath) {
await noteContext.setNote(notePath, { viewScope });
}
}
async openAndActivateEmptyTab() {
const noteContext = await this.openEmptyTab();
await this.activateNoteContext(noteContext.ntxId);
noteContext.setEmpty();
}
async openEmptyTab(
ntxId: string | null = null,
hoistedNoteId: string = "root",
mainNtxId: string | null = null
): Promise<NoteContext> {
const noteContext = new NoteContext(ntxId, hoistedNoteId, mainNtxId);
const existingNoteContext = this.children.find((nc) => nc.ntxId === noteContext.ntxId);
if (existingNoteContext) {
await existingNoteContext.setHoistedNoteId(hoistedNoteId);
return existingNoteContext;
}
this.child(noteContext);
await this.triggerEvent("newNoteContextCreated", { noteContext });
return noteContext;
}
async openInNewTab(targetNoteId: string, hoistedNoteId: string | null = null) {
const noteContext = await this.openEmptyTab(null, hoistedNoteId || this.getActiveContext()?.hoistedNoteId);
await noteContext.setNote(targetNoteId);
}
async openInSameTab(targetNoteId: string, hoistedNoteId: string | null = null) {
const activeContext = this.getActiveContext();
if (!activeContext) return;
await activeContext.setHoistedNoteId(hoistedNoteId || activeContext.hoistedNoteId);
await activeContext.setNote(targetNoteId);
}
async openTabWithNoteWithHoisting(
notePath: string,
opts: {
activate?: boolean | null;
ntxId?: string | null;
mainNtxId?: string | null;
hoistedNoteId?: string | null;
viewScope?: Record<string, any> | null;
} = {}
): Promise<NoteContext> {
const noteContext = this.getActiveContext();
let hoistedNoteId = "root";
if (noteContext) {
const resolvedNotePath = await treeService.resolveNotePath(notePath, noteContext.hoistedNoteId);
if (resolvedNotePath?.includes(noteContext.hoistedNoteId) || resolvedNotePath?.includes("_hidden")) {
hoistedNoteId = noteContext.hoistedNoteId;
}
}
opts.hoistedNoteId = hoistedNoteId;
return this.openContextWithNote(notePath, opts);
}
async openContextWithNote(
notePath: string | null,
opts: {
activate?: boolean | null;
ntxId?: string | null;
mainNtxId?: string | null;
hoistedNoteId?: string | null;
viewScope?: Record<string, any> | null;
} = {}
): Promise<NoteContext> {
const activate = !!opts.activate;
const ntxId = opts.ntxId || null;
const mainNtxId = opts.mainNtxId || null;
const hoistedNoteId = opts.hoistedNoteId || "root";
const viewScope = opts.viewScope || { viewMode: "default" };
const noteContext = await this.openEmptyTab(ntxId, hoistedNoteId, mainNtxId);
if (notePath) {
await noteContext.setNote(notePath, {
// if activate is false, then send normal noteSwitched event
triggerSwitchEvent: !activate,
viewScope: viewScope
});
}
if (activate && noteContext.notePath) {
this.activateNoteContext(noteContext.ntxId, false);
await this.triggerEvent("noteSwitchedAndActivated", {
noteContext,
notePath: noteContext.notePath // resolved note path
});
}
return noteContext;
}
async activateOrOpenNote(noteId: string) {
for (const noteContext of this.getNoteContexts()) {
if (noteContext.note && noteContext.note.noteId === noteId) {
this.activateNoteContext(noteContext.ntxId);
return;
}
}
// if no tab with this note has been found we'll create new tab
await this.openContextWithNote(noteId, { activate: true });
}
async activateNoteContext(ntxId: string | null, triggerEvent: boolean = true) {
if (!ntxId) {
logError("activateNoteContext: ntxId is null");
return;
}
if (ntxId === this.activeNtxId) {
return;
}
this.activeNtxId = ntxId;
if (triggerEvent) {
await this.triggerEvent("activeContextChanged", {
noteContext: this.getNoteContextById(ntxId)
});
}
this.tabsUpdate.scheduleUpdate();
this.setCurrentNavigationStateToHash();
}
async removeNoteContext(ntxId: string | null): Promise<boolean> {
// removing note context is an async process which can take some time, if users presses CTRL-W quickly, two
// close events could interleave which would then lead to attempting to activate already removed context.
return await this.mutex.runExclusively(async (): Promise<boolean> => {
let noteContextToRemove;
try {
noteContextToRemove = this.getNoteContextById(ntxId);
} catch {
// note context not found
return false;
}
if (noteContextToRemove.isMainContext()) {
const mainNoteContexts = this.getNoteContexts().filter((nc) => nc.isMainContext());
if (mainNoteContexts.length === 1) {
if (noteContextToRemove.isEmpty()) {
// this is already the empty note context, no point in closing it and replacing with another
// empty tab
return false;
}
await this.openEmptyTab();
}
}
// close dangling autocompletes after closing the tab
const $autocompleteEl = $(".aa-input");
if ("autocomplete" in $autocompleteEl) {
$autocompleteEl.autocomplete("close");
}
const noteContextsToRemove = noteContextToRemove.getSubContexts();
const ntxIdsToRemove = noteContextsToRemove.map((nc) => nc.ntxId);
await this.triggerEvent("beforeNoteContextRemove", { ntxIds: ntxIdsToRemove.filter((id) => id !== null) });
if (!noteContextToRemove.isMainContext()) {
const siblings = noteContextToRemove.getMainContext().getSubContexts();
const idx = siblings.findIndex((nc) => nc.ntxId === noteContextToRemove.ntxId);
const contextToActivateIdx = idx === siblings.length - 1 ? idx - 1 : idx + 1;
const contextToActivate = siblings[contextToActivateIdx];
await this.activateNoteContext(contextToActivate.ntxId);
} else if (this.mainNoteContexts.length <= 1) {
await this.openAndActivateEmptyTab();
} else if (ntxIdsToRemove.includes(this.activeNtxId)) {
const idx = this.mainNoteContexts.findIndex((nc) => nc.ntxId === noteContextToRemove.ntxId);
if (idx === this.mainNoteContexts.length - 1) {
await this.activatePreviousTabCommand();
} else {
await this.activateNextTabCommand();
}
}
this.removeNoteContexts(noteContextsToRemove);
return true;
});
}
removeNoteContexts(noteContextsToRemove: NoteContext[]) {
const ntxIdsToRemove = noteContextsToRemove.map((nc) => nc.ntxId);
const position = this.noteContexts.findIndex((nc) => ntxIdsToRemove.includes(nc.ntxId));
this.children = this.children.filter((nc) => !ntxIdsToRemove.includes(nc.ntxId));
this.addToRecentlyClosedTabs(noteContextsToRemove, position);
this.triggerEvent("noteContextRemoved", { ntxIds: ntxIdsToRemove.filter((id) => id !== null) });
this.tabsUpdate.scheduleUpdate();
}
addToRecentlyClosedTabs(noteContexts: NoteContext[], position: number) {
if (noteContexts.length === 1 && noteContexts[0].isEmpty()) {
return;
}
this.recentlyClosedTabs.push({ contexts: noteContexts, position: position });
}
tabReorderEvent({ ntxIdsInOrder }: { ntxIdsInOrder: string[] }) {
const order: Record<string, number> = {};
let i = 0;
for (const ntxId of ntxIdsInOrder) {
for (const noteContext of this.getNoteContextById(ntxId).getSubContexts()) {
if (noteContext.ntxId) {
order[noteContext.ntxId] = i++;
}
}
}
this.children.sort((a, b) => {
if (!a.ntxId || !b.ntxId) return 0;
return (order[a.ntxId] ?? 0) < (order[b.ntxId] ?? 0) ? -1 : 1;
});
this.tabsUpdate.scheduleUpdate();
}
noteContextReorderEvent({
ntxIdsInOrder,
oldMainNtxId,
newMainNtxId
}: {
ntxIdsInOrder: string[];
oldMainNtxId?: string;
newMainNtxId?: string;
}) {
const order = Object.fromEntries(ntxIdsInOrder.map((v, i) => [v, i]));
this.children.sort((a, b) => {
if (!a.ntxId || !b.ntxId) return 0;
return (order[a.ntxId] ?? 0) < (order[b.ntxId] ?? 0) ? -1 : 1;
});
if (oldMainNtxId && newMainNtxId) {
this.children.forEach((c) => {
if (c.ntxId === newMainNtxId) {
// new main context has null mainNtxId
c.mainNtxId = null;
} else if (c.ntxId === oldMainNtxId || c.mainNtxId === oldMainNtxId) {
// old main context or subcontexts all have the new mainNtxId
c.mainNtxId = newMainNtxId;
}
});
}
this.tabsUpdate.scheduleUpdate();
}
async activateNextTabCommand() {
const activeMainNtxId = this.getActiveMainContext()?.ntxId;
if (!activeMainNtxId) return;
const oldIdx = this.mainNoteContexts.findIndex((nc) => nc.ntxId === activeMainNtxId);
const newActiveNtxId = this.mainNoteContexts[oldIdx === this.mainNoteContexts.length - 1 ? 0 : oldIdx + 1].ntxId;
await this.activateNoteContext(newActiveNtxId);
}
async activatePreviousTabCommand() {
const activeMainNtxId = this.getActiveMainContext()?.ntxId;
if (!activeMainNtxId) return;
const oldIdx = this.mainNoteContexts.findIndex((nc) => nc.ntxId === activeMainNtxId);
const newActiveNtxId = this.mainNoteContexts[oldIdx === 0 ? this.mainNoteContexts.length - 1 : oldIdx - 1].ntxId;
await this.activateNoteContext(newActiveNtxId);
}
async closeActiveTabCommand() {
await this.removeNoteContext(this.activeNtxId);
}
beforeUnloadEvent(): boolean {
this.tabsUpdate.updateNowIfNecessary();
return true; // don't block closing the tab, this metadata is not that important
}
openNewTabCommand() {
this.openAndActivateEmptyTab();
}
async closeAllTabsCommand() {
for (const ntxIdToRemove of this.mainNoteContexts.map((nc) => nc.ntxId)) {
await this.removeNoteContext(ntxIdToRemove);
}
}
async closeOtherTabsCommand({ ntxId }: { ntxId: string }) {
for (const ntxIdToRemove of this.mainNoteContexts.map((nc) => nc.ntxId)) {
if (ntxIdToRemove !== ntxId) {
await this.removeNoteContext(ntxIdToRemove);
}
}
}
async closeRightTabsCommand({ ntxId }: { ntxId: string }) {
const ntxIds = this.mainNoteContexts.map((nc) => nc.ntxId);
const index = ntxIds.indexOf(ntxId);
if (index !== -1) {
const idsToRemove = ntxIds.slice(index + 1);
for (const ntxIdToRemove of idsToRemove) {
await this.removeNoteContext(ntxIdToRemove);
}
}
}
async closeTabCommand({ ntxId }: { ntxId: string }) {
await this.removeNoteContext(ntxId);
}
async moveTabToNewWindowCommand({ ntxId }: { ntxId: string }) {
const { notePath, hoistedNoteId } = this.getNoteContextById(ntxId);
const removed = await this.removeNoteContext(ntxId);
if (removed) {
this.triggerCommand("openInWindow", { notePath, hoistedNoteId });
}
}
async copyTabToNewWindowCommand({ ntxId }: { ntxId: string }) {
const { notePath, hoistedNoteId } = this.getNoteContextById(ntxId);
this.triggerCommand("openInWindow", { notePath, hoistedNoteId });
}
async reopenLastTabCommand() {
const closeLastEmptyTab: NoteContext | undefined = await this.mutex.runExclusively(async () => {
let closeLastEmptyTab
if (this.recentlyClosedTabs.length === 0) {
return closeLastEmptyTab;
}
if (this.noteContexts.length === 1 && this.noteContexts[0].isEmpty()) {
// new empty tab is created after closing the last tab, this reverses the empty tab creation
closeLastEmptyTab = this.noteContexts[0];
}
const lastClosedTab = this.recentlyClosedTabs.pop();
if (!lastClosedTab) return closeLastEmptyTab;
const noteContexts = lastClosedTab.contexts;
for (const noteContext of noteContexts) {
this.child(noteContext);
await this.triggerEvent("newNoteContextCreated", { noteContext });
}
// restore last position of contexts stored in tab manager
const ntxsInOrder = [
...this.noteContexts.slice(0, lastClosedTab.position),
...this.noteContexts.slice(-noteContexts.length),
...this.noteContexts.slice(lastClosedTab.position, -noteContexts.length)
];
this.noteContextReorderEvent({ ntxIdsInOrder: ntxsInOrder.map((nc) => nc.ntxId).filter((id) => id !== null) });
let mainNtx = noteContexts.find((nc) => nc.isMainContext());
if (mainNtx) {
// reopened a tab, need to reorder new tab widget in tab row
await this.triggerEvent("contextsReopened", {
mainNtxId: mainNtx.ntxId,
tabPosition: ntxsInOrder.filter((nc) => nc.isMainContext()).findIndex((nc) => nc.ntxId === mainNtx.ntxId)
});
} else {
// reopened a single split, need to reorder the pane widget in split note container
await this.triggerEvent("contextsReopened", {
mainNtxId: ntxsInOrder[lastClosedTab.position].ntxId,
// this is safe since lastClosedTab.position can never be 0 in this case
tabPosition: lastClosedTab.position - 1
});
}
const noteContextToActivate = noteContexts.length === 1 ? noteContexts[0] : noteContexts.find((nc) => nc.isMainContext());
if (!noteContextToActivate) return closeLastEmptyTab;
await this.activateNoteContext(noteContextToActivate.ntxId);
await this.triggerEvent("noteSwitched", {
noteContext: noteContextToActivate,
notePath: noteContextToActivate.notePath
});
return closeLastEmptyTab;
});
if (closeLastEmptyTab) {
await this.removeNoteContext(closeLastEmptyTab.ntxId);
}
}
hoistedNoteChangedEvent() {
this.tabsUpdate.scheduleUpdate();
}
async updateDocumentTitle(activeNoteContext: NoteContext | null) {
if (!activeNoteContext) return;
const titleFragments = [
// it helps to navigate in history if note title is included in the title
await activeNoteContext.getNavigationTitle(),
"TriliumNext Notes"
].filter(Boolean);
document.title = titleFragments.join(" - ");
}
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
const activeContext = this.getActiveContext();
if (activeContext && loadResults.isNoteReloaded(activeContext.noteId)) {
await this.updateDocumentTitle(activeContext);
}
}
async frocaReloadedEvent() {
const activeContext = this.getActiveContext();
if (activeContext) {
await this.updateDocumentTitle(activeContext);
}
}
}

View File

@@ -1,135 +0,0 @@
import utils from "../services/utils.js";
import Component from "./component.js";
import appContext from "./app_context.js";
import type { TouchBarButton, TouchBarGroup, TouchBarSegmentedControl, TouchBarSpacer } from "@electron/remote";
export type TouchBarItem = (TouchBarButton | TouchBarSpacer | TouchBarGroup | TouchBarSegmentedControl);
export function buildSelectedBackgroundColor(isSelected: boolean) {
return isSelected ? "#757575" : undefined;
}
export default class TouchBarComponent extends Component {
nativeImage: typeof import("electron").nativeImage;
remote: typeof import("@electron/remote");
lastFocusedComponent?: Component;
private $activeModal?: JQuery<HTMLElement>;
constructor() {
super();
this.nativeImage = utils.dynamicRequire("electron").nativeImage;
this.remote = utils.dynamicRequire("@electron/remote") as typeof import("@electron/remote");
this.$widget = $("<div>");
$(window).on("focusin", async (e) => {
const $target = $(e.target);
this.$activeModal = $target.closest(".modal-dialog");
const parentComponentEl = $target.closest(".component");
this.lastFocusedComponent = appContext.getComponentByEl(parentComponentEl[0]);
this.#refreshTouchBar();
});
}
buildIcon(name: string) {
const sourceImage = this.nativeImage.createFromNamedImage(name, [-1, 0, 1]);
const { width, height } = sourceImage.getSize();
const newImage = this.nativeImage.createEmpty();
newImage.addRepresentation({
scaleFactor: 1,
width: width / 2,
height: height / 2,
buffer: sourceImage.resize({ height: height / 2 }).toBitmap()
});
newImage.addRepresentation({
scaleFactor: 2,
width: width,
height: height,
buffer: sourceImage.toBitmap()
});
return newImage;
}
#refreshTouchBar() {
const { TouchBar } = this.remote;
const parentComponent = this.lastFocusedComponent;
let touchBar = null;
if (this.$activeModal?.length) {
touchBar = this.#buildModalTouchBar();
} else if (parentComponent) {
const items = parentComponent.triggerCommand("buildTouchBar", {
TouchBar,
buildIcon: this.buildIcon.bind(this)
}) as unknown as TouchBarItem[];
touchBar = this.#buildTouchBar(items);
}
if (touchBar) {
this.remote.getCurrentWindow().setTouchBar(touchBar);
}
}
#buildModalTouchBar() {
const { TouchBar } = this.remote;
const { TouchBarButton, TouchBarLabel, TouchBarSpacer } = this.remote.TouchBar;
const items: TouchBarItem[] = [];
// Look for the modal title.
const $title = this.$activeModal?.find(".modal-title");
if ($title?.length) {
items.push(new TouchBarLabel({ label: $title.text() }))
}
items.push(new TouchBarSpacer({ size: "flexible" }));
// Look for buttons in the modal.
const $buttons = this.$activeModal?.find(".modal-footer button");
for (const button of $buttons ?? []) {
items.push(new TouchBarButton({
label: button.innerText,
click: () => button.click(),
enabled: !button.hasAttribute("disabled")
}));
}
items.push(new TouchBarSpacer({ size: "flexible" }));
return new TouchBar({ items });
}
#buildTouchBar(componentSpecificItems?: TouchBarItem[]) {
const { TouchBar } = this.remote;
const { TouchBarButton, TouchBarSpacer, TouchBarGroup, TouchBarSegmentedControl, TouchBarOtherItemsProxy } = this.remote.TouchBar;
// Disregard recursive calls or empty results.
if (!componentSpecificItems || "then" in componentSpecificItems) {
componentSpecificItems = [];
}
const items = [
new TouchBarButton({
icon: this.buildIcon("NSTouchBarComposeTemplate"),
click: () => this.triggerCommand("createNoteIntoInbox")
}),
new TouchBarSpacer({ size: "small" }),
...componentSpecificItems,
new TouchBarSpacer({ size: "flexible" }),
new TouchBarOtherItemsProxy(),
new TouchBarButton({
icon: this.buildIcon("NSTouchBarAddDetailTemplate"),
click: () => this.triggerCommand("jumpToNote")
})
].flat();
console.log("Update ", items);
return new TouchBar({
items
});
}
refreshTouchBarEvent() {
this.#refreshTouchBar();
}
}

View File

@@ -1,68 +0,0 @@
import options from "../services/options.js";
import Component from "./component.js";
import utils from "../services/utils.js";
const MIN_ZOOM = 0.5;
const MAX_ZOOM = 2.0;
class ZoomComponent extends Component {
constructor() {
super();
if (utils.isElectron()) {
options.initializedPromise.then(() => {
const zoomFactor = options.getFloat("zoomFactor");
if (zoomFactor) {
this.setZoomFactor(zoomFactor);
}
});
window.addEventListener("wheel", (event) => {
if (event.ctrlKey) {
this.setZoomFactorAndSave(this.getCurrentZoom() - event.deltaY * 0.001);
}
});
}
}
setZoomFactor(zoomFactor: string | number) {
const parsedZoomFactor = typeof zoomFactor !== "number" ? parseFloat(zoomFactor) : zoomFactor;
const webFrame = utils.dynamicRequire("electron").webFrame;
webFrame.setZoomFactor(parsedZoomFactor);
}
async setZoomFactorAndSave(zoomFactor: number) {
if (zoomFactor >= MIN_ZOOM && zoomFactor <= MAX_ZOOM) {
zoomFactor = Math.round(zoomFactor * 10) / 10;
this.setZoomFactor(zoomFactor);
await options.save("zoomFactor", zoomFactor);
} else {
console.log(`Zoom factor ${zoomFactor} outside of the range, ignored.`);
}
}
getCurrentZoom() {
return utils.dynamicRequire("electron").webFrame.getZoomFactor();
}
zoomOutEvent() {
this.setZoomFactorAndSave(this.getCurrentZoom() - 0.1);
}
zoomInEvent() {
this.setZoomFactorAndSave(this.getCurrentZoom() + 0.1);
}
zoomResetEvent() {
this.setZoomFactorAndSave(1);
}
setZoomFactorAndSaveEvent({ zoomFactor }: { zoomFactor: number }) {
this.setZoomFactorAndSave(zoomFactor);
}
}
const zoomService = new ZoomComponent();
export default zoomService;

View File

@@ -1,113 +0,0 @@
import appContext from "./components/app_context.js";
import utils from "./services/utils.js";
import noteTooltipService from "./services/note_tooltip.js";
import bundleService from "./services/bundle.js";
import toastService from "./services/toast.js";
import noteAutocompleteService from "./services/note_autocomplete.js";
import electronContextMenu from "./menus/electron_context_menu.js";
import glob from "./services/glob.js";
import { t } from "./services/i18n.js";
import options from "./services/options.js";
import type ElectronRemote from "@electron/remote";
import type Electron from "electron";
import "../stylesheets/bootstrap.scss";
await appContext.earlyInit();
bundleService.getWidgetBundlesByParent().then(async (widgetBundles) => {
// A dynamic import is required for layouts since they initialize components which require translations.
const DesktopLayout = (await import("./layouts/desktop_layout.js")).default;
appContext.setLayout(new DesktopLayout(widgetBundles));
appContext.start().catch((e) => {
toastService.showPersistent({
title: t("toast.critical-error.title"),
icon: "alert",
message: t("toast.critical-error.message", { message: e.message })
});
console.error("Critical error occured", e);
});
});
glob.setupGlobs();
if (utils.isElectron()) {
initOnElectron();
}
noteTooltipService.setupGlobalTooltip();
noteAutocompleteService.init();
if (utils.isElectron()) {
electronContextMenu.setupContextMenu();
}
function initOnElectron() {
const electron: typeof Electron = utils.dynamicRequire("electron");
electron.ipcRenderer.on("globalShortcut", async (event, actionName) => appContext.triggerCommand(actionName));
electron.ipcRenderer.on("openInSameTab", async (event, noteId) => appContext.tabManager.openInSameTab(noteId));
const electronRemote: typeof ElectronRemote = utils.dynamicRequire("@electron/remote");
const currentWindow = electronRemote.getCurrentWindow();
const style = window.getComputedStyle(document.body);
initDarkOrLightMode(style);
initTransparencyEffects(style, currentWindow);
if (options.get("nativeTitleBarVisible") !== "true") {
initTitleBarButtons(style, currentWindow);
}
}
function initTitleBarButtons(style: CSSStyleDeclaration, currentWindow: Electron.BrowserWindow) {
if (window.glob.platform === "win32") {
const applyWindowsOverlay = () => {
const color = style.getPropertyValue("--native-titlebar-background");
const symbolColor = style.getPropertyValue("--native-titlebar-foreground");
if (color && symbolColor) {
currentWindow.setTitleBarOverlay({ color, symbolColor });
}
};
applyWindowsOverlay();
// Register for changes to the native title bar colors.
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", applyWindowsOverlay);
}
if (window.glob.platform === "darwin") {
const xOffset = parseInt(style.getPropertyValue("--native-titlebar-darwin-x-offset"), 10);
const yOffset = parseInt(style.getPropertyValue("--native-titlebar-darwin-y-offset"), 10);
currentWindow.setWindowButtonPosition({ x: xOffset, y: yOffset });
}
}
function initTransparencyEffects(style: CSSStyleDeclaration, currentWindow: Electron.BrowserWindow) {
if (window.glob.platform === "win32") {
const material = style.getPropertyValue("--background-material");
// TriliumNextTODO: find a nicer way to make TypeScript happy unfortunately TS did not like Array.includes here
const bgMaterialOptions = ["auto", "none", "mica", "acrylic", "tabbed"] as const;
const foundBgMaterialOption = bgMaterialOptions.find((bgMaterialOption) => material === bgMaterialOption);
if (foundBgMaterialOption) {
currentWindow.setBackgroundMaterial(foundBgMaterialOption);
}
}
}
/**
* Informs Electron that we prefer a dark or light theme. Apart from changing prefers-color-scheme at CSS level which is a side effect,
* this fixes color issues with background effects or native title bars.
*
* @param style the root CSS element to read variables from.
*/
function initDarkOrLightMode(style: CSSStyleDeclaration) {
let themeSource: typeof nativeTheme.themeSource = "system";
const themeStyle = style.getPropertyValue("--theme-style");
if (style.getPropertyValue("--theme-style-auto") !== "true" && (themeStyle === "light" || themeStyle === "dark")) {
themeSource = themeStyle;
}
const { nativeTheme } = utils.dynamicRequire("@electron/remote") as typeof ElectronRemote;
nativeTheme.themeSource = themeSource;
}

View File

@@ -1,65 +0,0 @@
import type { Froca } from "../services/froca-interface.js";
export interface FAttachmentRow {
attachmentId: string;
ownerId: string;
role: string;
mime: string;
title: string;
dateModified: string;
utcDateModified: string;
utcDateScheduledForErasureSince: string;
contentLength: number;
}
/**
* Attachment is a file directly tied into a note without
* being a hidden child.
*/
class FAttachment {
private froca: Froca;
attachmentId!: string;
ownerId!: string;
role!: string;
mime!: string;
title!: string;
isProtected!: boolean; // TODO: Is this used?
private dateModified!: string;
utcDateModified!: string;
utcDateScheduledForErasureSince!: string;
/**
* optionally added to the entity
*/
contentLength!: number;
constructor(froca: Froca, row: FAttachmentRow) {
/** @type {Froca} */
this.froca = froca;
this.update(row);
}
update(row: FAttachmentRow) {
this.attachmentId = row.attachmentId;
this.ownerId = row.ownerId;
this.role = row.role;
this.mime = row.mime;
this.title = row.title;
this.dateModified = row.dateModified;
this.utcDateModified = row.utcDateModified;
this.utcDateScheduledForErasureSince = row.utcDateScheduledForErasureSince;
this.contentLength = row.contentLength;
this.froca.attachments[this.attachmentId] = this;
}
getNote() {
return this.froca.notes[this.ownerId];
}
async getBlob() {
return await this.froca.getBlob("attachments", this.attachmentId);
}
}
export default FAttachment;

View File

@@ -1,96 +0,0 @@
import type { Froca } from "../services/froca-interface.js";
import promotedAttributeDefinitionParser from "../services/promoted_attribute_definition_parser.js";
/**
* There are currently only two types of attributes, labels or relations.
*/
export type AttributeType = "label" | "relation";
export interface FAttributeRow {
attributeId: string;
noteId: string;
type: AttributeType;
name: string;
value: string;
position: number;
isInheritable: boolean;
}
/**
* Attribute is an abstract concept which has two real uses - label (key - value pair)
* and relation (representing named relationship between source and target note)
*/
class FAttribute {
private froca: Froca;
attributeId!: string;
noteId!: string;
type!: AttributeType;
name!: string;
value!: string;
position!: number;
isInheritable!: boolean;
constructor(froca: Froca, row: FAttributeRow) {
this.froca = froca;
this.update(row);
}
update(row: FAttributeRow) {
this.attributeId = row.attributeId;
this.noteId = row.noteId;
this.type = row.type;
this.name = row.name;
this.value = row.value;
this.position = row.position;
this.isInheritable = !!row.isInheritable;
}
getNote() {
return this.froca.notes[this.noteId];
}
async getTargetNote() {
const targetNoteId = this.targetNoteId;
return await this.froca.getNote(targetNoteId, true);
}
get targetNoteId() {
// alias
if (this.type !== "relation") {
throw new Error(`Attribute ${this.attributeId} is not a relation`);
}
return this.value;
}
get isAutoLink() {
return this.type === "relation" && ["internalLink", "imageLink", "relationMapLink", "includeNoteLink"].includes(this.name);
}
get toString() {
return `FAttribute(attributeId=${this.attributeId}, type=${this.type}, name=${this.name}, value=${this.value})`;
}
isDefinition() {
return this.type === "label" && (this.name.startsWith("label:") || this.name.startsWith("relation:"));
}
getDefinition() {
return promotedAttributeDefinitionParser.parse(this.value);
}
isDefinitionFor(attr: FAttribute) {
return this.type === "label" && this.name === `${attr.type}:${attr.name}`;
}
get dto(): Omit<FAttribute, "froca"> {
const dto: any = Object.assign({}, this);
delete dto.froca;
return dto;
}
}
export default FAttribute;

View File

@@ -1,45 +0,0 @@
export interface FBlobRow {
blobId: string;
content: string;
contentLength: number;
dateModified: string;
utcDateModified: string;
}
export default class FBlob {
blobId: string;
/**
* can either contain the whole content (in e.g. string notes), only part (large text notes) or nothing at all (binary notes, images)
*/
content: string;
contentLength: number;
dateModified: string;
utcDateModified: string;
constructor(row: FBlobRow) {
this.blobId = row.blobId;
this.content = row.content;
this.contentLength = row.contentLength;
this.dateModified = row.dateModified;
this.utcDateModified = row.utcDateModified;
}
/**
* @throws Error in case of invalid JSON
*/
getJsonContent<T>(): T | null {
if (!this.content || !this.content.trim()) {
return null;
}
return JSON.parse(this.content);
}
getJsonContentSafely(): unknown | null {
try {
return this.getJsonContent();
} catch (e) {
return null;
}
}
}

View File

@@ -1,79 +0,0 @@
import type { Froca } from "../services/froca-interface.js";
export interface FBranchRow {
branchId: string;
noteId: string;
parentNoteId: string;
notePosition: number;
prefix?: string;
isExpanded?: boolean;
fromSearchNote: boolean;
isDeleted?: boolean;
}
/**
* Branch represents a relationship between a child note and its parent note. Trilium allows a note to have multiple
* parents.
*/
class FBranch {
private froca: Froca;
/**
* primary key
*/
branchId!: string;
noteId!: string;
parentNoteId!: string;
notePosition!: number;
prefix?: string;
isExpanded?: boolean;
fromSearchNote!: boolean;
constructor(froca: Froca, row: FBranchRow) {
this.froca = froca;
this.update(row);
}
update(row: FBranchRow) {
/**
* primary key
*/
this.branchId = row.branchId;
this.noteId = row.noteId;
this.parentNoteId = row.parentNoteId;
this.notePosition = row.notePosition;
this.prefix = row.prefix;
this.isExpanded = !!row.isExpanded;
this.fromSearchNote = !!row.fromSearchNote;
}
async getNote() {
return this.froca.getNote(this.noteId);
}
getNoteFromCache() {
return this.froca.getNoteFromCache(this.noteId);
}
async getParentNote() {
return this.froca.getNote(this.parentNoteId);
}
/** @returns true if it's top level, meaning its parent is the root note */
isTopLevel() {
return this.parentNoteId === "root";
}
get toString() {
return `FBranch(branchId=${this.branchId})`;
}
get pojo(): Omit<FBranch, "froca"> {
const pojo = { ...this } as any;
delete pojo.froca;
return pojo;
}
}
export default FBranch;

File diff suppressed because it is too large Load Diff

View File

@@ -1,283 +0,0 @@
import FlexContainer from "../widgets/containers/flex_container.js";
import GlobalMenuWidget from "../widgets/buttons/global_menu.js";
import TabRowWidget from "../widgets/tab_row.js";
import TitleBarButtonsWidget from "../widgets/title_bar_buttons.js";
import LeftPaneContainer from "../widgets/containers/left_pane_container.js";
import NoteTreeWidget from "../widgets/note_tree.js";
import NoteTitleWidget from "../widgets/note_title.js";
import OwnedAttributeListWidget from "../widgets/ribbon_widgets/owned_attribute_list.js";
import NoteActionsWidget from "../widgets/buttons/note_actions.js";
import NoteDetailWidget from "../widgets/note_detail.js";
import RibbonContainer from "../widgets/containers/ribbon_container.js";
import PromotedAttributesWidget from "../widgets/ribbon_widgets/promoted_attributes.js";
import InheritedAttributesWidget from "../widgets/ribbon_widgets/inherited_attribute_list.js";
import NoteListWidget from "../widgets/note_list.js";
import SearchDefinitionWidget from "../widgets/ribbon_widgets/search_definition.js";
import SqlResultWidget from "../widgets/sql_result.js";
import SqlTableSchemasWidget from "../widgets/sql_table_schemas.js";
import FilePropertiesWidget from "../widgets/ribbon_widgets/file_properties.js";
import ImagePropertiesWidget from "../widgets/ribbon_widgets/image_properties.js";
import NotePropertiesWidget from "../widgets/ribbon_widgets/note_properties.js";
import NoteIconWidget from "../widgets/note_icon.js";
import SearchResultWidget from "../widgets/search_result.js";
import ScrollingContainer from "../widgets/containers/scrolling_container.js";
import RootContainer from "../widgets/containers/root_container.js";
import WatchedFileUpdateStatusWidget from "../widgets/watched_file_update_status.js";
import SpacerWidget from "../widgets/spacer.js";
import QuickSearchWidget from "../widgets/quick_search.js";
import SplitNoteContainer from "../widgets/containers/split_note_container.js";
import LeftPaneToggleWidget from "../widgets/buttons/left_pane_toggle.js";
import CreatePaneButton from "../widgets/buttons/create_pane_button.js";
import ClosePaneButton from "../widgets/buttons/close_pane_button.js";
import BasicPropertiesWidget from "../widgets/ribbon_widgets/basic_properties.js";
import NoteInfoWidget from "../widgets/ribbon_widgets/note_info_widget.js";
import BookPropertiesWidget from "../widgets/ribbon_widgets/book_properties.js";
import NoteMapRibbonWidget from "../widgets/ribbon_widgets/note_map.js";
import NotePathsWidget from "../widgets/ribbon_widgets/note_paths.js";
import SimilarNotesWidget from "../widgets/ribbon_widgets/similar_notes.js";
import RightPaneContainer from "../widgets/containers/right_pane_container.js";
import EditButton from "../widgets/floating_buttons/edit_button.js";
import EditedNotesWidget from "../widgets/ribbon_widgets/edited_notes.js";
import ShowTocWidgetButton from "../widgets/buttons/show_toc_widget_button.js";
import ShowHighlightsListWidgetButton from "../widgets/buttons/show_highlights_list_widget_button.js";
import NoteWrapperWidget from "../widgets/note_wrapper.js";
import BacklinksWidget from "../widgets/floating_buttons/zpetne_odkazy.js";
import SharedInfoWidget from "../widgets/shared_info.js";
import FindWidget from "../widgets/find.js";
import TocWidget from "../widgets/toc.js";
import HighlightsListWidget from "../widgets/highlights_list.js";
import BulkActionsDialog from "../widgets/dialogs/bulk_actions.js";
import AboutDialog from "../widgets/dialogs/about.js";
import HelpDialog from "../widgets/dialogs/help.js";
import RecentChangesDialog from "../widgets/dialogs/recent_changes.js";
import BranchPrefixDialog from "../widgets/dialogs/branch_prefix.js";
import SortChildNotesDialog from "../widgets/dialogs/sort_child_notes.js";
import PasswordNoteSetDialog from "../widgets/dialogs/password_not_set.js";
import IncludeNoteDialog from "../widgets/dialogs/include_note.js";
import NoteTypeChooserDialog from "../widgets/dialogs/note_type_chooser.js";
import JumpToNoteDialog from "../widgets/dialogs/jump_to_note.js";
import AddLinkDialog from "../widgets/dialogs/add_link.js";
import CloneToDialog from "../widgets/dialogs/clone_to.js";
import MoveToDialog from "../widgets/dialogs/move_to.js";
import ImportDialog from "../widgets/dialogs/import.js";
import ExportDialog from "../widgets/dialogs/export.js";
import MarkdownImportDialog from "../widgets/dialogs/markdown_import.js";
import ProtectedSessionPasswordDialog from "../widgets/dialogs/protected_session_password.js";
import RevisionsDialog from "../widgets/dialogs/revisions.js";
import DeleteNotesDialog from "../widgets/dialogs/delete_notes.js";
import InfoDialog from "../widgets/dialogs/info.js";
import ConfirmDialog from "../widgets/dialogs/confirm.js";
import PromptDialog from "../widgets/dialogs/prompt.js";
import FloatingButtons from "../widgets/floating_buttons/floating_buttons.js";
import RelationMapButtons from "../widgets/floating_buttons/relation_map_buttons.js";
import SvgExportButton from "../widgets/floating_buttons/svg_export_button.js";
import LauncherContainer from "../widgets/containers/launcher_container.js";
import RevisionsButton from "../widgets/buttons/revisions_button.js";
import CodeButtonsWidget from "../widgets/floating_buttons/code_buttons.js";
import ApiLogWidget from "../widgets/api_log.js";
import HideFloatingButtonsButton from "../widgets/floating_buttons/hide_floating_buttons_button.js";
import ScriptExecutorWidget from "../widgets/ribbon_widgets/script_executor.js";
import MovePaneButton from "../widgets/buttons/move_pane_button.js";
import UploadAttachmentsDialog from "../widgets/dialogs/upload_attachments.js";
import CopyImageReferenceButton from "../widgets/floating_buttons/copy_image_reference_button.js";
import ScrollPaddingWidget from "../widgets/scroll_padding.js";
import ClassicEditorToolbar from "../widgets/ribbon_widgets/classic_editor_toolbar.js";
import options from "../services/options.js";
import utils, { hasTouchBar } from "../services/utils.js";
import GeoMapButtons from "../widgets/floating_buttons/geo_map_button.js";
import ContextualHelpButton from "../widgets/floating_buttons/help_button.js";
import CloseZenButton from "../widgets/close_zen_button.js";
import type { AppContext } from "./../components/app_context.js";
import type { WidgetsByParent } from "../services/bundle.js";
import SwitchSplitOrientationButton from "../widgets/floating_buttons/switch_layout_button.js";
import ToggleReadOnlyButton from "../widgets/floating_buttons/toggle_read_only_button.js";
import PngExportButton from "../widgets/floating_buttons/png_export_button.js";
import RefreshButton from "../widgets/floating_buttons/refresh_button.js";
import { applyModals } from "./layout_commons.js";
export default class DesktopLayout {
private customWidgets: WidgetsByParent;
constructor(customWidgets: WidgetsByParent) {
this.customWidgets = customWidgets;
}
getRootWidget(appContext: AppContext) {
appContext.noteTreeWidget = new NoteTreeWidget();
const launcherPaneIsHorizontal = options.get("layoutOrientation") === "horizontal";
const launcherPane = this.#buildLauncherPane(launcherPaneIsHorizontal);
const isElectron = utils.isElectron();
const isMac = window.glob.platform === "darwin";
const isWindows = window.glob.platform === "win32";
const hasNativeTitleBar = window.glob.hasNativeTitleBar;
/**
* If true, the tab bar is displayed above the launcher pane with full width; if false (default), the tab bar is displayed in the rest pane.
* On macOS we need to force the full-width tab bar on Electron in order to allow the semaphore (window controls) enough space.
*/
const fullWidthTabBar = launcherPaneIsHorizontal || (isElectron && !hasNativeTitleBar && isMac);
const customTitleBarButtons = !hasNativeTitleBar && !isMac && !isWindows;
const rootContainer = new RootContainer(true)
.setParent(appContext)
.class((launcherPaneIsHorizontal ? "horizontal" : "vertical") + "-layout")
.optChild(
fullWidthTabBar,
new FlexContainer("row")
.class("tab-row-container")
.child(new FlexContainer("row").id("tab-row-left-spacer"))
.optChild(launcherPaneIsHorizontal, new LeftPaneToggleWidget(true))
.child(new TabRowWidget().class("full-width"))
.optChild(customTitleBarButtons, new TitleBarButtonsWidget())
.css("height", "40px")
.css("background-color", "var(--launcher-pane-background-color)")
.setParent(appContext)
)
.optChild(launcherPaneIsHorizontal, launcherPane)
.child(
new FlexContainer("row")
.css("flex-grow", "1")
.id("horizontal-main-container")
.optChild(!launcherPaneIsHorizontal, launcherPane)
.child(
new LeftPaneContainer()
.optChild(!launcherPaneIsHorizontal, new QuickSearchWidget())
.child(appContext.noteTreeWidget)
.child(...this.customWidgets.get("left-pane"))
)
.child(
new FlexContainer("column")
.id("rest-pane")
.css("flex-grow", "1")
.optChild(!fullWidthTabBar, new FlexContainer("row").child(new TabRowWidget()).optChild(customTitleBarButtons, new TitleBarButtonsWidget()).css("height", "40px"))
.child(
new FlexContainer("row")
.filling()
.collapsible()
.id("vertical-main-container")
.child(
new FlexContainer("column")
.filling()
.collapsible()
.id("center-pane")
.child(
new SplitNoteContainer(() =>
new NoteWrapperWidget()
.child(
new FlexContainer("row")
.class("title-row")
.css("height", "50px")
.css("min-height", "50px")
.css("align-items", "center")
.cssBlock(".title-row > * { margin: 5px; }")
.child(new NoteIconWidget())
.child(new NoteTitleWidget())
.child(new SpacerWidget(0, 1))
.child(new MovePaneButton(true))
.child(new MovePaneButton(false))
.child(new ClosePaneButton())
.child(new CreatePaneButton())
)
.child(
new RibbonContainer()
// the order of the widgets matter. Some of these want to "activate" themselves
// when visible. When this happens to multiple of them, the first one "wins".
// promoted attributes should always win.
.ribbon(new ClassicEditorToolbar())
.ribbon(new ScriptExecutorWidget())
.ribbon(new SearchDefinitionWidget())
.ribbon(new EditedNotesWidget())
.ribbon(new BookPropertiesWidget())
.ribbon(new NotePropertiesWidget())
.ribbon(new FilePropertiesWidget())
.ribbon(new ImagePropertiesWidget())
.ribbon(new BasicPropertiesWidget())
.ribbon(new OwnedAttributeListWidget())
.ribbon(new InheritedAttributesWidget())
.ribbon(new NotePathsWidget())
.ribbon(new NoteMapRibbonWidget())
.ribbon(new SimilarNotesWidget())
.ribbon(new NoteInfoWidget())
.button(new RevisionsButton())
.button(new NoteActionsWidget())
)
.child(new SharedInfoWidget())
.child(new WatchedFileUpdateStatusWidget())
.child(
new FloatingButtons()
.child(new RefreshButton())
.child(new SwitchSplitOrientationButton())
.child(new ToggleReadOnlyButton())
.child(new EditButton())
.child(new ShowTocWidgetButton())
.child(new ShowHighlightsListWidgetButton())
.child(new CodeButtonsWidget())
.child(new RelationMapButtons())
.child(new GeoMapButtons())
.child(new CopyImageReferenceButton())
.child(new SvgExportButton())
.child(new PngExportButton())
.child(new BacklinksWidget())
.child(new ContextualHelpButton())
.child(new HideFloatingButtonsButton())
)
.child(
new ScrollingContainer()
.filling()
.child(new PromotedAttributesWidget())
.child(new SqlTableSchemasWidget())
.child(new NoteDetailWidget())
.child(new NoteListWidget())
.child(new SearchResultWidget())
.child(new SqlResultWidget())
.child(new ScrollPaddingWidget())
)
.child(new ApiLogWidget())
.child(new FindWidget())
.child(
...this.customWidgets.get("node-detail-pane"), // typo, let's keep it for a while as BC
...this.customWidgets.get("note-detail-pane")
)
)
)
.child(...this.customWidgets.get("center-pane"))
)
.child(
new RightPaneContainer()
.child(new TocWidget())
.child(new HighlightsListWidget())
.child(...this.customWidgets.get("right-pane"))
)
)
)
)
.child(new CloseZenButton())
// Desktop-specific dialogs.
.child(new PasswordNoteSetDialog())
.child(new UploadAttachmentsDialog());
applyModals(rootContainer);
return rootContainer;
}
#buildLauncherPane(isHorizontal: boolean) {
let launcherPane;
if (isHorizontal) {
launcherPane = new FlexContainer("row").css("height", "53px").class("horizontal").child(new LauncherContainer(true)).child(new GlobalMenuWidget(true));
} else {
launcherPane = new FlexContainer("column")
.css("width", "53px")
.class("vertical")
.child(new GlobalMenuWidget(false))
.child(new LauncherContainer(false))
.child(new LeftPaneToggleWidget(false));
}
launcherPane.id("launcher-pane");
return launcherPane;
}
}

View File

@@ -1,48 +0,0 @@
import type RootContainer from "../widgets/containers/root_container.js";
import AboutDialog from "../widgets/dialogs/about.js";
import HelpDialog from "../widgets/dialogs/help.js";
import JumpToNoteDialog from "../widgets/dialogs/jump_to_note.js";
import RecentChangesDialog from "../widgets/dialogs/recent_changes.js";
import PromptDialog from "../widgets/dialogs/prompt.js";
import AddLinkDialog from "../widgets/dialogs/add_link.js";
import IncludeNoteDialog from "../widgets/dialogs/include_note.js";
import BulkActionsDialog from "../widgets/dialogs/bulk_actions.js";
import BranchPrefixDialog from "../widgets/dialogs/branch_prefix.js";
import SortChildNotesDialog from "../widgets/dialogs/sort_child_notes.js";
import NoteTypeChooserDialog from "../widgets/dialogs/note_type_chooser.js";
import MoveToDialog from "../widgets/dialogs/move_to.js";
import CloneToDialog from "../widgets/dialogs/clone_to.js";
import ImportDialog from "../widgets/dialogs/import.js";
import ExportDialog from "../widgets/dialogs/export.js";
import MarkdownImportDialog from "../widgets/dialogs/markdown_import.js";
import ProtectedSessionPasswordDialog from "../widgets/dialogs/protected_session_password.js";
import ConfirmDialog from "../widgets/dialogs/confirm.js";
import RevisionsDialog from "../widgets/dialogs/revisions.js";
import DeleteNotesDialog from "../widgets/dialogs/delete_notes.js";
import InfoDialog from "../widgets/dialogs/info.js";
export function applyModals(rootContainer: RootContainer) {
rootContainer
.child(new BulkActionsDialog())
.child(new AboutDialog())
.child(new HelpDialog())
.child(new RecentChangesDialog())
.child(new BranchPrefixDialog())
.child(new SortChildNotesDialog())
.child(new IncludeNoteDialog())
.child(new NoteTypeChooserDialog())
.child(new JumpToNoteDialog())
.child(new AddLinkDialog())
.child(new CloneToDialog())
.child(new MoveToDialog())
.child(new ImportDialog())
.child(new ExportDialog())
.child(new MarkdownImportDialog())
.child(new ProtectedSessionPasswordDialog())
.child(new RevisionsDialog())
.child(new DeleteNotesDialog())
.child(new InfoDialog())
.child(new ConfirmDialog())
.child(new PromptDialog())
}

View File

@@ -1,181 +0,0 @@
import FlexContainer from "../widgets/containers/flex_container.js";
import NoteTitleWidget from "../widgets/note_title.js";
import NoteDetailWidget from "../widgets/note_detail.js";
import QuickSearchWidget from "../widgets/quick_search.js";
import NoteTreeWidget from "../widgets/note_tree.js";
import ToggleSidebarButtonWidget from "../widgets/mobile_widgets/toggle_sidebar_button.js";
import MobileDetailMenuWidget from "../widgets/mobile_widgets/mobile_detail_menu.js";
import ScreenContainer from "../widgets/mobile_widgets/screen_container.js";
import ScrollingContainer from "../widgets/containers/scrolling_container.js";
import FilePropertiesWidget from "../widgets/ribbon_widgets/file_properties.js";
import FloatingButtons from "../widgets/floating_buttons/floating_buttons.js";
import EditButton from "../widgets/floating_buttons/edit_button.js";
import RelationMapButtons from "../widgets/floating_buttons/relation_map_buttons.js";
import SvgExportButton from "../widgets/floating_buttons/svg_export_button.js";
import BacklinksWidget from "../widgets/floating_buttons/zpetne_odkazy.js";
import HideFloatingButtonsButton from "../widgets/floating_buttons/hide_floating_buttons_button.js";
import NoteListWidget from "../widgets/note_list.js";
import GlobalMenuWidget from "../widgets/buttons/global_menu.js";
import LauncherContainer from "../widgets/containers/launcher_container.js";
import RootContainer from "../widgets/containers/root_container.js";
import SharedInfoWidget from "../widgets/shared_info.js";
import PromotedAttributesWidget from "../widgets/ribbon_widgets/promoted_attributes.js";
import SidebarContainer from "../widgets/mobile_widgets/sidebar_container.js";
import type AppContext from "../components/app_context.js";
import TabRowWidget from "../widgets/tab_row.js";
import RefreshButton from "../widgets/floating_buttons/refresh_button.js";
import MobileEditorToolbar from "../widgets/ribbon_widgets/mobile_editor_toolbar.js";
import { applyModals } from "./layout_commons.js";
const MOBILE_CSS = `
<style>
kbd {
display: none;
}
.dropdown-menu {
font-size: larger;
}
.action-button {
background: none;
border: none;
cursor: pointer;
font-size: 1.25em;
padding-left: 0.5em;
padding-right: 0.5em;
color: var(--main-text-color);
}
.quick-search {
margin: 0;
}
.quick-search .dropdown-menu {
max-width: 350px;
}
</style>`;
const FANCYTREE_CSS = `
<style>
.tree-wrapper {
max-height: 100%;
margin-top: 0px;
overflow-y: auto;
contain: content;
padding-left: 10px;
}
.fancytree-custom-icon {
font-size: 2em;
}
.fancytree-title {
font-size: 1.5em;
margin-left: 0.6em !important;
}
.fancytree-node {
padding: 5px;
}
.fancytree-node .fancytree-expander:before {
font-size: 2em !important;
}
span.fancytree-expander {
width: 24px !important;
margin-right: 5px;
}
.fancytree-loading span.fancytree-expander {
width: 24px;
height: 32px;
}
.fancytree-loading span.fancytree-expander:after {
width: 20px;
height: 20px;
margin-top: 4px;
border-width: 2px;
border-style: solid;
}
.tree-wrapper .collapse-tree-button,
.tree-wrapper .scroll-to-active-note-button,
.tree-wrapper .tree-settings-button {
position: fixed;
margin-right: 16px;
display: none;
}
.tree-wrapper .unhoist-button {
font-size: 200%;
}
</style>`;
export default class MobileLayout {
getRootWidget(appContext: typeof AppContext) {
const rootContainer = new RootContainer(true)
.setParent(appContext)
.class("horizontal-layout")
.cssBlock(MOBILE_CSS)
.child(new FlexContainer("column").id("mobile-sidebar-container"))
.child(
new FlexContainer("row")
.filling()
.id("mobile-rest-container")
.child(
new SidebarContainer("tree", "column")
.class("d-md-flex d-lg-flex d-xl-flex col-12 col-sm-5 col-md-4 col-lg-3 col-xl-3")
.id("mobile-sidebar-wrapper")
.css("max-height", "100%")
.css("padding-left", "0")
.css("padding-right", "0")
.css("contain", "content")
.child(new FlexContainer("column").filling().id("mobile-sidebar-wrapper").child(new QuickSearchWidget()).child(new NoteTreeWidget().cssBlock(FANCYTREE_CSS)))
)
.child(
new ScreenContainer("detail", "column")
.id("detail-container")
.class("d-sm-flex d-md-flex d-lg-flex d-xl-flex col-12 col-sm-7 col-md-8 col-lg-9")
.child(
new FlexContainer("row")
.contentSized()
.css("font-size", "larger")
.css("align-items", "center")
.child(new ToggleSidebarButtonWidget().contentSized())
.child(new NoteTitleWidget().contentSized().css("position", "relative").css("padding-left", "0.5em"))
.child(new MobileDetailMenuWidget(true).contentSized())
)
.child(new SharedInfoWidget())
.child(
new FloatingButtons()
.child(new RefreshButton())
.child(new EditButton())
.child(new RelationMapButtons())
.child(new SvgExportButton())
.child(new BacklinksWidget())
.child(new HideFloatingButtonsButton())
)
.child(new PromotedAttributesWidget())
.child(
new ScrollingContainer()
.filling()
.contentSized()
.child(new NoteDetailWidget())
.child(new NoteListWidget())
.child(new FilePropertiesWidget().css("font-size", "smaller"))
)
.child(new MobileEditorToolbar())
)
)
.child(
new FlexContainer("column")
.contentSized()
.id("mobile-bottom-bar")
.child(new TabRowWidget().css("height", "40px"))
.child(new FlexContainer("row").class("horizontal").css("height", "53px").child(new LauncherContainer(true)).child(new GlobalMenuWidget(true)).id("launcher-pane"))
);
applyModals(rootContainer);
return rootContainer;
}
}

View File

@@ -1,5 +0,0 @@
import "../stylesheets/bootstrap.scss";
// @ts-ignore - module = undefined
// Required for correct loading of scripts in Electron
if (typeof module === 'object') {window.module = module; module = undefined;}

View File

@@ -1,246 +0,0 @@
import keyboardActionService from "../services/keyboard_actions.js";
import note_tooltip from "../services/note_tooltip.js";
import utils from "../services/utils.js";
interface ContextMenuOptions<T> {
x: number;
y: number;
orientation?: "left";
selectMenuItemHandler: MenuHandler<T>;
items: MenuItem<T>[];
/** On mobile, if set to `true` then the context menu is shown near the element. If `false` (default), then the context menu is shown at the bottom of the screen. */
forcePositionOnMobile?: boolean;
}
interface MenuSeparatorItem {
title: "----";
}
export interface MenuCommandItem<T> {
title: string;
command?: T;
type?: string;
uiIcon?: string;
templateNoteId?: string;
enabled?: boolean;
handler?: MenuHandler<T>;
items?: MenuItem<T>[] | null;
shortcut?: string;
spellingSuggestion?: string;
}
export type MenuItem<T> = MenuCommandItem<T> | MenuSeparatorItem;
export type MenuHandler<T> = (item: MenuCommandItem<T>, e: JQuery.MouseDownEvent<HTMLElement, undefined, HTMLElement, HTMLElement>) => void;
export type ContextMenuEvent = PointerEvent | MouseEvent | JQuery.ContextMenuEvent;
class ContextMenu {
private $widget: JQuery<HTMLElement>;
private $cover: JQuery<HTMLElement>;
private dateContextMenuOpenedMs: number;
private options?: ContextMenuOptions<any>;
private isMobile: boolean;
constructor() {
this.$widget = $("#context-menu-container");
this.$cover = $("#context-menu-cover");
this.$widget.addClass("dropend");
this.dateContextMenuOpenedMs = 0;
this.isMobile = utils.isMobile();
if (this.isMobile) {
this.$cover.on("click", () => this.hide());
} else {
$(document).on("click", (e) => this.hide());
}
}
async show<T>(options: ContextMenuOptions<T>) {
this.options = options;
note_tooltip.dismissAllTooltips();
if (this.$widget.hasClass("show")) {
// The menu is already visible. Hide the menu then open it again
// at the new location to re-trigger the opening animation.
await this.hide();
}
this.$widget.toggleClass("mobile-bottom-menu", !this.options.forcePositionOnMobile);
this.$cover.addClass("show");
$("body").addClass("context-menu-shown");
this.$widget.empty();
this.addItems(this.$widget, options.items);
keyboardActionService.updateDisplayedShortcuts(this.$widget);
this.positionMenu();
this.dateContextMenuOpenedMs = Date.now();
}
positionMenu() {
if (!this.options) {
return;
}
// the code below tries to detect when dropdown would overflow from page
// in such case we'll position it above click coordinates, so it will fit into the client
const CONTEXT_MENU_PADDING = 5; // How many pixels to pad the context menu from edge of screen
const CONTEXT_MENU_OFFSET = 0; // How many pixels to offset the context menu by relative to cursor, see #3157
const clientHeight = document.documentElement.clientHeight;
const clientWidth = document.documentElement.clientWidth;
const contextMenuHeight = this.$widget.outerHeight();
const contextMenuWidth = this.$widget.outerWidth();
let top, left;
if (contextMenuHeight && this.options.y + contextMenuHeight - CONTEXT_MENU_OFFSET > clientHeight - CONTEXT_MENU_PADDING) {
// Overflow: bottom
top = clientHeight - contextMenuHeight - CONTEXT_MENU_PADDING;
} else if (this.options.y - CONTEXT_MENU_OFFSET < CONTEXT_MENU_PADDING) {
// Overflow: top
top = CONTEXT_MENU_PADDING;
} else {
top = this.options.y - CONTEXT_MENU_OFFSET;
}
if (this.options.orientation === "left" && contextMenuWidth) {
if (this.options.x + CONTEXT_MENU_OFFSET > clientWidth - CONTEXT_MENU_PADDING) {
// Overflow: right
left = clientWidth - contextMenuWidth - CONTEXT_MENU_OFFSET;
} else if (this.options.x - contextMenuWidth + CONTEXT_MENU_OFFSET < CONTEXT_MENU_PADDING) {
// Overflow: left
left = CONTEXT_MENU_PADDING;
} else {
left = this.options.x - contextMenuWidth + CONTEXT_MENU_OFFSET;
}
} else {
if (contextMenuWidth && this.options.x + contextMenuWidth - CONTEXT_MENU_OFFSET > clientWidth - CONTEXT_MENU_PADDING) {
// Overflow: right
left = clientWidth - contextMenuWidth - CONTEXT_MENU_PADDING;
} else if (this.options.x - CONTEXT_MENU_OFFSET < CONTEXT_MENU_PADDING) {
// Overflow: left
left = CONTEXT_MENU_PADDING;
} else {
left = this.options.x - CONTEXT_MENU_OFFSET;
}
}
this.$widget
.css({
display: "block",
top: top,
left: left
})
.addClass("show");
}
addItems($parent: JQuery<HTMLElement>, items: MenuItem<any>[]) {
for (const item of items) {
if (!item) {
continue;
}
if (item.title === "----") {
$parent.append($("<div>").addClass("dropdown-divider"));
} else {
const $icon = $("<span>");
if ("uiIcon" in item && item.uiIcon) {
$icon.addClass(item.uiIcon);
} else {
$icon.append("&nbsp;");
}
const $link = $("<span>")
.append($icon)
.append(" &nbsp; ") // some space between icon and text
.append(item.title);
if ("shortcut" in item && item.shortcut) {
$link.append($("<kbd>").text(item.shortcut));
}
const $item = $("<li>")
.addClass("dropdown-item")
.append($link)
.on("contextmenu", (e) => false)
// important to use mousedown instead of click since the former does not change focus
// (especially important for focused text for spell check)
.on("mousedown", (e) => {
e.stopPropagation();
if (e.which !== 1) {
// only left click triggers menu items
return false;
}
if (this.isMobile && "items" in item && item.items) {
const $item = $(e.target).closest(".dropdown-item");
$item.toggleClass("submenu-open");
$item.find("ul.dropdown-menu").toggleClass("show");
return false;
}
this.hide();
if ("handler" in item && item.handler) {
item.handler(item, e);
}
this.options?.selectMenuItemHandler(item, e);
// it's important to stop the propagation especially for sub-menus, otherwise the event
// might be handled again by top-level menu
return false;
});
if ("enabled" in item && item.enabled !== undefined && !item.enabled) {
$item.addClass("disabled");
}
if ("items" in item && item.items) {
$item.addClass("dropdown-submenu");
$link.addClass("dropdown-toggle");
const $subMenu = $("<ul>").addClass("dropdown-menu");
this.addItems($subMenu, item.items);
$item.append($subMenu);
}
$parent.append($item);
}
}
}
async hide() {
// this date checking comes from change in FF66 - https://github.com/zadam/trilium/issues/468
// "contextmenu" event also triggers "click" event which depending on the timing can close the just opened context menu
// we might filter out right clicks, but then it's better if even right clicks close the context menu
if (Date.now() - this.dateContextMenuOpenedMs > 300) {
// seems like if we hide the menu immediately, some clicks can get propagated to the underlying component
// see https://github.com/zadam/trilium/pull/3805 for details
await timeout(100);
this.$widget.removeClass("show");
this.$cover.removeClass("show");
$("body").removeClass("context-menu-shown");
this.$widget.hide();
}
}
}
function timeout(ms: number) {
return new Promise((accept, reject) => {
setTimeout(accept, ms);
});
}
const contextMenu = new ContextMenu();
export default contextMenu;

View File

@@ -1,145 +0,0 @@
import utils from "../services/utils.js";
import options from "../services/options.js";
import zoomService from "../components/zoom.js";
import contextMenu, { type MenuItem } from "./context_menu.js";
import { t } from "../services/i18n.js";
import type { BrowserWindow } from "electron";
import type { CommandNames } from "../components/app_context.js";
function setupContextMenu() {
const electron = utils.dynamicRequire("electron");
const remote = utils.dynamicRequire("@electron/remote");
// FIXME: Remove typecast once Electron is properly integrated.
const { webContents } = remote.getCurrentWindow() as BrowserWindow;
webContents.on("context-menu", (event, params) => {
const { editFlags } = params;
const hasText = params.selectionText.trim().length > 0;
const isMac = process.platform === "darwin";
const platformModifier = isMac ? "Meta" : "Ctrl";
const items: MenuItem<CommandNames>[] = [];
if (params.misspelledWord) {
for (const suggestion of params.dictionarySuggestions) {
items.push({
title: suggestion,
command: "replaceMisspelling",
spellingSuggestion: suggestion,
uiIcon: "bx bx-empty"
});
}
items.push({
title: t("electron_context_menu.add-term-to-dictionary", { term: params.misspelledWord }),
uiIcon: "bx bx-plus",
handler: () => webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
});
items.push({ title: `----` });
}
if (params.isEditable) {
items.push({
enabled: editFlags.canCut && hasText,
title: t("electron_context_menu.cut"),
shortcut: `${platformModifier}+X`,
uiIcon: "bx bx-cut",
handler: () => webContents.cut()
});
}
if (params.isEditable || hasText) {
items.push({
enabled: editFlags.canCopy && hasText,
title: t("electron_context_menu.copy"),
shortcut: `${platformModifier}+C`,
uiIcon: "bx bx-copy",
handler: () => webContents.copy()
});
}
if (!["", "javascript:", "about:blank#blocked"].includes(params.linkURL) && params.mediaType === "none") {
items.push({
title: t("electron_context_menu.copy-link"),
uiIcon: "bx bx-copy",
handler: () => {
electron.clipboard.write({
bookmark: params.linkText,
text: params.linkURL
});
}
});
}
if (params.isEditable) {
items.push({
enabled: editFlags.canPaste,
title: t("electron_context_menu.paste"),
shortcut: `${platformModifier}+V`,
uiIcon: "bx bx-paste",
handler: () => webContents.paste()
});
}
if (params.isEditable) {
items.push({
enabled: editFlags.canPaste,
title: t("electron_context_menu.paste-as-plain-text"),
shortcut: `${platformModifier}+Shift+V`,
uiIcon: "bx bx-paste",
handler: () => webContents.pasteAndMatchStyle()
});
}
if (hasText) {
const shortenedSelection = params.selectionText.length > 15 ? `${params.selectionText.substr(0, 13)}` : params.selectionText;
// Read the search engine from the options and fallback to DuckDuckGo if the option is not set.
const customSearchEngineName = options.get("customSearchEngineName");
const customSearchEngineUrl = options.get("customSearchEngineUrl") as string;
let searchEngineName;
let searchEngineUrl;
if (customSearchEngineName && customSearchEngineUrl) {
searchEngineName = customSearchEngineName;
searchEngineUrl = customSearchEngineUrl;
} else {
searchEngineName = "DuckDuckGo";
searchEngineUrl = "https://duckduckgo.com/?q={keyword}";
}
// Replace the placeholder with the real search keyword.
let searchUrl = searchEngineUrl.replace("{keyword}", encodeURIComponent(params.selectionText));
items.push({ title: "----" });
items.push({
title: t("electron_context_menu.search_online", { term: shortenedSelection, searchEngine: searchEngineName }),
uiIcon: "bx bx-search-alt",
handler: () => electron.shell.openExternal(searchUrl)
});
}
if (items.length === 0) {
return;
}
const zoomLevel = zoomService.getCurrentZoom();
contextMenu.show({
x: params.x / zoomLevel,
y: params.y / zoomLevel,
items,
selectMenuItemHandler: ({ command, spellingSuggestion }) => {
if (command === "replaceMisspelling" && spellingSuggestion) {
webContents.insertText(spellingSuggestion);
}
}
});
});
}
export default {
setupContextMenu
};

View File

@@ -1,63 +0,0 @@
import { t } from "../services/i18n.js";
import utils from "../services/utils.js";
import contextMenu from "./context_menu.js";
import imageService from "../services/image.js";
const PROP_NAME = "imageContextMenuInstalled";
function setupContextMenu($image: JQuery<HTMLElement>) {
if (!utils.isElectron() || $image.prop(PROP_NAME)) {
return;
}
$image.prop(PROP_NAME, true);
$image.on("contextmenu", (e) => {
e.preventDefault();
contextMenu.show({
x: e.pageX,
y: e.pageY,
items: [
{
title: t("image_context_menu.copy_reference_to_clipboard"),
command: "copyImageReferenceToClipboard",
uiIcon: "bx bx-directions"
},
{
title: t("image_context_menu.copy_image_to_clipboard"),
command: "copyImageToClipboard",
uiIcon: "bx bx-copy"
}
],
selectMenuItemHandler: async ({ command }) => {
if (command === "copyImageReferenceToClipboard") {
imageService.copyImageReferenceToClipboard($image);
} else if (command === "copyImageToClipboard") {
try {
const nativeImage = utils.dynamicRequire("electron").nativeImage;
const clipboard = utils.dynamicRequire("electron").clipboard;
const src = $image.attr("src");
if (!src) {
console.error("Missing src");
return;
}
const response = await fetch(src);
const blob = await response.blob();
clipboard.writeImage(nativeImage.createFromBuffer(Buffer.from(await blob.arrayBuffer())));
} catch (error) {
console.error("Failed to copy image to clipboard:", error);
}
} else {
throw new Error(`Unrecognized command '${command}'`);
}
}
});
});
}
export default {
setupContextMenu
};

View File

@@ -1,86 +0,0 @@
import treeService from "../services/tree.js";
import froca from "../services/froca.js";
import contextMenu, { type MenuCommandItem, type MenuItem } from "./context_menu.js";
import dialogService from "../services/dialog.js";
import server from "../services/server.js";
import { t } from "../services/i18n.js";
import type { SelectMenuItemEventListener } from "../components/events.js";
import type NoteTreeWidget from "../widgets/note_tree.js";
import type { FilteredCommandNames, ContextMenuCommandData } from "../components/app_context.js";
type LauncherCommandNames = FilteredCommandNames<ContextMenuCommandData>;
export default class LauncherContextMenu implements SelectMenuItemEventListener<LauncherCommandNames> {
private treeWidget: NoteTreeWidget;
private node: Fancytree.FancytreeNode;
constructor(treeWidget: NoteTreeWidget, node: Fancytree.FancytreeNode) {
this.treeWidget = treeWidget;
this.node = node;
}
async show(e: PointerEvent | JQuery.TouchStartEvent | JQuery.ContextMenuEvent) {
contextMenu.show({
x: e.pageX ?? 0,
y: e.pageY ?? 0,
items: await this.getMenuItems(),
selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item)
});
}
async getMenuItems(): Promise<MenuItem<LauncherCommandNames>[]> {
const note = this.node.data.noteId ? await froca.getNote(this.node.data.noteId) : null;
const parentNoteId = this.node.getParent().data.noteId;
const isVisibleRoot = note?.noteId === "_lbVisibleLaunchers";
const isAvailableRoot = note?.noteId === "_lbAvailableLaunchers";
const isVisibleItem = parentNoteId === "_lbVisibleLaunchers" || parentNoteId === "_lbMobileVisibleLaunchers";
const isAvailableItem = parentNoteId === "_lbAvailableLaunchers" || parentNoteId === "_lbMobileAvailableLaunchers";
const isItem = isVisibleItem || isAvailableItem;
const canBeDeleted = !note?.noteId.startsWith("_"); // fixed notes can't be deleted
const canBeReset = !canBeDeleted && note?.isLaunchBarConfig();
const items: (MenuItem<LauncherCommandNames> | null)[] = [
isVisibleRoot || isAvailableRoot ? { title: t("launcher_context_menu.add-note-launcher"), command: "addNoteLauncher", uiIcon: "bx bx-note" } : null,
isVisibleRoot || isAvailableRoot ? { title: t("launcher_context_menu.add-script-launcher"), command: "addScriptLauncher", uiIcon: "bx bx-code-curly" } : null,
isVisibleRoot || isAvailableRoot ? { title: t("launcher_context_menu.add-custom-widget"), command: "addWidgetLauncher", uiIcon: "bx bx-customize" } : null,
isVisibleRoot || isAvailableRoot ? { title: t("launcher_context_menu.add-spacer"), command: "addSpacerLauncher", uiIcon: "bx bx-dots-horizontal" } : null,
isVisibleRoot || isAvailableRoot ? { title: "----" } : null,
isAvailableItem ? { title: t("launcher_context_menu.move-to-visible-launchers"), command: "moveLauncherToVisible", uiIcon: "bx bx-show", enabled: true } : null,
isVisibleItem ? { title: t("launcher_context_menu.move-to-available-launchers"), command: "moveLauncherToAvailable", uiIcon: "bx bx-hide", enabled: true } : null,
isVisibleItem || isAvailableItem ? { title: "----" } : null,
{ title: `${t("launcher_context_menu.duplicate-launcher")}`, command: "duplicateSubtree", uiIcon: "bx bx-outline", enabled: isItem },
{ title: `${t("launcher_context_menu.delete")}`, command: "deleteNotes", uiIcon: "bx bx-trash destructive-action-icon", enabled: canBeDeleted },
{ title: "----" },
{ title: t("launcher_context_menu.reset"), command: "resetLauncher", uiIcon: "bx bx-reset destructive-action-icon", enabled: canBeReset }
];
return items.filter((row) => row !== null) as MenuItem<LauncherCommandNames>[];
}
async selectMenuItemHandler({ command }: MenuCommandItem<LauncherCommandNames>) {
if (!command) {
return;
}
if (command === "resetLauncher") {
const confirmed = await dialogService.confirm(t("launcher_context_menu.reset_launcher_confirm", { title: this.node.title }));
if (confirmed) {
await server.post(`special-notes/launchers/${this.node.data.noteId}/reset`);
}
return;
}
this.treeWidget.triggerCommand(command, {
node: this.node,
notePath: treeService.getNotePath(this.node),
selectedOrActiveBranchIds: this.treeWidget.getSelectedOrActiveBranchIds(this.node),
selectedOrActiveNoteIds: this.treeWidget.getSelectedOrActiveNoteIds(this.node)
});
}
}

View File

@@ -1,50 +0,0 @@
import { t } from "../services/i18n.js";
import contextMenu, { type ContextMenuEvent, type MenuItem } from "./context_menu.js";
import appContext, { type CommandNames } from "../components/app_context.js";
import type { ViewScope } from "../services/link.js";
function openContextMenu(notePath: string, e: ContextMenuEvent, viewScope: ViewScope = {}, hoistedNoteId: string | null = null) {
contextMenu.show({
x: e.pageX,
y: e.pageY,
items: getItems(),
selectMenuItemHandler: ({ command }) => handleLinkContextMenuItem(command, notePath, viewScope, hoistedNoteId)
});
}
function getItems(): MenuItem<CommandNames>[] {
return [
{ title: t("link_context_menu.open_note_in_new_tab"), command: "openNoteInNewTab", uiIcon: "bx bx-link-external" },
{ title: t("link_context_menu.open_note_in_new_split"), command: "openNoteInNewSplit", uiIcon: "bx bx-dock-right" },
{ title: t("link_context_menu.open_note_in_new_window"), command: "openNoteInNewWindow", uiIcon: "bx bx-window-open" }
];
}
function handleLinkContextMenuItem(command: string | undefined, notePath: string, viewScope = {}, hoistedNoteId: string | null = null) {
if (!hoistedNoteId) {
hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId ?? null;
}
if (command === "openNoteInNewTab") {
appContext.tabManager.openContextWithNote(notePath, { hoistedNoteId, viewScope });
} else if (command === "openNoteInNewSplit") {
const subContexts = appContext.tabManager.getActiveContext()?.getSubContexts();
if (!subContexts) {
logError("subContexts is null");
return;
}
const { ntxId } = subContexts[subContexts.length - 1];
appContext.triggerCommand("openNewNoteSplit", { ntxId, notePath, hoistedNoteId, viewScope });
} else if (command === "openNoteInNewWindow") {
appContext.triggerCommand("openInWindow", { notePath, hoistedNoteId, viewScope });
}
}
export default {
getItems,
handleLinkContextMenuItem,
openContextMenu
};

View File

@@ -1,270 +0,0 @@
import treeService from "../services/tree.js";
import froca from "../services/froca.js";
import clipboard from "../services/clipboard.js";
import noteCreateService from "../services/note_create.js";
import contextMenu, { type MenuCommandItem, type MenuItem } from "./context_menu.js";
import appContext, { type ContextMenuCommandData, type FilteredCommandNames } from "../components/app_context.js";
import noteTypesService from "../services/note_types.js";
import server from "../services/server.js";
import toastService from "../services/toast.js";
import dialogService from "../services/dialog.js";
import { t } from "../services/i18n.js";
import type NoteTreeWidget from "../widgets/note_tree.js";
import type FAttachment from "../entities/fattachment.js";
import type { SelectMenuItemEventListener } from "../components/events.js";
import utils from "../services/utils.js";
// TODO: Deduplicate once client/server is well split.
interface ConvertToAttachmentResponse {
attachment?: FAttachment;
}
// This will include all commands that implement ContextMenuCommandData, but it will not work if it additional options are added via the `|` operator,
// so they need to be added manually.
export type TreeCommandNames = FilteredCommandNames<ContextMenuCommandData> | "openBulkActionsDialog";
export default class TreeContextMenu implements SelectMenuItemEventListener<TreeCommandNames> {
private treeWidget: NoteTreeWidget;
private node: Fancytree.FancytreeNode;
constructor(treeWidget: NoteTreeWidget, node: Fancytree.FancytreeNode) {
this.treeWidget = treeWidget;
this.node = node;
}
async show(e: PointerEvent | JQuery.TouchStartEvent | JQuery.ContextMenuEvent) {
contextMenu.show({
x: e.pageX ?? 0,
y: e.pageY ?? 0,
items: await this.getMenuItems(),
selectMenuItemHandler: (item, e) => this.selectMenuItemHandler(item)
});
}
async getMenuItems(): Promise<MenuItem<TreeCommandNames>[]> {
const note = this.node.data.noteId ? await froca.getNote(this.node.data.noteId) : null;
const branch = froca.getBranch(this.node.data.branchId);
const isNotRoot = note?.noteId !== "root";
const isHoisted = note?.noteId === appContext.tabManager.getActiveContext()?.hoistedNoteId;
const parentNote = isNotRoot && branch ? await froca.getNote(branch.parentNoteId) : null;
// some actions don't support multi-note, so they are disabled when notes are selected,
// the only exception is when the only selected note is the one that was right-clicked, then
// it's clear what the user meant to do.
const selNodes = this.treeWidget.getSelectedNodes();
const noSelectedNotes = selNodes.length === 0 || (selNodes.length === 1 && selNodes[0] === this.node);
const notSearch = note?.type !== "search";
const notOptionsOrHelp = !note?.noteId.startsWith("_options") && !note?.noteId.startsWith("_help");
const parentNotSearch = !parentNote || parentNote.type !== "search";
const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
const items: (MenuItem<TreeCommandNames> | null)[] = [
{ title: `${t("tree-context-menu.open-in-a-new-tab")}`, command: "openInTab", uiIcon: "bx bx-link-external", enabled: noSelectedNotes },
{ title: t("tree-context-menu.open-in-a-new-split"), command: "openNoteInSplit", uiIcon: "bx bx-dock-right", enabled: noSelectedNotes },
isHoisted
? null
: {
title: `${t("tree-context-menu.hoist-note")} <kbd data-command="toggleNoteHoisting"></kbd>`,
command: "toggleNoteHoisting",
uiIcon: "bx bxs-chevrons-up",
enabled: noSelectedNotes && notSearch
},
!isHoisted || !isNotRoot
? null
: { title: `${t("tree-context-menu.unhoist-note")} <kbd data-command="toggleNoteHoisting"></kbd>`, command: "toggleNoteHoisting", uiIcon: "bx bx-door-open" },
{ title: "----" },
{
title: `${t("tree-context-menu.insert-note-after")}<kbd data-command="createNoteAfter"></kbd>`,
command: "insertNoteAfter",
uiIcon: "bx bx-plus",
items: insertNoteAfterEnabled ? await noteTypesService.getNoteTypeItems("insertNoteAfter") : null,
enabled: insertNoteAfterEnabled && noSelectedNotes && notOptionsOrHelp
},
{
title: `${t("tree-context-menu.insert-child-note")}<kbd data-command="createNoteInto"></kbd>`,
command: "insertChildNote",
uiIcon: "bx bx-plus",
items: notSearch ? await noteTypesService.getNoteTypeItems("insertChildNote") : null,
enabled: notSearch && noSelectedNotes && notOptionsOrHelp
},
{ title: "----" },
{ title: t("tree-context-menu.protect-subtree"), command: "protectSubtree", uiIcon: "bx bx-check-shield", enabled: noSelectedNotes },
{ title: t("tree-context-menu.unprotect-subtree"), command: "unprotectSubtree", uiIcon: "bx bx-shield", enabled: noSelectedNotes },
{ title: "----" },
{
title: t("tree-context-menu.advanced"),
uiIcon: "bx bxs-wrench",
enabled: true,
items: [
{ title: t("tree-context-menu.apply-bulk-actions"), command: "openBulkActionsDialog", uiIcon: "bx bx-list-plus", enabled: true },
{ title: "----" },
{
title: `${t("tree-context-menu.edit-branch-prefix")} <kbd data-command="editBranchPrefix"></kbd>`,
command: "editBranchPrefix",
uiIcon: "bx bx-rename",
enabled: isNotRoot && parentNotSearch && noSelectedNotes && notOptionsOrHelp
},
{ title: t("tree-context-menu.convert-to-attachment"), command: "convertNoteToAttachment", uiIcon: "bx bx-paperclip", enabled: isNotRoot && !isHoisted && notOptionsOrHelp },
{
title: `${t("tree-context-menu.duplicate-subtree")} <kbd data-command="duplicateSubtree">`,
command: "duplicateSubtree",
uiIcon: "bx bx-outline",
enabled: parentNotSearch && isNotRoot && !isHoisted && notOptionsOrHelp
},
{ title: "----" },
{ title: `${t("tree-context-menu.expand-subtree")} <kbd data-command="expandSubtree"></kbd>`, command: "expandSubtree", uiIcon: "bx bx-expand", enabled: noSelectedNotes },
{ title: `${t("tree-context-menu.collapse-subtree")} <kbd data-command="collapseSubtree"></kbd>`, command: "collapseSubtree", uiIcon: "bx bx-collapse", enabled: noSelectedNotes },
{
title: `${t("tree-context-menu.sort-by")} <kbd data-command="sortChildNotes"></kbd>`,
command: "sortChildNotes",
uiIcon: "bx bx-sort-down",
enabled: noSelectedNotes && notSearch
},
{ title: "----" },
{ title: t("tree-context-menu.copy-note-path-to-clipboard"), command: "copyNotePathToClipboard", uiIcon: "bx bx-directions", enabled: true },
{ title: t("tree-context-menu.recent-changes-in-subtree"), command: "recentChangesInSubtree", uiIcon: "bx bx-history", enabled: noSelectedNotes && notOptionsOrHelp }
]
},
{ title: "----" },
{
title: `${t("tree-context-menu.cut")} <kbd data-command="cutNotesToClipboard"></kbd>`,
command: "cutNotesToClipboard",
uiIcon: "bx bx-cut",
enabled: isNotRoot && !isHoisted && parentNotSearch
},
{ title: `${t("tree-context-menu.copy-clone")} <kbd data-command="copyNotesToClipboard"></kbd>`, command: "copyNotesToClipboard", uiIcon: "bx bx-copy", enabled: isNotRoot && !isHoisted },
{
title: `${t("tree-context-menu.paste-into")} <kbd data-command="pasteNotesFromClipboard"></kbd>`,
command: "pasteNotesFromClipboard",
uiIcon: "bx bx-paste",
enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes
},
{
title: t("tree-context-menu.paste-after"),
command: "pasteNotesAfterFromClipboard",
uiIcon: "bx bx-paste",
enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes
},
{
title: `${t("tree-context-menu.move-to")} <kbd data-command="moveNotesTo"></kbd>`,
command: "moveNotesTo",
uiIcon: "bx bx-transfer",
enabled: isNotRoot && !isHoisted && parentNotSearch
},
{ title: `${t("tree-context-menu.clone-to")} <kbd data-command="cloneNotesTo"></kbd>`, command: "cloneNotesTo", uiIcon: "bx bx-duplicate", enabled: isNotRoot && !isHoisted },
{
title: `${t("tree-context-menu.delete")} <kbd data-command="deleteNotes"></kbd>`,
command: "deleteNotes",
uiIcon: "bx bx-trash destructive-action-icon",
enabled: isNotRoot && !isHoisted && parentNotSearch && notOptionsOrHelp
},
{ title: "----" },
{ title: t("tree-context-menu.import-into-note"), command: "importIntoNote", uiIcon: "bx bx-import", enabled: notSearch && noSelectedNotes && notOptionsOrHelp },
{ title: t("tree-context-menu.export"), command: "exportNote", uiIcon: "bx bx-export", enabled: notSearch && noSelectedNotes && notOptionsOrHelp },
{ title: "----" },
{
title: `${t("tree-context-menu.search-in-subtree")} <kbd data-command="searchInSubtree"></kbd>`,
command: "searchInSubtree",
uiIcon: "bx bx-search",
enabled: notSearch && noSelectedNotes
}
];
return items.filter((row) => row !== null) as MenuItem<TreeCommandNames>[];
}
async selectMenuItemHandler({ command, type, templateNoteId }: MenuCommandItem<TreeCommandNames>) {
const notePath = treeService.getNotePath(this.node);
if (utils.isMobile()) {
this.treeWidget.triggerCommand("setActiveScreen", { screen: "detail" });
}
if (command === "openInTab") {
appContext.tabManager.openTabWithNoteWithHoisting(notePath);
} else if (command === "insertNoteAfter") {
const parentNotePath = treeService.getNotePath(this.node.getParent());
const isProtected = treeService.getParentProtectedStatus(this.node);
noteCreateService.createNote(parentNotePath, {
target: "after",
targetBranchId: this.node.data.branchId,
type: type,
isProtected: isProtected,
templateNoteId: templateNoteId
});
} else if (command === "insertChildNote") {
const parentNotePath = treeService.getNotePath(this.node);
noteCreateService.createNote(parentNotePath, {
type: type,
isProtected: this.node.data.isProtected,
templateNoteId: templateNoteId
});
} else if (command === "openNoteInSplit") {
const subContexts = appContext.tabManager.getActiveContext()?.getSubContexts();
const { ntxId } = subContexts?.[subContexts.length - 1] ?? {};
this.treeWidget.triggerCommand("openNewNoteSplit", { ntxId, notePath });
} else if (command === "convertNoteToAttachment") {
if (!(await dialogService.confirm(t("tree-context-menu.convert-to-attachment-confirm")))) {
return;
}
let converted = 0;
for (const noteId of this.treeWidget.getSelectedOrActiveNoteIds(this.node)) {
const note = await froca.getNote(noteId);
if (note?.isEligibleForConversionToAttachment()) {
const { attachment } = await server.post<ConvertToAttachmentResponse>(`notes/${note.noteId}/convert-to-attachment`);
if (attachment) {
converted++;
}
}
}
toastService.showMessage(t("tree-context-menu.converted-to-attachments", { count: converted }));
} else if (command === "copyNotePathToClipboard") {
navigator.clipboard.writeText("#" + notePath);
} else if (command) {
this.treeWidget.triggerCommand<TreeCommandNames>(command, {
node: this.node,
notePath: notePath,
noteId: this.node.data.noteId,
selectedOrActiveBranchIds: this.treeWidget.getSelectedOrActiveBranchIds(this.node),
selectedOrActiveNoteIds: this.treeWidget.getSelectedOrActiveNoteIds(this.node)
});
}
}
}

View File

@@ -1,16 +0,0 @@
import appContext from "./components/app_context.js";
import noteAutocompleteService from "./services/note_autocomplete.js";
import glob from "./services/glob.js";
import "../stylesheets/bootstrap.scss";
glob.setupGlobs();
await appContext.earlyInit();
noteAutocompleteService.init();
// A dynamic import is required for layouts since they initialize components which require translations.
const MobileLayout = (await import("./layouts/mobile_layout.js")).default;
appContext.setLayout(new MobileLayout());
appContext.start();

View File

@@ -1,24 +0,0 @@
import type { EntityRowNames } from "./services/load_results.js";
interface Entity {
isDeleted?: boolean;
}
// TODO: Deduplicate with src/services/entity_changes_interface.ts
export interface EntityChange {
id?: number | null;
noteId?: string;
entityName: EntityRowNames;
entityId: string;
entity?: Entity;
positions?: Record<string, number>;
hash: string;
utcDateChanged?: string;
utcDateModified?: string;
utcDateCreated?: string;
isSynced: boolean | 1 | 0;
isErased: boolean | 1 | 0;
componentId?: string | null;
changeId?: string | null;
instanceId?: string | null;
}

View File

@@ -1,114 +0,0 @@
import type { AttributeType } from "../entities/fattribute.js";
import server from "./server.js";
interface InitOptions {
$el: JQuery<HTMLElement>;
attributeType?: AttributeType | (() => AttributeType);
open: boolean;
nameCallback?: () => string;
}
/**
* @param $el - element on which to init autocomplete
* @param attributeType - "relation" or "label" or callback providing one of those values as a type of autocompleted attributes
* @param open - should the autocomplete be opened after init?
*/
function initAttributeNameAutocomplete({ $el, attributeType, open }: InitOptions) {
if (!$el.hasClass("aa-input")) {
$el.autocomplete(
{
appendTo: document.querySelector("body"),
hint: false,
openOnFocus: true,
minLength: 0,
tabAutocomplete: false
},
[
{
displayKey: "name",
// disabling cache is important here because otherwise cache can stay intact when switching between attribute type which will lead to autocomplete displaying attribute names for incorrect attribute type
cache: false,
source: async (term, cb) => {
const type = typeof attributeType === "function" ? attributeType() : attributeType;
const names = await server.get<string[]>(`attribute-names/?type=${type}&query=${encodeURIComponent(term)}`);
const result = names.map((name) => ({ name }));
cb(result);
}
}
]
);
$el.on("autocomplete:opened", () => {
if ($el.attr("readonly")) {
$el.autocomplete("close");
}
});
}
if (open) {
$el.autocomplete("open");
}
}
async function initLabelValueAutocomplete({ $el, open, nameCallback }: InitOptions) {
if ($el.hasClass("aa-input")) {
// we reinit every time because autocomplete seems to have a bug where it retains state from last
// open even though the value was reset
$el.autocomplete("destroy");
}
let attributeName = "";
if (nameCallback) {
attributeName = nameCallback();
}
if (attributeName.trim() === "") {
return;
}
const attributeValues = (await server.get<string[]>(`attribute-values/${encodeURIComponent(attributeName)}`)).map((attribute) => ({ value: attribute }));
if (attributeValues.length === 0) {
return;
}
$el.autocomplete(
{
appendTo: document.querySelector("body"),
hint: false,
openOnFocus: false, // handled manually
minLength: 0,
tabAutocomplete: false
},
[
{
displayKey: "value",
cache: false,
source: async function (term, cb) {
term = term.toLowerCase();
const filtered = attributeValues.filter((attr) => attr.value.toLowerCase().includes(term));
cb(filtered);
}
}
]
);
$el.on("autocomplete:opened", () => {
if ($el.attr("readonly")) {
$el.autocomplete("close");
}
});
if (open) {
$el.autocomplete("open");
}
}
export default {
initAttributeNameAutocomplete,
initLabelValueAutocomplete
};

View File

@@ -1,100 +0,0 @@
import { describe, it, expect } from "vitest";
import attributeParser from "./attribute_parser.js";
describe("Lexing", () => {
it("simple label", () => {
expect(attributeParser.lex("#label").map((t: any) => t.text)).toEqual(["#label"]);
});
it("simple label with trailing spaces", () => {
expect(attributeParser.lex(" #label ").map((t: any) => t.text)).toEqual(["#label"]);
});
it("inherited label", () => {
expect(attributeParser.lex("#label(inheritable)").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);
expect(attributeParser.lex("#label ( inheritable ) ").map((t: any) => t.text)).toEqual(["#label", "(", "inheritable", ")"]);
});
it("label with value", () => {
expect(attributeParser.lex("#label=Hallo").map((t: any) => t.text)).toEqual(["#label", "=", "Hallo"]);
});
it("label with value", () => {
const tokens = attributeParser.lex("#label=Hallo");
expect(tokens[0].startIndex).toEqual(0);
expect(tokens[0].endIndex).toEqual(5);
});
it("relation with value", () => {
expect(attributeParser.lex("~relation=#root/RclIpMauTOKS/NFi2gL4xtPxM").map((t: any) => t.text)).toEqual(["~relation", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"]);
});
it("use quotes to define value", () => {
expect(attributeParser.lex("#'label a'='hello\"` world'").map((t: any) => t.text)).toEqual(["#label a", "=", 'hello"` world']);
expect(attributeParser.lex('#"label a" = "hello\'` world"').map((t: any) => t.text)).toEqual(["#label a", "=", "hello'` world"]);
expect(attributeParser.lex("#`label a` = `hello'\" world`").map((t: any) => t.text)).toEqual(["#label a", "=", "hello'\" world"]);
});
});
describe.todo("Parser", () => {
/* #TODO
it("simple label", () => {
const attrs = attributeParser.parse(["#token"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].isInheritable).toBeFalsy();
expect(attrs[0].value).toBeFalsy();
});
it("inherited label", () => {
const attrs = attributeParser.parse(["#token", "(", "inheritable", ")"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].isInheritable).toBeTruthy();
expect(attrs[0].value).toBeFalsy();
});
it("label with value", () => {
const attrs = attributeParser.parse(["#token", "=", "val"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("label");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual("val");
});
it("relation", () => {
let attrs = attributeParser.parse(["~token", "=", "#root/RclIpMauTOKS/NFi2gL4xtPxM"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("relation");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual("NFi2gL4xtPxM");
attrs = attributeParser.parse(["~token", "=", "#NFi2gL4xtPxM"].map((t: any) => ({ text: t })));
expect(attrs.length).toEqual(1);
expect(attrs[0].type).toEqual("relation");
expect(attrs[0].name).toEqual("token");
expect(attrs[0].value).toEqual("NFi2gL4xtPxM");
});
*/
});
describe("error cases", () => {
it("error cases", () => {
expect(() => attributeParser.lexAndParse("~token")).toThrow('Relation "~token" in "~token" should point to a note.');
expect(() => attributeParser.lexAndParse("#a&b/s")).toThrow(`Attribute name "a&b/s" contains disallowed characters, only alphanumeric characters, colon and underscore are allowed.`);
expect(() => attributeParser.lexAndParse("#")).toThrow(`Attribute name is empty, please fill the name.`);
});
});

View File

@@ -1,230 +0,0 @@
import type { AttributeType, FAttributeRow } from "../entities/fattribute.js";
import utils from "./utils.js";
interface Token {
text: string;
startIndex: number;
endIndex: number;
}
export interface Attribute {
attributeId?: string;
type: AttributeType;
name: string;
isInheritable?: boolean;
value?: string;
startIndex?: number;
endIndex?: number;
noteId?: string;
}
function lex(str: string) {
str = str.trim();
const tokens: Token[] = [];
let quotes: boolean | string = false;
let currentWord = "";
function isOperatorSymbol(chr: string) {
return ["=", "*", ">", "<", "!"].includes(chr);
}
function previousOperatorSymbol() {
if (currentWord.length === 0) {
return false;
} else {
return isOperatorSymbol(currentWord[currentWord.length - 1]);
}
}
/**
* @param endIndex - index of the last character of the token
*/
function finishWord(endIndex: number) {
if (currentWord === "") {
return;
}
tokens.push({
text: currentWord,
startIndex: endIndex - currentWord.length + 1,
endIndex: endIndex
});
currentWord = "";
}
for (let i = 0; i < str.length; i++) {
const chr = str[i];
if (chr === "\\") {
if (i + 1 < str.length) {
i++;
currentWord += str[i];
} else {
currentWord += chr;
}
continue;
} else if (['"', "'", "`"].includes(chr)) {
if (!quotes) {
if (previousOperatorSymbol()) {
finishWord(i - 1);
}
quotes = chr;
} else if (quotes === chr) {
quotes = false;
finishWord(i - 1);
} else {
// it's a quote, but within other kind of quotes, so it's valid as a literal character
currentWord += chr;
}
continue;
} else if (!quotes) {
if (currentWord.length === 0 && (chr === "#" || chr === "~")) {
currentWord = chr;
continue;
} else if (chr === " ") {
finishWord(i - 1);
continue;
} else if (["(", ")"].includes(chr)) {
finishWord(i - 1);
currentWord = chr;
finishWord(i);
continue;
} else if (previousOperatorSymbol() !== isOperatorSymbol(chr)) {
finishWord(i - 1);
currentWord += chr;
continue;
}
}
currentWord += chr;
}
finishWord(str.length - 1);
return tokens;
}
function checkAttributeName(attrName: string) {
if (attrName.length === 0) {
throw new Error("Attribute name is empty, please fill the name.");
}
if (!utils.isValidAttributeName(attrName)) {
throw new Error(`Attribute name "${attrName}" contains disallowed characters, only alphanumeric characters, colon and underscore are allowed.`);
}
}
function parse(tokens: Token[], str: string, allowEmptyRelations = false) {
const attrs: Attribute[] = [];
function context(i: number) {
let { startIndex, endIndex } = tokens[i];
startIndex = Math.max(0, startIndex - 20);
endIndex = Math.min(str.length, endIndex + 20);
return `"${startIndex !== 0 ? "..." : ""}${str.substr(startIndex, endIndex - startIndex)}${endIndex !== str.length ? "..." : ""}"`;
}
for (let i = 0; i < tokens.length; i++) {
const { text, startIndex } = tokens[i];
function isInheritable() {
if (tokens.length > i + 3 && tokens[i + 1].text === "(" && tokens[i + 2].text === "inheritable" && tokens[i + 3].text === ")") {
i += 3;
return true;
} else {
return false;
}
}
if (text.startsWith("#")) {
const labelName = text.substr(1);
checkAttributeName(labelName);
const attr: Attribute = {
type: "label",
name: labelName,
isInheritable: isInheritable(),
startIndex: startIndex,
endIndex: tokens[i].endIndex // i could be moved by isInheritable
};
if (i + 1 < tokens.length && tokens[i + 1].text === "=") {
if (i + 2 >= tokens.length) {
throw new Error(`Missing value for label "${text}" in ${context(i)}`);
}
i += 2;
attr.value = tokens[i].text;
attr.endIndex = tokens[i].endIndex;
}
attrs.push(attr);
} else if (text.startsWith("~")) {
const relationName = text.substr(1);
checkAttributeName(relationName);
const attr: Attribute = {
type: "relation",
name: relationName,
isInheritable: isInheritable(),
startIndex: startIndex,
endIndex: tokens[i].endIndex // i could be moved by isInheritable
};
attrs.push(attr);
if (i + 2 >= tokens.length || tokens[i + 1].text !== "=") {
if (allowEmptyRelations) {
break;
} else {
throw new Error(`Relation "${text}" in ${context(i)} should point to a note.`);
}
}
i += 2;
let notePath = tokens[i].text;
if (notePath.startsWith("#")) {
notePath = notePath.substr(1);
}
const noteId = notePath.split("/").pop();
attr.value = noteId;
attr.endIndex = tokens[i].endIndex;
} else {
throw new Error(`Invalid attribute "${text}" in ${context(i)}`);
}
}
return attrs;
}
function lexAndParse(str: string, allowEmptyRelations = false) {
const tokens = lex(str);
return parse(tokens, str, allowEmptyRelations);
}
export default {
lex,
parse,
lexAndParse
};

View File

@@ -1,106 +0,0 @@
import ws from "./ws.js";
import froca from "./froca.js";
import type FAttribute from "../entities/fattribute.js";
import type FNote from "../entities/fnote.js";
async function renderAttribute(attribute: FAttribute, renderIsInheritable: boolean) {
const isInheritable = renderIsInheritable && attribute.isInheritable ? `(inheritable)` : "";
const $attr = $("<span>");
if (attribute.type === "label") {
$attr.append(document.createTextNode(`#${attribute.name}${isInheritable}`));
if (attribute.value) {
$attr.append("=");
$attr.append(document.createTextNode(formatValue(attribute.value)));
}
} else if (attribute.type === "relation") {
if (attribute.isAutoLink) {
return $attr;
}
// when the relation has just been created, then it might not have a value
if (attribute.value) {
$attr.append(document.createTextNode(`~${attribute.name}${isInheritable}=`));
const link = await createLink(attribute.value);
if (link) {
$attr.append(link);
}
}
} else {
ws.logError(`Unknown attr type: ${attribute.type}`);
}
return $attr;
}
function formatValue(val: string) {
if (/^[\p{L}\p{N}\-_,.]+$/u.test(val)) {
return val;
} else if (!val.includes('"')) {
return `"${val}"`;
} else if (!val.includes("'")) {
return `'${val}'`;
} else if (!val.includes("`")) {
return `\`${val}\``;
} else {
return `"${val.replace(/"/g, '\\"')}"`;
}
}
async function createLink(noteId: string) {
const note = await froca.getNote(noteId);
if (!note) {
return;
}
return $("<a>", {
href: `#root/${noteId}`,
class: "reference-link"
}).text(note.title);
}
async function renderAttributes(attributes: FAttribute[], renderIsInheritable: boolean) {
const $container = $('<span class="rendered-note-attributes">');
for (let i = 0; i < attributes.length; i++) {
const attribute = attributes[i];
const $attr = await renderAttribute(attribute, renderIsInheritable);
$container.append($attr.html()); // .html() to get only inner HTML, we don't want any spans
if (i < attributes.length - 1) {
$container.append(" ");
}
}
return $container;
}
const HIDDEN_ATTRIBUTES = ["originalFileName", "fileSize", "template", "inherit", "cssClass", "iconClass", "pageSize", "viewType", "geolocation", "docName"];
async function renderNormalAttributes(note: FNote) {
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
let attrs = note.getAttributes();
if (promotedDefinitionAttributes.length > 0) {
attrs = attrs.filter((attr) => !!promotedDefinitionAttributes.find((promAttr) => promAttr.isDefinitionFor(attr)));
} else {
attrs = attrs.filter((attr) => !attr.isDefinition() && !attr.isAutoLink && !HIDDEN_ATTRIBUTES.includes(attr.name) && attr.noteId === note.noteId);
}
const $renderedAttributes = await renderAttributes(attrs, false);
return {
count: attrs.length,
$renderedAttributes
};
}
export default {
renderAttribute,
renderAttributes,
renderNormalAttributes
};

View File

@@ -1,112 +0,0 @@
import server from "./server.js";
import froca from "./froca.js";
import type FNote from "../entities/fnote.js";
import type { AttributeRow } from "./load_results.js";
async function addLabel(noteId: string, name: string, value: string = "") {
await server.put(`notes/${noteId}/attribute`, {
type: "label",
name: name,
value: value
});
}
async function setLabel(noteId: string, name: string, value: string = "") {
await server.put(`notes/${noteId}/set-attribute`, {
type: "label",
name: name,
value: value
});
}
async function removeAttributeById(noteId: string, attributeId: string) {
await server.remove(`notes/${noteId}/attributes/${attributeId}`);
}
/**
* Removes a label identified by its name from the given note, if it exists. Note that the label must be owned, i.e.
* it will not remove inherited attributes.
*
* @param note the note from which to remove the label.
* @param labelName the name of the label to remove.
* @returns `true` if an attribute was identified and removed, `false` otherwise.
*/
function removeOwnedLabelByName(note: FNote, labelName: string) {
const label = note.getOwnedLabel(labelName);
if (label) {
removeAttributeById(note.noteId, label.attributeId);
return true;
}
return false;
}
/**
* Sets the attribute of the given note to the provided value if its truthy, or removes the attribute if the value is falsy.
* For an attribute with an empty value, pass an empty string instead.
*
* @param note the note to set the attribute to.
* @param type the type of attribute (label or relation).
* @param name the name of the attribute to set.
* @param value the value of the attribute to set.
*/
async function setAttribute(note: FNote, type: "label" | "relation", name: string, value: string | null | undefined) {
if (value) {
// Create or update the attribute.
await server.put(`notes/${note.noteId}/set-attribute`, { type, name, value });
} else {
// Remove the attribute if it exists on the server but we don't define a value for it.
const attributeId = note.getAttribute(type, name)?.attributeId;
if (attributeId) {
await server.remove(`notes/${note.noteId}/attributes/${attributeId}`);
}
}
}
/**
* @returns - returns true if this attribute has the potential to influence the note in the argument.
* That can happen in multiple ways:
* 1. attribute is owned by the note
* 2. attribute is owned by the template of the note
* 3. attribute is owned by some note's ancestor and is inheritable
*/
function isAffecting(attrRow: AttributeRow, affectedNote: FNote | null | undefined) {
if (!affectedNote || !attrRow) {
return false;
}
const attrNote = attrRow.noteId && froca.notes[attrRow.noteId];
if (!attrNote) {
// the note (owner of the attribute) is not even loaded into the cache, so it should not affect anything else
return false;
}
const owningNotes = [affectedNote, ...affectedNote.getNotesToInheritAttributesFrom()];
for (const owningNote of owningNotes) {
if (owningNote.noteId === attrNote.noteId) {
return true;
}
}
// TODO: This doesn't seem right.
//@ts-ignore
if (this.isInheritable) {
for (const owningNote of owningNotes) {
if (owningNote.hasAncestor(attrNote.noteId, true)) {
return true;
}
}
}
return false;
}
export default {
addLabel,
setLabel,
setAttribute,
removeAttributeById,
removeOwnedLabelByName,
isAffecting
};

View File

@@ -1,276 +0,0 @@
import utils from "./utils.js";
import server from "./server.js";
import toastService, { type ToastOptions } from "./toast.js";
import froca from "./froca.js";
import hoistedNoteService from "./hoisted_note.js";
import ws from "./ws.js";
import appContext from "../components/app_context.js";
import { t } from "./i18n.js";
import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";
// TODO: Deduplicate type with server
interface Response {
success: boolean;
message: string;
}
async function moveBeforeBranch(branchIdsToMove: string[], beforeBranchId: string) {
branchIdsToMove = filterRootNote(branchIdsToMove);
branchIdsToMove = filterSearchBranches(branchIdsToMove);
const beforeBranch = froca.getBranch(beforeBranchId);
if (!beforeBranch) {
return;
}
if (beforeBranch.noteId === "root" || utils.isLaunchBarConfig(beforeBranch.noteId)) {
toastService.showError(t("branches.cannot-move-notes-here"));
return;
}
for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-before/${beforeBranchId}`);
if (!resp.success) {
toastService.showError(resp.message);
return;
}
}
}
async function moveAfterBranch(branchIdsToMove: string[], afterBranchId: string) {
branchIdsToMove = filterRootNote(branchIdsToMove);
branchIdsToMove = filterSearchBranches(branchIdsToMove);
const afterNote = await froca.getBranch(afterBranchId)?.getNote();
if (!afterNote) {
return;
}
const forbiddenNoteIds = ["root", hoistedNoteService.getHoistedNoteId(), "_lbRoot", "_lbAvailableLaunchers", "_lbVisibleLaunchers"];
if (forbiddenNoteIds.includes(afterNote.noteId)) {
toastService.showError(t("branches.cannot-move-notes-here"));
return;
}
branchIdsToMove.reverse(); // need to reverse to keep the note order
for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${afterBranchId}`);
if (!resp.success) {
toastService.showError(resp.message);
return;
}
}
}
async function moveToParentNote(branchIdsToMove: string[], newParentBranchId: string) {
const newParentBranch = froca.getBranch(newParentBranchId);
if (!newParentBranch) {
return;
}
if (newParentBranch.noteId === "_lbRoot") {
toastService.showError(t("branches.cannot-move-notes-here"));
return;
}
branchIdsToMove = filterRootNote(branchIdsToMove);
for (const branchIdToMove of branchIdsToMove) {
const branchToMove = froca.getBranch(branchIdToMove);
if (!branchToMove || branchToMove.noteId === hoistedNoteService.getHoistedNoteId() || (await branchToMove.getParentNote())?.type === "search") {
continue;
}
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-to/${newParentBranchId}`);
if (!resp.success) {
toastService.showError(resp.message);
return;
}
}
}
async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = false) {
branchIdsToDelete = filterRootNote(branchIdsToDelete);
if (branchIdsToDelete.length === 0) {
return false;
}
const { proceed, deleteAllClones, eraseNotes } = await new Promise<ResolveOptions>((res) =>
appContext.triggerCommand("showDeleteNotesDialog", { branchIdsToDelete, callback: res, forceDeleteAllClones })
);
if (!proceed) {
return false;
}
try {
await activateParentNotePath();
} catch (e) {
console.error(e);
}
const taskId = utils.randomString(10);
let counter = 0;
for (const branchIdToDelete of branchIdsToDelete) {
counter++;
const last = counter === branchIdsToDelete.length;
const query = `?taskId=${taskId}&eraseNotes=${eraseNotes ? "true" : "false"}&last=${last ? "true" : "false"}`;
const branch = froca.getBranch(branchIdToDelete);
if (deleteAllClones && branch) {
await server.remove(`notes/${branch.noteId}${query}`);
} else {
await server.remove(`branches/${branchIdToDelete}${query}`);
}
}
if (eraseNotes) {
utils.reloadFrontendApp("erasing notes requires reload");
}
return true;
}
async function activateParentNotePath() {
// this is not perfect, maybe we should find the next/previous sibling, but that's more complex
const activeContext = appContext.tabManager.getActiveContext();
const parentNotePathArr = activeContext?.notePathArray.slice(0, -1);
if (parentNotePathArr && parentNotePathArr.length > 0) {
activeContext?.setNote(parentNotePathArr.join("/"));
}
}
async function moveNodeUpInHierarchy(node: Fancytree.FancytreeNode) {
if (hoistedNoteService.isHoistedNode(node) || hoistedNoteService.isTopLevelNode(node) || node.getParent().data.noteType === "search") {
return;
}
const targetBranchId = node.getParent().data.branchId;
const branchIdToMove = node.data.branchId;
const resp = await server.put<Response>(`branches/${branchIdToMove}/move-after/${targetBranchId}`);
if (!resp.success) {
toastService.showError(resp.message);
return;
}
if (!hoistedNoteService.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
}
}
function filterSearchBranches(branchIds: string[]) {
return branchIds.filter((branchId) => !branchId.startsWith("virt-"));
}
function filterRootNote(branchIds: string[]) {
const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
return branchIds.filter((branchId) => {
const branch = froca.getBranch(branchId);
if (!branch) {
return false;
}
return branch.noteId !== "root" && branch.noteId !== hoistedNoteId;
});
}
function makeToast(id: string, message: string): ToastOptions {
return {
id: id,
title: t("branches.delete-status"),
message: message,
icon: "trash"
};
}
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "deleteNotes") {
return;
}
if (message.type === "taskError") {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("branches.delete-notes-in-progress", { count: message.progressCount })));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("branches.delete-finished-successfully"));
toast.closeAfter = 5000;
toastService.showPersistent(toast);
}
});
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "undeleteNotes") {
return;
}
if (message.type === "taskError") {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("branches.undeleting-notes-in-progress", { count: message.progressCount })));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("branches.undeleting-notes-finished-successfully"));
toast.closeAfter = 5000;
toastService.showPersistent(toast);
}
});
async function cloneNoteToBranch(childNoteId: string, parentBranchId: string, prefix?: string) {
const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, {
prefix: prefix
});
if (!resp.success) {
toastService.showError(resp.message);
}
}
async function cloneNoteToParentNote(childNoteId: string, parentNoteId: string, prefix?: string) {
const resp = await server.put<Response>(`notes/${childNoteId}/clone-to-note/${parentNoteId}`, {
prefix: prefix
});
if (!resp.success) {
toastService.showError(resp.message);
}
}
// beware that the first arg is noteId and the second is branchId!
async function cloneNoteAfter(noteId: string, afterBranchId: string) {
const resp = await server.put<Response>(`notes/${noteId}/clone-after/${afterBranchId}`);
if (!resp.success) {
toastService.showError(resp.message);
}
}
export default {
moveBeforeBranch,
moveAfterBranch,
moveToParentNote,
deleteNotes,
moveNodeUpInHierarchy,
cloneNoteAfter,
cloneNoteToBranch,
cloneNoteToParentNote
};

View File

@@ -1,97 +0,0 @@
import server from "./server.js";
import ws from "./ws.js";
import MoveNoteBulkAction from "../widgets/bulk_actions/note/move_note.js";
import DeleteNoteBulkAction from "../widgets/bulk_actions/note/delete_note.js";
import DeleteRevisionsBulkAction from "../widgets/bulk_actions/note/delete_revisions.js";
import DeleteLabelBulkAction from "../widgets/bulk_actions/label/delete_label.js";
import DeleteRelationBulkAction from "../widgets/bulk_actions/relation/delete_relation.js";
import RenameLabelBulkAction from "../widgets/bulk_actions/label/rename_label.js";
import RenameRelationBulkAction from "../widgets/bulk_actions/relation/rename_relation.js";
import UpdateLabelValueBulkAction from "../widgets/bulk_actions/label/update_label_value.js";
import UpdateRelationTargetBulkAction from "../widgets/bulk_actions/relation/update_relation_target.js";
import ExecuteScriptBulkAction from "../widgets/bulk_actions/execute_script.js";
import AddLabelBulkAction from "../widgets/bulk_actions/label/add_label.js";
import AddRelationBulkAction from "../widgets/bulk_actions/relation/add_relation.js";
import RenameNoteBulkAction from "../widgets/bulk_actions/note/rename_note.js";
import { t } from "./i18n.js";
import type FNote from "../entities/fnote.js";
const ACTION_GROUPS = [
{
title: t("bulk_actions.labels"),
actions: [AddLabelBulkAction, UpdateLabelValueBulkAction, RenameLabelBulkAction, DeleteLabelBulkAction]
},
{
title: t("bulk_actions.relations"),
actions: [AddRelationBulkAction, UpdateRelationTargetBulkAction, RenameRelationBulkAction, DeleteRelationBulkAction]
},
{
title: t("bulk_actions.notes"),
actions: [RenameNoteBulkAction, MoveNoteBulkAction, DeleteNoteBulkAction, DeleteRevisionsBulkAction]
},
{
title: t("bulk_actions.other"),
actions: [ExecuteScriptBulkAction]
}
];
const ACTION_CLASSES = [
RenameNoteBulkAction,
MoveNoteBulkAction,
DeleteNoteBulkAction,
DeleteRevisionsBulkAction,
DeleteLabelBulkAction,
DeleteRelationBulkAction,
RenameLabelBulkAction,
RenameRelationBulkAction,
AddLabelBulkAction,
AddRelationBulkAction,
UpdateLabelValueBulkAction,
UpdateRelationTargetBulkAction,
ExecuteScriptBulkAction
];
async function addAction(noteId: string, actionName: string) {
await server.post(`notes/${noteId}/attributes`, {
type: "label",
name: "action",
value: JSON.stringify({
name: actionName
})
});
await ws.waitForMaxKnownEntityChangeId();
}
function parseActions(note: FNote) {
const actionLabels = note.getLabels("action");
return actionLabels
.map((actionAttr) => {
let actionDef;
try {
actionDef = JSON.parse(actionAttr.value);
} catch (e: any) {
logError(`Parsing of attribute: '${actionAttr.value}' failed with error: ${e.message}`);
return null;
}
const ActionClass = ACTION_CLASSES.find((actionClass) => actionClass.actionName === actionDef.name);
if (!ActionClass) {
logError(`No action class for '${actionDef.name}' found.`);
return null;
}
return new ActionClass(actionAttr, actionDef);
})
.filter((action) => !!action);
}
export default {
addAction,
parseActions,
ACTION_CLASSES,
ACTION_GROUPS
};

View File

@@ -1,125 +0,0 @@
import ScriptContext from "./script_context.js";
import server from "./server.js";
import toastService from "./toast.js";
import froca from "./froca.js";
import utils from "./utils.js";
import { t } from "./i18n.js";
import type { Entity } from "./frontend_script_api.js";
// TODO: Deduplicate with server.
export interface Bundle {
script: string;
html: string;
noteId: string;
allNoteIds: string[];
}
interface Widget {
parentWidget?: string;
}
async function getAndExecuteBundle(noteId: string, originEntity = null, script = null, params = null) {
const bundle = await server.post<Bundle>(`script/bundle/${noteId}`, {
script,
params
});
return await executeBundle(bundle, originEntity);
}
async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {
const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);
try {
return await function () {
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
}.call(apiContext);
} catch (e: any) {
const note = await froca.getNote(bundle.noteId);
toastService.showAndLogError(`Execution of JS note "${note?.title}" with ID ${bundle.noteId} failed with error: ${e?.message}`);
}
}
async function executeStartupBundles() {
const isMobile = utils.isMobile();
const scriptBundles = await server.get<Bundle[]>("script/startup" + (isMobile ? "?mobile=true" : ""));
for (const bundle of scriptBundles) {
await executeBundle(bundle);
}
}
export class WidgetsByParent {
private byParent: Record<string, Widget[]>;
constructor() {
this.byParent = {};
}
add(widget: Widget) {
if (!widget.parentWidget) {
console.log(`Custom widget does not have mandatory 'parentWidget' property defined`);
return;
}
this.byParent[widget.parentWidget] = this.byParent[widget.parentWidget] || [];
this.byParent[widget.parentWidget].push(widget);
}
get(parentName: string) {
if (!this.byParent[parentName]) {
return [];
}
return (
this.byParent[parentName]
// previously, custom widgets were provided as a single instance, but that has the disadvantage
// for splits where we actually need multiple instaces and thus having a class to instantiate is better
// https://github.com/zadam/trilium/issues/4274
.map((w: any) => (w.prototype ? new w() : w))
);
}
}
async function getWidgetBundlesByParent() {
const scriptBundles = await server.get<Bundle[]>("script/widgets");
const widgetsByParent = new WidgetsByParent();
for (const bundle of scriptBundles) {
let widget;
try {
widget = await executeBundle(bundle);
if (widget) {
widget._noteId = bundle.noteId;
widgetsByParent.add(widget);
}
} catch (e: any) {
const noteId = bundle.noteId;
const note = await froca.getNote(noteId);
toastService.showPersistent({
title: t("toast.bundle-error.title"),
icon: "alert",
message: t("toast.bundle-error.message", {
id: noteId,
title: note?.title,
message: e.message
})
});
logError("Widget initialization failed: ", e);
continue;
}
}
return widgetsByParent;
}
export default {
executeBundle,
getAndExecuteBundle,
executeStartupBundles,
getWidgetBundlesByParent
};

View File

@@ -1,117 +0,0 @@
import branchService from "./branches.js";
import toastService from "./toast.js";
import froca from "./froca.js";
import linkService from "./link.js";
import utils from "./utils.js";
import { t } from "./i18n.js";
let clipboardBranchIds: string[] = [];
let clipboardMode: string | null = null;
async function pasteAfter(afterBranchId: string) {
if (isClipboardEmpty()) {
return;
}
if (clipboardMode === "cut") {
await branchService.moveAfterBranch(clipboardBranchIds, afterBranchId);
clipboardBranchIds = [];
clipboardMode = null;
} else if (clipboardMode === "copy") {
const clipboardBranches = clipboardBranchIds.map((branchId) => froca.getBranch(branchId));
for (const clipboardBranch of clipboardBranches) {
if (!clipboardBranch) {
continue;
}
const clipboardNote = await clipboardBranch.getNote();
if (!clipboardNote) {
continue;
}
await branchService.cloneNoteAfter(clipboardNote.noteId, afterBranchId);
}
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
} else {
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`);
}
}
async function pasteInto(parentBranchId: string) {
if (isClipboardEmpty()) {
return;
}
if (clipboardMode === "cut") {
await branchService.moveToParentNote(clipboardBranchIds, parentBranchId);
clipboardBranchIds = [];
clipboardMode = null;
} else if (clipboardMode === "copy") {
const clipboardBranches = clipboardBranchIds.map((branchId) => froca.getBranch(branchId));
for (const clipboardBranch of clipboardBranches) {
if (!clipboardBranch) {
continue;
}
const clipboardNote = await clipboardBranch.getNote();
if (!clipboardNote) {
continue;
}
await branchService.cloneNoteToBranch(clipboardNote.noteId, parentBranchId);
}
// copy will keep clipboardBranchIds and clipboardMode, so it's possible to paste into multiple places
} else {
toastService.throwError(`Unrecognized clipboard mode=${clipboardMode}`);
}
}
async function copy(branchIds: string[]) {
clipboardBranchIds = branchIds;
clipboardMode = "copy";
if (utils.isElectron()) {
// https://github.com/zadam/trilium/issues/2401
const { clipboard } = require("electron");
const links = [];
for (const branch of froca.getBranches(clipboardBranchIds)) {
const $link = await linkService.createLink(`${branch.parentNoteId}/${branch.noteId}`, { referenceLink: true });
links.push($link[0].outerHTML);
}
clipboard.writeHTML(links.join(", "));
}
toastService.showMessage(t("clipboard.copied"));
}
function cut(branchIds: string[]) {
clipboardBranchIds = branchIds;
if (clipboardBranchIds.length > 0) {
clipboardMode = "cut";
toastService.showMessage(t("clipboard.cut"));
}
}
function isClipboardEmpty() {
clipboardBranchIds = clipboardBranchIds.filter((branchId) => !!froca.getBranch(branchId));
return clipboardBranchIds.length === 0;
}
export default {
pasteAfter,
pasteInto,
cut,
copy,
isClipboardEmpty
};

View File

@@ -1,327 +0,0 @@
import renderService from "./render.js";
import protectedSessionService from "./protected_session.js";
import protectedSessionHolder from "./protected_session_holder.js";
import libraryLoader from "./library_loader.js";
import openService from "./open.js";
import froca from "./froca.js";
import utils from "./utils.js";
import linkService from "./link.js";
import treeService from "./tree.js";
import FNote from "../entities/fnote.js";
import FAttachment from "../entities/fattachment.js";
import imageContextMenuService from "../menus/image_context_menu.js";
import { applySingleBlockSyntaxHighlight, applySyntaxHighlight } from "./syntax_highlight.js";
import { loadElkIfNeeded, postprocessMermaidSvg } from "./mermaid.js";
import { normalizeMimeTypeForCKEditor } from "./mime_type_definitions.js";
import renderDoc from "./doc_renderer.js";
import { t } from "i18next";
import WheelZoom from 'vanilla-js-wheel-zoom';
let idCounter = 1;
interface Options {
tooltip?: boolean;
trim?: boolean;
imageHasZoom?: boolean;
}
const CODE_MIME_TYPES = new Set(["application/json"]);
async function getRenderedContent(this: {} | { ctx: string }, entity: FNote | FAttachment, options: Options = {}) {
options = Object.assign(
{
tooltip: false
},
options
);
const type = getRenderingType(entity);
// attachment supports only image and file/pdf/audio/video
const $renderedContent = $('<div class="rendered-content">');
if (type === "text" || type === "book") {
await renderText(entity, $renderedContent);
} else if (type === "code") {
await renderCode(entity, $renderedContent);
} else if (["image", "canvas", "mindMap"].includes(type)) {
renderImage(entity, $renderedContent, options);
} else if (!options.tooltip && ["file", "pdf", "audio", "video"].includes(type)) {
renderFile(entity, type, $renderedContent);
} else if (type === "mermaid") {
await renderMermaid(entity, $renderedContent);
} else if (type === "render" && entity instanceof FNote) {
const $content = $("<div>");
await renderService.render(entity, $content);
$renderedContent.append($content);
} else if (type === "doc" && "noteId" in entity) {
const $content = await renderDoc(entity);
$renderedContent.html($content.html());
} else if (!options.tooltip && type === "protectedSession") {
const $button = $(`<button class="btn btn-sm"><span class="bx bx-log-in"></span> Enter protected session</button>`).on("click", protectedSessionService.enterProtectedSession);
$renderedContent.append($("<div>").append("<div>This note is protected and to access it you need to enter password.</div>").append("<br/>").append($button));
} else if (entity instanceof FNote) {
$renderedContent.append(
$("<div>")
.css("display", "flex")
.css("justify-content", "space-around")
.css("align-items", "center")
.css("height", "100%")
.css("font-size", "500%")
.append($("<span>").addClass(entity.getIcon()))
);
}
if (entity instanceof FNote) {
$renderedContent.addClass(entity.getCssClass());
}
return {
$renderedContent,
type
};
}
async function renderText(note: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>) {
// entity must be FNote
const blob = await note.getBlob();
if (blob && !utils.isHtmlEmpty(blob.content)) {
$renderedContent.append($('<div class="ck-content">').html(blob.content));
if ($renderedContent.find("span.math-tex").length > 0) {
await libraryLoader.requireLibrary(libraryLoader.KATEX);
renderMathInElement($renderedContent[0], { trust: true });
}
const getNoteIdFromLink = (el: HTMLElement) => treeService.getNoteIdFromUrl($(el).attr("href") || "");
const referenceLinks = $renderedContent.find("a.reference-link");
const noteIdsToPrefetch = referenceLinks.map((i, el) => getNoteIdFromLink(el));
await froca.getNotes(noteIdsToPrefetch);
for (const el of referenceLinks) {
await linkService.loadReferenceLinkTitle($(el));
}
await applySyntaxHighlight($renderedContent);
} else if (note instanceof FNote) {
await renderChildrenList($renderedContent, note);
}
}
/**
* Renders a code note, by displaying its content and applying syntax highlighting based on the selected MIME type.
*/
async function renderCode(note: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>) {
const blob = await note.getBlob();
const $codeBlock = $("<code>");
$codeBlock.text(blob?.content || "");
$renderedContent.append($("<pre>").append($codeBlock));
await applySingleBlockSyntaxHighlight($codeBlock, normalizeMimeTypeForCKEditor(note.mime));
}
function renderImage(entity: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>, options: Options = {}) {
const encodedTitle = encodeURIComponent(entity.title);
let url;
if (entity instanceof FNote) {
url = `api/images/${entity.noteId}/${encodedTitle}?${Math.random()}`;
} else if (entity instanceof FAttachment) {
url = `api/attachments/${entity.attachmentId}/image/${encodedTitle}?${entity.utcDateModified}">`;
}
$renderedContent // styles needed for the zoom to work well
.css("display", "flex")
.css("align-items", "center")
.css("justify-content", "center");
const $img = $("<img>")
.attr("src", url || "")
.attr("id", "attachment-image-" + idCounter++)
.css("max-width", "100%");
$renderedContent.append($img);
if (options.imageHasZoom) {
const initZoom = async () => {
const element = document.querySelector(`#${$img.attr("id")}`);
if (element) {
WheelZoom.create(`#${$img.attr("id")}`, {
maxScale: 50,
speed: 1.3,
zoomOnClick: false
});
} else {
requestAnimationFrame(initZoom);
}
};
initZoom();
}
imageContextMenuService.setupContextMenu($img);
}
function renderFile(entity: FNote | FAttachment, type: string, $renderedContent: JQuery<HTMLElement>) {
let entityType, entityId;
if (entity instanceof FNote) {
entityType = "notes";
entityId = entity.noteId;
} else if (entity instanceof FAttachment) {
entityType = "attachments";
entityId = entity.attachmentId;
} else {
throw new Error(`Can't recognize entity type of '${entity}'`);
}
const $content = $('<div style="display: flex; flex-direction: column; height: 100%;">');
if (type === "pdf") {
const $pdfPreview = $('<iframe class="pdf-preview" style="width: 100%; flex-grow: 100;"></iframe>');
$pdfPreview.attr("src", openService.getUrlForDownload(`api/${entityType}/${entityId}/open`));
$content.append($pdfPreview);
} else if (type === "audio") {
const $audioPreview = $("<audio controls></audio>")
.attr("src", openService.getUrlForDownload(`api/${entityType}/${entityId}/open-partial`))
.attr("type", entity.mime)
.css("width", "100%");
$content.append($audioPreview);
} else if (type === "video") {
const $videoPreview = $("<video controls></video>")
.attr("src", openService.getUrlForDownload(`api/${entityType}/${entityId}/open-partial`))
.attr("type", entity.mime)
.css("width", "100%");
$content.append($videoPreview);
}
if (entityType === "notes" && "noteId" in entity) {
// TODO: we should make this available also for attachments, but there's a problem with "Open externally" support
// in attachment list
const $downloadButton = $(`
<button class="file-download btn btn-primary" type="button">
<span class="bx bx-download"></span>
${t("file_properties.download")}
</button>
`);
const $openButton = $(`
<button class="file-open btn btn-primary" type="button">
<span class="bx bx-link-external"></span>
${t("file_properties.open")}
</button>
`);
$downloadButton.on("click", () => openService.downloadFileNote(entity.noteId));
$openButton.on("click", () => openService.openNoteExternally(entity.noteId, entity.mime));
// open doesn't work for protected notes since it works through a browser which isn't in protected session
$openButton.toggle(!entity.isProtected);
$content.append($('<footer class="file-footer">').append($downloadButton).append($openButton));
}
$renderedContent.append($content);
}
async function renderMermaid(note: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>) {
const mermaid = (await import("mermaid")).default;
const blob = await note.getBlob();
const content = blob?.content || "";
$renderedContent.css("display", "flex").css("justify-content", "space-around");
const documentStyle = window.getComputedStyle(document.documentElement);
const mermaidTheme = documentStyle.getPropertyValue("--mermaid-theme");
mermaid.mermaidAPI.initialize({ startOnLoad: false, theme: mermaidTheme.trim() as "default", securityLevel: "antiscript" });
try {
await loadElkIfNeeded(mermaid, content);
const { svg } = await mermaid.mermaidAPI.render("in-mermaid-graph-" + idCounter++, content);
$renderedContent.append($(postprocessMermaidSvg(svg)));
} catch (e) {
const $error = $("<p>The diagram could not displayed.</p>");
$renderedContent.append($error);
}
}
/**
* @param {jQuery} $renderedContent
* @param {FNote} note
* @returns {Promise<void>}
*/
async function renderChildrenList($renderedContent: JQuery<HTMLElement>, note: FNote) {
let childNoteIds = note.getChildNoteIds();
if (!childNoteIds.length) {
return;
}
$renderedContent.css("padding", "10px");
$renderedContent.addClass("text-with-ellipsis");
if (childNoteIds.length > 10) {
childNoteIds = childNoteIds.slice(0, 10);
}
// just load the first 10 child notes
const childNotes = await froca.getNotes(childNoteIds);
for (const childNote of childNotes) {
$renderedContent.append(
await linkService.createLink(`${note.noteId}/${childNote.noteId}`, {
showTooltip: false,
showNoteIcon: true
})
);
$renderedContent.append("<br>");
}
}
function getRenderingType(entity: FNote | FAttachment) {
let type: string = "";
if ("type" in entity) {
type = entity.type;
} else if ("role" in entity) {
type = entity.role;
}
const mime = "mime" in entity && entity.mime;
if (type === "file" && mime === "application/pdf") {
type = "pdf";
} else if (type === "file" && mime && CODE_MIME_TYPES.has(mime)) {
type = "code";
} else if (type === "file" && mime && mime.startsWith("audio/")) {
type = "audio";
} else if (type === "file" && mime && mime.startsWith("video/")) {
type = "video";
}
if (entity.isProtected) {
if (protectedSessionHolder.isProtectedSessionAvailable()) {
protectedSessionHolder.touchProtectedSession();
} else {
type = "protectedSession";
}
}
return type;
}
export default {
getRenderedContent
};

View File

@@ -1,28 +0,0 @@
const registeredClasses = new Set<string>();
function createClassForColor(color: string | null) {
if (!color?.trim()) {
return "";
}
const normalizedColorName = color.replace(/[^a-z0-9]/gi, "");
if (!normalizedColorName.trim()) {
return "";
}
const className = `color-${normalizedColorName}`;
if (!registeredClasses.has(className)) {
// make the active fancytree selector more specific than the normal color setting
$("head").append(`<style>.${className}, span.fancytree-active.${className} { color: ${color} !important; }</style>`);
registeredClasses.add(className);
}
return className;
}
export default {
createClassForColor
};

View File

@@ -1,92 +0,0 @@
import dayjs from "dayjs";
import type { FNoteRow } from "../entities/fnote.js";
import froca from "./froca.js";
import server from "./server.js";
import ws from "./ws.js";
async function getInboxNote() {
const note = await server.get<FNoteRow>(`special-notes/inbox/${dayjs().format("YYYY-MM-DD")}`, "date-note");
return await froca.getNote(note.noteId);
}
async function getTodayNote() {
return await getDayNote(dayjs().format("YYYY-MM-DD"));
}
async function getDayNote(date: string) {
const note = await server.get<FNoteRow>(`special-notes/days/${date}`, "date-note");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
async function getWeekFirstDayNote(date: string) {
const note = await server.get<FNoteRow>(`special-notes/week-first-day/${date}`, "date-note");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
async function getWeekNote(week: string) {
const note = await server.get<FNoteRow>(`special-notes/weeks/${week}`, "date-note");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note?.noteId);
}
async function getMonthNote(month: string) {
const note = await server.get<FNoteRow>(`special-notes/months/${month}`, "date-note");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
async function getQuarterNote(quarter: string) {
const note = await server.get<FNoteRow>(`special-notes/quarters/${quarter}`, "date-note");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
async function getYearNote(year: string) {
const note = await server.get<FNoteRow>(`special-notes/years/${year}`, "date-note");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
async function createSqlConsole() {
const note = await server.post<FNoteRow>("special-notes/sql-console");
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
async function createSearchNote(opts = {}) {
const note = await server.post<FNoteRow>("special-notes/search-note", opts);
await ws.waitForMaxKnownEntityChangeId();
return await froca.getNote(note.noteId);
}
export default {
getInboxNote,
getTodayNote,
getDayNote,
getWeekFirstDayNote,
getWeekNote,
getQuarterNote,
getMonthNote,
getYearNote,
createSqlConsole,
createSearchNote
};

View File

@@ -1,74 +0,0 @@
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing. The function also has a property 'clear'
* that is a function which will clear the timer to prevent previously scheduled executions.
*
* @source underscore.js
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
* @param func to wrap
* @param waitMs in ms (`100`)
* @param whether to execute at the beginning (`false`)
* @api public
*/
function debounce<T>(func: (...args: unknown[]) => T, waitMs: number, immediate: boolean = false) {
let timeout: any; // TODO: fix once we split client and server.
let args: unknown[] | null;
let context: unknown;
let timestamp: number;
let result: T;
if (null == waitMs) waitMs = 100;
function later() {
const last = Date.now() - timestamp;
if (last < waitMs && last >= 0) {
timeout = setTimeout(later, waitMs - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args || []);
context = args = null;
}
}
}
const debounced = function (this: any) {
context = this;
args = arguments as unknown as unknown[];
timestamp = Date.now();
const callNow = immediate && !timeout;
if (!timeout) timeout = setTimeout(later, waitMs);
if (callNow) {
result = func.apply(context, args || []);
context = args = null;
}
return result;
};
debounced.clear = function () {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
};
debounced.flush = function () {
if (timeout) {
result = func.apply(context, args || []);
context = args = null;
clearTimeout(timeout);
timeout = null;
}
};
return debounced;
}
// Adds compatibility for ES modules
debounce.debounce = debounce;
export default debounce;

View File

@@ -1,31 +0,0 @@
import appContext from "../components/app_context.js";
import type { ConfirmDialogOptions, ConfirmDialogResult, ConfirmWithMessageOptions } from "../widgets/dialogs/confirm.js";
import type { PromptDialogOptions } from "../widgets/dialogs/prompt.js";
async function info(message: string) {
return new Promise((res) => appContext.triggerCommand("showInfoDialog", { message, callback: res }));
}
async function confirm(message: string) {
return new Promise((res) =>
appContext.triggerCommand("showConfirmDialog", <ConfirmWithMessageOptions>{
message,
callback: (x: false | ConfirmDialogOptions) => res(x && x.confirmed)
})
);
}
async function confirmDeleteNoteBoxWithNote(title: string) {
return new Promise<ConfirmDialogResult | undefined>((res) => appContext.triggerCommand("showConfirmDeleteNoteBoxWithNoteDialog", { title, callback: res }));
}
async function prompt(props: PromptDialogOptions) {
return new Promise<string | null>((res) => appContext.triggerCommand("showPromptDialog", { ...props, callback: res }));
}
export default {
info,
confirm,
confirmDeleteNoteBoxWithNote,
prompt
};

View File

@@ -1,52 +0,0 @@
import type FNote from "../entities/fnote.js";
import { getCurrentLanguage } from "./i18n.js";
import { applySyntaxHighlight } from "./syntax_highlight.js";
export default function renderDoc(note: FNote) {
return new Promise<JQuery<HTMLElement>>((resolve) => {
let docName = note.getLabelValue("docName");
const $content = $("<div>");
if (docName) {
// find doc based on language
const url = getUrl(docName, getCurrentLanguage());
$content.load(url, (response, status) => {
// fallback to english doc if no translation available
if (status === "error") {
const fallbackUrl = getUrl(docName, "en");
$content.load(fallbackUrl, () => {
processContent(fallbackUrl, $content)
resolve($content);
});
return;
}
processContent(url, $content);
resolve($content);
});
} else {
resolve($content);
}
return $content;
});
}
function processContent(url: string, $content: JQuery<HTMLElement>) {
const dir = url.substring(0, url.lastIndexOf("/"));
// Images are relative to the docnote but that will not work when rendered in the application since the path breaks.
$content.find("img").each((i, el) => {
const $img = $(el);
$img.attr("src", dir + "/" + $img.attr("src"));
});
applySyntaxHighlight($content);
}
function getUrl(docNameValue: string, language: string) {
// Cannot have spaces in the URL due to how JQuery.load works.
docNameValue = docNameValue.replaceAll(" ", "%20");
return `${window.glob.appPath}/doc_notes/${language}/${docNameValue}.html`;
}

View File

@@ -1,62 +0,0 @@
import { lint } from "./eslint.js";
import { trimIndentation } from "@triliumnext/commons";
import { describe, expect, it } from "vitest";
describe("Linter", () => {
it("reports some basic errors", async () => {
const result = await lint(trimIndentation`
for (const i = 0; i<10; i++) {
}
`, "application/javascript;env=frontend");
expect(result).toMatchObject([
{ message: "'i' is constant.", },
{ message: "Empty block statement." }
]);
});
it("reports no error for correct script", async () => {
const result = await lint(trimIndentation`
const foo = "bar";
console.log(foo.toString());
for (const x of [ 1, 2, 3]) {
console.log(x?.toString());
}
api.showMessage("Hi");
`, "application/javascript;env=frontend");
expect(result.length).toBe(0);
});
it("reports unused functions as warnings", async () => {
const result = await lint(trimIndentation`
function hello() { }
function world() { }
console.log("Hello world");
`, "application/javascript;env=frontend");
expect(result).toMatchObject([
{
message: "'hello' is defined but never used.",
severity: 1
},
{
message: "'world' is defined but never used.",
severity: 1
}
]);
});
it("supports JQuery global", async () => {
expect(await lint(`$("<div>");`, "application/javascript;env=backend")).toMatchObject([{ "ruleId": "no-undef" }]);
expect(await lint(`console.log($("<div>"));`, "application/javascript;env=frontend")).toStrictEqual([]);
});
it("supports module.exports", async () => {
expect(await lint(`module.exports("Hi");`, "application/javascript;env=backend")).toStrictEqual([]);
expect(await lint(`module.exports("Hi");`, "application/javascript;env=frontend")).toStrictEqual([]);
});
it("ignores TypeScript file", async () => {
expect(await lint("export async function lint(code: string, mimeType: string) {}", "text/typescript-jsx")).toStrictEqual([]);
});
});

View File

@@ -1,40 +0,0 @@
export async function lint(code: string, mimeType: string) {
const Linter = (await import("eslint-linter-browserify")).Linter;
const js = (await import("@eslint/js"));
const globalDefinitions = (await import("globals"));
let globals: Record<string, any> = {
...globalDefinitions.browser,
api: "readonly",
module: "readonly"
};
// Unsupported languages
if (mimeType.startsWith("text/typescript")) {
return [];
}
// Custom globals
if (mimeType === "application/javascript;env=frontend") {
globals = { ...globals, ...globalDefinitions.jquery };
} else if (mimeType === "application/javascript;env=backend") {
}
return new Linter().verify(code, [
js.configs.recommended,
{
languageOptions: {
parserOptions: {
ecmaVersion: 2024
},
globals
},
rules: {
"no-unused-vars": [ "warn", { vars: "local", args: "after-used" }]
}
}
]);
}

View File

@@ -1,63 +0,0 @@
import ws from "./ws.js";
import appContext from "../components/app_context.js";
// TODO: Deduplicate
interface Message {
type: string;
entityType: string;
entityId: string;
lastModifiedMs: number;
filePath: string;
}
const fileModificationStatus: Record<string, Record<string, Message>> = {
notes: {},
attachments: {}
};
function checkType(type: string) {
if (type !== "notes" && type !== "attachments") {
throw new Error(`Unrecognized type '${type}', should be 'notes' or 'attachments'`);
}
}
function getFileModificationStatus(entityType: string, entityId: string) {
checkType(entityType);
return fileModificationStatus[entityType][entityId];
}
function fileModificationUploaded(entityType: string, entityId: string) {
checkType(entityType);
delete fileModificationStatus[entityType][entityId];
}
function ignoreModification(entityType: string, entityId: string) {
checkType(entityType);
delete fileModificationStatus[entityType][entityId];
}
ws.subscribeToMessages(async (message: Message) => {
if (message.type !== "openedFileUpdated") {
return;
}
checkType(message.entityType);
fileModificationStatus[message.entityType][message.entityId] = message;
appContext.triggerEvent("openedFileUpdated", {
entityType: message.entityType,
entityId: message.entityId,
lastModifiedMs: message.lastModifiedMs,
filePath: message.filePath
});
});
export default {
getFileModificationStatus,
fileModificationUploaded,
ignoreModification
};

View File

@@ -1,24 +0,0 @@
import type FAttachment from "../entities/fattachment.js";
import type FAttribute from "../entities/fattribute.js";
import type FBlob from "../entities/fblob.js";
import type FBranch from "../entities/fbranch.js";
import type FNote from "../entities/fnote.js";
export interface Froca {
notes: Record<string, FNote>;
branches: Record<string, FBranch>;
attributes: Record<string, FAttribute>;
attachments: Record<string, FAttachment>;
blobPromises: Record<string, Promise<void | FBlob> | null>;
getBlob(entityType: string, entityId: string): Promise<FBlob | null>;
getNote(noteId: string, silentNotFoundError?: boolean): Promise<FNote | null>;
getNoteFromCache(noteId: string): FNote;
getNotesFromCache(noteIds: string[], silentNotFoundError?: boolean): FNote[];
getNotes(noteIds: string[], silentNotFoundError?: boolean): Promise<FNote[]>;
getBranch(branchId: string, silentNotFoundError?: boolean): FBranch | undefined;
getBranches(branchIds: string[], silentNotFoundError?: boolean): FBranch[];
getAttachmentsForNote(noteId: string): Promise<FAttachment[]>;
}

View File

@@ -1,398 +0,0 @@
import FBranch, { type FBranchRow } from "../entities/fbranch.js";
import FNote, { type FNoteRow } from "../entities/fnote.js";
import FAttribute, { type FAttributeRow } from "../entities/fattribute.js";
import server from "./server.js";
import appContext from "../components/app_context.js";
import FBlob, { type FBlobRow } from "../entities/fblob.js";
import FAttachment, { type FAttachmentRow } from "../entities/fattachment.js";
import type { Froca } from "./froca-interface.js";
interface SubtreeResponse {
notes: FNoteRow[];
branches: FBranchRow[];
attributes: FAttributeRow[];
}
interface SearchNoteResponse {
searchResultNoteIds: string[];
highlightedTokens: string[];
error: string | null;
}
/**
* Froca (FROntend CAche) keeps a read only cache of note tree structure in frontend's memory.
* - notes are loaded lazily when unknown noteId is requested
* - when note is loaded, all its parent and child branches are loaded as well. For a branch to be used, it's not must be loaded before
* - deleted notes are present in the cache as well, but they don't have any branches. As a result check for deleted branch is done by presence check - if the branch is not there even though the corresponding note has been loaded, we can infer it is deleted.
*
* Note and branch deletions are corner cases and usually not needed.
*
* Backend has a similar cache called Becca
*/
class FrocaImpl implements Froca {
initializedPromise: Promise<void>;
notes!: Record<string, FNote>;
branches!: Record<string, FBranch>;
attributes!: Record<string, FAttribute>;
attachments!: Record<string, FAttachment>;
blobPromises!: Record<string, Promise<FBlob> | null>;
constructor() {
this.initializedPromise = this.loadInitialTree();
}
async loadInitialTree() {
const resp = await server.get<SubtreeResponse>("tree");
// clear the cache only directly before adding new content which is important for e.g., switching to protected session
this.notes = {};
this.branches = {};
this.attributes = {};
this.attachments = {};
this.blobPromises = {};
this.addResp(resp);
}
async loadSubTree(subTreeNoteId: string) {
const resp = await server.get<SubtreeResponse>(`tree?subTreeNoteId=${subTreeNoteId}`);
this.addResp(resp);
return this.notes[subTreeNoteId];
}
addResp(resp: SubtreeResponse) {
const noteRows = resp.notes;
const branchRows = resp.branches;
const attributeRows = resp.attributes;
const noteIdsToSort = new Set<string>();
for (const noteRow of noteRows) {
const { noteId } = noteRow;
let note = this.notes[noteId];
if (note) {
note.update(noteRow);
// search note doesn't have child branches in the database and all the children are virtual branches
if (note.type !== "search") {
for (const childNoteId of note.children) {
const childNote = this.notes[childNoteId];
if (childNote) {
childNote.parents = childNote.parents.filter((p) => p !== noteId);
delete this.branches[childNote.parentToBranch[noteId]];
delete childNote.parentToBranch[noteId];
}
}
note.children = [];
note.childToBranch = {};
}
// we want to remove all "real" branches (represented in the database) since those will be created
// from branches argument but want to preserve all virtual ones from saved search
note.parents = note.parents.filter((parentNoteId) => {
const parentNote = this.notes[parentNoteId];
const branch = this.branches[parentNote.childToBranch[noteId]];
if (!parentNote || !branch) {
return false;
}
if (branch.fromSearchNote) {
return true;
}
parentNote.children = parentNote.children.filter((p) => p !== noteId);
delete this.branches[parentNote.childToBranch[noteId]];
delete parentNote.childToBranch[noteId];
return false;
});
} else {
this.notes[noteId] = new FNote(this, noteRow);
}
}
for (const branchRow of branchRows) {
const branch = new FBranch(this, branchRow);
this.branches[branch.branchId] = branch;
const childNote = this.notes[branch.noteId];
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId, false);
}
const parentNote = this.notes[branch.parentNoteId];
if (parentNote) {
parentNote.addChild(branch.noteId, branch.branchId, false);
noteIdsToSort.add(parentNote.noteId);
}
}
for (const attributeRow of attributeRows) {
const { attributeId } = attributeRow;
this.attributes[attributeId] = new FAttribute(this, attributeRow);
const note = this.notes[attributeRow.noteId];
if (note && !note.attributes.includes(attributeId)) {
note.attributes.push(attributeId);
}
if (attributeRow.type === "relation") {
const targetNote = this.notes[attributeRow.value];
if (targetNote) {
if (!targetNote.targetRelations.includes(attributeId)) {
targetNote.targetRelations.push(attributeId);
}
}
}
}
// sort all of them at once, this avoids repeated sorts (#1480)
for (const noteId of noteIdsToSort) {
this.notes[noteId].sortChildren();
this.notes[noteId].sortParents();
}
}
async reloadNotes(noteIds: string[]) {
if (noteIds.length === 0) {
return;
}
noteIds = Array.from(new Set(noteIds)); // make noteIds unique
const resp = await server.post<SubtreeResponse>("tree/load", { noteIds });
this.addResp(resp);
appContext.triggerEvent("notesReloaded", { noteIds });
}
async loadSearchNote(noteId: string) {
const note = await this.getNote(noteId);
if (!note || note.type !== "search") {
return;
}
const { searchResultNoteIds, highlightedTokens, error } = await server.get<SearchNoteResponse>(`search-note/${note.noteId}`);
if (!Array.isArray(searchResultNoteIds)) {
throw new Error(`Search note '${note.noteId}' failed: ${searchResultNoteIds}`);
}
// reset all the virtual branches from old search results
if (note.noteId in froca.notes) {
froca.notes[note.noteId].children = [];
froca.notes[note.noteId].childToBranch = {};
}
const branches: FBranchRow[] = [...note.getParentBranches(), ...note.getChildBranches()];
searchResultNoteIds.forEach((resultNoteId, index) =>
branches.push({
// branchId should be repeatable since sometimes we reload some notes without rerendering the tree
branchId: `virt-${note.noteId}-${resultNoteId}`,
noteId: resultNoteId,
parentNoteId: note.noteId,
notePosition: (index + 1) * 10,
fromSearchNote: true
})
);
// update this note with standard (parent) branches + virtual (children) branches
this.addResp({
notes: [note],
branches,
attributes: []
});
froca.notes[note.noteId].searchResultsLoaded = true;
froca.notes[note.noteId].highlightedTokens = highlightedTokens;
return { error };
}
getNotesFromCache(noteIds: string[], silentNotFoundError = false): FNote[] {
return noteIds
.map((noteId) => {
if (!this.notes[noteId] && !silentNotFoundError) {
console.trace(`Can't find note '${noteId}'`);
return null;
} else {
return this.notes[noteId];
}
})
.filter((note) => !!note) as FNote[];
}
async getNotes(noteIds: string[] | JQuery<string>, silentNotFoundError = false): Promise<FNote[]> {
noteIds = Array.from(new Set(noteIds)); // make unique
const missingNoteIds = noteIds.filter((noteId) => !this.notes[noteId]);
await this.reloadNotes(missingNoteIds);
return noteIds
.map((noteId) => {
if (!this.notes[noteId] && !silentNotFoundError) {
console.trace(`Can't find note '${noteId}'`);
return null;
} else {
return this.notes[noteId];
}
})
.filter((note) => !!note) as FNote[];
}
async noteExists(noteId: string): Promise<boolean> {
const notes = await this.getNotes([noteId], true);
return notes.length === 1;
}
async getNote(noteId: string, silentNotFoundError = false): Promise<FNote | null> {
if (noteId === "none") {
console.trace(`No 'none' note.`);
return null;
} else if (!noteId) {
console.trace(`Falsy noteId '${noteId}', returning null.`);
return null;
}
return (await this.getNotes([noteId], silentNotFoundError))[0];
}
getNoteFromCache(noteId: string) {
if (!noteId) {
throw new Error("Empty noteId");
}
return this.notes[noteId];
}
getBranches(branchIds: string[], silentNotFoundError = false): FBranch[] {
return branchIds.map((branchId) => this.getBranch(branchId, silentNotFoundError)).filter((b) => !!b) as FBranch[];
}
getBranch(branchId: string, silentNotFoundError = false) {
if (!(branchId in this.branches)) {
if (!silentNotFoundError) {
logError(`Not existing branch '${branchId}'`);
}
} else {
return this.branches[branchId];
}
}
async getBranchId(parentNoteId: string, childNoteId: string) {
if (childNoteId === "root") {
return "none_root";
}
const child = await this.getNote(childNoteId);
if (!child) {
logError(`Could not find branchId for parent '${parentNoteId}', child '${childNoteId}' since child does not exist`);
return null;
}
return child.parentToBranch[parentNoteId];
}
async getAttachment(attachmentId: string, silentNotFoundError = false) {
const attachment = this.attachments[attachmentId];
if (attachment) {
return attachment;
}
// load all attachments for the given note even if one is requested, don't load one by one
let attachmentRows;
try {
attachmentRows = await server.getWithSilentNotFound<FAttachmentRow[]>(`attachments/${attachmentId}/all`);
} catch (e: any) {
if (silentNotFoundError) {
logInfo(`Attachment '${attachmentId}' not found, but silentNotFoundError is enabled: ` + e.message);
return null;
} else {
throw e;
}
}
const attachments = this.processAttachmentRows(attachmentRows);
if (attachments.length) {
attachments[0].getNote().attachments = attachments;
}
return this.attachments[attachmentId];
}
async getAttachmentsForNote(noteId: string) {
const attachmentRows = await server.get<FAttachmentRow[]>(`notes/${noteId}/attachments`);
return this.processAttachmentRows(attachmentRows);
}
processAttachmentRows(attachmentRows: FAttachmentRow[]): FAttachment[] {
return attachmentRows.map((attachmentRow) => {
let attachment;
if (attachmentRow.attachmentId in this.attachments) {
attachment = this.attachments[attachmentRow.attachmentId];
attachment.update(attachmentRow);
} else {
attachment = new FAttachment(this, attachmentRow);
this.attachments[attachment.attachmentId] = attachment;
}
return attachment;
});
}
async getBlob(entityType: string, entityId: string): Promise<FBlob | null> {
// I'm not sure why we're not using blobIds directly, it would save us this composite key ...
// perhaps one benefit is that we're always requesting the latest blob, not relying on perhaps faulty/slow
// websocket update?
const key = `${entityType}-${entityId}`;
if (!this.blobPromises[key]) {
this.blobPromises[key] = server
.get<FBlobRow>(`${entityType}/${entityId}/blob`)
.then((row) => new FBlob(row))
.catch((e) => {
console.error(`Cannot get blob for ${entityType} '${entityId}'`, e);
return null;
});
// we don't want to keep large payloads forever in memory, so we clean that up quite quickly
// this cache is more meant to share the data between different components within one business transaction (e.g. loading of the note into the tab context and all the components)
// if the blob is updated within the cache lifetime, it should be invalidated by froca_updater
this.blobPromises[key]?.then(() => setTimeout(() => (this.blobPromises[key] = null), 1000));
}
return await this.blobPromises[key];
}
}
const froca = new FrocaImpl();
export default froca;

View File

@@ -1,311 +0,0 @@
import LoadResults from "./load_results.js";
import froca from "./froca.js";
import utils from "./utils.js";
import options from "./options.js";
import noteAttributeCache from "./note_attribute_cache.js";
import FBranch, { type FBranchRow } from "../entities/fbranch.js";
import FAttribute, { type FAttributeRow } from "../entities/fattribute.js";
import FAttachment, { type FAttachmentRow } from "../entities/fattachment.js";
import type { default as FNote, FNoteRow } from "../entities/fnote.js";
import type { EntityChange } from "../server_types.js";
async function processEntityChanges(entityChanges: EntityChange[]) {
const loadResults = new LoadResults(entityChanges);
for (const ec of entityChanges) {
try {
if (ec.entityName === "notes") {
processNoteChange(loadResults, ec);
} else if (ec.entityName === "branches") {
await processBranchChange(loadResults, ec);
} else if (ec.entityName === "attributes") {
processAttributeChange(loadResults, ec);
} else if (ec.entityName === "note_reordering") {
processNoteReordering(loadResults, ec);
} else if (ec.entityName === "revisions") {
loadResults.addRevision(ec.entityId, ec.noteId, ec.componentId);
} else if (ec.entityName === "options") {
const attributeEntity = ec.entity as FAttributeRow;
if (attributeEntity.name === "openNoteContexts") {
continue; // only noise
}
options.set(attributeEntity.name, attributeEntity.value);
loadResults.addOption(attributeEntity.name);
} else if (ec.entityName === "attachments") {
processAttachment(loadResults, ec);
} else if (ec.entityName === "blobs" || ec.entityName === "etapi_tokens") {
// NOOP
} else {
throw new Error(`Unknown entityName '${ec.entityName}'`);
}
} catch (e: any) {
throw new Error(`Can't process entity ${JSON.stringify(ec)} with error ${e.message} ${e.stack}`);
}
}
// froca is supposed to contain all notes currently being visible to the users in the tree / otherwise being processed
// and their complete "ancestor relationship", so it's always possible to go up in the hierarchy towards the root.
// To this we count: standard parent-child relationships and template/inherit relations (attribute inheritance follows them).
// Here we watch for changes which might violate this principle - e.g., an introduction of a new "inherit" relation might
// mean we need to load the target of the relation (and then perhaps transitively the whole note path of this target).
const missingNoteIds = [];
for (const { entityName, entity } of entityChanges) {
if (!entity) {
// if erased
continue;
}
if (entityName === "branches" && !((entity as FBranchRow).parentNoteId in froca.notes)) {
missingNoteIds.push((entity as FBranchRow).parentNoteId);
} else if (entityName === "attributes") {
let attributeEntity = entity as FAttributeRow;
if (attributeEntity.type === "relation" && (attributeEntity.name === "template" || attributeEntity.name === "inherit") && !(attributeEntity.value in froca.notes)) {
missingNoteIds.push(attributeEntity.value);
}
}
}
if (missingNoteIds.length > 0) {
await froca.reloadNotes(missingNoteIds);
}
if (!loadResults.isEmpty()) {
if (loadResults.hasAttributeRelatedChanges()) {
noteAttributeCache.invalidate();
}
// TODO: Remove after porting the file
// @ts-ignore
const appContext = (await import("../components/app_context.js")).default as any;
await appContext.triggerEvent("entitiesReloaded", { loadResults });
}
}
function processNoteChange(loadResults: LoadResults, ec: EntityChange) {
const note = froca.notes[ec.entityId];
if (!note) {
// if this note has not been requested before then it's not part of froca's cached subset, and
// we're not interested in it
return;
}
loadResults.addNote(ec.entityId, ec.componentId);
if (ec.isErased && ec.entityId in froca.notes) {
utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
return;
}
if (ec.isErased || ec.entity?.isDeleted) {
delete froca.notes[ec.entityId];
} else {
if (note.blobId !== (ec.entity as FNoteRow).blobId) {
for (const key of Object.keys(froca.blobPromises)) {
if (key.includes(note.noteId)) {
delete froca.blobPromises[key];
}
}
if (ec.componentId) {
loadResults.addNoteContent(note.noteId, ec.componentId);
}
}
note.update(ec.entity as FNoteRow);
}
}
async function processBranchChange(loadResults: LoadResults, ec: EntityChange) {
if (ec.isErased && ec.entityId in froca.branches) {
utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
return;
}
let branch = froca.branches[ec.entityId];
if (ec.isErased || ec.entity?.isDeleted) {
if (branch) {
const childNote = froca.notes[branch.noteId];
const parentNote = froca.notes[branch.parentNoteId];
if (childNote) {
childNote.parents = childNote.parents.filter((parentNoteId) => parentNoteId !== branch.parentNoteId);
delete childNote.parentToBranch[branch.parentNoteId];
}
if (parentNote) {
parentNote.children = parentNote.children.filter((childNoteId) => childNoteId !== branch.noteId);
delete parentNote.childToBranch[branch.noteId];
}
if (ec.componentId) {
loadResults.addBranch(ec.entityId, ec.componentId);
}
delete froca.branches[ec.entityId];
}
return;
}
if (ec.componentId) {
loadResults.addBranch(ec.entityId, ec.componentId);
}
const branchEntity = ec.entity as FBranchRow;
const childNote = froca.notes[branchEntity.noteId];
let parentNote: FNote | null = froca.notes[branchEntity.parentNoteId];
if (childNote && !childNote.isRoot() && !parentNote) {
// a branch cannot exist without the parent
// a note loaded into froca has to also contain all its ancestors,
// this problem happened, e.g., in sharing where _share was hidden and thus not loaded
// sharing meant cloning into _share, which crashed because _share was not loaded
parentNote = await froca.getNote(branchEntity.parentNoteId);
}
if (branch) {
branch.update(ec.entity as FBranch);
} else if (childNote || parentNote) {
froca.branches[ec.entityId] = branch = new FBranch(froca, branchEntity);
}
if (childNote) {
childNote.addParent(branch.parentNoteId, branch.branchId);
}
if (parentNote) {
parentNote.addChild(branch.noteId, branch.branchId);
}
}
function processNoteReordering(loadResults: LoadResults, ec: EntityChange) {
const parentNoteIdsToSort = new Set<string>();
for (const branchId in ec.positions) {
const branch = froca.branches[branchId];
if (branch) {
branch.notePosition = ec.positions[branchId];
parentNoteIdsToSort.add(branch.parentNoteId);
}
}
for (const parentNoteId of parentNoteIdsToSort) {
const parentNote = froca.notes[parentNoteId];
if (parentNote) {
parentNote.sortChildren();
}
}
if (ec.componentId) {
loadResults.addNoteReordering(ec.entityId, ec.componentId);
}
}
function processAttributeChange(loadResults: LoadResults, ec: EntityChange) {
let attribute = froca.attributes[ec.entityId];
if (ec.isErased && ec.entityId in froca.attributes) {
utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
return;
}
if (ec.isErased || ec.entity?.isDeleted) {
if (attribute) {
const sourceNote = froca.notes[attribute.noteId];
const targetNote = attribute.type === "relation" && froca.notes[attribute.value];
if (sourceNote) {
sourceNote.attributes = sourceNote.attributes.filter((attributeId) => attributeId !== attribute.attributeId);
}
if (targetNote) {
targetNote.targetRelations = targetNote.targetRelations.filter((attributeId) => attributeId !== attribute.attributeId);
}
if (ec.componentId) {
loadResults.addAttribute(ec.entityId, ec.componentId);
}
delete froca.attributes[ec.entityId];
}
return;
}
if (ec.componentId) {
loadResults.addAttribute(ec.entityId, ec.componentId);
}
const attributeEntity = ec.entity as FAttributeRow;
const sourceNote = froca.notes[attributeEntity.noteId];
const targetNote = attributeEntity.type === "relation" && froca.notes[attributeEntity.value];
if (attribute) {
attribute.update(ec.entity as FAttributeRow);
} else if (sourceNote || targetNote) {
attribute = new FAttribute(froca, ec.entity as FAttributeRow);
froca.attributes[attribute.attributeId] = attribute;
if (sourceNote && !sourceNote.attributes.includes(attribute.attributeId)) {
sourceNote.attributes.push(attribute.attributeId);
}
if (targetNote && !targetNote.targetRelations.includes(attribute.attributeId)) {
targetNote.targetRelations.push(attribute.attributeId);
}
}
}
function processAttachment(loadResults: LoadResults, ec: EntityChange) {
if (ec.isErased && ec.entityId in froca.attachments) {
utils.reloadFrontendApp(`${ec.entityName} '${ec.entityId}' is erased, need to do complete reload.`);
return;
}
const attachment = froca.attachments[ec.entityId];
const attachmentEntity = ec.entity as FAttachmentRow;
if (ec.isErased || (ec.entity as any)?.isDeleted) {
if (attachment) {
const note = attachment.getNote();
if (note && note.attachments) {
note.attachments = note.attachments.filter((att) => att.attachmentId !== attachment.attachmentId);
}
loadResults.addAttachmentRow(attachmentEntity);
delete froca.attachments[ec.entityId];
}
return;
}
if (ec.entity) {
if (attachment) {
attachment.update(ec.entity as FAttachmentRow);
} else {
const attachmentRow = ec.entity as FAttachmentRow;
const note = froca.notes[attachmentRow.ownerId];
if (note?.attachments) {
note.attachments.push(new FAttachment(froca, attachmentRow));
}
}
}
loadResults.addAttachmentRow(attachmentEntity);
}
export default {
processEntityChanges
};

View File

@@ -1,720 +0,0 @@
import server from "./server.js";
import utils from "./utils.js";
import toastService from "./toast.js";
import linkService from "./link.js";
import froca from "./froca.js";
import noteTooltipService from "./note_tooltip.js";
import protectedSessionService from "./protected_session.js";
import dateNotesService from "./date_notes.js";
import searchService from "./search.js";
import RightPanelWidget from "../widgets/right_panel_widget.js";
import ws from "./ws.js";
import appContext from "../components/app_context.js";
import NoteContextAwareWidget from "../widgets/note_context_aware_widget.js";
import BasicWidget from "../widgets/basic_widget.js";
import SpacedUpdate from "./spaced_update.js";
import shortcutService from "./shortcuts.js";
import dialogService from "./dialog.js";
import type FNote from "../entities/fnote.js";
import { t } from "./i18n.js";
import dayjs from "dayjs";
import type NoteContext from "../components/note_context.js";
import type NoteDetailWidget from "../widgets/note_detail.js";
import type Component from "../components/component.js";
/**
* A whole number
* @typedef {number} int
*/
/**
* An instance of the frontend api available globally.
* @global
* @var {FrontendScriptApi} api
*/
interface AddToToolbarOpts {
title: string;
/** callback handling the click on the button */
action: () => void;
/** id of the button, used to identify the old instances of this button to be replaced
* ID is optional because of BC, but not specifying it is deprecated. ID can be alphanumeric only. */
id: string;
/** name of the boxicon to be used (e.g. "time" for "bx-time" icon) */
icon: string;
/** keyboard shortcut for the button, e.g. "alt+t" */
shortcut: string;
}
// TODO: Deduplicate me with the server.
interface ExecResult {
success: boolean;
executionResult: unknown;
error?: string;
}
export interface Entity {
noteId: string;
}
type Func = ((...args: unknown[]) => unknown) | string;
export interface Api {
/**
* Container of all the rendered script content
* */
$container: JQuery<HTMLElement> | null;
/**
* Note where the script started executing, i.e., the (event) entrypoint of the current script execution.
*/
startNote: FNote;
/**
* Note where the script is currently executing, i.e. the note where the currently executing source code is written.
*/
currentNote: FNote;
/**
* Entity whose event triggered this execution.
*/
originEntity: unknown | null;
/**
* day.js library for date manipulation.
* See {@link https://day.js.org} for documentation
* @see https://day.js.org
*/
dayjs: typeof dayjs;
RightPanelWidget: typeof RightPanelWidget;
NoteContextAwareWidget: typeof NoteContextAwareWidget;
BasicWidget: typeof BasicWidget;
/**
* Activates note in the tree and in the note detail.
*
* @param notePath (or noteId)
*/
activateNote(notePath: string): Promise<void>;
/**
* Activates newly created note. Compared to this.activateNote() also makes sure that frontend has been fully synced.
*
* @param notePath (or noteId)
*/
activateNewNote(notePath: string): Promise<void>;
/**
* Open a note in a new tab.
*
* @method
* @param notePath (or noteId)
* @param activate - set to true to activate the new tab, false to stay on the current tab
*/
openTabWithNote(notePath: string, activate: boolean): Promise<void>;
/**
* Open a note in a new split.
*
* @param notePath (or noteId)
* @param activate - set to true to activate the new split, false to stay on the current split
*/
openSplitWithNote(notePath: string, activate: boolean): Promise<void>;
/**
* Adds a new launcher to the launchbar. If the launcher (id) already exists, it will be updated.
*
* @method
* @deprecated you can now create/modify launchers in the top-left Menu -> Configure Launchbar
* for special needs there's also backend API's createOrUpdateLauncher()
*/
addButtonToToolbar(opts: AddToToolbarOpts): void;
/**
* @private
*/
__runOnBackendInner(func: unknown, params: unknown[], transactional: boolean): unknown;
/**
* Executes given anonymous function on the backend.
* Internally this serializes the anonymous function into string and sends it to backend via AJAX.
* Please make sure that the supplied function is synchronous. Only sync functions will work correctly
* with transaction management. If you really know what you're doing, you can call api.runAsyncOnBackendWithManualTransactionHandling()
*
* @method
* @param func - (synchronous) function to be executed on the backend
* @param params - list of parameters to the anonymous function to be sent to backend
* @returns return value of the executed function on the backend
*/
runOnBackend(func: Func, params: unknown[]): unknown;
/**
* Executes given anonymous function on the backend.
* Internally this serializes the anonymous function into string and sends it to backend via AJAX.
* This function is meant for advanced needs where an async function is necessary.
* In this case, the automatic request-scoped transaction management is not applied,
* and you need to manually define transaction via api.transactional().
*
* If you have a synchronous function, please use api.runOnBackend().
*
* @method
* @param func - (synchronous) function to be executed on the backend
* @param params - list of parameters to the anonymous function to be sent to backend
* @returns return value of the executed function on the backend
*/
runAsyncOnBackendWithManualTransactionHandling(func: Func, params: unknown[]): unknown;
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See full documentation for all options at: https://triliumnext.github.io/Docs/Wiki/search.html
*/
searchForNotes(searchString: string): Promise<FNote[]>;
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See full documentation for all options at: https://triliumnext.github.io/Docs/Wiki/search.html
*/
searchForNote(searchString: string): Promise<FNote | null>;
/**
* Returns note by given noteId. If note is missing from the cache, it's loaded.
*/
getNote(noteId: string): Promise<FNote | null>;
/**
* Returns list of notes. If note is missing from the cache, it's loaded.
*
* This is often used to bulk-fill the cache with notes which would have to be picked one by one
* otherwise (by e.g. createLink())
*
* @param [silentNotFoundError] - don't report error if the note is not found
*/
getNotes(noteIds: string[], silentNotFoundError: boolean): Promise<FNote[]>;
/**
* Update frontend tree (note) cache from the backend.
*/
reloadNotes(noteIds: string[]): Promise<void>;
/**
* Instance name identifies particular Trilium instance. It can be useful for scripts
* if some action needs to happen on only one specific instance.
*/
getInstanceName(): string;
/**
* @returns date in YYYY-MM-DD format
*/
formatDateISO: typeof utils.formatDateISO;
parseDate: typeof utils.parseDate;
/**
* Show an info toast message to the user.
*/
showMessage: typeof toastService.showMessage;
/**
* Show an error toast message to the user.
*/
showError: typeof toastService.showError;
/**
* Show an info dialog to the user.
*/
showInfoDialog: typeof dialogService.info;
/**
* Show confirm dialog to the user.
* @returns promise resolving to true if the user confirmed
*/
showConfirmDialog: typeof dialogService.confirm;
/**
* Show prompt dialog to the user.
*
* @returns promise resolving to the answer provided by the user
*/
showPromptDialog: typeof dialogService.prompt;
/**
* Trigger command. This is a very low-level API which should be avoided if possible.
*/
triggerCommand: typeof appContext.triggerCommand;
/**
* Trigger event. This is a very low-level API which should be avoided if possible.
*/
triggerEvent: typeof appContext.triggerEvent;
/**
* Create a note link (jQuery object) for given note.
*
* @param {string} notePath (or noteId)
* @param {object} [params]
* @param {boolean} [params.showTooltip] - enable/disable tooltip on the link
* @param {boolean} [params.showNotePath] - show also whole note's path as part of the link
* @param {boolean} [params.showNoteIcon] - show also note icon before the title
* @param {string} [params.title] - custom link tile with note's title as default
* @param {string} [params.title=] - custom link tile with note's title as default
* @returns {jQuery} - jQuery element with the link (wrapped in <span>)
*/
createLink: typeof linkService.createLink;
/** @deprecated - use api.createLink() instead */
createNoteLink: typeof linkService.createLink;
/**
* Adds given text to the editor cursor
*
* @param text - this must be clear text, HTML is not supported.
*/
addTextToActiveContextEditor(text: string): void;
/**
* @returns active note (loaded into center pane)
*/
getActiveContextNote(): FNote;
/**
* @returns returns active context (split)
*/
getActiveContext(): NoteContext;
/**
* @returns returns active main context (first split in a tab, represents the tab as a whole)
*/
getActiveMainContext(): NoteContext;
/**
* @returns returns all note contexts (splits) in all tabs
*/
getNoteContexts(): NoteContext[];
/**
* @returns returns all main contexts representing tabs
*/
getMainNoteContexts(): NoteContext[];
/**
* See https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html for documentation on the returned instance.
*
* @returns {Promise<BalloonEditor>} instance of CKEditor
*/
getActiveContextTextEditor(): Promise<unknown>;
/**
* See https://codemirror.net/doc/manual.html#api
*
* @method
* @returns instance of CodeMirror
*/
getActiveContextCodeEditor(): Promise<unknown>;
/**
* Get access to the widget handling note detail. Methods like `getWidgetType()` and `getTypeWidget()` to get to the
* implementation of actual widget type.
*/
getActiveNoteDetailWidget(): Promise<NoteDetailWidget>;
/**
* @returns returns a note path of active note or null if there isn't active note
*/
getActiveContextNotePath(): string | null;
/**
* Returns component which owns the given DOM element (the nearest parent component in DOM tree)
*
* @method
* @param el DOM element
*/
getComponentByEl(el: HTMLElement): Component;
/**
* @param {object} $el - jquery object on which to set up the tooltip
*/
setupElementTooltip: typeof noteTooltipService.setupElementTooltip;
/**
* @param {boolean} protect - true to protect note, false to unprotect
*/
protectNote: typeof protectedSessionService.protectNote;
/**
* @param noteId
* @param protect - true to protect subtree, false to unprotect
*/
protectSubTree: typeof protectedSessionService.protectNote;
/**
* Returns date-note for today. If it doesn't exist, it is automatically created.
*/
getTodayNote: typeof dateNotesService.getTodayNote;
/**
* Returns day note for a given date. If it doesn't exist, it is automatically created.
*
* @param date - e.g. "2019-04-29"
*/
getDayNote: typeof dateNotesService.getDayNote;
/**
* Returns day note for the first date of the week of the given date. If it doesn't exist, it is automatically created.
*
* @param date - e.g. "2019-04-29"
*/
getWeekFirstDayNote: typeof dateNotesService.getWeekFirstDayNote;
/**
* Returns week note for given date. If such a note doesn't exist, it is automatically created.
*
* @param date in YYYY-MM-DD format
* @param rootNote - specify calendar root note, normally leave empty to use the default calendar
*/
getWeekNote: typeof dateNotesService.getWeekNote;
/**
* Returns month-note. If it doesn't exist, it is automatically created.
*
* @param month - e.g. "2019-04"
*/
getMonthNote: typeof dateNotesService.getMonthNote;
/**
* Returns quarter note for given date. If such a note doesn't exist, it is automatically created.
*
* @param date in YYYY-MM format
* @param rootNote - specify calendar root note, normally leave empty to use the default calendar
*/
getQuarterNote: typeof dateNotesService.getQuarterNote;
/**
* Returns year-note. If it doesn't exist, it is automatically created.
*
* @method
* @param {string} year - e.g. "2019"
* @returns {Promise<FNote>}
*/
getYearNote: typeof dateNotesService.getYearNote;
/**
* Hoist note in the current tab. See https://triliumnext.github.io/Docs/Wiki/note-hoisting.html
*
* @param {string} noteId - set hoisted note. 'root' will effectively unhoist
*/
setHoistedNoteId(noteId: string): void;
/**
* @param keyboardShortcut - e.g. "ctrl+shift+a"
* @param [namespace] specify namespace of the handler for the cases where call for bind may be repeated.
* If a handler with this ID exists, it's replaced by the new handler.
*/
bindGlobalShortcut: typeof shortcutService.bindGlobalShortcut;
/**
* Trilium runs in a backend and frontend process, when something is changed on the backend from a script,
* frontend will get asynchronously synchronized.
*
* This method returns a promise which resolves once all the backend -> frontend synchronization is finished.
* Typical use case is when a new note has been created, we should wait until it is synced into frontend and only then activate it.
*/
waitUntilSynced: typeof ws.waitForMaxKnownEntityChangeId;
/**
* This will refresh all currently opened notes which have included note specified in the parameter
*
* @param includedNoteId - noteId of the included note
*/
refreshIncludedNote(includedNoteId: string): void;
/**
* Return randomly generated string of given length. This random string generation is NOT cryptographically secure.
*
* @method
* @param length of the string
* @returns random string
*/
randomString: typeof utils.randomString;
/**
* @param size in bytes
* @return formatted string
*/
formatSize: typeof utils.formatSize;
/**
* @param size in bytes
* @return formatted string
* @deprecated - use api.formatSize()
*/
formatNoteSize: typeof utils.formatSize;
logMessages: Record<string, string[]>;
logSpacedUpdates: Record<string, SpacedUpdate>;
/**
* Log given message to the log pane in UI
*/
log(message: string): void;
}
/**
* <p>This is the main frontend API interface for scripts. All the properties and methods are published in the "api" object
* available in the JS frontend notes. You can use e.g. <code>api.showMessage(api.startNote.title);</code></p>
*/
function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, originEntity: Entity | null = null, $container: JQuery<HTMLElement> | null = null) {
this.$container = $container;
this.startNote = startNote;
this.currentNote = currentNote;
this.originEntity = originEntity;
this.dayjs = dayjs;
this.RightPanelWidget = RightPanelWidget;
this.NoteContextAwareWidget = NoteContextAwareWidget;
this.BasicWidget = BasicWidget;
this.activateNote = async (notePath) => {
await appContext.tabManager.getActiveContext()?.setNote(notePath);
};
this.activateNewNote = async (notePath) => {
await ws.waitForMaxKnownEntityChangeId();
await appContext.tabManager.getActiveContext()?.setNote(notePath);
await appContext.triggerEvent("focusAndSelectTitle", {});
};
this.openTabWithNote = async (notePath, activate) => {
await ws.waitForMaxKnownEntityChangeId();
await appContext.tabManager.openTabWithNoteWithHoisting(notePath, { activate });
if (activate) {
await appContext.triggerEvent("focusAndSelectTitle", {});
}
};
this.openSplitWithNote = async (notePath, activate) => {
await ws.waitForMaxKnownEntityChangeId();
const subContexts = appContext.tabManager.getActiveContext()?.getSubContexts();
const { ntxId } = subContexts?.[subContexts.length - 1] ?? {};
await appContext.triggerCommand("openNewNoteSplit", { ntxId, notePath });
if (activate) {
await appContext.triggerEvent("focusAndSelectTitle", {});
}
};
this.addButtonToToolbar = async (opts) => {
console.warn("api.addButtonToToolbar() has been deprecated since v0.58 and may be removed in the future. Use Menu -> Configure Launchbar to create/update launchers instead.");
const { action, ...reqBody } = opts;
await server.put("special-notes/api-script-launcher", {
action: action.toString(),
...reqBody
});
};
function prepareParams(params: unknown[]) {
if (!params) {
return params;
}
return params.map((p) => {
if (typeof p === "function") {
return `!@#Function: ${p.toString()}`;
} else {
return p;
}
});
}
this.__runOnBackendInner = async (func, params, transactional) => {
if (typeof func === "function") {
func = func.toString();
}
const ret = await server.post<ExecResult>(
"script/exec",
{
script: func,
params: prepareParams(params),
startNoteId: startNote.noteId,
currentNoteId: currentNote.noteId,
originEntityName: "notes", // currently there's no other entity on the frontend which can trigger event
originEntityId: originEntity ? originEntity.noteId : null,
transactional
},
"script"
);
if (ret.success) {
await ws.waitForMaxKnownEntityChangeId();
return ret.executionResult;
} else {
throw new Error(`server error: ${ret.error}`);
}
};
this.runOnBackend = async (func, params = []) => {
if (func?.constructor.name === "AsyncFunction" || (typeof func === "string" && func?.startsWith?.("async "))) {
toastService.showError(t("frontend_script_api.async_warning"));
}
return await this.__runOnBackendInner(func, params, true);
};
this.runAsyncOnBackendWithManualTransactionHandling = async (func, params = []) => {
if (func?.constructor.name === "Function" || (typeof func === "string" && func?.startsWith?.("function"))) {
toastService.showError(t("frontend_script_api.sync_warning"));
}
return await this.__runOnBackendInner(func, params, false);
};
this.searchForNotes = async (searchString) => {
return await searchService.searchForNotes(searchString);
};
this.searchForNote = async (searchString) => {
const notes = await this.searchForNotes(searchString);
return notes.length > 0 ? notes[0] : null;
};
this.getNote = async (noteId) => await froca.getNote(noteId);
this.getNotes = async (noteIds, silentNotFoundError = false) => await froca.getNotes(noteIds, silentNotFoundError);
this.reloadNotes = async (noteIds) => await froca.reloadNotes(noteIds);
this.getInstanceName = () => window.glob.instanceName;
this.formatDateISO = utils.formatDateISO;
this.parseDate = utils.parseDate;
this.showMessage = toastService.showMessage;
this.showError = toastService.showError;
this.showInfoDialog = dialogService.info;
this.showConfirmDialog = dialogService.confirm;
this.showPromptDialog = dialogService.prompt;
this.triggerCommand = (name, data) => appContext.triggerCommand(name, data);
this.triggerEvent = (name, data) => appContext.triggerEvent(name, data);
this.createLink = linkService.createLink;
this.createNoteLink = linkService.createLink;
this.addTextToActiveContextEditor = (text) => appContext.triggerCommand("addTextToActiveEditor", { text });
this.getActiveContextNote = (): FNote => {
const note = appContext.tabManager.getActiveContextNote();
if (!note) {
throw new Error("No active context note found");
}
return note;
};
this.getActiveContext = (): NoteContext => {
const context = appContext.tabManager.getActiveContext();
if (!context) {
throw new Error("No active context found");
}
return context;
};
this.getActiveMainContext = (): NoteContext => {
const context = appContext.tabManager.getActiveMainContext();
if (!context) {
throw new Error("No active main context found");
}
return context;
};
this.getNoteContexts = () => appContext.tabManager.getNoteContexts();
this.getMainNoteContexts = () => appContext.tabManager.getMainNoteContexts();
this.getActiveContextTextEditor = () => {
const context = appContext.tabManager.getActiveContext();
if (!context) {
throw new Error("No active context found");
}
return context.getTextEditor();
};
this.getActiveContextCodeEditor = () => {
const context = appContext.tabManager.getActiveContext();
if (!context) {
throw new Error("No active context found");
}
return context.getCodeEditor();
};
this.getActiveNoteDetailWidget = () => new Promise((resolve) => appContext.triggerCommand("executeInActiveNoteDetailWidget", { callback: resolve }));
this.getActiveContextNotePath = () => appContext.tabManager.getActiveContextNotePath();
this.getComponentByEl = (el) => appContext.getComponentByEl(el);
this.setupElementTooltip = noteTooltipService.setupElementTooltip;
this.protectNote = async (noteId, protect) => {
await protectedSessionService.protectNote(noteId, protect, false);
};
this.protectSubTree = async (noteId, protect) => {
await protectedSessionService.protectNote(noteId, protect, true);
};
this.getTodayNote = dateNotesService.getTodayNote;
this.getDayNote = dateNotesService.getDayNote;
this.getWeekFirstDayNote = dateNotesService.getWeekFirstDayNote;
this.getWeekNote = dateNotesService.getWeekNote;
this.getMonthNote = dateNotesService.getMonthNote;
this.getQuarterNote = dateNotesService.getQuarterNote;
this.getYearNote = dateNotesService.getYearNote;
this.setHoistedNoteId = (noteId) => {
const activeNoteContext = appContext.tabManager.getActiveContext();
if (activeNoteContext) {
activeNoteContext.setHoistedNoteId(noteId);
}
};
this.bindGlobalShortcut = shortcutService.bindGlobalShortcut;
this.waitUntilSynced = ws.waitForMaxKnownEntityChangeId;
this.refreshIncludedNote = (includedNoteId) => appContext.triggerEvent("refreshIncludedNote", { noteId: includedNoteId });
this.randomString = utils.randomString;
this.formatSize = utils.formatSize;
this.formatNoteSize = utils.formatSize;
this.logMessages = {};
this.logSpacedUpdates = {};
this.log = (message) => {
const { noteId } = this.startNote;
message = `${utils.now()}: ${message}`;
console.log(`Script ${noteId}: ${message}`);
this.logMessages[noteId] = this.logMessages[noteId] || [];
this.logSpacedUpdates[noteId] =
this.logSpacedUpdates[noteId] ||
new SpacedUpdate(() => {
const messages = this.logMessages[noteId];
this.logMessages[noteId] = [];
appContext.triggerEvent("apiLogMessages", { noteId, messages });
}, 100);
this.logMessages[noteId].push(message);
this.logSpacedUpdates[noteId].scheduleUpdate();
};
}
export default FrontendScriptApi as any as {
new(startNote: FNote, currentNote: FNote, originEntity: Entity | null, $container: JQuery<HTMLElement> | null): Api;
};

View File

@@ -1,28 +0,0 @@
/**
* The front script API is accessible to code notes with the "JS (frontend)" language.
*
* The entire API is exposed as a single global: {@link api}
*
* @module Frontend Script API
*/
/**
* This file creates the entrypoint for TypeDoc that simulates the context from within a
* script note.
*
* Make sure to keep in line with frontend's `script_context.ts`.
*/
export type { default as BasicWidget } from "../widgets/basic_widget.js";
export type { default as FAttachment } from "../entities/fattachment.js";
export type { default as FAttribute } from "../entities/fattribute.js";
export type { default as FBranch } from "../entities/fbranch.js";
export type { default as FNote } from "../entities/fnote.js";
export type { Api } from "./frontend_script_api.js";
export type { default as NoteContextAwareWidget } from "../widgets/note_context_aware_widget.js";
export type { default as RightPanelWidget } from "../widgets/right_panel_widget.js";
import FrontendScriptApi, { type Api } from "./frontend_script_api.js";
//@ts-expect-error
export const api: Api = new FrontendScriptApi();

View File

@@ -1,83 +0,0 @@
import utils from "./utils.js";
import appContext from "../components/app_context.js";
import server from "./server.js";
import libraryLoader from "./library_loader.js";
import ws from "./ws.js";
import froca from "./froca.js";
import linkService from "./link.js";
import { lint } from "./eslint.js";
function setupGlobs() {
window.glob.isDesktop = utils.isDesktop;
window.glob.isMobile = utils.isMobile;
window.glob.getComponentByEl = (el) => appContext.getComponentByEl(el);
window.glob.getHeaders = server.getHeaders;
window.glob.getReferenceLinkTitle = (href) => linkService.getReferenceLinkTitle(href);
window.glob.getReferenceLinkTitleSync = (href) => linkService.getReferenceLinkTitleSync(href);
// required for ESLint plugin and CKEditor
window.glob.getActiveContextNote = () => appContext.tabManager.getActiveContextNote();
window.glob.requireLibrary = libraryLoader.requireLibrary;
window.glob.linter = lint;
window.glob.appContext = appContext; // for debugging
window.glob.froca = froca;
window.glob.treeCache = froca; // compatibility for CKEditor builds for a while
// for CKEditor integration (button on block toolbar)
window.glob.importMarkdownInline = async () => appContext.triggerCommand("importMarkdownInline");
window.onerror = function (msg, url, lineNo, columnNo, error) {
const string = String(msg).toLowerCase();
let message = "Uncaught error: ";
if (string.includes("script error")) {
message += "No details available";
} else {
message += [`Message: ${msg}`, `URL: ${url}`, `Line: ${lineNo}`, `Column: ${columnNo}`, `Error object: ${JSON.stringify(error)}`, `Stack: ${error && error.stack}`].join(", ");
}
ws.logError(message);
return false;
};
window.addEventListener("unhandledrejection", (e) => {
const string = e?.reason?.message?.toLowerCase();
let message = "Uncaught error: ";
if (string?.includes("script error")) {
message += "No details available";
} else {
message += [
`Message: ${e.reason.message}`,
`Line: ${e.reason.lineNumber}`,
`Column: ${e.reason.columnNumber}`,
`Error object: ${JSON.stringify(e.reason)}`,
`Stack: ${e.reason && e.reason.stack}`
].join(", ");
}
ws.logError(message);
return false;
});
for (const appCssNoteId of glob.appCssNoteIds || []) {
libraryLoader.requireCss(`api/notes/download/${appCssNoteId}`, false);
}
utils.initHelpButtons($(window));
$("body").on("click", "a.external", function () {
window.open($(this).attr("href"), "_blank");
return false;
});
}
export default {
setupGlobs
};

View File

@@ -1,81 +0,0 @@
import appContext from "../components/app_context.js";
import treeService from "./tree.js";
import dialogService from "./dialog.js";
import froca from "./froca.js";
import type NoteContext from "../components/note_context.js";
import { t } from "./i18n.js";
function getHoistedNoteId() {
const activeNoteContext = appContext.tabManager.getActiveContext();
return activeNoteContext ? activeNoteContext.hoistedNoteId : "root";
}
async function unhoist() {
const activeNoteContext = appContext.tabManager.getActiveContext();
if (activeNoteContext) {
await activeNoteContext.unhoist();
}
}
function isTopLevelNode(node: Fancytree.FancytreeNode) {
return isHoistedNode(node.getParent());
}
function isHoistedNode(node: Fancytree.FancytreeNode) {
// even though check for 'root' should not be necessary, we keep it just in case
return node.data.noteId === "root" || node.data.noteId === getHoistedNoteId();
}
async function isHoistedInHiddenSubtree() {
const hoistedNoteId = getHoistedNoteId();
if (hoistedNoteId === "root") {
return false;
}
const hoistedNote = await froca.getNote(hoistedNoteId);
return hoistedNote?.isHiddenCompletely();
}
async function checkNoteAccess(notePath: string, noteContext: NoteContext) {
const resolvedNotePath = await treeService.resolveNotePath(notePath, noteContext.hoistedNoteId);
if (!resolvedNotePath) {
console.log(`Cannot activate '${notePath}'`);
return false;
}
const hoistedNoteId = noteContext.hoistedNoteId;
if (!resolvedNotePath.includes(hoistedNoteId) && (!resolvedNotePath.includes("_hidden") || resolvedNotePath.includes("_lbBookmarks"))) {
const noteId = treeService.getNoteIdFromUrl(resolvedNotePath);
if (!noteId) {
return false;
}
const requestedNote = await froca.getNote(noteId);
const hoistedNote = await froca.getNote(hoistedNoteId);
if (
(!hoistedNote?.hasAncestor("_hidden") || resolvedNotePath.includes("_lbBookmarks")) &&
!(await dialogService.confirm(t("hoisted_note.confirm_unhoisting", { requestedNote: requestedNote?.title, hoistedNote: hoistedNote?.title })))
) {
return false;
}
// unhoist so we can activate the note
await unhoist();
}
return true;
}
export default {
getHoistedNoteId,
unhoist,
isTopLevelNode,
isHoistedNode,
checkNoteAccess,
isHoistedInHiddenSubtree
};

View File

@@ -1,44 +0,0 @@
import options from "./options.js";
import i18next from "i18next";
import i18nextHttpBackend from "i18next-http-backend";
import server from "./server.js";
import type { Locale } from "@triliumnext/commons";
let locales: Locale[] | null;
export async function initLocale() {
const locale = (options.get("locale") as string) || "en";
locales = await server.get<Locale[]>("options/locales");
await i18next.use(i18nextHttpBackend).init({
lng: locale,
fallbackLng: "en",
backend: {
loadPath: `${window.glob.assetPath}/translations/{{lng}}/{{ns}}.json`
},
returnEmptyString: false
});
}
export function getAvailableLocales() {
if (!locales) {
throw new Error("Tried to load list of locales, but localization is not yet initialized.")
}
return locales;
}
/**
* Finds the given locale by ID.
*
* @param localeId the locale ID to search for.
* @returns the corresponding {@link Locale} or `null` if it was not found.
*/
export function getLocaleById(localeId: string | null | undefined) {
if (!localeId) return null;
return locales?.find((l) => l.id === localeId) ?? null;
}
export const t = i18next.t;
export const getCurrentLanguage = () => i18next.language;

View File

@@ -1,36 +0,0 @@
import { t } from "./i18n.js";
import toastService from "./toast.js";
function copyImageReferenceToClipboard($imageWrapper: JQuery<HTMLElement>) {
try {
$imageWrapper.attr("contenteditable", "true");
selectImage($imageWrapper.get(0));
const success = document.execCommand("copy");
if (success) {
toastService.showMessage(t("image.copied-to-clipboard"));
} else {
toastService.showAndLogError(t("image.cannot-copy"));
}
} finally {
window.getSelection()?.removeAllRanges();
$imageWrapper.removeAttr("contenteditable");
}
}
function selectImage(element: HTMLElement | undefined) {
if (!element) {
return;
}
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(element);
selection?.removeAllRanges();
selection?.addRange(range);
}
export default {
copyImageReferenceToClipboard
};

View File

@@ -1,118 +0,0 @@
import toastService, { type ToastOptions } from "./toast.js";
import server from "./server.js";
import ws from "./ws.js";
import utils from "./utils.js";
import appContext from "../components/app_context.js";
import { t } from "./i18n.js";
type BooleanLike = boolean | "true" | "false";
export interface UploadFilesOptions {
safeImport?: BooleanLike;
shrinkImages: BooleanLike;
textImportedAsText?: BooleanLike;
codeImportedAsCode?: BooleanLike;
explodeArchives?: BooleanLike;
replaceUnderscoresWithSpaces?: BooleanLike;
}
export async function uploadFiles(entityType: string, parentNoteId: string, files: string[] | File[], options: UploadFilesOptions) {
if (!["notes", "attachments"].includes(entityType)) {
throw new Error(`Unrecognized import entity type '${entityType}'.`);
}
if (files.length === 0) {
return;
}
const taskId = utils.randomString(10);
let counter = 0;
for (const file of files) {
counter++;
const formData = new FormData();
formData.append("upload", file);
formData.append("taskId", taskId);
formData.append("last", counter === files.length ? "true" : "false");
for (const key in options) {
formData.append(key, (options as any)[key]);
}
await $.ajax({
url: `${window.glob.baseApiUrl}notes/${parentNoteId}/${entityType}-import`,
headers: await server.getHeaders(),
data: formData,
dataType: "json",
type: "POST",
timeout: 60 * 60 * 1000,
error: function (xhr) {
toastService.showError(t("import.failed", { message: xhr.responseText }));
},
contentType: false, // NEEDED, DON'T REMOVE THIS
processData: false // NEEDED, DON'T REMOVE THIS
});
}
}
function makeToast(id: string, message: string): ToastOptions {
return {
id: id,
title: t("import.import-status"),
message: message,
icon: "plus"
};
}
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "importNotes") {
return;
}
if (message.type === "taskError") {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("import.in-progress", { progress: message.progressCount })));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("import.successful"));
toast.closeAfter = 5000;
toastService.showPersistent(toast);
if (message.result.importedNoteId) {
await appContext.tabManager.getActiveContext()?.setNote(message.result.importedNoteId);
}
}
});
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "importAttachments") {
return;
}
if (message.type === "taskError") {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
toastService.showPersistent(makeToast(message.taskId, t("import.in-progress", { progress: message.progressCount })));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("import.successful"));
toast.closeAfter = 5000;
toastService.showPersistent(toast);
if (message.result.parentNoteId) {
await appContext.tabManager.getActiveContext()?.setNote(message.result.importedNoteId, {
viewScope: {
viewMode: "attachments"
}
});
}
}
});
export default {
uploadFiles
};

View File

@@ -1,120 +0,0 @@
import server from "./server.js";
import appContext, { type CommandNames } from "../components/app_context.js";
import shortcutService from "./shortcuts.js";
import type Component from "../components/component.js";
const keyboardActionRepo: Record<string, Action> = {};
// TODO: Deduplicate with server.
export interface Action {
actionName: CommandNames;
effectiveShortcuts: string[];
scope: string;
}
const keyboardActionsLoaded = server.get<Action[]>("keyboard-actions").then((actions) => {
actions = actions.filter((a) => !!a.actionName); // filter out separators
for (const action of actions) {
action.effectiveShortcuts = action.effectiveShortcuts.filter((shortcut) => !shortcut.startsWith("global:"));
keyboardActionRepo[action.actionName] = action;
}
return actions;
});
async function getActions() {
return await keyboardActionsLoaded;
}
async function getActionsForScope(scope: string) {
const actions = await keyboardActionsLoaded;
return actions.filter((action) => action.scope === scope);
}
async function setupActionsForElement(scope: string, $el: JQuery<HTMLElement>, component: Component) {
const actions = await getActionsForScope(scope);
for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {
shortcutService.bindElShortcut($el, shortcut, () => component.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));
}
}
}
getActionsForScope("window").then((actions) => {
for (const action of actions) {
for (const shortcut of action.effectiveShortcuts) {
shortcutService.bindGlobalShortcut(shortcut, () => appContext.triggerCommand(action.actionName, { ntxId: appContext.tabManager.activeNtxId }));
}
}
});
async function getAction(actionName: string, silent = false) {
await keyboardActionsLoaded;
const action = keyboardActionRepo[actionName];
if (!action) {
if (silent) {
console.debug(`Cannot find action '${actionName}'`);
} else {
throw new Error(`Cannot find action '${actionName}'`);
}
}
return action;
}
function updateDisplayedShortcuts($container: JQuery<HTMLElement>) {
//@ts-ignore
//TODO: each() does not support async callbacks.
$container.find("kbd[data-command]").each(async (i, el) => {
const actionName = $(el).attr("data-command");
if (!actionName) {
return;
}
const action = await getAction(actionName, true);
if (action) {
const keyboardActions = action.effectiveShortcuts.join(", ");
if (keyboardActions || $(el).text() !== "not set") {
$(el).text(keyboardActions);
}
}
});
//@ts-ignore
//TODO: each() does not support async callbacks.
$container.find("[data-trigger-command]").each(async (i, el) => {
const actionName = $(el).attr("data-trigger-command");
if (!actionName) {
return;
}
const action = await getAction(actionName, true);
if (action) {
const title = $(el).attr("title");
const shortcuts = action.effectiveShortcuts.join(", ");
if (title?.includes(shortcuts)) {
return;
}
const newTitle = !title?.trim() ? shortcuts : `${title} (${shortcuts})`;
$(el).attr("title", newTitle);
}
});
}
export default {
updateDisplayedShortcuts,
setupActionsForElement,
getActions,
getActionsForScope
};

View File

@@ -1,163 +0,0 @@
import mimeTypesService from "./mime_types.js";
import optionsService from "./options.js";
import { getStylesheetUrl } from "./syntax_highlight.js";
export interface Library {
js?: string[] | (() => string[]);
css?: string[];
}
const CKEDITOR: Library = {
js: ["libraries/ckeditor/ckeditor.js"]
};
const CODE_MIRROR: Library = {
js: () => {
const scriptsToLoad = [
"node_modules/codemirror/lib/codemirror.js",
"node_modules/codemirror/addon/display/placeholder.js",
"node_modules/codemirror/addon/edit/matchbrackets.js",
"node_modules/codemirror/addon/edit/matchtags.js",
"node_modules/codemirror/addon/fold/xml-fold.js",
"node_modules/codemirror/addon/lint/lint.js",
"node_modules/codemirror/addon/mode/loadmode.js",
"node_modules/codemirror/addon/mode/multiplex.js",
"node_modules/codemirror/addon/mode/overlay.js",
"node_modules/codemirror/addon/mode/simple.js",
"node_modules/codemirror/addon/search/match-highlighter.js",
"node_modules/codemirror/mode/meta.js",
"node_modules/codemirror/keymap/vim.js",
"libraries/codemirror/eslint.js"
];
const mimeTypes = mimeTypesService.getMimeTypes();
for (const mimeType of mimeTypes) {
if (mimeType.enabled && mimeType.codeMirrorSource) {
scriptsToLoad.push(mimeType.codeMirrorSource);
}
}
return scriptsToLoad;
},
css: ["node_modules/codemirror/lib/codemirror.css", "node_modules/codemirror/addon/lint/lint.css"]
};
const KATEX: Library = {
js: ["node_modules/katex/dist/katex.min.js", "node_modules/katex/dist/contrib/mhchem.min.js", "node_modules/katex/dist/contrib/auto-render.min.js"],
css: ["node_modules/katex/dist/katex.min.css"]
};
const HIGHLIGHT_JS: Library = {
js: () => {
const mimeTypes = mimeTypesService.getMimeTypes();
const scriptsToLoad = new Set<string>();
scriptsToLoad.add("node_modules/@highlightjs/cdn-assets/highlight.min.js");
for (const mimeType of mimeTypes) {
const id = mimeType.highlightJs;
if (!mimeType.enabled || !id) {
continue;
}
if (mimeType.highlightJsSource === "libraries") {
scriptsToLoad.add(`libraries/highlightjs/${id}.js`);
} else {
// Built-in module.
scriptsToLoad.add(`node_modules/@highlightjs/cdn-assets/languages/${id}.min.js`);
}
}
const currentTheme = String(optionsService.get("codeBlockTheme"));
loadHighlightingTheme(currentTheme);
return Array.from(scriptsToLoad);
}
};
async function requireLibrary(library: Library) {
if (library.css) {
library.css.map((cssUrl) => requireCss(cssUrl));
}
if (library.js) {
for (const scriptUrl of await unwrapValue(library.js)) {
await requireScript(scriptUrl);
}
}
}
async function unwrapValue<T>(value: T | (() => T) | Promise<T>) {
if (value && typeof value === "object" && "then" in value) {
return (await (value as Promise<() => T>))();
}
if (typeof value === "function") {
return (value as () => T)();
}
return value;
}
// we save the promises in case of the same script being required concurrently multiple times
const loadedScriptPromises: Record<string, JQuery.jqXHR> = {};
async function requireScript(url: string) {
url = `${window.glob.assetPath}/${url}`;
if (!loadedScriptPromises[url]) {
loadedScriptPromises[url] = $.ajax({
url: url,
dataType: "script",
cache: true
});
}
await loadedScriptPromises[url];
}
async function requireCss(url: string, prependAssetPath = true) {
const cssLinks = Array.from(document.querySelectorAll("link")).map((el) => el.href);
if (!cssLinks.some((l) => l.endsWith(url))) {
if (prependAssetPath) {
url = `${window.glob.assetPath}/${url}`;
}
$("head").append($('<link rel="stylesheet" type="text/css" />').attr("href", url));
}
}
let highlightingThemeEl: JQuery<HTMLElement> | null = null;
function loadHighlightingTheme(theme: string) {
if (!theme) {
return;
}
if (theme === "none") {
// Deactivate the theme.
if (highlightingThemeEl) {
highlightingThemeEl.remove();
highlightingThemeEl = null;
}
return;
}
if (!highlightingThemeEl) {
highlightingThemeEl = $(`<link rel="stylesheet" type="text/css" />`);
$("head").append(highlightingThemeEl);
}
const url = getStylesheetUrl(theme);
if (url) {
highlightingThemeEl.attr("href", url);
}
}
export default {
requireCss,
requireLibrary,
loadHighlightingTheme,
CKEDITOR,
CODE_MIRROR,
KATEX,
HIGHLIGHT_JS
};

View File

@@ -1,19 +0,0 @@
import { describe, expect, it } from "vitest";
import { parseNavigationStateFromUrl } from "./link.js";
describe("Link", () => {
it("parses plain searchString", () => {
const output = parseNavigationStateFromUrl("http://localhost:8080/#?searchString=hello");
expect(output).toMatchObject({ searchString: "hello" });
});
it("parses searchString with hash", () => {
const output = parseNavigationStateFromUrl("https://github.com/orgs/TriliumNext/discussions/1526#discussioncomment-12656660");
expect(output).toStrictEqual({});
});
it("parses notePath", () => {
const output = parseNavigationStateFromUrl(`#root/WWaBNf3SSA1b/mQ2tIzLVFKHL`);
expect(output).toMatchObject({ notePath: "root/WWaBNf3SSA1b/mQ2tIzLVFKHL", noteId: "mQ2tIzLVFKHL" });
});
});

View File

@@ -1,489 +0,0 @@
import treeService from "./tree.js";
import linkContextMenuService from "../menus/link_context_menu.js";
import appContext, { type NoteCommandData } from "../components/app_context.js";
import froca from "./froca.js";
import utils from "./utils.js";
// Be consistent with `allowedSchemes` in `src\services\html_sanitizer.ts`
// TODO: Deduplicate with server once we can.
export const ALLOWED_PROTOCOLS = [
'http', 'https', 'ftp', 'ftps', 'mailto', 'data', 'evernote', 'file', 'facetime', 'gemini', 'git',
'gopher', 'imap', 'irc', 'irc6', 'jabber', 'jar', 'lastfm', 'ldap', 'ldaps', 'magnet', 'message',
'mumble', 'nfs', 'onenote', 'pop', 'rmi', 's3', 'sftp', 'skype', 'sms', 'spotify', 'steam', 'svn', 'udp',
'view-source', 'vlc', 'vnc', 'ws', 'wss', 'xmpp', 'jdbc', 'slack', 'tel', 'smb', 'zotero', 'geo',
'mid'
];
function getNotePathFromUrl(url: string) {
const notePathMatch = /#(root[A-Za-z0-9_/]*)$/.exec(url);
return notePathMatch === null ? null : notePathMatch[1];
}
async function getLinkIcon(noteId: string, viewMode: ViewMode | undefined) {
let icon;
if (!viewMode || viewMode === "default") {
const note = await froca.getNote(noteId);
icon = note?.getIcon();
} else if (viewMode === "source") {
icon = "bx bx-code-curly";
} else if (viewMode === "attachments") {
icon = "bx bx-file";
}
return icon;
}
// TODO: Remove `string` once all the view modes have been mapped.
type ViewMode = "default" | "source" | "attachments" | "contextual-help" | string;
export interface ViewScope {
/**
* - "source", when viewing the source code of a note.
* - "attachments", when viewing the attachments of a note.
* - "contextual-help", if the current view represents a help window that was opened to the side of the main content.
* - "default", otherwise.
*/
viewMode?: ViewMode;
attachmentId?: string;
readOnlyTemporarilyDisabled?: boolean;
highlightsListPreviousVisible?: boolean;
highlightsListTemporarilyHidden?: boolean;
tocTemporarilyHidden?: boolean;
/*
* The reason for adding tocPreviousVisible is to record whether the previous state of the toc is hidden or displayed,
* and then let it be displayed/hidden at the initial time. If there is no such value,
* when the right panel needs to display highlighttext but not toc, every time the note content is changed,
* toc will appear and then close immediately, because getToc(html) function will consume time
*/
tocPreviousVisible?: boolean;
}
interface CreateLinkOptions {
title?: string;
showTooltip?: boolean;
showNotePath?: boolean;
showNoteIcon?: boolean;
referenceLink?: boolean;
autoConvertToImage?: boolean;
viewScope?: ViewScope;
}
async function createLink(notePath: string | undefined, options: CreateLinkOptions = {}) {
if (!notePath || !notePath.trim()) {
logError("Missing note path");
return $("<span>").text("[missing note]");
}
if (!notePath.startsWith("root")) {
// all note paths should start with "root/" (except for "root" itself)
// used, e.g., to find internal links
notePath = `root/${notePath}`;
}
const showTooltip = options.showTooltip === undefined ? true : options.showTooltip;
const showNotePath = options.showNotePath === undefined ? false : options.showNotePath;
const showNoteIcon = options.showNoteIcon === undefined ? false : options.showNoteIcon;
const referenceLink = options.referenceLink === undefined ? false : options.referenceLink;
const autoConvertToImage = options.autoConvertToImage === undefined ? false : options.autoConvertToImage;
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
if (!noteId) {
logError("Missing note ID");
return $("<span>").text("[missing note]");
}
const viewScope = options.viewScope || {};
const viewMode = viewScope.viewMode || "default";
let linkTitle = options.title;
if (!linkTitle) {
if (viewMode === "attachments" && viewScope.attachmentId) {
const attachment = await froca.getAttachment(viewScope.attachmentId);
linkTitle = attachment ? attachment.title : "[missing attachment]";
} else if (noteId) {
linkTitle = await treeService.getNoteTitle(noteId, parentNoteId);
}
}
const note = await froca.getNote(noteId);
if (autoConvertToImage && note?.type && ["image", "canvas", "mermaid"].includes(note.type) && viewMode === "default") {
const encodedTitle = encodeURIComponent(linkTitle || "");
return $("<img>")
.attr("src", `api/images/${noteId}/${encodedTitle}?${Math.random()}`)
.attr("alt", linkTitle || "");
}
const $container = $("<span>");
if (showNoteIcon) {
let icon = await getLinkIcon(noteId, viewMode);
if (icon) {
$container.append($("<span>").addClass(`bx ${icon}`)).append(" ");
}
}
const hash = calculateHash({
notePath,
viewScope: viewScope
});
const $noteLink = $("<a>", {
href: hash,
text: linkTitle
});
if (!showTooltip) {
$noteLink.addClass("no-tooltip-preview");
}
if (referenceLink) {
$noteLink.addClass("reference-link");
}
$container.append($noteLink);
if (showNotePath) {
const resolvedPathSegments = (await treeService.resolveNotePathToSegments(notePath)) || [];
resolvedPathSegments.pop(); // Remove last element
const resolvedPath = resolvedPathSegments.join("/");
const pathSegments = await treeService.getNotePathTitleComponents(resolvedPath);
if (pathSegments) {
if (pathSegments.length) {
$container.append($("<small>").append(treeService.formatNotePath(pathSegments)));
}
}
}
return $container;
}
function calculateHash({ notePath, ntxId, hoistedNoteId, viewScope = {} }: NoteCommandData) {
notePath = notePath || "";
const params = [
ntxId ? { ntxId: ntxId } : null,
hoistedNoteId && hoistedNoteId !== "root" ? { hoistedNoteId: hoistedNoteId } : null,
viewScope.viewMode && viewScope.viewMode !== "default" ? { viewMode: viewScope.viewMode } : null,
viewScope.attachmentId ? { attachmentId: viewScope.attachmentId } : null
].filter((p) => !!p);
const paramStr = params
.map((pair) => {
const name = Object.keys(pair)[0];
const value = (pair as Record<string, string | undefined>)[name];
return `${encodeURIComponent(name)}=${encodeURIComponent(value || "")}`;
})
.join("&");
if (!notePath && !paramStr) {
return "";
}
let hash = `#${notePath}`;
if (paramStr) {
hash += `?${paramStr}`;
}
return hash;
}
export function parseNavigationStateFromUrl(url: string | undefined) {
if (!url) {
return {};
}
const hashIdx = url.indexOf("#");
if (hashIdx === -1) {
return {};
}
const hash = url.substr(hashIdx + 1); // strip also the initial '#'
let [notePath, paramString] = hash.split("?");
const viewScope: ViewScope = {
viewMode: "default"
};
let ntxId = null;
let hoistedNoteId = null;
let searchString = null;
if (paramString) {
for (const pair of paramString.split("&")) {
let [name, value] = pair.split("=");
name = decodeURIComponent(name);
value = decodeURIComponent(value);
if (name === "ntxId") {
ntxId = value;
} else if (name === "hoistedNoteId") {
hoistedNoteId = value;
} else if (name === "searchString") {
searchString = value; // supports triggering search from URL, e.g. #?searchString=blabla
} else if (["viewMode", "attachmentId"].includes(name)) {
(viewScope as any)[name] = value;
} else {
console.warn(`Unrecognized hash parameter '${name}'.`);
}
}
}
if (searchString) {
return { searchString }
}
if (!notePath.match(/^[_a-z0-9]{4,}(\/[_a-z0-9]{4,})*$/i)) {
return {};
}
return {
notePath,
noteId: treeService.getNoteIdFromUrl(notePath),
ntxId,
hoistedNoteId,
viewScope,
searchString
};
}
function goToLink(evt: MouseEvent | JQuery.ClickEvent | JQuery.MouseDownEvent) {
const $link = $(evt.target as any).closest("a,.block-link");
const hrefLink = $link.attr("href") || $link.attr("data-href");
return goToLinkExt(evt, hrefLink, $link);
}
function goToLinkExt(evt: MouseEvent | JQuery.ClickEvent | JQuery.MouseDownEvent | React.PointerEvent<HTMLCanvasElement>, hrefLink: string | undefined, $link?: JQuery<HTMLElement> | null) {
if (hrefLink?.startsWith("data:")) {
return true;
}
evt.preventDefault();
evt.stopPropagation();
if (hrefLink?.startsWith("#fn") && $link) {
return handleFootnote(hrefLink, $link);
}
const { notePath, viewScope } = parseNavigationStateFromUrl(hrefLink);
const ctrlKey = utils.isCtrlKey(evt);
const shiftKey = evt.shiftKey;
const isLeftClick = "which" in evt && evt.which === 1;
const isMiddleClick = "which" in evt && evt.which === 2;
const targetIsBlank = ($link?.attr("target") === "_blank");
const openInNewTab = (isLeftClick && ctrlKey) || isMiddleClick || targetIsBlank;
const activate = (isLeftClick && ctrlKey && shiftKey) || (isMiddleClick && shiftKey);
const openInNewWindow = isLeftClick && evt.shiftKey && !ctrlKey;
if (notePath) {
if (openInNewWindow) {
appContext.triggerCommand("openInWindow", { notePath, viewScope });
} else if (openInNewTab) {
appContext.tabManager.openTabWithNoteWithHoisting(notePath, {
activate: activate ? true : targetIsBlank,
viewScope
});
} else if (isLeftClick) {
const ntxId = $(evt.target as any)
.closest("[data-ntx-id]")
.attr("data-ntx-id");
const noteContext = ntxId ? appContext.tabManager.getNoteContextById(ntxId) : appContext.tabManager.getActiveContext();
if (noteContext) {
noteContext.setNote(notePath, { viewScope }).then(() => {
if (noteContext !== appContext.tabManager.getActiveContext()) {
appContext.tabManager.activateNoteContext(noteContext.ntxId);
}
});
} else {
appContext.tabManager.openContextWithNote(notePath, { viewScope, activate: true });
}
}
} else if (hrefLink) {
const withinEditLink = $link?.hasClass("ck-link-actions__preview");
const outsideOfCKEditor = !$link || $link.closest("[contenteditable]").length === 0;
if (openInNewTab || (withinEditLink && (isLeftClick || isMiddleClick)) || (outsideOfCKEditor && (isLeftClick || isMiddleClick))) {
if (hrefLink.toLowerCase().startsWith("http") || hrefLink.startsWith("api/")) {
window.open(hrefLink, "_blank");
} else if ((hrefLink.toLowerCase().startsWith("file:") || hrefLink.toLowerCase().startsWith("geo:")) && utils.isElectron()) {
const electron = utils.dynamicRequire("electron");
electron.shell.openPath(hrefLink);
} else {
// Enable protocols supported by CKEditor 5 to be clickable.
if (ALLOWED_PROTOCOLS.some((protocol) => hrefLink.toLowerCase().startsWith(protocol + ":"))) {
window.open(hrefLink, "_blank");
}
}
}
}
return true;
}
/**
* Scrolls to either the footnote (if clicking on a reference such as `[1]`), or to the reference of a footnote (if clicking on the footnote `^` arrow).
*
* @param hrefLink the URL of the link that was clicked (it should be in the form of `#fn` or `#fnref`).
* @param $link the element of the link that was clicked.
* @returns whether the event should be consumed or not.
*/
function handleFootnote(hrefLink: string, $link: JQuery<HTMLElement>) {
const el = $link.closest(".ck-content").find(hrefLink)[0];
if (el) {
el.scrollIntoView({ behavior: "smooth", block: "center" });
}
return true;
}
function linkContextMenu(e: PointerEvent) {
const $link = $(e.target as any).closest("a");
const url = $link.attr("href") || $link.attr("data-href");
if ($link.attr("data-no-context-menu")) {
return;
}
const { notePath, viewScope } = parseNavigationStateFromUrl(url);
if (!notePath) {
return;
}
e.preventDefault();
linkContextMenuService.openContextMenu(notePath, e, viewScope, null);
}
async function loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string | null | undefined = null) {
const $link = $el[0].tagName === "A" ? $el : $el.find("a");
href = href || $link.attr("href");
if (!href) {
console.warn("Empty URL for parsing: " + $el[0].outerHTML);
return;
}
const { noteId, viewScope } = parseNavigationStateFromUrl(href);
if (!noteId) {
console.warn("Missing note ID.");
return;
}
const note = await froca.getNote(noteId, true);
if (note) {
$el.addClass(note.getColorClass());
}
const title = await getReferenceLinkTitle(href);
$el.text(title);
if (note) {
const icon = await getLinkIcon(noteId, viewScope.viewMode);
if (icon) {
$el.prepend($("<span>").addClass(icon));
}
}
}
async function getReferenceLinkTitle(href: string) {
const { noteId, viewScope } = parseNavigationStateFromUrl(href);
if (!noteId) {
return "[missing note]";
}
const note = await froca.getNote(noteId);
if (!note) {
return "[missing note]";
}
if (viewScope?.viewMode === "attachments" && viewScope?.attachmentId) {
const attachment = await note.getAttachmentById(viewScope.attachmentId);
return attachment ? attachment.title : "[missing attachment]";
} else {
return note.title;
}
}
function getReferenceLinkTitleSync(href: string) {
const { noteId, viewScope } = parseNavigationStateFromUrl(href);
if (!noteId) {
return "[missing note]";
}
const note = froca.getNoteFromCache(noteId);
if (!note) {
return "[missing note]";
}
if (viewScope?.viewMode === "attachments" && viewScope?.attachmentId) {
if (!note.attachments) {
return "[loading title...]";
}
const attachment = note.attachments.find((att) => att.attachmentId === viewScope.attachmentId);
return attachment ? attachment.title : "[missing attachment]";
} else {
return note.title;
}
}
// TODO: Check why the event is not supported.
//@ts-ignore
$(document).on("click", "a", goToLink);
// TODO: Check why the event is not supported.
//@ts-ignore
$(document).on("auxclick", "a", goToLink); // to handle the middle button
// TODO: Check why the event is not supported.
//@ts-ignore
$(document).on("contextmenu", "a", linkContextMenu);
$(document).on("dblclick", "a", (e) => {
e.preventDefault();
e.stopPropagation();
const $link = $(e.target).closest("a");
const address = $link.attr("href");
if (address && address.startsWith("http")) {
window.open(address, "_blank");
}
});
$(document).on("mousedown", "a", (e) => {
if (e.which === 2) {
// prevent paste on middle click
// https://github.com/zadam/trilium/issues/2995
// https://developer.mozilla.org/en-US/docs/Web/API/Element/auxclick_event#preventing_default_actions
e.preventDefault();
return false;
}
});
export default {
getNotePathFromUrl,
createLink,
goToLink,
goToLinkExt,
loadReferenceLinkTitle,
getReferenceLinkTitle,
getReferenceLinkTitleSync,
calculateHash,
parseNavigationStateFromUrl
};

View File

@@ -1,228 +0,0 @@
import type { AttachmentRow } from "@triliumnext/commons";
import type { AttributeType } from "../entities/fattribute.js";
import type { EntityChange } from "../server_types.js";
// TODO: Deduplicate with server.
interface NoteRow {
isDeleted?: boolean;
}
// TODO: Deduplicate with BranchRow from `rows.ts`/
export interface BranchRow {
noteId?: string;
branchId: string;
componentId: string;
parentNoteId?: string;
isDeleted?: boolean;
isExpanded?: boolean;
}
export interface AttributeRow {
noteId?: string;
attributeId: string;
componentId: string;
isInheritable?: boolean;
isDeleted?: boolean;
name?: string;
value?: string;
type?: AttributeType;
}
interface RevisionRow {
revisionId: string;
noteId?: string;
componentId?: string | null;
}
interface ContentNoteIdToComponentIdRow {
noteId: string;
componentId: string;
}
interface OptionRow {}
interface NoteReorderingRow {}
interface ContentNoteIdToComponentIdRow {
noteId: string;
componentId: string;
}
type EntityRowMappings = {
notes: NoteRow;
branches: BranchRow;
attributes: AttributeRow;
options: OptionRow;
revisions: RevisionRow;
note_reordering: NoteReorderingRow;
};
export type EntityRowNames = keyof EntityRowMappings;
export default class LoadResults {
private entities: Record<keyof EntityRowMappings, Record<string, any>>;
private noteIdToComponentId: Record<string, string[]>;
private componentIdToNoteIds: Record<string, string[]>;
private branchRows: BranchRow[];
private attributeRows: AttributeRow[];
private revisionRows: RevisionRow[];
private noteReorderings: string[];
private contentNoteIdToComponentId: ContentNoteIdToComponentIdRow[];
private optionNames: string[];
private attachmentRows: AttachmentRow[];
constructor(entityChanges: EntityChange[]) {
const entities: Record<string, Record<string, any>> = {};
for (const { entityId, entityName, entity } of entityChanges) {
if (entity) {
entities[entityName] = entities[entityName] || [];
entities[entityName][entityId] = entity;
}
}
this.entities = entities;
this.noteIdToComponentId = {};
this.componentIdToNoteIds = {};
this.branchRows = [];
this.attributeRows = [];
this.noteReorderings = [];
this.revisionRows = [];
this.contentNoteIdToComponentId = [];
this.optionNames = [];
this.attachmentRows = [];
}
getEntityRow<T extends EntityRowNames>(entityName: T, entityId: string): EntityRowMappings[T] {
return this.entities[entityName]?.[entityId];
}
addNote(noteId: string, componentId?: string | null) {
this.noteIdToComponentId[noteId] = this.noteIdToComponentId[noteId] || [];
if (componentId) {
if (!this.noteIdToComponentId[noteId].includes(componentId)) {
this.noteIdToComponentId[noteId].push(componentId);
}
this.componentIdToNoteIds[componentId] = this.componentIdToNoteIds[componentId] || [];
if (this.componentIdToNoteIds[componentId]) {
this.componentIdToNoteIds[componentId].push(noteId);
}
}
}
addBranch(branchId: string, componentId: string) {
this.branchRows.push({ branchId, componentId });
}
getBranchRows() {
return this.branchRows.map((row) => this.getEntityRow("branches", row.branchId)).filter((branch) => !!branch);
}
addNoteReordering(parentNoteId: string, componentId: string) {
this.noteReorderings.push(parentNoteId);
}
getNoteReorderings() {
return this.noteReorderings;
}
addAttribute(attributeId: string, componentId: string) {
this.attributeRows.push({ attributeId, componentId });
}
getAttributeRows(componentId = "none"): AttributeRow[] {
return this.attributeRows
.filter((row) => row.componentId !== componentId)
.map((row) => this.getEntityRow("attributes", row.attributeId))
.filter((attr) => !!attr) as AttributeRow[];
}
addRevision(revisionId: string, noteId?: string, componentId?: string | null) {
this.revisionRows.push({ revisionId, noteId, componentId });
}
hasRevisionForNote(noteId: string) {
return !!this.revisionRows.find((row) => row.noteId === noteId);
}
getNoteIds() {
return Object.keys(this.noteIdToComponentId);
}
isNoteReloaded(noteId: string | undefined | null, componentId: string | null = null) {
if (!noteId) {
return false;
}
const componentIds = this.noteIdToComponentId[noteId];
return componentIds && componentIds.find((sId) => sId !== componentId) !== undefined;
}
addNoteContent(noteId: string, componentId: string) {
this.contentNoteIdToComponentId.push({ noteId, componentId });
}
isNoteContentReloaded(noteId: string, componentId?: string) {
if (!noteId) {
return false;
}
return this.contentNoteIdToComponentId.find((l) => l.noteId === noteId && l.componentId !== componentId);
}
addOption(name: string) {
this.optionNames.push(name);
}
isOptionReloaded(name: string) {
return this.optionNames.includes(name);
}
getOptionNames() {
return this.optionNames;
}
addAttachmentRow(attachment: AttachmentRow) {
this.attachmentRows.push(attachment);
}
getAttachmentRows() {
return this.attachmentRows;
}
/**
* @returns {boolean} true if there are changes which could affect the attributes (including inherited ones)
* notably changes in note itself should not have any effect on attributes
*/
hasAttributeRelatedChanges() {
return this.branchRows.length > 0 || this.attributeRows.length > 0;
}
isEmpty() {
return (
Object.keys(this.noteIdToComponentId).length === 0 &&
this.branchRows.length === 0 &&
this.attributeRows.length === 0 &&
this.noteReorderings.length === 0 &&
this.revisionRows.length === 0 &&
this.contentNoteIdToComponentId.length === 0 &&
this.optionNames.length === 0 &&
this.attachmentRows.length === 0
);
}
isEmptyForTree() {
return Object.keys(this.noteIdToComponentId).length === 0 && this.branchRows.length === 0 && this.attributeRows.length === 0 && this.noteReorderings.length === 0;
}
}

View File

@@ -1,35 +0,0 @@
import { describe, expect, it } from "vitest";
import { postprocessMermaidSvg } from "./mermaid.js";
import { trimIndentation } from "@triliumnext/commons";
describe("Mermaid", () => {
it("converts <br> properly", () => {
const before = trimIndentation`\
<g transform="translate(-55.71875, -24)" style="color:black !important" class="label">
<rect></rect>
<foreignObject height="48" width="111.4375">
<div xmlns="http://www.w3.org/1999/xhtml"
style="color: black !important; display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;">
<span class="nodeLabel" style="color:black !important">
<p>Verify Output<br>Against<BR > Criteria</p>
</span>
</div>
</foreignObject>
</g>
`;
const after = trimIndentation`\
<g transform="translate(-55.71875, -24)" style="color:black !important" class="label">
<rect></rect>
<foreignObject height="48" width="111.4375">
<div xmlns="http://www.w3.org/1999/xhtml"
style="color: black !important; display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;">
<span class="nodeLabel" style="color:black !important">
<p>Verify Output<br/>Against<br/> Criteria</p>
</span>
</div>
</foreignObject>
</g>
`;
expect(postprocessMermaidSvg(before)).toBe(after);
});
});

View File

@@ -1,59 +0,0 @@
import type { MermaidConfig } from "mermaid";
import type { Mermaid } from "mermaid";
let elkLoaded = false;
export function getMermaidConfig(): MermaidConfig {
const documentStyle = window.getComputedStyle(document.documentElement);
const mermaidTheme = documentStyle.getPropertyValue("--mermaid-theme") as "default";
return {
theme: mermaidTheme.trim() as "default",
securityLevel: "antiscript",
flowchart: { useMaxWidth: false },
sequence: { useMaxWidth: false },
gantt: { useMaxWidth: false },
class: { useMaxWidth: false },
state: { useMaxWidth: false },
pie: { useMaxWidth: true },
journey: { useMaxWidth: false },
gitGraph: { useMaxWidth: false }
};
}
/**
* Determines whether the ELK extension of Mermaid.js needs to be loaded (which is a relatively large library), based on the
* front-matter of the diagram and loads the library if needed.
*
* <p>
* If the library has already been loaded or the diagram does not require it, the method will exit immediately.
*
* @param mermaidContent the plain text of the mermaid diagram, potentially including a frontmatter.
*/
export async function loadElkIfNeeded(mermaid: Mermaid, mermaidContent: string) {
if (elkLoaded) {
// Exit immediately since the ELK library is already loaded.
return;
}
const parsedContent = await mermaid.parse(mermaidContent, {
suppressErrors: true
});
if (parsedContent && parsedContent.config?.layout === "elk") {
elkLoaded = true;
mermaid.registerLayoutLoaders((await import("@mermaid-js/layout-elk")).default);
}
}
/**
* Processes the output of a Mermaid SVG render before it should be delivered to the user.
*
* <p>
* Currently this fixes <br> to <br/> which would otherwise cause an invalid XML.
*
* @param svg the Mermaid SVG to process.
* @returns the processed SVG.
*/
export function postprocessMermaidSvg(svg: string) {
return svg.replaceAll(/<br\s*>/ig, "<br/>");
}

View File

@@ -1,221 +0,0 @@
// TODO: deduplicate with /src/services/import/mime_type_definitions.ts
/**
* A pseudo-MIME type which is used in the editor to automatically determine the language used in code blocks via heuristics.
*/
export const MIME_TYPE_AUTO = "text-x-trilium-auto";
export interface MimeTypeDefinition {
default?: boolean;
title: string;
mime: string;
/** The name of the language/mime type as defined by highlight.js (or one of the aliases), in order to be used for syntax highlighting such as inside code blocks. */
highlightJs?: string;
/** If specified, will load the corresponding highlight.js file from the `libraries/highlightjs/${id}.js` instead of `node_modules/@highlightjs/cdn-assets/languages/${id}.min.js`. */
highlightJsSource?: "libraries";
/** If specified, will load the corresponding highlight file from the given path instead of `node_modules`. */
codeMirrorSource?: string;
}
/**
* For highlight.js-supported languages, see https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md.
*/
export const MIME_TYPES_DICT: readonly MimeTypeDefinition[] = Object.freeze([
{ title: "Plain text", mime: "text/plain", highlightJs: "plaintext", default: true },
// Keep sorted alphabetically.
{ title: "APL", mime: "text/apl" },
{ title: "ASN.1", mime: "text/x-ttcn-asn" },
{ title: "ASP.NET", mime: "application/x-aspx" },
{ title: "Asterisk", mime: "text/x-asterisk" },
{ title: "Batch file (DOS)", mime: "application/x-bat", highlightJs: "dos", codeMirrorSource: "libraries/codemirror/batch.js" },
{ title: "Brainfuck", mime: "text/x-brainfuck", highlightJs: "brainfuck" },
{ title: "C", mime: "text/x-csrc", highlightJs: "c", default: true },
{ title: "C#", mime: "text/x-csharp", highlightJs: "csharp", default: true },
{ title: "C++", mime: "text/x-c++src", highlightJs: "cpp", default: true },
{ title: "Clojure", mime: "text/x-clojure", highlightJs: "clojure" },
{ title: "ClojureScript", mime: "text/x-clojurescript" },
{ title: "Closure Stylesheets (GSS)", mime: "text/x-gss" },
{ title: "CMake", mime: "text/x-cmake", highlightJs: "cmake" },
{ title: "Cobol", mime: "text/x-cobol" },
{ title: "CoffeeScript", mime: "text/coffeescript", highlightJs: "coffeescript" },
{ title: "Common Lisp", mime: "text/x-common-lisp", highlightJs: "lisp" },
{ title: "CQL", mime: "text/x-cassandra" },
{ title: "Crystal", mime: "text/x-crystal", highlightJs: "crystal" },
{ title: "CSS", mime: "text/css", highlightJs: "css", default: true },
{ title: "Cypher", mime: "application/x-cypher-query" },
{ title: "Cython", mime: "text/x-cython" },
{ title: "D", mime: "text/x-d", highlightJs: "d" },
{ title: "Dart", mime: "application/dart", highlightJs: "dart" },
{ title: "diff", mime: "text/x-diff", highlightJs: "diff" },
{ title: "Django", mime: "text/x-django", highlightJs: "django" },
{ title: "Dockerfile", mime: "text/x-dockerfile", highlightJs: "dockerfile" },
{ title: "DTD", mime: "application/xml-dtd" },
{ title: "Dylan", mime: "text/x-dylan" },
{ title: "EBNF", mime: "text/x-ebnf", highlightJs: "ebnf" },
{ title: "ECL", mime: "text/x-ecl" },
{ title: "edn", mime: "application/edn" },
{ title: "Eiffel", mime: "text/x-eiffel" },
{ title: "Elm", mime: "text/x-elm", highlightJs: "elm" },
{ title: "Embedded Javascript", mime: "application/x-ejs" },
{ title: "Embedded Ruby", mime: "application/x-erb", highlightJs: "erb" },
{ title: "Erlang", mime: "text/x-erlang", highlightJs: "erlang" },
{ title: "Esper", mime: "text/x-esper" },
{ title: "F#", mime: "text/x-fsharp", highlightJs: "fsharp" },
{ title: "Factor", mime: "text/x-factor" },
{ title: "FCL", mime: "text/x-fcl" },
{ title: "Forth", mime: "text/x-forth" },
{ title: "Fortran", mime: "text/x-fortran", highlightJs: "fortran" },
{ title: "Gas", mime: "text/x-gas" },
{ title: "Gherkin", mime: "text/x-feature", highlightJs: "gherkin" },
{ title: "GitHub Flavored Markdown", mime: "text/x-gfm", highlightJs: "markdown" },
{ title: "Go", mime: "text/x-go", highlightJs: "go", default: true },
{ title: "Groovy", mime: "text/x-groovy", highlightJs: "groovy", default: true },
{ title: "HAML", mime: "text/x-haml", highlightJs: "haml" },
{ title: "Haskell (Literate)", mime: "text/x-literate-haskell" },
{ title: "Haskell", mime: "text/x-haskell", highlightJs: "haskell", default: true },
{ title: "Haxe", mime: "text/x-haxe", highlightJs: "haxe" },
{ title: "HTML", mime: "text/html", highlightJs: "xml", default: true },
{ title: "HTTP", mime: "message/http", highlightJs: "http", default: true },
{ title: "HXML", mime: "text/x-hxml" },
{ title: "IDL", mime: "text/x-idl" },
{ title: "Java Server Pages", mime: "application/x-jsp", highlightJs: "java" },
{ title: "Java", mime: "text/x-java", highlightJs: "java", default: true },
{ title: "Jinja2", mime: "text/jinja2" },
{ title: "JS backend", mime: "application/javascript;env=backend", highlightJs: "javascript", default: true },
{ title: "JS frontend", mime: "application/javascript;env=frontend", highlightJs: "javascript", default: true },
{ title: "JSON-LD", mime: "application/ld+json", highlightJs: "json" },
{ title: "JSON", mime: "application/json", highlightJs: "json", default: true },
{ title: "JSX", mime: "text/jsx", highlightJs: "javascript" },
{ title: "Julia", mime: "text/x-julia", highlightJs: "julia" },
{ title: "Kotlin", mime: "text/x-kotlin", highlightJs: "kotlin", default: true },
{ title: "LaTeX", mime: "text/x-latex", highlightJs: "latex" },
{ title: "LESS", mime: "text/x-less", highlightJs: "less" },
{ title: "LiveScript", mime: "text/x-livescript", highlightJs: "livescript" },
{ title: "Lua", mime: "text/x-lua", highlightJs: "lua" },
{ title: "MariaDB SQL", mime: "text/x-mariadb", highlightJs: "sql" },
{ title: "Markdown", mime: "text/x-markdown", highlightJs: "markdown", default: true },
{ title: "Mathematica", mime: "text/x-mathematica", highlightJs: "mathematica" },
{ title: "mbox", mime: "application/mbox" },
{ title: "MIPS Assembler", mime: "text/x-asm-mips", highlightJs: "mipsasm" },
{ title: "mIRC", mime: "text/mirc" },
{ title: "Modelica", mime: "text/x-modelica" },
{ title: "MS SQL", mime: "text/x-mssql", highlightJs: "sql" },
{ title: "mscgen", mime: "text/x-mscgen" },
{ title: "msgenny", mime: "text/x-msgenny" },
{ title: "MUMPS", mime: "text/x-mumps" },
{ title: "MySQL", mime: "text/x-mysql", highlightJs: "sql" },
{ title: "Nginx", mime: "text/x-nginx-conf", highlightJs: "nginx" },
{ title: "NSIS", mime: "text/x-nsis", highlightJs: "nsis" },
{ title: "NTriples", mime: "application/n-triples" },
{ title: "Objective-C", mime: "text/x-objectivec", highlightJs: "objectivec" },
{ title: "OCaml", mime: "text/x-ocaml", highlightJs: "ocaml" },
{ title: "Octave", mime: "text/x-octave" },
{ title: "Oz", mime: "text/x-oz" },
{ title: "Pascal", mime: "text/x-pascal", highlightJs: "delphi" },
{ title: "PEG.js", mime: "null" },
{ title: "Perl", mime: "text/x-perl", default: true },
{ title: "PGP", mime: "application/pgp" },
{ title: "PHP", mime: "text/x-php", default: true, highlightJs: "php" },
{ title: "Pig", mime: "text/x-pig" },
{ title: "PLSQL", mime: "text/x-plsql", highlightJs: "sql" },
{ title: "PostgreSQL", mime: "text/x-pgsql", highlightJs: "pgsql" },
{ title: "PowerShell", mime: "application/x-powershell", highlightJs: "powershell" },
{ title: "Properties files", mime: "text/x-properties", highlightJs: "properties" },
{ title: "ProtoBuf", mime: "text/x-protobuf", highlightJs: "protobuf" },
{ title: "Pug", mime: "text/x-pug" },
{ title: "Puppet", mime: "text/x-puppet", highlightJs: "puppet" },
{ title: "Python", mime: "text/x-python", highlightJs: "python", default: true },
{ title: "Q", mime: "text/x-q", highlightJs: "q" },
{ title: "R", mime: "text/x-rsrc", highlightJs: "r" },
{ title: "reStructuredText", mime: "text/x-rst" },
{ title: "RPM Changes", mime: "text/x-rpm-changes" },
{ title: "RPM Spec", mime: "text/x-rpm-spec" },
{ title: "Ruby", mime: "text/x-ruby", highlightJs: "ruby", default: true },
{ title: "Rust", mime: "text/x-rustsrc", highlightJs: "rust" },
{ title: "SAS", mime: "text/x-sas", highlightJs: "sas" },
{ title: "Sass", mime: "text/x-sass", highlightJs: "scss" },
{ title: "Scala", mime: "text/x-scala" },
{ title: "Scheme", mime: "text/x-scheme" },
{ title: "SCSS", mime: "text/x-scss", highlightJs: "scss" },
{ title: "Shell (bash)", mime: "text/x-sh", highlightJs: "bash", default: true },
{ title: "Sieve", mime: "application/sieve" },
{ title: "Slim", mime: "text/x-slim" },
{ title: "Smalltalk", mime: "text/x-stsrc", highlightJs: "smalltalk" },
{ title: "Smarty", mime: "text/x-smarty" },
{ title: "SML", mime: "text/x-sml", highlightJs: "sml" },
{ title: "Solr", mime: "text/x-solr" },
{ title: "Soy", mime: "text/x-soy" },
{ title: "SPARQL", mime: "application/sparql-query" },
{ title: "Spreadsheet", mime: "text/x-spreadsheet" },
{ title: "SQL", mime: "text/x-sql", highlightJs: "sql", default: true },
{ title: "SQLite (Trilium)", mime: "text/x-sqlite;schema=trilium", highlightJs: "sql", default: true },
{ title: "SQLite", mime: "text/x-sqlite", highlightJs: "sql" },
{ title: "Squirrel", mime: "text/x-squirrel" },
{ title: "sTeX", mime: "text/x-stex" },
{ title: "Stylus", mime: "text/x-styl", highlightJs: "stylus" },
{ title: "Swift", mime: "text/x-swift", default: true },
{ title: "SystemVerilog", mime: "text/x-systemverilog" },
{ title: "Tcl", mime: "text/x-tcl", highlightJs: "tcl" },
{ title: "Terraform (HCL)", mime: "text/x-hcl", highlightJs: "terraform", highlightJsSource: "libraries", codeMirrorSource: "libraries/codemirror/hcl.js" },
{ title: "Textile", mime: "text/x-textile" },
{ title: "TiddlyWiki ", mime: "text/x-tiddlywiki" },
{ title: "Tiki wiki", mime: "text/tiki" },
{ title: "TOML", mime: "text/x-toml", highlightJs: "ini" },
{ title: "Tornado", mime: "text/x-tornado" },
{ title: "troff", mime: "text/troff" },
{ title: "TTCN_CFG", mime: "text/x-ttcn-cfg" },
{ title: "TTCN", mime: "text/x-ttcn" },
{ title: "Turtle", mime: "text/turtle" },
{ title: "Twig", mime: "text/x-twig", highlightJs: "twig" },
{ title: "TypeScript-JSX", mime: "text/typescript-jsx", highlightJs: "typescript" },
{ title: "TypeScript", mime: "application/typescript", highlightJs: "typescript" },
{ title: "VB.NET", mime: "text/x-vb", highlightJs: "vbnet" },
{ title: "VBScript", mime: "text/vbscript", highlightJs: "vbscript" },
{ title: "Velocity", mime: "text/velocity" },
{ title: "Verilog", mime: "text/x-verilog", highlightJs: "verilog" },
{ title: "VHDL", mime: "text/x-vhdl", highlightJs: "vhdl" },
{ title: "Vue.js Component", mime: "text/x-vue" },
{ title: "Web IDL", mime: "text/x-webidl" },
{ title: "XML", mime: "text/xml", highlightJs: "xml", default: true },
{ title: "XQuery", mime: "application/xquery", highlightJs: "xquery" },
{ title: "xu", mime: "text/x-xu" },
{ title: "Yacas", mime: "text/x-yacas" },
{ title: "YAML", mime: "text/x-yaml", highlightJs: "yaml", default: true },
{ title: "Z80", mime: "text/x-z80" }
]);
/**
* Given a MIME type in the usual format (e.g. `text/csrc`), it returns a MIME type that can be passed down to the CKEditor
* code plugin.
*
* @param mimeType The MIME type to normalize, in the usual format (e.g. `text/c-src`).
* @returns the normalized MIME type (e.g. `text-c-src`).
*/
export function normalizeMimeTypeForCKEditor(mimeType: string) {
return mimeType.toLowerCase().replace(/[\W_]+/g, "-");
}
let byHighlightJsNameMappings: Record<string, MimeTypeDefinition> | null = null;
/**
* Given a Highlight.js language tag (e.g. `css`), it returns a corresponding {@link MimeTypeDefinition} if found.
*
* If there are multiple {@link MimeTypeDefinition}s for the language tag, then only the first one is retrieved. For example for `javascript`, the "JS frontend" mime type is returned.
*
* @param highlightJsName a language tag.
* @returns the corresponding {@link MimeTypeDefinition} if found, or `undefined` otherwise.
*/
export function getMimeTypeFromHighlightJs(highlightJsName: string) {
if (!byHighlightJsNameMappings) {
byHighlightJsNameMappings = {};
for (const mimeType of MIME_TYPES_DICT) {
if (mimeType.highlightJs && !byHighlightJsNameMappings[mimeType.highlightJs]) {
byHighlightJsNameMappings[mimeType.highlightJs] = mimeType;
}
}
}
return byHighlightJsNameMappings[highlightJsName];
}

View File

@@ -1,62 +0,0 @@
import { MIME_TYPE_AUTO, MIME_TYPES_DICT, normalizeMimeTypeForCKEditor, type MimeTypeDefinition } from "./mime_type_definitions.js";
import options from "./options.js";
interface MimeType extends MimeTypeDefinition {
/**
* True if this mime type was enabled by the user in the "Available MIME types in the dropdown" option in the Code Notes settings.
*/
enabled: boolean;
}
let mimeTypes: MimeType[] | null = null;
function loadMimeTypes() {
mimeTypes = JSON.parse(JSON.stringify(MIME_TYPES_DICT)) as MimeType[]; // clone
const enabledMimeTypes = options.getJson("codeNotesMimeTypes") || MIME_TYPES_DICT.filter((mt) => mt.default).map((mt) => mt.mime);
for (const mt of mimeTypes) {
mt.enabled = enabledMimeTypes.includes(mt.mime) || mt.mime === "text/plain"; // text/plain is always enabled
}
}
function getMimeTypes(): MimeType[] {
if (mimeTypes === null) {
loadMimeTypes();
}
return mimeTypes as MimeType[];
}
let mimeToHighlightJsMapping: Record<string, string> | null = null;
/**
* Obtains the corresponding language tag for highlight.js for a given MIME type.
*
* The mapping is built the first time this method is built and then the results are cached for better performance.
*
* @param mimeType The MIME type of the code block, in the CKEditor-normalized format (e.g. `text-c-src` instead of `text/c-src`).
* @returns the corresponding highlight.js tag, for example `c` for `text-c-src`.
*/
function getHighlightJsNameForMime(mimeType: string) {
if (!mimeToHighlightJsMapping) {
const mimeTypes = getMimeTypes();
mimeToHighlightJsMapping = {};
for (const mimeType of mimeTypes) {
// The mime stored by CKEditor is text-x-csrc instead of text/x-csrc so we keep this format for faster lookup.
const normalizedMime = normalizeMimeTypeForCKEditor(mimeType.mime);
if (mimeType.highlightJs) {
mimeToHighlightJsMapping[normalizedMime] = mimeType.highlightJs;
}
}
}
return mimeToHighlightJsMapping[mimeType];
}
export default {
MIME_TYPE_AUTO,
getMimeTypes,
loadMimeTypes,
getHighlightJsNameForMime
};

View File

@@ -1,24 +0,0 @@
import type FAttribute from "../entities/fattribute.js";
/**
* The purpose of this class is to cache the list of attributes for notes.
*
* Cache invalidation granularity is global - whenever a write operation is detected to notes, branches or attributes,
* we invalidate the whole cache. That's OK, since the purpose for this is to speed up batch read-only operations, such
* as loading the tree which uses attributes heavily.
*/
class NoteAttributeCache {
attributes: Record<string, FAttribute[]>;
constructor() {
this.attributes = {};
}
invalidate() {
this.attributes = {};
}
}
const noteAttributeCache = new NoteAttributeCache();
export default noteAttributeCache;

View File

@@ -1,397 +0,0 @@
import server from "./server.js";
import appContext from "../components/app_context.js";
import noteCreateService from "./note_create.js";
import froca from "./froca.js";
import { t } from "./i18n.js";
// this key needs to have this value, so it's hit by the tooltip
const SELECTED_NOTE_PATH_KEY = "data-note-path";
const SELECTED_EXTERNAL_LINK_KEY = "data-external-link";
// To prevent search lag when there are a large number of notes, set a delay based on the number of notes to avoid jitter.
const notesCount = await server.get<number>(`autocomplete/notesCount`);
let debounceTimeoutId: ReturnType<typeof setTimeout>;
function getSearchDelay(notesCount: number): number {
const maxNotes = 20000;
const maxDelay = 1000;
const delay = Math.min(maxDelay, (notesCount / maxNotes) * maxDelay);
return delay;
}
let searchDelay = getSearchDelay(notesCount);
// TODO: Deduplicate with server.
export interface Suggestion {
noteTitle?: string;
externalLink?: string;
notePathTitle?: string;
notePath?: string;
highlightedNotePathTitle?: string;
action?: string | "create-note" | "search-notes" | "external-link";
parentNoteId?: string;
icon?: string;
}
interface Options {
container?: HTMLElement;
fastSearch?: boolean;
allowCreatingNotes?: boolean;
allowJumpToSearchNotes?: boolean;
allowExternalLinks?: boolean;
hideGoToSelectedNoteButton?: boolean;
}
async function autocompleteSourceForCKEditor(queryText: string) {
return await new Promise<MentionItem[]>((res, rej) => {
autocompleteSource(
queryText,
(rows) => {
res(
rows.map((row) => {
return {
action: row.action,
noteTitle: row.noteTitle,
id: `@${row.notePathTitle}`,
name: row.notePathTitle || "",
link: `#${row.notePath}`,
notePath: row.notePath,
highlightedNotePathTitle: row.highlightedNotePathTitle
};
})
);
},
{
allowCreatingNotes: true
}
);
});
}
async function autocompleteSource(term: string, cb: (rows: Suggestion[]) => void, options: Options = {}) {
const fastSearch = options.fastSearch === false ? false : true;
if (fastSearch === false) {
if (term.trim().length === 0) {
return;
}
cb([
{
noteTitle: term,
highlightedNotePathTitle: t("quick-search.searching")
}
]);
}
const activeNoteId = appContext.tabManager.getActiveContextNoteId();
const length = term.trim().length;
let results = await server.get<Suggestion[]>(`autocomplete?query=${encodeURIComponent(term)}&activeNoteId=${activeNoteId}&fastSearch=${fastSearch}`);
options.fastSearch = true;
if (length >= 1 && options.allowCreatingNotes) {
results = [
{
action: "create-note",
noteTitle: term,
parentNoteId: activeNoteId || "root",
highlightedNotePathTitle: t("note_autocomplete.create-note", { term })
} as Suggestion
].concat(results);
}
if (length >= 1 && options.allowJumpToSearchNotes) {
results = results.concat([
{
action: "search-notes",
noteTitle: term,
highlightedNotePathTitle: `${t("note_autocomplete.search-for", { term })} <kbd style='color: var(--muted-text-color); background-color: transparent; float: right;'>Ctrl+Enter</kbd>`
}
]);
}
if (term.match(/^[a-z]+:\/\/.+/i) && options.allowExternalLinks) {
results = [
{
action: "external-link",
externalLink: term,
highlightedNotePathTitle: t("note_autocomplete.insert-external-link", { term })
} as Suggestion
].concat(results);
}
cb(results);
}
function clearText($el: JQuery<HTMLElement>) {
searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", "").trigger("change");
}
function setText($el: JQuery<HTMLElement>, text: string) {
$el.setSelectedNotePath("");
$el.autocomplete("val", text.trim()).autocomplete("open");
}
function showRecentNotes($el: JQuery<HTMLElement>) {
searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", "");
$el.autocomplete("open");
$el.trigger("focus");
}
function fullTextSearch($el: JQuery<HTMLElement>, options: Options) {
const searchString = $el.autocomplete("val") as unknown as string;
if (options.fastSearch === false || searchString?.trim().length === 0) {
return;
}
$el.trigger("focus");
options.fastSearch = false;
$el.autocomplete("val", "");
$el.setSelectedNotePath("");
searchDelay = 0;
$el.autocomplete("val", searchString);
}
function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
if ($el.hasClass("note-autocomplete-input")) {
// clear any event listener added in previous invocation of this function
$el.off("autocomplete:noteselected");
return $el;
}
options = options || {};
// Used to track whether the user is performing character composition with an input method (such as Chinese Pinyin, Japanese, Korean, etc.) and to avoid triggering a search during the composition process.
let isComposingInput = false;
$el.on("compositionstart", () => {
isComposingInput = true;
});
$el.on("compositionend", () => {
isComposingInput = false;
const searchString = $el.autocomplete("val") as unknown as string;
$el.autocomplete("val", "");
$el.autocomplete("val", searchString);
});
$el.addClass("note-autocomplete-input");
const $clearTextButton = $("<a>").addClass("input-group-text input-clearer-button bx bxs-tag-x").prop("title", t("note_autocomplete.clear-text-field"));
const $showRecentNotesButton = $("<a>").addClass("input-group-text show-recent-notes-button bx bx-time").prop("title", t("note_autocomplete.show-recent-notes"));
const $fullTextSearchButton = $("<a>")
.addClass("input-group-text full-text-search-button bx bx-search")
.prop("title", `${t("note_autocomplete.full-text-search")} (Shift+Enter)`);
const $goToSelectedNoteButton = $("<a>").addClass("input-group-text go-to-selected-note-button bx bx-arrow-to-right");
$el.after($clearTextButton).after($showRecentNotesButton).after($fullTextSearchButton);
if (!options.hideGoToSelectedNoteButton) {
$el.after($goToSelectedNoteButton);
}
$clearTextButton.on("click", () => clearText($el));
$showRecentNotesButton.on("click", (e) => {
showRecentNotes($el);
// this will cause the click not give focus to the "show recent notes" button
// this is important because otherwise input will lose focus immediately and not show the results
return false;
});
$fullTextSearchButton.on("click", (e) => {
fullTextSearch($el, options);
return false;
});
let autocompleteOptions: AutoCompleteConfig = {};
if (options.container) {
autocompleteOptions.dropdownMenuContainer = options.container;
autocompleteOptions.debug = true; // don't close on blur
}
if (options.allowJumpToSearchNotes) {
$el.on("keydown", (event) => {
if (event.ctrlKey && event.key === "Enter") {
// Prevent Ctrl + Enter from triggering autoComplete.
event.stopImmediatePropagation();
event.preventDefault();
$el.trigger("autocomplete:selected", { action: "search-notes", noteTitle: $el.autocomplete("val") });
}
});
}
$el.on("keydown", async (event) => {
if (event.shiftKey && event.key === "Enter") {
// Prevent Enter from triggering autoComplete.
event.stopImmediatePropagation();
event.preventDefault();
fullTextSearch($el, options);
}
});
$el.autocomplete(
{
...autocompleteOptions,
appendTo: document.querySelector("body"),
hint: false,
autoselect: true,
// openOnFocus has to be false, otherwise re-focus (after return from note type chooser dialog) forces
// re-querying of the autocomplete source which then changes the currently selected suggestion
openOnFocus: false,
minLength: 0,
tabAutocomplete: false
},
[
{
source: (term, cb) => {
clearTimeout(debounceTimeoutId);
debounceTimeoutId = setTimeout(() => {
if (isComposingInput) {
return;
}
autocompleteSource(term, cb, options);
}, searchDelay);
if (searchDelay === 0) {
searchDelay = getSearchDelay(notesCount);
}
},
displayKey: "notePathTitle",
templates: {
suggestion: (suggestion) => `<span class="${suggestion.icon ?? "bx bx-note"}"></span> ${suggestion.highlightedNotePathTitle}`
},
// we can't cache identical searches because notes can be created / renamed, new recent notes can be added
cache: false
}
]
);
// TODO: Types fail due to "autocomplete:selected" not being registered in type definitions.
($el as any).on("autocomplete:selected", async (event: Event, suggestion: Suggestion) => {
if (suggestion.action === "external-link") {
$el.setSelectedNotePath(null);
$el.setSelectedExternalLink(suggestion.externalLink);
$el.autocomplete("val", suggestion.externalLink);
$el.autocomplete("close");
$el.trigger("autocomplete:externallinkselected", [suggestion]);
return;
}
if (suggestion.action === "create-note") {
const { success, noteType, templateNoteId } = await noteCreateService.chooseNoteType();
if (!success) {
return;
}
const { note } = await noteCreateService.createNote(suggestion.parentNoteId, {
title: suggestion.noteTitle,
activate: false,
type: noteType,
templateNoteId: templateNoteId
});
const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
suggestion.notePath = note?.getBestNotePathString(hoistedNoteId);
}
if (suggestion.action === "search-notes") {
const searchString = suggestion.noteTitle;
appContext.triggerCommand("searchNotes", { searchString });
return;
}
$el.setSelectedNotePath(suggestion.notePath);
$el.setSelectedExternalLink(null);
$el.autocomplete("val", suggestion.noteTitle);
$el.autocomplete("close");
$el.trigger("autocomplete:noteselected", [suggestion]);
});
$el.on("autocomplete:closed", () => {
if (!String($el.val())?.trim()) {
clearText($el);
}
});
$el.on("autocomplete:opened", () => {
if ($el.attr("readonly")) {
$el.autocomplete("close");
}
});
// clear any event listener added in previous invocation of this function
$el.off("autocomplete:noteselected");
return $el;
}
function init() {
$.fn.getSelectedNotePath = function () {
if (!String($(this).val())?.trim()) {
return "";
} else {
return $(this).attr(SELECTED_NOTE_PATH_KEY);
}
};
$.fn.getSelectedNoteId = function () {
const $el = $(this as unknown as HTMLElement);
const notePath = $el.getSelectedNotePath();
if (!notePath) {
return null;
}
const chunks = notePath.split("/");
return chunks.length >= 1 ? chunks[chunks.length - 1] : null;
};
$.fn.setSelectedNotePath = function (notePath) {
notePath = notePath || "";
$(this).attr(SELECTED_NOTE_PATH_KEY, notePath);
$(this).closest(".input-group").find(".go-to-selected-note-button").toggleClass("disabled", !notePath.trim()).attr("href", `#${notePath}`); // we also set href here so tooltip can be displayed
};
$.fn.getSelectedExternalLink = function () {
if (!String($(this).val())?.trim()) {
return "";
} else {
return $(this).attr(SELECTED_EXTERNAL_LINK_KEY);
}
};
$.fn.setSelectedExternalLink = function (externalLink: string | null) {
$(this).attr(SELECTED_EXTERNAL_LINK_KEY, externalLink);
$(this).closest(".input-group").find(".go-to-selected-note-button").toggleClass("disabled", true);
};
$.fn.setNote = async function (noteId) {
const note = noteId ? await froca.getNote(noteId, true) : null;
$(this)
.val(note ? note.title : "")
.setSelectedNotePath(noteId);
};
}
export default {
autocompleteSourceForCKEditor,
initNoteAutocomplete,
showRecentNotes,
setText,
init
};

View File

@@ -1,166 +0,0 @@
import appContext from "../components/app_context.js";
import protectedSessionHolder from "./protected_session_holder.js";
import server from "./server.js";
import ws from "./ws.js";
import froca from "./froca.js";
import treeService from "./tree.js";
import toastService from "./toast.js";
import { t } from "./i18n.js";
import type FNote from "../entities/fnote.js";
import type FBranch from "../entities/fbranch.js";
import type { ChooseNoteTypeResponse } from "../widgets/dialogs/note_type_chooser.js";
interface CreateNoteOpts {
isProtected?: boolean;
saveSelection?: boolean;
title?: string | null;
content?: string | null;
type?: string;
mime?: string;
templateNoteId?: string;
activate?: boolean;
focus?: "title" | "content";
target?: string;
targetBranchId?: string;
textEditor?: TextEditor;
}
interface Response {
// TODO: Deduplicate with server once we have client/server architecture.
note: FNote;
branch: FBranch;
}
interface DuplicateResponse {
// TODO: Deduplicate with server once we have client/server architecture.
note: FNote;
}
async function createNote(parentNotePath: string | undefined, options: CreateNoteOpts = {}) {
options = Object.assign(
{
activate: true,
focus: "title",
target: "into"
},
options
);
// if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted,
// but this is quite weird since the user doesn't see WHERE the note is being created, so it shouldn't occur often
if (!options.isProtected || !protectedSessionHolder.isProtectedSessionAvailable()) {
options.isProtected = false;
}
if (appContext.tabManager.getActiveContextNoteType() !== "text") {
options.saveSelection = false;
}
if (options.saveSelection && options.textEditor) {
[options.title, options.content] = parseSelectedHtml(options.textEditor.getSelectedHtml());
}
const parentNoteId = treeService.getNoteIdFromUrl(parentNotePath);
if (options.type === "mermaid" && !options.content && !options.templateNoteId) {
options.content = `graph TD;
A-->B;
A-->C;
B-->D;
C-->D;`;
}
const { note, branch } = await server.post<Response>(`notes/${parentNoteId}/children?target=${options.target}&targetBranchId=${options.targetBranchId || ""}`, {
title: options.title,
content: options.content || "",
isProtected: options.isProtected,
type: options.type,
mime: options.mime,
templateNoteId: options.templateNoteId
});
if (options.saveSelection) {
// we remove the selection only after it was saved to server to make sure we don't lose anything
options.textEditor?.removeSelection();
}
await ws.waitForMaxKnownEntityChangeId();
const activeNoteContext = appContext.tabManager.getActiveContext();
if (activeNoteContext && options.activate) {
await activeNoteContext.setNote(`${parentNotePath}/${note.noteId}`);
if (options.focus === "title") {
appContext.triggerEvent("focusAndSelectTitle", { isNewNote: true });
} else if (options.focus === "content") {
appContext.triggerEvent("focusOnDetail", { ntxId: activeNoteContext.ntxId });
}
}
const noteEntity = await froca.getNote(note.noteId);
const branchEntity = froca.getBranch(branch.branchId);
return {
note: noteEntity,
branch: branchEntity
};
}
async function chooseNoteType() {
return new Promise<ChooseNoteTypeResponse>((res) => {
// TODO: Remove ignore after callback for chooseNoteType is defined in app_context.ts
//@ts-ignore
appContext.triggerCommand("chooseNoteType", { callback: res });
});
}
async function createNoteWithTypePrompt(parentNotePath: string, options: CreateNoteOpts = {}) {
const { success, noteType, templateNoteId } = await chooseNoteType();
if (!success) {
return;
}
options.type = noteType;
options.templateNoteId = templateNoteId;
return await createNote(parentNotePath, options);
}
/* If the first element is heading, parse it out and use it as a new heading. */
function parseSelectedHtml(selectedHtml: string) {
const dom = $.parseHTML(selectedHtml);
// TODO: tagName and outerHTML appear to be missing.
//@ts-ignore
if (dom.length > 0 && dom[0].tagName && dom[0].tagName.match(/h[1-6]/i)) {
const title = $(dom[0]).text();
// remove the title from content (only first occurrence)
// TODO: tagName and outerHTML appear to be missing.
//@ts-ignore
const content = selectedHtml.replace(dom[0].outerHTML, "");
return [title, content];
} else {
return [null, selectedHtml];
}
}
async function duplicateSubtree(noteId: string, parentNotePath: string) {
const parentNoteId = treeService.getNoteIdFromUrl(parentNotePath);
const { note } = await server.post<DuplicateResponse>(`notes/${noteId}/duplicate/${parentNoteId}`);
await ws.waitForMaxKnownEntityChangeId();
appContext.tabManager.getActiveContext()?.setNote(`${parentNotePath}/${note.noteId}`);
const origNote = await froca.getNote(noteId);
toastService.showMessage(t("note_create.duplicated", { title: origNote?.title }));
}
export default {
createNote,
createNoteWithTypePrompt,
duplicateSubtree,
chooseNoteType
};

View File

@@ -1,55 +0,0 @@
import type FNote from "../entities/fnote.js";
import CalendarView from "../widgets/view_widgets/calendar_view.js";
import ListOrGridView from "../widgets/view_widgets/list_or_grid_view.js";
import type { ViewModeArgs } from "../widgets/view_widgets/view_mode.js";
import type ViewMode from "../widgets/view_widgets/view_mode.js";
export type ViewTypeOptions = "list" | "grid" | "calendar";
export default class NoteListRenderer {
private viewType: ViewTypeOptions;
public viewMode: ViewMode | null;
constructor($parent: JQuery<HTMLElement>, parentNote: FNote, noteIds: string[], showNotePath: boolean = false) {
this.viewType = this.#getViewType(parentNote);
const args: ViewModeArgs = {
$parent,
parentNote,
noteIds,
showNotePath
};
if (this.viewType === "list" || this.viewType === "grid") {
this.viewMode = new ListOrGridView(this.viewType, args);
} else if (this.viewType === "calendar") {
this.viewMode = new CalendarView(args);
} else {
this.viewMode = null;
}
}
#getViewType(parentNote: FNote): ViewTypeOptions {
const viewType = parentNote.getLabelValue("viewType");
if (!["list", "grid", "calendar"].includes(viewType || "")) {
// when not explicitly set, decide based on the note type
return parentNote.type === "search" ? "list" : "grid";
} else {
return viewType as ViewTypeOptions;
}
}
get isFullHeight() {
return this.viewMode?.isFullHeight;
}
async renderList() {
if (!this.viewMode) {
return null;
}
return await this.viewMode.renderList();
}
}

View File

@@ -1,186 +0,0 @@
import treeService from "./tree.js";
import linkService from "./link.js";
import froca from "./froca.js";
import utils from "./utils.js";
import attributeRenderer from "./attribute_renderer.js";
import contentRenderer from "./content_renderer.js";
import appContext from "../components/app_context.js";
import type FNote from "../entities/fnote.js";
import { t } from "./i18n.js";
function setupGlobalTooltip() {
$(document).on("mouseenter", "a", mouseEnterHandler);
// close any note tooltip after click, this fixes the problem that sometimes tooltips remained on the screen
$(document).on("click", (e) => {
if ($(e.target).closest(".note-tooltip").length) {
// click within the tooltip shouldn't close it
return;
}
dismissAllTooltips();
});
}
function dismissAllTooltips() {
$(".note-tooltip").remove();
}
function setupElementTooltip($el: JQuery<HTMLElement>) {
$el.on("mouseenter", mouseEnterHandler);
}
async function mouseEnterHandler(this: HTMLElement) {
const $link = $(this);
if ($link.hasClass("no-tooltip-preview") || $link.hasClass("disabled")) {
return;
} else if ($link.closest(".ck-link-actions").length) {
// this is to avoid showing tooltip from inside the CKEditor link editor dialog
return;
} else if ($link.closest(".note-tooltip").length) {
// don't show tooltip for links within tooltip
return;
}
const url = $link.attr("href") || $link.attr("data-href");
const { notePath, noteId, viewScope } = linkService.parseNavigationStateFromUrl(url);
if (url?.startsWith("#fnref")) {
// The "^" symbol from footnotes within text notes, doesn't require a tooltip.
return;
}
if (!notePath || !noteId || viewScope?.viewMode !== "default") {
return;
}
const linkId = $link.attr("data-link-id") || `link-${Math.floor(Math.random() * 1000000)}`;
$link.attr("data-link-id", linkId);
if ($(`.${linkId}`).is(":visible")) {
// tooltip is already open for this link
return;
}
let renderPromise;
if (url?.startsWith("#fn")) {
renderPromise = renderFootnote($link, url);
} else {
renderPromise = renderTooltip(await froca.getNote(noteId));
}
const [content] = await Promise.all([
renderPromise,
// to reduce flicker due to accidental mouseover, cursor must stay for a bit over the link for tooltip to appear
new Promise((res) => setTimeout(res, 500))
]);
if (!content || utils.isHtmlEmpty(content)) {
return;
}
const html = `<div class="note-tooltip-content">${content}</div>`;
const tooltipClass = "tooltip-" + Math.floor(Math.random() * 999_999_999);
// we need to check if we're still hovering over the element
// since the operation to get tooltip content was async, it is possible that
// we now create tooltip which won't close because it won't receive mouseleave event
if ($(this).filter(":hover").length > 0) {
$(this).tooltip({
container: "body",
// https://github.com/zadam/trilium/issues/2794 https://github.com/zadam/trilium/issues/2988
// with bottom this flickering happens a bit less
placement: "bottom",
trigger: "manual",
//TODO: boundary No longer applicable?
//boundary: 'window',
title: html,
html: true,
template: `<div class="tooltip note-tooltip ${tooltipClass}" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>`,
sanitize: false,
customClass: linkId
});
dismissAllTooltips();
$(this).tooltip("show");
// Dismiss the tooltip immediately if a link was clicked inside the tooltip.
$(`.${tooltipClass} a`).on("click", (e) => {
dismissAllTooltips();
});
// the purpose of the code below is to:
// - allow user to go from hovering the link to hovering the tooltip to be able to scroll,
// click on links within tooltip etc. without tooltip disappearing
// - once the user moves the cursor away from both link and the tooltip, hide the tooltip
const checkTooltip = () => {
if (!$(this).filter(":hover").length && !$(`.${linkId}:hover`).length) {
// cursor is neither over the link nor over the tooltip, user likely is not interested
dismissAllTooltips();
} else {
setTimeout(checkTooltip, 1000);
}
};
setTimeout(checkTooltip, 1000);
}
}
async function renderTooltip(note: FNote | null) {
if (!note) {
return `<div>${t("note_tooltip.note-has-been-deleted")}</div>`;
}
const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
const bestNotePath = note.getBestNotePathString(hoistedNoteId);
if (!bestNotePath) {
return;
}
const noteTitleWithPathAsSuffix = await treeService.getNoteTitleWithPathAsSuffix(bestNotePath);
const { $renderedAttributes } = await attributeRenderer.renderNormalAttributes(note);
const { $renderedContent } = await contentRenderer.getRenderedContent(note, {
tooltip: true,
trim: true
});
const isContentEmpty = $renderedContent[0].innerHTML.length === 0;
let content = "";
if (noteTitleWithPathAsSuffix) {
const classes = ["note-tooltip-title"];
if (isContentEmpty) {
classes.push("note-no-content");
}
content = `<h5 class="${classes.join(" ")}"><a href="#${note.noteId}" data-no-context-menu="true">${noteTitleWithPathAsSuffix.prop("outerHTML")}</a></h5>`;
}
content = `${content}<div class="note-tooltip-attributes">${$renderedAttributes[0].outerHTML}</div>`;
if (!isContentEmpty) {
content += $renderedContent[0].outerHTML;
}
return content;
}
function renderFootnote($link: JQuery<HTMLElement>, url: string) {
// A footnote text reference
const footnoteRef = url.substring(3);
const $footnoteContent = $link
.closest(".ck-content") // find the parent CK content
.find("> .footnote-section") // find the footnote section
.find(`a[href="#fnref${footnoteRef}"]`) // find the footnote link
.closest(".footnote-item") // find the parent container of the footnote
.find(".footnote-content"); // find the actual text content of the footnote
return $footnoteContent.html() || "";
}
export default {
setupGlobalTooltip,
setupElementTooltip,
dismissAllTooltips
};

View File

@@ -1,45 +0,0 @@
import server from "./server.js";
import froca from "./froca.js";
import { t } from "./i18n.js";
import type { MenuItem } from "../menus/context_menu.js";
import type { TreeCommandNames } from "../menus/tree_context_menu.js";
async function getNoteTypeItems(command?: TreeCommandNames) {
const items: MenuItem<TreeCommandNames>[] = [
{ title: t("note_types.text"), command, type: "text", uiIcon: "bx bx-note" },
{ title: t("note_types.code"), command, type: "code", uiIcon: "bx bx-code" },
{ title: t("note_types.saved-search"), command, type: "search", uiIcon: "bx bx-file-find" },
{ title: t("note_types.relation-map"), command, type: "relationMap", uiIcon: "bx bxs-network-chart" },
{ title: t("note_types.note-map"), command, type: "noteMap", uiIcon: "bx bxs-network-chart" },
{ title: t("note_types.render-note"), command, type: "render", uiIcon: "bx bx-extension" },
{ title: t("note_types.book"), command, type: "book", uiIcon: "bx bx-book" },
{ title: t("note_types.mermaid-diagram"), command, type: "mermaid", uiIcon: "bx bx-selection" },
{ title: t("note_types.canvas"), command, type: "canvas", uiIcon: "bx bx-pen" },
{ title: t("note_types.web-view"), command, type: "webView", uiIcon: "bx bx-globe-alt" },
{ title: t("note_types.mind-map"), command, type: "mindMap", uiIcon: "bx bx-sitemap" },
{ title: t("note_types.geo-map"), command, type: "geoMap", uiIcon: "bx bx-map-alt" },
];
const templateNoteIds = await server.get<string[]>("search-templates");
const templateNotes = await froca.getNotes(templateNoteIds);
if (templateNotes.length > 0) {
items.push({ title: "----" });
for (const templateNote of templateNotes) {
items.push({
title: templateNote.title,
uiIcon: templateNote.getIcon(),
command: command,
type: templateNote.type,
templateNoteId: templateNote.noteId
});
}
}
return items;
}
export default {
getNoteTypeItems
};

View File

@@ -1,202 +0,0 @@
import utils from "./utils.js";
import server from "./server.js";
type ExecFunction = (command: string, cb: (err: string, stdout: string, stderror: string) => void) => void;
interface TmpResponse {
tmpFilePath: string;
}
function checkType(type: string) {
if (type !== "notes" && type !== "attachments") {
throw new Error(`Unrecognized type '${type}', should be 'notes' or 'attachments'`);
}
}
function getFileUrl(type: string, noteId?: string) {
checkType(type);
return getUrlForDownload(`api/${type}/${noteId}/download`);
}
function getOpenFileUrl(type: string, noteId: string) {
checkType(type);
return getUrlForDownload(`api/${type}/${noteId}/open`);
}
function download(url: string) {
if (utils.isElectron()) {
const remote = utils.dynamicRequire("@electron/remote");
remote.getCurrentWebContents().downloadURL(url);
} else {
window.location.href = url;
}
}
function downloadFileNote(noteId: string) {
const url = `${getFileUrl("notes", noteId)}?${Date.now()}`; // don't use cache
download(url);
}
function downloadAttachment(attachmentId: string) {
const url = `${getFileUrl("attachments", attachmentId)}?${Date.now()}`; // don't use cache
download(url);
}
async function openCustom(type: string, entityId: string, mime: string) {
checkType(type);
if (!utils.isElectron() || utils.isMac()) {
return;
}
const resp = await server.post<TmpResponse>(`${type}/${entityId}/save-to-tmp-dir`);
let filePath = resp.tmpFilePath;
const exec = utils.dynamicRequire("child_process").exec as ExecFunction;
const platform = process.platform;
if (platform === "linux") {
// we don't know which terminal is available, try in succession
const terminals = ["x-terminal-emulator", "gnome-terminal", "konsole", "xterm", "xfce4-terminal", "mate-terminal", "rxvt", "terminator", "terminology"];
const openFileWithTerminal = (terminal: string) => {
const command = `${terminal} -e 'mimeopen -d "${filePath}"'`;
console.log(`Open Note custom: ${command} `);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Open Note custom: Failed to open file with ${terminal}: ${error}`);
searchTerminal(terminals.indexOf(terminal) + 1);
} else {
console.log(`Open Note custom: File opened with ${terminal}: ${stdout}`);
}
});
};
const searchTerminal = (index: number) => {
const terminal = terminals[index];
if (!terminal) {
console.error("Open Note custom: No terminal found!");
// TODO: Remove {url: true} if not needed.
(open as any)(getFileUrl(type, entityId), { url: true });
return;
}
exec(`which ${terminal}`, (error, stdout, stderr) => {
if (stdout.trim()) {
openFileWithTerminal(terminal);
} else {
searchTerminal(index + 1);
}
});
};
searchTerminal(0);
} else if (platform === "win32") {
if (filePath.indexOf("/") !== -1) {
// Note that the path separator must be \ instead of /
filePath = filePath.replace(/\//g, "\\");
}
const command = `rundll32.exe shell32.dll,OpenAs_RunDLL ` + filePath;
exec(command, (err, stdout, stderr) => {
if (err) {
console.error("Open Note custom: ", err);
// TODO: This appears to be broken, since getFileUrl expects two arguments, with the first one being the type.
// Also don't know why {url: true} is passed.
(open as any)(getFileUrl(entityId), { url: true });
return;
}
});
} else {
console.log('Currently "Open Note custom" only supports linux and windows systems');
// TODO: This appears to be broken, since getFileUrl expects two arguments, with the first one being the type.
// Also don't know why {url: true} is passed.
(open as any)(getFileUrl(entityId), { url: true });
}
}
const openNoteCustom = async (noteId: string, mime: string) => await openCustom("notes", noteId, mime);
const openAttachmentCustom = async (attachmentId: string, mime: string) => await openCustom("attachments", attachmentId, mime);
function downloadRevision(noteId: string, revisionId: string) {
const url = getUrlForDownload(`api/revisions/${revisionId}/download`);
download(url);
}
/**
* @param url - should be without initial slash!!!
*/
function getUrlForDownload(url: string) {
if (utils.isElectron()) {
// electron needs absolute URL, so we extract current host, port, protocol
return `${getHost()}/${url}`;
} else {
// web server can be deployed on subdomain, so we need to use a relative path
return url;
}
}
function canOpenInBrowser(mime: string) {
return mime === "application/pdf" || mime.startsWith("image") || mime.startsWith("audio") || mime.startsWith("video");
}
async function openExternally(type: string, entityId: string, mime: string) {
checkType(type);
if (utils.isElectron()) {
const resp = await server.post<TmpResponse>(`${type}/${entityId}/save-to-tmp-dir`);
const electron = utils.dynamicRequire("electron");
const res = await electron.shell.openPath(resp.tmpFilePath);
if (res) {
// fallback in case there's no default application for this file
window.open(getFileUrl(type, entityId));
}
} else {
// allow browser to handle opening common file
if (canOpenInBrowser(mime)) {
window.open(getOpenFileUrl(type, entityId));
} else {
window.location.href = getFileUrl(type, entityId);
}
}
}
const openNoteExternally = async (noteId: string, mime: string) => await openExternally("notes", noteId, mime);
const openAttachmentExternally = async (attachmentId: string, mime: string) => await openExternally("attachments", attachmentId, mime);
function getHost() {
const url = new URL(window.location.href);
return `${url.protocol}//${url.hostname}:${url.port}`;
}
async function openDirectory(directory: string) {
try {
if (utils.isElectron()) {
const electron = utils.dynamicRequire("electron");
const res = await electron.shell.openPath(directory);
if (res) {
console.error("Failed to open directory:", res);
}
} else {
console.error("Not running in an Electron environment.");
}
} catch (err: any) {
// Handle file system errors (e.g. path does not exist or is inaccessible)
console.error("Error:", err.message);
}
}
export default {
download,
downloadFileNote,
downloadRevision,
downloadAttachment,
getUrlForDownload,
openNoteExternally,
openAttachmentExternally,
openNoteCustom,
openAttachmentCustom,
openDirectory
};

View File

@@ -1,81 +0,0 @@
import server from "./server.js";
type OptionValue = number | string;
class Options {
initializedPromise: Promise<void>;
private arr!: Record<string, OptionValue>;
constructor() {
this.initializedPromise = server.get<Record<string, OptionValue>>("options").then((data) => this.load(data));
}
load(arr: Record<string, OptionValue>) {
this.arr = arr;
}
get(key: string) {
return this.arr?.[key] as string;
}
getNames() {
return Object.keys(this.arr || []);
}
getJson(key: string) {
const value = this.arr?.[key];
if (typeof value !== "string") {
return null;
}
try {
return JSON.parse(value);
} catch (e) {
return null;
}
}
getInt(key: string) {
const value = this.arr?.[key];
if (typeof value === "number") {
return value;
}
if (typeof value == "string") {
return parseInt(value);
}
console.warn("Attempting to read int for unsupported value: ", value);
return null;
}
getFloat(key: string) {
const value = this.arr?.[key];
if (typeof value !== "string") {
return null;
}
return parseFloat(value);
}
is(key: string) {
return this.arr[key] === "true";
}
set(key: string, value: OptionValue) {
this.arr[key] = value;
}
async save(key: string, value: OptionValue) {
this.set(key, value);
const payload: Record<string, OptionValue> = {};
payload[key] = value;
await server.put(`options`, payload);
}
async toggle(key: string) {
await this.save(key, (!this.is(key)).toString());
}
}
const options = new Options();
export default options;

View File

@@ -1,46 +0,0 @@
type LabelType = "text" | "number" | "boolean" | "date" | "datetime" | "time" | "url";
type Multiplicity = "single" | "multi";
interface DefinitionObject {
isPromoted?: boolean;
labelType?: LabelType;
multiplicity?: Multiplicity;
numberPrecision?: number;
promotedAlias?: string;
inverseRelation?: string;
}
function parse(value: string) {
const tokens = value.split(",").map((t) => t.trim());
const defObj: DefinitionObject = {};
for (const token of tokens) {
if (token === "promoted") {
defObj.isPromoted = true;
} else if (["text", "number", "boolean", "date", "datetime", "time", "url"].includes(token)) {
defObj.labelType = token as LabelType;
} else if (["single", "multi"].includes(token)) {
defObj.multiplicity = token as Multiplicity;
} else if (token.startsWith("precision")) {
const chunks = token.split("=");
defObj.numberPrecision = parseInt(chunks[1]);
} else if (token.startsWith("alias")) {
const chunks = token.split("=");
defObj.promotedAlias = chunks[1];
} else if (token.startsWith("inverse")) {
const chunks = token.split("=");
defObj.inverseRelation = chunks[1];
} else {
console.log("Unrecognized attribute definition token:", token);
}
}
return defObj;
}
export default {
parse
};

View File

@@ -1,138 +0,0 @@
import server from "./server.js";
import protectedSessionHolder from "./protected_session_holder.js";
import toastService from "./toast.js";
import type { ToastOptions } from "./toast.js";
import ws from "./ws.js";
import appContext from "../components/app_context.js";
import froca from "./froca.js";
import utils from "./utils.js";
import options from "./options.js";
import { t } from "./i18n.js";
let protectedSessionDeferred: JQuery.Deferred<any, any, any> | null = null;
// TODO: Deduplicate with server when possible.
interface Response {
success: boolean;
}
interface Message {
taskId: string;
data: {
protect: boolean;
};
}
async function leaveProtectedSession() {
if (protectedSessionHolder.isProtectedSessionAvailable()) {
await protectedSessionHolder.resetProtectedSession();
}
}
/** returned promise resolves with true if new protected session was established, false if no action was necessary */
function enterProtectedSession() {
const dfd = $.Deferred();
if (!options.is("isPasswordSet")) {
appContext.triggerCommand("showPasswordNotSet");
return dfd;
}
if (protectedSessionHolder.isProtectedSessionAvailable()) {
dfd.resolve(false);
} else {
// using deferred instead of promise because it allows resolving from the outside
protectedSessionDeferred = dfd;
appContext.triggerCommand("showProtectedSessionPasswordDialog");
}
return dfd.promise();
}
async function reloadData() {
const allNoteIds = Object.keys(froca.notes);
await froca.loadInitialTree();
// make sure that all notes used in the application are loaded, including the ones not shown in the tree
await froca.reloadNotes(allNoteIds);
}
async function setupProtectedSession(password: string) {
const response = await server.post<Response>("login/protected", { password: password });
if (!response.success) {
toastService.showError(t("protected_session.wrong_password"), 3000);
return;
}
protectedSessionHolder.enableProtectedSession();
}
ws.subscribeToMessages(async (message) => {
if (message.type === "protectedSessionLogin") {
await reloadData();
await appContext.triggerEvent("frocaReloaded", {});
appContext.triggerEvent("protectedSessionStarted", {});
appContext.triggerCommand("closeProtectedSessionPasswordDialog");
if (protectedSessionDeferred !== null) {
protectedSessionDeferred.resolve(true);
protectedSessionDeferred = null;
}
toastService.showMessage(t("protected_session.started"));
} else if (message.type === "protectedSessionLogout") {
utils.reloadFrontendApp(`Protected session logout`);
}
});
async function protectNote(noteId: string, protect: boolean, includingSubtree: boolean) {
await enterProtectedSession();
await server.put(`notes/${noteId}/protect/${protect ? 1 : 0}?subtree=${includingSubtree ? 1 : 0}`);
}
function makeToast(message: Message, title: string, text: string): ToastOptions {
return {
id: message.taskId,
title,
message: text,
icon: message.data.protect ? "check-shield" : "shield"
};
}
ws.subscribeToMessages(async (message) => {
if (message.taskType !== "protectNotes") {
return;
}
const isProtecting = message.data.protect;
const title = isProtecting ? t("protected_session.protecting-title") : t("protected_session.unprotecting-title");
if (message.type === "taskError") {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
const count = message.progressCount;
const text = isProtecting ? t("protected_session.protecting-in-progress", { count }) : t("protected_session.unprotecting-in-progress-count", { count });
toastService.showPersistent(makeToast(message, title, text));
} else if (message.type === "taskSucceeded") {
const text = isProtecting ? t("protected_session.protecting-finished-successfully") : t("protected_session.unprotecting-finished-successfully");
const toast = makeToast(message, title, text);
toast.closeAfter = 3000;
toastService.showPersistent(toast);
}
});
export default {
protectNote,
enterProtectedSession,
leaveProtectedSession,
setupProtectedSession
};

View File

@@ -1,36 +0,0 @@
import type FNote from "../entities/fnote.js";
import server from "./server.js";
function enableProtectedSession() {
glob.isProtectedSessionAvailable = true;
touchProtectedSession();
}
async function resetProtectedSession() {
await server.post("logout/protected");
}
function isProtectedSessionAvailable() {
return glob.isProtectedSessionAvailable;
}
async function touchProtectedSession() {
if (isProtectedSessionAvailable()) {
await server.post("login/protected/touch");
}
}
function touchProtectedSessionIfNecessary(note: FNote | null) {
if (note && note.isProtected && isProtectedSessionAvailable()) {
touchProtectedSession();
}
}
export default {
enableProtectedSession,
resetProtectedSession,
isProtectedSessionAvailable,
touchProtectedSession,
touchProtectedSessionIfNecessary
};

View File

@@ -1,28 +0,0 @@
import server from "./server.js";
import bundleService, { type Bundle } from "./bundle.js";
import type FNote from "../entities/fnote.js";
async function render(note: FNote, $el: JQuery<HTMLElement>) {
const relations = note.getRelations("renderNote");
const renderNoteIds = relations.map((rel) => rel.value).filter((noteId) => noteId);
$el.empty().toggle(renderNoteIds.length > 0);
for (const renderNoteId of renderNoteIds) {
const bundle = await server.post<Bundle>(`script/bundle/${renderNoteId}`);
const $scriptContainer = $("<div>");
$el.append($scriptContainer);
$scriptContainer.append(bundle.html);
// async so that scripts cannot block trilium execution
bundleService.executeBundle(bundle, note, $scriptContainer);
}
return renderNoteIds.length > 0;
}
export default {
render
};

View File

@@ -1,69 +0,0 @@
import options from "./options.js";
import Split from "split.js"
export const DEFAULT_GUTTER_SIZE = 5;
let leftInstance: ReturnType<typeof Split> | null;
let rightInstance: ReturnType<typeof Split> | null;
function setupLeftPaneResizer(leftPaneVisible: boolean) {
if (leftInstance) {
leftInstance.destroy();
leftInstance = null;
}
$("#left-pane").toggle(leftPaneVisible);
if (!leftPaneVisible) {
$("#rest-pane").css("width", "100%");
return;
}
let leftPaneWidth = options.getInt("leftPaneWidth");
if (!leftPaneWidth || leftPaneWidth < 5) {
leftPaneWidth = 5;
}
if (leftPaneVisible) {
leftInstance = Split(["#left-pane", "#rest-pane"], {
sizes: [leftPaneWidth, 100 - leftPaneWidth],
gutterSize: DEFAULT_GUTTER_SIZE,
onDragEnd: (sizes) => options.save("leftPaneWidth", Math.round(sizes[0]))
});
}
}
function setupRightPaneResizer() {
if (rightInstance) {
rightInstance.destroy();
rightInstance = null;
}
const rightPaneVisible = $("#right-pane").is(":visible");
if (!rightPaneVisible) {
$("#center-pane").css("width", "100%");
return;
}
let rightPaneWidth = options.getInt("rightPaneWidth");
if (!rightPaneWidth || rightPaneWidth < 5) {
rightPaneWidth = 5;
}
if (rightPaneVisible) {
rightInstance = Split(["#center-pane", "#right-pane"], {
sizes: [100 - rightPaneWidth, rightPaneWidth],
gutterSize: DEFAULT_GUTTER_SIZE,
minSize: [ 300, 180 ],
onDragEnd: (sizes) => options.save("rightPaneWidth", Math.round(sizes[1]))
});
}
}
export default {
setupLeftPaneResizer,
setupRightPaneResizer
};

View File

@@ -1,36 +0,0 @@
import FrontendScriptApi, { type Entity } from "./frontend_script_api.js";
import utils from "./utils.js";
import froca from "./froca.js";
async function ScriptContext(startNoteId: string, allNoteIds: string[], originEntity: Entity | null = null, $container: JQuery<HTMLElement> | null = null) {
const modules: Record<string, { exports: unknown }> = {};
await froca.initializedPromise;
const startNote = await froca.getNote(startNoteId);
const allNotes = await froca.getNotes(allNoteIds);
if (!startNote) {
throw new Error(`Could not find start note ${startNoteId}.`);
}
return {
modules: modules,
notes: utils.toObject(allNotes, (note) => [note.noteId, note]),
apis: utils.toObject(allNotes, (note) => [note.noteId, new FrontendScriptApi(startNote, note, originEntity, $container)]),
require: (moduleNoteIds: string) => {
return (moduleName: string) => {
const candidates = allNotes.filter((note) => moduleNoteIds.includes(note.noteId));
const note = candidates.find((c) => c.title === moduleName);
if (!note) {
throw new Error(`Could not find module note ${moduleName}`);
}
return modules[note.noteId].exports;
};
}
};
}
export default ScriptContext;

View File

@@ -1,17 +0,0 @@
import server from "./server.js";
import froca from "./froca.js";
async function searchForNoteIds(searchString: string) {
return await server.get<string[]>(`search/${encodeURIComponent(searchString)}`);
}
async function searchForNotes(searchString: string) {
const noteIds = await searchForNoteIds(searchString);
return await froca.getNotes(noteIds);
}
export default {
searchForNoteIds,
searchForNotes
};

View File

@@ -1,278 +0,0 @@
import utils from "./utils.js";
import ValidationError from "./validation_error.js";
type Headers = Record<string, string | null | undefined>;
type Method = string;
interface Response {
headers: Headers;
body: unknown;
}
interface Arg extends Response {
statusCode: number;
method: Method;
url: string;
requestId: string;
}
interface RequestData {
resolve: (value: unknown) => any;
reject: (reason: unknown) => any;
silentNotFound: boolean;
}
export interface StandardResponse {
success: boolean;
}
async function getHeaders(headers?: Headers) {
const appContext = (await import("../components/app_context.js")).default;
const activeNoteContext = appContext.tabManager ? appContext.tabManager.getActiveContext() : null;
// headers need to be lowercase because node.js automatically converts them to lower case
// also avoiding using underscores instead of dashes since nginx filters them out by default
const allHeaders: Headers = {
"trilium-component-id": glob.componentId,
"trilium-local-now-datetime": utils.localNowDateTime(),
"trilium-hoisted-note-id": activeNoteContext ? activeNoteContext.hoistedNoteId : null,
"x-csrf-token": glob.csrfToken
};
for (const headerName in headers) {
if (headers[headerName]) {
allHeaders[headerName] = headers[headerName];
}
}
if (utils.isElectron()) {
// passing it explicitly here because of the electron HTTP bypass
allHeaders.cookie = document.cookie;
}
return allHeaders;
}
async function getWithSilentNotFound<T>(url: string, componentId?: string) {
return await call<T>("GET", url, componentId, { silentNotFound: true });
}
async function get<T>(url: string, componentId?: string) {
return await call<T>("GET", url, componentId);
}
async function post<T>(url: string, data?: unknown, componentId?: string) {
return await call<T>("POST", url, componentId, { data });
}
async function put<T>(url: string, data?: unknown, componentId?: string) {
return await call<T>("PUT", url, componentId, { data });
}
async function patch<T>(url: string, data: unknown, componentId?: string) {
return await call<T>("PATCH", url, componentId, { data });
}
async function remove<T>(url: string, componentId?: string) {
return await call<T>("DELETE", url, componentId);
}
async function upload(url: string, fileToUpload: File) {
const formData = new FormData();
formData.append("upload", fileToUpload);
return await $.ajax({
url: window.glob.baseApiUrl + url,
headers: await getHeaders(),
data: formData,
type: "PUT",
timeout: 60 * 60 * 1000,
contentType: false, // NEEDED, DON'T REMOVE THIS
processData: false // NEEDED, DON'T REMOVE THIS
});
}
let idCounter = 1;
const idToRequestMap: Record<string, RequestData> = {};
let maxKnownEntityChangeId = 0;
interface CallOptions {
data?: unknown;
silentNotFound?: boolean;
}
async function call<T>(method: string, url: string, componentId?: string, options: CallOptions = {}) {
let resp;
const headers = await getHeaders({
"trilium-component-id": componentId
});
const { data } = options;
if (utils.isElectron()) {
const ipc = utils.dynamicRequire("electron").ipcRenderer;
const requestId = idCounter++;
resp = (await new Promise((resolve, reject) => {
idToRequestMap[requestId] = {
resolve,
reject,
silentNotFound: !!options.silentNotFound
};
ipc.send("server-request", {
requestId: requestId,
headers: headers,
method: method,
url: `/${window.glob.baseApiUrl}${url}`,
data: data
});
})) as any;
} else {
resp = await ajax(url, method, data, headers, !!options.silentNotFound);
}
const maxEntityChangeIdStr = resp.headers["trilium-max-entity-change-id"];
if (maxEntityChangeIdStr && maxEntityChangeIdStr.trim()) {
maxKnownEntityChangeId = Math.max(maxKnownEntityChangeId, parseInt(maxEntityChangeIdStr));
}
return resp.body as T;
}
function ajax(url: string, method: string, data: unknown, headers: Headers, silentNotFound: boolean): Promise<Response> {
return new Promise((res, rej) => {
const options: JQueryAjaxSettings = {
url: window.glob.baseApiUrl + url,
type: method,
headers: headers,
timeout: 60000,
success: (body, textStatus, jqXhr) => {
const respHeaders: Headers = {};
jqXhr
.getAllResponseHeaders()
.trim()
.split(/[\r\n]+/)
.forEach((line) => {
const parts = line.split(": ");
const header = parts.shift();
if (header) {
respHeaders[header] = parts.join(": ");
}
});
res({
body,
headers: respHeaders
});
},
error: async (jqXhr) => {
if (jqXhr.status === 0) {
// don't report requests that are rejected by the browser, usually when the user is refreshing or going to a different page.
rej("rejected by browser");
return;
} else if (silentNotFound && jqXhr.status === 404) {
// report nothing
} else {
await reportError(method, url, jqXhr.status, jqXhr.responseText);
}
rej(jqXhr.responseText);
}
};
if (data) {
try {
options.data = JSON.stringify(data);
} catch (e) {
console.log("Can't stringify data: ", data, " because of error: ", e);
}
options.contentType = "application/json";
}
$.ajax(options);
});
}
if (utils.isElectron()) {
const ipc = utils.dynamicRequire("electron").ipcRenderer;
ipc.on("server-response", async (event: string, arg: Arg) => {
if (arg.statusCode >= 200 && arg.statusCode < 300) {
handleSuccessfulResponse(arg);
} else {
if (arg.statusCode === 404 && idToRequestMap[arg.requestId]?.silentNotFound) {
// report nothing
} else {
await reportError(arg.method, arg.url, arg.statusCode, arg.body);
}
idToRequestMap[arg.requestId].reject(new Error(`Server responded with ${arg.statusCode}`));
}
delete idToRequestMap[arg.requestId];
});
function handleSuccessfulResponse(arg: Arg) {
if (arg.headers["Content-Type"] === "application/json" && typeof arg.body === "string") {
arg.body = JSON.parse(arg.body);
}
if (!(arg.requestId in idToRequestMap)) {
// this can happen when reload happens between firing up the request and receiving the response
throw new Error(`Unknown requestId '${arg.requestId}'`);
}
idToRequestMap[arg.requestId].resolve({
body: arg.body,
headers: arg.headers
});
}
}
async function reportError(method: string, url: string, statusCode: number, response: unknown) {
let message = response;
if (typeof response === "string") {
try {
response = JSON.parse(response);
message = (response as any).message;
} catch (e) {}
}
const toastService = (await import("./toast.js")).default;
const messageStr = typeof message === "string" ? message : JSON.stringify(message);
if ([400, 404].includes(statusCode) && response && typeof response === "object") {
toastService.showError(messageStr);
throw new ValidationError({
requestUrl: url,
method,
statusCode,
...response
});
} else {
const title = `${statusCode} ${method} ${url}`;
toastService.showErrorTitleAndMessage(title, messageStr);
toastService.throwError(`${title} - ${message}`);
}
}
export default {
get,
getWithSilentNotFound,
post,
put,
patch,
remove,
upload,
// don't remove, used from CKEditor image upload!
getHeaders,
getMaxKnownEntityChangeId: () => maxKnownEntityChangeId
};

View File

@@ -1,57 +0,0 @@
import utils from "./utils.js";
type ElementType = HTMLElement | Document;
type Handler = (e: JQuery.TriggeredEvent<ElementType | Element, string, ElementType | Element, ElementType | Element>) => void;
function removeGlobalShortcut(namespace: string) {
bindGlobalShortcut("", null, namespace);
}
function bindGlobalShortcut(keyboardShortcut: string, handler: Handler | null, namespace: string | null = null) {
bindElShortcut($(document), keyboardShortcut, handler, namespace);
}
function bindElShortcut($el: JQuery<ElementType | Element>, keyboardShortcut: string, handler: Handler | null, namespace: string | null = null) {
if (utils.isDesktop()) {
keyboardShortcut = normalizeShortcut(keyboardShortcut);
let eventName = "keydown";
if (namespace) {
eventName += `.${namespace}`;
// if there's a namespace, then we replace the existing event handler with the new one
$el.off(eventName);
}
// method can be called to remove the shortcut (e.g. when keyboardShortcut label is deleted)
if (keyboardShortcut) {
$el.bind(eventName, keyboardShortcut, (e) => {
if (handler) {
handler(e);
}
e.preventDefault();
e.stopPropagation();
});
}
}
}
/**
* Normalize to the form expected by the jquery.hotkeys.js
*/
function normalizeShortcut(shortcut: string): string {
if (!shortcut) {
return shortcut;
}
return shortcut.toLowerCase().replace("enter", "return").replace("delete", "del").replace("ctrl+alt", "alt+ctrl").replace("meta+alt", "alt+meta"); // alt needs to be first;
}
export default {
bindGlobalShortcut,
bindElShortcut,
removeGlobalShortcut,
normalizeShortcut
};

View File

@@ -1,78 +0,0 @@
type Callback = () => Promise<void> | void;
export default class SpacedUpdate {
private updater: Callback;
private lastUpdated: number;
private changed: boolean;
private updateInterval: number;
private changeForbidden?: boolean;
constructor(updater: Callback, updateInterval = 1000) {
this.updater = updater;
this.lastUpdated = Date.now();
this.changed = false;
this.updateInterval = updateInterval;
}
scheduleUpdate() {
if (!this.changeForbidden) {
this.changed = true;
setTimeout(() => this.triggerUpdate());
}
}
async updateNowIfNecessary() {
if (this.changed) {
this.changed = false; // optimistic...
try {
await this.updater();
} catch (e) {
this.changed = true;
throw e;
}
}
}
isAllSavedAndTriggerUpdate() {
const allSaved = !this.changed;
this.updateNowIfNecessary();
return allSaved;
}
/**
* Normally {@link scheduleUpdate()} would actually trigger the update only once per {@link updateInterval}. If the method is called 200 times within 20s, it will execute only 20 times.
* Sometimes, if the updates are continuous this would cause a performance impact. Resetting the time ensures that the calls to {@link triggerUpdate} have stopped before actually triggering an update.
*/
resetUpdateTimer() {
this.lastUpdated = Date.now();
}
triggerUpdate() {
if (!this.changed) {
return;
}
if (Date.now() - this.lastUpdated > this.updateInterval) {
this.updater();
this.lastUpdated = Date.now();
this.changed = false;
} else {
// update isn't triggered but changes are still pending, so we need to schedule another check
this.scheduleUpdate();
}
}
async allowUpdateWithoutChange(callback: Callback) {
this.changeForbidden = true;
try {
await callback();
} finally {
this.changeForbidden = false;
}
}
}

View File

@@ -1,30 +0,0 @@
import { t } from "./i18n.js";
import server from "./server.js";
import toastService from "./toast.js";
// TODO: De-duplicate with server once we have a commons.
interface SyncResult {
success: boolean;
message: string;
errorCode?: string;
}
async function syncNow(ignoreNotConfigured = false) {
const result = await server.post<SyncResult>("sync/now");
if (result.success) {
toastService.showMessage(t("sync.finished-successfully"));
} else {
if (result.message.length > 200) {
result.message = `${result.message.substr(0, 200)}...`;
}
if (!ignoreNotConfigured || result.errorCode !== "NOT_CONFIGURED") {
toastService.showError(t("sync.failed", { message: result.message }));
}
}
}
export default {
syncNow
};

View File

@@ -1,91 +0,0 @@
import library_loader from "./library_loader.js";
import mime_types from "./mime_types.js";
import options from "./options.js";
export function getStylesheetUrl(theme: string) {
if (!theme) {
return null;
}
const defaultPrefix = "default:";
if (theme.startsWith(defaultPrefix)) {
return `${window.glob.assetPath}/node_modules/@highlightjs/cdn-assets/styles/${theme.substr(defaultPrefix.length)}.min.css`;
}
return null;
}
/**
* Identifies all the code blocks (as `pre code`) under the specified hierarchy and uses the highlight.js library to obtain the highlighted text which is then applied on to the code blocks.
*
* @param $container the container under which to look for code blocks and to apply syntax highlighting to them.
*/
export async function applySyntaxHighlight($container: JQuery<HTMLElement>) {
if (!isSyntaxHighlightEnabled()) {
return;
}
const codeBlocks = $container.find("pre code");
for (const codeBlock of codeBlocks) {
const normalizedMimeType = extractLanguageFromClassList(codeBlock);
if (!normalizedMimeType) {
continue;
}
applySingleBlockSyntaxHighlight($(codeBlock), normalizedMimeType);
}
}
/**
* Applies syntax highlight to the given code block (assumed to be <pre><code>), using highlight.js.
*/
export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLElement>, normalizedMimeType: string) {
$codeBlock.parent().toggleClass("hljs");
const text = $codeBlock.text();
if (!window.hljs) {
await library_loader.requireLibrary(library_loader.HIGHLIGHT_JS);
}
let highlightedText = null;
if (normalizedMimeType === mime_types.MIME_TYPE_AUTO) {
highlightedText = hljs.highlightAuto(text);
} else if (normalizedMimeType) {
const language = mime_types.getHighlightJsNameForMime(normalizedMimeType);
if (language) {
highlightedText = hljs.highlight(text, { language });
} else {
console.warn(`Unknown mime type: ${normalizedMimeType}.`);
}
}
if (highlightedText) {
$codeBlock.html(highlightedText.value);
}
}
/**
* Indicates whether syntax highlighting should be enabled for code blocks, by querying the value of the `codeblockTheme` option.
* @returns whether syntax highlighting should be enabled for code blocks.
*/
export function isSyntaxHighlightEnabled() {
const theme = options.get("codeBlockTheme");
return theme && theme !== "none";
}
/**
* Given a HTML element, tries to extract the `language-` class name out of it.
*
* @param el the HTML element from which to extract the language tag.
* @returns the normalized MIME type (e.g. `text-css` instead of `language-text-css`).
*/
function extractLanguageFromClassList(el: HTMLElement) {
const prefix = "language-";
for (const className of el.classList) {
if (className.startsWith(prefix)) {
return className.substring(prefix.length);
}
}
return null;
}

Some files were not shown because too many files have changed in this diff Show More