From 600c382a2b0c0e4ca96350dcb7adf14af6b93156 Mon Sep 17 00:00:00 2001
From: takker99 <37929109+takker99@users.noreply.github.com>
Date: Fri, 22 Apr 2022 06:53:50 +0900
Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E3=81=84=E3=82=8D=E3=82=93?=
=?UTF-8?q?=E3=81=AA=E6=96=B9=E6=B3=95=E3=81=A7=E3=83=9A=E3=83=BC=E3=82=B8?=
=?UTF-8?q?=E3=82=92=E9=96=8B=E3=81=91=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?=
=?UTF-8?q?=E3=81=97=E3=81=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
browser/dom/mod.ts | 2 +-
browser/dom/open.ts | 78 +++++++++++++++++++++++++++++++++
browser/dom/openInTheSameTab.ts | 26 -----------
3 files changed, 79 insertions(+), 27 deletions(-)
create mode 100644 browser/dom/open.ts
delete mode 100644 browser/dom/openInTheSameTab.ts
diff --git a/browser/dom/mod.ts b/browser/dom/mod.ts
index 333021b..03dc042 100644
--- a/browser/dom/mod.ts
+++ b/browser/dom/mod.ts
@@ -6,5 +6,5 @@ export * from "./click.ts";
export * from "./statusBar.ts";
export * from "./caret.ts";
export * from "./dom.ts";
-export * from "./openInTheSameTab.ts";
+export * from "./open.ts";
export * from "./cache.ts";
diff --git a/browser/dom/open.ts b/browser/dom/open.ts
new file mode 100644
index 0000000..3657bb4
--- /dev/null
+++ b/browser/dom/open.ts
@@ -0,0 +1,78 @@
+///
+///
+///
+
+import { encodeTitleURI } from "../../title.ts";
+import type { Scrapbox } from "../../deps/scrapbox.ts";
+declare const scrapbox: Scrapbox;
+
+export interface OpenOptions {
+ /** line id */
+ id?: string;
+
+ /** ページに追記するテキスト */
+ body?: string;
+
+ /** 新しいタブで開くかどうか
+ *
+ * @default 同じタブで開く
+ */
+ newTab?: boolean;
+
+ /** 同じタブでページを開く場合、ページを再読込するかどうか
+ *
+ * @default 同じprojectの場合は再読み込みせず、違うprojectの場合は再読込する
+ */
+ reload?: boolean;
+}
+
+/** ページを開く
+ *
+ * @param project 開くページのproject名
+ * @param title 開くページのタイトル
+ * @param options
+ */
+export const open = (
+ project: string,
+ title: string,
+ options?: OpenOptions,
+) => {
+ const url = new URL(`/${project}/${encodeTitleURI(title)}`, location.href);
+ if (options?.body) url.search = `?body=${encodeURIComponent(options.body)}`;
+ if (options?.id) url.hash = `#${options.id}`;
+
+ if (
+ options?.newTab !== false &&
+ (options?.newTab === true || project !== scrapbox.Project.name)
+ ) {
+ window.open(url);
+ return;
+ }
+ if (
+ options?.reload !== false &&
+ (options?.reload === true || project !== scrapbox.Project.name)
+ ) {
+ window.open(url, "_self");
+ return;
+ }
+
+ const a = document.createElement("a");
+ a.href = url.toString();
+ document.body.append(a);
+ a.click();
+ a.remove();
+};
+
+/** 同じタブでページを開く
+ *
+ * このとき、ページは再読み込みされない
+ *
+ * @param project 開くページのproject名
+ * @param title 開くページのタイトル
+ * @param [body] ページに追記するテキスト
+ */
+export const openInTheSameTab = (
+ project: string,
+ title: string,
+ body?: string,
+) => open(project, title, { newTab: false, reload: false, body });
diff --git a/browser/dom/openInTheSameTab.ts b/browser/dom/openInTheSameTab.ts
deleted file mode 100644
index aafc163..0000000
--- a/browser/dom/openInTheSameTab.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-///
-///
-///
-import { encodeTitleURI } from "../../title.ts";
-
-/** 同じタブでページを開く
- *
- * このとき、ページは再読み込みされない
- *
- * @param project 開くページのproject名
- * @param title 開くページのタイトル
- * @param [body] ページに追記するテキスト
- */
-export function openInTheSameTab(
- project: string,
- title: string,
- body?: string,
-) {
- const a = document.createElement("a");
- a.href = `/${project}/${encodeTitleURI(title)}${
- typeof body !== "string" ? "" : `?body=${encodeURIComponent(body)}`
- }`;
- document.body.append(a);
- a.click();
- a.remove();
-}