@@ -268,6 +268,38 @@ export class VisualBuilder {
268268 } , 1000 )
269269 ) ;
270270
271+ private dataCslpMutationObserver = new MutationObserver ( ( mutations ) => {
272+ let shouldCheck = false ;
273+
274+ mutations . forEach ( ( mutation ) => {
275+ if (
276+ mutation . type === "childList" &&
277+ mutation . addedNodes . length > 0
278+ ) {
279+ for ( const node of mutation . addedNodes ) {
280+ if ( node . nodeType === Node . ELEMENT_NODE ) {
281+ if ( ( node as Element ) . hasAttribute ( "data-cslp" ) ) {
282+ shouldCheck = true ;
283+ break ;
284+ }
285+ }
286+ }
287+ }
288+ } ) ;
289+
290+ if ( shouldCheck ) {
291+ const dataCslpElements = document . querySelectorAll ( "[data-cslp]" ) ;
292+ if ( dataCslpElements . length > 0 ) {
293+ dataCslpElements . forEach ( ( element ) => {
294+ if ( ! element . hasAttribute ( "data-cslp-unique-id" ) ) {
295+ const uniqueId = `cslp-${ window . crypto . randomUUID ( ) } ` ;
296+ element . setAttribute ( "data-cslp-unique-id" , uniqueId ) ;
297+ }
298+ } ) ;
299+ }
300+ }
301+ } ) ;
302+
271303 constructor ( ) {
272304 // Handles changes in element positions due to sidebar toggling or window resizing,
273305 // triggering a redraw of the visual builder
@@ -296,6 +328,12 @@ export class VisualBuilder {
296328 if ( ! config . enable || config . mode < ILivePreviewModeConfig . BUILDER ) {
297329 return ;
298330 }
331+
332+ this . dataCslpMutationObserver . observe ( document . body , {
333+ childList : true ,
334+ subtree : true ,
335+ } ) ;
336+
299337 visualBuilderPostMessage
300338 ?. send < IVisualBuilderInitEvent > ( "init" , {
301339 isSSR : config . ssr ,
@@ -409,6 +447,7 @@ export class VisualBuilder {
409447 this . resizeObserver . disconnect ( ) ;
410448 this . mutationObserver . disconnect ( ) ;
411449 this . threadMutationObserver . disconnect ( ) ;
450+ this . dataCslpMutationObserver . disconnect ( )
412451
413452 // Clear global state
414453 VisualBuilder . VisualBuilderGlobalState . value = {
0 commit comments