Skip to content

Commit

Permalink
feat(websocket): Add /websocket submodule, which provides types abo…
Browse files Browse the repository at this point in the history
…ut Websocket events used in Cosense
  • Loading branch information
takker99 committed Feb 7, 2025
1 parent 790ef72 commit b888ac6
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 67 deletions.
89 changes: 35 additions & 54 deletions change.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import type { BasePage, LineId, StringLc } from "./base.ts";
import type { LineId, StringLc } from "./base.ts";
import type {
ChangeLine,
CharsCountChange,
DeleteChange,
DescriptionsChange,
FilesChange,
HelpFeelsChange,
ImageChange,
InfoboxDefinitionChange,
InsertChange,
LinesCountChange,
PinChange,
} from "./websocket/change.ts";

/** ページの変更内容 */
export type Change =
| InsertChange
| UpdateChange
| DeleteChange
| LinksChange
| ProjectLinksChange
| IconsChange
| DescriptionsChange
| ImageChange
| FilesChange
| HelpFeelsChange
| InfoboxDefinitionChange
| TitleChange
| LinesCountChange
| CharsCountChange
| PinChange;

/** 行を新規作成する変更 */
export interface InsertChange {
/** このIDが示す行の上に挿入する
*
* 末尾に挿入するときは`"_end"`を指定する
*/
_insert: LineId;

/** 挿入する行のデータ */
lines: NewLine;
}

export interface NewLine {
/** 新しく挿入する行のID */
id: LineId;

/** 行のテキスト */
text: string;
}

/** 既存の行を書き換える変更 */
export interface UpdateChange {
/** 書き換える行のID */
Expand All @@ -40,23 +40,6 @@ export interface UpdateChange {
lines: ChangeLine;
}

export interface ChangeLine {
/**変更前の文字列*/
origText: string;

/**変更後の文字列*/
text: string;
}

/** 既存の行を削除する変更 */
export interface DeleteChange {
/** 削除する行のID */
_delete: LineId;

/** 常に `-1` */
lines: -1;
}

/** ページ中のリンクが変更されると発生する */
export interface LinksChange {
/** 新しいリンク */
Expand All @@ -66,19 +49,22 @@ export interface LinksChange {
linksLc: StringLc[];
}

/** ページのサムネイル本文が変更されると発生する */
export interface DescriptionsChange {
/** 新しいサムネイル本文 */
descriptions: string[];
/** ページ中のproject linksが変更されると発生する */
export interface ProjectLinksChange {
/** 新しいリンク */
projectLinks: string[];

/** 新しいリンク */
projectLinksLc: StringLc[];
}

/** ページのサムネイルが変更されると発生する */
export interface ImageChange {
/** 新しいサムネイルのURL
*
* サムネイルがなくなったときは`null`になる
*/
image: string | null;
/** ページ中のiconsが変更されると発生する */
export interface IconsChange {
/** 新しいicons */
icons: string[];

/** 新しいicons */
iconsLc: StringLc[];
}

/** ページのタイトルが変更されると発生する */
Expand All @@ -89,8 +75,3 @@ export interface TitleChange {
/** 新しいタイトル */
titleLc: StringLc;
}

/** ページのピンの状態が変更されると発生する */
export interface PinChange {
pin: BasePage["pin"];
}
5 changes: 3 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"fix": "deno fmt && deno lint --fix && deno test --allow-read --doc --parallel --shuffle && deno publish --dry-run --allow-dirty",
"check": "deno fmt --check && deno lint && deno test --allow-read --doc --parallel --shuffle && deno publish --dry-run",
"coverage": "deno test --allow-read=./ --parallel --shuffle --coverage && deno coverage --html",
"doc": "deno doc --html rest.ts userscript.ts"
"doc": "deno doc --html rest.ts userscript.ts websocket.ts"
},
"imports": {
"@std/testing/types": "jsr:@std/testing@0/types"
},
"exports": {
"./rest": "./rest.ts",
"./userscript": "./userscript.ts"
"./userscript": "./userscript.ts",
"./websocket": "./websocket.ts"
},
"compilerOptions": {
"lib": [
Expand Down
19 changes: 8 additions & 11 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./websocket/change.ts";
export * from "./websocket/event.ts";
136 changes: 136 additions & 0 deletions websocket/change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import type { BasePage, LineId } from "../base.ts";

/** Changes to push to the cosense server */
export type ChangeToPush =
| InsertChange
| UpdateChange
| DeleteChange
| LinksChange
| ProjectLinksChange
| IconsChange
| DescriptionsChange
| ImageChange
| FilesChange
| HelpFeelsChange
| InfoboxDefinitionChange
| TitleChange
| LinesCountChange
| CharsCountChange
| PinChange;

/** 行を新規作成する変更 */
export interface InsertChange {
/** このIDが示す行の上に挿入する
*
* 末尾に挿入するときは`"_end"`を指定する
*/
_insert: LineId;

/** 挿入する行のデータ */
lines: NewLine;
}
export interface NewLine {
/** 新しく挿入する行のID */
id: LineId;

/** 行のテキスト */
text: string;
}

export interface UpdateChange {
_update: string;
lines: Pick<ChangeLine, "text">;
noTimestampUpdate?: unknown;
}

export interface ChangeLine {
/**変更前の文字列*/
origText: string;

/**変更後の文字列*/
text: string;
}

/** 既存の行を削除する変更 */
export interface DeleteChange {
/** 削除する行のID */
_delete: LineId;

/** 常に `-1` */
lines: -1;
}

export interface LinksChange {
links: string[];
}

export interface ProjectLinksChange {
projectLinks: string[];
}

export interface IconsChange {
icons: string[];
}

/** ページのサムネイル本文が変更されると発生する */
export interface DescriptionsChange {
/** 新しいサムネイル本文 */
descriptions: string[];
}

/** ページのサムネイルが変更されると発生する */
export interface ImageChange {
/** 新しいサムネイルのURL
*
* サムネイルがなくなったときは`null`になる
*/
image: string | null;
}

export interface TitleChange {
title: string;
}

export interface FilesChange {
/** Array of file IDs
*
* These IDs reference files that have been uploaded to the page.
* Files can include images, documents, or other attachments.
*/
files: string[];
}
export interface HelpFeelsChange {
/** Array of Helpfeel entries without the leading "? " prefix
*
* Helpfeel is a Scrapbox notation for creating help/documentation entries.
* Example: "? How to use" becomes "How to use" in this array.
* These entries are used to build the page's help documentation.
*/
helpfeels: string[];
}

export interface InfoboxDefinitionChange {
/** Array of trimmed lines from infobox tables
*
* Contains lines from tables marked with either `table:infobox` or `table:cosense`
*/
infoboxDefinition: string[];
}

export interface LinesCountChange {
linesCount: number;
}

export interface CharsCountChange {
charsCount: number;
}

/** ページのピンの状態が変更されると発生する */
export interface PinChange {
pin: BasePage["pin"];
}

export interface DeletePageChange {
deleted: true;
merged?: true;
}
Loading

0 comments on commit b888ac6

Please sign in to comment.