@netlisian/softconfig 0.1.7 → 0.1.9

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.
@@ -99,7 +99,8 @@ __export(puck_exports, {
99
99
  useInspect: () => useInspect,
100
100
  useRemodel: () => useRemodel,
101
101
  useSetDefaultVersion: () => useSetDefaultVersion,
102
- useSoftConfig: () => useSoftConfig
102
+ useSoftConfig: () => useSoftConfig,
103
+ useSoftConfigStore: () => useSoftConfigStore
103
104
  });
104
105
  module.exports = __toCommonJS(puck_exports);
105
106
 
@@ -670,6 +671,15 @@ var createUseSoftConfig = () => {
670
671
  };
671
672
  };
672
673
  var useSoftConfig = createUseSoftConfig();
674
+ var useSoftConfigStore = () => {
675
+ const context = (0, import_react.useContext)(appStoreContext);
676
+ if (!context) {
677
+ throw new Error(
678
+ "useSoftConfigStore must be used inside a SoftConfigProvider."
679
+ );
680
+ }
681
+ return context;
682
+ };
673
683
 
674
684
  // src/puck/lib/apply-mapping.ts
675
685
  var import_react_fast_compare = __toESM(require("react-fast-compare"));
@@ -2289,25 +2299,6 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2289
2299
  if (!selectedItem || !itemSelector) {
2290
2300
  throw new Error("No item selected to build from.");
2291
2301
  }
2292
- puckDispatch({
2293
- type: "set",
2294
- state: (previous) => {
2295
- var _a;
2296
- return {
2297
- ui: __spreadProps(__spreadValues({}, previous.ui), {
2298
- itemSelector: null
2299
- }),
2300
- data: __spreadProps(__spreadValues({}, previous.data), {
2301
- root: __spreadProps(__spreadValues({}, previous.data.root), {
2302
- props: __spreadProps(__spreadValues({}, (_a = previous.data.root) == null ? void 0 : _a.props), {
2303
- _name: name || "New Soft Component"
2304
- })
2305
- })
2306
- // content: [{ ...selectedItem }],
2307
- })
2308
- };
2309
- }
2310
- });
2311
2302
  const config = __spreadValues({}, get().softConfig);
2312
2303
  const overrides = get().overrides;
2313
2304
  const buildConfig = builderConfig(
@@ -2353,10 +2344,21 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2353
2344
  }));
2354
2345
  requestAnimationFrame(
2355
2346
  () => puckDispatch({
2356
- type: "replaceRoot",
2357
- root: {
2358
- title: "Soft Component Builder",
2359
- _name: name || "New Soft Component"
2347
+ type: "set",
2348
+ state: (previous) => {
2349
+ var _a;
2350
+ return {
2351
+ ui: __spreadProps(__spreadValues({}, previous.ui), {
2352
+ itemSelector: null
2353
+ }),
2354
+ data: __spreadProps(__spreadValues({}, previous.data), {
2355
+ root: __spreadProps(__spreadValues({}, previous.data.root), {
2356
+ props: __spreadProps(__spreadValues({}, (_a = previous.data.root) == null ? void 0 : _a.props), {
2357
+ _name: name || "New Soft Component"
2358
+ })
2359
+ })
2360
+ })
2361
+ };
2360
2362
  }
2361
2363
  })
2362
2364
  );
@@ -2381,12 +2383,6 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2381
2383
  `Soft component "${softComponentName}" with version "${softComponentVersion}" not found.`
2382
2384
  );
2383
2385
  }
2384
- puckDispatch({
2385
- type: "setUi",
2386
- ui: (previous) => __spreadProps(__spreadValues({}, previous), {
2387
- itemSelector: void 0
2388
- })
2389
- });
2390
2386
  const { root, content } = softComponentToAppState(
2391
2387
  softComponent,
2392
2388
  softComponentName,
@@ -2422,36 +2418,45 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2422
2418
  return components;
2423
2419
  }
2424
2420
  );
