diff --git a/src/visualBuilder/components/FieldToolbar.tsx b/src/visualBuilder/components/FieldToolbar.tsx index a9b3f00..88e3d1b 100644 --- a/src/visualBuilder/components/FieldToolbar.tsx +++ b/src/visualBuilder/components/FieldToolbar.tsx @@ -316,7 +316,7 @@ function FieldToolbarComponent( }, [fieldMetadata]); useEffect(() => { - visualBuilderPostMessage?.on( + const event = visualBuilderPostMessage?.on( VisualBuilderPostMessageEvents.DELETE_INSTANCE, (args: { data: { path: string } }) => { if ( @@ -327,6 +327,9 @@ function FieldToolbarComponent( } } ); + return () => { + event?.unregister(); + } }, []); const multipleFieldToolbarButtonClasses = classNames( diff --git a/src/visualBuilder/generators/generateToolbar.tsx b/src/visualBuilder/generators/generateToolbar.tsx index 862a758..87f6c67 100644 --- a/src/visualBuilder/generators/generateToolbar.tsx +++ b/src/visualBuilder/generators/generateToolbar.tsx @@ -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(); diff --git a/src/visualBuilder/utils/handleIndividualFields.ts b/src/visualBuilder/utils/handleIndividualFields.ts index b273580..524224c 100644 --- a/src/visualBuilder/utils/handleIndividualFields.ts +++ b/src/visualBuilder/utils/handleIndividualFields.ts @@ -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. @@ -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); + }); } }