UNPKG

@react-querybuilder/dnd

Version:

Drag-and-drop-enabled version of react-querybuilder (DnD-library-agnostic)

2 lines 4.12 kB
import{i as e,n as t,o as n,r,s as i,t as a}from"./dndLogic-DR8uS_bm.mjs";import*as o from"react";import{useRef as s}from"react";import{findPath as c}from"react-querybuilder";const l=()=>typeof window<`u`&&`ontouchstart`in window||typeof navigator<`u`&&navigator.maxTouchPoints>0;let u;const d=()=>(u||(u=new Image,u.src=`data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==`),u),f=[`rule`,`ruleGroup`],p=(e,t,r)=>r(()=>({type:t,item:()=>({...c(e.path,e.schema.getQuery()),path:e.path,qbId:e.schema.qbId}),canDrag:!e.disabled,previewOptions:{captureDraggingState:!!e.hideDefaultDragPreview},collect:t=>({isDragging:!e.disabled&&t.isDragging(),dragMonitorId:t.getHandlerId()??``}),end:(t,r)=>{n({item:t,dropResult:r.getDropResult(),schema:e.schema,actions:e.actions,copyModeModifierKey:e.copyModeModifierKey,groupModeModifierKey:e.groupModeModifierKey,onRuleDrop:e.onRuleDrop})}}),[e.actions.groupRule,e.actions.moveRule,e.disabled,e.path]),m=n=>{let{useDrag:c,useDrop:u,DndProvider:m,DndContext:h}=n,g=n.ReactDndBackend;return g||=l()?n.TouchBackend??n.HTML5Backend:n.HTML5Backend??n.TouchBackend,{DndProvider:({debugMode:e,children:t})=>o.createElement(m,{backend:g,debugMode:e},o.createElement(h.Consumer,null,()=>o.createElement(o.Fragment,null,t))),useRuleDnD:e=>{let t=s(null),n=s(null),[{isDragging:l,dragMonitorId:m},h,g]=p(e,`rule`,c),[{isOver:_,dropMonitorId:v,dropEffect:y,groupItems:b,dropNotAllowed:x},S]=u(()=>({accept:f,canDrop:t=>r({dragging:t,path:e.path,schema:e.schema,canDrop:e.canDrop,groupModeModifierKey:e.groupModeModifierKey,disabled:e.disabled,rule:e.rule}),collect:t=>({dropNotAllowed:t.isOver()&&!t.canDrop(),isOver:t.canDrop()&&t.isOver(),dropMonitorId:t.getHandlerId()??``,dropEffect:i(e.copyModeModifierKey)?`copy`:`move`,groupItems:i(e.groupModeModifierKey)}),drop:()=>a({type:`rule`,path:e.path,schema:e.schema,copyModeModifierKey:e.copyModeModifierKey,groupModeModifierKey:e.groupModeModifierKey})}),[e.disabled,e.actions.moveRule,e.path,e.canDrop,e.rule,e.schema]);return o.useEffect(()=>{h(n),S(t),g(e.hideDefaultDragPreview?d():t)},[h,S,e.hideDefaultDragPreview,g]),{isDragging:l,dragMonitorId:m,isOver:_,dropMonitorId:v,dndRef:t,dragRef:n,dropEffect:y,groupItems:b,dropNotAllowed:x}},useRuleGroupDnD:t=>{let n=s(null),r=s(null),l=s(null),[{isDragging:m,dragMonitorId:h},g,_]=p(t,`ruleGroup`,c),[{isOver:v,dropMonitorId:y,dropEffect:b,groupItems:x,dropNotAllowed:S},C]=u(()=>({accept:f,canDrop:n=>e({dragging:n,path:t.path,schema:t.schema,canDrop:t.canDrop,disabled:t.disabled,ruleGroup:t.ruleGroup}),collect:e=>({dropNotAllowed:e.isOver()&&!e.canDrop(),isOver:e.canDrop()&&e.isOver(),dropMonitorId:e.getHandlerId()??``,dropEffect:i(t.copyModeModifierKey)?`copy`:`move`,groupItems:i(t.groupModeModifierKey)}),drop:()=>a({type:`ruleGroup`,path:t.path,schema:t.schema,copyModeModifierKey:t.copyModeModifierKey,groupModeModifierKey:t.groupModeModifierKey})}),[t.disabled,t.actions.groupRule,t.actions.moveRule,t.path,t.canDrop,t.ruleGroup,t.schema]);return o.useEffect(()=>{t.path.length>0&&(g(r),_(t.hideDefaultDragPreview?d():n)),C(l)},[g,C,t.hideDefaultDragPreview,t.path.length,_]),{isDragging:m,dragMonitorId:h,isOver:v,dropMonitorId:y,previewRef:n,dragRef:r,dropRef:l,dropEffect:b,groupItems:x,dropNotAllowed:S}},useInlineCombinatorDnD:e=>{let n=s(null),r=(e.rules??[])[e.path.at(-1)-1],[{isOver:o,dropMonitorId:c,dropEffect:l,dropNotAllowed:d},f]=u(()=>({accept:[`rule`,`ruleGroup`],canDrop:n=>t({dragging:n,path:e.path,schema:e.schema,canDrop:e.canDrop,groupModeModifierKey:e.groupModeModifierKey,hoveringItem:r}),collect:t=>({dropNotAllowed:t.isOver()&&!t.canDrop(),isOver:t.canDrop()&&t.isOver(),dropMonitorId:t.getHandlerId()??``,dropEffect:i(e.copyModeModifierKey)?`copy`:`move`,groupItems:i(e.groupModeModifierKey)}),drop:()=>a({type:`inlineCombinator`,path:e.path,schema:e.schema,copyModeModifierKey:e.copyModeModifierKey,groupModeModifierKey:e.groupModeModifierKey})}),[e.canDrop,r,e.path,e.schema]);return f(n),{dropRef:n,dropMonitorId:c,isOver:o,dropEffect:l,dropNotAllowed:d}}}};export{l as n,m as t}; //# sourceMappingURL=react-dnd-Ded7Ym3k.mjs.map