Skip to content

Commit b9d9912

Browse files
authored
Merge pull request #221 from takker99:patch
feat(websocket): Allow to return `{ text: string; }` style lines from `update`
2 parents 9806ecd + 71e36fd commit b9d9912

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

websocket/makeChanges.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import type { Page } from "@cosense/types/rest";
33
import type { Change } from "./change.ts";
44
import { findMetadata, getHelpfeels } from "./findMetadata.ts";
55
import { isSameArray } from "./isSameArray.ts";
6+
import { isString } from "@core/unknownutil/is/string";
67

78
export function* makeChanges(
89
before: Page,
9-
after: string[],
10+
after: (string | { text: string })[],
1011
userId: string,
1112
): Generator<Change, void, unknown> {
1213
// Prevent newline characters from being included in the text
1314
// This ensures consistent line handling across different platforms
14-
const after_ = after.flatMap((text) => text.split("\n"));
15+
const after_ = after.flatMap((text) =>
16+
(isString(text) ? text : text.text).split("\n")
17+
);
1518

1619
// First, yield changes in the main content
1720
// Content changes must be processed before metadata to maintain consistency

websocket/patch.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { suggestUnDupTitle } from "./suggestUnDupTitle.ts";
66
import type { Result } from "option-t/plain_result";
77
import type { Socket } from "socket.io-client";
88

9-
export type PatchOptions = PushOptions;
10-
119
export interface PatchMetadata extends Page {
1210
/** Number of retry attempts for page modification
1311
*
@@ -17,6 +15,30 @@ export interface PatchMetadata extends Page {
1715
attempts: number;
1816
}
1917

18+
/**
19+
* Function used in {@linkcode patch} to generate a patch from the current page state
20+
*
21+
* This function is used to generate a patch from the current page state.
22+
* It receives the current page lines and metadata and returns the new page content.
23+
* The function can be synchronous or asynchronous.
24+
*
25+
* @param lines - Current page lines
26+
* @param metadata - Current page metadata
27+
* @returns one of the following or a {@linkcode Promise} resolving to one:
28+
* - `(string | { text: string; })[]`: New page content
29+
* - `[]`: Delete the page
30+
* - `undefined`: Abort modification
31+
*/
32+
export type MakePatchFn = (
33+
lines: BaseLine[],
34+
metadata: PatchMetadata,
35+
) =>
36+
| (string | { text: string })[]
37+
| undefined
38+
| Promise<(string | { text: string })[] | undefined>;
39+
40+
export type PatchOptions = PushOptions;
41+
2042
/** Modify an entire Scrapbox page by computing and sending only the differences
2143
*
2244
* This function handles the entire page modification process:
@@ -26,29 +48,20 @@ export interface PatchMetadata extends Page {
2648
* 4. Handles errors (e.g., duplicate titles)
2749
* 5. Retries on conflicts
2850
*
29-
* @param project - Project ID containing the target page
30-
* @param title - Title of the page to modify
31-
* @param update - Function to generate new content:
32-
* - Input: Current page lines and metadata
33-
* - Return values:
34-
* - `string[]`: New page content
35-
* - `undefined`: Abort modification
36-
* - `[]`: Delete the page
37-
* Can be async (returns `Promise`)
38-
* @param options - Optional WebSocket configuration
51+
* @param project Project ID containing the target page
52+
* @param title Title of the page to modify
53+
* @param update Function to generate new content
54+
* @param options Optional WebSocket configuration
3955
*
4056
* Special cases:
41-
* - If update returns undefined: Operation is cancelled
42-
* - If update returns []: Page is deleted
57+
* - If `update` returns `undefined`: Operation is cancelled
58+
* - If `update` returns `[]`: Page is deleted
4359
* - On duplicate title: Automatically suggests non-conflicting title
4460
*/
4561
export const patch = (
4662
project: string,
4763
title: string,
48-
update: (
49-
lines: BaseLine[],
50-
metadata: PatchMetadata,
51-
) => string[] | undefined | Promise<string[] | undefined>,
64+
update: MakePatchFn,
5265
options?: PatchOptions,
5366
): Promise<Result<string, PushError | Socket.DisconnectReason>> =>
5467
push(

0 commit comments

Comments
 (0)