Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/visualBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,38 @@ export class VisualBuilder {
}, 1000)
);

private dataCslpMutationObserver = new MutationObserver((mutations) => {
let shouldCheck = false;

mutations.forEach((mutation) => {
if (
mutation.type === "childList" &&
mutation.addedNodes.length > 0
) {
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
if ((node as Element).hasAttribute("data-cslp")) {
shouldCheck = true;
break;
}
}
}
}
});

if (shouldCheck) {
const dataCslpElements = document.querySelectorAll("[data-cslp]");
if (dataCslpElements.length > 0) {
dataCslpElements.forEach((element) => {
if (!element.hasAttribute("data-cslp-unique-id")) {
const uniqueId = `cslp-${window.crypto.randomUUID()}`;
element.setAttribute("data-cslp-unique-id", uniqueId);
}
});
}
}
});

constructor() {
// Handles changes in element positions due to sidebar toggling or window resizing,
// triggering a redraw of the visual builder
Expand Down Expand Up @@ -296,6 +328,12 @@ export class VisualBuilder {
if (!config.enable || config.mode < ILivePreviewModeConfig.BUILDER) {
return;
}

this.dataCslpMutationObserver.observe(document.body, {
childList: true,
subtree: true,
});

visualBuilderPostMessage
?.send<IVisualBuilderInitEvent>("init", {
isSSR: config.ssr,
Expand Down Expand Up @@ -409,6 +447,7 @@ export class VisualBuilder {
this.resizeObserver.disconnect();
this.mutationObserver.disconnect();
this.threadMutationObserver.disconnect();
this.dataCslpMutationObserver.disconnect()

// Clear global state
VisualBuilder.VisualBuilderGlobalState.value = {
Expand Down
10 changes: 5 additions & 5 deletions src/visualBuilder/utils/updateFocussedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export function updateFocussedState({
return;
}

const previousSelectedElementCslp =
previousSelectedEditableDOM?.getAttribute("data-cslp");
const previousSelectedElementCslpUniqueId =
previousSelectedEditableDOM?.getAttribute("data-cslp-unique-id");
const newPreviousSelectedElement = document.querySelector(
`[data-cslp="${previousSelectedElementCslp}"]`
`[data-cslp-unique-id="${previousSelectedElementCslpUniqueId}"]`
);
if (!newPreviousSelectedElement && resizeObserver) {
hideFocusOverlay({
Expand Down Expand Up @@ -227,9 +227,9 @@ export function updateFocussedStateOnMutation(
.previousSelectedEditableDOM;
if (!selectedElement) return;

const selectedElementCslp = selectedElement?.getAttribute("data-cslp");
const selectedElementCslpUniqueId = selectedElement?.getAttribute("data-cslp-unique-id");
const newSelectedElement = document.querySelector(
`[data-cslp="${selectedElementCslp}"]`
`[data-cslp-unique-id="${selectedElementCslpUniqueId}"]`
);
if (!newSelectedElement && resizeObserver) {
hideFocusOverlay({
Expand Down
Loading