2024-09-21 18:37:28 +05:30
|
|
|
import React, { createContext, useContext, useState } from 'react'
|
2024-09-22 12:39:03 +05:30
|
|
|
import { isSubClassOfWidget } from '../../utils/widget'
|
|
|
|
|
// import Widget from '../../canvas/widgets/base'
|
2024-09-16 12:23:15 +05:30
|
|
|
|
2024-09-21 18:37:28 +05:30
|
|
|
export const DragContext = createContext()
|
2024-09-16 12:23:15 +05:30
|
|
|
|
|
|
|
|
export const useDragContext = () => useContext(DragContext)
|
|
|
|
|
|
|
|
|
|
// Provider component to wrap around parts of your app that need drag-and-drop functionality
|
|
|
|
|
export const DragProvider = ({ children }) => {
|
|
|
|
|
const [draggedElement, setDraggedElement] = useState(null)
|
2024-09-17 18:32:33 +05:30
|
|
|
const [overElement, setOverElement] = useState(null) // the element the dragged items is over
|
2024-09-16 12:23:15 +05:30
|
|
|
|
2024-09-22 12:39:03 +05:30
|
|
|
const [widgetClass, setWidgetClass] = useState(null) // helper to help pass the widget type from sidebar to canvas
|
|
|
|
|
|
|
|
|
|
const onDragStart = (element, widgetClass=null) => {
|
2024-09-16 12:23:15 +05:30
|
|
|
setDraggedElement(element)
|
2024-09-22 12:39:03 +05:30
|
|
|
|
|
|
|
|
if (widgetClass && !isSubClassOfWidget(widgetClass))
|
|
|
|
|
throw new Error("widgetClass must inherit from the Widget base class")
|
|
|
|
|
|
|
|
|
|
setWidgetClass(() => widgetClass) // store the class so later it can be passed to the canvas from sidebar
|
2024-09-16 12:23:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onDragEnd = () => {
|
|
|
|
|
setDraggedElement(null)
|
2024-09-22 12:39:03 +05:30
|
|
|
setWidgetClass(null)
|
2024-09-16 12:23:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2024-09-22 12:39:03 +05:30
|
|
|
<DragContext.Provider value={{ draggedElement, overElement, setOverElement,
|
|
|
|
|
widgetClass, onDragStart, onDragEnd }}>
|
2024-09-16 12:23:15 +05:30
|
|
|
{children}
|
|
|
|
|
</DragContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|