@@ -5,6 +5,7 @@ import { push, type PushError, type PushOptions } from "./push.ts";
5
5
import { suggestUnDupTitle } from "./suggestUnDupTitle.ts" ;
6
6
import type { Result } from "option-t/plain_result" ;
7
7
import type { Socket } from "socket.io-client" ;
8
+ import { pinNumber } from "./pin.ts" ;
8
9
9
10
export interface PatchMetadata extends Page {
10
11
/** Number of retry attempts for page modification
@@ -25,17 +26,27 @@ export interface PatchMetadata extends Page {
25
26
* @param lines - Current page lines
26
27
* @param metadata - Current page metadata
27
28
* @returns one of the following or a {@linkcode Promise} resolving to one:
28
- * - `(string | { text: string; })[]`: New page content
29
+ * - `NewPageContent["lines"]`: New page lines
30
+ * - `NewPageContent`: New page content with optional pinning operation
29
31
* - `[]`: Delete the page
30
32
* - `undefined`: Abort modification
31
33
*/
32
34
export type MakePatchFn = (
33
35
lines : BaseLine [ ] ,
34
36
metadata : PatchMetadata ,
35
37
) =>
36
- | ( string | { text : string } ) [ ]
38
+ | NewPageContent [ "lines" ]
39
+ | NewPageContent
37
40
| undefined
38
- | Promise < ( string | { text : string } ) [ ] | undefined > ;
41
+ | Promise < NewPageContent [ "lines" ] | NewPageContent | undefined > ;
42
+
43
+ export interface NewPageContent {
44
+ /** New page lines */
45
+ lines : ( string | { text : string } ) [ ] ;
46
+
47
+ /** Whether to pin the page */
48
+ pin ?: boolean ;
49
+ }
39
50
40
51
export type PatchOptions = PushOptions ;
41
52
@@ -48,6 +59,8 @@ export type PatchOptions = PushOptions;
48
59
* 4. Handles errors (e.g., duplicate titles)
49
60
* 5. Retries on conflicts
50
61
*
62
+ * This function also can pin/unpin pages by setting the `pin` property in the return of `update`.
63
+ *
51
64
* @param project Project ID containing the target page
52
65
* @param title Title of the page to modify
53
66
* @param update Function to generate new content
@@ -76,10 +89,23 @@ export const patch = (
76
89
} ) as Change [ ] | [ DeletePageChange ] ;
77
90
}
78
91
const pending = update ( page . lines , { ...page , attempts } ) ;
79
- const newLines = pending instanceof Promise ? await pending : pending ;
80
- if ( newLines === undefined ) return [ ] ;
92
+ const newContent = pending instanceof Promise ? await pending : pending ;
93
+ if ( newContent === undefined ) return [ ] ;
94
+ const [ newLines , pin ] = Array . isArray ( newContent )
95
+ ? ( [ newContent , undefined ] as const )
96
+ : ( [ newContent . lines , newContent . pin ] as const ) ;
97
+
81
98
if ( newLines . length === 0 ) return [ { deleted : true } ] ;
82
- return [ ...makeChanges ( page , newLines , page . userId ) ] ;
99
+
100
+ const changes = page . lines === newLines
101
+ ? [ ]
102
+ : [ ...makeChanges ( page , newLines , page . userId ) ] ;
103
+ if (
104
+ pin !== undefined && ( ( pin && page . pin === 0 ) || ( ! pin && page . pin > 0 ) )
105
+ ) {
106
+ changes . push ( { pin : pin ? pinNumber ( ) : 0 } ) ;
107
+ }
108
+ return changes ;
83
109
} ,
84
110
options ,
85
111
) ;
0 commit comments