@measured/puck 0.21.0-canary.15fe8d60 → 0.21.0-canary.1d823fd9

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.
@@ -284,46 +284,11 @@ function walkTree(data, config, callbackFn) {
284
284
  };
285
285
  }
286
286
 
287
- // lib/migrate.ts
287
+ // reducer/index.ts
288
288
  init_react_import();
289
289
 
290
- // store/default-app-state.ts
291
- init_react_import();
292
-
293
- // components/ViewportControls/default-viewports.ts
290
+ // reducer/actions/set.ts
294
291
  init_react_import();
295
- var defaultViewports = [
296
- { width: 360, height: "auto", icon: "Smartphone", label: "Small" },
297
- { width: 768, height: "auto", icon: "Tablet", label: "Medium" },
298
- { width: 1280, height: "auto", icon: "Monitor", label: "Large" }
299
- ];
300
-
301
- // store/default-app-state.ts
302
- var defaultAppState = {
303
- data: { content: [], root: {}, zones: {} },
304
- ui: {
305
- leftSideBarVisible: true,
306
- rightSideBarVisible: true,
307
- arrayState: {},
308
- itemSelector: null,
309
- componentList: {},
310
- isDragging: false,
311
- previewMode: "edit",
312
- viewports: {
313
- current: {
314
- width: defaultViewports[0].width,
315
- height: defaultViewports[0].height || "auto"
316
- },
317
- options: [],
318
- controlsVisible: true
319
- },
320
- field: { focus: null }
321
- },
322
- indexes: {
323
- nodes: {},
324
- zones: {}
325
- }
326
- };
327
292
 
328
293
  // lib/data/walk-app-state.ts
329
294
  init_react_import();
@@ -512,6 +477,565 @@ function walkAppState(state, config, mapContent = (content) => content, mapNodeO
512
477
  });
513
478
  }
514
479
 
