Back to Trilium

Interface Api

docs/Script API/interfaces/Frontend_Script_API.Api.html

0.102.230.6 KB
Original Source

Interface Api

interface Api {
$container: null | JQuery<HTMLElement>;
BasicWidget: typeof BasicWidget;
bindGlobalShortcut: (
keyboardShortcut: string,
handler: null | Handler,
namespace?: null | string,
) => void;
createLink: (
notePath: undefined | string,
options?: CreateLinkOptions,
) => Promise<JQuery<HTMLElement>>;
createNoteLink: (
notePath: undefined | string,
options?: CreateLinkOptions,
) => Promise<JQuery<HTMLElement>>;
currentNote: FNote;
dayjs: __module;
formatDateISO: (date: Date) => string;
formatNoteSize: (size: number) => string;
formatSize: (size: number) => string;
getDayNote: (date: string) => Promise<null | FNote>;
getMonthNote: (month: string) => Promise<null | FNote>;
getQuarterNote: (quarter: string) => Promise<null | FNote>;
getTodayNote: () => Promise<null | FNote>;
getWeekFirstDayNote: (date: string) => Promise<null | FNote>;
getWeekNote: (week: string) => Promise<null | FNote>;
getYearNote: (year: string) => Promise<null | FNote>;
logMessages: Record<string, string[]>;
logSpacedUpdates: Record<string, default>;
NoteContextAwareWidget: typeof NoteContextAwareWidget;
originEntity: unknown;
parseDate: (str: string) => Date;
protectNote: (
noteId: string,
protect: boolean,
includingSubtree: boolean,
) => Promise<void>;
protectSubTree: (
noteId: string,
protect: boolean,
includingSubtree: boolean,
) => Promise<void>;
randomString: (len: number) => string;
RightPanelWidget: typeof RightPanelWidget;
setupElementTooltip: ($el: JQuery<HTMLElement>) => void;
showConfirmDialog: (message: string) => Promise<unknown>;
showError: (message: string, delay?: number) => void;
showInfoDialog: (message: string) => Promise<unknown>;
showMessage: (message: string, delay?: number) => void;
showPromptDialog: (props: PromptDialogOptions) => Promise<null | string>;
startNote: FNote;
triggerCommand: <K extends keyof CommandMappings>(
name: K,
_data?: CommandMappings[K],
) => undefined | null | Promise<unknown>;
triggerEvent: <K extends EventNames>(
name: K,
data: EventData<K>,
) => undefined | null | Promise<unknown>;
waitUntilSynced: () => Promise<void>;
activateNewNote(notePath: string): Promise<void>;
activateNote(notePath: string): Promise<void>;
addButtonToToolbar(opts: AddToToolbarOpts): void;
addTextToActiveContextEditor(text: string): void;
getActiveContext(): NoteContext;
getActiveContextCodeEditor(): Promise<unknown>;
getActiveContextNote(): FNote;
getActiveContextNotePath(): null | string;
getActiveContextTextEditor(): Promise<unknown>;
getActiveMainContext(): NoteContext;
getActiveNoteDetailWidget(): Promise<default>;
getComponentByEl(el: HTMLElement): default;
getInstanceName(): string;
getMainNoteContexts(): NoteContext[];
getNote(noteId: string): Promise<null | FNote>;
getNoteContexts(): NoteContext[];
getNotes(
noteIds: string[],
silentNotFoundError?: boolean,
): Promise<FNote[]>;
log(message: string): void;
openSplitWithNote(notePath: string, activate: boolean): Promise<void>;
openTabWithNote(notePath: string, activate: boolean): Promise<void>;
refreshIncludedNote(includedNoteId: string): void;
reloadNotes(noteIds: string[]): Promise<void>;
runAsyncOnBackendWithManualTransactionHandling(
func: Func,
params: unknown[],
): unknown;
runOnBackend(func: Func, params: unknown[]): unknown;
searchForNote(searchString: string): Promise<null | FNote>;
searchForNotes(searchString: string): Promise<FNote[]>;
setHoistedNoteId(noteId: string): void;
}

Index

Properties

$containerBasicWidgetbindGlobalShortcutcreateLinkcreateNoteLinkcurrentNotedayjsformatDateISOformatNoteSizeformatSizegetDayNotegetMonthNotegetQuarterNotegetTodayNotegetWeekFirstDayNotegetWeekNotegetYearNotelogMessageslogSpacedUpdatesNoteContextAwareWidgetoriginEntityparseDateprotectNoteprotectSubTreerandomStringRightPanelWidgetsetupElementTooltipshowConfirmDialogshowErrorshowInfoDialogshowMessageshowPromptDialogstartNotetriggerCommandtriggerEventwaitUntilSynced