2425
- puckDispatch({
2426
- type: "setData",
2427
- data: (prevData) => ({
2428
- root: __spreadProps(__spreadValues({}, root), { _versions: versions }),
2429
- content: (0, import_puck4.walkTree)(__spreadValues({}, prevData), __spreadValues({}, config), (components) => {
2430
- const next = components.map((component) => __spreadProps(__spreadValues({}, component), {
2431
- props: __spreadValues({}, component.props)
2432
- }));
2433
- const index = next.findIndex(
2434
- (component) => component.props.id === selectedItem.props.id
2435
- );
2436
- if (index !== -1) {
2437
- next.splice(
2438
- index,
2439
- 1,
2440
- ...decomposedComponents.map((component) => __spreadProps(__spreadValues({}, component), {
2441
- props: __spreadValues({}, component.props)
2442
- }))
2443
- );
2444
- }
2445
- return next;
2446
- }).content
2447
- })
2448
- });
2449
- requestAnimationFrame(
2450
- () => setEditVisibility(get().iframeDoc, {
2421
+ requestAnimationFrame(() => {
2422
+ puckDispatch({
2423
+ type: "set",
2424
+ state: (previous) => ({
2425
+ data: {
2426
+ root: __spreadProps(__spreadValues({}, root), { _versions: versions }),
2427
+ content: (0, import_puck4.walkTree)(
2428
+ __spreadValues({}, previous.data),
2429
+ __spreadValues({}, config),
2430
+ (components) => {
2431
+ const next = components.map((component) => __spreadProps(__spreadValues({}, component), {
2432
+ props: __spreadValues({}, component.props)
2433
+ }));
2434
+ const index = next.findIndex(
2435
+ (component) => component.props.id === selectedItem.props.id
2436
+ );
2437
+ if (index !== -1) {
2438
+ next.splice(
2439
+ index,
2440
+ 1,
2441
+ ...decomposedComponents.map((component) => __spreadProps(__spreadValues({}, component), {
2442
+ props: __spreadValues({}, component.props)
2443
+ }))
2444
+ );
2445
+ }
2446
+ return next;
2447
+ }
2448
+ ).content
2449
+ },
2450
+ ui: __spreadProps(__spreadValues({}, previous.ui), {
2451
+ itemSelector: null
2452
+ })
2453
+ })
2454
+ });
2455
+ setEditVisibility(get().iframeDoc, {
2451
2456
  mode: "remodel",
2452
2457
  editableIds
2453
- })
2454
- );
2458
+ });
2459
+ });
2455
2460
  set((s) => __spreadProps(__spreadValues({}, s), {
2456
2461
  storedConfig: config,
2457
2462
  softConfig: buildConfig,
@@ -2558,6 +2563,7 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2558
2563
  }),
2559
2564
  storedConfig: void 0,
2560
2565
  state: "inspecting",
2566
+ // Temporarily shift state to inspect() before finalizing back to ready
2561
2567
  originalHistory: []
2562
2568
  });
2563
2569
  });
@@ -2606,34 +2612,41 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2606
2612
  type: "setData",
2607
2613
  data: (data) => {
2608
2614
  return reconstructedTree(data);
2609
- }
2615
+ },
2616
+ recordHistory: true
2617
+ // Record this swap on the standard undo/redo stack
2610
2618
  });
2619
+ clearEditVisibility(get().iframeDoc);
2620
+ set((s) => __spreadProps(__spreadValues({}, s), {
2621
+ state: "ready",
2622
+ setItemSelector: void 0,
2623
+ setOriginalItem: void 0,
2624
+ editingComponent: null,
2625
+ editingComponentId: null,
2626
+ editableComponentIds: /* @__PURE__ */ new Set()
2627
+ }));
2611
2628
  });
2612
- requestAnimationFrame(() => clearEditVisibility(get().iframeDoc));
2613
- set((s) => __spreadProps(__spreadValues({}, s), {
2614
- state: "ready",
2615
- setItemSelector: void 0,
2616
- setOriginalItem: void 0,
2617
- editingComponent: null,
2618
- editingComponentId: null,
2619
- editableComponentIds: /* @__PURE__ */ new Set()
2620
- }));
2621
2629
  },
2622
2630
  cancel: (setHistories) => {
2623
2631
  const storedHistories = get().originalHistory;
2624
- requestAnimationFrame(() => setHistories([...storedHistories]));
2625
- requestAnimationFrame(() => clearEditVisibility(get().iframeDoc));
2626
2632
  set((s) => __spreadProps(__spreadValues({}, s), {
2627
- softConfig: get().storedConfig || initialConfig,
2628
- storedConfig: void 0,
2629
- originalHistory: [],
2630
- itemSelector: null,
2631
- originalItem: null,
2632
- state: "ready",
2633
- editingComponent: null,
2634
- editingComponentId: null,
2635
- editableComponentIds: /* @__PURE__ */ new Set()
2633
+ state: "cancelling"
2636
2634
  }));
2635
+ setHistories([...storedHistories]);
2636
+ requestAnimationFrame(() => {
2637
+ clearEditVisibility(get().iframeDoc);
2638
+ set((s) => __spreadProps(__spreadValues({}, s), {
2639
+ softConfig: get().storedConfig || initialConfig,
2640
+ storedConfig: void 0,
2641
+ originalHistory: [],
2642
+ itemSelector: null,
2643
+ originalItem: null,
2644
+ state: "ready",
2645
+ editingComponent: null,
2646
+ editingComponentId: null,
2647
+ editableComponentIds: /* @__PURE__ */ new Set()
2648
+ }));
2649
+ });
2637
2650
  },
