@measured/puck 0.15.0-canary.aedd401 → 0.15.0-canary.dd7051e

Sign up to get free protection for your applications and to get access to all the features.
@@ -152,8 +152,16 @@ type ComponentConfig<ComponentProps extends DefaultComponentProps = DefaultCompo
152
152
  label?: string;
153
153
  defaultProps?: DefaultProps;
154
154
  fields?: Fields<ComponentProps>;
155
+ resolveFields?: (data: DataShape, params: {
156
+ changed: Partial<Record<keyof ComponentProps, boolean>>;
157
+ fields: Fields<ComponentProps>;
158
+ lastFields: Fields<ComponentProps>;
159
+ lastData: DataShape;
160
+ appState: AppState;
161
+ }) => Promise<Fields<ComponentProps>> | Fields<ComponentProps>;
155
162
  resolveData?: (data: DataShape, params: {
156
163
  changed: Partial<Record<keyof ComponentProps, boolean>>;
164
+ lastData: DataShape;
157
165
  }) => Promise<{
158
166
  props?: Partial<ComponentProps>;
159
167
  readOnly?: Partial<Record<keyof ComponentProps, boolean>>;
@@ -179,7 +187,7 @@ type Config<Props extends Record<string, any> = Record<string, any>, RootProps e
179
187
  children?: ReactNode;
180
188
  }, Partial<RootProps & {
181
189
  children?: ReactNode;
182
- }>, RootDataWithProps>>;
190
+ }>, RootData>>;
183
191
  };
184
192
  type BaseData<Props extends {
185
193
  [key: string]: any;
@@ -192,11 +200,11 @@ type ComponentData<Props extends DefaultComponentProps = DefaultComponentProps>
192
200
  type: keyof Props;
193
201
  props: WithPuckProps<Props>;
194
202
  } & BaseData<Props>;