Methods

activateNewNoteactivateNoteaddButtonToToolbaraddTextToActiveContextEditorgetActiveContextgetActiveContextCodeEditorgetActiveContextNotegetActiveContextNotePathgetActiveContextTextEditorgetActiveMainContextgetActiveNoteDetailWidgetgetComponentByElgetInstanceNamegetMainNoteContextsgetNotegetNoteContextsgetNoteslogopenSplitWithNoteopenTabWithNoterefreshIncludedNotereloadNotesrunAsyncOnBackendWithManualTransactionHandlingrunOnBackendsearchForNotesearchForNotessetHoistedNoteId

Properties

$container

$container: null | JQuery<HTMLElement>

Container of all the rendered script content

BasicWidget

BasicWidget: typeof BasicWidget

bindGlobalShortcut

bindGlobalShortcut: (
keyboardShortcut: string,
handler: null | Handler,
namespace?: null | string,
) => void

Type declaration

    • (
      keyboardShortcut: string,
      handler: null | Handler,
      namespace?: null | string,
      ): void

Parameters

- keyboardShortcut: string

e.g. "ctrl+shift+a"

- handler: null | [Handler](../types/Frontend_Script_API._internal_.Handler.html)
- namespace: null | string = null

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.

Returns void

createLink: (
notePath: undefined | string,
options?: CreateLinkOptions,
) => Promise<JQuery<HTMLElement>>

Create a note link (jQuery object) for given note.

Type declaration

    • (
      notePath: undefined | string,
      options?: CreateLinkOptions,
      ): Promise<JQuery<HTMLElement>>

Parameters

- notePath: undefined | string

(or noteId)

- options: [CreateLinkOptions](Frontend_Script_API._internal_.CreateLinkOptions.html) = {}

Returns Promise<JQuery<HTMLElement>>

- jQuery element with the link (wrapped in )

createNoteLink: (
notePath: undefined | string,
options?: CreateLinkOptions,
) => Promise<JQuery<HTMLElement>>

Deprecated

  • use api.createLink() instead

currentNote

currentNote: FNote

Note where the script is currently executing, i.e. the note where the currently executing source code is written.

dayjs

dayjs: __module

day.js library for date manipulation. See https://day.js.org for documentation

See

https://day.js.org

formatDateISO

formatDateISO: (date: Date) => string

Type declaration

    • (date: Date): string

this is producing local time! *

Parameters

- date: Date

Returns string

Returns

date in YYYY-MM-DD format

formatNoteSize

formatNoteSize: (size: number) => string

Type declaration

    • (size: number): string

Parameters

- size: number

in bytes

Returns string

formatted string

Deprecated

  • use api.formatSize()

formatSize

formatSize: (size: number) => string

Type declaration

    • (size: number): string

Parameters

- size: number

in bytes

Returns string

formatted string

getDayNote

getDayNote: (date: string) => Promise<null | FNote>

Returns day note for a given date. If it doesn't exist, it is automatically created.

Type declaration

    • (date: string): Promise<null | FNote>

Parameters

- date: string

e.g. "2019-04-29"

Returns Promise<null | FNote>

getMonthNote

getMonthNote: (month: string) => Promise<null | FNote>

Returns month-note. If it doesn't exist, it is automatically created.

Type declaration

    • (month: string): Promise<null | FNote>

Parameters

- month: string

e.g. "2019-04"

Returns Promise<null | FNote>

getQuarterNote

getQuarterNote: (quarter: string) => Promise<null | FNote>

Returns quarter note for given date. If such a note doesn't exist, it is automatically created.

getTodayNote

getTodayNote: () => Promise<null | FNote>

Returns date-note for today. If it doesn't exist, it is automatically created.

getWeekFirstDayNote

getWeekFirstDayNote: (date: string) => Promise<null | FNote>

Returns day note for the first date of the week of the given date. If it doesn't exist, it is automatically created.

Type declaration

    • (date: string): Promise<null | FNote>

Parameters

- date: string

e.g. "2019-04-29"

Returns Promise<null | FNote>

getWeekNote

getWeekNote: (week: string) => Promise<null | FNote>

Returns week note for given date. If such a note doesn't exist, it is automatically created.

getYearNote

getYearNote: (year: string) => Promise<null | FNote>

Returns year-note. If it doesn't exist, it is automatically created.

Type declaration

    • (year: string): Promise<null | FNote>

Parameters

- year: string

e.g. "2019"

Returns Promise<null | FNote>

Method

logMessages

logMessages: Record<string, string[]>

logSpacedUpdates

logSpacedUpdates: Record<string, default>

NoteContextAwareWidget

NoteContextAwareWidget: typeof NoteContextAwareWidget

