@caprionlinesrl/puck-plugin-media 0.1.4 → 0.1.5

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.
package/README.md CHANGED
@@ -26,6 +26,34 @@ yarn add @caprionlinesrl/puck-plugin-media
26
26
 
27
27
  ### 1. Create the plugin
28
28
 
29
+ #### Option A: Using mock data (for demos/prototyping)
30
+
31
+ ```tsx
32
+ import { createMediaPlugin } from '@caprionlinesrl/puck-plugin-media';
33
+ import { mockMediaConfig } from '@caprionlinesrl/puck-plugin-media/mocks';
34
+
35
+ // One-liner with mock data
36
+ const mediaPlugin = createMediaPlugin(mockMediaConfig);
37
+
38
+ // Or with custom options
39
+ const mediaPlugin = createMediaPlugin({
40
+ ...mockMediaConfig,
41
+ languages: [
42
+ { code: 'en', label: 'English' },
43
+ { code: 'it', label: 'Italiano' },
44
+ ],
45
+ image: {
46
+ ...mockMediaConfig.image,
47
+ uploadConfig: {
48
+ accept: 'image/jpeg,image/png,image/webp',
49
+ maxSize: 10 * 1024 * 1024, // 10MB
50
+ },
51
+ },
52
+ });
53
+ ```
54
+
55
+ #### Option B: With your own API
56
+
29
57
  ```tsx
30
58
  import { createMediaPlugin } from '@caprionlinesrl/puck-plugin-media';
31
59
 
@@ -150,6 +178,29 @@ const config = {
150
178
  };
151
179
  ```
152
180
 
181
+ ## Mock Data
182
+
183
+ The plugin includes mock data for quick prototyping and demos. Import from the `/mocks` subpath:
184
+
185
+ ```tsx
186
+ import { mockMediaConfig } from '@caprionlinesrl/puck-plugin-media/mocks';
187
+ ```
188
+
189
+ `mockMediaConfig` provides:
190
+ - **12 sample images** from Unsplash with multilingual alt text
191
+ - **3 sample galleries** (Landscapes, Team, Portfolio)
192
+ - **4 sample documents** (PDF files)
193
+ - Full CRUD operations (in-memory, resets on page reload)
194
+ - Simulated upload with progress
195
+ - Search and pagination support
196
+
197
+ This is useful for:
198
+ - Quick demos and prototypes
199
+ - Testing your Puck configuration
200
+ - Development without a backend
201
+
202
+ > **Note:** Mock data is stored in memory and will reset on page reload.
203
+
153
204
  ## Configuration
154
205
 
155
206
  ### Languages
