Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
docs(script-api): clarify startNote, currentNote and originEntity
Elian Dorane9f98a3091c4
diff --git a/apps/client/src/services/frontend_script_api.ts b/apps/client/src/services/frontend_script_api.tsindex 55292d5a44..a268b6fb9a 100644--- a/apps/client/src/services/frontend_script_api.ts+++ b/apps/client/src/services/frontend_script_api.ts@@ -68,21 +68,31 @@ export interface Api {$container: JQuery<HTMLElement> | null;/**- * Note where the script started executing, i.e., the (event) entrypoint of the current script execution.+ * Note where the script execution started — the entry point of the current script bundle+ * (in C terms, the file containing `main()`). When a script is spread across multiple code+ * notes (descendant code notes loaded as modules via `api.require()`), every note in the+ * bundle shares the same `startNote`, while {@link currentNote} differs per note.+ * Messages from `api.log()` are grouped under this note.*/startNote: FNote;/**- * Note where the script is currently executing, i.e. the note where the currently executing source code is written.+ * Note containing the source code that is currently executing (in C terms, `__FILE__`).+ * Equal to {@link startNote} unless execution has moved into a descendant module note+ * loaded via `api.require()`. Don't confuse this with the note open in the UI — use+ * `api.getActiveContextNote()` for that.*/currentNote: FNote;/**- * Entity whose event triggered this execution.+ * Entity whose event triggered this execution, or `null`.*- * <p>- * For front-end scripts, generally there's no origin entity specified since the scripts are run by the user or automatically by the UI (widgets).- * If there is an origin entity specified, then it's going to be a note entity.+ * Most frontend scripts are started by the user or by the UI (startup scripts, widgets),+ * so this is usually `null`. It is set to a note when:+ * - the script runs through a `~renderNote` relation — then it's the note being rendered+ * (the one carrying the relation), not the script note;+ * - a backend script calls `api.runOnFrontend()` — then it's the backend execution's+ * `originEntity`, provided that entity was a note.*/originEntity: unknown | null;diff --git a/packages/commons/src/lib/script_api.ts b/packages/commons/src/lib/script_api.tsindex ec6b19280c..99a415c790 100644--- a/packages/commons/src/lib/script_api.ts+++ b/packages/commons/src/lib/script_api.ts@@ -137,19 +137,29 @@ export interface FrontendApi {* */$container: unknown;/**- * Note where the script started executing, i.e., the (event) entrypoint of the current script execution.+ * Note where the script execution started — the entry point of the current script bundle+ * (in C terms, the file containing `main()`). When a script is spread across multiple code+ * notes (descendant code notes loaded as modules via `api.require()`), every note in the+ * bundle shares the same `startNote`, while {@link currentNote} differs per note.+ * Messages from `api.log()` are grouped under this note.*/startNote: ScriptFNote;/**- * Note where the script is currently executing, i.e. the note where the currently executing source code is written.+ * Note containing the source code that is currently executing (in C terms, `__FILE__`).+ * Equal to {@link startNote} unless execution has moved into a descendant module note+ * loaded via `api.require()`. Don't confuse this with the note open in the UI — use+ * `api.getActiveContextNote()` for that.*/currentNote: ScriptFNote;/**- * Entity whose event triggered this execution.+ * Entity whose event triggered this execution, or `null`.*- * <p>- * For front-end scripts, generally there's no origin entity specified since the scripts are run by the user or automatically by the UI (widgets).- * If there is an origin entity specified, then it's going to be a note entity.+ * Most frontend scripts are started by the user or by the UI (startup scripts, widgets),+ * so this is usually `null`. It is set to a note when:+ * - the script runs through a `~renderNote` relation — then it's the note being rendered+ * (the one carrying the relation), not the script note;+ * - a backend script calls `api.runOnFrontend()` — then it's the backend execution's+ * `originEntity`, provided that entity was a note.*/originEntity: unknown | null;/**@@ -595,19 +605,35 @@ export interface ScriptResponse {*/export interface BackendApi {/**- * Note where the script started executing (entrypoint).- * As an analogy, in C this would be the file which contains the main() function of the current process.+ * Note where the script execution started — the entry point of the current script bundle+ * (in C terms, the file containing `main()`). When a script is spread across multiple code+ * notes (descendant code notes loaded as modules via `api.require()`), every note in the+ * bundle shares the same `startNote`, while {@link currentNote} differs per note.+ * Messages from `api.log()` are grouped under this note.+ *+ * When a frontend script calls `api.runOnBackend()`, the frontend's `startNote` is+ * preserved here; since that note may not be resolvable on the backend, this can be null.*/startNote?: ScriptBNote | null;/**- * Note where the script is currently executing. This comes into play when your script is spread in multiple code- * notes, the script starts in "startNote", but then through function calls may jump into another note (currentNote).- * A similar concept in C would be __FILE__- * Don't mix this up with the concept of active note.+ * Note containing the source code that is currently executing (in C terms, `__FILE__`).+ * Equal to {@link startNote} unless execution has moved into a descendant module note+ * loaded via `api.require()`. Don't confuse this with the concept of the active note in+ * the UI.*/currentNote: ScriptBNote;/**- * Entity whose event triggered this execution+ * Entity whose event triggered this execution; `undefined` when the run was not+ * event-driven (e.g. started manually via "Execute script" or `note.executeScript()`).+ *+ * What it holds depends on the trigger:+ * - `~runOnNoteCreation`, `~runOnNoteChange`, `~runOnNoteTitleChange`,+ * `~runOnNoteContentChange` — the affected note;+ * - `~runOnChildNoteCreation` — the newly created child note;+ * - `~runOnAttributeCreation`, `~runOnAttributeChange` — the attribute;+ * - `~runOnBranchCreation`, `~runOnBranchChange`, `~runOnBranchDeletion` — the branch;+ * - scheduled scripts (`#run=hourly` / `#run=daily`) — the script note itself;+ * - search scripts (`~searchScript`) — the search note.*/originEntity?: unknown | null;diff --git a/packages/trilium-core/src/services/backend_script_api.ts b/packages/trilium-core/src/services/backend_script_api.tsindex a83f0a2e24..894671177c 100644--- a/packages/trilium-core/src/services/backend_script_api.ts+++ b/packages/trilium-core/src/services/backend_script_api.ts@@ -61,21 +61,37 @@ interface NoteAndBranch {export interface Api {/**- * Note where the script started executing (entrypoint).- * As an analogy, in C this would be the file which contains the main() function of the current process.+ * Note where the script execution started — the entry point of the current script bundle+ * (in C terms, the file containing `main()`). When a script is spread across multiple code+ * notes (descendant code notes loaded as modules via `api.require()`), every note in the+ * bundle shares the same `startNote`, while {@link currentNote} differs per note.+ * Messages from `api.log()` are grouped under this note.+ *+ * When a frontend script calls `api.runOnBackend()`, the frontend's `startNote` is+ * preserved here; since that note may not be resolvable on the backend, this can be null.*/startNote?: BNote | null;/**- * Note where the script is currently executing. This comes into play when your script is spread in multiple code- * notes, the script starts in "startNote", but then through function calls may jump into another note (currentNote).- * A similar concept in C would be __FILE__- * Don't mix this up with the concept of active note.+ * Note containing the source code that is currently executing (in C terms, `__FILE__`).+ * Equal to {@link startNote} unless execution has moved into a descendant module note+ * loaded via `api.require()`. Don't confuse this with the concept of the active note in+ * the UI.*/currentNote: BNote;/**- * Entity whose event triggered this execution+ * Entity whose event triggered this execution; `undefined` when the run was not+ * event-driven (e.g. started manually via "Execute script" or `note.executeScript()`).+ *+ * What it holds depends on the trigger:+ * - `~runOnNoteCreation`, `~runOnNoteChange`, `~runOnNoteTitleChange`,+ * `~runOnNoteContentChange` — the affected note ({@link BNote});+ * - `~runOnChildNoteCreation` — the newly created child note;+ * - `~runOnAttributeCreation`, `~runOnAttributeChange` — the attribute;+ * - `~runOnBranchCreation`, `~runOnBranchChange`, `~runOnBranchDeletion` — the branch;+ * - scheduled scripts (`#run=hourly` / `#run=daily`) — the script note itself;+ * - search scripts (`~searchScript`) — the search note.*/originEntity?: AbstractBeccaEntity<any> | null;