2022-10-10 12:50:21 +00:00
|
|
|
import { Editor } from "./editor.tsx";
|
|
|
|
import { parseYamlSettings, safeRun } from "../common/util.ts";
|
|
|
|
import { Space } from "../common/spaces/space.ts";
|
|
|
|
import { HttpSpacePrimitives } from "../common/spaces/http_space_primitives.ts";
|
2022-11-24 11:04:00 +00:00
|
|
|
import { PlugSpacePrimitives } from "../server/hooks/plug_space_primitives.ts";
|
|
|
|
import { PageNamespaceHook } from "../server/hooks/page_namespace.ts";
|
|
|
|
import { SilverBulletHooks } from "../common/manifest.ts";
|
|
|
|
import { System } from "../plugos/system.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
|
|
|
safeRun(async () => {
|
|
|
|
let password: string | undefined = localStorage.getItem("password") ||
|
|
|
|
undefined;
|
|
|
|
|
|
|
|
let httpPrimitives = new HttpSpacePrimitives("", password);
|
|
|
|
let settingsPageText = "";
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
settingsPageText = (
|
|
|
|
await httpPrimitives.readFile("SETTINGS.md", "string")
|
|
|
|
).data as string;
|
|
|
|
break;
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e.message === "Unauthorized") {
|
|
|
|
password = prompt("Password: ") || undefined;
|
|
|
|
if (!password) {
|
|
|
|
alert("Sorry, need a password");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
localStorage.setItem("password", password!);
|
|
|
|
httpPrimitives = new HttpSpacePrimitives("", password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 11:04:00 +00:00
|
|
|
|
|
|
|
// Instantiate a PlugOS system for the client
|
|
|
|
const system = new System<SilverBulletHooks>("client");
|
|
|
|
|
|
|
|
// Attach the page namespace hook
|
|
|
|
const namespaceHook = new PageNamespaceHook();
|
|
|
|
system.addHook(namespaceHook);
|
|
|
|
|
|
|
|
const spacePrimitives = new PlugSpacePrimitives(
|
|
|
|
httpPrimitives,
|
|
|
|
namespaceHook,
|
|
|
|
"client",
|
|
|
|
);
|
|
|
|
|
|
|
|
const serverSpace = new Space(spacePrimitives);
|
2022-10-10 12:50:21 +00:00
|
|
|
serverSpace.watch();
|
|
|
|
|
|
|
|
console.log("Booting...");
|
|
|
|
|
|
|
|
const settings = parseYamlSettings(settingsPageText);
|
|
|
|
|
|
|
|
const editor = new Editor(
|
|
|
|
serverSpace,
|
2022-11-24 11:04:00 +00:00
|
|
|
system,
|
2022-10-10 12:50:21 +00:00
|
|
|
document.getElementById("sb-root")!,
|
|
|
|
"",
|
|
|
|
settings.indexPage || "index",
|
|
|
|
);
|
|
|
|
// @ts-ignore: for convenience
|
|
|
|
window.editor = editor;
|
2022-10-25 16:50:07 +00:00
|
|
|
|
|
|
|
await editor.init();
|
2022-10-10 12:50:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (navigator.serviceWorker) {
|
|
|
|
navigator.serviceWorker
|
2022-11-24 11:04:00 +00:00
|
|
|
.register(new URL("/service_worker.js", location.href), {
|
2022-10-10 12:50:21 +00:00
|
|
|
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)",
|
|
|
|
);
|
|
|
|
}
|