@contentful/experiences-visual-editor-react 1.0.4 → 1.0.5-beta.0

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/renderApp.js CHANGED
@@ -34767,7 +34767,7 @@ function baseIsNative(value) {
34767
34767
  * @param {string} key The key of the property to get.
34768
34768
  * @returns {*} Returns the property value.
34769
34769
  */
34770
- function getValue$1(object, key) {
34770
+ function getValue$2(object, key) {
34771
34771
  return object == null ? undefined : object[key];
34772
34772
  }
34773
34773
 
@@ -34780,7 +34780,7 @@ function getValue$1(object, key) {
34780
34780
  * @returns {*} Returns the function if it's native, else `undefined`.
34781
34781
  */
34782
34782
  function getNative(object, key) {
34783
- var value = getValue$1(object, key);
34783
+ var value = getValue$2(object, key);
34784
34784
  return baseIsNative(value) ? value : undefined;
34785
34785
  }
34786
34786
 
@@ -38633,7 +38633,7 @@ const builtInStyles = {
38633
38633
  },
38634
38634
  cfHyperlink: {
38635
38635
  displayName: 'Hyperlink',
38636
- type: 'Text',
38636
+ type: 'Hyperlink',
38637
38637
  defaultValue: '',
38638
38638
  validations: {
38639
38639
  format: 'URL',
@@ -39093,6 +39093,56 @@ const lastPathNamedSegmentEq = (path, expectedName) => {
39093
39093
  return secondLast === expectedName;
39094
39094
  };
39095
39095
 
39096
+ const resolveHyperlinkPattern = (pattern, entry, locale) => {
39097
+ if (!entry || !locale)
39098
+ return null;
39099
+ const variables = {
39100
+ entry,
39101
+ locale,
39102
+ };
39103
+ return buildTemplate({ template: pattern, context: variables });
39104
+ };
39105
+ function getValue$1(obj, path) {
39106
+ return path
39107
+ .replace(/\[/g, '.')
39108
+ .replace(/\]/g, '')
39109
+ .split('.')
39110
+ .reduce((o, k) => (o || {})[k], obj);
39111
+ }
39112
+ function addLocale(str, locale) {
39113
+ const fieldsIndicator = 'fields';
39114
+ const fieldsIndex = str.indexOf(fieldsIndicator);
39115
+ if (fieldsIndex !== -1) {
39116
+ const dotIndex = str.indexOf('.', fieldsIndex + fieldsIndicator.length + 1); // +1 for '.'
39117
+ if (dotIndex !== -1) {
39118
+ return str.slice(0, dotIndex + 1) + locale + '.' + str.slice(dotIndex + 1);
39119
+ }
39120
+ }
39121
+ return str;
39122
+ }
39123
+ function getTemplateValue(ctx, path) {
39124
+ const pathWithLocale = addLocale(path, ctx.locale);
39125
+ const retrievedValue = getValue$1(ctx, pathWithLocale);
39126
+ return typeof retrievedValue === 'object' && retrievedValue !== null
39127
+ ? retrievedValue[ctx.locale]
39128
+ : retrievedValue;
39129
+ }
39130
+ function buildTemplate({ template, context, }) {
39131
+ const localeVariable = /{\s*locale\s*}/g;
39132
+ // e.g. "{ page.sys.id }"
39133
+ const variables = /{\s*([\S]+?)\s*}/g;
39134
+ return (template
39135
+ // first replace the locale pattern
39136
+ .replace(localeVariable, context.locale)
39137
+ // then resolve the remaining variables
39138
+ .replace(variables, (_, path) => {
39139
+ const fallback = path + '_NOT_FOUND';
39140
+ const value = getTemplateValue(context, path) ?? fallback;
39141
+ // using _.result didn't gave proper results so we run our own version of it
39142
+ return String(typeof value === 'function' ? value() : value);
39143
+ }));
39144
+ }
39145
+
39096
39146
  const sendMessage = (eventType, data) => {
39097
39147
  if (typeof window === 'undefined') {
39098
39148
  return;
@@ -50095,6 +50145,7 @@ const CF_STYLE_ATTRIBUTES = [
50095
50145
  'cfBackgroundImageAlignmentHorizontal',
50096
50146
  ];
50097
50147
  const EMPTY_CONTAINER_HEIGHT = '80px';
50148
+ const HYPERLINK_DEFAULT_PATTERN = `/{locale}/{entry.fields.slug}/`;
50098
50149
  var PostMessageMethods$1;
50099
50150
  (function (PostMessageMethods) {
50100
50151
  PostMessageMethods["REQUEST_ENTITIES"] = "REQUEST_ENTITIES";
@@ -50614,11 +50665,15 @@ const createAssemblyRegistration = ({ definitionId, definitionName, component, }
50614
50665
 
50615
50666
  const useEditorStore = create((set, get) => ({
50616
50667
  dataSource: {},
50668
+ hyperLinkPattern: undefined,
50617
50669
  unboundValues: {},
50618
50670
  isDragging: false,
50619
50671
  dragItem: '',
50620
50672
  selectedNodeId: null,
50621
50673
  locale: null,
50674
+ setHyperLinkPattern: (pattern) => {
50675
+ set({ hyperLinkPattern: pattern });
50676
+ },
50622
50677
  setSelectedNodeId: (id) => {
50623
50678
  set({ selectedNodeId: id });
50624
50679
  },
@@ -50730,6 +50785,8 @@ const useEntityStore = create((set) => ({
50730
50785
 
50731
50786
  const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, renderDropzone, definition, userIsDragging, }) => {
50732
50787
  const unboundValues = useEditorStore((state) => state.unboundValues);
50788
+ const hyperlinkPattern = useEditorStore((state) => state.hyperLinkPattern);
50789
+ const locale = useEditorStore((state) => state.locale);
50733
50790
  const dataSource = useEditorStore((state) => state.dataSource);
50734
50791
  const entityStore = useEntityStore((state) => state.entityStore);
50735
50792
  const props = reactExports.useMemo(() => {
@@ -50759,6 +50816,14 @@ const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, rende
50759
50816
  [variableName]: designValue,
50760
50817
  };
50761
50818
  }
50819
+ else if (variableMapping.type === 'HyperlinkValue') {
50820
+ const binding = dataSource[variableMapping.linkTargetKey];
50821
+ const hyperlinkEntry = entityStore.getEntryOrAsset(binding, variableMapping.linkTargetKey);
50822
+ return {
50823
+ ...acc,
50824
+ [variableName]: resolveHyperlinkPattern(definition.hyperlinkPattern || hyperlinkPattern || HYPERLINK_DEFAULT_PATTERN, hyperlinkEntry, locale),
50825
+ };
50826
+ }
50762
50827
  else if (variableMapping.type === 'BoundValue') {
50763
50828
  const [, uuid, path] = variableMapping.path.split('/');
50764
50829
  const binding = dataSource[uuid];
@@ -50797,15 +50862,14 @@ const useComponentProps = ({ node, areEntitiesFetched, resolveDesignValue, rende
50797
50862
  }
50798
50863
  }, {});
50799
50864
  }, [
50865
+ hyperlinkPattern,
50866
+ node,
50867
+ locale,
50800
50868
  definition,
50801
- node.data.props,
50802
- node.children,
50803
- node.data.blockId,
50804
50869
  resolveDesignValue,
50805
50870
  dataSource,
50806
50871
  areEntitiesFetched,
50807
50872
  unboundValues,
50808
- node.type,
50809
50873
  entityStore,
50810
50874
  ]);
50811
50875
  const cfStyles = buildCfStyles(props, node.data.blockId);
@@ -51521,6 +51585,11 @@ const deserializeAssemblyNode = ({ node, nodeId, nodeLocation, parentId, assembl
51521
51585
  path: instanceProperty.path,
51522
51586
  };
51523
51587
  }
51588
+ else if (instanceProperty?.type === 'HyperlinkValue') {
51589
+ const componentInstanceValue = componentInstanceDataSource[instanceProperty.linkTargetKey];
51590
+ dataSource[instanceProperty.linkTargetKey] == componentInstanceValue;
51591
+ childNodeVariable[variableName] = instanceProperty;
51592
+ }
51524
51593
  }
51525
51594
  }
51526
51595
  const isAssembly = assembliesRegistry.has(node.definitionId);
@@ -54185,10 +54254,16 @@ const useInitializeEditor = () => {
54185
54254
  return initialized;
54186
54255
  };
54187
54256
 
54188
- const VisualEditorRoot = () => {
54257
+ const VisualEditorRoot = ({ experience }) => {
54189
54258
  const initialized = useInitializeEditor();
54259
+ const setHyperLinkPattern = useEditorStore((state) => state.setHyperLinkPattern);
54190
54260
  const setMousePosition = useDraggedItemStore((state) => state.setMousePosition);
54191
54261
  const setHoveringZone = useZoneStore((state) => state.setHoveringZone);
54262
+ reactExports.useEffect(() => {
54263
+ if (experience?.hyperlinkPattern) {
54264
+ setHyperLinkPattern(experience.hyperlinkPattern);
54265
+ }
54266
+ }, [experience?.hyperlinkPattern, setHyperLinkPattern]);
54192
54267
  reactExports.useEffect(() => {
54193
54268
  const onMouseMove = (e) => {
54194
54269
  setMousePosition(e.clientX, e.clientY);