From 522c36c3425bd3d9005589dc254520b56580a6ee Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:34:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?:sparkles:=20=E3=83=AA=E3=83=B3=E3=82=AF?= =?UTF-8?q?=E5=85=88=E3=81=B8=E3=82=B9=E3=82=AF=E3=83=AD=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/dom/pushPageTransition.ts | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 browser/dom/pushPageTransition.ts diff --git a/browser/dom/pushPageTransition.ts b/browser/dom/pushPageTransition.ts new file mode 100644 index 0000000..155bfd5 --- /dev/null +++ b/browser/dom/pushPageTransition.ts @@ -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 = 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.to.title}`, + } + : { + searchQuery: context.query, + }; + pageTransitionContext[`page_${toTitleLc(context.to.title)}`] = value; + localStorage.setItem( + "pageTransitionContext", + JSON.stringify(pageTransitionContext), + ); +}; From c04aa90ac71d351cf3fb661029900eeac07e0f7e Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:37:23 +0900 Subject: [PATCH 2/4] :bug: Forgot to export `pushPageTransition --- browser/dom/mod.ts | 1 + title.test.ts | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/browser/dom/mod.ts b/browser/dom/mod.ts index b046f36..9455b1a 100644 --- a/browser/dom/mod.ts +++ b/browser/dom/mod.ts @@ -11,3 +11,4 @@ export * from "./cache.ts"; export * from "./cursor.ts"; export * from "./selection.ts"; export * from "./stores.ts"; +export * from "./pushPageTransition.ts"; diff --git a/title.test.ts b/title.test.ts index 42d2134..be71ce0 100644 --- a/title.test.ts +++ b/title.test.ts @@ -8,7 +8,10 @@ import { assertStrictEquals } from "./deps/testing.ts"; Deno.test("toTitleLc()", async (t) => { await t.step("` ` -> `_`", () => { - assertStrictEquals(toTitleLc("空白入り タイトル"), "空白入り_タイトル"); + assertStrictEquals( + toTitleLc("空白入り タイトル"), + "空白入り_タイトル", + ); assertStrictEquals( toTitleLc(" 前後にも 空白入り _タイトル "), "_前後にも_空白入り__タイトル_", From 2effad599aaa4d904f677985e1ffde0fbced31e3 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:44:18 +0900 Subject: [PATCH 3/4] =?UTF-8?q?:sparkles:=20open()=E3=81=A7=E3=83=AA?= =?UTF-8?q?=E3=83=B3=E3=82=AF=E5=85=88=E3=81=B8=E3=82=B9=E3=82=AF=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=AB=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E3=81=88=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/dom/open.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/browser/dom/open.ts b/browser/dom/open.ts index 3657bb4..22dc0d3 100644 --- a/browser/dom/open.ts +++ b/browser/dom/open.ts @@ -3,6 +3,10 @@ /// import { encodeTitleURI } from "../../title.ts"; +import { + PageTransitionContext, + pushPageTransition, +} from "./pushPageTransition.ts"; import type { Scrapbox } from "../../deps/scrapbox.ts"; declare const scrapbox: Scrapbox; @@ -24,6 +28,9 @@ export interface OpenOptions { * @default 同じprojectの場合は再読み込みせず、違うprojectの場合は再読込する */ reload?: boolean; + + /** リンク先へスクロールする機能を使うために必要な情報 */ + context?: Omit; } /** ページを開く @@ -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) From d672f57804d9a52539f50f5f27c8ab6d59eb1386 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 5 Jan 2023 10:24:51 +0900 Subject: [PATCH 4/4] =?UTF-8?q?:bug:=20from=E3=81=A8to=E3=82=92=E5=8F=96?= =?UTF-8?q?=E3=82=8A=E9=81=95=E3=81=88=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/dom/pushPageTransition.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/dom/pushPageTransition.ts b/browser/dom/pushPageTransition.ts index 155bfd5..0a167f4 100644 --- a/browser/dom/pushPageTransition.ts +++ b/browser/dom/pushPageTransition.ts @@ -53,7 +53,7 @@ export const pushPageTransition = (context: PageTransitionContext): void => { linkFrom: context.from.title, } : { - linkFrom: `/${context.from.project}/${context.to.title}`, + linkFrom: `/${context.from.project}/${context.from.title}`, } : { searchQuery: context.query,