@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.
@@ -611,6 +611,15 @@ var createUseSoftConfig = () => {
611
611
  };
612
612
  };
613
613
  var useSoftConfig = createUseSoftConfig();
614
+ var useSoftConfigStore = () => {
615
+ const context = useContext(appStoreContext);
616
+ if (!context) {
617
+ throw new Error(
618
+ "useSoftConfigStore must be used inside a SoftConfigProvider."
619
+ );
620
+ }
621
+ return context;
622
+ };
614
623
 
615
624
  // src/puck/lib/apply-mapping.ts
616
625
  import equal from "react-fast-compare";
@@ -2230,25 +2239,6 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2230
2239
  if (!selectedItem || !itemSelector) {
2231
2240
  throw new Error("No item selected to build from.");
2232
2241
  }
2233
- puckDispatch({
2234
- type: "set",
2235
- state: (previous) => {
2236
- var _a;
2237
- return {
2238
- ui: __spreadProps(__spreadValues({}, previous.ui), {
2239
- itemSelector: null
2240
- }),
2241
- data: __spreadProps(__spreadValues({}, previous.data), {
2242
- root: __spreadProps(__spreadValues({}, previous.data.root), {
2243
- props: __spreadProps(__spreadValues({}, (_a = previous.data.root) == null ? void 0 : _a.props), {
2244
- _name: name || "New Soft Component"
2245
- })
2246
- })
2247
- // content: [{ ...selectedItem }],
2248
- })
2249
- };
2250
- }
2251
- });
2252
2242
  const config = __spreadValues({}, get().softConfig);
2253
2243
  const overrides = get().overrides;
2254
2244
  const buildConfig = builderConfig(
@@ -2294,10 +2284,21 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2294
2284
  }));
2295
2285
  requestAnimationFrame(
2296
2286
  () => puckDispatch({
2297
- type: "replaceRoot",
2298
- root: {
2299
- title: "Soft Component Builder",
2300
- _name: name || "New Soft Component"
2287
+ type: "set",
2288
+ state: (previous) => {
2289
+ var _a;
2290
+ return {
2291
+ ui: __spreadProps(__spreadValues({}, previous.ui), {
2292
+ itemSelector: null
2293
+ }),
2294
+ data: __spreadProps(__spreadValues({}, previous.data), {
2295
+ root: __spreadProps(__spreadValues({}, previous.data.root), {
2296
+ props: __spreadProps(__spreadValues({}, (_a = previous.data.root) == null ? void 0 : _a.props), {
2297
+ _name: name || "New Soft Component"
2298
+ })
2299
+ })
2300
+ })
2301
+ };
2301
2302
  }
2302
2303
  })
2303
2304
  );
@@ -2322,12 +2323,6 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2322
2323
  `Soft component "${softComponentName}" with version "${softComponentVersion}" not found.`
2323
2324
  );
2324
2325
  }
2325
- puckDispatch({
2326
- type: "setUi",
2327
- ui: (previous) => __spreadProps(__spreadValues({}, previous), {
2328
- itemSelector: void 0
2329
- })
2330
- });
2331
2326
  const { root, content } = softComponentToAppState(
2332
2327
  softComponent,
2333
2328
  softComponentName,
@@ -2363,36 +2358,45 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2363
2358
  return components;
2364
2359
  }
2365
2360
  );