480
+ // reducer/actions/set.ts
481
+ var setAction = (state, action, appStore) => {
482
+ if (typeof action.state === "object") {
483
+ const newState = __spreadValues(__spreadValues({}, state), action.state);
484
+ if (action.state.indexes) {
485
+ return newState;
486
+ }
487
+ console.warn(
488
+ "`set` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
489
+ );
490
+ return walkAppState(newState, appStore.config);
491
+ }
492
+ return __spreadValues(__spreadValues({}, state), action.state(state));
493
+ };
494
+
495
+ // reducer/actions/insert.ts
496
+ init_react_import();
497
+
498
+ // lib/data/insert.ts
499
+ init_react_import();
500
+ var insert = (list, index, item) => {
501
+ const result = Array.from(list || []);
502
+ result.splice(index, 0, item);
503
+ return result;
504
+ };
505
+
506
+ // lib/generate-id.ts
507
+ init_react_import();
508
+ import { v4 as uuidv4 } from "uuid";
509
+ var generateId = (type) => type ? `${type}-${uuidv4()}` : uuidv4();
510
+
511
+ // lib/data/get-ids-for-parent.ts
512
+ init_react_import();
513
+ var getIdsForParent = (zoneCompound, state) => {
514
+ const [parentId] = zoneCompound.split(":");
515
+ const node = state.indexes.nodes[parentId];
516
+ return ((node == null ? void 0 : node.path) || []).map((p) => p.split(":")[0]);
517
+ };
518
+
519
+ // lib/data/populate-ids.ts
520
+ init_react_import();
521
+ var populateIds = (data, config, override = false) => {
522
+ const id = generateId(data.type);
523
+ return walkTree(
524
+ __spreadProps(__spreadValues({}, data), {
525
+ props: override ? __spreadProps(__spreadValues({}, data.props), { id }) : __spreadValues({}, data.props)
526
+ }),
527
+ config,
528
+ (contents) => contents.map((item) => {
529
+ const id2 = generateId(item.type);
530
+ return __spreadProps(__spreadValues({}, item), {
531
+ props: override ? __spreadProps(__spreadValues({}, item.props), { id: id2 }) : __spreadValues({ id: id2 }, item.props)
532
+ });
533
+ })
534
+ );
535
+ };
536
+
537
+ // reducer/actions/insert.ts
538
+ function insertAction(state, action, appStore) {
539
+ const id = action.id || generateId(action.componentType);
540
+ const emptyComponentData = populateIds(
541
+ {
542
+ type: action.componentType,
543
+ props: __spreadProps(__spreadValues({}, appStore.config.components[action.componentType].defaultProps || {}), {
544
+ id
545
+ })
546
+ },
547
+ appStore.config
548
+ );
549
+ const [parentId] = action.destinationZone.split(":");
550
+ const idsInPath = getIdsForParent(action.destinationZone, state);
551
+ return walkAppState(
552
+ state,
553
+ appStore.config,
554
+ (content, zoneCompound) => {
555
+ if (zoneCompound === action.destinationZone) {
556
+ return insert(
557
+ content || [],
558
+ action.destinationIndex,
559
+ emptyComponentData
560
+ );
561
+ }
562
+ return content;
563
+ },
564
+ (childItem, path) => {
565
+ if (childItem.props.id === id || childItem.props.id === parentId) {
566
+ return childItem;
567
+ } else if (idsInPath.includes(childItem.props.id)) {
568
+ return childItem;
569
+ } else if (path.includes(action.destinationZone)) {
570
+ return childItem;
571
+ }
572
+ return null;
573
+ }
574
+ );
575
+ }
576
+
577
+ // reducer/actions/replace.ts
578
+ init_react_import();
579
+ var replaceAction = (state, action, appStore) => {
580
+ const [parentId] = action.destinationZone.split(":");
581
+ const idsInPath = getIdsForParent(action.destinationZone, state);
582
+ const originalId = state.indexes.zones[action.destinationZone].contentIds[action.destinationIndex];
583
+ const idChanged = originalId !== action.data.props.id;
584
+ if (idChanged) {
585
+ throw new Error(
586
+ `Can't change the id during a replace action. Please us "remove" and "insert" to define a new node.`
587
+ );
588
+ }
589
+ const newSlotIds = [];
590
+ const data = walkTree(action.data, appStore.config, (contents, opts) => {
591
+ newSlotIds.push(`${opts.parentId}:${opts.propName}`);
592
+ return contents.map((item) => {
593
+ const id = generateId(item.type);
594
+ return __spreadProps(__spreadValues({}, item), {
595
+ props: __spreadValues({ id }, item.props)
596
+ });
597
+ });
598
+ });
599
+ const stateWithDeepSlotsRemoved = __spreadValues({}, state);
600
+ Object.keys(state.indexes.zones).forEach((zoneCompound) => {
601
+ const id = zoneCompound.split(":")[0];
602
+ if (id === originalId) {
603
+ if (!newSlotIds.includes(zoneCompound)) {
604
+ delete stateWithDeepSlotsRemoved.indexes.zones[zoneCompound];
605
+ }
606
+ }
607
+ });
608
+ return walkAppState(
609
+ stateWithDeepSlotsRemoved,
610
+ appStore.config,
611
+ (content, zoneCompound) => {
612
+ const newContent = [...content];
613
+ if (zoneCompound === action.destinationZone) {
614
+ newContent[action.destinationIndex] = data;
615
+ }
616
+ return newContent;
617
+ },
618
+ (childItem, path) => {
619
+ const pathIds = path.map((p) => p.split(":")[0]);
620
+ if (childItem.props.id === data.props.id) {
621
+ return data;
622
+ } else if (childItem.props.id === parentId) {
623
+ return childItem;
624
+ } else if (idsInPath.indexOf(childItem.props.id) > -1) {
625
+ return childItem;
626
+ } else if (pathIds.indexOf(data.props.id) > -1) {
627
+ return childItem;
628
+ }
629
+ return null;
630
+ }
631
+ );
632
+ };
633
+
634
+ // reducer/actions/replace-root.ts
635
+ init_react_import();
636
+ var replaceRootAction = (state, action, appStore) => {
637
+ return walkAppState(
638
+ state,
639
+ appStore.config,
640
+ (content) => content,
641
+ (childItem) => {
642
+ if (childItem.props.id === "root") {
643
+ return __spreadProps(__spreadValues({}, childItem), {
644
+ props: __spreadValues(__spreadValues({}, childItem.props), action.root.props),
645
+ readOnly: action.root.readOnly
646
+ });
647
+ }
648
+ return childItem;
649
+ }
650
+ );
651
+ };
652
+
653
+ // reducer/actions/duplicate.ts
654
+ init_react_import();
655
+
656
+ // lib/data/get-item.ts
657
+ init_react_import();
658
+ function getItem(selector, state) {
659
+ var _a, _b;
660
+ const zone = (_a = state.indexes.zones) == null ? void 0 : _a[selector.zone || rootDroppableId];
661
+ return zone ? (_b = state.indexes.nodes[zone.contentIds[selector.index]]) == null ? void 0 : _b.data : void 0;
662
+ }
663
+
664
+ // reducer/actions/duplicate.ts
665
+ function duplicateAction(state, action, appStore) {
666
+ const item = getItem(
667
+ { index: action.sourceIndex, zone: action.sourceZone },
668
+ state
669
+ );
670
+ const idsInPath = getIdsForParent(action.sourceZone, state);
671
+ const newItem = __spreadProps(__spreadValues({}, item), {
672
+ props: __spreadProps(__spreadValues({}, item.props), {
673
+ id: generateId(item.type)
674
+ })
675
+ });
676
+ const modified = walkAppState(
677
+ state,
678
+ appStore.config,
679
+ (content, zoneCompound) => {
680
+ if (zoneCompound === action.sourceZone) {
681
+ return insert(content, action.sourceIndex + 1, item);
682
+ }
683
+ return content;
684
+ },
685
+ (childItem, path, index) => {
686
+ const zoneCompound = path[path.length - 1];
687
+ const parents = path.map((p) => p.split(":")[0]);
688
+ if (parents.indexOf(newItem.props.id) > -1) {
689
+ return __spreadProps(__spreadValues({}, childItem), {
690
+ props: __spreadProps(__spreadValues({}, childItem.props), {
691
+ id: generateId(childItem.type)
692
+ })
693
+ });
694
+ }
695
+ if (zoneCompound === action.sourceZone && index === action.sourceIndex + 1) {
696
+ return newItem;
697
+ }
698
+ const [sourceZoneParent] = action.sourceZone.split(":");
699
+ if (sourceZoneParent === childItem.props.id || idsInPath.indexOf(childItem.props.id) > -1) {
700
+ return childItem;
701
+ }
702
+ return null;
703
+ }
704
+ );
705
+ return __spreadProps(__spreadValues({}, modified), {
706
+ ui: __spreadProps(__spreadValues({}, modified.ui), {
707
+ itemSelector: {
708
+ index: action.sourceIndex + 1,
709
+ zone: action.sourceZone
710
+ }
711
+ })
712
+ });
713
+ }
714
+
715
+ // reducer/actions/reorder.ts
716
+ init_react_import();
717
+
718
+ // reducer/actions/move.ts
719
+ init_react_import();
720
+
721
+ // lib/data/remove.ts
722
+ init_react_import();
723
+ var remove = (list, index) => {
724
+ const result = Array.from(list);
725
+ result.splice(index, 1);
726
+ return result;
727
+ };
728
+
729
+ // reducer/actions/move.ts
730
+ var moveAction = (state, action, appStore) => {
731
+ if (action.sourceZone === action.destinationZone && action.sourceIndex === action.destinationIndex) {
732
+ return state;
733
+ }
734
+ const item = getItem(
735
+ { zone: action.sourceZone, index: action.sourceIndex },
736
+ state
737
+ );
738
+ if (!item) return state;
739
+ const idsInSourcePath = getIdsForParent(action.sourceZone, state);
740
+ const idsInDestinationPath = getIdsForParent(action.destinationZone, state);
741
+ return walkAppState(
742
+ state,
743
+ appStore.config,
744
+ (content, zoneCompound) => {
745
+ if (zoneCompound === action.sourceZone && zoneCompound === action.destinationZone) {
746
+ return insert(
747
+ remove(content, action.sourceIndex),
748
+ action.destinationIndex,
749
+ item
750
+ );
751
+ } else if (zoneCompound === action.sourceZone) {
752
+ return remove(content, action.sourceIndex);
753
+ } else if (zoneCompound === action.destinationZone) {
754
+ return insert(content, action.destinationIndex, item);
755
+ }
756
+ return content;
757
+ },
758
+ (childItem, path) => {
759
+ const [sourceZoneParent] = action.sourceZone.split(":");
760
+ const [destinationZoneParent] = action.destinationZone.split(":");
761
+ const childId = childItem.props.id;
762
+ if (sourceZoneParent === childId || destinationZoneParent === childId || item.props.id === childId || idsInSourcePath.indexOf(childId) > -1 || idsInDestinationPath.indexOf(childId) > -1 || path.includes(action.destinationZone)) {
763
+ return childItem;
764
+ }
765
+ return null;
766
+ }
767
+ );
768
+ };
769
+
770
+ // reducer/actions/reorder.ts
771
+ var reorderAction = (state, action, appStore) => {
772
+ return moveAction(
773
+ state,
774
+ {
775
+ type: "move",
776
+ sourceIndex: action.sourceIndex,
777
+ sourceZone: action.destinationZone,
778
+ destinationIndex: action.destinationIndex,
779
+ destinationZone: action.destinationZone
780
+ },
781
+ appStore
782
+ );
783
+ };
784
+
785
+ // reducer/actions/remove.ts
786
+ init_react_import();
787
+ var removeAction = (state, action, appStore) => {
788
+ const item = getItem({ index: action.index, zone: action.zone }, state);
789
+ const nodesToDelete = Object.entries(state.indexes.nodes).reduce(
790
+ (acc, [nodeId, nodeData]) => {
791
+ const pathIds = nodeData.path.map((p) => p.split(":")[0]);
792
+ if (pathIds.includes(item.props.id)) {
793
+ return [...acc, nodeId];
794
+ }
795
+ return acc;
796
+ },
797
+ [item.props.id]
798
+ );
799
+ const newState = walkAppState(
800
+ state,
801
+ appStore.config,
802
+ (content, zoneCompound) => {
803
+ if (zoneCompound === action.zone) {
804
+ return remove(content, action.index);
805
+ }
806
+ return content;
807
+ }
808
+ );
809
+ Object.keys(newState.data.zones || {}).forEach((zoneCompound) => {
810
+ const parentId = zoneCompound.split(":")[0];
811
+ if (nodesToDelete.includes(parentId) && newState.data.zones) {
812
+ delete newState.data.zones[zoneCompound];
813
+ }
814
+ });
815
+ Object.keys(newState.indexes.zones).forEach((zoneCompound) => {
816
+ const parentId = zoneCompound.split(":")[0];
817
+ if (nodesToDelete.includes(parentId)) {
818
+ delete newState.indexes.zones[zoneCompound];
819
+ }
820
+ });
821
+ nodesToDelete.forEach((id) => {
822
+ delete newState.indexes.nodes[id];
823
+ });
824
+ return newState;
825
+ };
826
+
827
+ // reducer/actions/register-zone.ts
828
+ init_react_import();
829
+
830
+ // lib/data/setup-zone.ts
831
+ init_react_import();
832
+ var setupZone = (data, zoneKey) => {
833
+ if (zoneKey === rootDroppableId) {
834
+ return data;
835
+ }
836
+ const newData = __spreadProps(__spreadValues({}, data), {
837
+ zones: data.zones ? __spreadValues({}, data.zones) : {}
838
+ });
839
+ newData.zones[zoneKey] = newData.zones[zoneKey] || [];
840
+ return newData;
841
+ };
842
+
843
+ // reducer/actions/register-zone.ts
844
+ var zoneCache = {};
845
+ function registerZoneAction(state, action) {
846
+ if (zoneCache[action.zone]) {
847
+ return __spreadProps(__spreadValues({}, state), {
848
+ data: __spreadProps(__spreadValues({}, state.data), {
849
+ zones: __spreadProps(__spreadValues({}, state.data.zones), {
850
+ [action.zone]: zoneCache[action.zone]
851
+ })
852
+ }),
853
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
854
+ zones: __spreadProps(__spreadValues({}, state.indexes.zones), {
855
+ [action.zone]: __spreadProps(__spreadValues({}, state.indexes.zones[action.zone]), {
856
+ contentIds: zoneCache[action.zone].map((item) => item.props.id),
857
+ type: "dropzone"
858
+ })
859
+ })
860
+ })
861
+ });
862
+ }
863
+ return __spreadProps(__spreadValues({}, state), { data: setupZone(state.data, action.zone) });
864
+ }
865
+ function unregisterZoneAction(state, action) {
866
+ const _zones = __spreadValues({}, state.data.zones || {});
867
+ const zoneIndex = __spreadValues({}, state.indexes.zones || {});
868
+ if (_zones[action.zone]) {
869
+ zoneCache[action.zone] = _zones[action.zone];
870
+ delete _zones[action.zone];
871
+ }
872
+ delete zoneIndex[action.zone];
873
+ return __spreadProps(__spreadValues({}, state), {
874
+ data: __spreadProps(__spreadValues({}, state.data), {
875
+ zones: _zones
876
+ }),
877
+ indexes: __spreadProps(__spreadValues({}, state.indexes), {
878
+ zones: zoneIndex
879
+ })
880
+ });
881
+ }
882
+
883
+ // reducer/actions/set-data.ts
884
+ init_react_import();
885
+ var setDataAction = (state, action, appStore) => {
886
+ if (typeof action.data === "object") {
887
+ console.warn(
888
+ "`setData` is expensive and may cause unnecessary re-renders. Consider using a more atomic action instead."
889
+ );
890
+ return walkAppState(
891
+ __spreadProps(__spreadValues({}, state), {
892
+ data: __spreadValues(__spreadValues({}, state.data), action.data)
893
+ }),
894
+ appStore.config
895
+ );
896
+ }
897
+ return walkAppState(
898
+ __spreadProps(__spreadValues({}, state), {
899
+ data: __spreadValues(__spreadValues({}, state.data), action.data(state.data))
900
+ }),
901
+ appStore.config
902
+ );
903
+ };
904
+
905
+ // reducer/actions/set-ui.ts
906
+ init_react_import();
907
+ var setUiAction = (state, action) => {
908
+ if (typeof action.ui === "object") {
909
+ return __spreadProps(__spreadValues({}, state), {
910
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui)
911
+ });
912
+ }
913
+ return __spreadProps(__spreadValues({}, state), {
914
+ ui: __spreadValues(__spreadValues({}, state.ui), action.ui(state.ui))
915
+ });
916
+ };
917
+
918
+ // lib/data/make-state-public.ts
919
+ init_react_import();
920
+ var makeStatePublic = (state) => {
921
+ const { data, ui } = state;
922
+ return { data, ui };
923
+ };
924
+
925
+ // reducer/actions.tsx
926
+ init_react_import();
927
+
928
+ // reducer/index.ts
929
+ function storeInterceptor(reducer, record, onAction) {
930
+ return (state, action) => {
931
+ const newAppState = reducer(state, action);
932
+ const isValidType = ![
933
+ "registerZone",
934
+ "unregisterZone",
935
+ "setData",
936
+ "setUi",
937
+ "set"
938
+ ].includes(action.type);
939
+ if (typeof action.recordHistory !== "undefined" ? action.recordHistory : isValidType) {
940
+ if (record) record(newAppState);
941
+ }
942
+ onAction == null ? void 0 : onAction(action, makeStatePublic(newAppState), makeStatePublic(state));
943
+ return newAppState;
944
+ };
945
+ }
946
+ function createReducer({
947
+ record,
948
+ onAction,
949
+ appStore
950
+ }) {
951
+ return storeInterceptor(
952
+ (state, action) => {
953
+ if (action.type === "set") {
954
+ return setAction(state, action, appStore);
955
+ }
956
+ if (action.type === "insert") {
957
+ return insertAction(state, action, appStore);
958
+ }
959
+ if (action.type === "replace") {
960
+ return replaceAction(state, action, appStore);
961
+ }
962
+ if (action.type === "replaceRoot") {
963
+ return replaceRootAction(state, action, appStore);
964
+ }
965
+ if (action.type === "duplicate") {
966
+ return duplicateAction(state, action, appStore);
967
+ }
968
+ if (action.type === "reorder") {
969
+ return reorderAction(state, action, appStore);
970
+ }
971
+ if (action.type === "move") {
972
+ return moveAction(state, action, appStore);
973
+ }
974
+ if (action.type === "remove") {
975
+ return removeAction(state, action, appStore);
976
+ }
977
+ if (action.type === "registerZone") {
978
+ return registerZoneAction(state, action);
979
+ }
980
+ if (action.type === "unregisterZone") {
981
+ return unregisterZoneAction(state, action);
982
+ }
983
+ if (action.type === "setData") {
984
+ return setDataAction(state, action, appStore);
985
+ }
986
+ if (action.type === "setUi") {
987
+ return setUiAction(state, action);
988
+ }
989
+ return state;
990
+ },
991
+ record,
992
+ onAction
993
+ );
994
+ }
995
+
996
+ // lib/migrate.ts
997
+ init_react_import();
998
+
999
+ // store/default-app-state.ts
1000
+ init_react_import();
1001
+
1002
+ // components/ViewportControls/default-viewports.ts
1003
+ init_react_import();
1004
+ var defaultViewports = [
1005
+ { width: 360, height: "auto", icon: "Smartphone", label: "Small" },
1006
+ { width: 768, height: "auto", icon: "Tablet", label: "Medium" },
1007
+ { width: 1280, height: "auto", icon: "Monitor", label: "Large" },
1008
+ { width: "100%", height: "auto", icon: "FullWidth", label: "Full-width" }
1009
+ ];
1010
+
1011
+ // store/default-app-state.ts
1012
+ var defaultAppState = {
1013
+ data: { content: [], root: {}, zones: {} },
1014
+ ui: {
1015
+ leftSideBarVisible: true,
1016
+ rightSideBarVisible: true,
1017
+ arrayState: {},
1018
+ itemSelector: null,
1019
+ componentList: {},
1020
+ isDragging: false,
1021
+ previewMode: "edit",
1022
+ viewports: {
1023
+ current: {
1024
+ width: defaultViewports[0].width,
1025
+ height: defaultViewports[0].height || "auto"
1026
+ },
1027
+ options: [],
1028
+ controlsVisible: true
1029
+ },
1030
+ field: { focus: null },
1031
+ plugin: { current: null }
1032
+ },
1033
+ indexes: {
1034
+ nodes: {},
1035
+ zones: {}
1036
+ }
1037
+ };
1038
+
515
1039
  // lib/migrate.ts
