@react-querybuilder/dnd
Version:
Drag-and-drop-enabled version of react-querybuilder (DnD-library-agnostic)
2 lines • 3.38 kB
JavaScript
import{add as e,findPath as t,getParentPath as n,group as r,insert as i,isAncestor as a,lc as o,pathsAreEqual as s}from"react-querybuilder";const c=new Set([`shift`,`alt`,`meta`,`mod`,`ctrl`]),l={esc:`escape`,return:`enter`,".":`period`,",":`comma`,"-":`slash`," ":`space`,"`":`backquote`,"#":`backslash`,"+":`bracketright`,ShiftLeft:`shift`,ShiftRight:`shift`,AltLeft:`alt`,AltRight:`alt`,MetaLeft:`meta`,MetaRight:`meta`,OSLeft:`meta`,OSRight:`meta`,ControlLeft:`ctrl`,ControlRight:`ctrl`},u=e=>o((e&&l[e]||e||``).trim()).replace(/key|digit|numpad|arrow/,``),d=e=>c.has(e),f={"⌘":`meta`,cmd:`meta`,command:`meta`,"⊞":`meta`,win:`meta`,windows:`meta`,"⇧":`shift`,"⌥":`alt`,"⌃":`ctrl`,control:`ctrl`};typeof document<`u`&&(document.addEventListener(`keydown`,e=>{e.key!==void 0&&g([u(e.key),u(e.code)])}),document.addEventListener(`keyup`,e=>{e.key!==void 0&&_([u(e.key),u(e.code)])})),typeof window<`u`&&window.addEventListener(`blur`,()=>{p.clear()});const p=new Set,m=e=>Array.isArray(e),h=(e,t=`,`)=>(m(e)?e:e.split(t)).every(e=>{let t=o(e.trim());return p.has(f[t]??t)}),g=e=>{let t=Array.isArray(e)?e:[e];if(p.has(`meta`))for(let e of p)d(e)||p.delete(o(e));for(let e of t)p.add(o(e))},_=e=>{let t=Array.isArray(e)?e:[e];if(e===`meta`)p.clear();else for(let e of t)p.delete(o(e))},v=({dragging:e,path:t,schema:r,canDrop:i,groupModeModifierKey:o,disabled:c,rule:l})=>{if(h(o)&&c||e&&typeof i==`function`&&!i({dragging:e,hovering:{...l,path:t,qbId:r.qbId}}))return!1;if(r.qbId!==e.qbId)return!0;let u=n(t),d=n(e.path),f=t.at(-1),p=e.path.at(-1);return!(a(e.path,t)||s(t,e.path)||!h(o)&&s(u,d)&&(f===p-1||r.independentCombinators&&f===p-2))},y=({dragging:e,path:t,schema:r,canDrop:i,disabled:o,ruleGroup:c})=>{if(o||e&&typeof i==`function`&&!i({dragging:e,hovering:{...c,path:t,qbId:r.qbId}}))return!1;if(r.qbId!==e.qbId)return!0;let l=n(e.path),u=e.path.at(-1);return!(a(e.path,t)||s(t,l)&&u===0||s(t,e.path))},b=({dragging:e,path:t,schema:r,canDrop:i,groupModeModifierKey:o,hoveringItem:c})=>{let{path:l}=e;if(h(o)||e&&typeof i==`function`&&!i({dragging:e,hovering:{...c,path:t,qbId:r.qbId}}))return!1;let u=n(t),d=n(l),f=t.at(-1),p=l.at(-1);return!(a(l,t)||s(l,t)||s(u,d)&&f-1===p||r.independentCombinators&&s(u,d)&&f===p-1)},x=({type:e,path:t,schema:n,copyModeModifierKey:r,groupModeModifierKey:i,copyModeOverride:a,groupModeOverride:o})=>{let{qbId:s,getQuery:c,dispatchQuery:l}=n;return{type:e,path:t,qbId:s,getQuery:c,dispatchQuery:l,groupItems:o||h(i),dropEffect:a||h(r)?`copy`:`move`}},S=(e,t)=>{let r=n(e.path),i=e.path.at(-1);return t?e.path:e.type===`ruleGroup`?[...e.path,0]:e.type===`inlineCombinator`?[...r,i]:[...r,i+1]},C=({item:t,dropResult:n,schema:a,actions:o,copyModeModifierKey:s,groupModeModifierKey:c,copyModeOverride:l,groupModeOverride:u,onRuleDrop:d})=>{if(!n)return;let f=l||h(s)?`copy`:`move`,p=u||h(c),m=S(n,p),g=a.qbId!==n.qbId;if(!g)p?o.groupRule(t.path,m,f===`copy`):o.moveRule(t.path,m,f===`copy`);else{let a=n.getQuery();a&&(p?n.dispatchQuery(r(e(a,t,[]),[a.rules.length],m,{clone:!1})):n.dispatchQuery(i(a,t,m)),f!==`copy`&&o.onRuleRemove(t.path))}d?.({draggedItem:t,sourceQbId:a.qbId,targetQbId:n.qbId,sourcePath:t.path,targetPath:m,dropEffect:f,groupItems:p,isCrossBuilder:g})},w=(e,n)=>({...t(e,n.getQuery()),path:e,qbId:n.qbId});export{w as a,y as i,b as n,C as o,v as r,h as s,x as t};
//# sourceMappingURL=dndLogic-DR8uS_bm.mjs.map