Skip to content

Commit 522c36c

Browse files
committed
✨ リンク先へスクロールする
1 parent 1f0e5c2 commit 522c36c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

browser/dom/pushPageTransition.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { toTitleLc } from "../../title.ts";
2+
3+
/** ページリンク */
4+
export interface Link {
5+
/** リンク先のproject name */
6+
project: string;
7+
8+
/** リンク先のpage title */
9+
title: string;
10+
}
11+
12+
/** ページから別のページに遷移するときの状態を表す */
13+
export interface PageTransitionContextLink {
14+
type: "page";
15+
16+
/** 遷移元ページのリンク */
17+
from: Link;
18+
19+
/** 遷移先ページのリンク */
20+
to: Link;
21+
}
22+
23+
/** 全文検索結果から別のページに遷移するときの状態を表す */
24+
export interface PageTransitionContextQuery {
25+
type: "search";
26+
27+
/** 全文検索での検索語句 */
28+
query: string;
29+
30+
/** 遷移先ページのリンク */
31+
to: Link;
32+
}
33+
34+
export type PageTransitionContext =
35+
| PageTransitionContextLink
36+
| PageTransitionContextQuery;
37+
38+
/** ページ遷移状態を登録し、次回のページ遷移時にリンク先へスクロールする
39+
*
40+
* @param context 遷移状態
41+
*/
42+
export const pushPageTransition = (context: PageTransitionContext): void => {
43+
const pageTransitionContext: Record<string, unknown> = JSON.parse(
44+
localStorage.getItem("pageTransitionContext") ?? "",
45+
);
46+
const value = context.type === "page"
47+
? context.from.project === context.to.project
48+
? context.from.title === context.to.title
49+
? {
50+
titleHint: context.to.title,
51+
}
52+
: {
53+
linkFrom: context.from.title,
54+
}
55+
: {
56+
linkFrom: `/${context.from.project}/${context.to.title}`,
57+
}
58+
: {
59+
searchQuery: context.query,
60+
};
61+
pageTransitionContext[`page_${toTitleLc(context.to.title)}`] = value;
62+
localStorage.setItem(
63+
"pageTransitionContext",
64+
JSON.stringify(pageTransitionContext),
65+
);
66+
};

0 commit comments

Comments
 (0)