516
1040
  var migrations = [
517
1041
  // Migrate root to root.props
@@ -815,19 +1339,6 @@ function resolveAllData(_0, _1) {
815
1339
  });
816
1340
  }
817
1341
 
818
- // lib/data/setup-zone.ts
819
- init_react_import();
820
- var setupZone = (data, zoneKey) => {
821
- if (zoneKey === rootDroppableId) {
822
- return data;
823
- }
824
- const newData = __spreadProps(__spreadValues({}, data), {
825
- zones: data.zones ? __spreadValues({}, data.zones) : {}
826
- });
827
- newData.zones[zoneKey] = newData.zones[zoneKey] || [];
828
- return newData;
829
- };
830
-
831
1342
  // lib/field-transforms/use-field-transforms.tsx
832
1343
  init_react_import();
833
1344
  import { useMemo } from "react";
@@ -947,8 +1458,15 @@ export {
947
1458
  walkField,
948
1459
  expandNode,
949
1460
  walkAppState,
1461
+ insert,
1462
+ generateId,
950
1463
  walkTree,
1464
+ populateIds,
1465
+ insertAction,
1466
+ getItem,
951
1467
  setupZone,
1468
+ makeStatePublic,
1469
+ createReducer,
952
1470
  defaultViewports,
953
1471
  getChanged,
954
1472
  resolveComponentData,