@creopse/react 0.0.11 → 0.0.13

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/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const React = require("react");
4
- const index = require("./index-C3UpNpqk.cjs");
4
+ const content = require("./content-WeAhwlM_.cjs");
5
5
  require("@inertiajs/react");
6
6
  const reactDom = require("react-dom");
7
7
  function _interopNamespaceDefault(e) {
@@ -424,7 +424,7 @@ const AsyncImg = ({
424
424
  };
425
425
  const Image = (props) => {
426
426
  const { src, alt = "", width, height, title, loading, style, size = "original", sync = false } = props;
427
- const { getImage } = index.useHelper();
427
+ const { getImage } = content.useHelper();
428
428
  const filteredProps = {
429
429
  alt,
430
430
  width,
@@ -454,9 +454,9 @@ function addUniqueItem(arr, item) {
454
454
  arr.push(item);
455
455
  }
456
456
  function removeItem(arr, item) {
457
- const index2 = arr.indexOf(item);
458
- if (index2 > -1)
459
- arr.splice(index2, 1);
457
+ const index = arr.indexOf(item);
458
+ if (index > -1)
459
+ arr.splice(index, 1);
460
460
  }
461
461
  const clamp = (min, max, v) => {
462
462
  if (v > max)
@@ -1535,8 +1535,8 @@ const isNotNull$1 = (value) => value !== null;
1535
1535
  function getFinalKeyframe$1(keyframes2, { repeat, repeatType = "loop" }, finalKeyframe, speed = 1) {
1536
1536
  const resolvedKeyframes = keyframes2.filter(isNotNull$1);
1537
1537
  const useFirstKeyframe = speed < 0 || repeat && repeatType !== "loop" && repeat % 2 === 1;
1538
- const index2 = useFirstKeyframe ? 0 : resolvedKeyframes.length - 1;
1539
- return !index2 || finalKeyframe === void 0 ? resolvedKeyframes[index2] : finalKeyframe;
1538
+ const index = useFirstKeyframe ? 0 : resolvedKeyframes.length - 1;
1539
+ return !index || finalKeyframe === void 0 ? resolvedKeyframes[index] : finalKeyframe;
1540
1540
  }
1541
1541
  const transitionTypeMap = {
1542
1542
  decay: inertia,
@@ -3921,8 +3921,8 @@ function makeLatestValues(props, context, presenceContext, scrapeMotionValues) {
3921
3921
  for (const key in target) {
3922
3922
  let valueTarget = target[key];
3923
3923
  if (Array.isArray(valueTarget)) {
3924
- const index2 = isInitialAnimationBlocked ? valueTarget.length - 1 : 0;
3925
- valueTarget = valueTarget[index2];
3924
+ const index = isInitialAnimationBlocked ? valueTarget.length - 1 : 0;
3925
+ valueTarget = valueTarget[index];
3926
3926
  }
3927
3927
  if (valueTarget !== null) {
3928
3928
  values[key] = valueTarget;
@@ -4875,8 +4875,8 @@ function getOptimisedAppearId(visualElement) {
4875
4875
  const isNotNull = (value) => value !== null;
4876
4876
  function getFinalKeyframe(keyframes2, { repeat, repeatType = "loop" }, finalKeyframe) {
4877
4877
  const resolvedKeyframes = keyframes2.filter(isNotNull);
4878
- const index2 = repeat && repeatType !== "loop" && repeat % 2 === 1 ? 0 : resolvedKeyframes.length - 1;
4879
- return resolvedKeyframes[index2];
4878
+ const index = repeat && repeatType !== "loop" && repeat % 2 === 1 ? 0 : resolvedKeyframes.length - 1;
4879
+ return resolvedKeyframes[index];
4880
4880
  }
4881
4881
  const underDampedSpring = {
4882
4882
  type: "spring",
@@ -8087,24 +8087,131 @@ const StickyTop = ({
8087
8087
  }
8088
8088
  );
8089
8089
  };
8090
- const RootContainer = ({
8091
- page,
8092
- sections = [],
8093
- components = {}
8094
- }) => {
8090
+ const byteToHex = [];
8091
+ for (let i = 0; i < 256; ++i) {
8092
+ byteToHex.push((i + 256).toString(16).slice(1));
8093
+ }
8094
+ function unsafeStringify(arr, offset = 0) {
8095
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
8096
+ }
8097
+ let getRandomValues;
8098
+ const rnds8 = new Uint8Array(16);
8099
+ function rng() {
8100
+ if (!getRandomValues) {
8101
+ if (typeof crypto === "undefined" || !crypto.getRandomValues) {
8102
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
8103
+ }
8104
+ getRandomValues = crypto.getRandomValues.bind(crypto);
8105
+ }
8106
+ return getRandomValues(rnds8);
8107
+ }
8108
+ const randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
8109
+ const native = { randomUUID };
8110
+ function _v4(options, buf, offset) {
8111
+ options = options || {};
8112
+ const rnds = options.random ?? options.rng?.() ?? rng();
8113
+ if (rnds.length < 16) {
8114
+ throw new Error("Random bytes length must be >= 16");
8115
+ }
8116
+ rnds[6] = rnds[6] & 15 | 64;
8117
+ rnds[8] = rnds[8] & 63 | 128;
8118
+ return unsafeStringify(rnds);
8119
+ }
8120
+ function v4(options, buf, offset) {
8121
+ if (native.randomUUID && true && !options) {
8122
+ return native.randomUUID();
8123
+ }
8124
+ return _v4(options);
8125
+ }
8126
+ const useSetup = () => {
8127
+ const getComponents = React.useCallback(
8128
+ (resolveSections) => {
8129
+ const components = {};
8130
+ if (!resolveSections) {
8131
+ throw new Error("[@creopse/react] resolveSections is required");
8132
+ }
8133
+ const files = resolveSections();
8134
+ for (const [key, value] of Object.entries(files)) {
8135
+ const componentName = key.replace(/^\.\/(.*)\.\w+$/, "$1");
8136
+ const parts = componentName.split("/");
8137
+ const name = parts[parts.length - 1]?.split(".")[0];
8138
+ components[name] = value.default;
8139
+ }
8140
+ return components;
8141
+ },
8142
+ []
8143
+ );
8144
+ const getSectionsInOrder = React.useCallback(
8145
+ (props) => {
8146
+ const sections = props.pageData?.sections;
8147
+ const sectionsOrder = props.pageData?.sectionsOrder;
8148
+ const sectionsOrdered = [];
8149
+ if (Array.isArray(sections) && Array.isArray(sectionsOrder)) {
8150
+ for (const section of sectionsOrder) {
8151
+ if (section) {
8152
+ const item = sections.find(
8153
+ (value) => `${value.slug}__${value.pivot?.linkId}` === section
8154
+ );
8155
+ if (item) {
8156
+ sectionsOrdered.push(item);
8157
+ }
8158
+ }
8159
+ }
8160
+ }
8161
+ return sectionsOrdered.length ? sectionsOrdered : sections || [];
8162
+ },
8163
+ []
8164
+ );
8165
+ const getFinalPageSections = React.useCallback(
8166
+ (props) => {
8167
+ const sections = getSectionsInOrder(props);
8168
+ const sectionsDisabled = props.pageData?.sectionsDisabled;
8169
+ return sections.filter((section) => {
8170
+ return !sectionsDisabled?.includes(
8171
+ `${section.slug}__${section.pivot?.linkId}`
8172
+ );
8173
+ });
8174
+ },
8175
+ [getSectionsInOrder]
8176
+ );
8177
+ return {
8178
+ getComponents,
8179
+ getSectionsInOrder,
8180
+ getFinalPageSections
8181
+ };
8182
+ };
8183
+ const RootContainer = () => {
8184
+ const { page } = content.useContent();
8185
+ const props = content.useProps();
8186
+ const manager = React.useContext(content.PropsContext);
8187
+ const resolveSections = React.useContext(content.ResolveSectionsContext);
8188
+ const { getComponents, getFinalPageSections } = useSetup();
8189
+ const [sections, setSections] = React.useState(
8190
+ () => getFinalPageSections(props)
8191
+ );
8192
+ const [components] = React.useState(() => getComponents(resolveSections));
8193
+ const [sectionsWrapperKey, setSectionsWrapperKey] = React.useState(v4());
8194
+ const [sectionKeys, setSectionKeys] = React.useState({});
8195
+ const sectionsStateRef = React.useRef({});
8196
+ const pagePropsCopyRef = React.useRef(content.cloneDeep(props));
8197
+ React.useEffect(() => {
8198
+ const keys = {};
8199
+ sections.forEach((section) => {
8200
+ keys[`${section.slug}__${section.pivot?.linkId}-key`] = v4();
8201
+ });
8202
+ setSectionKeys(keys);
8203
+ }, [sections]);
8095
8204
  React.useEffect(() => {
8096
- const timeout = setTimeout(() => {
8097
- const sectionData = page.props?.sectionData;
8205
+ const timer = setTimeout(() => {
8206
+ const sectionData = page.props.sectionData;
8098
8207
  if (sectionData && sections.find(
8099
8208
  (section) => section.slug === sectionData.slug && section.pivot?.linkId === sectionData.pivot?.linkId
8100
8209
  )) {
8101
- index.slideToId(`${sectionData.slug}__${sectionData.pivot?.linkId}-container`);
8210
+ content.slideToId(`${sectionData.slug}__${sectionData.pivot?.linkId}-container`);
8102
8211
  }
8103
8212
  }, 1e3);
8104
- return () => clearTimeout(timeout);
8105
- }, [page, sections]);
8106
- const sectionsStateRef = React.useRef({});
8107
- const sectionsRef = React.useRef(sections);
8213
+ return () => clearTimeout(timer);
8214
+ }, [page.props.sectionData, sections]);
8108
8215
  React.useEffect(() => {
8109
8216
  const deselectAllSections = () => {
8110
8217
  Object.keys(sectionsStateRef.current).forEach((key) => {
@@ -8118,16 +8225,16 @@ const RootContainer = ({
8118
8225
  };
8119
8226
  const handleMessage = (event) => {
8120
8227
  switch (event.data?.type) {
8121
- case index.EditorMessageType.RELOAD:
8228
+ case content.EditorMessageType.RELOAD:
8122
8229
  sessionStorage.setItem("shouldNotifyReload", "1");
8123
8230
  sessionStorage.setItem("replyOrigin", event.origin);
8124
8231
  window.location.reload();
8125
8232
  break;
8126
- case index.EditorMessageType.ENABLE_EDITION_MODE: {
8233
+ case content.EditorMessageType.ENABLE_EDITION_MODE: {
8127
8234
  const primaryColor = event.data?.primaryColor ?? "blue";
8128
8235
  sessionStorage.setItem("replyOrigin", event.origin);
8129
8236
  sessionStorage.setItem("primaryColor", primaryColor);
8130
- sectionsRef.current?.forEach((section) => {
8237
+ sections?.forEach((section) => {
8131
8238
  if (section.slug && section.pivot?.linkId) {
8132
8239
  const sectionElementId = `${section.slug}__${section.pivot?.linkId}-container`;
8133
8240
  const sectionElement = document.getElementById(sectionElementId);
@@ -8145,7 +8252,7 @@ const RootContainer = ({
8145
8252
  this.style.boxShadow = `0 0 10px ${primaryColor}`;
8146
8253
  window.parent.postMessage(
8147
8254
  {
8148
- type: index.EditorMessageType.SELECT_PREVIEW_SECTION,
8255
+ type: content.EditorMessageType.SELECT_PREVIEW_SECTION,
8149
8256
  slug: section.slug,
8150
8257
  linkId: section.pivot?.linkId
8151
8258
  },
@@ -8160,15 +8267,15 @@ const RootContainer = ({
8160
8267
  });
8161
8268
  break;
8162
8269
  }
8163
- case index.EditorMessageType.DESELECT_ALL_SECTIONS:
8270
+ case content.EditorMessageType.DESELECT_ALL_SECTIONS:
8164
8271
  deselectAllSections();
8165
8272
  break;
8166
- case index.EditorMessageType.SELECT_SIDEBAR_SECTION: {
8273
+ case content.EditorMessageType.SELECT_SIDEBAR_SECTION: {
8167
8274
  setTimeout(() => {
8168
- if (sectionsRef.current?.find(
8275
+ if (sections.find(
8169
8276
  (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8170
8277
  )) {
8171
- index.slideToId(`${event.data?.slug}__${event.data?.linkId}-container`);
8278
+ content.slideToId(`${event.data?.slug}__${event.data?.linkId}-container`);
8172
8279
  }
8173
8280
  }, 1e3);
8174
8281
  const elementId = `${event.data?.slug}__${event.data?.linkId}-container`;
@@ -8182,23 +8289,72 @@ const RootContainer = ({
8182
8289
  }
8183
8290
  break;
8184
8291
  }
8185
- case index.EditorMessageType.SEND_BASE_SECTION_SETTINGS:
8186
- break;
8187
- case index.EditorMessageType.SEND_BASE_SECTION_DATA:
8188
- break;
8189
- case index.EditorMessageType.SEND_SECTION_SETTINGS:
8292
+ case content.EditorMessageType.SEND_BASE_SECTION_SETTINGS:
8293
+ case content.EditorMessageType.SEND_SECTION_SETTINGS: {
8294
+ const sectionIndex = pagePropsCopyRef.current.pageData?.sections?.findIndex(
8295
+ (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8296
+ );
8297
+ if (typeof sectionIndex !== "number" || sectionIndex === -1) {
8298
+ return;
8299
+ }
8300
+ if (pagePropsCopyRef.current.pageData?.sections && pagePropsCopyRef.current.pageData?.sections[sectionIndex].pivot?.settings) {
8301
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.settings = event.data?.settings;
8302
+ manager?.sync(pagePropsCopyRef.current);
8303
+ setSectionKeys((prev) => ({
8304
+ ...prev,
8305
+ [`${event.data?.slug}__${event.data?.linkId}-key`]: v4()
8306
+ }));
8307
+ }
8190
8308
  break;
8191
- case index.EditorMessageType.SEND_SECTION_DATA:
8309
+ }
8310
+ case content.EditorMessageType.SEND_BASE_SECTION_DATA:
8311
+ case content.EditorMessageType.SEND_SECTION_DATA: {
8312
+ const sectionIndex = pagePropsCopyRef.current.pageData?.sections?.findIndex(
8313
+ (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8314
+ );
8315
+ if (typeof sectionIndex !== "number" || sectionIndex === -1) {
8316
+ return;
8317
+ }
8318
+ if (pagePropsCopyRef.current.pageData?.sections && pagePropsCopyRef.current.pageData?.sections[sectionIndex].pivot?.data) {
8319
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.data = event.data?.data;
8320
+ manager?.sync(pagePropsCopyRef.current);
8321
+ setSectionKeys((prev) => ({
8322
+ ...prev,
8323
+ [`${event.data?.slug}__${event.data?.linkId}-key`]: v4()
8324
+ }));
8325
+ }
8192
8326
  break;
8193
- case index.EditorMessageType.SEND_PAGE_SECTIONS:
8327
+ }
8328
+ case content.EditorMessageType.SEND_PAGE_SECTIONS: {
8329
+ if (!pagePropsCopyRef.current.pageData) return;
8330
+ pagePropsCopyRef.current.pageData.sectionsOrder = event.data?.sectionsOrder;
8331
+ pagePropsCopyRef.current.pageData.sectionsDisabled = event.data?.sectionsDisabled;
8332
+ const currentSections = content.cloneDeep(
8333
+ pagePropsCopyRef.current.pageData.sections || []
8334
+ );
8335
+ pagePropsCopyRef.current.pageData.sections = event.data?.sectionsList || [];
8336
+ currentSections.forEach((currentSection) => {
8337
+ const sectionIndex = pagePropsCopyRef.current.pageData?.sections?.findIndex(
8338
+ (newSection) => newSection.slug === currentSection?.slug && newSection.pivot?.linkId === currentSection.pivot?.linkId
8339
+ );
8340
+ if (typeof sectionIndex === "number" && sectionIndex > -1 && pagePropsCopyRef.current.pageData?.sections?.[sectionIndex].pivot) {
8341
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.data = currentSection.pivot?.data;
8342
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.settings = currentSection.pivot?.settings;
8343
+ }
8344
+ });
8345
+ manager?.sync(pagePropsCopyRef.current);
8346
+ const newProps = manager?.getState().props;
8347
+ setSections(getFinalPageSections(newProps));
8348
+ setSectionsWrapperKey(v4());
8194
8349
  break;
8350
+ }
8195
8351
  }
8196
8352
  };
8197
8353
  window.addEventListener("message", handleMessage);
8198
8354
  if (sessionStorage.getItem("shouldNotifyReload") === "1") {
8199
8355
  const origin = sessionStorage.getItem("replyOrigin") || "*";
8200
8356
  window.parent.postMessage(
8201
- { type: index.EditorMessageType.RELOAD_COMPLETE },
8357
+ { type: content.EditorMessageType.RELOAD_COMPLETE },
8202
8358
  origin
8203
8359
  );
8204
8360
  sessionStorage.removeItem("shouldNotifyReload");
@@ -8207,15 +8363,19 @@ const RootContainer = ({
8207
8363
  return () => {
8208
8364
  window.removeEventListener("message", handleMessage);
8209
8365
  };
8210
- }, []);
8211
- return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: sections.map((section, i) => {
8366
+ }, [sections, manager, getFinalPageSections]);
8367
+ return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { all: "unset" }, children: sections.map((section) => {
8212
8368
  const Component = components[section.name];
8213
- if (!Component) return null;
8369
+ const key = sectionKeys[`${section.slug}__${section.pivot?.linkId}-key`];
8370
+ if (!Component) {
8371
+ console.warn(`[@creopse/react] Component not found: ${section.name}`);
8372
+ return null;
8373
+ }
8214
8374
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
8215
8375
  "section",
8216
8376
  {
8217
8377
  id: `${section.slug}__${section.pivot?.linkId}-container`,
8218
- className: "reset-styles",
8378
+ style: { all: "unset" },
8219
8379
  children: /* @__PURE__ */ jsxRuntimeExports.jsx(
8220
8380
  Component,
8221
8381
  {
@@ -8233,9 +8393,9 @@ const RootContainer = ({
8233
8393
  }
8234
8394
  )
8235
8395
  },
8236
- `section-${i}`
8396
+ key
8237
8397
  );
8238
- }) });
8398
+ }) }, sectionsWrapperKey);
8239
8399
  };
8240
8400
  function CreopseProvider({ children, options }) {
8241
8401
  if (!options?.initialProps) {
@@ -8258,7 +8418,7 @@ function CreopseProvider({ children, options }) {
8258
8418
  console.warn("[@creopse/react] No router - props won't sync");
8259
8419
  }
8260
8420
  }, [options.router, manager]);
8261
- return /* @__PURE__ */ jsxRuntimeExports.jsx(index.RouterContext.Provider, { value: options.router || null, children: /* @__PURE__ */ jsxRuntimeExports.jsx(index.ConfigContext.Provider, { value: options.config, children: /* @__PURE__ */ jsxRuntimeExports.jsx(index.ResolveSectionsContext.Provider, { value: options.resolveSections, children: /* @__PURE__ */ jsxRuntimeExports.jsx(index.PropsContext.Provider, { value: manager, children }) }) }) });
8421
+ return /* @__PURE__ */ jsxRuntimeExports.jsx(content.RouterContext.Provider, { value: options.router || null, children: /* @__PURE__ */ jsxRuntimeExports.jsx(content.ConfigContext.Provider, { value: options.config, children: /* @__PURE__ */ jsxRuntimeExports.jsx(content.ResolveSectionsContext.Provider, { value: options.resolveSections, children: /* @__PURE__ */ jsxRuntimeExports.jsx(content.PropsContext.Provider, { value: manager, children }) }) }) });
8262
8422
  }
8263
8423
  exports.AsyncImg = AsyncImg;
8264
8424
  exports.CreopseProvider = CreopseProvider;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
2
  import React__default, { useState, useEffect, createContext, useRef, useLayoutEffect, useId, useContext, useInsertionEffect, useMemo, useCallback, Children, isValidElement, Fragment, createElement, forwardRef, Component } from "react";
3
- import { u as useHelper, s as slideToId, E as EditorMessageType, R as RouterContext, C as ConfigContext, a as ResolveSectionsContext, P as PropsContext } from "./index-B7GqfDTJ.js";
3
+ import { u as useHelper, a as useContent, b as useProps, P as PropsContext, R as ResolveSectionsContext, c as cloneDeep, s as slideToId, E as EditorMessageType, d as RouterContext, C as ConfigContext } from "./content-CM9eJ2st.js";
4
4
  import "@inertiajs/react";
5
5
  import { createPortal } from "react-dom";
6
6
  var jsxRuntime = { exports: {} };
@@ -8069,24 +8069,131 @@ const StickyTop = ({
8069
8069
  }
8070
8070
  );
8071
8071
  };
8072
- const RootContainer = ({
8073
- page,
8074
- sections = [],
8075
- components = {}
8076
- }) => {
8072
+ const byteToHex = [];
8073
+ for (let i = 0; i < 256; ++i) {
8074
+ byteToHex.push((i + 256).toString(16).slice(1));
8075
+ }
8076
+ function unsafeStringify(arr, offset = 0) {
8077
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
8078
+ }
8079
+ let getRandomValues;
8080
+ const rnds8 = new Uint8Array(16);
8081
+ function rng() {
8082
+ if (!getRandomValues) {
8083
+ if (typeof crypto === "undefined" || !crypto.getRandomValues) {
8084
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
8085
+ }
8086
+ getRandomValues = crypto.getRandomValues.bind(crypto);
8087
+ }
8088
+ return getRandomValues(rnds8);
8089
+ }
8090
+ const randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
8091
+ const native = { randomUUID };
8092
+ function _v4(options, buf, offset) {
8093
+ options = options || {};
8094
+ const rnds = options.random ?? options.rng?.() ?? rng();
8095
+ if (rnds.length < 16) {
8096
+ throw new Error("Random bytes length must be >= 16");
8097
+ }
8098
+ rnds[6] = rnds[6] & 15 | 64;
8099
+ rnds[8] = rnds[8] & 63 | 128;
8100
+ return unsafeStringify(rnds);
8101
+ }
8102
+ function v4(options, buf, offset) {
8103
+ if (native.randomUUID && true && !options) {
8104
+ return native.randomUUID();
8105
+ }
8106
+ return _v4(options);
8107
+ }
8108
+ const useSetup = () => {
8109
+ const getComponents = useCallback(
8110
+ (resolveSections) => {
8111
+ const components = {};
8112
+ if (!resolveSections) {
8113
+ throw new Error("[@creopse/react] resolveSections is required");
8114
+ }
8115
+ const files = resolveSections();
8116
+ for (const [key, value] of Object.entries(files)) {
8117
+ const componentName = key.replace(/^\.\/(.*)\.\w+$/, "$1");
8118
+ const parts = componentName.split("/");
8119
+ const name = parts[parts.length - 1]?.split(".")[0];
8120
+ components[name] = value.default;
8121
+ }
8122
+ return components;
8123
+ },
8124
+ []
8125
+ );
8126
+ const getSectionsInOrder = useCallback(
8127
+ (props) => {
8128
+ const sections = props.pageData?.sections;
8129
+ const sectionsOrder = props.pageData?.sectionsOrder;
8130
+ const sectionsOrdered = [];
8131
+ if (Array.isArray(sections) && Array.isArray(sectionsOrder)) {
8132
+ for (const section of sectionsOrder) {
8133
+ if (section) {
8134
+ const item = sections.find(
8135
+ (value) => `${value.slug}__${value.pivot?.linkId}` === section
8136
+ );
8137
+ if (item) {
8138
+ sectionsOrdered.push(item);
8139
+ }
8140
+ }
8141
+ }
8142
+ }
8143
+ return sectionsOrdered.length ? sectionsOrdered : sections || [];
8144
+ },
8145
+ []
8146
+ );
8147
+ const getFinalPageSections = useCallback(
8148
+ (props) => {
8149
+ const sections = getSectionsInOrder(props);
8150
+ const sectionsDisabled = props.pageData?.sectionsDisabled;
8151
+ return sections.filter((section) => {
8152
+ return !sectionsDisabled?.includes(
8153
+ `${section.slug}__${section.pivot?.linkId}`
8154
+ );
8155
+ });
8156
+ },
8157
+ [getSectionsInOrder]
8158
+ );
8159
+ return {
8160
+ getComponents,
8161
+ getSectionsInOrder,
8162
+ getFinalPageSections
8163
+ };
8164
+ };
8165
+ const RootContainer = () => {
8166
+ const { page } = useContent();
8167
+ const props = useProps();
8168
+ const manager = useContext(PropsContext);
8169
+ const resolveSections = useContext(ResolveSectionsContext);
8170
+ const { getComponents, getFinalPageSections } = useSetup();
8171
+ const [sections, setSections] = useState(
8172
+ () => getFinalPageSections(props)
8173
+ );
8174
+ const [components] = useState(() => getComponents(resolveSections));
8175
+ const [sectionsWrapperKey, setSectionsWrapperKey] = useState(v4());
8176
+ const [sectionKeys, setSectionKeys] = useState({});
8177
+ const sectionsStateRef = useRef({});
8178
+ const pagePropsCopyRef = useRef(cloneDeep(props));
8179
+ useEffect(() => {
8180
+ const keys = {};
8181
+ sections.forEach((section) => {
8182
+ keys[`${section.slug}__${section.pivot?.linkId}-key`] = v4();
8183
+ });
8184
+ setSectionKeys(keys);
8185
+ }, [sections]);
8077
8186
  useEffect(() => {
8078
- const timeout = setTimeout(() => {
8079
- const sectionData = page.props?.sectionData;
8187
+ const timer = setTimeout(() => {
8188
+ const sectionData = page.props.sectionData;
8080
8189
  if (sectionData && sections.find(
8081
8190
  (section) => section.slug === sectionData.slug && section.pivot?.linkId === sectionData.pivot?.linkId
8082
8191
  )) {
8083
8192
  slideToId(`${sectionData.slug}__${sectionData.pivot?.linkId}-container`);
8084
8193
  }
8085
8194
  }, 1e3);
8086
- return () => clearTimeout(timeout);
8087
- }, [page, sections]);
8088
- const sectionsStateRef = useRef({});
8089
- const sectionsRef = useRef(sections);
8195
+ return () => clearTimeout(timer);
8196
+ }, [page.props.sectionData, sections]);
8090
8197
  useEffect(() => {
8091
8198
  const deselectAllSections = () => {
8092
8199
  Object.keys(sectionsStateRef.current).forEach((key) => {
@@ -8109,7 +8216,7 @@ const RootContainer = ({
8109
8216
  const primaryColor = event.data?.primaryColor ?? "blue";
8110
8217
  sessionStorage.setItem("replyOrigin", event.origin);
8111
8218
  sessionStorage.setItem("primaryColor", primaryColor);
8112
- sectionsRef.current?.forEach((section) => {
8219
+ sections?.forEach((section) => {
8113
8220
  if (section.slug && section.pivot?.linkId) {
8114
8221
  const sectionElementId = `${section.slug}__${section.pivot?.linkId}-container`;
8115
8222
  const sectionElement = document.getElementById(sectionElementId);
@@ -8147,7 +8254,7 @@ const RootContainer = ({
8147
8254
  break;
8148
8255
  case EditorMessageType.SELECT_SIDEBAR_SECTION: {
8149
8256
  setTimeout(() => {
8150
- if (sectionsRef.current?.find(
8257
+ if (sections.find(
8151
8258
  (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8152
8259
  )) {
8153
8260
  slideToId(`${event.data?.slug}__${event.data?.linkId}-container`);
@@ -8165,15 +8272,64 @@ const RootContainer = ({
8165
8272
  break;
8166
8273
  }
8167
8274
  case EditorMessageType.SEND_BASE_SECTION_SETTINGS:
8275
+ case EditorMessageType.SEND_SECTION_SETTINGS: {
8276
+ const sectionIndex = pagePropsCopyRef.current.pageData?.sections?.findIndex(
8277
+ (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8278
+ );
8279
+ if (typeof sectionIndex !== "number" || sectionIndex === -1) {
8280
+ return;
8281
+ }
8282
+ if (pagePropsCopyRef.current.pageData?.sections && pagePropsCopyRef.current.pageData?.sections[sectionIndex].pivot?.settings) {
8283
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.settings = event.data?.settings;
8284
+ manager?.sync(pagePropsCopyRef.current);
8285
+ setSectionKeys((prev) => ({
8286
+ ...prev,
8287
+ [`${event.data?.slug}__${event.data?.linkId}-key`]: v4()
8288
+ }));
8289
+ }
8168
8290
  break;
8291
+ }
8169
8292
  case EditorMessageType.SEND_BASE_SECTION_DATA:
8293
+ case EditorMessageType.SEND_SECTION_DATA: {
8294
+ const sectionIndex = pagePropsCopyRef.current.pageData?.sections?.findIndex(
8295
+ (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8296
+ );
8297
+ if (typeof sectionIndex !== "number" || sectionIndex === -1) {
8298
+ return;
8299
+ }
8300
+ if (pagePropsCopyRef.current.pageData?.sections && pagePropsCopyRef.current.pageData?.sections[sectionIndex].pivot?.data) {
8301
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.data = event.data?.data;
8302
+ manager?.sync(pagePropsCopyRef.current);
8303
+ setSectionKeys((prev) => ({
8304
+ ...prev,
8305
+ [`${event.data?.slug}__${event.data?.linkId}-key`]: v4()
8306
+ }));
8307
+ }
8170
8308
  break;
8171
- case EditorMessageType.SEND_SECTION_SETTINGS:
8172
- break;
8173
- case EditorMessageType.SEND_SECTION_DATA:
8174
- break;
8175
- case EditorMessageType.SEND_PAGE_SECTIONS:
8309
+ }
8310
+ case EditorMessageType.SEND_PAGE_SECTIONS: {
8311
+ if (!pagePropsCopyRef.current.pageData) return;
8312
+ pagePropsCopyRef.current.pageData.sectionsOrder = event.data?.sectionsOrder;
8313
+ pagePropsCopyRef.current.pageData.sectionsDisabled = event.data?.sectionsDisabled;
8314
+ const currentSections = cloneDeep(
8315
+ pagePropsCopyRef.current.pageData.sections || []
8316
+ );
8317
+ pagePropsCopyRef.current.pageData.sections = event.data?.sectionsList || [];
8318
+ currentSections.forEach((currentSection) => {
8319
+ const sectionIndex = pagePropsCopyRef.current.pageData?.sections?.findIndex(
8320
+ (newSection) => newSection.slug === currentSection?.slug && newSection.pivot?.linkId === currentSection.pivot?.linkId
8321
+ );
8322
+ if (typeof sectionIndex === "number" && sectionIndex > -1 && pagePropsCopyRef.current.pageData?.sections?.[sectionIndex].pivot) {
8323
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.data = currentSection.pivot?.data;
8324
+ pagePropsCopyRef.current.pageData.sections[sectionIndex].pivot.settings = currentSection.pivot?.settings;
8325
+ }
8326
+ });
8327
+ manager?.sync(pagePropsCopyRef.current);
8328
+ const newProps = manager?.getState().props;
8329
+ setSections(getFinalPageSections(newProps));
8330
+ setSectionsWrapperKey(v4());
8176
8331
  break;
8332
+ }
8177
8333
  }
8178
8334
  };
8179
8335
  window.addEventListener("message", handleMessage);
@@ -8189,15 +8345,19 @@ const RootContainer = ({
8189
8345
  return () => {
8190
8346
  window.removeEventListener("message", handleMessage);
8191
8347
  };
8192
- }, []);
8193
- return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children: sections.map((section, i) => {
8348
+ }, [sections, manager, getFinalPageSections]);
8349
+ return /* @__PURE__ */ jsxRuntimeExports.jsx("div", { style: { all: "unset" }, children: sections.map((section) => {
8194
8350
  const Component2 = components[section.name];
8195
- if (!Component2) return null;
8351
+ const key = sectionKeys[`${section.slug}__${section.pivot?.linkId}-key`];
8352
+ if (!Component2) {
8353
+ console.warn(`[@creopse/react] Component not found: ${section.name}`);
8354
+ return null;
8355
+ }
8196
8356
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
8197
8357
  "section",
8198
8358
  {
8199
8359
  id: `${section.slug}__${section.pivot?.linkId}-container`,
8200
- className: "reset-styles",
8360
+ style: { all: "unset" },
8201
8361
  children: /* @__PURE__ */ jsxRuntimeExports.jsx(
8202
8362
  Component2,
8203
8363
  {
@@ -8215,9 +8375,9 @@ const RootContainer = ({
8215
8375
  }
8216
8376
  )
8217
8377
  },
8218
- `section-${i}`
8378
+ key
8219
8379
  );
8220
- }) });
8380
+ }) }, sectionsWrapperKey);
8221
8381
  };
8222
8382
  function CreopseProvider({ children, options }) {
8223
8383
  if (!options?.initialProps) {