Skip to content
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
2 changes: 1 addition & 1 deletion browser/dom/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
78 changes: 78 additions & 0 deletions browser/dom/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// <reference no-default-lib="true"/>
/// <reference lib="esnext"/>
/// <reference lib="dom" />

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 });
26 changes: 0 additions & 26 deletions browser/dom/openInTheSameTab.ts

This file was deleted.