Skip to content

Commit 600c382

Browse files
committed
✨ いろんな方法でページを開けるようにした
1 parent deae0b4 commit 600c382

File tree

3 files changed

+79
-27
lines changed

3 files changed

+79
-27
lines changed

browser/dom/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export * from "./click.ts";
66
export * from "./statusBar.ts";
77
export * from "./caret.ts";
88
export * from "./dom.ts";
9-
export * from "./openInTheSameTab.ts";
9+
export * from "./open.ts";
1010
export * from "./cache.ts";

browser/dom/open.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/// <reference no-default-lib="true"/>
2+
/// <reference lib="esnext"/>
3+
/// <reference lib="dom" />
4+
5+
import { encodeTitleURI } from "../../title.ts";
6+
import type { Scrapbox } from "../../deps/scrapbox.ts";
7+
declare const scrapbox: Scrapbox;
8+
9+
export interface OpenOptions {
10+
/** line id */
11+
id?: string;
12+
13+
/** ページに追記するテキスト */
14+
body?: string;
15+
16+
/** 新しいタブで開くかどうか
17+
*
18+
* @default 同じタブで開く
19+
*/
20+
newTab?: boolean;
21+
22+
/** 同じタブでページを開く場合、ページを再読込するかどうか
23+
*
24+
* @default 同じprojectの場合は再読み込みせず、違うprojectの場合は再読込する
25+
*/
26+
reload?: boolean;
27+
}
28+
29+
/** ページを開く
30+
*
31+
* @param project 開くページのproject名
32+
* @param title 開くページのタイトル
33+
* @param options
34+
*/
35+
export const open = (
36+
project: string,
37+
title: string,
38+
options?: OpenOptions,
39+
) => {
40+
const url = new URL(`/${project}/${encodeTitleURI(title)}`, location.href);
41+
if (options?.body) url.search = `?body=${encodeURIComponent(options.body)}`;
42+
if (options?.id) url.hash = `#${options.id}`;
43+
44+
if (
45+
options?.newTab !== false &&
46+
(options?.newTab === true || project !== scrapbox.Project.name)
47+
) {
48+
window.open(url);
49+
return;
50+
}
51+
if (
52+
options?.reload !== false &&
53+
(options?.reload === true || project !== scrapbox.Project.name)
54+
) {
55+
window.open(url, "_self");
56+
return;
57+
}
58+
59+
const a = document.createElement("a");
60+
a.href = url.toString();
61+
document.body.append(a);
62+
a.click();
63+
a.remove();
64+
};
65+
66+
/** 同じタブでページを開く
67+
*
68+
* このとき、ページは再読み込みされない
69+
*
70+
* @param project 開くページのproject名
71+
* @param title 開くページのタイトル
72+
* @param [body] ページに追記するテキスト
73+
*/
74+
export const openInTheSameTab = (
75+
project: string,
76+
title: string,
77+
body?: string,
78+
) => open(project, title, { newTab: false, reload: false, body });

browser/dom/openInTheSameTab.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)