kirby-types 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,280 @@
1
+ /**
2
+ * Type definitions for Kirby Writer (ProseMirror-based rich text editor).
3
+ *
4
+ * These types document the context objects passed to Writer extension methods
5
+ * (`commands`, `keys`, `plugins`, `inputRules`, `pasteRules`) by Kirby's
6
+ * Writer component.
7
+ *
8
+ * @see https://github.com/getkirby/kirby/tree/main/panel/src/components/Forms/Writer
9
+ */
10
+
11
+ import type { InputRule } from "prosemirror-inputrules";
12
+ import type { MarkType, NodeType, Schema } from "prosemirror-model";
13
+ import type { Command, EditorState, Plugin } from "prosemirror-state";
14
+
15
+ /**
16
+ * Kirby Writer utility functions.
17
+ *
18
+ * A collection of ProseMirror commands and custom helpers for working
19
+ * with marks, nodes, and editor state. These utilities are passed to
20
+ * extension methods via the context object.
21
+ *
22
+ * @see https://github.com/getkirby/kirby/tree/main/panel/src/components/Forms/Writer/Utils/index.js
23
+ */
24
+ export interface WriterUtils {
25
+ // ProseMirror commands
26
+ /** Chains multiple commands, executing until one returns true */
27
+ chainCommands: typeof import("prosemirror-commands").chainCommands;
28
+ /** Exits a code block at the cursor position */
29
+ exitCode: typeof import("prosemirror-commands").exitCode;
30
+ /** Lifts content out of its wrapping node */
31
+ lift: typeof import("prosemirror-commands").lift;
32
+ /** Sets the block type at the cursor position */
33
+ setBlockType: typeof import("prosemirror-commands").setBlockType;
34
+ /** Toggles a mark on the current selection */
35
+ toggleMark: typeof import("prosemirror-commands").toggleMark;
36
+ /** Wraps the selection in a node type */
37
+ wrapIn: typeof import("prosemirror-commands").wrapIn;
38
+
39
+ // ProseMirror input rules
40
+ /** Creates an input rule that wraps matching text in a node */
41
+ wrappingInputRule: typeof import("prosemirror-inputrules").wrappingInputRule;
42
+ /** Creates an input rule that changes the textblock type */
43
+ textblockTypeInputRule: typeof import("prosemirror-inputrules").textblockTypeInputRule;
44
+
45
+ // ProseMirror schema list
46
+ /** Adds list nodes to a schema */
47
+ addListNodes: typeof import("prosemirror-schema-list").addListNodes;
48
+ /** Wraps selection in a list */
49
+ wrapInList: typeof import("prosemirror-schema-list").wrapInList;
50
+ /** Splits a list item at the cursor */
51
+ splitListItem: typeof import("prosemirror-schema-list").splitListItem;
52
+ /** Lifts a list item out of its parent list */
53
+ liftListItem: typeof import("prosemirror-schema-list").liftListItem;
54
+ /** Sinks a list item into a nested list */
55
+ sinkListItem: typeof import("prosemirror-schema-list").sinkListItem;
56
+
57
+ // Custom utilities
58
+
59
+ /**
60
+ * Gets the attributes of the active mark of the given type.
61
+ * @param state - The current editor state
62
+ * @param type - The mark type to get attributes for
63
+ * @returns The mark attributes or an empty object
64
+ */
65
+ getMarkAttrs: (state: EditorState, type: MarkType) => Record<string, unknown>;
66
+
67
+ /**
68
+ * Gets the attributes of the active node of the given type.
69
+ * @param state - The current editor state
70
+ * @param type - The node type to get attributes for
71
+ * @returns The node attributes or an empty object
72
+ */
73
+ getNodeAttrs: (state: EditorState, type: NodeType) => Record<string, unknown>;
74
+
75
+ /**
76
+ * Creates a command that inserts a node of the given type.
77
+ * @param type - The node type to insert
78
+ * @param attrs - Optional attributes for the node
79
+ * @returns A ProseMirror command
80
+ */
81
+ insertNode: (type: NodeType, attrs?: Record<string, unknown>) => Command;
82
+
83
+ /**
84
+ * Creates an input rule that applies a mark when the pattern matches.
85
+ * @param regexp - The pattern to match
86
+ * @param type - The mark type to apply
87
+ * @param getAttrs - Optional function to compute mark attributes from the match
88
+ * @returns An input rule
89
+ */
90
+ markInputRule: (
91
+ regexp: RegExp,
92
+ type: MarkType,
93
+ getAttrs?: (match: RegExpMatchArray) => Record<string, unknown>,
94
+ ) => InputRule;
95
+
96
+ /**
97
+ * Checks if a mark of the given type is active in the current selection.
98
+ * @param state - The current editor state
99
+ * @param type - The mark type to check
100
+ * @returns True if the mark is active
101
+ */
102
+ markIsActive: (state: EditorState, type: MarkType) => boolean;
103
+
104
+ /**
105
+ * Creates a paste rule that applies a mark to pasted text matching the pattern.
106
+ * @param regexp - The pattern to match
107
+ * @param type - The mark type to apply
108
+ * @param getAttrs - Optional function to compute mark attributes from the match
109
+ * @returns A ProseMirror plugin
110
+ */
111
+ markPasteRule: (
112
+ regexp: RegExp,
113
+ type: MarkType,
114
+ getAttrs?: (match: string) => Record<string, unknown>,
115
+ ) => Plugin;
116
+
117
+ /**
118
+ * Clamps a value between a minimum and maximum.
119
+ * @param value - The value to clamp
120
+ * @param min - The minimum allowed value
121
+ * @param max - The maximum allowed value
122
+ * @returns The clamped value
123
+ */
124
+ minMax: (value: number, min: number, max: number) => number;
125
+
126
+ /**
127
+ * Creates an input rule that inserts a node when the pattern matches.
128
+ * @param regexp - The pattern to match
129
+ * @param type - The node type to insert
130
+ * @param getAttrs - Optional function to compute node attributes from the match
131
+ * @returns An input rule
132
+ */
133
+ nodeInputRule: (
134
+ regexp: RegExp,
135
+ type: NodeType,
136
+ getAttrs?: (match: RegExpMatchArray) => Record<string, unknown>,
137
+ ) => InputRule;
138
+
139
+ /**
140
+ * Checks if a node of the given type is active in the current selection.
141
+ * @param state - The current editor state
142
+ * @param type - The node type to check
143
+ * @param attrs - Optional attributes to match
144
+ * @returns True if the node is active
145
+ */
146
+ nodeIsActive: (
147
+ state: EditorState,
148
+ type: NodeType,
149
+ attrs?: Record<string, unknown>,
150
+ ) => boolean;
151
+
152
+ /**
153
+ * Creates a paste rule that applies a mark to pasted URLs matching the pattern.
154
+ * @param regexp - The pattern to match URLs
155
+ * @param type - The mark type to apply
156
+ * @param getAttrs - Optional function to compute mark attributes from the URL
157
+ * @returns A ProseMirror plugin
158
+ */
159
+ pasteRule: (
160
+ regexp: RegExp,
161
+ type: MarkType,
162
+ getAttrs?: (url: string) => Record<string, unknown>,
163
+ ) => Plugin;
164
+
165
+ /**
166
+ * Creates a command that removes a mark from the current selection.
167
+ * @param type - The mark type to remove
168
+ * @returns A ProseMirror command
169
+ */
170
+ removeMark: (type: MarkType) => Command;
171
+
172
+ /**
173
+ * Creates a command that toggles between two block types.
174
+ * @param type - The block type to toggle to
175
+ * @param toggleType - The block type to toggle back to (usually paragraph)
176
+ * @param attrs - Optional attributes for the node
177
+ * @returns A ProseMirror command
178
+ */
179
+ toggleBlockType: (
180
+ type: NodeType,
181
+ toggleType: NodeType,
182
+ attrs?: Record<string, unknown>,
183
+ ) => Command;
184
+
185
+ /**
186
+ * Creates a command that toggles a list.
187
+ * @param type - The list type to toggle
188
+ * @param itemType - The list item type
189
+ * @returns A ProseMirror command
190
+ */
191
+ toggleList: (type: NodeType, itemType: NodeType) => Command;
192
+
193
+ /**
194
+ * Creates a command that toggles wrapping the selection in a node.
195
+ * @param type - The node type to wrap in
196
+ * @returns A ProseMirror command
197
+ */
198
+ toggleWrap: (type: NodeType) => Command;
199
+
200
+ /**
201
+ * Creates a command that updates the attributes of the active mark.
202
+ * @param type - The mark type to update
203
+ * @param attrs - The new attributes
204
+ * @returns A ProseMirror command
205
+ */
206
+ updateMark: (type: MarkType, attrs: Record<string, unknown>) => Command;
207
+ }
208
+
209
+ /**
210
+ * Context passed to mark extension methods.
211
+ *
212
+ * The context provides access to the ProseMirror schema, the current mark's
213
+ * type, and utility functions for working with the editor. This is passed
214
+ * to methods like `commands`, `keys`, `inputRules`, `pasteRules`, and `plugins`.
215
+ *
216
+ * @example
217
+ * ```js
218
+ * export default class Bold extends Mark {
219
+ * commands({ type, utils }) {
220
+ * return () => utils.toggleMark(type);
221
+ * }
222
+ *
223
+ * inputRules({ type, utils }) {
224
+ * return [
225
+ * utils.markInputRule(/\*\*([^*]+)\*\*$/, type)
226
+ * ];
227
+ * }
228
+ * }
229
+ * ```
230
+ *
231
+ * @see https://github.com/getkirby/kirby/tree/main/panel/src/components/Forms/Writer/Extensions.js:124-134
232
+ */
233
+ export interface WriterMarkContext {
234
+ /** The ProseMirror schema with all registered nodes and marks */
235
+ schema: Schema;
236
+ /** The MarkType instance for this mark extension */
237
+ type: MarkType;
238
+ /** Writer utility functions */
239
+ utils: WriterUtils;
240
+ }
241
+
242
+ /**
243
+ * Context passed to node extension methods.
244
+ *
245
+ * Similar to WriterMarkContext but provides a NodeType instead of MarkType.
246
+ *
247
+ * @example
248
+ * ```js
249
+ * export default class Heading extends Node {
250
+ * commands({ type, schema, utils }) {
251
+ * return (attrs) => utils.toggleBlockType(type, schema.nodes.paragraph, attrs);
252
+ * }
253
+ * }
254
+ * ```
255
+ *
256
+ * @see https://github.com/getkirby/kirby/tree/main/panel/src/components/Forms/Writer/Extensions.js:124-134
257
+ */
258
+ export interface WriterNodeContext {
259
+ /** The ProseMirror schema with all registered nodes and marks */
260
+ schema: Schema;
261
+ /** The NodeType instance for this node extension */
262
+ type: NodeType;
263
+ /** Writer utility functions */
264
+ utils: WriterUtils;
265
+ }
266
+
267
+ /**
268
+ * Context passed to generic extension methods (non-mark, non-node).
269
+ *
270
+ * Generic extensions don't have a specific type, so only schema and utils
271
+ * are provided.
272
+ *
273
+ * @see https://github.com/getkirby/kirby/tree/main/panel/src/components/Forms/Writer/Extensions.js:112-121
274
+ */
275
+ export interface WriterExtensionContext {
276
+ /** The ProseMirror schema with all registered nodes and marks */
277
+ schema: Schema;
278
+ /** Writer utility functions */
279
+ utils: WriterUtils;
280
+ }
package/src/query.d.ts CHANGED
@@ -172,20 +172,20 @@ export type KirbyQuery<CustomModel extends string = never> =
172
172
  * @template M - Optional custom model names to include in validation
