1
0
silverbullet/packages/silverbullet-web/navigator.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import { safeRun } from "../silverbullet-common/util";
function encodePageUrl(name: string): string {
return name.replaceAll(" ", "_");
}
function decodePageUrl(url: string): string {
return url.replaceAll("_", " ");
}
2022-03-28 13:25:05 +00:00
export class PathPageNavigator {
navigationResolve?: () => void;
async navigate(page: string, pos?: number) {
2022-04-01 13:02:35 +00:00
window.history.pushState({ page, pos }, page, `/${encodePageUrl(page)}`);
window.dispatchEvent(
new PopStateEvent("popstate", {
state: { page, pos },
})
2022-03-28 13:25:05 +00:00
);
await new Promise<void>((resolve) => {
this.navigationResolve = resolve;
});
this.navigationResolve = undefined;
}
2022-03-28 13:25:05 +00:00
subscribe(
pageLoadCallback: (pageName: string, pos: number) => Promise<void>
): void {
2022-04-01 13:03:12 +00:00
const cb = (event?: PopStateEvent) => {
const gotoPage = this.getCurrentPage();
if (!gotoPage) {
return;
}
safeRun(async () => {
2022-04-04 13:25:07 +00:00
await pageLoadCallback(
this.getCurrentPage(),
event?.state && event.state.pos
);
2022-04-01 13:03:12 +00:00
if (this.navigationResolve) {
this.navigationResolve();
}
2022-04-01 13:03:12 +00:00
});
};
window.addEventListener("popstate", cb);
cb();
}
getCurrentPage(): string {
2022-04-10 09:04:07 +00:00
let [page] = decodeURI(location.pathname).substring(1).split("@");
2022-03-28 13:25:05 +00:00
return decodePageUrl(page);
}
2022-03-28 13:25:05 +00:00
getCurrentPos(): number {
2022-04-10 09:04:07 +00:00
let [, pos] = decodeURI(location.pathname).substring(1).split("@");
2022-03-28 13:25:05 +00:00
return +pos || 0;
}
}