Skip to content

Build your personal knowledge base with Trilium Notes

chore(commons): add ESDoc for the Electron API

Elian Dorand210baed7158
diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.ts
index f3789368e7..a2e1da4453 100644
--- a/packages/commons/src/lib/electron_api_interface.ts
+++ b/packages/commons/src/lib/electron_api_interface.ts
@@ -1,124 +1,369 @@
+/**
+ * Parameters delivered by the main process to the renderer when the user opens
+ * the system context menu (right-click) somewhere inside a web view.
+ *
+ * Mirrors the subset of fields from Electron's `Electron.ContextMenuParams`
+ * that the renderer actually consumes when building its custom context menu.
+ */
export interface ElectronContextMenuParams {
+ /** Horizontal position of the click, in CSS pixels, relative to the web view. */
x: number;
+ /** Vertical position of the click, in CSS pixels, relative to the web view. */
y: number;
+ /** URL of the link the user clicked on, or an empty string if not on a link. */
linkURL: string;
+ /** Visible text of the link the user clicked on, or an empty string. */
linkText: string;
+ /** Media kind under the cursor (e.g. `"none"`, `"image"`, `"video"`, `"audio"`). */
mediaType: string;
+ /** Whether the element under the cursor is editable (input, textarea, contenteditable). */
isEditable: boolean;
+ /** Currently selected text, or an empty string if nothing is selected. */
selectionText: string;
+ /** The misspelled word under the cursor, or an empty string if none. */
misspelledWord: string;
+ /** Spell-check suggestions for {@link misspelledWord}, in order of relevance. */
dictionarySuggestions: string[];
+ /** Hints about which clipboard operations are currently available. */
editFlags: {
+ /** Whether the selection can be cut to the clipboard. */
canCut: boolean;
+ /** Whether the selection can be copied to the clipboard. */
canCopy: boolean;
+ /** Whether the clipboard contents can be pasted at the cursor. */
canPaste: boolean;
};
}
+/**
+ * Window-level controls: zoom, theme, title bar, full screen, lifecycle, and
+ * a handful of main → renderer event subscriptions.
+ */
export interface ElectronWindowApi {
- // Zoom
+ // #region Zoom
+
+ /**
+ * Sets the page zoom factor for the current window.
+ * @param factor `1.0` is 100%; e.g. `1.2` zooms to 120%.
+ */
setZoomFactor(factor: number): void;
+
+ /** Returns the current page zoom factor (`1.0` = 100%). */
getZoomFactor(): number;
- // Theme
+ // #endregion
+
+ // #region Theme
+
+ /**
+ * Overrides the operating system's reported color scheme for this app.
+ * Use `"system"` to follow the OS setting.
+ */
setNativeThemeSource(source: "system" | "light" | "dark"): void;
- // Title bar
+ // #endregion
+
+ // #region Title bar
+
+ /**
+ * Customizes the colors of the Windows native title bar overlay
+ * (the area containing the minimize / maximize / close buttons).
+ */
setTitleBarOverlay(options: { color: string; symbolColor: string }): void;
+
+ /**
+ * Repositions the macOS traffic-light window buttons.
+ * Coordinates are in CSS pixels relative to the window's top-left corner.
+ */
setWindowButtonPosition(position: { x: number; y: number }): void;
- // Full screen
+ // #endregion
+
+ // #region Full screen
+
+ /** Registers a callback fired whenever the window enters full-screen mode. */
onEnterFullScreen(callback: () => void): void;
+
+ /** Registers a callback fired whenever the window leaves full-screen mode. */
onLeaveFullScreen(callback: () => void): void;
+
+ /** Synchronously returns whether the window is currently in full-screen mode. */
isFullScreen(): boolean;
+
+ /** Enters or leaves full-screen mode. */
setFullScreen(enabled: boolean): void;
- // Window state
+ // #endregion
+
+ // #region Window state
+
+ /** Minimizes the current window to the taskbar / dock. */
minimizeWindow(): void;
+
+ /** Maximizes the current window to fill the available screen area. */
maximizeWindow(): void;
+
+ /** Restores the window from a maximized state to its previous size. */
unmaximizeWindow(): void;
+
+ /** Synchronously returns whether the window is currently maximized. */
isMaximized(): boolean;
+
+ /** Closes the current window. */
closeWindow(): void;
+
+ /**
+ * Opens a new top-level Trilium window navigated to the given hash route.
+ * @param extraWindowHash The URL hash fragment (without the leading `#`) for the new window.
+ */
createExtraWindow(extraWindowHash: string): void;
+
+ /** Synchronously returns whether the window is pinned above all others. */
isAlwaysOnTop(): boolean;
+
+ /** Toggles the always-on-top (pinned) state of the window. */
setAlwaysOnTop(enabled: boolean): void;
+
+ /** Opens or closes Chromium DevTools for the current window. */
toggleDevTools(): void;
- // Background effects
+ // #endregion
+
+ // #region Background effects
+
+ /**
+ * Sets the Windows 11 backdrop material (e.g. `"mica"`, `"acrylic"`, `"tabbed"`, `"none"`)
+ * applied behind the window contents. No-op on other platforms.
+ */
setBackgroundMaterial(material: string): void;
+
+ /**
+ * Sets the macOS vibrancy effect (e.g. `"sidebar"`, `"under-window"`, `"fullscreen-ui"`)
+ * applied behind the window contents. No-op on other platforms.
+ */
setVibrancy(vibrancy: string): void;
- // App lifecycle
+ // #endregion
+
+ // #region App lifecycle
+
+ /** Triggers a hard reload of every open Trilium window. */
reloadAllWindows(): void;
+
+ /** Quits the app and immediately relaunches it. */
restartApp(): void;
+
+ /**
+ * Shows all hidden windows or hides all visible ones — used by the
+ * "show/hide app" global shortcut and tray menu entry.
+ */
toggleAllWindows(): void;
+
+ /** Clears the underlying Chromium HTTP cache. Resolves once the cache is empty. */
clearCache(): Promise<void>;
+
+ /** Brings the main window to the foreground, restoring it if minimized. */
showWindow(): void;
- // Main → renderer events
+ // #endregion
+
+ // #region Main → renderer events
+
+ /**
+ * Subscribes to globally registered keyboard shortcuts.
+ * The callback receives the logical action name (not the keystroke).
+ */
onGlobalShortcut(callback: (actionName: string) => void): void;
+
+ /**
+ * Subscribes to "open note in active tab" requests originating from outside
+ * the renderer (e.g. tray menu, deep links).
+ */
onOpenInSameTab(callback: (noteId: string) => void): void;
+
+ // #endregion
}
+/** Renderer access to the system clipboard for cases the standard Web API can't cover. */
export interface ElectronClipboardApi {
+ /**
+ * Writes a raw image (PNG-encoded bytes) to the system clipboard so it can be
+ * pasted into other applications as an image rather than as a file.
+ */
copyImageToClipboard(buffer: Uint8Array): void;
}
+/**
+ * Wrappers around Electron's `shell` module for interacting with the OS
+ * (default browser, default file handler, downloads, etc.).
+ */
export interface ElectronShellApi {
+ /** Opens a URL in the user's default external browser. */
openExternal(url: string): void;
+
+ /**
+ * Opens a local path with its default OS handler.
+ * @returns An empty string on success, or an error message on failure.
+ */
openPath(path: string): Promise<string>;
+
+ /**
+ * Opens a `file://` URL with its default OS handler. Convenience wrapper that
+ * decodes the URL before delegating to {@link openPath}.
+ * @returns An empty string on success, or an error message on failure.
+ */
openFileUrl(fileUrl: string): Promise<string>;
+
+ /**
+ * Triggers a Chromium download for the given URL — the file is saved through
+ * the normal "Save As" flow instead of being opened in the renderer.
+ */
downloadURL(url: string): void;
+
+ /**
+ * Opens a file with a Trilium-specific custom handler (configured via the
+ * "Open With" option), falling back to the OS default if none is set.
+ */
openCustom(filePath: string): void;
}
+/** Bridge for the native context menu shown over web contents. */
export interface ElectronContextMenuApi {
+ /** Subscribes to right-click events forwarded from the main process. */
onContextMenu(callback: (params: ElectronContextMenuParams) => void): void;
+
+ /**
+ * Executes a clipboard / text-input action on the focused web contents.
+ * `"insertText"` requires the `text` argument; the other actions ignore it.
+ */
webContentsAction(action: "cut" | "copy" | "paste" | "pasteAndMatchStyle" | "insertText", text?: string): void;
}
+/** Renderer-side access to Chromium's built-in spell checker. */
export interface ElectronSpellcheckApi {
+ /** Adds a word to the user's personal dictionary so it is no longer flagged. */
addWordToDictionary(word: string): void;
+
+ /** Returns the BCP-47 language tags Chromium can spell-check on this platform. */
getAvailableSpellCheckerLanguages(): string[];
}
+/** System tray controls. */
export interface ElectronTrayApi {
+ /** Rebuilds the tray icon and menu — call after changing tray-related settings. */
reloadTray(): void;
}
+/**
+ * Printing and PDF export flow. Trilium drives Electron's printing pipeline
+ * through a small state machine that the main process owns, with progress and
+ * results pushed back to the renderer via IPC events.
+ */
export interface ElectronPrintingApi {
+ /** Reports rendering progress (`0..100`) from the renderer to the main process. */
sendPrintProgress(progress: number): void;
+
+ /**
+ * Subscribes to progress updates emitted by the main process during a print
+ * or PDF export run. `action` describes the current stage (e.g. `"rendering"`).
+ */
onPrintProgress(callback: (data: { progress: number; action: string }) => void): void;
+
+ /** Subscribes to the "print finished" event, fired once the OS print job is dispatched. */
onPrintDone(callback: (printReport: unknown) => void): void;
+
+ /** Removes all listeners registered via {@link onPrintProgress} and {@link onPrintDone}. */
removePrintListeners(): void;
+
+ /** Returns the list of printers known to the OS. */
getPrinters(): Promise<unknown[]>;
+
+ /**
+ * Asks the main process to render the current note as a PDF and return the
+ * resulting bytes for the in-app preview. Result is delivered via
+ * {@link onExportAsPdfPreviewResult}.
+ */
exportAsPdfPreview(opts: Record<string, unknown>): void;
+
+ /** Subscribes to the result of an {@link exportAsPdfPreview} call. */
onExportAsPdfPreviewResult(callback: (result: { buffer?: Uint8Array; error?: string }) => void): void;
+
+ /** Removes the listener registered via {@link onExportAsPdfPreviewResult}. */
removeExportAsPdfPreviewResultListener(): void;
+
+ /**
+ * Persists a previously generated PDF buffer to disk via the native save
+ * dialog. `title` is used as the suggested filename.
+ */
savePdf(data: { title: string; buffer: Uint8Array }): void;
+
+ /** Sends the previewed PDF to the OS print dialog with the given options. */
printFromPreview(opts: Record<string, unknown>): void;
}
+/**
+ * Accessors for the underlying Chromium navigation history of the current
+ * web contents (back/forward stack), exposed so the renderer can mirror it
+ * in custom UI such as the breadcrumb / tab back button.
+ */
export interface ElectronNavigationApi {
+ /** Clears the entire back/forward navigation history. */
clearNavigationHistory(): void;
+
+ /** Synchronously returns whether a back navigation is possible. */
navigationCanGoBack(): boolean;
+
+ /** Synchronously returns whether a forward navigation is possible. */
navigationCanGoForward(): boolean;
+
+ /** Returns every entry in the navigation history, oldest first. */
navigationGetAllEntries(): Array<{ url: string; title: string }>;
+
+ /** Returns the index of the currently active entry inside {@link navigationGetAllEntries}. */
navigationGetActiveIndex(): number;
+
+ /** Returns the total number of entries in the navigation history. */
navigationLength(): number;
+
+ /** Navigates to the entry at the given index in the history stack. */
navigationGoToIndex(index: number): void;
+
+ /** Subscribes to top-level navigation events (URL changes that load a new document). */
onDidNavigate(callback: () => void): void;
+
+ /** Subscribes to in-page navigation events (hash changes, `history.pushState`). */
onDidNavigateInPage(callback: () => void): void;
+
+ /** Removes all listeners registered via {@link onDidNavigate} and {@link onDidNavigateInPage}. */
removeDidNavigateListeners(): void;
}
+/**
+ * The complete surface exposed to the renderer as `window.electronApi` via
+ * `contextBridge`. The renderer must access Electron-only functionality through
+ * this object — direct `require("electron")` and `@electron/remote` are
+ * unavailable because `nodeIntegration` is disabled and `contextIsolation` is
+ * enabled.
+ *
+ * The runtime value lives in `apps/desktop/src/preload.ts`; this interface is
+ * the contract both the preload script (`satisfies ElectronApi`) and the
+ * client (`window.electronApi`) share.
+ */
export interface ElectronApi {
+ /** Window chrome, zoom, theme, lifecycle, and main → renderer events. */
window: ElectronWindowApi;
+ /** System clipboard access beyond what the standard Web Clipboard API offers. */
clipboard: ElectronClipboardApi;
+ /** Shell integration — opening URLs, files, and downloads with OS handlers. */
shell: ElectronShellApi;
+ /** Native context-menu plumbing for right-click events on web contents. */
contextMenu: ElectronContextMenuApi;
+ /** Chromium spell checker controls (dictionary, language list). */
spellcheck: ElectronSpellcheckApi;
+ /** System tray menu controls. */
tray: ElectronTrayApi;
+ /** Printing and PDF export pipeline. */
printing: ElectronPrintingApi;
+ /** Read/write access to Chromium's back/forward navigation history. */
navigation: ElectronNavigationApi;
}