kirby-types 1.0.0 → 1.1.0

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,456 @@
1
+ /**
2
+ * Library type definitions for Kirby Panel.
3
+ *
4
+ * Provides types for the `$library` utilities available on the Vue prototype.
5
+ * Includes color manipulation, date handling (dayjs), and textarea autosize.
6
+ *
7
+ * @see https://github.com/getkirby/kirby/tree/main/panel/src/libraries
8
+ * @since 4.0.0
9
+ */
10
+
11
+ // -----------------------------------------------------------------------------
12
+ // Color Types
13
+ // -----------------------------------------------------------------------------
14
+
15
+ /**
16
+ * Color format identifiers.
17
+ */
18
+ export type PanelColorFormat = "hex" | "rgb" | "hsl" | "hsv";
19
+
20
+ /**
21
+ * RGB color object.
22
+ */
23
+ export interface PanelColorRGB {
24
+ /** Red channel (0-255) */
25
+ r: number;
26
+ /** Green channel (0-255) */
27
+ g: number;
28
+ /** Blue channel (0-255) */
29
+ b: number;
30
+ /** Alpha channel (0-1) */
31
+ a?: number;
32
+ }
33
+
34
+ /**
35
+ * HSL color object.
36
+ */
37
+ export interface PanelColorHSL {
38
+ /** Hue (0-360) */
39
+ h: number;
40
+ /** Saturation (0-1) */
41
+ s: number;
42
+ /** Lightness (0-1) */
43
+ l: number;
44
+ /** Alpha channel (0-1) */
45
+ a?: number;
46
+ }
47
+
48
+ /**
49
+ * HSV color object.
50
+ */
51
+ export interface PanelColorHSV {
52
+ /** Hue (0-360) */
53
+ h: number;
54
+ /** Saturation (0-1) */
55
+ s: number;
56
+ /** Value/Brightness (0-1) */
57
+ v: number;
58
+ /** Alpha channel (0-1) */
59
+ a?: number;
60
+ }
61
+
62
+ /**
63
+ * Any color object type.
64
+ */
65
+ export type PanelColorObject = PanelColorRGB | PanelColorHSL | PanelColorHSV;
66
+
67
+ /**
68
+ * Color input (string or object).
69
+ */
70
+ export type PanelColorInput = string | PanelColorObject;
71
+
72
+ /**
73
+ * Color library for color space conversions.
74
+ *
75
+ * Provides comprehensive color manipulation including
76
+ * parsing CSS color strings and converting between
77
+ * HEX, RGB, HSL, and HSV color spaces.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const rgb = this.$library.colors.parse('hsl(180 50% 50%)');
82
+ * const hex = this.$library.colors.convert(rgb, 'hex');
83
+ * const css = this.$library.colors.toString(hex, 'rgb');
84
+ * ```
85
+ *
86
+ * @see https://github.com/getkirby/kirby/blob/main/panel/src/libraries/colors.js
87
+ * @since 4.0.0
88
+ */
89
+ export interface PanelLibraryColors {
90
+ /**
91
+ * Converts a color to another color space.
92
+ *
93
+ * @param color - Color to convert (hex string or color object)
94
+ * @param format - Target format
95
+ * @returns Converted color
96
+ * @throws Error if invalid color or conversion
97
+ */
98
+ convert: {
99
+ (color: string, format: "hex"): string;
100
+ (color: string, format: "rgb"): PanelColorRGB;
101
+ (color: string, format: "hsl"): PanelColorHSL;
102
+ (color: string, format: "hsv"): PanelColorHSV;
103
+ (color: PanelColorRGB, format: "hex"): string;
104
+ (color: PanelColorRGB, format: "rgb"): PanelColorRGB;
105
+ (color: PanelColorRGB, format: "hsl"): PanelColorHSL;
106
+ (color: PanelColorRGB, format: "hsv"): PanelColorHSV;
107
+ (color: PanelColorHSL, format: "hex"): string;
108
+ (color: PanelColorHSL, format: "rgb"): PanelColorRGB;
109
+ (color: PanelColorHSL, format: "hsl"): PanelColorHSL;
110
+ (color: PanelColorHSL, format: "hsv"): PanelColorHSV;
111
+ (color: PanelColorHSV, format: "hex"): string;
112
+ (color: PanelColorHSV, format: "rgb"): PanelColorRGB;
113
+ (color: PanelColorHSV, format: "hsl"): PanelColorHSL;
114
+ (color: PanelColorHSV, format: "hsv"): PanelColorHSV;
115
+ (
116
+ color: PanelColorInput,
117
+ format: PanelColorFormat,
118
+ ): string | PanelColorObject;
119
+ };
120
+
121
+ /**
122
+ * Parses a CSS color string to HEX string or color object.
123
+ *
124
+ * Supports:
125
+ * - HEX: #fff, #ffffff, #ffffffff
126
+ * - RGB: rgb(255 255 255), rgb(255, 255, 255), rgba(255 255 255 / 0.5)
127
+ * - HSL: hsl(180 50% 50%), hsl(180deg 50% 50% / 0.5)
128
+ *
129
+ * @param string - CSS color string
130
+ * @returns Parsed color or null/false if invalid
131
+ */
132
+ parse: (
133
+ string: string,
134
+ ) => string | PanelColorRGB | PanelColorHSL | null | false;
135
+
136
+ /**
137
+ * Parses a color string and converts to target format.
138
+ *
139
+ * @param string - CSS color string
140
+ * @param format - Target format
141
+ * @returns Converted color or false if invalid
142
+ */
143
+ parseAs: {
144
+ (string: string, format: "hex"): string | false;
145
+ (string: string, format: "rgb"): PanelColorRGB | false;
146
+ (string: string, format: "hsl"): PanelColorHSL | false;
147
+ (string: string, format: "hsv"): PanelColorHSV | false;
148
+ (
149
+ string: string,
150
+ format?: PanelColorFormat,
151
+ ): string | PanelColorObject | false;
152
+ };
153
+
154
+ /**
155
+ * Formats a color as a CSS string.
156
+ *
157
+ * @param color - Color to format (string or object)
158
+ * @param format - Target format (optional, converts if needed)
159
+ * @param alpha - Include alpha channel (default: true)
160
+ * @returns CSS color string
161
+ * @throws Error if unsupported color
162
+ */
163
+ toString: (
164
+ color: PanelColorInput,
165
+ format?: PanelColorFormat,
166
+ alpha?: boolean,
167
+ ) => string;
168
+ }
169
+
170
+ // -----------------------------------------------------------------------------
171
+ // Dayjs Types
172
+ // -----------------------------------------------------------------------------
173
+
174
+ /**
175
+ * ISO format type.
176
+ */
177
+ export type PanelDayjsISOFormat = "date" | "time" | "datetime";
178
+
179
+ /**
180
+ * Pattern part information.
181
+ */
182
+ export interface PanelDayjsPatternPart {
183
+ /** Pattern token */
184
+ token: string;
185
+ /** Start position */
186
+ start: number;
187
+ /** End position */
188
+ end: number;
189
+ }
190
+
191
+ /**
192
+ * Pattern analyzer object.
193
+ */
194
+ export interface PanelDayjsPattern {
195
+ /** Original pattern string */
196
+ pattern: string;
197
+ /** Parsed pattern parts */
198
+ parts: PanelDayjsPatternPart[];
199
+ /**
200
+ * Gets part information at position range.
201
+ *
202
+ * @param start - Start position
203
+ * @param end - End position
204
+ * @returns Part info or undefined
205
+ */
206
+ at: (start: number, end: number) => PanelDayjsPatternPart | undefined;
207
+ /**
208
+ * Formats a dayjs instance using this pattern.
209
+ *
210
+ * @param dt - Dayjs instance
211
+ * @returns Formatted string
212
+ */
213
+ format: (dt: PanelDayjsInstance) => string;
214
+ }
215
+
216
+ /**
217
+ * Extended dayjs instance with Kirby plugins.
218
+ */
219
+ export interface PanelDayjsInstance {
220
+ /**
221
+ * Formats as ISO string.
222
+ *
223
+ * @param format - 'date', 'time', or 'datetime'
224
+ * @returns ISO formatted string
225
+ */
226
+ toISO: (format?: PanelDayjsISOFormat) => string;
227
+
228
+ /**
229
+ * Validates against a boundary.
230
+ *
231
+ * @param boundary - Boundary date
232
+ * @param type - Validation type
233
+ * @param unit - Comparison unit (default: `'day'`)
234
+ * @returns Whether valid
235
+ */
236
+ validate: (
237
+ boundary: PanelDayjsInput,
238
+ type: "min" | "max",
239
+ unit?: PanelDayjsUnit,
240
+ ) => boolean;
241
+
242
+ /**
243
+ * Merges date or time parts from another dayjs.
244
+ *
245
+ * @param dt - Dayjs to merge from
246
+ * @param units - 'date', 'time', or array of units
247
+ * @returns New dayjs instance
248
+ */
249
+ merge: (
250
+ dt: PanelDayjsInput,
251
+ units?: "date" | "time" | PanelDayjsUnit[],
252
+ ) => PanelDayjsInstance;
253
+
254
+ /**
255
+ * Rounds to nearest unit step.
256
+ *
257
+ * @param unit - Unit to round (default: `'date'`)
258
+ * @param size - Step size (default: 1)
259
+ * @returns Rounded dayjs instance
260
+ */
261
+ round: (unit?: PanelDayjsUnit, size?: number) => PanelDayjsInstance;
262
+
263
+ // Standard dayjs methods
264
+ format: (template?: string) => string;
265
+ valueOf: () => number;
266
+ unix: () => number;
267
+ toString: () => string;
268
+ toDate: () => Date;
269
+ toJSON: () => string;
270
+ toISOString: () => string;
271
+ isValid: () => boolean;
272
+ isSame: (date?: PanelDayjsInput, unit?: PanelDayjsUnit) => boolean;
273
+ isBefore: (date?: PanelDayjsInput, unit?: PanelDayjsUnit) => boolean;
274
+ isAfter: (date?: PanelDayjsInput, unit?: PanelDayjsUnit) => boolean;
275
+ year: () => number;
276
+ month: () => number;
277
+ date: () => number;
278
+ day: () => number;
279
+ hour: () => number;
280
+ minute: () => number;
281
+ second: () => number;
282
+ millisecond: () => number;
283
+ set: (unit: PanelDayjsUnit, value: number) => PanelDayjsInstance;
284
+ add: (value: number, unit?: PanelDayjsUnit) => PanelDayjsInstance;
285
+ subtract: (value: number, unit?: PanelDayjsUnit) => PanelDayjsInstance;
286
+ startOf: (unit: PanelDayjsUnit) => PanelDayjsInstance;
287
+ endOf: (unit: PanelDayjsUnit) => PanelDayjsInstance;
288
+ clone: () => PanelDayjsInstance;
289
+ locale: (locale?: string) => PanelDayjsInstance | string;
290
+ }
291
+
292
+ /**
293
+ * Dayjs input types.
294
+ */
295
+ export type PanelDayjsInput =
296
+ | string
297
+ | number
298
+ | Date
299
+ | PanelDayjsInstance
300
+ | null
301
+ | undefined;
302
+
303
+ /**
304
+ * Dayjs unit types.
305
+ */
306
+ export type PanelDayjsUnit =
307
+ | "year"
308
+ | "month"
309
+ | "week"
310
+ | "day"
311
+ | "date"
312
+ | "hour"
313
+ | "minute"
314
+ | "second"
315
+ | "millisecond";
316
+
317
+ /**
318
+ * Extended dayjs library with Kirby plugins.
319
+ *
320
+ * Provides date manipulation with additional methods
321
+ * for Panel-specific date handling.
322
+ *
323
+ * @example
324
+ * ```ts
325
+ * const dt = this.$library.dayjs('2024-01-15');
326
+ * const iso = dt.toISO('date'); // '2024-01-15'
327
+ * const rounded = dt.round('hour', 15); // Round to 15-minute intervals
328
+ * ```
329
+ *
330
+ * @see https://github.com/getkirby/kirby/blob/main/panel/src/libraries/dayjs.js
331
+ * @since 4.0.0
332
+ */
333
+ export interface PanelLibraryDayjs {
334
+ /**
335
+ * Creates a dayjs instance.
336
+ *
337
+ * @param date - Date input
338
+ * @param format - Parse format
339
+ * @returns Dayjs instance
340
+ */
341
+ (date?: PanelDayjsInput, format?: string): PanelDayjsInstance;
342
+
343
+ /**
344
+ * Interprets date/time from various formats.
345
+ * Tries multiple format variations automatically.
346
+ *
347
+ * @param input - Input string
348
+ * @param format - Expected format type
349
+ * @returns Dayjs instance
350
+ */
351
+ interpret: (
352
+ input: string,
353
+ format?: PanelDayjsISOFormat,
354
+ ) => PanelDayjsInstance;
355
+
356
+ /**
357
+ * Parses ISO formatted string.
358
+ *
359
+ * @param value - ISO string
360
+ * @param format - ISO format type
361
+ * @returns Dayjs instance
362
+ */
363
+ iso: (value: string, format?: PanelDayjsISOFormat) => PanelDayjsInstance;
364
+
365
+ /**
366
+ * Creates a pattern analyzer.
367
+ *
368
+ * @param pattern - Date format pattern
369
+ * @returns Pattern analyzer object
370
+ */
371
+ pattern: (pattern: string) => PanelDayjsPattern;
372
+
373
+ // Standard dayjs static methods
374
+ extend: (plugin: unknown) => void;
375
+ locale: (locale?: string) => string;
376
+ isDayjs: (value: unknown) => value is PanelDayjsInstance;
377
+ }
378
+
379
+ // -----------------------------------------------------------------------------
380
+ // Autosize Types
381
+ // -----------------------------------------------------------------------------
382
+
383
+ /**
384
+ * Autosize library for textarea auto-resizing.
385
+ *
386
+ * External library from npm package "autosize".
387
+ * Automatically adjusts textarea height based on content.
388
+ *
389
+ * @see https://www.npmjs.com/package/autosize
390
+ */
391
+ export interface PanelLibraryAutosize {
392
+ /**
393
+ * Enables autosize on textarea element(s).
394
+ *
395
+ * @param element - Element(s) to autosize
396
+ * @returns The input element(s)
397
+ */
398
+ (
399
+ element: HTMLTextAreaElement | HTMLTextAreaElement[] | NodeList,
400
+ ): typeof element;
401
+
402
+ /**
403
+ * Triggers a resize update.
404
+ *
405
+ * @param element - Element(s) to update
406
+ */
407
+ update: (
408
+ element: HTMLTextAreaElement | HTMLTextAreaElement[] | NodeList,
409
+ ) => void;
410
+
411
+ /**
412
+ * Destroys autosize on element(s).
413
+ *
414
+ * @param element - Element(s) to destroy
415
+ */
416
+ destroy: (
417
+ element: HTMLTextAreaElement | HTMLTextAreaElement[] | NodeList,
418
+ ) => void;
419
+ }
420
+
421
+ // -----------------------------------------------------------------------------
422
+ // Main Library Interface
423
+ // -----------------------------------------------------------------------------
424
+
425
+ /**
426
+ * Panel libraries available on the Vue prototype as `$library`.
427
+ *
428
+ * Provides utilities for color manipulation, date handling,
429
+ * and textarea auto-resizing.
430
+ *
431
+ * @example
432
+ * ```ts
433
+ * // In a Vue component
434
+ * const hex = this.$library.colors.toString({ r: 255, g: 0, b: 0 }, 'hex');
435
+ * const date = this.$library.dayjs('2024-01-15').format('DD.MM.YYYY');
436
+ * this.$library.autosize(this.$refs.textarea);
437
+ * ```
438
+ *
439
+ * @see https://github.com/getkirby/kirby/blob/main/panel/src/libraries/index.js
440
+ */
441
+ export interface PanelLibrary {
442
+ /**
443
+ * Textarea auto-resize library.
444
+ */
445
+ autosize: PanelLibraryAutosize;
446
+
447
+ /**
448
+ * Color manipulation library.
449
+ */
450
+ colors: PanelLibraryColors;
451
+
452
+ /**
453
+ * Date manipulation library (extended dayjs).
454
+ */
455
+ dayjs: PanelLibraryDayjs;
456
+ }