Skip to content

Build your personal knowledge base with Trilium Notes

refactor(electron): get rid of non-null assertions

Elian Doran5793f7d48eae
diff --git a/apps/client/src/components/zoom.ts b/apps/client/src/components/zoom.ts
index 8e8aecb95c..5386754146 100644
--- a/apps/client/src/components/zoom.ts
+++ b/apps/client/src/components/zoom.ts
@@ -27,7 +27,7 @@ class ZoomComponent extends Component {
setZoomFactor(zoomFactor: string | number) {
const parsedZoomFactor = typeof zoomFactor !== "number" ? parseFloat(zoomFactor) : zoomFactor;
- window.electronApi!.setZoomFactor(parsedZoomFactor);
+ window.electronApi?.setZoomFactor(parsedZoomFactor);
}
async setZoomFactorAndSave(zoomFactor: number) {
@@ -43,7 +43,7 @@ class ZoomComponent extends Component {
}
getCurrentZoom() {
- return window.electronApi!.getZoomFactor();
+ return window.electronApi?.getZoomFactor() ?? 1.0;
}
zoomOutEvent() {
diff --git a/apps/client/src/desktop.ts b/apps/client/src/desktop.ts
index 4bec4d3e90..d8f2ca8341 100644
--- a/apps/client/src/desktop.ts
+++ b/apps/client/src/desktop.ts
@@ -44,32 +44,33 @@ if (utils.isElectron()) {
}
function initOnElectron() {
- const api = window.electronApi!;
+ const api = window.electronApi;
+ if (!api) return;
api.onGlobalShortcut(async (actionName) => appContext.triggerCommand(actionName));
api.onOpenInSameTab(async (noteId) => appContext.tabManager.openInSameTab(noteId));
const style = window.getComputedStyle(document.body);
- initDarkOrLightMode();
- initTransparencyEffects(style);
- initFullScreenDetection();
+ initDarkOrLightMode(api);
+ initTransparencyEffects(api, style);
+ initFullScreenDetection(api);
if (options.get("nativeTitleBarVisible") !== "true") {
- initTitleBarButtons(style);
+ initTitleBarButtons(api, style);
}
// Clear navigation history on frontend refresh.
api.clearNavigationHistory();
}
-function initTitleBarButtons(style: CSSStyleDeclaration) {
+function initTitleBarButtons(api: ElectronApi, style: CSSStyleDeclaration) {
if (window.glob.platform === "win32") {
const applyWindowsOverlay = () => {
const color = style.getPropertyValue("--native-titlebar-background");
const symbolColor = style.getPropertyValue("--native-titlebar-foreground");
if (color && symbolColor) {
- window.electronApi!.setTitleBarOverlay({ color, symbolColor });
+ api.setTitleBarOverlay({ color, symbolColor });
}
};
@@ -82,22 +83,22 @@ function initTitleBarButtons(style: CSSStyleDeclaration) {
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);
- window.electronApi!.setWindowButtonPosition({ x: xOffset, y: yOffset });
+ api.setWindowButtonPosition({ x: xOffset, y: yOffset });
}
}
-function initFullScreenDetection() {
- window.electronApi!.onEnterFullScreen(() => document.body.classList.add("full-screen"));
- window.electronApi!.onLeaveFullScreen(() => document.body.classList.remove("full-screen"));
+function initFullScreenDetection(api: ElectronApi) {
+ api.onEnterFullScreen(() => document.body.classList.add("full-screen"));
+ api.onLeaveFullScreen(() => document.body.classList.remove("full-screen"));
}
-function initTransparencyEffects(style: CSSStyleDeclaration) {
+function initTransparencyEffects(api: ElectronApi, style: CSSStyleDeclaration) {
const material = style.getPropertyValue("--background-material").trim();
if (window.glob.platform === "win32") {
const bgMaterialOptions = ["auto", "none", "mica", "acrylic", "tabbed"] as const;
const foundBgMaterialOption = bgMaterialOptions.find((bgMaterialOption) => material === bgMaterialOption);
if (foundBgMaterialOption) {
- window.electronApi!.setBackgroundMaterial(foundBgMaterialOption);
+ api.setBackgroundMaterial(foundBgMaterialOption);
}
}
@@ -105,7 +106,7 @@ function initTransparencyEffects(style: CSSStyleDeclaration) {
const bgMaterialOptions = [ "popover", "tooltip", "titlebar", "selection", "menu", "sidebar", "header", "sheet", "window", "hud", "fullscreen-ui", "content", "under-window", "under-page" ] as const;
const foundBgMaterialOption = bgMaterialOptions.find((bgMaterialOption) => material === bgMaterialOption);
if (foundBgMaterialOption) {
- window.electronApi!.setVibrancy(foundBgMaterialOption);
+ api.setVibrancy(foundBgMaterialOption);
}
}
}
@@ -114,7 +115,7 @@ function initTransparencyEffects(style: CSSStyleDeclaration) {
* 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.
*/
-function initDarkOrLightMode() {
+function initDarkOrLightMode(api: ElectronApi) {
let themeSource: "system" | "light" | "dark" = "system";
const themeStyle = window.glob.getThemeStyle();
@@ -122,5 +123,5 @@ function initDarkOrLightMode() {
themeSource = themeStyle;
}
- window.electronApi!.setNativeThemeSource(themeSource);
+ api.setNativeThemeSource(themeSource);
}
diff --git a/apps/client/src/menus/electron_context_menu.ts b/apps/client/src/menus/electron_context_menu.ts
index 8584984c59..b8cbc97ac9 100644
--- a/apps/client/src/menus/electron_context_menu.ts
+++ b/apps/client/src/menus/electron_context_menu.ts
@@ -9,7 +9,8 @@ import utils from "../services/utils.js";
import contextMenu, { type MenuItem } from "./context_menu.js";
function setupContextMenu() {
- const api = window.electronApi!;
+ const api = window.electronApi;
+ if (!api) return;
const isMac = window.glob.platform === "darwin";
const platformModifier = isMac ? "Meta" : "Ctrl";
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index d74d506ed4..846c39e6ad 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -140,8 +140,8 @@ export default function NoteDetail() {
// Handle toast notifications.
useEffect(() => {
- if (!isElectron()) return;
- const api = window.electronApi!;
+ const api = window.electronApi;
+ if (!api) return;
api.onPrintProgress(({ progress, action }) => showToast(action as "printing" | "exporting_pdf", progress));
api.onPrintDone((printReport) => {
toast.closePersistent("printing");
diff --git a/apps/client/src/widgets/TabHistoryNavigationButtons.tsx b/apps/client/src/widgets/TabHistoryNavigationButtons.tsx
index 2aa9c7825b..75a51af50c 100644
--- a/apps/client/src/widgets/TabHistoryNavigationButtons.tsx
+++ b/apps/client/src/widgets/TabHistoryNavigationButtons.tsx
@@ -36,13 +36,12 @@ export default function TabHistoryNavigationButtons() {
}
function useBackForwardState(hasApi: boolean | ElectronApi | undefined) {
- const [ canGoBack, setCanGoBack ] = useState(() => hasApi ? window.electronApi!.navigationCanGoBack() : true);
- const [ canGoForward, setCanGoForward ] = useState(() => hasApi ? window.electronApi!.navigationCanGoForward() : true);
+ const [ canGoBack, setCanGoBack ] = useState(() => hasApi ? window.electronApi?.navigationCanGoBack() ?? true : true);
+ const [ canGoForward, setCanGoForward ] = useState(() => hasApi ? window.electronApi?.navigationCanGoForward() ?? true : true);
useEffect(() => {
- if (!hasApi) return;
-
- const api = window.electronApi!;
+ const api = window.electronApi;
+ if (!api) return;
const updateNavigationState = () => {
setCanGoBack(api.navigationCanGoBack());
setCanGoForward(api.navigationCanGoForward());
diff --git a/apps/client/src/widgets/launch_bar/HistoryNavigation.tsx b/apps/client/src/widgets/launch_bar/HistoryNavigation.tsx
index 16a60d8cfe..9e203c2bb6 100644
--- a/apps/client/src/widgets/launch_bar/HistoryNavigation.tsx
+++ b/apps/client/src/widgets/launch_bar/HistoryNavigation.tsx
@@ -32,7 +32,7 @@ export default function HistoryNavigationButton({ launcherNote, command }: Histo
extraItems: items,
onCommand: (cmd) => {
if (cmd && hasApi) {
- window.electronApi!.navigationGoToIndex(parseInt(cmd, 10));
+ window.electronApi?.navigationGoToIndex(parseInt(cmd, 10));
}
}
});
@@ -42,8 +42,8 @@ export default function HistoryNavigationButton({ launcherNote, command }: Histo
}
async function getHistoryItems(): Promise<MenuCommandItem<string>[]> {
- const api = window.electronApi!;
- if (api.navigationLength() < 2) return [];
+ const api = window.electronApi;
+ if (!api || api.navigationLength() < 2) return [];
let items: MenuCommandItem<string>[] = [];
@@ -90,7 +90,7 @@ export function handleHistoryContextMenu() {
selectMenuItemHandler: (item: MenuCommandItem<string>) => {
if (item && item.command) {
const idx = parseInt(item.command, 10);
- window.electronApi!.navigationGoToIndex(idx);
+ window.electronApi?.navigationGoToIndex(idx);
}
}
});