originEntity

originEntity: unknown

Entity whose event triggered this execution.

parseDate

parseDate: (str: string) => Date

protectNote

protectNote: (
noteId: string,
protect: boolean,
includingSubtree: boolean,
) => Promise<void>

Type declaration

    • (noteId: string, protect: boolean, includingSubtree: boolean): Promise<void>

Parameters

- noteId: string
- protect: boolean

true to protect note, false to unprotect

- includingSubtree: boolean

Returns Promise<void>

protectSubTree

protectSubTree: (
noteId: string,
protect: boolean,
includingSubtree: boolean,
) => Promise<void>

Type declaration

    • (noteId: string, protect: boolean, includingSubtree: boolean): Promise<void>

Parameters

- noteId: string

- protect: boolean

true to protect subtree, false to unprotect

- includingSubtree: boolean

Returns Promise<void>

randomString

randomString: (len: number) => string

Return randomly generated string of given length. This random string generation is NOT cryptographically secure.

Type declaration

    • (len: number): string

Parameters

- len: number

Returns string

random string

Method

RightPanelWidget

RightPanelWidget: typeof RightPanelWidget

setupElementTooltip

setupElementTooltip: ($el: JQuery<HTMLElement>) => void

Type declaration

    • ($el: JQuery<HTMLElement>): void

Parameters

- $el: JQuery\<HTMLElement\>

jquery object on which to set up the tooltip

Returns void

showConfirmDialog

showConfirmDialog: (message: string) => Promise<unknown>

Show confirm dialog to the user.

Type declaration

    • (message: string): Promise<unknown>

Parameters

- message: string

Returns Promise<unknown>

promise resolving to true if the user confirmed

showError

showError: (message: string, delay?: number) => void

Show an error toast message to the user.

showInfoDialog

showInfoDialog: (message: string) => Promise<unknown>

Show an info dialog to the user.

showMessage

showMessage: (message: string, delay?: number) => void

Show an info toast message to the user.

showPromptDialog

showPromptDialog: (props: PromptDialogOptions) => Promise<null | string>

Show prompt dialog to the user.

Type declaration

Parameters

- props: [PromptDialogOptions](Frontend_Script_API._internal_.PromptDialogOptions.html)

Returns Promise<null | string>

promise resolving to the answer provided by the user

startNote

startNote: FNote

Note where the script started executing, i.e., the (event) entrypoint of the current script execution.

triggerCommand

triggerCommand: <K extends keyof CommandMappings>(
name: K,
_data?: CommandMappings[K],
) => undefined | null | Promise<unknown>

Trigger command. This is a very low-level API which should be avoided if possible.

triggerEvent

triggerEvent: <K extends EventNames>(
name: K,
data: EventData<K>,
) => undefined | null | Promise<unknown>

Trigger event. This is a very low-level API which should be avoided if possible.

waitUntilSynced

waitUntilSynced: () => Promise<void>

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.

Methods

activateNewNote

activateNewNote(notePath: string): Promise<void>

Activates newly created note. Compared to this.activateNote() also makes sure that frontend has been fully synced.

Parameters

  • notePath: string

(or noteId)

Returns Promise<void>

activateNote

activateNote(notePath: string): Promise<void>

Activates note in the tree and in the note detail.

Parameters

  • notePath: string

(or noteId)

Returns Promise<void>

addButtonToToolbar

addButtonToToolbar(opts: AddToToolbarOpts): void

Adds a new launcher to the launchbar. If the launcher (id) already exists, it will be updated.

Parameters

Returns void

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()

addTextToActiveContextEditor

addTextToActiveContextEditor(text: string): void

Adds given text to the editor cursor

Parameters

  • text: string

this must be clear text, HTML is not supported.

Returns void

getActiveContext

getActiveContext(): NoteContext

Returns NoteContext

returns active context (split)

getActiveContextCodeEditor

getActiveContextCodeEditor(): Promise<unknown>

See https://codemirror.net/doc/manual.html#api

Returns Promise<unknown>

instance of CodeMirror

Method

getActiveContextNote

getActiveContextNote(): FNote

Returns FNote

active note (loaded into center pane)

getActiveContextNotePath

getActiveContextNotePath(): null | string

Returns null | string

returns a note path of active note or null if there isn't active note

getActiveContextTextEditor

getActiveContextTextEditor(): Promise<unknown>

See https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html for documentation on the returned instance.

Returns Promise<unknown>

instance of CKEditor

getActiveMainContext

getActiveMainContext(): NoteContext

Returns NoteContext

returns active main context (first split in a tab, represents the tab as a whole)

getActiveNoteDetailWidget

getActiveNoteDetailWidget(): Promise<default>

