@measured/puck 0.16.0-canary.6d43ba0 → 0.16.0-canary.72b8720

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- import { CSSProperties, ReactNode, ReactElement } from 'react';
1
+ import { CSSProperties, ReactElement, ReactNode } from 'react';
2
2
 
3
3
  type ItemSelector = {
4
4
  index: number;
@@ -12,15 +12,6 @@ type DropZoneProps = {
12
12
  style?: CSSProperties;
13
13
  };
14
14
 
15
- type iconTypes = "Smartphone" | "Monitor" | "Tablet";
16
- type Viewport = {
17
- width: number;
18
- height?: number | "auto";
19
- label?: string;
20
- icon?: iconTypes | ReactNode;
21
- };
22
- type Viewports = Viewport[];
23
-
24
15
  type FieldOption = {
25
16
  label: string;
26
17
  value: string | number | boolean;
@@ -138,35 +129,7 @@ type FieldProps<ValueType = any, F = Field<any>> = {
138
129
  readOnly?: boolean;
139
130
  };
140
131
 
141
- type WithId<Props> = Props & {
142
- id: string;
143
- };
144
- type WithPuckProps<Props> = Props & {
145
- puck: PuckContext;
146
- editMode?: boolean;
147
- };
148
- type AsFieldProps<Props> = Partial<Omit<Props, "children" | "puck" | "editMode">>;
149
- type WithChildren<Props> = Props & {
150
- children: ReactNode;
151
- };
152
- type DefaultRootFieldProps = {
153
- title?: string;
154
- };
155
- type DefaultRootRenderProps<Props extends DefaultComponentProps = DefaultRootFieldProps> = WithPuckProps<WithChildren<Props>>;
156
- type DefaultRootProps = DefaultRootRenderProps;
157
- type DefaultComponentProps = {
158
- [key: string]: any;
159
- };
160
- type Content<PropsMap extends {
161
- [key: string]: any;
162
- } = {
163
- [key: string]: any;
164
- }> = ComponentDataMap<PropsMap>[];
165
132
  type PuckComponent<Props> = (props: WithId<WithPuckProps<Props>>) => JSX.Element;
166
- type PuckContext = {
167
- renderDropZone: React.FC<DropZoneProps>;
168
- isEditing: boolean;
169
- };
170
133
  type ComponentConfig<RenderProps extends DefaultComponentProps = DefaultComponentProps, FieldProps extends DefaultComponentProps = RenderProps, DataShape = Omit<ComponentData<FieldProps>, "type">> = {
171
134
  render: PuckComponent<RenderProps>;
172
135
  label?: string;
@@ -177,12 +140,12 @@ type ComponentConfig<RenderProps extends DefaultComponentProps = DefaultComponen
177
140
  changed: Partial<Record<keyof FieldProps, boolean>>;
178
141
  fields: Fields<FieldProps>;
179
142
  lastFields: Fields<FieldProps>;
180
- lastData: DataShape;
143
+ lastData: DataShape | null;
181
144
  appState: AppState;
182
145
  }) => Promise<Fields<FieldProps>> | Fields<FieldProps>;
183
146
  resolveData?: (data: DataShape, params: {
184
147
  changed: Partial<Record<keyof FieldProps, boolean>>;
185
- lastData: DataShape;
148
+ lastData: DataShape | null;
186
149
  }) => Promise<{
187
150
  props?: Partial<FieldProps>;
188
151
  readOnly?: Partial<Record<keyof FieldProps, boolean>>;
@@ -192,10 +155,11 @@ type ComponentConfig<RenderProps extends DefaultComponentProps = DefaultComponen
192
155
  };
193
156
  resolvePermissions?: (data: DataShape, params: {
194
157
  changed: Partial<Record<keyof FieldProps, boolean>>;
195
- lastPermissions: Partial<Permissions> | undefined;
196
- initialPermissions: Partial<Permissions>;
158
+ lastPermissions: Partial<Permissions>;
159
+ permissions: Partial<Permissions>;
197
160
  appState: AppState;
198
- }) => Partial<Permissions>;
161
+ lastData: DataShape | null;
162
+ }) => Promise<Partial<Permissions>> | Partial<Permissions>;
199
163
  };
