( componentId: string, accept: (ComponentType | MetaComponentType)[] = rootComponents, canDrop: boolean = true, )
| 4 | import builder from '~core/models/composer/builder' |
| 5 | |
| 6 | export const useDropComponent = ( |
| 7 | componentId: string, |
| 8 | accept: (ComponentType | MetaComponentType)[] = rootComponents, |
| 9 | canDrop: boolean = true, |
| 10 | ) => { |
| 11 | const dispatch = useDispatch() |
| 12 | |
| 13 | const [{ isOver }, drop] = useDrop({ |
| 14 | accept, |
| 15 | collect: monitor => ({ |
| 16 | isOver: monitor.isOver({ shallow: true }) && monitor.canDrop(), |
| 17 | }), |
| 18 | drop: (item: ComponentItemProps, monitor: DropTargetMonitor) => { |
| 19 | if (!monitor.isOver()) { |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | if (item.isMoved) { |
| 24 | dispatch.components.moveComponent({ |
| 25 | parentId: componentId, |
| 26 | componentId: item.id, |
| 27 | }) |
| 28 | } else if (item.isMeta) { |
| 29 | dispatch.components.addMetaComponent(builder[item.type](componentId)) |
| 30 | } else { |
| 31 | dispatch.components.addComponent({ |
| 32 | parentName: componentId, |
| 33 | type: item.type, |
| 34 | rootParentType: item.rootParentType, |
| 35 | }) |
| 36 | } |
| 37 | }, |
| 38 | canDrop: () => canDrop, |
| 39 | }) |
| 40 | |
| 41 | return { drop, isOver } |
| 42 | } |
no test coverage detected