@creopse/react 0.0.12 → 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-hI__ihu3.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,26 +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);
8108
- const pageProps = index.useProps();
8109
- console.log(pageProps);
8213
+ return () => clearTimeout(timer);
8214
+ }, [page.props.sectionData, sections]);
8110
8215
  React.useEffect(() => {
8111
8216
  const deselectAllSections = () => {
8112
8217
  Object.keys(sectionsStateRef.current).forEach((key) => {
@@ -8120,16 +8225,16 @@ const RootContainer = ({
8120
8225
  };
8121
8226
  const handleMessage = (event) => {
8122
8227
  switch (event.data?.type) {
8123
- case index.EditorMessageType.RELOAD:
8228
+ case content.EditorMessageType.RELOAD:
8124
8229
  sessionStorage.setItem("shouldNotifyReload", "1");
8125
8230
  sessionStorage.setItem("replyOrigin", event.origin);
8126
8231
  window.location.reload();
8127
8232
  break;
8128
- case index.EditorMessageType.ENABLE_EDITION_MODE: {
8233
+ case content.EditorMessageType.ENABLE_EDITION_MODE: {
8129
8234
  const primaryColor = event.data?.primaryColor ?? "blue";
8130
8235
  sessionStorage.setItem("replyOrigin", event.origin);
8131
8236
  sessionStorage.setItem("primaryColor", primaryColor);
8132
- sectionsRef.current?.forEach((section) => {
8237
+ sections?.forEach((section) => {
8133
8238
  if (section.slug && section.pivot?.linkId) {
8134
8239
  const sectionElementId = `${section.slug}__${section.pivot?.linkId}-container`;
8135
8240
  const sectionElement = document.getElementById(sectionElementId);
@@ -8147,7 +8252,7 @@ const RootContainer = ({
8147
8252
  this.style.boxShadow = `0 0 10px ${primaryColor}`;
8148
8253
  window.parent.postMessage(
8149
8254
  {
8150
- type: index.EditorMessageType.SELECT_PREVIEW_SECTION,
8255
+ type: content.EditorMessageType.SELECT_PREVIEW_SECTION,
8151
8256
  slug: section.slug,
8152
8257
  linkId: section.pivot?.linkId
8153
8258
  },
@@ -8162,15 +8267,15 @@ const RootContainer = ({
8162
8267
  });
8163
8268
  break;
8164
8269
  }
8165
- case index.EditorMessageType.DESELECT_ALL_SECTIONS:
8270
+ case content.EditorMessageType.DESELECT_ALL_SECTIONS:
8166
8271
  deselectAllSections();
8167
8272
  break;
8168
- case index.EditorMessageType.SELECT_SIDEBAR_SECTION: {
8273
+ case content.EditorMessageType.SELECT_SIDEBAR_SECTION: {
8169
8274
  setTimeout(() => {
8170
- if (sectionsRef.current?.find(
8275
+ if (sections.find(
8171
8276
  (section) => section.slug === event.data?.slug && section.pivot?.linkId === event.data?.linkId
8172
8277
  )) {
8173
- index.slideToId(`${event.data?.slug}__${event.data?.linkId}-container`);
8278
+ content.slideToId(`${event.data?.slug}__${event.data?.linkId}-container`);
8174
8279
  }
8175
8280
  }, 1e3);
8176
8281
  const elementId = `${event.data?.slug}__${event.data?.linkId}-container`;
@@ -8184,38 +8289,72 @@ const RootContainer = ({
8184
8289
  }
8185
8290
  break;
8186
8291
  }
8187
- case index.EditorMessageType.SEND_BASE_SECTION_SETTINGS:
8188
- {
8189
- console.log(event.data);
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;
8190
8299
  }
8191
- break;
8192
- case index.EditorMessageType.SEND_BASE_SECTION_DATA:
8193
- {
8194
- console.log(event.data);
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
+ }));
8195
8307
  }
8196
8308
  break;
8197
- case index.EditorMessageType.SEND_SECTION_SETTINGS:
8198
- {
8199
- console.log(event.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;
8200
8317
  }
8201
- break;
8202
- case index.EditorMessageType.SEND_SECTION_DATA:
8203
- {
8204
- console.log(event.data);
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
+ }));
8205
8325
  }
8206
8326
  break;
8207
- case index.EditorMessageType.SEND_PAGE_SECTIONS:
8208
- {
8209
- console.log(event.data);
8210
- }
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());
8211
8349
  break;
8350
+ }
8212
8351
  }
8213
8352
  };
8214
8353
  window.addEventListener("message", handleMessage);
8215
8354
  if (sessionStorage.getItem("shouldNotifyReload") === "1") {
8216
8355
  const origin = sessionStorage.getItem("replyOrigin") || "*";
8217
8356
  window.parent.postMessage(
8218
- { type: index.EditorMessageType.RELOAD_COMPLETE },
8357
+ { type: content.EditorMessageType.RELOAD_COMPLETE },
8219
8358
  origin
8220
8359
  );
8221
8360
  sessionStorage.removeItem("shouldNotifyReload");
@@ -8224,15 +8363,19 @@ const RootContainer = ({
8224
8363
  return () => {
8225
8364
  window.removeEventListener("message", handleMessage);
8226
8365
  };
8227
- }, []);
8228
- 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) => {
8229
8368
  const Component = components[section.name];
8230
- 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
+ }
8231
8374
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
8232
8375
  "section",
8233
8376
  {
8234
8377
  id: `${section.slug}__${section.pivot?.linkId}-container`,
8235
- className: "reset-styles",
8378
+ style: { all: "unset" },
8236
8379
  children: /* @__PURE__ */ jsxRuntimeExports.jsx(
8237
8380
  Component,
8238
8381
  {
@@ -8250,9 +8393,9 @@ const RootContainer = ({
8250
8393
  }
8251
8394
  )
8252
8395
  },
8253
- `section-${i}`
8396
+ key
8254
8397
  );
8255
- }) });
8398
+ }) }, sectionsWrapperKey);
8256
8399
  };
8257
8400
  function CreopseProvider({ children, options }) {
8258
8401
  if (!options?.initialProps) {
@@ -8275,7 +8418,7 @@ function CreopseProvider({ children, options }) {
8275
8418
  console.warn("[@creopse/react] No router - props won't sync");
8276
8419
  }
8277
8420
  }, [options.router, manager]);
8278
- 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 }) }) }) });
8279
8422
  }
8280
8423
  exports.AsyncImg = AsyncImg;
8281
8424
  exports.CreopseProvider = CreopseProvider;