200
164
  type Category<ComponentName> = {
201
165
  components?: ComponentName[];
@@ -203,15 +167,51 @@ type Category<ComponentName> = {
203
167
  visible?: boolean;
204
168
  defaultExpanded?: boolean;
205
169
  };
206
- type Config<Props extends Record<string, any> = Record<string, any>, RootProps extends DefaultComponentProps = DefaultRootFieldProps, CategoryName extends string = string> = {
170
+ type Config<Props extends DefaultComponentProps = DefaultComponentProps, RootProps extends DefaultComponentProps = any, CategoryName extends string = string> = {
207
171
  categories?: Record<CategoryName, Category<keyof Props>> & {
208
172
  other?: Category<keyof Props>;
209
173
  };
210
174
  components: {
211
175
  [ComponentName in keyof Props]: Omit<ComponentConfig<Props[ComponentName], Props[ComponentName]>, "type">;
212
176
  };
213
- root?: Partial<ComponentConfig<DefaultRootRenderProps<RootProps>, AsFieldProps<RootProps>, RootData>>;
177
+ root?: Partial<ComponentConfig<DefaultRootRenderProps<RootProps>, AsFieldProps<RootProps>, RootData<AsFieldProps<RootProps>>>>;
178
+ };
179
+
180
+ type WithId<Props> = Props & {
181
+ id: string;
182
+ };
183
+ type WithPuckProps<Props> = Props & {
184
+ puck: PuckContext;
185
+ editMode?: boolean;
186
+ };
187
+ type AsFieldProps<Props> = Omit<Props, "children" | "puck" | "editMode">;
188
+ type WithChildren<Props> = Props & {
189
+ children: ReactNode;
190
+ };
191
+ type ExtractPropsFromConfig<UserConfig> = UserConfig extends Config<infer P, any, any> ? P : never;
192
+ type ExtractRootPropsFromConfig<UserConfig> = UserConfig extends Config<any, infer P, any> ? P : never;
193
+ type UserGenerics<UserConfig extends Config = Config, UserProps extends ExtractPropsFromConfig<UserConfig> = ExtractPropsFromConfig<UserConfig>, UserRootProps extends ExtractRootPropsFromConfig<UserConfig> = ExtractRootPropsFromConfig<UserConfig>, UserData extends Data<UserProps, UserRootProps> | Data = Data<UserProps, UserRootProps>, UserAppState extends AppState<UserData> = AppState<UserData>, UserComponentData extends ComponentData = UserData["content"][0]> = {
194
+ UserConfig: UserConfig;
195
+ UserProps: UserProps;
196
+ UserRootProps: UserRootProps;
197
+ UserData: UserData;
198
+ UserAppState: UserAppState;
199
+ UserComponentData: UserComponentData;
214
200
  };
201
+
202
+ type PuckContext = {
203
+ renderDropZone: React.FC<DropZoneProps>;
204
+ isEditing: boolean;
205
+ };
206
+ type DefaultRootFieldProps = {
207
+ title?: string;
208
+ };
209
+ type DefaultRootRenderProps<Props extends DefaultComponentProps = DefaultRootFieldProps> = WithPuckProps<WithChildren<Props>>;
210
+ type DefaultRootProps = DefaultRootRenderProps;
211
+ type DefaultComponentProps = {
212
+ [key: string]: any;
213
+ };
214
+
215
215
  type BaseData<Props extends {
216
216
  [key: string]: any;
217
217
  } = {
@@ -219,24 +219,30 @@ type BaseData<Props extends {
219
219
  }> = {
220
220
  readOnly?: Partial<Record<keyof Props, boolean>>;
221
221
  };
222
- type ComponentData<Props extends DefaultComponentProps = DefaultComponentProps, Name = string> = {
223
- type: Name;
224
- props: WithId<Props>;
225
- } & BaseData<Props>;
226
- type ComponentDataMap<Props extends Record<string, DefaultComponentProps> = DefaultComponentProps> = {
227
- [K in keyof Props]: ComponentData<Props[K], K>;
228
- }[keyof Props];
229
222
  type RootDataWithProps<Props extends DefaultComponentProps = DefaultRootFieldProps> = BaseData<Props> & {
230
223
  props: Props;
231
224
  };
232
225
  type RootDataWithoutProps<Props extends DefaultComponentProps = DefaultRootFieldProps> = Props;
233
226
  type RootData<Props extends DefaultComponentProps = DefaultRootFieldProps> = Partial<RootDataWithProps<AsFieldProps<Props>>> & Partial<RootDataWithoutProps<Props>>;
227
+ type ComponentData<Props extends DefaultComponentProps = DefaultComponentProps, Name = string> = {
228
+ type: Name;
229
+ props: WithId<Props>;
230
+ } & BaseData<Props>;
234
231
  type MappedItem = ComponentData;
232
+ type ComponentDataMap<Props extends Record<string, DefaultComponentProps> = DefaultComponentProps> = {
233
+ [K in keyof Props]: ComponentData<Props[K], K extends string ? K : never>;
234
+ }[keyof Props];
235
+ type Content<PropsMap extends {
236
+ [key: string]: any;
237
+ } = {
238
+ [key: string]: any;
239
+ }> = ComponentDataMap<PropsMap>[];
235
240
  type Data<Props extends DefaultComponentProps = DefaultComponentProps, RootProps extends DefaultComponentProps = DefaultRootFieldProps> = {
236
241
  root: RootData<RootProps>;
237
242
  content: Content<Props>;
238
243
  zones?: Record<string, Content<Props>>;
239
244
  };
245
+
240
246
  type ItemWithId = {
241
247
  _arrayId: string;
242
248
  _originalIndex: number;
@@ -266,10 +272,83 @@ type UiState = {
266
272
  options: Viewport[];
267
273
  };
268
274
  };
269
- type AppState = {
270
- data: Data;
275
+ type AppState<UserData extends Data = Data> = {
276
+ data: UserData;
271
277
  ui: UiState;
272
278
  };
279
+
280
+ type RenderFunc<Props extends {
281
+ [key: string]: any;
282
+ } = {
283
+ children: ReactNode;
284
+ }> = (props: Props) => ReactElement;
285
+ declare const overrideKeys: readonly ["header", "headerActions", "fields", "fieldLabel", "components", "componentItem", "outline", "puck", "preview"];
286
+ type OverrideKey = (typeof overrideKeys)[number];
287
+ type OverridesGeneric<Shape extends {
288
+ [key in OverrideKey]: any;
289
+ }> = Shape;
290
+ type Overrides = OverridesGeneric<{
291
+ fieldTypes: Partial<FieldRenderFunctions>;
292
+ header: RenderFunc<{
293
+ actions: ReactNode;
294
+ children: ReactNode;
295
+ }>;
296
+ actionBar: RenderFunc<{
297
+ label?: string;
298
+ children: ReactNode;
299
+ }>;
300
+ headerActions: RenderFunc<{
301
+ children: ReactNode;
302
+ }>;
303
+ preview: RenderFunc;
304
+ fields: RenderFunc<{
305
+ children: ReactNode;
306
+ isLoading: boolean;
307
+ itemSelector?: ItemSelector | null;
308
+ }>;
309
+ fieldLabel: RenderFunc<{
310
+ children?: ReactNode;
311
+ icon?: ReactNode;
312
+ label: string;
313
+ el?: "label" | "div";
314
+ readOnly?: boolean;
315
+ className?: string;
316
+ }>;
317
+ components: RenderFunc;
318
+ componentItem: RenderFunc<{
319
+ children: ReactNode;
320
+ name: string;
321
+ }>;
322
+ iframe: RenderFunc<{
323
+ children: ReactNode;
324
+ document?: Document;
325
+ }>;
326
+ outline: RenderFunc;
327
+ puck: RenderFunc;
328
+ }>;
329
+ type FieldRenderFunctions = Omit<{
330
+ [Type in Field["type"]]: React.FunctionComponent<FieldProps<Extract<Field, {
331
+ type: Type;
332
+ }>> & {
333
+ children: ReactNode;
334
+ name: string;
335
+ }>;
336
+ }, "custom"> & {
337
+ [key: string]: React.FunctionComponent<FieldProps<any> & {
338
+ children: ReactNode;
339
+ name: string;
340
+ }>;
341
+ };
342
+
343
+ type iconTypes = "Smartphone" | "Monitor" | "Tablet";
344
+ type Viewport = {
345
+ width: number;
346
+ height?: number | "auto";
347
+ label?: string;
348
+ icon?: iconTypes | ReactNode;
349
+ };
350
+ type Viewports = Viewport[];
351
+
273
352
  type Permissions = {
274
353
  drag: boolean;
275
354
  duplicate: boolean;
@@ -277,5 +356,88 @@ type Permissions = {
277
356
  edit: boolean;
278
357
  insert: boolean;
279
358
  } & Record<string, boolean>;
359
+ type IframeConfig = {
360
+ enabled?: boolean;
361
+ waitForStyles?: boolean;
362
+ };
363
+ type OnAction<UserData extends Data = Data> = (action: PuckAction, appState: AppState<UserData>, prevAppState: AppState<UserData>) => void;
364
+ type Plugin = {
365
+ overrides: Partial<Overrides>;
366
+ };
367
+ type History<D = any> = {
368
+ state: D;
369
+ id?: string;
370
+ };
371
+ type InitialHistoryAppend<AS = Partial<AppState>> = {
372
+ histories: History<AS>[];
373
+ index?: number;
374
+ appendData?: true;
375
+ };
376
+ type InitialHistoryNoAppend<AS = Partial<AppState>> = {
377
+ histories: [History<AS>, ...History<AS>[]];
378
+ index?: number;
379
+ appendData?: false;
380
+ };
381
+ type InitialHistory<AS = Partial<AppState>> = InitialHistoryAppend<AS> | InitialHistoryNoAppend<AS>;
382
+
383
+ type InsertAction = {
384
+ type: "insert";
385
+ componentType: string;
386
+ destinationIndex: number;
387
+ destinationZone: string;
388
+ id?: string;
389
+ };
390
+ type DuplicateAction = {
391
+ type: "duplicate";
392
+ sourceIndex: number;
393
+ sourceZone: string;
394
+ };
395
+ type ReplaceAction = {
396
+ type: "replace";
397
+ destinationIndex: number;
398
+ destinationZone: string;
399
+ data: any;
400
+ };
401
+ type ReorderAction = {
402
+ type: "reorder";
403
+ sourceIndex: number;
404
+ destinationIndex: number;
405
+ destinationZone: string;
406
+ };
407
+ type MoveAction = {
408
+ type: "move";
409
+ sourceIndex: number;
410
+ sourceZone: string;
411
+ destinationIndex: number;
412
+ destinationZone: string;
413
+ };
414
+ type RemoveAction = {
415
+ type: "remove";
416
+ index: number;
417
+ zone: string;
418
+ };
419
+ type SetUiAction = {
420
+ type: "setUi";
421
+ ui: Partial<UiState> | ((previous: UiState) => Partial<UiState>);
422
+ };
423
+ type SetDataAction = {
424
+ type: "setData";
425
+ data: Partial<Data> | ((previous: Data) => Partial<Data>);
426
+ };
427
+ type SetAction<UserData extends Data = Data> = {
428
+ type: "set";
429
+ state: Partial<AppState<UserData>> | ((previous: AppState<UserData>) => Partial<AppState<UserData>>);
430
+ };
431
+ type RegisterZoneAction = {
432
+ type: "registerZone";
433
+ zone: string;
434
+ };
435
+ type UnregisterZoneAction = {
436
+ type: "unregisterZone";
437
+ zone: string;
438
+ };
439
+ type PuckAction = {
440
+ recordHistory?: boolean;
441
+ } & (ReorderAction | InsertAction | MoveAction | ReplaceAction | RemoveAction | DuplicateAction | SetAction | SetDataAction | SetUiAction | RegisterZoneAction | UnregisterZoneAction);
280
442
 
281
- export type { AppState as A, BaseData as B, Config as C, Data as D, ExternalFieldWithAdaptor as E, Field as F, ItemSelector as I, MappedItem as M, NumberField as N, ObjectField as O, Permissions as P, RootData as R, SelectField as S, TextField as T, UiState as U, Viewports as V, FieldProps as a, DropZoneProps as b, DefaultComponentProps as c, DefaultRootFieldProps as d, ComponentData as e, RootDataWithProps as f, Content as g, DefaultRootRenderProps as h, DefaultRootProps as i, PuckComponent as j, PuckContext as k, ComponentConfig as l, ComponentDataMap as m, RootDataWithoutProps as n, ItemWithId as o, ArrayState as p, BaseField as q, TextareaField as r, RadioField as s, ArrayField as t, Adaptor as u, ExternalField as v, CustomField as w, Fields as x };
443
+ export { type DefaultRootProps as $, type AppState as A, type BaseData as B, type Config as C, type Data as D, type ExtractPropsFromConfig as E, type Field as F, type RadioField as G, type History as H, type ItemSelector as I, type ArrayField as J, type ObjectField as K, type Adaptor as L, type MappedItem as M, type NumberField as N, type OnAction as O, type PuckAction as P, type ExternalFieldWithAdaptor as Q, type RootDataWithProps as R, type SelectField as S, type TextField as T, type UserGenerics as U, type Viewports as V, type ExternalField as W, type CustomField as X, type Fields as Y, type PuckContext as Z, type DefaultRootRenderProps as _, type FieldProps as a, type WithId as a0, type WithPuckProps as a1, type AsFieldProps as a2, type WithChildren as a3, type DropZoneProps as b, type UiState as c, type Permissions as d, type Plugin as e, type Overrides as f, type IframeConfig as g, type InitialHistory as h, type DefaultComponentProps as i, type DefaultRootFieldProps as j, type ComponentData as k, type ExtractRootPropsFromConfig as l, type ComponentDataMap as m, type Viewport as n, overrideKeys as o, type OverrideKey as p, type FieldRenderFunctions as q, type ItemWithId as r, type ArrayState as s, type PuckComponent as t, type ComponentConfig as u, type RootDataWithoutProps as v, type RootData as w, type Content as x, type BaseField as y, type TextareaField as z };