@chaibuilder/sdk 2.0.0-beta.83 → 2.0.0-beta.84

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.
package/dist/core.cjs CHANGED
@@ -17,8 +17,8 @@ const reactIcons = require("@radix-ui/react-icons");
17
17
  const web = require("@react-hookz/web");
18
18
  const flagged = require("flagged");
19
19
  const runtime = require("@chaibuilder/runtime");
20
- const ChaiThemeFn = require("./ChaiThemeFn-DcE5RdLQ.cjs");
21
20
  const TreeModel = require("tree-model");
21
+ const ChaiThemeFn = require("./ChaiThemeFn-DcE5RdLQ.cjs");
22
22
  const reactHotkeysHook = require("react-hotkeys-hook");
23
23
  const lucideReact = require("lucide-react");
24
24
  const ReactQuill = require("react-quill");
@@ -586,142 +586,6 @@ const getDefaultStore = () => {
586
586
  const RESET = Symbol(
587
587
  ""
588
588
  );
589
- const getCached$1 = (c, m, k) => (m.has(k) ? m : m.set(k, c())).get(k);
590
- const cache1$2 = /* @__PURE__ */ new WeakMap();
591
- const memo2$1 = (create, dep1, dep2) => {
592
- const cache2 = getCached$1(() => /* @__PURE__ */ new WeakMap(), cache1$2, dep1);
593
- return getCached$1(create, cache2, dep2);
594
- };
595
- const cacheKeyForEmptyKeyExtractor = {};
596
- const isWritable = (atom2) => !!atom2.write;
597
- const isFunction = (x) => typeof x === "function";
598
- function splitAtom(arrAtom, keyExtractor) {
599
- return memo2$1(
600
- () => {
601
- const mappingCache = /* @__PURE__ */ new WeakMap();
602
- const getMapping = (arr, prev) => {
603
- let mapping = mappingCache.get(arr);
604
- if (mapping) {
605
- return mapping;
606
- }
607
- const prevMapping = prev && mappingCache.get(prev);
608
- const atomList = [];
609
- const keyList = [];
610
- arr.forEach((item, index) => {
611
- const key = index;
612
- keyList[index] = key;
613
- const cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)];
614
- if (cachedAtom) {
615
- atomList[index] = cachedAtom;
616
- return;
617
- }
618
- const read = (get) => {
619
- const prev2 = get(mappingAtom);
620
- const currArr = get(arrAtom);
621
- const mapping2 = getMapping(currArr, prev2 == null ? void 0 : prev2.arr);
622
- const index2 = mapping2.keyList.indexOf(key);
623
- if (index2 < 0 || index2 >= currArr.length) {
624
- const prevItem = arr[getMapping(arr).keyList.indexOf(key)];
625
- if (prevItem) {
626
- return prevItem;
627
- }
628
- throw new Error("splitAtom: index out of bounds for read");
629
- }
630
- return currArr[index2];
631
- };
632
- const write = (get, set, update) => {
633
- const prev2 = get(mappingAtom);
634
- const arr2 = get(arrAtom);
635
- const mapping2 = getMapping(arr2, prev2 == null ? void 0 : prev2.arr);
636
- const index2 = mapping2.keyList.indexOf(key);
637
- if (index2 < 0 || index2 >= arr2.length) {
638
- throw new Error("splitAtom: index out of bounds for write");
639
- }
640
- const nextItem = isFunction(update) ? update(arr2[index2]) : update;
641
- if (!Object.is(arr2[index2], nextItem)) {
642
- set(arrAtom, [
643
- ...arr2.slice(0, index2),
644
- nextItem,
645
- ...arr2.slice(index2 + 1)
646
- ]);
647
- }
648
- };
649
- atomList[index] = isWritable(arrAtom) ? atom(read, write) : atom(read);
650
- });
651
- if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every((x, i) => x === keyList[i])) {
652
- mapping = prevMapping;
653
- } else {
654
- mapping = { arr, atomList, keyList };
655
- }
656
- mappingCache.set(arr, mapping);
657
- return mapping;
658
- };
659
- const mappingAtom = atom((get) => {
660
- const prev = get(mappingAtom);
661
- const arr = get(arrAtom);
662
- const mapping = getMapping(arr, prev == null ? void 0 : prev.arr);
663
- return mapping;
664
- });
665
- mappingAtom.init = void 0;
666
- const splittedAtom = isWritable(arrAtom) ? atom(
667
- (get) => get(mappingAtom).atomList,
668
- (get, set, action) => {
669
- switch (action.type) {
670
- case "remove": {
671
- const index = get(splittedAtom).indexOf(action.atom);
672
- if (index >= 0) {
673
- const arr = get(arrAtom);
674
- set(arrAtom, [
675
- ...arr.slice(0, index),
676
- ...arr.slice(index + 1)
677
- ]);
678
- }
679
- break;
680
- }
681
- case "insert": {
682
- const index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
683
- if (index >= 0) {
684
- const arr = get(arrAtom);
685
- set(arrAtom, [
686
- ...arr.slice(0, index),
687
- action.value,
688
- ...arr.slice(index)
689
- ]);
690
- }
691
- break;
692
- }
693
- case "move": {
694
- const index1 = get(splittedAtom).indexOf(action.atom);
695
- const index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
696
- if (index1 >= 0 && index2 >= 0) {
697
- const arr = get(arrAtom);
698
- if (index1 < index2) {
699
- set(arrAtom, [
700
- ...arr.slice(0, index1),
701
- ...arr.slice(index1 + 1, index2),
702
- arr[index1],
703
- ...arr.slice(index2)
704
- ]);
705
- } else {
706
- set(arrAtom, [
707
- ...arr.slice(0, index2),
708
- arr[index1],
709
- ...arr.slice(index2, index1),
710
- ...arr.slice(index1 + 1)
711
- ]);
712
- }
713
- }
714
- break;
715
- }
716
- }
717
- }
718
- ) : atom((get) => get(mappingAtom).atomList);
719
- return splittedAtom;
720
- },
721
- arrAtom,
722
- cacheKeyForEmptyKeyExtractor
723
- );
724
- }
725
589
  const isPromiseLike$1 = (x) => typeof (x == null ? void 0 : x.then) === "function";