Get access to the widget handling note detail. Methods like getWidgetType() and getTypeWidget() to get to the implementation of actual widget type.

Returns Promise<default>

getComponentByEl

getComponentByEl(el: HTMLElement): default

Returns component which owns the given DOM element (the nearest parent component in DOM tree)

Parameters

  • el: HTMLElement

DOM element

Returns default

Method

getInstanceName

getInstanceName(): string

Instance name identifies particular Trilium instance. It can be useful for scripts if some action needs to happen on only one specific instance.

Returns string

getMainNoteContexts

getMainNoteContexts(): NoteContext[]

Returns NoteContext[]

returns all main contexts representing tabs

getNote

getNote(noteId: string): Promise<null | FNote>

Returns note by given noteId. If note is missing from the cache, it's loaded.

Parameters

  • noteId: string

Returns Promise<null | FNote>

getNoteContexts

getNoteContexts(): NoteContext[]

Returns NoteContext[]

returns all note contexts (splits) in all tabs

getNotes

getNotes(noteIds: string[], silentNotFoundError?: boolean): Promise<FNote[]>

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())

Parameters

  • noteIds: string[]
  • OptionalsilentNotFoundError: boolean

don't report error if the note is not found

Returns Promise<FNote[]>

log

log(message: string): void

Log given message to the log pane in UI

Parameters

  • message: string

Returns void

openSplitWithNote

openSplitWithNote(notePath: string, activate: boolean): Promise<void>

Open a note in a new split.

Parameters

  • notePath: string

(or noteId)

  • activate: boolean

set to true to activate the new split, false to stay on the current split

Returns Promise<void>

openTabWithNote

openTabWithNote(notePath: string, activate: boolean): Promise<void>

Open a note in a new tab.

Parameters

  • notePath: string

(or noteId)

  • activate: boolean

set to true to activate the new tab, false to stay on the current tab

Returns Promise<void>

Method

refreshIncludedNote

refreshIncludedNote(includedNoteId: string): void

This will refresh all currently opened notes which have included note specified in the parameter

Parameters

  • includedNoteId: string

noteId of the included note

Returns void

reloadNotes

reloadNotes(noteIds: string[]): Promise<void>

Update frontend tree (note) cache from the backend.

Parameters

  • noteIds: string[]

Returns Promise<void>

runAsyncOnBackendWithManualTransactionHandling

runAsyncOnBackendWithManualTransactionHandling(
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().

Parameters

(synchronous) function to be executed on the backend

  • params: unknown[]

list of parameters to the anonymous function to be sent to backend

Returns unknown

return value of the executed function on the backend

Method

runOnBackend

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. 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()

Parameters

(synchronous) function to be executed on the backend

  • params: unknown[]

list of parameters to the anonymous function to be sent to backend

Returns unknown

return value of the executed function on the backend

Method

searchForNote

searchForNote(searchString: string): Promise<null | 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

Parameters

  • searchString: string

Returns Promise<null | FNote>

searchForNotes

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

Parameters

  • searchString: string

Returns Promise<FNote[]>

setHoistedNoteId

setHoistedNoteId(noteId: string): void

Hoist note in the current tab. See https://triliumnext.github.io/Docs/Wiki/note-hoisting.html

Parameters

  • noteId: string

set hoisted note. 'root' will effectively unhoist

Returns void

Settings

Member Visibility

  • Protected
  • Inherited
  • External

ThemeOSLightDark

On This Page

Properties $containerBasicWidgetbindGlobalShortcutcreateLinkcreateNoteLinkcurrentNotedayjsformatDateISOformatNoteSizeformatSizegetDayNotegetMonthNotegetQuarterNotegetTodayNotegetWeekFirstDayNotegetWeekNotegetYearNotelogMessageslogSpacedUpdatesNoteContextAwareWidgetoriginEntityparseDateprotectNoteprotectSubTreerandomStringRightPanelWidgetsetupElementTooltipshowConfirmDialogshowErrorshowInfoDialogshowMessageshowPromptDialogstartNotetriggerCommandtriggerEventwaitUntilSynced Methods activateNewNoteactivateNoteaddButtonToToolbaraddTextToActiveContextEditorgetActiveContextgetActiveContextCodeEditorgetActiveContextNotegetActiveContextNotePathgetActiveContextTextEditorgetActiveMainContextgetActiveNoteDetailWidgetgetComponentByElgetInstanceNamegetMainNoteContextsgetNotegetNoteContextsgetNoteslogopenSplitWithNoteopenTabWithNoterefreshIncludedNotereloadNotesrunAsyncOnBackendWithManualTransactionHandlingrunOnBackendsearchForNotesearchForNotessetHoistedNoteId