package/dist/index.d.mts CHANGED
@@ -1,352 +1,7 @@
1
- import { ReactNode } from 'react';
1
+ import { M as MediaPluginOptions, P as PuckPlugin, I as ImageFieldProps, a as ImagePickerModalProps, b as ImageGridProps, G as GalleryFieldProps, c as GalleryPickerModalProps, D as DocumentFieldProps, d as DocumentPickerModalProps, L as Language, e as ImageOptions, f as GalleryOptions, g as DocumentOptions, U as UploadConfig, h as UploadingFile, i as ImageItem, j as DocumentItem, k as UploadFn } from './types-DTNDlwYP.mjs';
2
+ export { l as DEFAULT_LANGUAGES, F as FetchListFn, m as FetchListParams, n as FetchListResult, o as GalleryItem, p as LocalizedString, q as UploadCallbacks, r as UploadStatus } from './types-DTNDlwYP.mjs';
2
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
-
4
- /**
5
- * Language configuration for multilingual fields
6
- */
7
- interface Language {
8
- code: string;
9
- label: string;
10
- }
11
- /**
12
- * Default languages when not specified
13
- */
14
- declare const DEFAULT_LANGUAGES: Language[];
15
- /**
16
- * Localized string value (e.g., { en: 'Hello', it: 'Ciao' })
17
- */
18
- type LocalizedString = Record<string, string | undefined>;
19
- /**
20
- * Image item - used both for API responses and stored values in Puck JSON
21
- */
22
- interface ImageItem {
23
- /** Unique identifier */
24
- id: string;
25
- /** Full URL to the image file */
26
- url: string;
27
- /** Original filename */
28
- filename?: string;
29
- /** Alt text (multilingual) */
30
- alt?: LocalizedString;
31
- /** Image width in pixels */
32
- width?: number;
33
- /** Image height in pixels */
34
- height?: number;
35
- /** File size in bytes */
36
- size?: number;
37
- /** Thumbnail URL for faster loading in picker (falls back to url if not provided) */
38
- thumbnailUrl?: string;
39
- /** Creation date (useful for cache invalidation) */
40
- createdAt?: string;
41
- }
42
- /**
43
- * Document item - used both for API responses and stored values in Puck JSON
44
- */
45
- interface DocumentItem {
46
- /** Unique identifier */
47
- id: string;
48
- /** Full URL to the document file */
49
- url: string;
50
- /** Original filename */
51
- filename: string;
52
- /** Display title (multilingual) */
53
- title?: LocalizedString;
54
- /** MIME type (e.g., 'application/pdf') */
55
- mimeType: string;
56
- /** File size in bytes */
57
- size: number;
58
- /** File extension (e.g., 'pdf', 'docx') */
59
- extension: string;
60
- /** Creation date (useful for cache invalidation) */
61
- createdAt?: string;
62
- }
63
- /**
64
- * Gallery item - used both for API responses and stored values in Puck JSON
65
- */
66
- interface GalleryItem {
67
- /** Unique identifier */
68
- id: string;
69
- /** Gallery name */
70
- name: string;
71
- /** Cover image for preview */
72
- coverImage?: ImageItem;
73
- /** Images in the gallery */
74
- images: ImageItem[];
75
- /** Number of images in the gallery (optional, can be calculated from images.length) */
76
- imageCount?: number;
77
- /** Creation date (useful for cache invalidation) */
78
- createdAt?: string;
79
- }
80
- /**
81
- * Parameters passed to fetchList functions
82
- */
83
- interface FetchListParams {
84
- /** Search query string */
85
- query?: string;
86
- /** Page number (1-indexed) */
87
- page?: number;
88
- /** Items per page */
89
- pageSize?: number;
90
- /** Additional filters */
91
- filters?: Record<string, unknown>;
92
- }
93
- /**
94
- * Result from fetchList - paginated result
95
- */
96
- interface FetchListResult<T> {
97
- /** Array of items */
98
- items: T[];
99
- /** Total number of items (for pagination) */
100
- total?: number;
101
- /** Whether there are more items to load */
102
- hasMore?: boolean;
103
- }
104
- /**
105
- * Generic fetch list function type
106
- */
107
- type FetchListFn<T> = (params: FetchListParams) => Promise<T[] | FetchListResult<T>>;
108
- /**
109
- * Callbacks passed to upload functions
110
- */
111
- interface UploadCallbacks {
112
- /** Called with upload progress (0-100) */
113
- onProgress: (percent: number) => void;
114
- }
115
- /**
116
- * Upload function that handles single or multiple files
117
- */
118
- type UploadFn<T> = (files: File | File[], callbacks: UploadCallbacks) => Promise<T | T[]>;
119
- /**
120
- * Configuration for file uploads
121
- */
122
- interface UploadConfig {
123
- /**
124
- * Accepted file types (MIME types or extensions)
125
- * @default 'image/*' for images, '*' for documents
126
- */
127
- accept?: string;
128
- /**
129
- * Maximum file size in bytes
130
- * @default 10485760 (10MB)
131
- */
132
- maxSize?: number;
133
- /**
134
- * Allow multiple file uploads
135
- * @default true
136
- */
137
- multiple?: boolean;
138
- }
139
- /**
140
- * Status of a file being uploaded
141
- */
142
- type UploadStatus = 'pending' | 'uploading' | 'completed' | 'error';
143
- /**
144
- * State of a file in the upload queue
145
- */
146
- interface UploadingFile {
147
- /** Unique ID for this upload */
148
- id: string;
149
- /** The file being uploaded */
150
- file: File;
151
- /** Upload progress (0-100) */
152
- progress: number;
153
- /** Current upload status */
154
- status: UploadStatus;
155
- /** Error message if status is 'error' */
156
- error?: string;
157
- /** Resulting item if status is 'completed' */
158
- result?: ImageItem | DocumentItem;
159
- /** Preview URL for the file (blob URL) */
160
- previewUrl?: string;
161
- }
162
- /**
163
- * Image-related callbacks
164
- */
165
- interface ImageOptions {
166
- /** Function to fetch image list from your API */
167
- fetchList: FetchListFn<ImageItem>;
168
- /** Function to upload images (optional) */
169
- upload?: UploadFn<ImageItem>;
170
- /** Function to update image metadata (alt text) */
171
- update?: (id: string, data: {
172
- alt?: LocalizedString;
173
- }) => Promise<ImageItem>;
174
- /** Function to delete an image (optional) */
175
- delete?: (id: string) => Promise<void>;
176
- /** Configuration for file uploads */
177
- uploadConfig?: UploadConfig;
178
- }
179
- /**
180
- * Gallery-related callbacks
181
- */
182
- interface GalleryOptions {
183
- /** Function to fetch gallery list from your API (required) */
184
- fetchList: FetchListFn<GalleryItem>;
185
- /** Function to fetch a single gallery by ID */
186
- fetch: (id: string) => Promise<GalleryItem>;
187
- /** Function to create a new gallery */
188
- create: (name: string) => Promise<GalleryItem>;
189
- /** Function to delete a gallery */
190
- delete: (id: string) => Promise<void>;
191
- /** Function to upload images to a gallery */
192
- upload: (galleryId: string, files: File | File[], callbacks: UploadCallbacks) => Promise<ImageItem | ImageItem[]>;
193
- /** Function to remove an image from a gallery */
194
- removeImage: (galleryId: string, imageId: string) => Promise<void>;
195
- /** Function to update image metadata within a gallery (optional - hides alt editing if not provided) */
196
- updateImage?: (galleryId: string, imageId: string, data: {
197
- alt?: LocalizedString;
198
- }) => Promise<ImageItem>;
199
- }
200
- /**
201
- * Document-related callbacks
202
- */
203
- interface DocumentOptions {
204
- /** Function to fetch document list from your API */
205
- fetchList: FetchListFn<DocumentItem>;
206
- /** Function to upload documents (optional - hides upload area if not provided) */
207
- upload?: UploadFn<DocumentItem>;
208
- /** Function to update document metadata (title) (optional - hides edit icon if not provided) */
209
- update?: (id: string, data: {
210
- title?: LocalizedString;
211
- }) => Promise<DocumentItem>;
212
- /** Function to delete a document (optional - hides manage button if not provided) */
213
- delete?: (id: string) => Promise<void>;
214
- /** Configuration for file uploads */
215
- uploadConfig?: UploadConfig;
216
- }
217
- /**
218
- * Plugin configuration options
219
- */
220
- interface MediaPluginOptions {
221
- /**
222
- * Languages for multilingual fields (alt, title)
223
- * @default [{ code: 'it', label: 'Italiano' }, { code: 'en', label: 'English' }]
224
- */
225
- languages?: Language[];
226
- /**
227
- * Image-related configuration (required)
228
- */
229
- image: ImageOptions;
230
- /**
231
- * Gallery-related configuration (optional)
232
- */
233
- gallery?: GalleryOptions;
234
- /**
235
- * Document-related configuration (optional)
236
- */
237
- document?: DocumentOptions;
238
- }
239
- /**
240
- * Internal props passed to ImageField component
241
- */
242
- interface ImageFieldProps {
243
- name: string;
244
- value: ImageItem | null;
245
- onChange: (value: ImageItem | null) => void;
246
- field: {
247
- label?: string;
248
- [key: string]: unknown;
249
- };
250
- languages: Language[];
251
- imageOptions: ImageOptions;
252
- }
253
- /**
254
- * Internal props passed to GalleryField component
255
- */
256
- interface GalleryFieldProps {
257
- name: string;
258
- value: GalleryItem | null;
259
- onChange: (value: GalleryItem | null) => void;
260
- field: {
261
- label?: string;
262
- [key: string]: unknown;
263
- };
264
- languages: Language[];
265
- galleryOptions: GalleryOptions;
266
- }
267
- /**
268
- * Internal props passed to DocumentField component
269
- */
270
- interface DocumentFieldProps {
271
- name: string;
272
- value: DocumentItem | null;
273
- onChange: (value: DocumentItem | null) => void;
274
- field: {
275
- label?: string;
276
- [key: string]: unknown;
277
- };
278
- languages: Language[];
279
- documentOptions: DocumentOptions;
280
- }
281
- /**
282
- * Props for ImagePickerModal component
283
- */
284
- interface ImagePickerModalProps {
285
- languages: Language[];
286
- imageOptions: ImageOptions;
287
- title?: string;
288
- selectedImage?: ImageItem | null;
289
- onSelect?: (item: ImageItem) => void;
290
- onClose: () => void;
291
- /** When false, hides the "Select Image" button. Useful for media library mode. Default: true */
292
- selectable?: boolean;
293
- }
294
- /**
295
- * Props for GalleryPickerModal component
296
- */
297
- interface GalleryPickerModalProps {
298
- languages: Language[];
299
- galleryOptions: GalleryOptions;
300
- title?: string;
301
- selectedGallery?: GalleryItem | null;
302
- onSelect?: (gallery: GalleryItem) => void;
303
- onClose: () => void;
304
- /** When false, hides the "Select Gallery" button. Useful for media library mode. Default: true */
305
- selectable?: boolean;
306
- }
307
- /**
308
- * Props for DocumentPickerModal component
309
- */
310
- interface DocumentPickerModalProps {
311
- languages: Language[];
312
- documentOptions: DocumentOptions;
313
- title?: string;
314
- selectedDocument?: DocumentItem | null;
315
- onSelect?: (item: DocumentItem) => void;
316
- onClose: () => void;
317
- /** When false, hides the "Select Document" button. Useful for media library mode. Default: true */
318
- selectable?: boolean;
319
- }
320
- /**
321
- * Props for ImageGrid component
322
- */
323
- interface ImageGridProps {
324
- items: ImageItem[];
325
- onSelect: (item: ImageItem) => void;
326
- selectedId?: string;
327
- loading?: boolean;
328
- /** Callback when user clicks edit icon */
329
- onEditAlt?: (item: ImageItem) => void;
330
- /** Whether manage mode is active (for bulk delete) */
331
- manageMode?: boolean;
332
- /** Set of selected item IDs for bulk operations */
333
- selectedIds?: Set<string>;
334
- /** Callback when user toggles selection in manage mode */
335
- onToggleSelect?: (item: ImageItem) => void;
336
- }
337
- /**
338
- * Puck Plugin type
339
- */
340
- interface PuckPlugin {
341
- name: string;
342
- label?: string;
343
- icon?: ReactNode;
344
- render?: () => ReactNode;
345
- overrides?: {
346
- fieldTypes?: Record<string, (props: unknown) => ReactNode>;
347
- [key: string]: unknown;
348
- };
349
- }
4
+ import 'react';
350
5
 
