summaryrefslogtreecommitdiff
path: root/packages/frontend/src/nirax.ts
diff options
context:
space:
mode:
authorsyuilo <4439005+syuilo@users.noreply.github.com>2025-03-19 15:17:41 +0900
committersyuilo <4439005+syuilo@users.noreply.github.com>2025-03-19 15:17:41 +0900
commitbdc72e5817782316bb2c56ab362b4aa237236bef (patch)
tree209e6fca274123ea93dc38bce5e9a96bef15a974 /packages/frontend/src/nirax.ts
parent🎨 (diff)
downloadsharkey-bdc72e5817782316bb2c56ab362b4aa237236bef.tar.gz
sharkey-bdc72e5817782316bb2c56ab362b4aa237236bef.tar.bz2
sharkey-bdc72e5817782316bb2c56ab362b4aa237236bef.zip
clean up
Diffstat (limited to 'packages/frontend/src/nirax.ts')
-rw-r--r--packages/frontend/src/nirax.ts57
1 files changed, 7 insertions, 50 deletions
diff --git a/packages/frontend/src/nirax.ts b/packages/frontend/src/nirax.ts
index ea3f1fb01a..03c8b43aae 100644
--- a/packages/frontend/src/nirax.ts
+++ b/packages/frontend/src/nirax.ts
@@ -51,18 +51,15 @@ export type RouterEvent = {
beforePath: string;
path: string;
resolved: Resolved;
- key: string;
}) => void;
replace: (ctx: {
path: string;
- key: string;
}) => void;
push: (ctx: {
beforePath: string;
path: string;
route: RouteDef | null;
props: Map<string, string> | null;
- key: string;
}) => void;
same: () => void;
};
@@ -121,11 +118,9 @@ export interface IRouter extends EventEmitter<RouterEvent> {
getCurrentPath(): string;
- getCurrentKey(): string;
-
push(path: string, flag?: RouterFlag): void;
- replace(path: string, key?: string | null): void;
+ replace(path: string): void;
/** @see EventEmitter */
eventNames(): Array<EventEmitter.EventNames<RouterEvent>>;
@@ -197,7 +192,6 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
private currentPath: string;
private isLoggedIn: boolean;
private notFoundPageComponent: Component;
- private currentKey = Date.now().toString();
private redirectCount = 0;
public navHook: ((path: string, flag?: RouterFlag) => boolean) | null = null;
@@ -215,10 +209,9 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
}
public init() {
- const res = this.navigate(this.currentPath, null, false);
+ const res = this.navigate(this.currentPath, false);
this.emit('replace', {
path: res._parsedRoute.fullPath,
- key: this.currentKey,
});
}
@@ -345,7 +338,7 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
return check(this.routes, _parts);
}
- private navigate(path: string, key: string | null | undefined, emitChange = true, _redirected = false): Resolved {
+ private navigate(path: string, emitChange = true, _redirected = false): Resolved {
const beforePath = this.currentPath;
this.currentPath = path;
@@ -366,7 +359,7 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
if (_redirected && this.redirectCount++ > 10) {
throw new Error('redirect loop detected');
}
- return this.navigate(redirectPath, null, emitChange, true);
+ return this.navigate(redirectPath, emitChange, true);
}
if (res.route.loginRequired && !this.isLoggedIn) {
@@ -375,18 +368,15 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
}
const isSamePath = beforePath === path;
- if (isSamePath && key == null) key = this.currentKey;
this.current = res;
this.currentRef.value = res;
this.currentRoute.value = res.route;
- this.currentKey = res.route.globalCacheKey ?? key ?? path;
if (emitChange && res.route.path !== '/:(*)') {
this.emit('change', {
beforePath,
path,
resolved: res,
- key: this.currentKey,
});
}
@@ -401,10 +391,6 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
return this.currentPath;
}
- public getCurrentKey() {
- return this.currentKey;
- }
-
public push(path: string, flag?: RouterFlag) {
const beforePath = this.currentPath;
if (path === beforePath) {
@@ -415,7 +401,7 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
const cancel = this.navHook(path, flag);
if (cancel) return;
}
- const res = this.navigate(path, null);
+ const res = this.navigate(path);
if (res.route.path === '/:(*)') {
location.href = path;
} else {
@@ -424,43 +410,14 @@ export class Router extends EventEmitter<RouterEvent> implements IRouter {
path: res._parsedRoute.fullPath,
route: res.route,
props: res.props,
- key: this.currentKey,
});
}
}
- public replace(path: string, key?: string | null) {
- const res = this.navigate(path, key);
+ public replace(path: string) {
+ const res = this.navigate(path);
this.emit('replace', {
path: res._parsedRoute.fullPath,
- key: this.currentKey,
});
}
}
-
-export function useScrollPositionManager(getScrollContainer: () => HTMLElement | null, router: IRouter) {
- const scrollPosStore = new Map<string, number>();
-
- onMounted(() => {
- const scrollContainer = getScrollContainer();
- if (scrollContainer == null) return;
-
- scrollContainer.addEventListener('scroll', () => {
- scrollPosStore.set(router.getCurrentKey(), scrollContainer.scrollTop);
- }, { passive: true });
-
- router.addListener('change', ctx => {
- const scrollPos = scrollPosStore.get(ctx.key) ?? 0;
- scrollContainer.scroll({ top: scrollPos, behavior: 'instant' });
- if (scrollPos !== 0) {
- window.setTimeout(() => { // ι·η§»η›΄εΎŒγ―γ‚Ώγ‚€γƒŸγƒ³γ‚°γ«γ‚ˆγ£γ¦γ―γ‚³γƒ³γƒγƒΌγƒγƒ³γƒˆγŒεΎ©ε…ƒγ—εˆ‡γ£γ¦γͺγ„ε―θƒ½ζ€§γ‚‚θ€ƒγˆγ‚‰γ‚Œγ‚‹γŸγ‚ε°‘γ—ζ™‚ι–“γ‚’η©Ίγ‘γ¦ε†εΊ¦γ‚Ήγ‚―γƒ­γƒΌγƒ«
- scrollContainer.scroll({ top: scrollPos, behavior: 'instant' });
- }, 100);
- }
- });
-
- router.addListener('same', () => {
- scrollContainer.scroll({ top: 0, behavior: 'smooth' });
- });
- });
-}