Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clean up event listeners in FieldToolbar and update query selector #311

Merged
merged 1 commit into from
Dec 17, 2024
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
5 changes: 4 additions & 1 deletion src/visualBuilder/components/FieldToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function FieldToolbarComponent(
}, [fieldMetadata]);

useEffect(() => {
visualBuilderPostMessage?.on(
const event = visualBuilderPostMessage?.on(
VisualBuilderPostMessageEvents.DELETE_INSTANCE,
(args: { data: { path: string } }) => {
if (
Expand All @@ -327,6 +327,9 @@ function FieldToolbarComponent(
}
}
);
return () => {
event?.unregister();
}
}, []);

const multipleFieldToolbarButtonClasses = classNames(
Expand Down
2 changes: 1 addition & 1 deletion src/visualBuilder/generators/generateToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function appendFieldPathDropdown(
eventDetails: VisualBuilderCslpEventDetails,
focusedToolbarElement: HTMLDivElement
): void {
if(document.querySelector("visual-builder__focused-toolbar__field-label-wrapper"))
if(document.querySelector(".visual-builder__focused-toolbar__field-label-wrapper"))
return;
const { editableElement: targetElement, fieldMetadata } = eventDetails;
const targetElementDimension = targetElement.getBoundingClientRect();
Expand Down
7 changes: 7 additions & 0 deletions src/visualBuilder/utils/handleIndividualFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
import { updateFocussedState } from "./updateFocussedState";
import { FieldDataType, ISchemaFieldMap } from "./types/index.types";
import { getMultilinePlaintext } from "./getMultilinePlaintext";
import { VisualBuilderPostMessageEvents } from "./types/postMessage.types";
import visualBuilderPostMessage from "./visualBuilderPostMessage";

/**
* It handles all the fields based on their data type and its "multiple" property.
Expand Down Expand Up @@ -293,6 +295,11 @@ export function cleanIndividualFieldResidual(elements: {

if (focusedToolbar) {
focusedToolbar.innerHTML = "";
const toolbarEvents = [VisualBuilderPostMessageEvents.DELETE_INSTANCE, VisualBuilderPostMessageEvents.UPDATE_DISCUSSION_ID]
toolbarEvents.forEach((event) => {
//@ts-expect-error - We are accessing private method here, but it is necessary to clean up the event listeners.
visualBuilderPostMessage?.unregisterEvent?.(event);
});
}
}

Expand Down
Loading