351
6
  /**
352
7
  * Create a Puck plugin for media library integration
@@ -476,4 +131,4 @@ interface UseUploadReturn {
476
131
  }
477
132
  declare function useUpload<T extends ImageItem | DocumentItem>({ upload, config, onUploadComplete, }: UseUploadOptions<T>): UseUploadReturn;
478
133
 
479
- export { DEFAULT_LANGUAGES, DocumentField, type DocumentItem, type DocumentOptions, DocumentPickerModal, type FetchListFn, type FetchListParams, type FetchListResult, GalleryField, type GalleryItem, type GalleryOptions, GalleryPickerModal, ImageField, ImageGrid, type ImageItem, type ImageOptions, ImagePickerModal, type Language, type LocalizedString, MediaPanel, type MediaPluginOptions, type UploadCallbacks, type UploadConfig, UploadDropzone, type UploadFn, UploadQueue, type UploadStatus, type UploadingFile, createMediaPlugin, formatFileSize, useUpload };
134
+ export { DocumentField, DocumentItem, DocumentOptions, DocumentPickerModal, GalleryField, GalleryOptions, GalleryPickerModal, ImageField, ImageGrid, ImageItem, ImageOptions, ImagePickerModal, Language, MediaPanel, MediaPluginOptions, UploadConfig, UploadDropzone, UploadFn, UploadQueue, UploadingFile, createMediaPlugin, formatFileSize, useUpload };