726
590
  function createJSONStorage(getStringStorage = () => {
727
591
  try {
@@ -1114,61 +978,10 @@ __publicField(Frame, "defaultProps", {
1114
978
  initialContent: '<!DOCTYPE html><html><head></head><body><div class="frame-root"></div></body></html>'
1115
979
  });
1116
980
  const ChaiFrame = React.forwardRef((props, ref) => /* @__PURE__ */ jsxRuntime.jsx(Frame, { ...props, forwardedRef: ref }));
1117
- const canAcceptChildBlock = (parentType, childType) => {
1118
- if (!parentType) return true;
1119
- const blockDefinition = runtime.getRegisteredChaiBlock(parentType);
1120
- if (!blockDefinition) return false;
1121
- return lodashEs.has(blockDefinition, "canAcceptBlock") ? blockDefinition.canAcceptBlock(childType) : false;
1122
- };
1123
- const canAddChildBlock = (parentType) => {
1124
- const blockDefinition = runtime.getRegisteredChaiBlock(parentType);
1125
- if (!blockDefinition) return false;
1126
- return lodashEs.has(blockDefinition, "canAcceptBlock");
1127
- };
1128
- const canBeNestedInside = (parentType, childType) => {
1129
- const blockDefinition = runtime.getRegisteredChaiBlock(childType);
1130
- if (!blockDefinition) return true;
1131
- return lodashEs.has(blockDefinition, "canBeNested") ? blockDefinition.canBeNested(parentType) : true;
1132
- };
1133
- const canDuplicateBlock = (type) => {
1134
- const blockDefinition = runtime.getRegisteredChaiBlock(type);
1135
- if (!blockDefinition) return true;
1136
- return lodashEs.has(blockDefinition, "canDuplicate") ? blockDefinition.canDuplicate() : true;
1137
- };
1138
- const canDeleteBlock = (type) => {
1139
- const blockDefinition = runtime.getRegisteredChaiBlock(type);
1140
- if (!blockDefinition) return true;
1141
- return lodashEs.has(blockDefinition, "canDelete") ? blockDefinition.canDelete() : true;
1142
- };
1143
- const canDropBlock = (_currentTree, { dragSource, dropTarget: dropTarget2 }) => {
1144
- const dragSourceType = lodashEs.get(dragSource, "data._type", "");
1145
- const dropTargetType = lodashEs.get(dropTarget2, "data._type", "");
1146
- return canAcceptChildBlock(dropTargetType, dragSourceType);
1147
- };
1148
- if (void 0) {
1149
- describe("canDropBlock Function", () => {
1150
- it('should return false if dragSourceType is "Slot"', () => {
1151
- const dragSource = { data: { _type: "Slot" } };
1152
- const dropTarget2 = { data: {} };
1153
- expect(canDropBlock({}, { dragSource, dropTarget: dropTarget2 })).toBe(true);
1154
- });
1155
- it("should return true if dropTargetType is empty", () => {
1156
- const dragSource = { data: { _type: "Box" } };
1157
- const dropTarget2 = { data: {} };
1158
- expect(canDropBlock({}, { dragSource, dropTarget: dropTarget2 })).toBe(true);
1159
- });
1160
- });
1161
- }
1162
981
  function duplicateBlocks(blocks, id, _parent) {
1163
982
  const children = lodashEs.filter(blocks, (c) => c._parent === id);
1164
983
  const newBlocks = [];
1165
984
  for (let i = 0; i < children.length; i++) {
1166
- const slots = getSlots(children[i]);
1167
- if (Object.keys(slots).length > 0) {
1168
- Object.keys(slots).forEach((key) => {
1169
- children[i][key] = `slot:${iconBase.generateUUID()}`;
1170
- });
1171
- }
1172
985
  if (lodashEs.filter(blocks, { _parent: children[i]._id }).length > 0) {
1173
986
  const newId = iconBase.generateUUID();
1174
987
  newBlocks.push({ ...children[i], oldId: children[i]._id, ...{ _id: newId, _parent } });
@@ -1233,15 +1046,21 @@ const getDuplicatedBlocks = (currentBlocks, id, newParentId = null) => {
1233
1046
  return lodashEs.omit(newBlock, ["global", "oldId"]);
1234
1047
  });
1235
1048
  };
1236
- const presentBlocksAtom = jotai.atom([]);
1237
- presentBlocksAtom.debugLabel = "presentBlocksAtom";
1049
+ const pageBlocksAtom = jotai.atom([]);
1050
+ pageBlocksAtom.debugLabel = "pageBlocksAtom";
1051
+ const convertToBlocksAtoms = (blocks) => {
1052
+ return blocks.map((block) => ({
1053
+ _type: block._type,
1054
+ _id: block._id,
1055
+ _parent: block._parent ?? null,
1056
+ _atom: jotai.atom(block)
1057
+ }));
1058
+ };
1238
1059
  const treeDSBlocks = jotai.atom((get) => {
1239
- const presentBlocks = get(presentBlocksAtom);
1240
- return convertToBlocksTree([...presentBlocks]);
1060
+ const presentBlocks = get(pageBlocksAtom).map((block) => get(block._atom));
1061
+ return convertToBlocksTree(presentBlocks);
1241
1062
  });
1242
1063
  treeDSBlocks.debugLabel = "treeDSBlocks";
1243
- const pageBlocksAtomsAtom = splitAtom(presentBlocksAtom);
1244
- pageBlocksAtomsAtom.debugLabel = "pageBlocksAtomsAtom";
1245
1064
  const builderActivePageAtom = jotai.atom("");
1246
1065
  builderActivePageAtom.debugLabel = "builderActivePageAtom";
1247
1066
  const destinationDropIndexAtom = jotai.atom(-1);
@@ -1253,10 +1072,87 @@ const globalBlocksAtom = jotai.atom((get) => {
1253
1072
  return lodashEs.filter(globalBlocks, (block) => lodashEs.has(block, "blockId"));
1254
1073
  });
1255
1074
  globalBlocksAtom.debugLabel = "globalBlocksAtom";
1075
+ const canAcceptChildBlock = (parentType, childType) => {
1076
+ if (!parentType) return true;
1077
+ const blockDefinition = runtime.getRegisteredChaiBlock(parentType);
1078
+ if (!blockDefinition) return false;
1079
+ return lodashEs.has(blockDefinition, "canAcceptBlock") ? blockDefinition.canAcceptBlock(childType) : false;
1080
+ };
1081
+ const canAddChildBlock = (parentType) => {
1082
+ const blockDefinition = runtime.getRegisteredChaiBlock(parentType);
1083
+ if (!blockDefinition) return false;
1084
+ return lodashEs.has(blockDefinition, "canAcceptBlock");
1085
+ };
1086
+ const canBeNestedInside = (parentType, childType) => {
1087
+ const blockDefinition = runtime.getRegisteredChaiBlock(childType);
1088
+ if (!blockDefinition) return true;
1089
+ return lodashEs.has(blockDefinition, "canBeNested") ? blockDefinition.canBeNested(parentType) : true;
1090
+ };
1091
+ const canDuplicateBlock = (type) => {
1092
+ const blockDefinition = runtime.getRegisteredChaiBlock(type);
1093
+ if (!blockDefinition) return true;
1094
+ return lodashEs.has(blockDefinition, "canDuplicate") ? blockDefinition.canDuplicate() : true;
1095
+ };
1096
+ const canDeleteBlock = (type) => {
1097
+ const blockDefinition = runtime.getRegisteredChaiBlock(type);
1098
+ if (!blockDefinition) return true;
1099
+ return lodashEs.has(blockDefinition, "canDelete") ? blockDefinition.canDelete() : true;
1100
+ };
1101
+ const canDropBlock = (_currentTree, { dragSource, dropTarget: dropTarget2 }) => {
1102
+ const dragSourceType = lodashEs.get(dragSource, "data._type", "");
1103
+ const dropTargetType = lodashEs.get(dropTarget2, "data._type", "");
1104
+ return canAcceptChildBlock(dropTargetType, dragSourceType);
1105
+ };
1106
+ if (void 0) {
1107
+ describe("canDropBlock Function", () => {
1108
+ it('should return false if dragSourceType is "Slot"', () => {
1109
+ const dragSource = { data: { _type: "Slot" } };
1110
+ const dropTarget2 = { data: {} };
1111
+ expect(canDropBlock({}, { dragSource, dropTarget: dropTarget2 })).toBe(true);
1112
+ });
1113
+ it("should return true if dropTargetType is empty", () => {
1114
+ const dragSource = { data: { _type: "Box" } };
1115
+ const dropTarget2 = { data: {} };
1116
+ expect(canDropBlock({}, { dragSource, dropTarget: dropTarget2 })).toBe(true);
1117
+ });
1118
+ });
1119
+ }
1120
+ const broadcastChannel = new BroadcastChannel("chaibuilder");
1121
+ const useBroadcastChannel = () => {
1122
+ const pageId = useBuilderProp("pageId", "chaibuilder_page");
1123
+ const postMessage = web.useDebouncedCallback(
1124
+ (message) => broadcastChannel.postMessage({ ...message, pageId }),
1125
+ [pageId],
1126
+ 200
1127
+ );
1128
+ return { postMessage };
1129
+ };
1130
+ const useUnmountBroadcastChannel = () => {
1131
+ const [, setBlocks] = useBlocksStore();
1132
+ const pageId = useBuilderProp("pageId", "chaibuilder_page");
1133
+ const { updateBlocksProps } = useBlocksStoreManager();
1134
+ React.useEffect(() => {
1135
+ broadcastChannel.onmessageerror = (event) => {
1136
+ console.log("error", event);
1137
+ };
1138
+ broadcastChannel.onmessage = (event) => {
1139
+ if (event.data.type === "blocks-updated" && event.data.pageId === pageId) {
1140
+ setBlocks(event.data.blocks);
1141
+ }
1142
+ if (event.data.type === "blocks-props-updated" && event.data.pageId === pageId) {
1143
+ updateBlocksProps(event.data.blocks);
1144
+ }
1145
+ };
1146
+ return () => {
1147
+ broadcastChannel.onmessage = null;
1148
+ broadcastChannel.onmessageerror = null;
1149
+ };
1150
+ }, [setBlocks, pageId]);
1151
+ };
1256
1152
  const selectedBlockIdsAtom = jotai.atom([]);
1257
1153
  selectedBlockIdsAtom.debugLabel = "selectedBlockIdsAtom";
1258
1154
  const selectedBlocksAtom = jotai.atom((get) => {
1259
- const blocks = get(presentBlocksAtom);
1155
+ const blocks = get(pageBlocksAtom);
1260
1156
  const blockIds = get(selectedBlockIdsAtom);
1261
1157
  return lodashEs.map(
1262
1158
  lodashEs.filter(blocks, ({ _id }) => lodashEs.includes(blockIds, _id)),
@@ -1270,7 +1166,7 @@ const selectedBlockAtom = jotai.atom((get) => {
1270
1166
  return null;
1271
1167
  }
1272
1168
  if (blocks.length === 1) {
1273
- return blocks[0];
1169
+ return get(lodashEs.first(blocks)._atom);
1274
1170
  }
1275
1171
  });
1276
1172
  selectedBlockAtom.debugLabel = "selectedBlockAtom";
@@ -1278,7 +1174,7 @@ const getParentId = (block) => lodashEs.get(block, "_parent", null);
1278
1174
  const selectedBlocksParentsAtom = jotai.atom((get) => {
1279
1175
  const selectedBlocks = get(selectedBlocksAtom);
1280
1176
  const parentIds = lodashEs.map(selectedBlocks, getParentId);
1281
- return lodashEs.filter(get(presentBlocksAtom), (block) => lodashEs.includes(parentIds, block._id));
1177
+ return lodashEs.filter(get(pageBlocksAtom), (block) => lodashEs.includes(parentIds, block._id));
1282
1178
  });
1283
1179
  selectedBlocksParentsAtom.debugLabel = "selectedBlocksParentsAtom";
1284
1180
  const selectedBlockFlexChildAtom = jotai.atom(() => {
@@ -1300,7 +1196,7 @@ const useSelectedBlocksDisplayChild = () => ({
1300
1196
  const useSelectedBlock = () => jotai.useAtomValue(selectedBlockAtom);
1301
1197
  const selectedBlockHierarchy = jotai.atom((get) => {
1302
1198
  const selectedBlock = get(selectedBlockAtom);
1303
- const allBlocks = get(presentBlocksAtom);
1199
+ const allBlocks = get(pageBlocksAtom);
1304
1200
  let block = selectedBlock;
1305
1201
  const blocks = [selectedBlock];
1306
1202
  do {
@@ -1326,6 +1222,241 @@ const useSelectedBlockIds = () => {
1326
1222
  );
1327
1223
  return [blockIds, setBlockIds, toggleSelectedBlockId];
1328
1224
  };
1225
+ const removeNestedBlocks = (blocks, blockIds) => {
1226
+ const _blockIds = [];
1227
+ const _blocks = lodashEs.filter(blocks, (block) => {
1228
+ if (lodashEs.includes(blockIds, block._id) || lodashEs.includes(blockIds, block._parent)) {
1229
+ _blockIds.push(block._id);
1230
+ return false;
1231
+ }
1232
+ return true;
1233
+ });
1234
+ if (!lodashEs.isEmpty(_blockIds)) return removeNestedBlocks(_blocks, _blockIds);
1235
+ return _blocks;
1236
+ };
1237
+ const useRemoveBlocks = () => {
1238
+ const [presentBlocks] = useBlocksStore();
1239
+ const [ids2, setSelectedIds] = useSelectedBlockIds();
1240
+ const { setNewBlocks } = useBlocksStoreUndoableActions();
1241
+ return React.useCallback(
1242
+ (blockIds) => {
1243
+ var _a;
1244
+ const parentBlockId = ((_a = lodashEs.find(presentBlocks, { _id: blockIds[0] })) == null ? void 0 : _a._parent) || null;
1245
+ setNewBlocks(removeNestedBlocks(presentBlocks, blockIds));
1246
+ setTimeout(() => setSelectedIds(parentBlockId ? [parentBlockId] : []), 200);
1247
+ },
1248
+ [presentBlocks, setSelectedIds, ids2]
1249
+ );
1250
+ };
1251
+ const useUpdateBlockAtom = () => {
1252
+ return useAtomCallback(
1253
+ React.useCallback((get, set, { id, props }) => {
1254
+ const blockAsAtoms = get(pageBlocksAtom);
1255
+ const blockAtom = lodashEs.find(blockAsAtoms, (b) => b._id === id);
1256
+ if (!blockAtom) {
1257
+ console.warn(`Block with id ${id} not found`);
1258
+ return;
1259
+ }
1260
+ return set(blockAtom._atom, { ...get(blockAtom._atom), ...props });
1261
+ }, [])
1262
+ );
1263
+ };
1264
+ const useGetBlockAtomValue = () => {
1265
+ return useAtomCallback(
1266
+ React.useCallback((get, _set, id) => {
1267
+ const blockAsAtoms = get(pageBlocksAtom);
1268
+ const blockAtom = lodashEs.find(blockAsAtoms, (b) => b._id === id);
1269
+ if (!blockAtom) {
1270
+ console.warn(`Block with id ${id} not found`);
1271
+ return;
1272
+ }
1273
+ return get(blockAtom._atom);
1274
+ }, [])
1275
+ );
1276
+ };
1277
+ function insertBlocksAtPosition(allBlocks, newBlocks, parentId, position) {
1278
+ let parentBlocks = allBlocks.filter((block) => !block._parent);
1279
+ if (parentId) {
1280
+ parentBlocks = allBlocks.filter((block) => block._parent === parentId);
1281
+ }
1282
+ const insertPosition = !isNaN(position) || position > -1 ? Math.min(position, parentBlocks.length) : parentBlocks.length;
1283
+ let insertIndex = allBlocks.length;
1284
+ for (let i = 0, count = 0; i < allBlocks.length; i++) {
1285
+ if (allBlocks[i]._parent === parentId) {
1286
+ if (count === insertPosition) {
1287
+ insertIndex = i;
1288
+ break;
1289
+ }
1290
+ count++;
1291
+ }
1292
+ }
1293
+ if (!parentId && position !== void 0 && position >= parentBlocks.length) {
1294
+ insertIndex = allBlocks.length;
1295
+ }
1296
+ return [...allBlocks.slice(0, insertIndex), ...newBlocks, ...allBlocks.slice(insertIndex)];
1297
+ }
1298
+ function getBlocksTree(blocks) {
1299
+ return convertToBlocksTree(blocks);
1300
+ }
1301
+ const nestedToFlatArray = (nestedJson, parent) => lodashEs.flatten(
1302
+ nestedJson.map((block) => {
1303
+ block = parent ? { ...block, _parent: parent } : { ...block };
1304
+ if (block.children) {
1305
+ const children = [...block.children];
1306
+ delete block.children;
1307
+ return lodashEs.flatten([block, ...nestedToFlatArray(children, block._id)]);
1308
+ }
1309
+ return block;
1310
+ })
1311
+ );
1312
+ function setProjectBlocksInMemory(nodes, initial = false) {
1313
+ for (let i = 0; i < nodes.length; i++) {
1314
+ const element = nodes[i];
1315
+ if (element.global && !initial) {
1316
+ nodes[i] = {
1317
+ type: "GlobalBlock",
1318
+ blockId: element.blockId,
1319
+ _parent: lodashEs.get(element, "_parent", null),
1320
+ _id: element._id
1321
+ };
1322
+ } else if (element.children && element.children.length) {
1323
+ setProjectBlocksInMemory(element.children);
1324
+ }
1325
+ }
1326
+ }
1327
+ function getInnerBlocks(flatArr) {
1328
+ let blocks = [];
1329
+ let pBlocks = lodashEs.filter(flatArr, { type: "GlobalBlock" });
1330
+ if (pBlocks.length > 0) {
1331
+ pBlocks = lodashEs.map(pBlocks, getPBlocks);
1332
+ lodashEs.each(pBlocks, (pBlock) => {
1333
+ blocks = [...blocks, ...getSingleBlock(pBlock)];
1334
+ });
1335
+ }
1336
+ return blocks;
1337
+ }
1338
+ function getSingleBlock(flatArray) {
1339
+ let blocks = [];
1340
+ const parent = lodashEs.get(lodashEs.first(flatArray), "_parent", null);
1341
+ lodashEs.set(lodashEs.first(flatArray), "_parent", null);
1342
+ const block = [flatToNestedInstance.convert(clone(flatArray))];
1343
+ setProjectBlocksInMemory(block, true);
1344
+ let flat = nestedToFlatArray(block, flatArray[0]._id);
1345
+ flat = lodashEs.set(flat, "0._parent", parent);
1346
+ blocks = [...blocks, flat, ...getInnerBlocks(flat)];
1347
+ return blocks;
1348
+ }
1349
+ function getPBlocks(block) {
1350
+ const rootBlock = lodashEs.find(FLAT_ARRAY, { _id: block._id });
1351
+ if (!rootBlock) return [];
1352
+ const blocks = [rootBlock];
1353
+ const children = lodashEs.filter(FLAT_ARRAY, { _parent: block._id });
1354
+ if (children.length) {
1355
+ return lodashEs.flatten([...blocks, ...lodashEs.flatten(lodashEs.map(children, getPBlocks))]);
1356
+ }
1357
+ return lodashEs.flatten(blocks);
1358
+ }
1359
+ const clone = (obj) => JSON.parse(JSON.stringify(obj));
1360
+ let FLAT_ARRAY = [];
1361
+ function splitPageBlocks(allPageBlocks) {
1362
+ FLAT_ARRAY = allPageBlocks;
1363
+ const clonedTree = getBlocksTree(clone(allPageBlocks));
1364
+ setProjectBlocksInMemory(clonedTree);
1365
+ const pageBlocks = nestedToFlatArray(clonedTree, null);
1366
+ const globalBlocks = getInnerBlocks(pageBlocks);
1367
+ const mappedBlocks = {};
1368
+ lodashEs.each(globalBlocks, (projectBlock) => lodashEs.set(mappedBlocks, lodashEs.first(projectBlock).blockId, projectBlock));
1369
+ return [pageBlocks, mappedBlocks];
1370
+ }
1371
+ function flattenTree(node) {
1372
+ let flatArray = [];
1373
+ node.walk((n) => {
1374
+ delete n.model.children;
1375
+ flatArray.push(n.model);
1376
+ return true;
1377
+ });
1378
+ return flatArray;
1379
+ }
1380
+ function findNodeById(node, id) {
1381
+ return node.first((n) => n.model._id === id) || null;
1382
+ }
1383
+ function moveNode(rootNode, nodeIdToMove, newParentId, position) {
1384
+ const nodeToMove = findNodeById(rootNode, nodeIdToMove);
1385
+ const newParentNode = findNodeById(rootNode, newParentId);
1386
+ if (nodeToMove && newParentNode) {
1387
+ nodeToMove.drop();
1388
+ if (!newParentNode.children) {
1389
+ newParentNode.model.children = [];
1390
+ }
1391
+ try {
1392
+ newParentNode.addChildAtIndex(nodeToMove, position);
1393
+ } catch (error) {
1394
+ console.error("Error adding child to parent:", error);
1395
+ return false;
1396
+ }
1397
+ return true;
1398
+ }
1399
+ return false;
1400
+ }
1401
+ function moveBlocksWithChildren(_blocks, idToMove, newParentId, newPosition, updateBlockAtom) {
1402
+ if (!idToMove) return _blocks;
1403
+ newParentId = newParentId || "root";
1404
+ const tree2 = new TreeModel();
1405
+ const root = tree2.parse({ _id: "root", children: getBlocksTree(_blocks) });
1406
+ if (moveNode(root, idToMove, newParentId, newPosition)) {
1407
+ const newBlocks = flattenTree(root);
1408
+ const movedBlock = newBlocks.find((block) => block._id === idToMove);
1409
+ if (movedBlock) {
1410
+ movedBlock._parent = newParentId === "root" ? null : newParentId;
1411
+ updateBlockAtom({ id: movedBlock._id, props: { _parent: movedBlock._parent } });
1412
+ }
1413
+ newBlocks.shift();
1414
+ return convertToBlocksAtoms(newBlocks);
1415
+ }
1416
+ return _blocks;
1417
+ }
1418
+ const useBlocksStoreManager = () => {
1419
+ const [, setBlocks] = useBlocksStore();
1420
+ const { postMessage } = useBroadcastChannel();
1421
+ const updateBlockAtom = useUpdateBlockAtom();
1422
+ return {
1423
+ setNewBlocks: (newBlocks) => {
1424
+ setBlocks(newBlocks);
1425
+ postMessage({ type: "blocks-updated", blocks: newBlocks });
1426
+ },
1427
+ addBlocks: (newBlocks, parent, position) => {
1428
+ setBlocks((prevBlocks) => {
1429
+ const blocks = insertBlocksAtPosition(prevBlocks, newBlocks, parent, position);
1430
+ postMessage({ type: "blocks-updated", blocks });
1431
+ return blocks;
1432
+ });
1433
+ },
1434
+ removeBlocks: (blockIds) => {
1435
+ setBlocks((prevBlocks) => {
1436
+ const blocks = removeNestedBlocks(prevBlocks, blockIds);
1437
+ postMessage({ type: "blocks-updated", blocks });
1438
+ return blocks;
1439
+ });
1440
+ },
1441
+ moveBlocks: (blockIds, newParent, position) => {
1442
+ setBlocks((prevBlocks) => {
1443
+ let blocks = prevBlocks;
1444
+ for (let i = 0; i < blockIds.length; i++) {
1445
+ blocks = moveBlocksWithChildren(blocks, blockIds[i], newParent, position, updateBlockAtom);
1446
+ }
1447
+ postMessage({ type: "blocks-updated", blocks });
1448
+ return blocks;
1449
+ });
1450
+ },
1451
+ updateBlocksProps: (blocks) => {
1452
+ blocks.forEach((block) => {
1453
+ const updatedBlock = lodashEs.omit(block, "_id");
1454
+ updateBlockAtom({ id: block._id, props: updatedBlock });
1455
+ });
1456
+ postMessage({ type: "blocks-props-updated", blocks });
1457
+ }
1458
+ };
1459
+ };
1329
1460
  var undomanager = { exports: {} };
1330
1461
  var hasRequiredUndomanager;
1331
1462
  function requireUndomanager() {
@@ -1481,79 +1612,6 @@ function requireUndomanager() {
1481
1612
  }
1482
1613
  var undomanagerExports = requireUndomanager();
1483
1614
  const UndoManager = /* @__PURE__ */ ChaiThemeFn.getDefaultExportFromCjs(undomanagerExports);
1484
- function getBlocksTree(blocks) {
1485
- return convertToBlocksTree(blocks);
1486
- }
1487
- const nestedToFlatArray = (nestedJson, parent) => lodashEs.flatten(
1488
- nestedJson.map((block) => {
1489
- block = parent ? { ...block, _parent: parent } : { ...block };
1490
- if (block.children) {
1491
- const children = [...block.children];
1492
- delete block.children;
1493
- return lodashEs.flatten([block, ...nestedToFlatArray(children, block._id)]);
1494
- }
1495
- return block;
1496
- })
1497
- );
1498
- function setProjectBlocksInMemory(nodes, initial = false) {
1499
- for (let i = 0; i < nodes.length; i++) {
1500
- const element = nodes[i];
1501
- if (element.global && !initial) {
1502
- nodes[i] = {
1503
- type: "GlobalBlock",
1504
- blockId: element.blockId,
1505
- _parent: lodashEs.get(element, "_parent", null),
1506
- _id: element._id
1507
- };
1508
- } else if (element.children && element.children.length) {
1509
- setProjectBlocksInMemory(element.children);
1510
- }
1511
- }
1512
- }
1513
- function getInnerBlocks(flatArr) {
1514
- let blocks = [];
1515
- let pBlocks = lodashEs.filter(flatArr, { type: "GlobalBlock" });
1516
- if (pBlocks.length > 0) {
1517
- pBlocks = lodashEs.map(pBlocks, getPBlocks);
1518
- lodashEs.each(pBlocks, (pBlock) => {
1519
- blocks = [...blocks, ...getSingleBlock(pBlock)];
1520
- });
1521
- }
1522
- return blocks;
1523
- }
1524
- function getSingleBlock(flatArray) {
1525
- let blocks = [];
1526
- const parent = lodashEs.get(lodashEs.first(flatArray), "_parent", null);
1527
- lodashEs.set(lodashEs.first(flatArray), "_parent", null);
1528
- const block = [flatToNestedInstance.convert(clone(flatArray))];
1529
- setProjectBlocksInMemory(block, true);
1530
- let flat = nestedToFlatArray(block, flatArray[0]._id);
1531
- flat = lodashEs.set(flat, "0._parent", parent);
1532
- blocks = [...blocks, flat, ...getInnerBlocks(flat)];
1533
- return blocks;
1534
- }
1535
- function getPBlocks(block) {
1536
- const rootBlock = lodashEs.find(FLAT_ARRAY, { _id: block._id });
1537
- if (!rootBlock) return [];
1538
- const blocks = [rootBlock];
1539
- const children = lodashEs.filter(FLAT_ARRAY, { _parent: block._id });
1540
- if (children.length) {
1541
- return lodashEs.flatten([...blocks, ...lodashEs.flatten(lodashEs.map(children, getPBlocks))]);
1542
- }
1543
- return lodashEs.flatten(blocks);
1544
- }
1545
- const clone = (obj) => JSON.parse(JSON.stringify(obj));
1546
- let FLAT_ARRAY = [];
1547
- function splitPageBlocks(allPageBlocks) {
1548
- FLAT_ARRAY = allPageBlocks;
1549
- const clonedTree = getBlocksTree(clone(allPageBlocks));
1550
- setProjectBlocksInMemory(clonedTree);
1551
- const pageBlocks = nestedToFlatArray(clonedTree, null);
1552
- const globalBlocks = getInnerBlocks(pageBlocks);
1553
- const mappedBlocks = {};
1554
- lodashEs.each(globalBlocks, (projectBlock) => lodashEs.set(mappedBlocks, lodashEs.first(projectBlock).blockId, projectBlock));
1555
- return [pageBlocks, mappedBlocks];
1556
- }
1557
1615
  const MODIFIERS = [
1558
1616
  "hover",
1559
1617
  "focus",
@@ -1634,9 +1692,10 @@ const useGetPageData = () => {
1634
1692
  const [projectOptions] = useBrandingOptions();
1635
1693
  const { currentPage } = useCurrentPage();
1636
1694
  const [presentBlocks] = useBlocksStore();
1695
+ const getBlockAtomValue = useGetBlockAtomValue();
1637
1696
  return React.useCallback(() => {
1638
1697
  const blocks = lodashEs.map(presentBlocks, (block) => {
1639
- return lodashEs.omit(block, getBlockBuilderProps(block._type));
1698
+ return lodashEs.omit(getBlockAtomValue(block._id), getBlockBuilderProps(block._type));
1640
1699
  });
1641
1700
  const [pageFilteredBlocks = []] = splitPageBlocks(blocks);
1642
1701
  return {
@@ -1764,190 +1823,8 @@ const useUndoManager = () => {
1764
1823
  clear: undoManager.clear
1765
1824
  };
1766
1825
  };
1767
- const useAtomSetterCb = () => {
1768
- return useAtomCallback(
1769
- React.useCallback((get, set, id) => {
1770
- const blockAtoms = get(pageBlocksAtomsAtom);
1771
- const blockAtom = lodashEs.find(blockAtoms, (atom2) => get(atom2)._id === id);
1772
- if (!blockAtom) return (_props) => void 0;
1773
- return (props) => set(blockAtom, { ...get(blockAtom), ...props });
1774
- }, [])
1775
- );
1776
- };
1777
- const broadcastChannel = new BroadcastChannel("chaibuilder");
1778
- const useBroadcastChannel = () => {
1779
- const pageId = useBuilderProp("pageId", "chaibuilder_page");
1780
- const postMessage = web.useDebouncedCallback(
1781
- (message) => broadcastChannel.postMessage({ ...message, pageId }),
1782
- [pageId],
1783
- 200
1784
- );
1785
- return { postMessage };
1786
- };
1787
- const useUnmountBroadcastChannel = () => {
1788
- const [, setBlocks] = useBlocksStore();
1789
- const pageId = useBuilderProp("pageId", "chaibuilder_page");
1790
- const { updateBlocksProps } = useBlocksStoreManager();
1791
- React.useEffect(() => {
1792
- broadcastChannel.onmessageerror = (event) => {
1793
- console.log("error", event);
1794
- };
1795
- broadcastChannel.onmessage = (event) => {
1796
- if (event.data.type === "blocks-updated" && event.data.pageId === pageId) {
1797
- setBlocks(event.data.blocks);
1798
- }
1799
- if (event.data.type === "blocks-props-updated" && event.data.pageId === pageId) {
1800
- updateBlocksProps(event.data.blocks);
1801
- }
1802
- };
1803
- return () => {
1804
- broadcastChannel.onmessage = null;
1805
- broadcastChannel.onmessageerror = null;
1806
- };
1807
- }, [setBlocks, pageId]);
1808
- };
1809
- const removeNestedBlocks = (blocks, blockIds) => {
1810
- const _blockIds = [];
1811
- const _blocks = lodashEs.filter(blocks, (block) => {
1812
- if (lodashEs.includes(blockIds, block._id) || lodashEs.includes(blockIds, block._parent)) {
1813
- _blockIds.push(block._id);
1814
- return false;
1815
- }
1816
- return true;
1817
- });
1818
- if (!lodashEs.isEmpty(_blockIds)) return removeNestedBlocks(_blocks, _blockIds);
1819
- return _blocks;
1820
- };
1821
- const useRemoveBlocks = () => {
1822
- const [presentBlocks] = useBlocksStore();
1823
- const [ids2, setSelectedIds] = useSelectedBlockIds();
1824
- const { setNewBlocks } = useBlocksStoreUndoableActions();
1825
- return React.useCallback(
1826
- (blockIds) => {
1827
- var _a;
1828
- const parentBlockId = ((_a = lodashEs.find(presentBlocks, { _id: blockIds[0] })) == null ? void 0 : _a._parent) || null;
1829
- setNewBlocks(removeNestedBlocks(presentBlocks, blockIds));
1830
- setTimeout(() => setSelectedIds(parentBlockId ? [parentBlockId] : []), 200);
1831
- },
1832
- [presentBlocks, setSelectedIds, ids2]
1833
- );
1834
- };
1835
- function insertBlocksAtPosition(allBlocks, newBlocks, parentId, position) {
1836
- let parentBlocks = allBlocks.filter((block) => !block._parent);
1837
- if (parentId) {
1838
- parentBlocks = allBlocks.filter((block) => block._parent === parentId);
1839
- }
1840
- const insertPosition = !isNaN(position) || position > -1 ? Math.min(position, parentBlocks.length) : parentBlocks.length;
1841
- let insertIndex = allBlocks.length;
1842
- for (let i = 0, count = 0; i < allBlocks.length; i++) {
1843
- if (allBlocks[i]._parent === parentId) {
1844
- if (count === insertPosition) {
1845
- insertIndex = i;
1846
- break;
1847
- }
1848
- count++;
1849
- }
1850
- }
1851
- if (!parentId && position !== void 0 && position >= parentBlocks.length) {
1852
- insertIndex = allBlocks.length;
1853
- }
1854
- return [...allBlocks.slice(0, insertIndex), ...newBlocks, ...allBlocks.slice(insertIndex)];
1855
- }
1856
- function flattenTree(node) {
1857
- let flatArray = [];
1858
- node.walk((n) => {
1859
- delete n.model.children;
1860
- flatArray.push(n.model);
1861
- return true;
1862
- });
1863
- return flatArray;
1864
- }
1865
- function findNodeById(node, id) {
1866
- return node.first((n) => n.model._id === id) || null;
1867
- }
1868
- function moveNode(rootNode, nodeIdToMove, newParentId, position) {
1869
- const nodeToMove = findNodeById(rootNode, nodeIdToMove);
1870
- const newParentNode = findNodeById(rootNode, newParentId);
1871
- if (nodeToMove && newParentNode) {
1872
- nodeToMove.drop();
1873
- if (!newParentNode.children) {
1874
- newParentNode.model.children = [];
1875
- }
1876
- try {
1877
- newParentNode.addChildAtIndex(nodeToMove, position);
1878
- } catch (error) {
1879
- console.error("Error adding child to parent:", error);
1880
- return false;
1881
- }
1882
- return true;
1883
- }
1884
- return false;
1885
- }
1886
- function moveBlocksWithChildren(_blocks, idToMove, newParentId, newPosition) {
1887
- if (!idToMove) return _blocks;
1888
- newParentId = newParentId || "root";
1889
- const tree2 = new TreeModel();
1890
- const root = tree2.parse({ _id: "root", children: getBlocksTree(_blocks) });
1891
- if (moveNode(root, idToMove, newParentId, newPosition)) {
1892
- const newBlocks = flattenTree(root);
1893
- const movedBlock = newBlocks.find((block) => block._id === idToMove);
1894
- if (movedBlock) movedBlock._parent = newParentId === "root" ? null : newParentId;
1895
- newBlocks.shift();
1896
- return newBlocks;
1897
- }
1898
- return _blocks;
1899
- }
1900
- const useBlocksStoreManager = () => {
1901
- const [, setBlocks] = useBlocksStore();
1902
- const { postMessage } = useBroadcastChannel();
1903
- const atomSetterCb = useAtomSetterCb();
1904
- const updateAtomProps = React.useCallback(
1905
- (id, props) => {
1906
- const setter = atomSetterCb(id);
1907
- setter({ ...props });
1908
- },
1909
- [atomSetterCb]
1910
- );
1911
- return {
1912
- setNewBlocks: (newBlocks) => {
1913
- setBlocks(newBlocks);
1914
- postMessage({ type: "blocks-updated", blocks: newBlocks });
1915
- },
1916
- addBlocks: (newBlocks, parent, position) => {
1917
- setBlocks((prevBlocks) => {
1918
- const blocks = insertBlocksAtPosition(prevBlocks, newBlocks, parent, position);
1919
- postMessage({ type: "blocks-updated", blocks });
1920
- return blocks;
1921
- });
1922
- },
1923
- removeBlocks: (blockIds) => {
1924
- setBlocks((prevBlocks) => {
1925
- const blocks = removeNestedBlocks(prevBlocks, blockIds);
1926
- postMessage({ type: "blocks-updated", blocks });
1927
- return blocks;
1928
- });
1929
- },
1930
- moveBlocks: (blockIds, newParent, position) => {
1931
- setBlocks((prevBlocks) => {
1932
- let blocks = prevBlocks;
1933
- for (let i = 0; i < blockIds.length; i++) {
1934
- blocks = moveBlocksWithChildren(blocks, blockIds[i], newParent, position);
1935
- }
1936
- postMessage({ type: "blocks-updated", blocks });
1937
- return blocks;
1938
- });
1939
- },
1940
- updateBlocksProps: (blocks) => {
1941
- blocks.forEach((block) => {
1942
- const updatedBlock = lodashEs.omit(block, "_id");
1943
- updateAtomProps(block._id, updatedBlock);
1944
- });
1945
- postMessage({ type: "blocks-props-updated", blocks });
1946
- }
1947
- };
1948
- };
1949
1826
  const useBlocksStore = () => {
1950
- return jotai.useAtom(presentBlocksAtom);
1827
+ return jotai.useAtom(pageBlocksAtom);
1951
1828
  };
1952
1829
  const useBlocksStoreUndoableActions = () => {
1953
1830
  const { add } = useUndoManager();
@@ -2081,7 +1958,7 @@ const useAddBlock = () => {
2081
1958
  blocks[0]._parent = parentBlock._parent;
2082
1959
  parentBlockId = parentBlock._parent;
2083
1960
  }
2084
- addBlocks(blocks, parentBlockId, position);
1961
+ addBlocks(convertToBlocksAtoms(blocks), parentBlockId, position);
2085
1962
  setSelected([(_a = lodashEs.first(blocks)) == null ? void 0 : _a._id]);
2086
1963
  return lodashEs.first(blocks);
2087
1964
  },
@@ -2113,7 +1990,7 @@ const useAddBlock = () => {
2113
1990
  parentBlockId = parentBlock._parent;
2114
1991
  }
2115
1992
  const newBlocks = [newBlock];
2116
- addBlocks(newBlocks, parentBlockId, position);
1993
+ addBlocks(convertToBlocksAtoms(newBlocks), parentBlockId, position);
2117
1994
  setSelected([newBlock._id]);
2118
1995
  return newBlock;
2119
1996
  },
@@ -3311,12 +3188,9 @@ const selectedStylingBlocksAtom = jotai.atom([]);
3311
3188
  selectedStylingBlocksAtom.debugLabel = "selectedStylingBlocksAtom";
3312
3189
  const useSelectedStylingBlocks = () => jotai.useAtom(selectedStylingBlocksAtom);
3313
3190
  const addClassesToBlocksAtom = jotai.atom(null, (get, _set, { blockIds, newClasses }) => {
3314
- const blockAtoms = lodashEs.filter(
3315
- get(pageBlocksAtomsAtom),
3316
- (blockAtom) => (
3317
- // @ts-ignore
3318
- blockIds.includes(get(blockAtom)._id)
3319
- )
3191
+ const blockAtoms = lodashEs.map(
3192
+ lodashEs.filter(get(pageBlocksAtom), (block) => blockIds.includes(block._id)),
3193
+ (block) => block._atom
3320
3194
  );
3321
3195
  const styleBlock = lodashEs.first(get(selectedStylingBlocksAtom));
3322
3196
  return lodashEs.map(blockAtoms, (blockAtom) => {
@@ -3430,7 +3304,7 @@ const useDuplicateBlocks = () => {
3430
3304
  const blockPosition = siblingBlocks.indexOf(block);
3431
3305
  const newBlockPosition = blockPosition + 1;
3432
3306
  const newBlocks = getDuplicatedBlocks(presentBlocks, blockId, parentId);
3433
- addBlocks(newBlocks, parentId, newBlockPosition);
3307
+ addBlocks(convertToBlocksAtoms(newBlocks), parentId, newBlockPosition);
3434
3308
  newBlockIds.push(lodashEs.get(newBlocks, "0._id", ""));
3435
3309
  });
3436
3310
  setSelected(newBlockIds);
@@ -3464,7 +3338,7 @@ const useCanPaste = () => {
3464
3338
  };
3465
3339
  };
3466
3340
  const useMoveCutBlocks = () => {
3467
- const presentBlocks = jotai.useAtomValue(presentBlocksAtom);
3341
+ const presentBlocks = jotai.useAtomValue(pageBlocksAtom);
3468
3342
  const { moveBlocks: moveBlocks2 } = useBlocksStoreUndoableActions();
3469
3343
  return React.useCallback(
3470
3344
  (blockIds, newParentId) => {
@@ -3542,12 +3416,9 @@ const usePreviewMode = () => {
3542
3416
  };
3543
3417
  const removeClassFromBlocksAtom = jotai.atom(null, (get, _set, { blockIds, fullClasses }) => {
3544
3418
  const styleBlock = lodashEs.first(get(selectedStylingBlocksAtom));
3545
- const blockAtoms = lodashEs.filter(
3546
- get(pageBlocksAtomsAtom),
3547
- (blockAtom) => (
3548
- // @ts-ignore
3549
- blockIds.includes(get(blockAtom)._id)
3550
- )
3419
+ const blockAtoms = lodashEs.map(
3420
+ lodashEs.filter(get(pageBlocksAtom), (block) => blockIds.includes(block._id)),
3421
+ (block) => block._atom
3551
3422
  );
3552
3423
  return lodashEs.map(blockAtoms, (blockAtom) => {
3553
3424
  const block = get(blockAtom);
@@ -3779,25 +3650,22 @@ const globalBlocksStoreAtom = atom({});
3779
3650
  const globalBlocksLoadingStateAtom = atom({});
3780
3651
  const useGlobalBlocksStore = () => {
3781
3652
  const [globalBlocks, setGlobalBlocks] = useAtom(globalBlocksStoreAtom);
3782
- const getGlobalBlocks = React.useCallback(
3783
- (block) => {
3784
- return lodashEs.get(globalBlocks, block == null ? void 0 : block.globalBlock, []);
3785
- },
3786
- [globalBlocks]
3787
- );
3788
- const reset = React.useCallback(() => {
3789
- setGlobalBlocks({});
3790
- }, [setGlobalBlocks]);
3653
+ const getGlobalBlocks = React.useCallback((globalBlockId) => lodashEs.get(globalBlocks, globalBlockId, []), [globalBlocks]);
3654
+ const reset = React.useCallback(() => setGlobalBlocks({}), [setGlobalBlocks]);
3791
3655
  return { getGlobalBlocks, reset };
3792
3656
  };
3793
3657
  const useWatchGlobalBlocks = () => {
3794
3658
  const [blocksStore] = useBlocksStore();
3795
3659
  const [globalBlocks, setGlobalBlocks] = useAtom(globalBlocksStoreAtom);
3796
3660
  const [globalBlocksLoadingState, setGlobalBlocksLoadingState] = useAtom(globalBlocksLoadingStateAtom);
3661
+ const getBlockAtomValue = useGetBlockAtomValue();
3797
3662
  const getGlobalBlockBlocks = useBuilderProp("getGlobalBlockBlocks", async (_key) => []);
3798
3663
  const globalBlocksList = React.useMemo(() => {
3799
3664
  const globalBlocks2 = blocksStore.filter((block) => block._type === "GlobalBlock");
3800
- return globalBlocks2.filter((block) => block._type === "GlobalBlock").map((block) => block.globalBlock);
3665
+ return globalBlocks2.filter((block) => block._type === "GlobalBlock").map((block) => {
3666
+ const blockValue = getBlockAtomValue(block._id);
3667
+ return blockValue.globalBlock;
3668
+ });
3801
3669
  }, [blocksStore]);
3802
3670
  React.useEffect(() => {
3803
3671
  lodashEs.forEach(globalBlocksList, (globalBlock) => {
@@ -4199,7 +4067,7 @@ const useAiAssistant = () => {
4199
4067
  };
4200
4068
  const wrapperBlockAtom = jotai.atom((get) => {
4201
4069
  var _a;
4202
- const blocks = get(presentBlocksAtom);
4070
+ const blocks = get(pageBlocksAtom);
4203
4071
  const blockIds = get(selectedBlockIdsAtom);
4204
4072
  const blockId = blockIds.length === 1 ? blockIds[0] : null;
4205
4073
  if (!blockId) return null;
@@ -4676,10 +4544,10 @@ const useHandleCanvasDblClick = () => {
4676
4544
  const updateContent = useUpdateBlocksProps();
4677
4545
  const [editingBlockId, setEditingBlockId] = jotai.useAtom(inlineEditingActiveAtom);
4678
4546
  const { clearHighlight } = useBlockHighlight();
4679
- const [blocks] = useBlocksStore();
4547
+ const getBlockAtomValue = useGetBlockAtomValue();
4680
4548
  return React.useCallback(
4681
4549
  (e2) => {
4682
- var _a, _b;
4550
+ var _a;
4683
4551
  if (editingBlockId) return;
4684
4552
  const chaiBlock = getTargetedBlock(e2.target);
4685
4553
  const blockType = chaiBlock.getAttribute("data-block-type");
@@ -4687,7 +4555,7 @@ const useHandleCanvasDblClick = () => {
4687
4555
  return;
4688
4556
  }
4689
4557
  const blockId = chaiBlock.getAttribute("data-block-id");
4690
- const content = (_a = lodashEs.find(blocks, { _id: blockId })) == null ? void 0 : _a.content;
4558
+ const content = getBlockAtomValue(blockId)["content"];
4691
4559
  const newBlock = chaiBlock.cloneNode(true);
4692
4560
  newBlock.innerHTML = content;
4693
4561
  chaiBlock.style.display = "none";
@@ -4719,10 +4587,10 @@ const useHandleCanvasDblClick = () => {
4719
4587
  }
4720
4588
  });
4721
4589
  quill.focus();
4722
- (_b = newBlock.querySelector(".ql-clipboard")) == null ? void 0 : _b.remove();
4590
+ (_a = newBlock.querySelector(".ql-clipboard")) == null ? void 0 : _a.remove();
4723
4591
  setEditingBlockId(chaiBlock.getAttribute("data-block-id"));
4724
4592
  },
4725
- [editingBlockId, blocks, setEditingBlockId, updateContent, clearHighlight]
4593
+ [editingBlockId, setEditingBlockId, updateContent, clearHighlight]
4726
4594
  );
4727
4595
  };
4728
4596
  const useHandleCanvasClick = () => {
@@ -5016,7 +4884,8 @@ const useBlockRuntimeProps = () => {
5016
4884
  [allBlocks]
5017
4885
  );
5018
4886
  };
5019
- const BlockRenderer = ({ block, children }) => {
4887
+ const BlockRenderer = ({ blockAtom, children }) => {
4888
+ const [block] = jotai.useAtom(blockAtom);
5020
4889
  const registeredChaiBlock = React.useMemo(() => runtime.getRegisteredChaiBlock(block._type), [block._type]);
5021
4890
  const { selectedLang, fallbackLang } = useLanguages();
5022
4891
  const getRuntimePropValues = useBlockRuntimeProps();
@@ -5053,16 +4922,21 @@ const BlockRenderer = ({ block, children }) => {
5053
4922
  if (lodashEs.isNull(Component2) || hiddenBlocks.includes(block._id) || editingBlockId === block._id) return null;
5054
4923
  return /* @__PURE__ */ jsxRuntime.jsx(React.Suspense, { children: React.createElement(Component2, { ...props, children }) });
5055
4924
  };
5056
- const GlobalBlocksRenderer = ({ block }) => {
4925
+ const GlobalBlocksRenderer = ({ blockAtom }) => {
5057
4926
  const { getGlobalBlocks } = useGlobalBlocksStore();
5058
- const globalBlocks = getGlobalBlocks(block);
5059
- return /* @__PURE__ */ jsxRuntime.jsx(BlocksRenderer, { blocks: globalBlocks, filterFn: (block2) => lodashEs.isEmpty(block2._parent) });
5060
- };
5061
- const BlocksRenderer = ({ blocks, filterFn }) => {
4927
+ const [block] = jotai.useAtom(blockAtom);
4928
+ const globalBlocks = React.useMemo(() => getGlobalBlocks(block == null ? void 0 : block.globalBlock), [block == null ? void 0 : block.globalBlock, getGlobalBlocks]);
4929
+ const globalBlocksAtoms = React.useMemo(() => convertToBlocksAtoms(globalBlocks), [globalBlocks]);
4930
+ return /* @__PURE__ */ jsxRuntime.jsx(BlocksRenderer, { blocks: globalBlocksAtoms, filterFn: (block2) => lodashEs.isEmpty(block2._parent) });
4931
+ };
4932
+ const BlocksRenderer = ({
4933
+ blocks,
4934
+ filterFn
4935
+ }) => {
5062
4936
  const filteredBlocks = React.useMemo(() => lodashEs.filter(blocks, filterFn), [blocks, filterFn]);
5063
4937
  const hasChildren = (blockId) => lodashEs.filter(blocks, (b) => b._parent === blockId).length > 0;
5064
4938
  return lodashEs.map(filteredBlocks, (block) => {
5065
- return /* @__PURE__ */ jsxRuntime.jsx(BlockRenderer, { block, children: block._type === "GlobalBlock" ? /* @__PURE__ */ jsxRuntime.jsx(GlobalBlocksRenderer, { block }) : hasChildren(block._id) ? /* @__PURE__ */ jsxRuntime.jsx(BlocksRenderer, { blocks, filterFn: (b) => b._parent === block._id }) : null }, block._id);
4939
+ return /* @__PURE__ */ jsxRuntime.jsx(BlockRenderer, { blockAtom: block._atom, children: block._type === "GlobalBlock" ? /* @__PURE__ */ jsxRuntime.jsx(GlobalBlocksRenderer, { blockAtom: block._atom }) : hasChildren(block._id) ? /* @__PURE__ */ jsxRuntime.jsx(BlocksRenderer, { blocks, filterFn: (b) => b._parent === block._id }) : null }, block._id);
5066
4940
  });
5067
4941
  };
5068
4942
  const PageBlocksRenderer = () => {
@@ -6279,7 +6153,7 @@ const JSONForm = React.memo(({ blockId, schema, uiSchema, formData, onChange })
6279
6153
  onChange({ formData: formData2 }, id);
6280
6154
  },
6281
6155
  [onChange, selectedLang],
6282
- 1e3
6156
+ 400
6283
6157
  // save only every 5 seconds
6284
6158
  );
6285
6159
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -11005,7 +10879,7 @@ const getParentNodeIds = (blocks, id) => {
11005
10879
  const expandedIdsAtom = jotai.atom([]);
11006
10880
  const useExpandTree = () => {
11007
10881
  const [ids2] = useSelectedBlockIds();
11008
- const pageBlocks = jotai.useAtomValue(presentBlocksAtom);
10882
+ const pageBlocks = jotai.useAtomValue(pageBlocksAtom);
11009
10883
  const [, setExpandedIds] = jotai.useAtom(expandedIdsAtom);
11010
10884
  React.useEffect(() => {
11011
10885
  let expandedIds = [];
@@ -11646,7 +11520,7 @@ const ChaiBuilderComponent = (props) => {
11646
11520
  React.useEffect(() => {
11647
11521
  setTimeout(() => {
11648
11522
  const withDefaults = runtime.syncBlocksWithDefaults(props.blocks || []);
11649
- setAllBlocks(withDefaults);
11523
+ setAllBlocks(convertToBlocksAtoms(withDefaults));
11650
11524
  if (withDefaults && withDefaults.length > 0) {
11651
11525
  postMessage({ type: "blocks-updated", blocks: withDefaults });
11652
11526
  }