Draft: #3491021 Use comment annotations
Issues
- An empty content region does not have the html comments like a slot does so the insertion of the
xb--sortable-slot-empty-placeholder
might need to change later - I spoted that the Image component does not have
data-component-id
like other components do. Not sure it’s an issue?
Changes
Just a brief explainer of the changes in this MR. The headline is that now each XB Component is not wrapped in a div but just rendered exactly as-is into the page. Instead there are HTML comments before and after each component and inside each slot and this MR leverages those comments to build a 'map' allowing the React app to find a given HTML element in the preview iFrame from a given Component UUID or Slot ID.
Viewport.tsx
- wrap each viewport with a new React Context Provider that passes through the ‘map’ of component and slot ids to their respective HTML DOM elements in the iFrame document.
ComponentOverlay.tsx
- moveElement replaced with moveDomElements in - so that a component made up of multiple elements can still be moved (although this move is just the ‘optimistic’ DOM update that happens instantly on DnD)
- uses the new slotsMap and componentsMap to look up DOM elements inside the iFrame replacing need to do e.g. iframeDocument.querySelector(`[data-xb-uuid="${component.uuid}"]
- sortable initialisation has been changed to find sortable items by data-xb-uuid instead of the class xb--sortable-item which no longer exists.
SlotOverlay.tsx
- uses the new slotsMap and componentsMap to look up DOM elements inside the iFrame replacing need to do e.g.
useSyncPreviewElementSize
- based on the existing useSyncElementSize but takes an array of elements and returns the rect (top, left, width, height) of all the elements as if a box had been drawn to contain them all.
- Only implements a resize observer, not a mutationObserver. I’m not sure if that is a regression.
function-utils.ts
- multiple new functions for traversing components and slots via the HTML comments - see individual comments in the file. A bunch of these turned out to not be useful yet, but I've left them in in case they are useful later.
- I've run out of time to write tests for these functions.
Edited by Jesse Baker