@@ -218,6 +218,38 @@ export class VisualBuilder {
218218 )
219219 ) ;
220220
221+ private dataCslpMutationObserver = new MutationObserver ( ( mutations ) => {
222+ let shouldCheck = false ;
223+
224+ mutations . forEach ( ( mutation ) => {
225+ if (
226+ mutation . type === "childList" &&
227+ mutation . addedNodes . length > 0
228+ ) {
229+ for ( const node of mutation . addedNodes ) {
230+ if ( node . nodeType === Node . ELEMENT_NODE ) {
231+ if ( ( node as Element ) . hasAttribute ( "data-cslp" ) ) {
232+ shouldCheck = true ;
233+ break ;
234+ }
235+ }
236+ }
237+ }
238+ } ) ;
239+
240+ if ( shouldCheck ) {
241+ const dataCslpElements = document . querySelectorAll ( "[data-cslp]" ) ;
242+ if ( dataCslpElements . length > 0 ) {
243+ dataCslpElements . forEach ( ( element ) => {
244+ if ( ! element . hasAttribute ( "data-cslp-unique-id" ) ) {
245+ const uniqueId = `cslp-${ window . crypto . randomUUID ( ) } ` ;
246+ element . setAttribute ( "data-cslp-unique-id" , uniqueId ) ;
247+ }
248+ } ) ;
249+ }
250+ }
251+ } ) ;
252+
221253 constructor ( ) {
222254 // Handles changes in element positions due to sidebar toggling or window resizing,
223255 // triggering a redraw of the visual builder
@@ -246,6 +278,12 @@ export class VisualBuilder {
246278 if ( ! config . enable || config . mode < ILivePreviewModeConfig . BUILDER ) {
247279 return ;
248280 }
281+
282+ this . dataCslpMutationObserver . observe ( document . body , {
283+ childList : true ,
284+ subtree : true ,
285+ } ) ;
286+
249287 visualBuilderPostMessage
250288 ?. send < IVisualBuilderInitEvent > ( "init" , {
251289 isSSR : config . ssr ,
@@ -336,6 +374,7 @@ export class VisualBuilder {
336374 // Disconnect observers
337375 this . resizeObserver . disconnect ( ) ;
338376 this . mutationObserver . disconnect ( ) ;
377+ this . dataCslpMutationObserver . disconnect ( )
339378
340379 // Clear global state
341380 VisualBuilder . VisualBuilderGlobalState . value = {
0 commit comments