@contentful/experiences-visual-editor-react 1.28.1 → 1.28.2

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.js CHANGED
@@ -457,7 +457,7 @@ const builtInStyles = {
457
457
  },
458
458
  type: 'Text',
459
459
  group: 'style',
460
- description: 'The horizontal alignment of the section',
460
+ description: 'The vertical alignment of the section',
461
461
  defaultValue: 'center',
462
462
  displayName: 'Vertical alignment',
463
463
  },
@@ -2983,34 +2983,19 @@ const useBreakpoints = (breakpoints) => {
2983
2983
  * and sends the DOM Rect to the client app
2984
2984
  */
2985
2985
  const sendSelectedComponentCoordinates = (instanceId) => {
2986
- if (!instanceId)
2987
- return;
2988
- let selectedElement = document.querySelector(`[data-cf-node-id="${instanceId}"]`);
2989
- let selectedAssemblyChild = undefined;
2990
- const [rootNodeId, nodeLocation] = instanceId.split('---');
2991
- if (nodeLocation) {
2992
- selectedAssemblyChild = selectedElement;
2993
- selectedElement = document.querySelector(`[data-cf-node-id="${rootNodeId}"]`);
2994
- }
2995
- // Finds the first parent that is a VisualEditorBlock
2996
- let parent = selectedElement?.parentElement;
2997
- while (parent) {
2998
- if (parent?.dataset?.cfNodeId) {
2999
- break;
3000
- }
3001
- parent = parent?.parentElement;
3002
- }
3003
- if (selectedElement) {
2986
+ const selection = getSelectionNodes(instanceId);
2987
+ if (selection?.target) {
3004
2988
  const sendUpdateSelectedComponentCoordinates = () => {
3005
2989
  sendMessage(OUTGOING_EVENTS.UpdateSelectedComponentCoordinates, {
3006
- selectedNodeCoordinates: getElementCoordinates(selectedElement),
3007
- selectedAssemblyChildCoordinates: selectedAssemblyChild
3008
- ? getElementCoordinates(selectedAssemblyChild)
2990
+ selectedNodeCoordinates: getElementCoordinates(selection.target),
2991
+ selectedAssemblyChildCoordinates: selection.patternChild
2992
+ ? getElementCoordinates(selection.patternChild)
3009
2993
  : undefined,
3010
- parentCoordinates: parent ? getElementCoordinates(parent) : undefined,
2994
+ parentCoordinates: selection.parent ? getElementCoordinates(selection.parent) : undefined,
3011
2995
  });
3012
2996
  };
3013
- const childImage = selectedElement.querySelector('img');
2997
+ // If the target contains an image, wait for this image to be loaded before sending the coordinates
2998
+ const childImage = selection.target.querySelector('img');
3014
2999
  if (childImage) {
3015
3000
  const handleImageLoad = () => {
3016
3001
  sendUpdateSelectedComponentCoordinates();
@@ -3021,6 +3006,37 @@ const sendSelectedComponentCoordinates = (instanceId) => {
3021
3006
  sendUpdateSelectedComponentCoordinates();
3022
3007
  }
3023
3008
  };
3009
+ const getSelectionNodes = (instanceId) => {
3010
+ if (!instanceId)
3011
+ return;
3012
+ let selectedNode = document.querySelector(`[data-cf-node-id="${instanceId}"]`);
3013
+ let selectedPatternChild = null;
3014
+ let selectedParent = null;
3015
+ // Use RegEx instead of split to match the last occurrence of '---' in the instanceId instead of the first one
3016
+ const idMatch = instanceId.match(/(.*)---(.*)/);
3017
+ const rootNodeId = idMatch?.[1] ?? instanceId;
3018
+ const nodeLocation = idMatch?.[2];
3019
+ const isNestedPattern = nodeLocation && selectedNode?.dataset?.cfNodeBlockType === ASSEMBLY_NODE_TYPE;
3020
+ const isPatternChild = !isNestedPattern && nodeLocation;
3021
+ if (isPatternChild) {
3022
+ // For pattern child nodes, render the pattern itself as selected component
3023
+ selectedPatternChild = selectedNode;
3024
+ selectedNode = document.querySelector(`[data-cf-node-id="${rootNodeId}"]`);
3025
+ }
3026
+ else if (isNestedPattern) {
3027
+ // For nested patterns, return the upper pattern as parent
3028
+ selectedParent = document.querySelector(`[data-cf-node-id="${rootNodeId}"]`);
3029
+ }
3030
+ else {
3031
+ // Find the next valid parent of the selected element
3032
+ selectedParent = selectedNode?.parentElement ?? null;
3033
+ // Ensure that the selection parent is a VisualEditorBlock
3034
+ while (selectedParent && !selectedParent.dataset?.cfNodeId) {
3035
+ selectedParent = selectedParent?.parentElement;
3036
+ }
3037
+ }
3038
+ return { target: selectedNode, patternChild: selectedPatternChild, parent: selectedParent };
3039
+ };
3024
3040
 
3025
3041
  // Note: During development, the hot reloading might empty this and it
3026
3042
  // stays empty leading to not rendering assemblies. Ideally, this is