2366
- puckDispatch({
2367
- type: "setData",
2368
- data: (prevData) => ({
2369
- root: __spreadProps(__spreadValues({}, root), { _versions: versions }),
2370
- content: walkTree3(__spreadValues({}, prevData), __spreadValues({}, config), (components) => {
2371
- const next = components.map((component) => __spreadProps(__spreadValues({}, component), {
2372
- props: __spreadValues({}, component.props)
2373
- }));
2374
- const index = next.findIndex(
2375
- (component) => component.props.id === selectedItem.props.id
2376
- );
2377
- if (index !== -1) {
2378
- next.splice(
2379
- index,
2380
- 1,
2381
- ...decomposedComponents.map((component) => __spreadProps(__spreadValues({}, component), {
2382
- props: __spreadValues({}, component.props)
2383
- }))
2384
- );
2385
- }
2386
- return next;
2387
- }).content
2388
- })
2389
- });
2390
- requestAnimationFrame(
2391
- () => setEditVisibility(get().iframeDoc, {
2361
+ requestAnimationFrame(() => {
2362
+ puckDispatch({
2363
+ type: "set",
2364
+ state: (previous) => ({
2365
+ data: {
2366
+ root: __spreadProps(__spreadValues({}, root), { _versions: versions }),
2367
+ content: walkTree3(
2368
+ __spreadValues({}, previous.data),
2369
+ __spreadValues({}, config),
2370
+ (components) => {
2371
+ const next = components.map((component) => __spreadProps(__spreadValues({}, component), {
2372
+ props: __spreadValues({}, component.props)
2373
+ }));
2374
+ const index = next.findIndex(
2375
+ (component) => component.props.id === selectedItem.props.id
2376
+ );
2377
+ if (index !== -1) {
2378
+ next.splice(
2379
+ index,
2380
+ 1,
2381
+ ...decomposedComponents.map((component) => __spreadProps(__spreadValues({}, component), {
2382
+ props: __spreadValues({}, component.props)
2383
+ }))
2384
+ );
2385
+ }
2386
+ return next;
2387
+ }
2388
+ ).content
2389
+ },
2390
+ ui: __spreadProps(__spreadValues({}, previous.ui), {
2391
+ itemSelector: null
2392
+ })
2393
+ })
2394
+ });
2395
+ setEditVisibility(get().iframeDoc, {
2392
2396
  mode: "remodel",
2393
2397
  editableIds
2394
- })
2395
- );
2398
+ });
2399
+ });
2396
2400
  set((s) => __spreadProps(__spreadValues({}, s), {
2397
2401
  storedConfig: config,
2398
2402
  softConfig: buildConfig,
@@ -2499,6 +2503,7 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2499
2503
  }),
2500
2504
  storedConfig: void 0,
2501
2505
  state: "inspecting",
2506
+ // Temporarily shift state to inspect() before finalizing back to ready
2502
2507
  originalHistory: []
2503
2508
  });
2504
2509
  });
@@ -2547,34 +2552,41 @@ var createBuildersSlice = (set, get, initialConfig) => ({
2547
2552
  type: "setData",
2548
2553
  data: (data) => {
2549
2554
  return reconstructedTree(data);
2550
- }
2555
+ },
2556
+ recordHistory: true
2557
+ // Record this swap on the standard undo/redo stack
2551
2558
  });
2559
+ clearEditVisibility(get().iframeDoc);
2560
+ set((s) => __spreadProps(__spreadValues({}, s), {
2561
+ state: "ready",
2562
+ setItemSelector: void 0,
2563
+ setOriginalItem: void 0,
2564
+ editingComponent: null,
2565
+ editingComponentId: null,
2566
+ editableComponentIds: /* @__PURE__ */ new Set()
2567
+ }));
2552
2568
  });
2553
- requestAnimationFrame(() => clearEditVisibility(get().iframeDoc));
2554
- set((s) => __spreadProps(__spreadValues({}, s), {
2555
- state: "ready",
2556
- setItemSelector: void 0,
2557
- setOriginalItem: void 0,
2558
- editingComponent: null,
2559
- editingComponentId: null,
2560
- editableComponentIds: /* @__PURE__ */ new Set()
2561
- }));
2562
2569
  },
2563
2570
  cancel: (setHistories) => {
2564
2571
  const storedHistories = get().originalHistory;
2565
- requestAnimationFrame(() => setHistories([...storedHistories]));
2566
- requestAnimationFrame(() => clearEditVisibility(get().iframeDoc));
2567
2572
  set((s) => __spreadProps(__spreadValues({}, s), {
2568
- softConfig: get().storedConfig || initialConfig,
2569
- storedConfig: void 0,
2570
- originalHistory: [],
2571
- itemSelector: null,
2572
- originalItem: null,
2573
- state: "ready",
2574
- editingComponent: null,
2575
- editingComponentId: null,
2576
- editableComponentIds: /* @__PURE__ */ new Set()
2573
+ state: "cancelling"
2577
2574
  }));
2575
+ setHistories([...storedHistories]);
2576
+ requestAnimationFrame(() => {
2577
+ clearEditVisibility(get().iframeDoc);
2578
+ set((s) => __spreadProps(__spreadValues({}, s), {
2579
+ softConfig: get().storedConfig || initialConfig,
2580
+ storedConfig: void 0,
2581
+ originalHistory: [],
2582
+ itemSelector: null,
2583
+ originalItem: null,
2584
+ state: "ready",
2585
+ editingComponent: null,
2586
+ editingComponentId: null,
2587
+ editableComponentIds: /* @__PURE__ */ new Set()
2588
+ }));
2589
+ });
2578
2590
  },
