silverbullet/web/boot.ts

54 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import { safeRun } from "../lib/async.ts";
2023-07-14 22:56:20 +08:00
import { Client } from "./client.ts";
2024-07-30 21:17:34 +08:00
const syncMode = globalThis.silverBulletConfig.syncOnly ||
2023-08-30 03:17:29 +08:00
!!localStorage.getItem("syncMode");
2023-08-28 23:12:15 +08:00
safeRun(async () => {
console.log(
"Booting SilverBullet client",
syncMode ? "in Sync Mode" : "in Online Mode",
);
2024-10-10 18:52:28 +08:00
if (globalThis.silverBulletConfig.readOnly) {
console.log("Running in read-only mode");
}
2023-07-14 22:56:20 +08:00
const client = new Client(
document.getElementById("sb-root")!,
2023-08-30 03:17:29 +08:00
syncMode,
2024-10-10 18:52:28 +08:00
globalThis.silverBulletConfig.readOnly,
);
2024-07-30 21:17:34 +08:00
// @ts-ignore: on purpose
globalThis.client = client;
2024-01-24 21:03:14 +08:00
await client.init();
});
if (navigator.serviceWorker) {
navigator.serviceWorker
2022-11-24 19:04:00 +08:00
.register(new URL("/service_worker.js", location.href), {
type: "module",
})
2022-11-24 23:55:30 +08:00
.then(() => {
console.log("Service worker registered...");
});
2023-08-30 03:17:29 +08:00
if (syncMode) {
2023-08-26 14:31:51 +08:00
navigator.serviceWorker.ready.then((registration) => {
registration.active!.postMessage({
type: "config",
2024-07-30 21:17:34 +08:00
config: globalThis.silverBulletConfig,
2023-08-26 14:31:51 +08:00
});
});
2023-08-26 14:31:51 +08:00
}
} else {
console.warn(
"Not launching service worker, likely because not running from localhost or over HTTPs. This means SilverBullet will not be available offline.",
);
}
2023-05-24 11:42:24 +08:00
if (!globalThis.indexedDB) {
alert(
"SilverBullet requires IndexedDB to operate and it is not available in your browser. Please use a recent version of Chrome, Firefox (not in private mode) or Safari.",
);
}