@chaibuilder/sdk 2.0.0-beta.82 → 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,141 +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 isWritable = (atom2) => !!atom2.write;
596
- const isFunction = (x) => typeof x === "function";
597
- function splitAtom(arrAtom, keyExtractor) {
598
- return memo2$1(
599
- () => {
600
- const mappingCache = /* @__PURE__ */ new WeakMap();
601
- const getMapping = (arr, prev) => {
602
- let mapping = mappingCache.get(arr);
603
- if (mapping) {
604
- return mapping;
605
- }
606
- const prevMapping = prev && mappingCache.get(prev);
607
- const atomList = [];
608
- const keyList = [];
609
- arr.forEach((item, index) => {
610
- const key = keyExtractor(item);
611
- keyList[index] = key;
612
- const cachedAtom = prevMapping && prevMapping.atomList[prevMapping.keyList.indexOf(key)];
613
- if (cachedAtom) {
614
- atomList[index] = cachedAtom;
615
- return;
616
- }
617
- const read = (get) => {
618
- const prev2 = get(mappingAtom);
619
- const currArr = get(arrAtom);
620
- const mapping2 = getMapping(currArr, prev2 == null ? void 0 : prev2.arr);
621
- const index2 = mapping2.keyList.indexOf(key);
622
- if (index2 < 0 || index2 >= currArr.length) {
623
- const prevItem = arr[getMapping(arr).keyList.indexOf(key)];
624
- if (prevItem) {
625
- return prevItem;
626
- }
627
- throw new Error("splitAtom: index out of bounds for read");
628
- }
629
- return currArr[index2];
630
- };
631
- const write = (get, set, update) => {
632
- const prev2 = get(mappingAtom);
633
- const arr2 = get(arrAtom);
634
- const mapping2 = getMapping(arr2, prev2 == null ? void 0 : prev2.arr);
635
- const index2 = mapping2.keyList.indexOf(key);
636
- if (index2 < 0 || index2 >= arr2.length) {
637
- throw new Error("splitAtom: index out of bounds for write");
638
- }
639
- const nextItem = isFunction(update) ? update(arr2[index2]) : update;
640
- if (!Object.is(arr2[index2], nextItem)) {
641
- set(arrAtom, [
642
- ...arr2.slice(0, index2),
643
- nextItem,
644
- ...arr2.slice(index2 + 1)
645
- ]);
646
- }
647
- };
648
- atomList[index] = isWritable(arrAtom) ? atom(read, write) : atom(read);
649
- });
650
- if (prevMapping && prevMapping.keyList.length === keyList.length && prevMapping.keyList.every((x, i) => x === keyList[i])) {
651
- mapping = prevMapping;
652
- } else {
653
- mapping = { arr, atomList, keyList };
654
- }
655
- mappingCache.set(arr, mapping);
656
- return mapping;
657
- };
658
- const mappingAtom = atom((get) => {
659
- const prev = get(mappingAtom);
660
- const arr = get(arrAtom);
661
- const mapping = getMapping(arr, prev == null ? void 0 : prev.arr);
662
- return mapping;
663
- });
664
- mappingAtom.init = void 0;
665
- const splittedAtom = isWritable(arrAtom) ? atom(
666
- (get) => get(mappingAtom).atomList,
667
- (get, set, action) => {
668
- switch (action.type) {
669
- case "remove": {
670
- const index = get(splittedAtom).indexOf(action.atom);
671
- if (index >= 0) {
672
- const arr = get(arrAtom);
673
- set(arrAtom, [
674
- ...arr.slice(0, index),
675
- ...arr.slice(index + 1)
676
- ]);
677
- }
678
- break;
679
- }
680
- case "insert": {
681
- const index = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
682
- if (index >= 0) {
683
- const arr = get(arrAtom);
684
- set(arrAtom, [
685
- ...arr.slice(0, index),
686
- action.value,
687
- ...arr.slice(index)
688
- ]);
689
- }
690
- break;
691
- }
692
- case "move": {
693
- const index1 = get(splittedAtom).indexOf(action.atom);
694
- const index2 = action.before ? get(splittedAtom).indexOf(action.before) : get(splittedAtom).length;
695
- if (index1 >= 0 && index2 >= 0) {
696
- const arr = get(arrAtom);
697
- if (index1 < index2) {
698
- set(arrAtom, [
699
- ...arr.slice(0, index1),
700
- ...arr.slice(index1 + 1, index2),
701
- arr[index1],
702
- ...arr.slice(index2)
703
- ]);
704
- } else {
705
- set(arrAtom, [
706
- ...arr.slice(0, index2),
707
- arr[index1],
708
- ...arr.slice(index2, index1),
709
- ...arr.slice(index1 + 1)
710
- ]);
711
- }
712
- }
713
- break;
714
- }
715
- }
716
- }
717
- ) : atom((get) => get(mappingAtom).atomList);
718
- return splittedAtom;
719
- },
720
- arrAtom,
721
- keyExtractor
722
- );
723
- }
724
589
  const isPromiseLike$1 = (x) => typeof (x == null ? void 0 : x.then) === "function";
