Skip to content

Commit 4bf7cf1

Browse files
committed
♻️ JSDocがつくように型実装を変えた
1 parent 8d3807b commit 4bf7cf1

File tree

1 file changed

+86
-64
lines changed

1 file changed

+86
-64
lines changed

scrapbox.ts

Lines changed: 86 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,122 @@
55

66
import type { Line } from "./blocks.ts";
77
import type { BasePage, StringLc } from "./base.ts";
8-
import type { PartialLayout } from "./layout.ts";
8+
import type { Layout, PartialLayout } from "./layout.ts";
99
import type { AddMenuInit, Item, PageMenu } from "./pageMenu.ts";
1010
import type { EventEmitter } from "./deps/events.ts";
1111

1212
/** Type definition of `window.scrapbox` */
1313
export type Scrapbox =
1414
& EventEmitter
1515
& {
16-
PageMenu: {
17-
/** get a particular Page Menu
18-
*
19-
* @param menuName Page Menu name to get. If it is set to "default" or undefined, return the default page Menu
20-
*/
21-
(menuName?: string): PageMenu;
22-
/** Add a new Page Menu button
23-
*
24-
* @param init information used for a Page Menu button
25-
*/
26-
addMenu: (init: AddMenuInit) => void;
27-
/** Add a menu item to the default Page Menu button
28-
*
29-
* @param item information used for a menu item
30-
*/
31-
addItem: (item: Item) => void;
32-
/** Add a separator to the default Page Menu button */
33-
addSeparater: () => void;
34-
/** remove all custom items from the default Page Menu button */
35-
removeAllItems: () => void;
36-
};
37-
PopupMenu: {
38-
/** Add a popup button
39-
*
40-
* @param button button settings
41-
*/
42-
addButton: (button: {
43-
/** ボタンのタイトル
44-
*
45-
* 関数を設定して、選択範囲が変わるたびにタイトルを変更させる事もできる
46-
*/
47-
title: string | ((text: string) => (string | undefined));
48-
/** ボタンをクリックしたときに実行する処理
49-
*
50-
* @return ここで返した文字列で選択範囲を置換し、popupを閉じる。`undefined`を返した場合は何もしない。popupも閉じない
51-
*/
52-
onClick: (text: string) => (string | undefined);
53-
}) => void;
54-
};
16+
PageMenu: ExposedPageMenu;
17+
PopupMenu: PopupMenu;
5518
TimeStamp: TimeStamp;
56-
Project: {
57-
/** get the current project name */
58-
get name(): string;
59-
/** get the dictionary used for comupletion */
60-
get pages(): Candidate[];
61-
};
19+
Project: Project;
6220
}
6321
& (
6422
{
6523
/** the current page layout */
6624
Layout: "page";
67-
Page: {
68-
/** get the current page lines data */
69-
get lines(): Line[];
70-
/** get the current page title */
71-
get title(): string;
72-
/** get the current page id */
73-
get id(): string;
74-
};
25+
Page: Page<"page">;
7526
} | {
7627
/** the current page layout */
7728
Layout: PartialLayout;
78-
Page: {
79-
get lines(): null;
80-
get title(): null;
81-
get id(): null;
82-
};
29+
Page: Page<PartialLayout>;
8330
}
8431
);
8532

86-
/** 入力補完に使われる辞書 */
87-
export interface Candidate extends Pick<BasePage, "id" | "title" | "updated"> {
88-
/** true when the page has contents */ exists: boolean;
89-
/** whether the page contains any image */ hasIcon?: boolean;
90-
/** lower case style of the page title */ titleLc: StringLc;
33+
/** `window.scrapbox`に露出している`PageMenu`の型 */
34+
export interface ExposedPageMenu {
35+
/** get a particular Page Menu
36+
*
37+
* @param menuName Page Menu name to get. If it is set to "default" or undefined, return the default page Menu
38+
*/
39+
(menuName?: string): PageMenu;
40+
41+
/** Add a new Page Menu button
42+
*
43+
* @param init information used for a Page Menu button
44+
*/
45+
addMenu: (init: AddMenuInit) => void;
46+
47+
/** Add a menu item to the default Page Menu button
48+
*
49+
* @param item information used for a menu item
50+
*/
51+
addItem: (item: Item) => void;
52+
53+
/** Add a separator to the default Page Menu button */
54+
addSeparater: () => void;
55+
56+
/** remove all custom items from the default Page Menu button */
57+
removeAllItems: () => void;
58+
}
59+
60+
/** `window.scrapbox`に露出している`PopupMenu`の型 */
61+
export interface PopupMenu {
62+
/** Add a popup button
63+
*
64+
* @param button button settings
65+
*/
66+
addButton: (button: {
67+
/** ボタンのタイトル
68+
*
69+
* 関数を設定して、選択範囲が変わるたびにタイトルを変更させる事もできる
70+
*/
71+
title: string | ((text: string) => (string | undefined));
72+
/** ボタンをクリックしたときに実行する処理
73+
*
74+
* @return ここで返した文字列で選択範囲を置換し、popupを閉じる。`undefined`を返した場合は何もしない。popupも閉じない
75+
*/
76+
onClick: (text: string) => (string | undefined);
77+
}) => void;
9178
}
9279

80+
/** `window.scrapbox`に露出している`TimeStamp`の型 */
9381
export interface TimeStamp {
9482
/** Add a timestamp format to Scrapbox
9583
*
9684
* @param format a format of timestamp. this follow the moment.js format. You can set a function which returns any string
9785
*/
9886
addFormat: (format: string | (() => string)) => void;
87+
9988
/** Remove all timestamp formats from Scrapbox
10089
*
10190
* These include default formats
10291
*/
10392
removeAllFormat: () => void;
10493
}
94+
95+
/** 入力補完に使われる辞書 */
96+
export interface Candidate extends Pick<BasePage, "id" | "title" | "updated"> {
97+
/** true when the page has contents */
98+
exists: boolean;
99+
100+
/** whether the page contains any image */
101+
hasIcon?: boolean;
102+
103+
/** lower case style of the page title */
104+
titleLc: StringLc;
105+
}
106+
107+
/** `window.scrapbox`に露出している`Project`の型 */
108+
export interface Project {
109+
/** get the current project name */
110+
get name(): string;
111+
112+
/** get the dictionary used for comupletion */
113+
get pages(): Candidate[];
114+
}
115+
116+
/** `window.scrapbox`に露出している`Page`の型 */
117+
export interface Page<T extends Layout> {
118+
/** get the current page lines data */
119+
get lines(): T extends "page" ? Line[] : null;
120+
121+
/** get the current page title */
122+
get title(): T extends "page" ? string : null;
123+
124+
/** get the current page id */
125+
get id(): T extends "page" ? string : null;
126+
}

0 commit comments

Comments
 (0)