2638
2651
  compose: (appState, componentName, editedItem, displayName, category) => {
2639
2652
  if (!componentName) {
@@ -3287,47 +3300,6 @@ var createSoftConfigStore = (hardConfig = { components: {} }, softComponents = {
3287
3300
 
3288
3301
  // src/puck/context/storeProvider.tsx
3289
3302
  var import_react5 = require("react");
3290
-
3291
- // src/puck/lib/notify.ts
3292
- var customHandler = null;
3293
- var defaultHandler = (message, type) => {
3294
- if (type === "error") {
3295
- console.error(`[Error] ${message}`);
3296
- } else {
3297
- console.log(`[Success] ${message}`);
3298
- }
3299
- };
3300
- var setNotificationHandler = (handler) => {
3301
- customHandler = handler;
3302
- };
3303
- var notify = {
3304
- error: (message) => {
3305
- const handler = customHandler || defaultHandler;
3306
- handler(message, "error");
3307
- },
3308
- success: (message) => {
3309
- const handler = customHandler || defaultHandler;
3310
- handler(message, "success");
3311
- }
3312
- };
3313
-
3314
- // src/puck/lib/action-callback.ts
3315
- var createActionCallback = (validateAction, undo) => {
3316
- return (action) => {
3317
- if (!undo) {
3318
- return;
3319
- }
3320
- const isValid = validateAction(action);
3321
- if (!isValid) {
3322
- notify.error(
3323
- "Editing outside the soft component is not allowed when you are editing component definition."
3324
- );
3325
- requestAnimationFrame(() => undo());
3326
- }
3327
- };
3328
- };
3329
-
3330
- // src/puck/context/storeProvider.tsx
3331
3303
  var import_jsx_runtime7 = require("react/jsx-runtime");
3332
3304
  var SoftConfigProvider = ({
3333
3305
  children,
@@ -3339,8 +3311,8 @@ var SoftConfigProvider = ({
3339
3311
  onActions,
3340
3312
  useVersioning = false
3341
3313
  }) => {
3342
- const store = value != null ? value : (0, import_react5.useMemo)(
3343
- () => createSoftConfigStore(
3314
+ const store = (0, import_react5.useMemo)(
3315
+ () => value != null ? value : createSoftConfigStore(
3344
3316
  hardConfig,
3345
3317
  softComponents,
3346
3318
  overrides,
@@ -3348,55 +3320,54 @@ var SoftConfigProvider = ({
3348
3320
  useVersioning,
3349
3321
  customFields
3350
3322
  ),
3351
- [
3352
- hardConfig,
3353
- softComponents,
3354
- overrides,
3355
- onActions,
3356
- useVersioning,
3357
- customFields
3358
- ]
3323
+ // eslint-disable-next-line react-hooks/exhaustive-deps
3324
+ [value]
3325
+ // Intentionally omitting the rest: createSoftConfigStore params are
3326
+ // treated as initialisation-time values. If callers need to react to
3327
+ // prop changes they should pass a new `value` store instead.
3359
3328
  );
3360
3329
  const [softConfig, setSoftConfig] = (0, import_react5.useState)(
3361
3330
  () => store.getState().softConfig
3362
3331
  );
3363
- const [internalSoftComponents, setSoftComponents] = (0, import_react5.useState)(
3364
- () => store.getState().softComponents
3365
- );
3366
- const [actionGuard, setActionGuard] = (0, import_react5.useState)(
3367
- (action) => null
3368
- );
3369
- (0, import_react5.useEffect)(() => {
3370
- const unsubscribe = store.subscribe(() => {
3371
- setSoftConfig(store.getState().softConfig);
3372
- setSoftComponents(store.getState().softComponents);
3373
- });
3374
- return () => {
3375
- unsubscribe();
3376
- };
3377
- }, [store]);
3378
3332
  (0, import_react5.useEffect)(() => {
3379
- const unsubscribe = store.subscribe((state, prevState) => {
3380
- if (prevState && prevState.undoFn === state.undoFn) {
3381
- return;
3333
+ let prev = store.getState().softConfig;
3334
+ const unsubscribe = store.subscribe((state) => {
3335
+ if (state.softConfig !== prev) {
3336
+ prev = state.softConfig;
3337
+ setSoftConfig(state.softConfig);
3382
3338
  }
3383
- setActionGuard(
3384
- () => createActionCallback(
3385
- state.validateAction,
3386
- state.undoFn
3387
- )
3388
- );
3389
3339
  });
3390
- return () => {
3391
- unsubscribe();
3392
- };
3393
- }, [store.getState().undoFn]);
3394
- return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(appStoreContext.Provider, { value: store, children: children(softConfig, internalSoftComponents, actionGuard) });
3340
+ return unsubscribe;
3341
+ }, [store]);
3342
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(appStoreContext.Provider, { value: store, children: children(softConfig) });
3395
3343
  };
3396
3344
 
3397
3345
  // src/puck/actions/useBuild.tsx
3398
3346
  var import_puck5 = require("@measured/puck");
3399
3347
 
3348
+ // src/puck/lib/notify.ts
3349
+ var customHandler = null;
3350
+ var defaultHandler = (message, type) => {
3351
+ if (type === "error") {
3352
+ console.error(`[Error] ${message}`);
3353
+ } else {
3354
+ console.log(`[Success] ${message}`);
3355
+ }
3356
+ };
3357
+ var setNotificationHandler = (handler) => {
3358
+ customHandler = handler;
3359
+ };
3360
+ var notify = {
3361
+ error: (message) => {
3362
+ const handler = customHandler || defaultHandler;
3363
+ handler(message, "error");
3364
+ },
3365
+ success: (message) => {
3366
+ const handler = customHandler || defaultHandler;
3367
+ handler(message, "success");
3368
+ }
3369
+ };
3370
+
3400
3371
  // src/puck/hooks/useActionEvent.ts
3401
3372
  var import_react6 = require("react");
3402
3373
  var useActionEvent = () => {
@@ -3815,7 +3786,6 @@ var usePublish = () => {
3815
3786
  // src/puck/overrides/Header.tsx
3816
3787
  var import_jsx_runtime8 = require("react/jsx-runtime");
3817
3788
  var getClassName2 = get_class_name_factory_default("Header", Header_module_default);
3818
- var usePuck = (0, import_puck13.createUsePuck)();
3819
3789
  var Header = ({
3820
3790
  onPublish,
3821
3791
  children
@@ -3865,7 +3835,7 @@ var ActionBar_module_default = { "ActionBar": "_ActionBar_pvuie_5", "ActionBar-l
3865
3835
  var import_shallow = require("zustand/shallow");
3866
3836
  var import_jsx_runtime9 = require("react/jsx-runtime");
3867
3837
  var getClassName3 = get_class_name_factory_default("ActionBar", ActionBar_module_default);
3868
- var usePuck2 = (0, import_puck14.createUsePuck)();
3838
+ var usePuck = (0, import_puck14.createUsePuck)();
3869
3839
  var ActionBarOverride = (props) => {
3870
3840
  var _a, _b;
3871
3841
  const { handleBuild } = useBuild(props.label ? props.label + " Soft Component" : "New Soft Component");
@@ -3874,10 +3844,10 @@ var ActionBarOverride = (props) => {
3874
3844
  const overrides = useSoftConfig((s) => s.overrides);
3875
3845
  const softComponents = useSoftConfig((s) => s.softComponents, import_shallow.shallow);
3876
3846
  const editableIds = useSoftConfig((s) => s.editableComponentIds);
3877
- const selectedItem = usePuck2((s) => s.selectedItem);
3878
- const rootProps = usePuck2((s) => s.appState.data.root.props);
3847
+ const selectedItem = usePuck((s) => s.selectedItem);
3848
+ const rootProps = usePuck((s) => s.appState.data.root.props);
3879
3849
  const status = useSoftConfig((s) => s.state);
3880
- const itemSelector = usePuck2((s) => s.appState.ui.itemSelector);
3850
+ const itemSelector = usePuck((s) => s.appState.ui.itemSelector);
3881
3851
  const softKeys = Object.keys(softComponents);
3882
3852
  const key = (0, import_react9.useMemo)(() => {
3883
3853
  const selectedType = selectedItem == null ? void 0 : selectedItem.type;
@@ -4032,14 +4002,14 @@ var Modal = ({
4032
4002
  var import_shallow2 = require("zustand/shallow");
4033
4003
  var import_jsx_runtime11 = require("react/jsx-runtime");
4034
4004
  var getClassName5 = get_class_name_factory_default("DrawerItem", DrawerItem_module_default);
4035
- var usePuck3 = (0, import_puck15.createUsePuck)();
4005
+ var usePuck2 = (0, import_puck15.createUsePuck)();
4036
4006
  var DrawerItem = (props) => {
4037
4007
  const componentMeta = useSoftConfig((s) => s.softComponents[props.name]);
4038
4008
  const displayName = props.label || (componentMeta == null ? void 0 : componentMeta.name) || props.name;
4039
4009
  const softComponents = new Set(
4040
4010
  Object.keys(useSoftConfig((s) => s.softComponents, import_shallow2.shallow))
4041
4011
  );
4042
- const getPermissions = usePuck3((s) => s.getPermissions);
4012
+ const getPermissions = usePuck2((s) => s.getPermissions);
4043
4013
  const insertAllowed = getPermissions({ type: props.name }).insert;
4044
4014
  const removeSoftComponentVersion = useSoftConfig(
4045
4015
  (s) => s.removeSoftComponentVersion
@@ -4265,7 +4235,7 @@ var Drawer_module_default = { "Drawer": "_Drawer_12zq5_1", "Drawer-category": "_
4265
4235
  var import_jsx_runtime12 = require("react/jsx-runtime");
4266
4236
  var getClassName6 = get_class_name_factory_default("Drawer", Drawer_module_default);
4267
4237
  var getCategoryClassName = get_class_name_factory_default("Drawer-category", Drawer_module_default);
4268
- var usePuck4 = (0, import_puck16.createUsePuck)();
4238
+ var usePuck3 = (0, import_puck16.createUsePuck)();
4269
4239
  var CategorySection = ({
4270
4240
  id,
4271
4241
  title,
@@ -4299,8 +4269,8 @@ var CategorySection = ({
4299
4269
  ] });
4300
4270
  var Drawer = (_props) => {
4301
4271
  var _a, _b;
4302
- const config = usePuck4((s) => s.config);
4303
- const getPermissions = usePuck4((s) => s.getPermissions);
4272
+ const config = usePuck3((s) => s.config);
4273
+ const getPermissions = usePuck3((s) => s.getPermissions);
4304
4274
  const categories = (_a = config.categories) != null ? _a : {};
4305
4275
  const categorised = new Set(
4306
4276
  Object.values(categories).flatMap((cat) => {
@@ -4373,11 +4343,11 @@ var Drawer = (_props) => {
4373
4343
  // src/puck/overrides/HeaderActions.tsx
4374
4344
  var import_puck17 = require("@measured/puck");
4375
4345
  var import_jsx_runtime13 = require("react/jsx-runtime");
4376
- var usePuck5 = (0, import_puck17.createUsePuck)();
4346
+ var usePuck4 = (0, import_puck17.createUsePuck)();
4377
4347
  var HeaderActions = ({ children }) => {
4378
4348
  const { handleComplete } = useComplete();
4379
4349
  const { handleCancel, canCancel } = useCancel();
4380
- const dispatch = usePuck5((s) => s.dispatch);
4350
+ const dispatch = usePuck4((s) => s.dispatch);
4381
4351
  const inspect = useSoftConfig((s) => s.builder.inspect);
4382
4352
  return /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(import_jsx_runtime13.Fragment, { children: canCancel ? /* @__PURE__ */ (0, import_jsx_runtime13.jsxs)(import_jsx_runtime13.Fragment, { children: [
4383
4353
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(import_puck17.Button, { onClick: handleCancel, children: "Cancel" }),
@@ -4403,6 +4373,22 @@ var HeaderActions = ({ children }) => {
4403
4373
  ] }) : children });
4404
4374
  };
4405
4375
 
4376
+ // src/puck/lib/action-callback.ts
4377
+ var createActionCallback = (validateAction, undo) => {
4378
+ return (action) => {
4379
+ if (!undo) {
4380
+ return;
4381
+ }
4382
+ const isValid = validateAction(action);
4383
+ if (!isValid) {
4384
+ notify.error(
4385
+ "Editing outside the soft component is not allowed when you are editing component definition."
4386
+ );
4387
+ requestAnimationFrame(() => undo());
4388
+ }
4389
+ };
4390
+ };
4391
+
4406
4392
  // src/puck/lib/dissolve-all-soft-components.ts
4407
4393
  function extractDependencies2(softComponents, componentName, version) {
4408
4394
  var _a, _b;
@@ -4637,5 +4623,6 @@ var resolveSoftConfig = (data, softComponents, config) => {
4637
4623
  useInspect,
4638
4624
  useRemodel,
4639
4625
  useSetDefaultVersion,
4640
- useSoftConfig
4626
+ useSoftConfig,
4627
+ useSoftConfigStore
4641
4628
  });