725
590
  function createJSONStorage(getStringStorage = () => {
726
591
  try {
@@ -1113,61 +978,10 @@ __publicField(Frame, "defaultProps", {
1113
978
  initialContent: '<!DOCTYPE html><html><head></head><body><div class="frame-root"></div></body></html>'
1114
979
  });
1115
980
  const ChaiFrame = React.forwardRef((props, ref) => /* @__PURE__ */ jsxRuntime.jsx(Frame, { ...props, forwardedRef: ref }));
1116
- const canAcceptChildBlock = (parentType, childType) => {
1117
- if (!parentType) return true;
1118
- const blockDefinition = runtime.getRegisteredChaiBlock(parentType);
1119
- if (!blockDefinition) return false;
1120
- return lodashEs.has(blockDefinition, "canAcceptBlock") ? blockDefinition.canAcceptBlock(childType) : false;
1121
- };
1122
- const canAddChildBlock = (parentType) => {
1123
- const blockDefinition = runtime.getRegisteredChaiBlock(parentType);
1124
- if (!blockDefinition) return false;
1125
- return lodashEs.has(blockDefinition, "canAcceptBlock");
1126
- };
1127
- const canBeNestedInside = (parentType, childType) => {
1128
- const blockDefinition = runtime.getRegisteredChaiBlock(childType);
1129
- if (!blockDefinition) return true;
1130
- return lodashEs.has(blockDefinition, "canBeNested") ? blockDefinition.canBeNested(parentType) : true;
1131
- };
1132
- const canDuplicateBlock = (type) => {
1133
- const blockDefinition = runtime.getRegisteredChaiBlock(type);
1134
- if (!blockDefinition) return true;
1135
- return lodashEs.has(blockDefinition, "canDuplicate") ? blockDefinition.canDuplicate() : true;
1136
- };
1137
- const canDeleteBlock = (type) => {
1138
- const blockDefinition = runtime.getRegisteredChaiBlock(type);
1139
- if (!blockDefinition) return true;
1140
- return lodashEs.has(blockDefinition, "canDelete") ? blockDefinition.canDelete() : true;
1141
- };
1142
- const canDropBlock = (_currentTree, { dragSource, dropTarget: dropTarget2 }) => {
1143
- const dragSourceType = lodashEs.get(dragSource, "data._type", "");
1144
- const dropTargetType = lodashEs.get(dropTarget2, "data._type", "");
1145
- return canAcceptChildBlock(dropTargetType, dragSourceType);
1146
- };
1147
- if (void 0) {
1148
- describe("canDropBlock Function", () => {
1149
- it('should return false if dragSourceType is "Slot"', () => {
1150
- const dragSource = { data: { _type: "Slot" } };
1151
- const dropTarget2 = { data: {} };
1152
- expect(canDropBlock({}, { dragSource, dropTarget: dropTarget2 })).toBe(true);
1153
- });
1154
- it("should return true if dropTargetType is empty", () => {
1155
- const dragSource = { data: { _type: "Box" } };
1156
- const dropTarget2 = { data: {} };
1157
- expect(canDropBlock({}, { dragSource, dropTarget: dropTarget2 })).toBe(true);
1158
- });
1159
- });
1160
- }
1161
981
  function duplicateBlocks(blocks, id, _parent) {
1162
982
  const children = lodashEs.filter(blocks, (c) => c._parent === id);
1163
983
  const newBlocks = [];
1164
984
  for (let i = 0; i < children.length; i++) {
1165
- const slots = getSlots(children[i]);
1166
- if (Object.keys(slots).length > 0) {
1167
- Object.keys(slots).forEach((key) => {
1168
- children[i][key] = `slot:${iconBase.generateUUID()}`;
1169
- });
1170
- }
1171
985
  if (lodashEs.filter(blocks, { _parent: children[i]._id }).length > 0) {
1172
986
  const newId = iconBase.generateUUID();
1173
987
  newBlocks.push({ ...children[i], oldId: children[i]._id, ...{ _id: newId, _parent } });
@@ -1232,15 +1046,21 @@ const getDuplicatedBlocks = (currentBlocks, id, newParentId = null) => {
1232
1046
  return lodashEs.omit(newBlock, ["global", "oldId"]);
1233
1047
  });
1234
1048
  };
1235
- const presentBlocksAtom = jotai.atom([]);
1236
- 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
+ };
1237
1059
  const treeDSBlocks = jotai.atom((get) => {
1238
- const presentBlocks = get(presentBlocksAtom);
1239
- return convertToBlocksTree([...presentBlocks]);
1060
+ const presentBlocks = get(pageBlocksAtom).map((block) => get(block._atom));
1061
+ return convertToBlocksTree(presentBlocks);
1240
1062
  });
1241
1063
  treeDSBlocks.debugLabel = "treeDSBlocks";
1242
- const pageBlocksAtomsAtom = splitAtom(presentBlocksAtom, (block) => block._id);
1243
- pageBlocksAtomsAtom.debugLabel = "pageBlocksAtomsAtom";
1244
1064
  const builderActivePageAtom = jotai.atom("");
1245
1065
  builderActivePageAtom.debugLabel = "builderActivePageAtom";
1246
1066
  const destinationDropIndexAtom = jotai.atom(-1);
@@ -1252,10 +1072,87 @@ const globalBlocksAtom = jotai.atom((get) => {
1252
1072
  return lodashEs.filter(globalBlocks, (block) => lodashEs.has(block, "blockId"));
1253
1073
  });
1254
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
+ };
1255
1152
  const selectedBlockIdsAtom = jotai.atom([]);
1256
1153
  selectedBlockIdsAtom.debugLabel = "selectedBlockIdsAtom";
1257
1154
  const selectedBlocksAtom = jotai.atom((get) => {
1258
- const blocks = get(presentBlocksAtom);
1155
+ const blocks = get(pageBlocksAtom);
1259
1156
  const blockIds = get(selectedBlockIdsAtom);
1260
1157
  return lodashEs.map(
1261
1158
  lodashEs.filter(blocks, ({ _id }) => lodashEs.includes(blockIds, _id)),
@@ -1269,7 +1166,7 @@ const selectedBlockAtom = jotai.atom((get) => {
1269
1166
  return null;
1270
1167
  }
1271
1168
  if (blocks.length === 1) {
1272
- return blocks[0];
1169
+ return get(lodashEs.first(blocks)._atom);
1273
1170
  }
1274
1171
  });
1275
1172
  selectedBlockAtom.debugLabel = "selectedBlockAtom";
@@ -1277,7 +1174,7 @@ const getParentId = (block) => lodashEs.get(block, "_parent", null);
1277
1174
  const selectedBlocksParentsAtom = jotai.atom((get) => {
1278
1175
  const selectedBlocks = get(selectedBlocksAtom);
1279
1176
  const parentIds = lodashEs.map(selectedBlocks, getParentId);
1280
- return lodashEs.filter(get(presentBlocksAtom), (block) => lodashEs.includes(parentIds, block._id));
1177
+ return lodashEs.filter(get(pageBlocksAtom), (block) => lodashEs.includes(parentIds, block._id));
1281
1178
  });
1282
1179
  selectedBlocksParentsAtom.debugLabel = "selectedBlocksParentsAtom";
1283
1180
  const selectedBlockFlexChildAtom = jotai.atom(() => {
@@ -1299,7 +1196,7 @@ const useSelectedBlocksDisplayChild = () => ({
1299
1196
  const useSelectedBlock = () => jotai.useAtomValue(selectedBlockAtom);
1300
1197
  const selectedBlockHierarchy = jotai.atom((get) => {
1301
1198
  const selectedBlock = get(selectedBlockAtom);
1302
- const allBlocks = get(presentBlocksAtom);
1199
+ const allBlocks = get(pageBlocksAtom);
1303
1200
  let block = selectedBlock;
1304
1201
  const blocks = [selectedBlock];
1305
1202
  do {
@@ -1325,6 +1222,241 @@ const useSelectedBlockIds = () => {
1325
1222
  );
1326
1223
  return [blockIds, setBlockIds, toggleSelectedBlockId];
1327
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
+ };
1328
1460
  var undomanager = { exports: {} };
1329
1461
  var hasRequiredUndomanager;
1330
1462
  function requireUndomanager() {
@@ -1480,79 +1612,6 @@ function requireUndomanager() {
1480
1612
  }
1481
1613
  var undomanagerExports = requireUndomanager();
1482
1614
  const UndoManager = /* @__PURE__ */ ChaiThemeFn.getDefaultExportFromCjs(undomanagerExports);
1483
- function getBlocksTree(blocks) {
1484
- return convertToBlocksTree(blocks);
1485
- }
1486
- const nestedToFlatArray = (nestedJson, parent) => lodashEs.flatten(
1487
- nestedJson.map((block) => {
1488
- block = parent ? { ...block, _parent: parent } : { ...block };
1489
- if (block.children) {
1490
- const children = [...block.children];
1491
- delete block.children;
1492
- return lodashEs.flatten([block, ...nestedToFlatArray(children, block._id)]);
1493
- }
1494
- return block;
1495
- })
1496
- );
1497
- function setProjectBlocksInMemory(nodes, initial = false) {
1498
- for (let i = 0; i < nodes.length; i++) {
1499
- const element = nodes[i];
1500
- if (element.global && !initial) {
1501
- nodes[i] = {
1502
- type: "GlobalBlock",
1503
- blockId: element.blockId,
1504
- _parent: lodashEs.get(element, "_parent", null),
1505
- _id: element._id
1506
- };
1507
- } else if (element.children && element.children.length) {
1508
- setProjectBlocksInMemory(element.children);
1509
- }
1510
- }
1511
- }
1512
- function getInnerBlocks(flatArr) {
1513
- let blocks = [];
1514
- let pBlocks = lodashEs.filter(flatArr, { type: "GlobalBlock" });
1515
- if (pBlocks.length > 0) {
1516
- pBlocks = lodashEs.map(pBlocks, getPBlocks);
1517
- lodashEs.each(pBlocks, (pBlock) => {
1518
- blocks = [...blocks, ...getSingleBlock(pBlock)];
1519
- });
1520
- }
1521
- return blocks;
1522
- }
1523
- function getSingleBlock(flatArray) {
1524
- let blocks = [];
1525
- const parent = lodashEs.get(lodashEs.first(flatArray), "_parent", null);
1526
- lodashEs.set(lodashEs.first(flatArray), "_parent", null);
1527
- const block = [flatToNestedInstance.convert(clone(flatArray))];
1528
- setProjectBlocksInMemory(block, true);
1529
- let flat = nestedToFlatArray(block, flatArray[0]._id);
1530
- flat = lodashEs.set(flat, "0._parent", parent);
1531
- blocks = [...blocks, flat, ...getInnerBlocks(flat)];
1532
- return blocks;
1533
- }
1534
- function getPBlocks(block) {
1535
- const rootBlock = lodashEs.find(FLAT_ARRAY, { _id: block._id });
1536
- if (!rootBlock) return [];
1537
- const blocks = [rootBlock];
1538
- const children = lodashEs.filter(FLAT_ARRAY, { _parent: block._id });
1539
- if (children.length) {
1540
- return lodashEs.flatten([...blocks, ...lodashEs.flatten(lodashEs.map(children, getPBlocks))]);
1541
- }
1542
- return lodashEs.flatten(blocks);
1543
- }
1544
- const clone = (obj) => JSON.parse(JSON.stringify(obj));
1545
- let FLAT_ARRAY = [];
1546
- function splitPageBlocks(allPageBlocks) {
1547
- FLAT_ARRAY = allPageBlocks;
1548
- const clonedTree = getBlocksTree(clone(allPageBlocks));
1549
- setProjectBlocksInMemory(clonedTree);
1550
- const pageBlocks = nestedToFlatArray(clonedTree, null);
1551
- const globalBlocks = getInnerBlocks(pageBlocks);
1552
- const mappedBlocks = {};
1553
- lodashEs.each(globalBlocks, (projectBlock) => lodashEs.set(mappedBlocks, lodashEs.first(projectBlock).blockId, projectBlock));
1554
- return [pageBlocks, mappedBlocks];
1555
- }
1556
1615
  const MODIFIERS = [
1557
1616
  "hover",
1558
1617
  "focus",
@@ -1633,9 +1692,10 @@ const useGetPageData = () => {
1633
1692
  const [projectOptions] = useBrandingOptions();
1634
1693
  const { currentPage } = useCurrentPage();
1635
1694
  const [presentBlocks] = useBlocksStore();
1695
+ const getBlockAtomValue = useGetBlockAtomValue();
1636
1696
  return React.useCallback(() => {
1637
1697
  const blocks = lodashEs.map(presentBlocks, (block) => {
1638
- return lodashEs.omit(block, getBlockBuilderProps(block._type));
1698
+ return lodashEs.omit(getBlockAtomValue(block._id), getBlockBuilderProps(block._type));
1639
1699
  });
1640
1700
  const [pageFilteredBlocks = []] = splitPageBlocks(blocks);
1641
1701
  return {
@@ -1763,190 +1823,8 @@ const useUndoManager = () => {
1763
1823
  clear: undoManager.clear
1764
1824
  };
1765
1825
  };
1766
- const useAtomSetterCb = () => {
1767
- return useAtomCallback(
1768
- React.useCallback((get, set, id) => {
1769
- const blockAtoms = get(pageBlocksAtomsAtom);
1770
- const blockAtom = lodashEs.find(blockAtoms, (atom2) => get(atom2)._id === id);
1771
- if (!blockAtom) return (_props) => void 0;
1772
- return (props) => set(blockAtom, { ...get(blockAtom), ...props });
1773
- }, [])
1774
- );
1775
- };
1776
- const broadcastChannel = new BroadcastChannel("chaibuilder");
1777
- const useBroadcastChannel = () => {
1778
- const pageId = useBuilderProp("pageId", "chaibuilder_page");
1779
- const postMessage = web.useDebouncedCallback(
1780
- (message) => broadcastChannel.postMessage({ ...message, pageId }),
1781
- [pageId],
1782
- 200
1783
- );
1784
- return { postMessage };
1785
- };
1786
- const useUnmountBroadcastChannel = () => {
1787
- const [, setBlocks] = useBlocksStore();
1788
- const pageId = useBuilderProp("pageId", "chaibuilder_page");
1789
- const { updateBlocksProps } = useBlocksStoreManager();
1790
- React.useEffect(() => {
1791
- broadcastChannel.onmessageerror = (event) => {
1792
- console.log("error", event);
1793
- };
1794
- broadcastChannel.onmessage = (event) => {
1795
- if (event.data.type === "blocks-updated" && event.data.pageId === pageId) {
1796
- setBlocks(event.data.blocks);
1797
- }
1798
- if (event.data.type === "blocks-props-updated" && event.data.pageId === pageId) {
1799
- updateBlocksProps(event.data.blocks);
1800
- }
1801
- };
1802
- return () => {
1803
- broadcastChannel.onmessage = null;
1804
- broadcastChannel.onmessageerror = null;
1805
- };
1806
- }, [setBlocks, pageId]);
1807
- };
1808
- const removeNestedBlocks = (blocks, blockIds) => {
1809
- const _blockIds = [];
1810
- const _blocks = lodashEs.filter(blocks, (block) => {
1811
- if (lodashEs.includes(blockIds, block._id) || lodashEs.includes(blockIds, block._parent)) {
1812
- _blockIds.push(block._id);
1813
- return false;
1814
- }
1815
- return true;
1816
- });
1817
- if (!lodashEs.isEmpty(_blockIds)) return removeNestedBlocks(_blocks, _blockIds);
1818
- return _blocks;
1819
- };
1820
- const useRemoveBlocks = () => {
1821
- const [presentBlocks] = useBlocksStore();
1822
- const [ids2, setSelectedIds] = useSelectedBlockIds();
1823
- const { setNewBlocks } = useBlocksStoreUndoableActions();
1824
- return React.useCallback(
1825
- (blockIds) => {
1826
- var _a;
1827
- const parentBlockId = ((_a = lodashEs.find(presentBlocks, { _id: blockIds[0] })) == null ? void 0 : _a._parent) || null;
1828
- setNewBlocks(removeNestedBlocks(presentBlocks, blockIds));
1829
- setTimeout(() => setSelectedIds(parentBlockId ? [parentBlockId] : []), 200);
1830
- },
1831
- [presentBlocks, setSelectedIds, ids2]
1832
- );
1833
- };
1834
- function insertBlocksAtPosition(allBlocks, newBlocks, parentId, position) {
1835
- let parentBlocks = allBlocks.filter((block) => !block._parent);
1836
- if (parentId) {
1837
- parentBlocks = allBlocks.filter((block) => block._parent === parentId);
1838
- }
1839
- const insertPosition = !isNaN(position) || position > -1 ? Math.min(position, parentBlocks.length) : parentBlocks.length;
1840
- let insertIndex = allBlocks.length;
1841
- for (let i = 0, count = 0; i < allBlocks.length; i++) {
1842
- if (allBlocks[i]._parent === parentId) {
1843
- if (count === insertPosition) {
1844
- insertIndex = i;
1845
- break;
1846
- }
1847
- count++;
1848
- }
1849
- }
1850
- if (!parentId && position !== void 0 && position >= parentBlocks.length) {
1851
- insertIndex = allBlocks.length;
1852
- }
1853
- return [...allBlocks.slice(0, insertIndex), ...newBlocks, ...allBlocks.slice(insertIndex)];
1854
- }
1855
- function flattenTree(node) {
1856
- let flatArray = [];
1857
- node.walk((n) => {
1858
- delete n.model.children;
1859
- flatArray.push(n.model);
1860
- return true;
1861
- });
1862
- return flatArray;
1863
- }
1864
- function findNodeById(node, id) {
1865
- return node.first((n) => n.model._id === id) || null;
1866
- }
1867
- function moveNode(rootNode, nodeIdToMove, newParentId, position) {
1868
- const nodeToMove = findNodeById(rootNode, nodeIdToMove);
1869
- const newParentNode = findNodeById(rootNode, newParentId);
1870
- if (nodeToMove && newParentNode) {
1871
- nodeToMove.drop();
1872
- if (!newParentNode.children) {
1873
- newParentNode.model.children = [];
1874
- }
1875
- try {
1876
- newParentNode.addChildAtIndex(nodeToMove, position);
1877
- } catch (error) {
1878
- console.error("Error adding child to parent:", error);
1879
- return false;
1880
- }
1881
- return true;
1882
- }
1883
- return false;
1884
- }
1885
- function moveBlocksWithChildren(_blocks, idToMove, newParentId, newPosition) {
1886
- if (!idToMove) return _blocks;
1887
- newParentId = newParentId || "root";
1888
- const tree2 = new TreeModel();
1889
- const root = tree2.parse({ _id: "root", children: getBlocksTree(_blocks) });
1890
- if (moveNode(root, idToMove, newParentId, newPosition)) {
1891
- const newBlocks = flattenTree(root);
1892
- const movedBlock = newBlocks.find((block) => block._id === idToMove);
1893
- if (movedBlock) movedBlock._parent = newParentId === "root" ? null : newParentId;
1894
- newBlocks.shift();
1895
- return newBlocks;
1896
- }
1897
- return _blocks;
1898
- }
1899
- const useBlocksStoreManager = () => {
1900
- const [, setBlocks] = useBlocksStore();
1901
- const { postMessage } = useBroadcastChannel();
1902
- const atomSetterCb = useAtomSetterCb();
1903
- const updateAtomProps = React.useCallback(
1904
- (id, props) => {
1905
- const setter = atomSetterCb(id);
1906
- setter({ ...props });
1907
- },
1908
- [atomSetterCb]
1909
- );
1910
- return {
1911
- setNewBlocks: (newBlocks) => {
1912
- setBlocks(newBlocks);
1913
- postMessage({ type: "blocks-updated", blocks: newBlocks });
1914
- },
1915
- addBlocks: (newBlocks, parent, position) => {
1916
- setBlocks((prevBlocks) => {
1917
- const blocks = insertBlocksAtPosition(prevBlocks, newBlocks, parent, position);
1918
- postMessage({ type: "blocks-updated", blocks });
1919
- return blocks;
1920
- });
1921
- },
1922
- removeBlocks: (blockIds) => {
1923
- setBlocks((prevBlocks) => {
1924
- const blocks = removeNestedBlocks(prevBlocks, blockIds);
1925
- postMessage({ type: "blocks-updated", blocks });
1926
- return blocks;
1927
- });
1928
- },
1929
- moveBlocks: (blockIds, newParent, position) => {
1930
- setBlocks((prevBlocks) => {
1931
- let blocks = prevBlocks;
1932
- for (let i = 0; i < blockIds.length; i++) {
1933
- blocks = moveBlocksWithChildren(blocks, blockIds[i], newParent, position);
1934
- }
1935
- postMessage({ type: "blocks-updated", blocks });
1936
- return blocks;
1937
- });
1938
- },
1939
- updateBlocksProps: (blocks) => {
1940
- blocks.forEach((block) => {
1941
- const updatedBlock = lodashEs.omit(block, "_id");
1942
- updateAtomProps(block._id, updatedBlock);
1943
- });
1944
- postMessage({ type: "blocks-props-updated", blocks });
1945
- }
1946
- };
1947
- };
1948
1826
  const useBlocksStore = () => {
1949
- return jotai.useAtom(presentBlocksAtom);
1827
+ return jotai.useAtom(pageBlocksAtom);
1950
1828
  };
1951
1829
  const useBlocksStoreUndoableActions = () => {
1952
1830
  const { add } = useUndoManager();
@@ -2080,7 +1958,7 @@ const useAddBlock = () => {
2080
1958
  blocks[0]._parent = parentBlock._parent;
2081
1959
  parentBlockId = parentBlock._parent;
2082
1960
  }
2083
- addBlocks(blocks, parentBlockId, position);
1961
+ addBlocks(convertToBlocksAtoms(blocks), parentBlockId, position);
2084
1962
  setSelected([(_a = lodashEs.first(blocks)) == null ? void 0 : _a._id]);
2085
1963
  return lodashEs.first(blocks);
2086
1964
  },
@@ -2112,7 +1990,7 @@ const useAddBlock = () => {
2112
1990
  parentBlockId = parentBlock._parent;
2113
1991
  }
2114
1992
  const newBlocks = [newBlock];
2115
- addBlocks(newBlocks, parentBlockId, position);
1993
+ addBlocks(convertToBlocksAtoms(newBlocks), parentBlockId, position);
2116
1994
  setSelected([newBlock._id]);
2117
1995
  return newBlock;
2118
1996
  },
@@ -3310,12 +3188,9 @@ const selectedStylingBlocksAtom = jotai.atom([]);
3310
3188
  selectedStylingBlocksAtom.debugLabel = "selectedStylingBlocksAtom";
3311
3189
  const useSelectedStylingBlocks = () => jotai.useAtom(selectedStylingBlocksAtom);
3312
3190
  const addClassesToBlocksAtom = jotai.atom(null, (get, _set, { blockIds, newClasses }) => {
3313
- const blockAtoms = lodashEs.filter(
3314
- get(pageBlocksAtomsAtom),
3315
- (blockAtom) => (
3316
- // @ts-ignore
3317
- blockIds.includes(get(blockAtom)._id)
3318
- )
3191
+ const blockAtoms = lodashEs.map(
3192
+ lodashEs.filter(get(pageBlocksAtom), (block) => blockIds.includes(block._id)),
3193
+ (block) => block._atom
3319
3194
  );
3320
3195
  const styleBlock = lodashEs.first(get(selectedStylingBlocksAtom));
3321
3196
  return lodashEs.map(blockAtoms, (blockAtom) => {
@@ -3429,7 +3304,7 @@ const useDuplicateBlocks = () => {
3429
3304
  const blockPosition = siblingBlocks.indexOf(block);
3430
3305
  const newBlockPosition = blockPosition + 1;
3431
3306
  const newBlocks = getDuplicatedBlocks(presentBlocks, blockId, parentId);
3432
- addBlocks(newBlocks, parentId, newBlockPosition);
3307
+ addBlocks(convertToBlocksAtoms(newBlocks), parentId, newBlockPosition);
3433
3308
  newBlockIds.push(lodashEs.get(newBlocks, "0._id", ""));
3434
3309
  });
3435
3310
  setSelected(newBlockIds);
@@ -3463,7 +3338,7 @@ const useCanPaste = () => {
3463
3338
  };
3464
3339
  };
3465
3340
  const useMoveCutBlocks = () => {
3466
- const presentBlocks = jotai.useAtomValue(presentBlocksAtom);
3341
+ const presentBlocks = jotai.useAtomValue(pageBlocksAtom);
3467
3342
  const { moveBlocks: moveBlocks2 } = useBlocksStoreUndoableActions();
3468
3343
  return React.useCallback(
3469
3344
  (blockIds, newParentId) => {
@@ -3541,12 +3416,9 @@ const usePreviewMode = () => {
3541
3416
  };
3542
3417
  const removeClassFromBlocksAtom = jotai.atom(null, (get, _set, { blockIds, fullClasses }) => {
3543
3418
  const styleBlock = lodashEs.first(get(selectedStylingBlocksAtom));
3544
- const blockAtoms = lodashEs.filter(
3545
- get(pageBlocksAtomsAtom),
3546
- (blockAtom) => (
3547
- // @ts-ignore
3548
- blockIds.includes(get(blockAtom)._id)
3549
- )
3419
+ const blockAtoms = lodashEs.map(
3420
+ lodashEs.filter(get(pageBlocksAtom), (block) => blockIds.includes(block._id)),
3421
+ (block) => block._atom
3550
3422
  );
3551
3423
  return lodashEs.map(blockAtoms, (blockAtom) => {
3552
3424
  const block = get(blockAtom);
@@ -3778,25 +3650,22 @@ const globalBlocksStoreAtom = atom({});
3778
3650
  const globalBlocksLoadingStateAtom = atom({});
3779
3651
  const useGlobalBlocksStore = () => {
3780
3652
  const [globalBlocks, setGlobalBlocks] = useAtom(globalBlocksStoreAtom);
3781
- const getGlobalBlocks = React.useCallback(
3782
- (block) => {
3783
- return lodashEs.get(globalBlocks, block == null ? void 0 : block.globalBlock, []);
3784
- },
3785
- [globalBlocks]
3786
- );
3787
- const reset = React.useCallback(() => {
3788
- setGlobalBlocks({});
3789
- }, [setGlobalBlocks]);
3653
+ const getGlobalBlocks = React.useCallback((globalBlockId) => lodashEs.get(globalBlocks, globalBlockId, []), [globalBlocks]);
3654
+ const reset = React.useCallback(() => setGlobalBlocks({}), [setGlobalBlocks]);
3790
3655
  return { getGlobalBlocks, reset };
3791
3656
  };
3792
3657
  const useWatchGlobalBlocks = () => {
3793
3658
  const [blocksStore] = useBlocksStore();
3794
3659
  const [globalBlocks, setGlobalBlocks] = useAtom(globalBlocksStoreAtom);
3795
3660
  const [globalBlocksLoadingState, setGlobalBlocksLoadingState] = useAtom(globalBlocksLoadingStateAtom);
3661
+ const getBlockAtomValue = useGetBlockAtomValue();
3796
3662
  const getGlobalBlockBlocks = useBuilderProp("getGlobalBlockBlocks", async (_key) => []);
3797
3663
  const globalBlocksList = React.useMemo(() => {
3798
3664
  const globalBlocks2 = blocksStore.filter((block) => block._type === "GlobalBlock");
3799
- 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
+ });
3800
3669
  }, [blocksStore]);
3801
3670
  React.useEffect(() => {
3802
3671
  lodashEs.forEach(globalBlocksList, (globalBlock) => {
@@ -4198,7 +4067,7 @@ const useAiAssistant = () => {
4198
4067
  };
4199
4068
  const wrapperBlockAtom = jotai.atom((get) => {
4200
4069
  var _a;
4201
- const blocks = get(presentBlocksAtom);
4070
+ const blocks = get(pageBlocksAtom);
4202
4071
  const blockIds = get(selectedBlockIdsAtom);
4203
4072
  const blockId = blockIds.length === 1 ? blockIds[0] : null;
4204
4073
  if (!blockId) return null;
@@ -4675,10 +4544,10 @@ const useHandleCanvasDblClick = () => {
4675
4544
  const updateContent = useUpdateBlocksProps();
4676
4545
  const [editingBlockId, setEditingBlockId] = jotai.useAtom(inlineEditingActiveAtom);
4677
4546
  const { clearHighlight } = useBlockHighlight();
4678
- const [blocks] = useBlocksStore();
4547
+ const getBlockAtomValue = useGetBlockAtomValue();
4679
4548
  return React.useCallback(
4680
4549
  (e2) => {
4681
- var _a, _b;
4550
+ var _a;
4682
4551
  if (editingBlockId) return;
4683
4552
  const chaiBlock = getTargetedBlock(e2.target);
4684
4553
  const blockType = chaiBlock.getAttribute("data-block-type");
@@ -4686,7 +4555,7 @@ const useHandleCanvasDblClick = () => {
4686
4555
  return;
4687
4556
  }
4688
4557
  const blockId = chaiBlock.getAttribute("data-block-id");
4689
- const content = (_a = lodashEs.find(blocks, { _id: blockId })) == null ? void 0 : _a.content;
4558
+ const content = getBlockAtomValue(blockId)["content"];
4690
4559
  const newBlock = chaiBlock.cloneNode(true);
4691
4560
  newBlock.innerHTML = content;
4692
4561
  chaiBlock.style.display = "none";
@@ -4718,10 +4587,10 @@ const useHandleCanvasDblClick = () => {
4718
4587
  }
4719
4588
  });
4720
4589
  quill.focus();
4721
- (_b = newBlock.querySelector(".ql-clipboard")) == null ? void 0 : _b.remove();
4590
+ (_a = newBlock.querySelector(".ql-clipboard")) == null ? void 0 : _a.remove();
4722
4591
  setEditingBlockId(chaiBlock.getAttribute("data-block-id"));
4723
4592
  },
4724
- [editingBlockId, blocks, setEditingBlockId, updateContent, clearHighlight]
4593
+ [editingBlockId, setEditingBlockId, updateContent, clearHighlight]
4725
4594
  );
4726
4595
  };
4727
4596
  const useHandleCanvasClick = () => {
@@ -5015,7 +4884,8 @@ const useBlockRuntimeProps = () => {
5015
4884
  [allBlocks]
5016
4885
  );
5017
4886
  };
5018
- const BlockRenderer = ({ block, children }) => {
4887
+ const BlockRenderer = ({ blockAtom, children }) => {
4888
+ const [block] = jotai.useAtom(blockAtom);
5019
4889
  const registeredChaiBlock = React.useMemo(() => runtime.getRegisteredChaiBlock(block._type), [block._type]);
5020
4890
  const { selectedLang, fallbackLang } = useLanguages();
5021
4891
  const getRuntimePropValues = useBlockRuntimeProps();
@@ -5052,16 +4922,21 @@ const BlockRenderer = ({ block, children }) => {
5052
4922
  if (lodashEs.isNull(Component2) || hiddenBlocks.includes(block._id) || editingBlockId === block._id) return null;
5053
4923
  return /* @__PURE__ */ jsxRuntime.jsx(React.Suspense, { children: React.createElement(Component2, { ...props, children }) });
5054
4924
  };
5055
- const GlobalBlocksRenderer = ({ block }) => {
4925
+ const GlobalBlocksRenderer = ({ blockAtom }) => {
5056
4926
  const { getGlobalBlocks } = useGlobalBlocksStore();
5057
- const globalBlocks = getGlobalBlocks(block);
5058
- return /* @__PURE__ */ jsxRuntime.jsx(BlocksRenderer, { blocks: globalBlocks, filterFn: (block2) => lodashEs.isEmpty(block2._parent) });
5059
- };
5060
- 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
+ }) => {
5061
4936
  const filteredBlocks = React.useMemo(() => lodashEs.filter(blocks, filterFn), [blocks, filterFn]);
5062
4937
  const hasChildren = (blockId) => lodashEs.filter(blocks, (b) => b._parent === blockId).length > 0;
5063
4938
  return lodashEs.map(filteredBlocks, (block) => {
5064
- 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);
5065
4940
  });
5066
4941
  };
5067
4942
  const PageBlocksRenderer = () => {
@@ -6278,7 +6153,7 @@ const JSONForm = React.memo(({ blockId, schema, uiSchema, formData, onChange })
6278
6153
  onChange({ formData: formData2 }, id);
6279
6154
  },
6280
6155
  [onChange, selectedLang],
6281
- 1e3
6156
+ 400
6282
6157
  // save only every 5 seconds
6283
6158
  );
6284
6159
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -11004,7 +10879,7 @@ const getParentNodeIds = (blocks, id) => {
11004
10879
  const expandedIdsAtom = jotai.atom([]);
11005
10880
  const useExpandTree = () => {
11006
10881
  const [ids2] = useSelectedBlockIds();
11007
- const pageBlocks = jotai.useAtomValue(presentBlocksAtom);
10882
+ const pageBlocks = jotai.useAtomValue(pageBlocksAtom);
11008
10883
  const [, setExpandedIds] = jotai.useAtom(expandedIdsAtom);
11009
10884
  React.useEffect(() => {
11010
10885
  let expandedIds = [];
@@ -11645,7 +11520,7 @@ const ChaiBuilderComponent = (props) => {
11645
11520
  React.useEffect(() => {
11646
11521
  setTimeout(() => {
11647
11522
  const withDefaults = runtime.syncBlocksWithDefaults(props.blocks || []);
11648
- setAllBlocks(withDefaults);
11523
+ setAllBlocks(convertToBlocksAtoms(withDefaults));
11649
11524
  if (withDefaults && withDefaults.length > 0) {
11650
11525
  postMessage({ type: "blocks-updated", blocks: withDefaults });
11651
11526
  }