diff --git a/src/Components/Components/Components.tsx b/src/Components/Components/Components.tsx index f7bd816..17d2241 100644 --- a/src/Components/Components/Components.tsx +++ b/src/Components/Components/Components.tsx @@ -22,6 +22,11 @@ function HandleDragStart(event: React.DragEvent): void { const element = event.target as HTMLButtonElement; event.dataTransfer.setData('type', element.id); event.dataTransfer.setDragImage(element, 0, 0); + window.dispatchEvent(new Event('componentDragStart')); +} + +function HandleDragEnd(): void { + window.dispatchEvent(new Event('componentDragEnd')); } interface SidebarCategory { @@ -85,7 +90,8 @@ export function Components(props: IComponentsProps): JSX.Element { title={componentOption.Type} onClick={() => { props.buttonOnClick(componentOption.Type); }} draggable={true} - onDragStart={(event) => { HandleDragStart(event); }} + onDragStart={HandleDragStart} + onDragEnd={HandleDragEnd} disabled={disabled} > {TruncateString(componentOption.DisplayedText ?? componentOption.Type, 25)} diff --git a/src/Components/ElementsSidebar/ElementsSidebar.tsx b/src/Components/ElementsSidebar/ElementsSidebar.tsx index 2a37176..11bffe4 100644 --- a/src/Components/ElementsSidebar/ElementsSidebar.tsx +++ b/src/Components/ElementsSidebar/ElementsSidebar.tsx @@ -34,8 +34,7 @@ function RemoveBorderClasses(target: HTMLElement, exception: string = ''): void function HandleDragLeave(event: React.DragEvent): void { let target: HTMLButtonElement = event.target as HTMLButtonElement; - - if (target.classList.contains('list-vertical-bar')) { + if ((target.parentElement?.classList.contains('elements-sidebar-row')) ?? false) { target = target.parentElement as HTMLButtonElement; } @@ -49,7 +48,7 @@ function HandleDragOver( event.preventDefault(); let target = event.target as HTMLElement; - if (target.classList.contains('list-vertical-bar')) { + if ((target.parentElement?.classList.contains('elements-sidebar-row')) ?? false) { target = target.parentElement as HTMLButtonElement; } @@ -83,7 +82,7 @@ function HandleOnDrop( const type = event.dataTransfer.getData('type'); let target: HTMLButtonElement = event.target as HTMLButtonElement; - if (target.classList.contains('list-vertical-bar')) { + if ((target.parentElement?.classList.contains('elements-sidebar-row')) ?? false) { target = target.parentElement as HTMLButtonElement; } @@ -141,11 +140,34 @@ function HandleOnDrop( } } +function useDragComponentsListener( + setDragActive: (isActive: boolean) => void +): void { + React.useEffect(() => { + function HandleComponentDragStart(): void { + setDragActive(true); + } + function HandleComponentDragEnd(): void { + setDragActive(false); + } + window.addEventListener('componentDragStart', HandleComponentDragStart); + window.addEventListener('componentDragEnd', HandleComponentDragEnd); + return () => { + window.removeEventListener('componentDragStart', HandleComponentDragStart); + window.addEventListener('componentDragEnd', HandleComponentDragEnd); + }; + }); +} + export function ElementsSidebar(props: IElementsSidebarProps): JSX.Element { // States const divRef = React.useRef(null); + const [isDragActive, setDragActive] = React.useState(false); const [,height] = useSize(divRef); + // Hooks + useDragComponentsListener(setDragActive); + // Render const it = MakeRecursionDFSIterator(props.mainContainer, props.containers, 0, [0, 0], true); const containers = [...it]; @@ -176,6 +198,7 @@ export function ElementsSidebar(props: IElementsSidebarProps): JSX.Element { text, props.containers, props.mainContainer, + isDragActive, props.addContainer, props.selectContainer ) @@ -209,7 +232,7 @@ export function ElementsSidebar(props: IElementsSidebarProps): JSX.Element { @@ -230,6 +253,7 @@ function ElementsListRow( text: string, containers: Map, mainContainer: IContainerModel, + isDragActive: boolean, addContainer: (index: number, type: string, parent: string) => void, selectContainer: (containerId: string) => void ): JSX.Element { @@ -259,7 +283,9 @@ function ElementsListRow( return
-
+ {isDragActive && +
+ }
; }