195
- type RootDataWithProps<Props extends DefaultRootProps = DefaultRootProps> = {
203
+ type RootDataWithProps<Props extends DefaultRootProps = DefaultRootProps> = BaseData<Props> & {
196
204
  props: Props;
197
205
  };
198
206
  type RootDataWithoutProps<Props extends DefaultRootProps = DefaultRootProps> = Props;
199
- type RootData<Props extends DefaultRootProps = DefaultRootProps> = BaseData<Props> & Partial<RootDataWithProps<Props>> & Partial<RootDataWithoutProps<Props>>;
207
+ type RootData<Props extends DefaultRootProps = DefaultRootProps> = Partial<RootDataWithProps<Props>> & Partial<RootDataWithoutProps<Props>>;
200
208
  type MappedItem = ComponentData;
201
209
  type Data<Props extends DefaultComponentProps = DefaultComponentProps, RootProps extends DefaultRootProps = DefaultRootProps> = {
202
210
  root: RootData<RootProps>;
package/dist/index.css CHANGED
@@ -1462,11 +1462,14 @@ textarea._Input-input_1qi5b_46 {
1462
1462
  }
1463
1463
 
1464
1464
  /* css-module:/home/runner/work/puck/puck/packages/core/components/Puck/components/Fields/styles.module.css/#css-module-data */
1465
- ._PuckFields_17k3p_1 {
1465
+ ._PuckFields_1o0if_1 {
1466
1466
  position: relative;
1467
1467
  font-family: var(--puck-font-family);
1468
1468
  }
1469
- ._PuckFields-loadingOverlay_17k3p_6 {
1469
+ ._PuckFields--isLoading_1o0if_6 {
1470
+ min-height: 48px;
1471
+ }
1472
+ ._PuckFields-loadingOverlay_1o0if_10 {
1470
1473
  background: var(--puck-color-white);
1471
1474
  display: flex;
1472
1475
  justify-content: center;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { U as UiState, D as Data, A as AppState, C as Config, a as DefaultRootProps, I as ItemSelector, b as DropZoneProps, F as Field, V as Viewports, c as DefaultComponentProps, M as MappedItem, R as RootDataWithProps, d as RootData, e as ComponentData } from './Config-550396e1.js';
2
- export { i as Adaptor, h as ArrayField, s as ArrayState, p as BaseData, B as BaseField, o as ComponentConfig, m as Content, k as CustomField, j as ExternalField, E as ExternalFieldWithAdaptor, l as Fields, r as ItemWithId, N as NumberField, O as ObjectField, P as PuckComponent, n as PuckContext, g as RadioField, q as RootDataWithoutProps, S as SelectField, T as TextField, f as TextareaField } from './Config-550396e1.js';
1
+ import { U as UiState, D as Data, A as AppState, C as Config, a as DefaultRootProps, I as ItemSelector, b as DropZoneProps, F as Field, V as Viewports, c as DefaultComponentProps, M as MappedItem, R as RootDataWithProps, d as RootData, e as ComponentData } from './Config-25917005.js';
2
+ export { i as Adaptor, h as ArrayField, s as ArrayState, p as BaseData, B as BaseField, o as ComponentConfig, m as Content, k as CustomField, j as ExternalField, E as ExternalFieldWithAdaptor, l as Fields, r as ItemWithId, N as NumberField, O as ObjectField, P as PuckComponent, n as PuckContext, g as RadioField, q as RootDataWithoutProps, S as SelectField, T as TextField, f as TextareaField } from './Config-25917005.js';
3
3
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
4
  import * as react from 'react';
5
5
  import { ReactNode, ReactElement, CSSProperties, SyntheticEvent } from 'react';
package/dist/index.js CHANGED
@@ -31095,6 +31095,20 @@ var import_react13 = require("react");
31095
31095
 
31096
31096
  // lib/resolve-component-data.ts
31097
31097
  init_react_import();
31098
+
31099
+ // lib/get-changed.ts
31100
+ init_react_import();
31101
+ var getChanged = (newItem, oldItem) => {
31102
+ return newItem ? Object.keys(newItem.props || {}).reduce((acc, item) => {
31103
+ const newItemProps = (newItem == null ? void 0 : newItem.props) || {};
31104
+ const oldItemProps = (oldItem == null ? void 0 : oldItem.props) || {};
31105
+ return __spreadProps(__spreadValues({}, acc), {
31106
+ [item]: oldItemProps[item] !== newItemProps[item]
31107
+ });
31108
+ }, {}) : {};
31109
+ };
31110
+
31111
+ // lib/resolve-component-data.ts
31098
31112
  var cache = { lastChange: {} };
31099
31113
  var resolveAllComponentData = (content, config, onResolveStart, onResolveEnd) => __async(void 0, null, function* () {
31100
31114
  return yield Promise.all(
@@ -31111,25 +31125,15 @@ var resolveAllComponentData = (content, config, onResolveStart, onResolveEnd) =>
31111
31125
  var resolveComponentData = (item, config, onResolveStart, onResolveEnd) => __async(void 0, null, function* () {
31112
31126
  const configForItem = config.components[item.type];
31113
31127
  if (configForItem.resolveData) {
31114
- let changed = Object.keys(item.props).reduce(
31115
- (acc, item2) => __spreadProps(__spreadValues({}, acc), { [item2]: true }),
31116
- {}
31117
- );
31118
- if (cache.lastChange[item.props.id]) {
31119
- const { item: oldItem, resolved } = cache.lastChange[item.props.id];
31120
- if (oldItem === item) {
31121
- return resolved;
31122
- }
31123
- Object.keys(item.props).forEach((propName) => {
31124
- if (oldItem.props[propName] === item.props[propName]) {
31125
- changed[propName] = false;
31126
- }
31127
- });
31128
+ const { item: oldItem = {}, resolved = {} } = cache.lastChange[item.props.id] || {};
31129
+ if (item && item === oldItem) {
31130
+ return resolved;
31128
31131
  }
31132
+ const changed = getChanged(item, oldItem);
31129
31133
  if (onResolveStart) {
31130
31134
  onResolveStart(item);
31131
31135
  }
31132
- const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, { changed });
31136
+ const { props: resolvedProps, readOnly = {} } = yield configForItem.resolveData(item, { changed, lastData: oldItem });
31133
31137
  const { readOnly: existingReadOnly = {} } = item || {};
31134
31138
  const newReadOnly = __spreadValues(__spreadValues({}, existingReadOnly), readOnly);
31135
31139
  const resolvedItem = __spreadProps(__spreadValues({}, item), {
@@ -31172,26 +31176,16 @@ var applyDynamicProps = (data, dynamicProps, rootData) => {
31172
31176
  init_react_import();
31173
31177
  var cache2 = {};
31174
31178
  var resolveRootData = (data, config) => __async(void 0, null, function* () {
31175
- var _a, _b;
31179
+ var _a, _b, _c, _d, _e;
31176
31180
  if (((_a = config.root) == null ? void 0 : _a.resolveData) && data.root.props) {
31177
- let changed = Object.keys(data.root.props).reduce(
31178
- (acc, item) => __spreadProps(__spreadValues({}, acc), { [item]: true }),
31179
- {}
31180
- );
31181
- if (cache2.lastChange) {
31182
- const { original, resolved } = cache2.lastChange;
31183
- if (original === data.root) {
31184
- return resolved;
31185
- }
31186
- Object.keys(data.root.props).forEach((propName) => {
31187
- if (original.props[propName] === data.root.props[propName]) {
31188
- changed[propName] = false;
31189
- }
31190
- });
31181
+ if (((_b = cache2.lastChange) == null ? void 0 : _b.original) === data.root) {
31182
+ return cache2.lastChange.resolved;
31191
31183
  }
31184
+ const changed = getChanged(data.root, (_c = cache2.lastChange) == null ? void 0 : _c.original);
31192
31185
  const rootWithProps = data.root;
31193
- const resolvedRoot = yield (_b = config.root) == null ? void 0 : _b.resolveData(rootWithProps, {
31194
- changed
31186
+ const resolvedRoot = yield (_e = config.root) == null ? void 0 : _e.resolveData(rootWithProps, {
31187
+ changed,
31188
+ lastData: ((_d = cache2.lastChange) == null ? void 0 : _d.original) || {}
31195
31189
  });
31196
31190
  cache2.lastChange = {
31197
31191
  original: data.root,
@@ -32321,7 +32315,7 @@ var InputOrGroup = (_a) => {
32321
32315
 
32322
32316
  // css-module:/home/runner/work/puck/puck/packages/core/components/Puck/components/Fields/styles.module.css#css-module
32323
32317
  init_react_import();
32324
- var styles_module_default14 = { "PuckFields": "_PuckFields_17k3p_1", "PuckFields-loadingOverlay": "_PuckFields-loadingOverlay_17k3p_6" };
32318
+ var styles_module_default14 = { "PuckFields": "_PuckFields_1o0if_1", "PuckFields--isLoading": "_PuckFields--isLoading_1o0if_6", "PuckFields-loadingOverlay": "_PuckFields-loadingOverlay_1o0if_10" };
32325
32319
 
32326
32320
  // components/Puck/components/Fields/index.tsx
32327
32321
  var import_react19 = require("react");
@@ -32334,13 +32328,73 @@ var DefaultFields = ({
32334
32328
  children,
32335
32329
  isLoading
32336
32330
  }) => {
32337
- return /* @__PURE__ */ (0, import_jsx_runtime25.jsxs)("div", { className: getClassName19(), children: [
32331
+ return /* @__PURE__ */ (0, import_jsx_runtime25.jsxs)("div", { className: getClassName19({ isLoading }), children: [
32338
32332
  children,
32339
32333
  isLoading && /* @__PURE__ */ (0, import_jsx_runtime25.jsx)("div", { className: getClassName19("loadingOverlay"), children: /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(import_react_spinners6.ClipLoader, { "aria-label": "loading" }) })
32340
32334
  ] });
32341
32335
  };
32336
+ var useResolvedFields = () => {
32337
+ var _a;
32338
+ const { selectedItem, state, config } = useAppContext();
32339
+ const { data } = state;
32340
+ const rootFields = ((_a = config.root) == null ? void 0 : _a.fields) || defaultPageFields;
32341
+ const componentConfig = selectedItem ? config.components[selectedItem.type] : null;
32342
+ const defaultFields = selectedItem ? componentConfig == null ? void 0 : componentConfig.fields : rootFields;
32343
+ const rootProps = data.root.props || data.root;
32344
+ const [lastSelectedData, setLastSelectedData] = (0, import_react19.useState)(
32345
+ {}
32346
+ );
32347
+ const [resolvedFields, setResolvedFields] = (0, import_react19.useState)(defaultFields || {});
32348
+ const [fieldsLoading, setFieldsLoading] = (0, import_react19.useState)(false);
32349
+ const defaultResolveFields = (_componentData, _params) => defaultFields;
32350
+ const componentData = selectedItem ? selectedItem : { props: rootProps, readOnly: data.root.readOnly };
32351
+ const resolveFields = (0, import_react19.useCallback)(
32352
+ (..._0) => __async(void 0, [..._0], function* (fields = {}) {
32353
+ var _a2, _b, _c;
32354
+ const lastData = ((_a2 = lastSelectedData.props) == null ? void 0 : _a2.id) === componentData.props.id ? lastSelectedData : {};
32355
+ const changed = getChanged(componentData, lastData);
32356
+ setLastSelectedData(componentData);
32357
+ if (selectedItem && (componentConfig == null ? void 0 : componentConfig.resolveFields)) {
32358
+ return yield componentConfig == null ? void 0 : componentConfig.resolveFields(
32359
+ componentData,
32360
+ {
32361
+ changed,
32362
+ fields,
32363
+ lastFields: resolvedFields,
32364
+ lastData,
32365
+ appState: state
32366
+ }
32367
+ );
32368
+ }
32369
+ if (!selectedItem && ((_b = config.root) == null ? void 0 : _b.resolveFields)) {
32370
+ return yield (_c = config.root) == null ? void 0 : _c.resolveFields(componentData, {
32371
+ changed,
32372
+ fields,
32373
+ lastFields: resolvedFields,
32374
+ lastData,
32375
+ appState: state
32376
+ });
32377
+ }
32378
+ return defaultResolveFields(componentData, {
32379
+ changed,
32380
+ fields,
32381
+ lastFields: resolvedFields,
32382
+ lastData
32383
+ });
32384
+ }),
32385
+ [data, config, componentData, selectedItem, resolvedFields, state]
32386
+ );
32387
+ (0, import_react19.useEffect)(() => {
32388
+ setFieldsLoading(true);
32389
+ resolveFields(defaultFields).then((fields) => {
32390
+ setResolvedFields(fields || {});
32391
+ setFieldsLoading(false);
32392
+ });
32393
+ }, [data, defaultFields]);
32394
+ return [resolvedFields, fieldsLoading];
32395
+ };
32342
32396
  var Fields = () => {
32343
- var _a, _b, _c, _d;
32397
+ var _a, _b;
32344
32398
  const {
32345
32399
  selectedItem,
32346
32400
  state,
@@ -32352,9 +32406,9 @@ var Fields = () => {
32352
32406
  } = useAppContext();
32353
32407
  const { data, ui } = state;
32354
32408
  const { itemSelector } = ui;
32355
- const rootFields = ((_a = config.root) == null ? void 0 : _a.fields) || defaultPageFields;
32356
- const fields = selectedItem ? ((_b = config.components[selectedItem.type]) == null ? void 0 : _b.fields) || {} : rootFields;
32357
- const isLoading = selectedItem ? (_c = componentState[selectedItem == null ? void 0 : selectedItem.props.id]) == null ? void 0 : _c.loading : (_d = componentState["puck-root"]) == null ? void 0 : _d.loading;
32409
+ const [fields, fieldsResolving] = useResolvedFields();
32410
+ const componentResolving = selectedItem ? (_a = componentState[selectedItem == null ? void 0 : selectedItem.props.id]) == null ? void 0 : _a.loading : (_b = componentState["puck-root"]) == null ? void 0 : _b.loading;
32411
+ const isLoading = fieldsResolving || componentResolving;
32358
32412
  const rootProps = data.root.props || data.root;
32359
32413
  const Wrapper = (0, import_react19.useMemo)(() => overrides.fields || DefaultFields, [overrides]);
32360
32414
  return /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(
@@ -32366,6 +32420,8 @@ var Fields = () => {
32366
32420
  },
32367
32421
  children: /* @__PURE__ */ (0, import_jsx_runtime25.jsx)(Wrapper, { isLoading, itemSelector, children: Object.keys(fields).map((fieldName) => {
32368
32422
  const field = fields[fieldName];
32423
+ if (!(field == null ? void 0 : field.type))
32424
+ return null;
32369
32425
  const onChange = (value, updatedUi) => {
32370
32426
  var _a2, _b2;
32371
32427
  let currentProps;
package/dist/rsc.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { C as Config, D as Data } from './Config-550396e1.js';
2
+ import { C as Config, D as Data } from './Config-25917005.js';
3
3
  import 'react';
4
4
 
5
5
  declare function Render<UserConfig extends Config = Config>({ config, data, }: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@measured/puck",
3
- "version": "0.15.0-canary.aedd401",
3
+ "version": "0.15.0-canary.dd7051e",
4
4
  "author": "Measured Corporation Ltd <hello@measured.co>",
5
5
  "repository": "measuredco/puck",
6
6
  "bugs": "https://github.com/measuredco/puck/issues",