Skip to content

✨ リンク先へスクロールする #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions browser/dom/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from "./cache.ts";
export * from "./cursor.ts";
export * from "./selection.ts";
export * from "./stores.ts";
export * from "./pushPageTransition.ts";
13 changes: 13 additions & 0 deletions browser/dom/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/// <reference lib="dom" />

import { encodeTitleURI } from "../../title.ts";
import {
PageTransitionContext,
pushPageTransition,
} from "./pushPageTransition.ts";
import type { Scrapbox } from "../../deps/scrapbox.ts";
declare const scrapbox: Scrapbox;

Expand All @@ -24,6 +28,9 @@ export interface OpenOptions {
* @default 同じprojectの場合は再読み込みせず、違うprojectの場合は再読込する
*/
reload?: boolean;

/** リンク先へスクロールする機能を使うために必要な情報 */
context?: Omit<PageTransitionContext, "to">;
}

/** ページを開く
Expand All @@ -41,6 +48,12 @@ export const open = (
if (options?.body) url.search = `?body=${encodeURIComponent(options.body)}`;
if (options?.id) url.hash = `#${options.id}`;

if (options?.context) {
pushPageTransition(
{ ...options?.context, to: { project, title } } as PageTransitionContext,
);
}

if (
options?.newTab !== false &&
(options?.newTab === true || project !== scrapbox.Project.name)
Expand Down
66 changes: 66 additions & 0 deletions browser/dom/pushPageTransition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { toTitleLc } from "../../title.ts";

/** ページリンク */
export interface Link {
/** リンク先のproject name */
project: string;

/** リンク先のpage title */
title: string;
}

/** ページから別のページに遷移するときの状態を表す */
export interface PageTransitionContextLink {
type: "page";

/** 遷移元ページのリンク */
from: Link;

/** 遷移先ページのリンク */
to: Link;
}

/** 全文検索結果から別のページに遷移するときの状態を表す */
export interface PageTransitionContextQuery {
type: "search";

/** 全文検索での検索語句 */
query: string;

/** 遷移先ページのリンク */
to: Link;
}

export type PageTransitionContext =
| PageTransitionContextLink
| PageTransitionContextQuery;

/** ページ遷移状態を登録し、次回のページ遷移時にリンク先へスクロールする
*
* @param context 遷移状態
*/
export const pushPageTransition = (context: PageTransitionContext): void => {
const pageTransitionContext: Record<string, unknown> = JSON.parse(
localStorage.getItem("pageTransitionContext") ?? "",
);
const value = context.type === "page"
? context.from.project === context.to.project
? context.from.title === context.to.title
? {
titleHint: context.to.title,
}
: {
linkFrom: context.from.title,
}
: {
linkFrom: `/${context.from.project}/${context.from.title}`,
}
: {
searchQuery: context.query,
};
pageTransitionContext[`page_${toTitleLc(context.to.title)}`] = value;
localStorage.setItem(
"pageTransitionContext",
JSON.stringify(pageTransitionContext),
);
};
5 changes: 4 additions & 1 deletion title.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { assertStrictEquals } from "./deps/testing.ts";

Deno.test("toTitleLc()", async (t) => {
await t.step("` ` -> `_`", () => {
assertStrictEquals<string>(toTitleLc("空白入り タイトル"), "空白入り_タイトル");
assertStrictEquals<string>(
toTitleLc("空白入り タイトル"),
"空白入り_タイトル",
);
assertStrictEquals<string>(
toTitleLc(" 前後にも 空白入り _タイトル "),
"_前後にも_空白入り__タイトル_",
Expand Down