173
173
  */
174
174
  export type ParseKirbyQuery<T extends string, M extends string = never> =
175
- // Case 1: Simple model name (e.g., "site", "page")
175
+ // Case 1: Simple model name (e.g., `site`, `page`)
176
176
  T extends KirbyQueryModel<M>
177
177
  ? { model: T; chain: [] }
178
- : // Case 2: Dot notation (e.g., "page.children.listed")
178
+ : // Case 2: Dot notation (e.g., `page.children.listed`)
179
179
  T extends `${infer Model}.${infer Chain}`
180
180
  ? Model extends KirbyQueryModel<M>
181
181
  ? { model: Model; chain: ParseQueryChain<Chain> }
182
182
  : never
183
- : // Case 3: Method call only (e.g., 'site("home")')
183
+ : // Case 3: Method call only (e.g., `site("home")`)
184
184
  T extends `${infer Model}(${infer Params})`
185
185
  ? Model extends KirbyQueryModel<M>
186
186
  ? { model: Model; chain: [ParseQuerySegment<T>] }
187
187
  : never
188
- : // Case 4: Method call followed by chain (e.g., 'site("home").children')
188
+ : // Case 4: Method call followed by chain (e.g., `site("home").children`)
189
189
  T extends `${infer Model}(${infer Params})${infer Rest}`
190
190
  ? Model extends KirbyQueryModel<M>
191
191
  ? Rest extends `.${infer Chain}`