1
0
silverbullet/packages/web/boot.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

import { Editor } from "./editor";
2022-04-25 17:46:08 +00:00
import { safeRun } from "@silverbulletmd/common/util";
import { Space } from "@silverbulletmd/common/spaces/space";
import { HttpSpacePrimitives } from "@silverbulletmd/common/spaces/http_space_primitives";
2022-04-29 16:54:27 +00:00
safeRun(async () => {
// let localSpace = new Space(new IndexedDBSpacePrimitives("pages"), true);
// localSpace.watch();
2022-06-28 12:14:15 +00:00
let password: string | undefined =
localStorage.getItem("password") || undefined;
2022-06-28 12:14:15 +00:00
let httpPrimitives = new HttpSpacePrimitives("", password);
2022-04-29 16:54:27 +00:00
while (true) {
try {
2022-06-28 12:14:15 +00:00
await httpPrimitives.getPageMeta("index");
2022-04-29 16:54:27 +00:00
break;
} catch (e: any) {
if (e.message === "Unauthorized") {
2022-06-28 12:14:15 +00:00
password = prompt("Password: ") || undefined;
if (!password) {
alert("Sorry, need a password");
2022-04-29 16:54:27 +00:00
return;
}
2022-06-28 12:14:15 +00:00
localStorage.setItem("password", password!);
httpPrimitives = new HttpSpacePrimitives("", password);
2022-04-29 16:54:27 +00:00
}
}
}
let serverSpace = new Space(httpPrimitives, true);
serverSpace.watch();
2022-04-25 17:46:08 +00:00
2022-04-29 16:54:27 +00:00
console.log("Booting...");
2022-04-29 16:54:27 +00:00
// // @ts-ignore
// window.syncer = async () => {
// let lastLocalSync = +(localStorage.getItem("lastLocalSync") || "0"),
// lastRemoteSync = +(localStorage.getItem("lastRemoteSync") || "0");
// let syncer = new SpaceSync(
// serverSpace,
// localSpace,
// lastRemoteSync,
// lastLocalSync,
// "_trash/"
// );
// await syncer.syncPages(
// SpaceSync.primaryConflictResolver(serverSpace, localSpace)
// );
// localStorage.setItem("lastLocalSync", "" + syncer.secondaryLastSync);
// localStorage.setItem("lastRemoteSync", "" + syncer.primaryLastSync);
// console.log("Done!");
// };
2022-07-22 11:44:28 +00:00
let editor = new Editor(serverSpace, document.getElementById("sb-root")!, "");
await editor.init();
2022-04-29 16:54:27 +00:00
// @ts-ignore
window.editor = editor;
});
2022-04-25 17:46:08 +00:00
// if (!isDesktop) {
2022-06-14 07:45:22 +00:00
if (localStorage.getItem("disable_sw") !== "true") {
if (navigator.serviceWorker) {
navigator.serviceWorker
.register(new URL("service_worker.ts", import.meta.url), {
type: "module",
})
.then((r) => {
console.log("Service worker registered...");
});
} else {
console.log(
"No launching service worker (not present, maybe because not running on localhost or over SSL)"
);
}
2022-05-04 14:09:39 +00:00
}
2022-04-25 17:46:08 +00:00
// }