Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone): report the host OS so macOS gets its shortcut defaults

Elian Doran55508128930f
diff --git a/apps/desktop/src/platform_provider.spec.ts b/apps/desktop/src/platform_provider.spec.ts
index 1b2861bbfc..1f75d335c3 100644
--- a/apps/desktop/src/platform_provider.spec.ts
+++ b/apps/desktop/src/platform_provider.spec.ts
@@ -46,6 +46,7 @@ describe("DesktopPlatformProvider", () => {
withPlatform("darwin", () => {
const p = new DesktopPlatformProvider();
expect(p.isElectron).toBe(true);
+ expect(p.isStandalone).toBe(false);
expect(p.isMac).toBe(true);
expect(p.isWindows).toBe(false);
expect(p.isLinux).toBe(false);
diff --git a/apps/desktop/src/platform_provider.ts b/apps/desktop/src/platform_provider.ts
index e3d499157b..77c1db7693 100644
--- a/apps/desktop/src/platform_provider.ts
+++ b/apps/desktop/src/platform_provider.ts
@@ -5,6 +5,7 @@ import path from "path";
export default class DesktopPlatformProvider implements PlatformProvider {
readonly isElectron = true;
+ readonly isStandalone = false;
readonly isMac = process.platform === "darwin";
readonly isWindows = process.platform === "win32";
readonly isLinux = process.platform === "linux";
diff --git a/apps/server/src/platform_provider.spec.ts b/apps/server/src/platform_provider.spec.ts
index 7b1d74485f..10fb36f34d 100644
--- a/apps/server/src/platform_provider.spec.ts
+++ b/apps/server/src/platform_provider.spec.ts
@@ -11,6 +11,7 @@ describe("ServerPlatformProvider", () => {
it("reflects the runtime platform flags", () => {
const provider = new ServerPlatformProvider();
expect(provider.isElectron).toBe(!!process.versions["electron"]);
+ expect(provider.isStandalone).toBe(false);
expect(provider.isMac).toBe(process.platform === "darwin");
expect(provider.isWindows).toBe(process.platform === "win32");
expect(provider.isLinux).toBe(process.platform === "linux");
diff --git a/apps/server/src/platform_provider.ts b/apps/server/src/platform_provider.ts
index 6244d629d2..14f382c23d 100644
--- a/apps/server/src/platform_provider.ts
+++ b/apps/server/src/platform_provider.ts
@@ -5,6 +5,7 @@ import dataDir from "./services/data_dir.js";
export default class ServerPlatformProvider implements PlatformProvider {
readonly isElectron = !!process.versions["electron"];
+ readonly isStandalone = false;
readonly isMac = process.platform === "darwin";
readonly isWindows = process.platform === "win32";
readonly isLinux = process.platform === "linux";
diff --git a/apps/standalone/src/lightweight/platform_provider.spec.ts b/apps/standalone/src/lightweight/platform_provider.spec.ts
index 6c03f897d8..0a2359a69a 100644
--- a/apps/standalone/src/lightweight/platform_provider.spec.ts
+++ b/apps/standalone/src/lightweight/platform_provider.spec.ts
@@ -10,9 +10,22 @@ describe("StandalonePlatformProvider", () => {
it("exposes the standalone platform flags", () => {
const provider = new StandalonePlatformProvider("");
expect(provider.isElectron).toBe(false);
- expect(provider.isMac).toBe(false);
- expect(provider.isWindows).toBe(false);
- expect(provider.isLinux).toBe(false);
+ expect(provider.isStandalone).toBe(true);
+ });
+
+ it.each([
+ ["MacIntel", "Mozilla/5.0 (Macintosh)", { isMac: true, isWindows: false, isLinux: false }],
+ ["Win32", "Mozilla/5.0 (Windows NT 10.0)", { isMac: false, isWindows: true, isLinux: false }],
+ ["Linux x86_64", "Mozilla/5.0 (X11; Linux x86_64)", { isMac: false, isWindows: false, isLinux: true }],
+ // Android reports a Linux platform, but is not desktop Linux.
+ ["Linux armv8l", "Mozilla/5.0 (Linux; Android 14)", { isMac: false, isWindows: false, isLinux: false }],
+ ["iPhone", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0)", { isMac: false, isWindows: false, isLinux: false }]
+ ])("reads the host OS from navigator.platform %s", (platform, userAgent, expected) => {
+ vi.spyOn(navigator, "platform", "get").mockReturnValue(platform);
+ vi.spyOn(navigator, "userAgent", "get").mockReturnValue(userAgent);
+
+ const provider = new StandalonePlatformProvider("");
+ expect({ isMac: provider.isMac, isWindows: provider.isWindows, isLinux: provider.isLinux }).toEqual(expected);
});
it("maps known query parameters to TRILIUM_ env vars", () => {
diff --git a/apps/standalone/src/lightweight/platform_provider.ts b/apps/standalone/src/lightweight/platform_provider.ts
index a6b0f35e20..72fe01bab1 100644
--- a/apps/standalone/src/lightweight/platform_provider.ts
+++ b/apps/standalone/src/lightweight/platform_provider.ts
@@ -11,9 +11,11 @@ const QUERY_TO_ENV: Record<string, string> = {
export default class StandalonePlatformProvider implements PlatformProvider {
readonly isElectron = false;
- readonly isMac = false;
- readonly isWindows = false;
- readonly isLinux = false;
+ readonly isStandalone = true;
+ readonly isMac = matchesPlatform("Mac");
+ readonly isWindows = matchesPlatform("Win");
+ // Android reports a Linux platform string; keep `isLinux` meaning desktop Linux.
+ readonly isLinux = matchesPlatform("Linux") && !navigator.userAgent.includes("Android");
private envMap: Record<string, string> = {};
@@ -47,3 +49,12 @@ export default class StandalonePlatformProvider implements PlatformProvider {
return null;
}
}
+
+/**
+ * Reads the host OS from the worker's `navigator`. `navigator.platform` is deprecated but is the one
+ * field a worker gets in every browser, and the client's `isMac()` reads it too — the two must agree,
+ * or the shortcuts the settings pane renders differ from the ones `/api/keyboard-actions` served.
+ */
+function matchesPlatform(name: string) {
+ return navigator.platform.includes(name);
+}
diff --git a/packages/trilium-core/src/services/keyboard_actions.spec.ts b/packages/trilium-core/src/services/keyboard_actions.spec.ts
index d183e0be07..ba6a8cf074 100644
--- a/packages/trilium-core/src/services/keyboard_actions.spec.ts
+++ b/packages/trilium-core/src/services/keyboard_actions.spec.ts
@@ -16,6 +16,7 @@ vi.mock("i18next", async (importOriginal) => {
import * as cls from "./context.js";
import keyboardActions from "./keyboard_actions.js";
import options from "./options.js";
+import { getPlatform, initPlatform } from "./platform.js";
function upsertOption(name: string, value: string) {
cls.init(() => {
@@ -75,6 +76,26 @@ describe("keyboard_actions service", () => {
expect(actions[firstIndex + splitActions.length]).toHaveProperty("separator");
});
+ it.each([
+ [true, { jumpToNote: ["Meta+J"], backInNoteHistory: ["Meta+["] }],
+ [false, { jumpToNote: ["Ctrl+J"], backInNoteHistory: ["Alt+Left"] }]
+ ])("resolves CommandOrControl and the history bindings for isMac=%s", (isMac, expected) => {
+ const real = getPlatform();
+ initPlatform(Object.create(real, { isMac: { value: isMac } }));
+
+ try {
+ const actions = keyboardActions.getDefaultKeyboardActions();
+
+ for (const [actionName, shortcuts] of Object.entries(expected)) {
+ const action = actions.find((a) => "actionName" in a && a.actionName === actionName);
+ expect(action, actionName).toBeDefined();
+ expect(action && "defaultShortcuts" in action && action.defaultShortcuts, actionName).toEqual(shortcuts);
+ }
+ } finally {
+ initPlatform(real);
+ }
+ });
+
it("throws if loaded before translations are available", async () => {
// keyboard_actions was already evaluated during the bootstrap (binding the
// real i18next), so re-import a fresh copy that picks up the mocked module.
diff --git a/packages/trilium-core/src/services/platform.ts b/packages/trilium-core/src/services/platform.ts
index 09cd499b4a..f271f1139a 100644
--- a/packages/trilium-core/src/services/platform.ts
+++ b/packages/trilium-core/src/services/platform.ts
@@ -6,6 +6,11 @@ export interface PlatformProvider {
/** Returns the value of an environment variable, or undefined if not set. */
getEnv(key: string): string | undefined;
readonly isElectron: boolean;
+ /**
+ * `true` in the standalone build, where core runs inside a browser worker rather than on Node.
+ * The OS flags below stay honest there, so this is what tells standalone apart from a desktop.
+ */
+ readonly isStandalone: boolean;
readonly isMac: boolean;
readonly isWindows: boolean;
readonly isLinux: boolean;
diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.ts
index ebcbdaf99e..e4e75ea333 100644
--- a/packages/trilium-core/src/services/sync.ts
+++ b/packages/trilium-core/src/services/sync.ts
@@ -15,7 +15,7 @@ import sqlInit from "./sql_init.js";
import syncMutexService from "./sync_mutex.js";
import syncOptions from "./sync_options.js";
import syncUpdateService from "./sync_update.js";
-import { isLinux, isMac, isWindows, randomString, timeLimit } from "./utils/index.js";
+import { isStandalone, randomString, timeLimit } from "./utils/index.js";
import ws from "./ws.js";
import getInstanceId from "./instance_id.js";
import request, { CookieJar, ExecOpts } from "./request.js";
@@ -445,14 +445,11 @@ async function fetchChangesChunk(syncContext: SyncContext, cursor: number, maxBl
* Maximum accumulated content size (in bytes) to buffer across pulled chunks before applying them
* in a single transaction during {@link pullChanges}. Larger batches mean fewer commits (each of
* which carries a large fixed overhead) at the cost of higher peak memory while the batch is held
- * in memory. The standalone/browser build runs SQLite (sql.js) fully in memory, so it uses a much
- * smaller budget than native desktop/server builds.
+ * in memory. The standalone build holds that batch in a browser worker, whose renderer heap is far
+ * smaller than a Node process's, so it uses a smaller budget than native desktop/server builds.
*/
function getMaxPullBatchBytes() {
- // Standalone/browser is the only platform reporting none of mac/windows/linux.
- const isBrowser = !isMac() && !isWindows() && !isLinux();
-
- if (!isBrowser) {
+ if (!isStandalone()) {
return 32 * 1024 * 1024; // desktop
}
diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.ts
index 3e9e8d8b80..fdb6f277e8 100644
--- a/packages/trilium-core/src/services/utils/index.ts
+++ b/packages/trilium-core/src/services/utils/index.ts
@@ -11,6 +11,7 @@ import { NoteMeta } from "../../meta";
export function isDev() { return getPlatform().getEnv("TRILIUM_ENV") === "dev"; }
export function isElectron() { return getPlatform().isElectron; }
+export function isStandalone() { return getPlatform().isStandalone; }
export function isMac() { return getPlatform().isMac; }
export function isWindows() { return getPlatform().isWindows; }
export function isLinux() { return getPlatform().isLinux; }