Make index page configurable

This commit is contained in:
Zef Hemel
2022-08-02 12:43:39 +02:00
parent b29b175b40
commit 7111fb6b1e
10 changed files with 81 additions and 25 deletions
+11 -3
View File
@@ -1,5 +1,5 @@
import { Editor } from "./editor";
import { safeRun } from "@silverbulletmd/common/util";
import { parseYamlSettings, safeRun } from "@silverbulletmd/common/util";
import { Space } from "@silverbulletmd/common/spaces/space";
import { HttpSpacePrimitives } from "@silverbulletmd/common/spaces/http_space_primitives";
@@ -8,9 +8,10 @@ safeRun(async () => {
localStorage.getItem("password") || undefined;
let httpPrimitives = new HttpSpacePrimitives("", password);
let settingsPageText = "";
while (true) {
try {
await httpPrimitives.getPageMeta("index");
settingsPageText = (await httpPrimitives.readPage("SETTINGS")).text;
break;
} catch (e: any) {
if (e.message === "Unauthorized") {
@@ -29,7 +30,14 @@ safeRun(async () => {
console.log("Booting...");
let editor = new Editor(serverSpace, document.getElementById("sb-root")!, "");
let settings = parseYamlSettings(settingsPageText);
let editor = new Editor(
serverSpace,
document.getElementById("sb-root")!,
"",
settings.indexPage || "index"
);
await editor.init();
// @ts-ignore
window.editor = editor;
+13 -7
View File
@@ -114,12 +114,19 @@ export class Editor {
private system = new System<SilverBulletHooks>("client");
private mdExtensions: MDExt[] = [];
urlPrefix: string;
indexPage: string;
constructor(space: Space, parent: Element, urlPrefix: string) {
constructor(
space: Space,
parent: Element,
urlPrefix: string,
indexPage: string
) {
this.space = space;
this.urlPrefix = urlPrefix;
this.viewState = initialViewState;
this.viewDispatch = () => {};
this.indexPage = indexPage;
// Event hook
this.eventHook = new EventHook();
@@ -146,7 +153,7 @@ export class Editor {
state: this.createEditorState("", ""),
parent: document.getElementById("sb-editor")!,
});
this.pageNavigator = new PathPageNavigator(urlPrefix);
this.pageNavigator = new PathPageNavigator(indexPage, urlPrefix);
this.system.registerSyscalls(
[],
@@ -242,11 +249,7 @@ export class Editor {
},
});
if (this.pageNavigator.getCurrentPage() === "") {
await this.pageNavigator.navigate("index");
}
await this.reloadPlugs();
await this.dispatchAppEvent("editor:init");
}
@@ -558,6 +561,9 @@ export class Editor {
}
async navigate(name: string, pos?: number, replaceState = false) {
if (!name) {
name = this.indexPage;
}
await this.pageNavigator.navigate(name, pos, replaceState);
}
@@ -712,7 +718,7 @@ export class Editor {
dispatch({ type: "start-navigate" });
}}
onHomeClick={() => {
editor.navigate("index");
editor.navigate("");
}}
onActionClick={() => {
dispatch({ type: "show-palette" });
+8 -4
View File
@@ -11,20 +11,24 @@ function decodePageUrl(url: string): string {
export class PathPageNavigator {
navigationResolve?: () => void;
constructor(readonly root: string = "") {}
constructor(readonly indexPage: string, readonly root: string = "") {}
async navigate(page: string, pos?: number, replaceState = false) {
let encodedPage = encodePageUrl(page);
if (page === this.indexPage) {
encodedPage = "";
}
if (replaceState) {
window.history.replaceState(
{ page, pos },
page,
`${this.root}/${encodePageUrl(page)}`
`${this.root}/${encodedPage}`
);
} else {
window.history.pushState(
{ page, pos },
page,
`${this.root}/${encodePageUrl(page)}`
`${this.root}/${encodedPage}`
);
}
window.dispatchEvent(
@@ -75,7 +79,7 @@ export class PathPageNavigator {
}
getCurrentPage(): string {
return decodePageUrl(this.decodeURI()[0]);
return decodePageUrl(this.decodeURI()[0]) || this.indexPage;
}
getCurrentPos(): number {
+1 -1
View File
@@ -23,7 +23,7 @@ export function spaceSyscalls(editor: Editor): SysCallMapping {
"space.deletePage": async (ctx, name: string) => {
// If we're deleting the current page, navigate to the index page
if (editor.currentPage === name) {
await editor.navigate("index");
await editor.navigate("");
}
// Remove page from open pages in editor
editor.openPages.delete(name);