2579
2591
  compose: (appState, componentName, editedItem, displayName, category) => {
2580
2592
  if (!componentName) {
@@ -3228,47 +3240,6 @@ var createSoftConfigStore = (hardConfig = { components: {} }, softComponents = {
3228
3240
 
3229
3241
  // src/puck/context/storeProvider.tsx
3230
3242
  import { useEffect as useEffect2, useMemo as useMemo2, useState } from "react";
3231
-
3232
- // src/puck/lib/notify.ts
3233
- var customHandler = null;
3234
- var defaultHandler = (message, type) => {
3235
- if (type === "error") {
3236
- console.error(`[Error] ${message}`);
3237
- } else {
3238
- console.log(`[Success] ${message}`);
3239
- }
3240
- };
3241
- var setNotificationHandler = (handler) => {
3242
- customHandler = handler;
3243
- };
3244
- var notify = {
3245
- error: (message) => {
3246
- const handler = customHandler || defaultHandler;
3247
- handler(message, "error");
3248
- },
3249
- success: (message) => {
3250
- const handler = customHandler || defaultHandler;
3251
- handler(message, "success");
3252
- }
3253
- };
3254
-
3255
- // src/puck/lib/action-callback.ts
3256
- var createActionCallback = (validateAction, undo) => {
3257
- return (action) => {
3258
- if (!undo) {
3259
- return;
3260
- }
3261
- const isValid = validateAction(action);
3262
- if (!isValid) {
3263
- notify.error(
3264
- "Editing outside the soft component is not allowed when you are editing component definition."
3265
- );
3266
- requestAnimationFrame(() => undo());
3267
- }
3268
- };
3269
- };
3270
-
3271
- // src/puck/context/storeProvider.tsx
3272
3243
  import { jsx as jsx7 } from "react/jsx-runtime";
3273
3244
  var SoftConfigProvider = ({
3274
3245
  children,
@@ -3280,8 +3251,8 @@ var SoftConfigProvider = ({
3280
3251
  onActions,
3281
3252
  useVersioning = false
3282
3253
  }) => {
3283
- const store = value != null ? value : useMemo2(
3284
- () => createSoftConfigStore(
3254
+ const store = useMemo2(
3255
+ () => value != null ? value : createSoftConfigStore(
3285
3256
  hardConfig,
3286
3257
  softComponents,
3287
3258
  overrides,
@@ -3289,55 +3260,54 @@ var SoftConfigProvider = ({
3289
3260
  useVersioning,
3290
3261
  customFields
3291
3262
  ),
3292
- [
3293
- hardConfig,
3294
- softComponents,
3295
- overrides,
3296
- onActions,
3297
- useVersioning,
3298
- customFields
3299
- ]
3263
+ // eslint-disable-next-line react-hooks/exhaustive-deps
3264
+ [value]
3265
+ // Intentionally omitting the rest: createSoftConfigStore params are
3266
+ // treated as initialisation-time values. If callers need to react to
3267
+ // prop changes they should pass a new `value` store instead.
3300
3268
  );
3301
3269
  const [softConfig, setSoftConfig] = useState(
3302
3270
  () => store.getState().softConfig
3303
3271
  );
3304
- const [internalSoftComponents, setSoftComponents] = useState(
3305
- () => store.getState().softComponents
3306
- );
3307
- const [actionGuard, setActionGuard] = useState(
3308
- (action) => null
3309
- );
3310
- useEffect2(() => {
3311
- const unsubscribe = store.subscribe(() => {
3312
- setSoftConfig(store.getState().softConfig);
3313
- setSoftComponents(store.getState().softComponents);
3314
- });
3315
- return () => {
3316
- unsubscribe();
3317
- };
3318
- }, [store]);
3319
3272
  useEffect2(() => {
3320
- const unsubscribe = store.subscribe((state, prevState) => {
3321
- if (prevState && prevState.undoFn === state.undoFn) {
3322
- return;
3273
+ let prev = store.getState().softConfig;
3274
+ const unsubscribe = store.subscribe((state) => {
3275
+ if (state.softConfig !== prev) {
3276
+ prev = state.softConfig;
3277
+ setSoftConfig(state.softConfig);
3323
3278
  }
3324
- setActionGuard(
3325
- () => createActionCallback(
3326
- state.validateAction,
3327
- state.undoFn
3328
- )
3329
- );
3330
3279
  });
3331
- return () => {
3332
- unsubscribe();
3333
- };
3334
- }, [store.getState().undoFn]);
3335
- return /* @__PURE__ */ jsx7(appStoreContext.Provider, { value: store, children: children(softConfig, internalSoftComponents, actionGuard) });
3280
+ return unsubscribe;
3281
+ }, [store]);
3282
+ return /* @__PURE__ */ jsx7(appStoreContext.Provider, { value: store, children: children(softConfig) });
3336
3283
  };
3337
3284
 
3338
3285
  // src/puck/actions/useBuild.tsx
3339
3286
  import { createUsePuck as createUsePuck2 } from "@measured/puck";
3340
3287
 
3288
+ // src/puck/lib/notify.ts
3289
+ var customHandler = null;
3290
+ var defaultHandler = (message, type) => {
3291
+ if (type === "error") {
3292
+ console.error(`[Error] ${message}`);
3293
+ } else {
3294
+ console.log(`[Success] ${message}`);
3295
+ }
3296
+ };
3297
+ var setNotificationHandler = (handler) => {
3298
+ customHandler = handler;
3299
+ };
3300
+ var notify = {
3301
+ error: (message) => {
3302
+ const handler = customHandler || defaultHandler;
3303
+ handler(message, "error");
3304
+ },
3305
+ success: (message) => {
3306
+ const handler = customHandler || defaultHandler;
3307
+ handler(message, "success");
3308
+ }
3309
+ };
3310
+
3341
3311
  // src/puck/hooks/useActionEvent.ts
3342
3312
  import { useCallback } from "react";
3343
3313
  var useActionEvent = () => {
@@ -3713,7 +3683,7 @@ var useSetDefaultVersion = () => {
3713
3683
  };
3714
3684
 
3715
3685
  // src/puck/overrides/Header.tsx
3716
- import { Button, createUsePuck as createUsePuck10 } from "@measured/puck";
3686
+ import { Button } from "@measured/puck";
3717
3687
 
3718
3688
  // css-module:/home/osamu/Documents/netlisian-soft/packages/soft-config/src/puck/overrides/Header.module.css#css-module
3719
3689
  var Header_module_default = { "Header": "_Header_19oj9_1" };
@@ -3756,7 +3726,6 @@ var usePublish = () => {
3756
3726
  // src/puck/overrides/Header.tsx
3757
3727
  import { Fragment as Fragment3, jsx as jsx8, jsxs as jsxs2 } from "react/jsx-runtime";
3758
3728
  var getClassName2 = get_class_name_factory_default("Header", Header_module_default);
3759
- var usePuck = createUsePuck10();
3760
3729
  var Header = ({
3761
3730
  onPublish,
3762
3731
  children
@@ -3806,7 +3775,7 @@ var ActionBar_module_default = { "ActionBar": "_ActionBar_pvuie_5", "ActionBar-l
3806
3775
  import { shallow } from "zustand/shallow";
3807
3776
  import { Fragment as Fragment4, jsx as jsx9, jsxs as jsxs3 } from "react/jsx-runtime";
3808
3777
  var getClassName3 = get_class_name_factory_default("ActionBar", ActionBar_module_default);
3809
- var usePuck2 = createUsePuck11();
3778
+ var usePuck = createUsePuck11();
3810
3779
  var ActionBarOverride = (props) => {
3811
3780
  var _a, _b;
3812
3781
  const { handleBuild } = useBuild(props.label ? props.label + " Soft Component" : "New Soft Component");
@@ -3815,10 +3784,10 @@ var ActionBarOverride = (props) => {
3815
3784
  const overrides = useSoftConfig((s) => s.overrides);
3816
3785
  const softComponents = useSoftConfig((s) => s.softComponents, shallow);
3817
3786
  const editableIds = useSoftConfig((s) => s.editableComponentIds);
3818
- const selectedItem = usePuck2((s) => s.selectedItem);
3819
- const rootProps = usePuck2((s) => s.appState.data.root.props);
3787
+ const selectedItem = usePuck((s) => s.selectedItem);
3788
+ const rootProps = usePuck((s) => s.appState.data.root.props);
3820
3789
  const status = useSoftConfig((s) => s.state);
3821
- const itemSelector = usePuck2((s) => s.appState.ui.itemSelector);
3790
+ const itemSelector = usePuck((s) => s.appState.ui.itemSelector);
3822
3791
  const softKeys = Object.keys(softComponents);
3823
3792
  const key = useMemo3(() => {
3824
3793
  const selectedType = selectedItem == null ? void 0 : selectedItem.type;
@@ -3973,14 +3942,14 @@ var Modal = ({
3973
3942
  import { shallow as shallow2 } from "zustand/shallow";
3974
3943
  import { Fragment as Fragment5, jsx as jsx11, jsxs as jsxs4 } from "react/jsx-runtime";
3975
3944
  var getClassName5 = get_class_name_factory_default("DrawerItem", DrawerItem_module_default);
3976
- var usePuck3 = createUsePuck12();
3945
+ var usePuck2 = createUsePuck12();
3977
3946
  var DrawerItem = (props) => {
3978
3947
  const componentMeta = useSoftConfig((s) => s.softComponents[props.name]);
3979
3948
  const displayName = props.label || (componentMeta == null ? void 0 : componentMeta.name) || props.name;
3980
3949
  const softComponents = new Set(
3981
3950
  Object.keys(useSoftConfig((s) => s.softComponents, shallow2))
3982
3951
  );
3983
- const getPermissions = usePuck3((s) => s.getPermissions);
3952
+ const getPermissions = usePuck2((s) => s.getPermissions);
3984
3953
  const insertAllowed = getPermissions({ type: props.name }).insert;
3985
3954
  const removeSoftComponentVersion = useSoftConfig(
3986
3955
  (s) => s.removeSoftComponentVersion
@@ -4206,7 +4175,7 @@ var Drawer_module_default = { "Drawer": "_Drawer_12zq5_1", "Drawer-category": "_
4206
4175
  import { jsx as jsx12, jsxs as jsxs5 } from "react/jsx-runtime";
4207
4176
  var getClassName6 = get_class_name_factory_default("Drawer", Drawer_module_default);
4208
4177
  var getCategoryClassName = get_class_name_factory_default("Drawer-category", Drawer_module_default);
4209
- var usePuck4 = createUsePuck13();
4178
+ var usePuck3 = createUsePuck13();
4210
4179
  var CategorySection = ({
4211
4180
  id,
4212
4181
  title,
@@ -4240,8 +4209,8 @@ var CategorySection = ({
4240
4209
  ] });
4241
4210
  var Drawer = (_props) => {
4242
4211
  var _a, _b;
4243
- const config = usePuck4((s) => s.config);
4244
- const getPermissions = usePuck4((s) => s.getPermissions);
4212
+ const config = usePuck3((s) => s.config);
4213
+ const getPermissions = usePuck3((s) => s.getPermissions);
4245
4214
  const categories = (_a = config.categories) != null ? _a : {};
4246
4215
  const categorised = new Set(
4247
4216
  Object.values(categories).flatMap((cat) => {
@@ -4314,11 +4283,11 @@ var Drawer = (_props) => {
4314
4283
  // src/puck/overrides/HeaderActions.tsx
4315
4284
  import { Button as Button3, createUsePuck as createUsePuck14 } from "@measured/puck";
4316
4285
  import { Fragment as Fragment6, jsx as jsx13, jsxs as jsxs6 } from "react/jsx-runtime";
4317
- var usePuck5 = createUsePuck14();
4286
+ var usePuck4 = createUsePuck14();
4318
4287
  var HeaderActions = ({ children }) => {
4319
4288
  const { handleComplete } = useComplete();
4320
4289
  const { handleCancel, canCancel } = useCancel();
4321
- const dispatch = usePuck5((s) => s.dispatch);
4290
+ const dispatch = usePuck4((s) => s.dispatch);
4322
4291
  const inspect = useSoftConfig((s) => s.builder.inspect);
4323
4292
  return /* @__PURE__ */ jsx13(Fragment6, { children: canCancel ? /* @__PURE__ */ jsxs6(Fragment6, { children: [
4324
4293
  /* @__PURE__ */ jsx13(Button3, { onClick: handleCancel, children: "Cancel" }),
@@ -4344,6 +4313,22 @@ var HeaderActions = ({ children }) => {
4344
4313
  ] }) : children });
4345
4314
  };
4346
4315
 
4316
+ // src/puck/lib/action-callback.ts
4317
+ var createActionCallback = (validateAction, undo) => {
4318
+ return (action) => {
4319
+ if (!undo) {
4320
+ return;
4321
+ }
4322
+ const isValid = validateAction(action);
4323
+ if (!isValid) {
4324
+ notify.error(
4325
+ "Editing outside the soft component is not allowed when you are editing component definition."
4326
+ );
4327
+ requestAnimationFrame(() => undo());
4328
+ }
4329
+ };
4330
+ };
4331
+
4347
4332
  // src/puck/lib/dissolve-all-soft-components.ts
4348
4333
  function extractDependencies2(softComponents, componentName, version) {
4349
4334
  var _a, _b;
@@ -4577,5 +4562,6 @@ export {
4577
4562
  useInspect,
4578
4563
  useRemodel,
4579
4564
  useSetDefaultVersion,
4580
- useSoftConfig
4565
+ useSoftConfig,
4566
+ useSoftConfigStore
4581
4567
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlisian/softconfig",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",