2022-04-25 08:33:38 +00:00
|
|
|
import { safeRun } from "../common/util";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
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) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
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-03-20 08:56:28 +00:00
|
|
|
}
|
2022-04-01 13:03:12 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
window.addEventListener("popstate", cb);
|
2022-03-20 08:56:28 +00:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:06:34 +00:00
|
|
|
decodeURI(): [string, number] {
|
|
|
|
let parts = decodeURI(location.pathname).substring(1).split("@");
|
|
|
|
let page =
|
|
|
|
parts.length > 1 ? parts.slice(0, parts.length - 1).join("@") : parts[0];
|
|
|
|
let pos = parts.length > 1 ? parts[parts.length - 1] : "0";
|
|
|
|
if (pos.match(/^\d+$/)) {
|
|
|
|
return [page, +pos];
|
|
|
|
} else {
|
|
|
|
return [`${page}@${pos}`, 0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
getCurrentPage(): string {
|
2022-04-24 16:06:34 +00:00
|
|
|
return decodePageUrl(this.decodeURI()[0]);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 13:25:05 +00:00
|
|
|
getCurrentPos(): number {
|
2022-04-24 16:06:34 +00:00
|
|
|
console.log("Pos", this.decodeURI()[1]);
|
|
|
|
return this.decodeURI()[1];
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
}
|