// src/InlineCombinatorDnD.tsx import * as React from "react"; import { useContext } from "react"; import { standardClassnames, TestID } from "react-querybuilder"; // src/hooks/useInlineCombinatorDnD.ts import { useRef } from "react"; import { getParentPath, isAncestor, pathsAreEqual } from "react-querybuilder"; var useInlineCombinatorDnD = ({ path, independentCombinators, useDrop }) => { const dropRef = useRef(null); const [{ isOver, dropMonitorId }, drop] = useDrop( () => ({ accept: ["rule", "ruleGroup"], canDrop: (item) => { const parentHoverPath = getParentPath(path); const parentItemPath = getParentPath(item.path); const hoverIndex = path[path.length - 1]; const itemIndex = item.path[item.path.length - 1]; return !(isAncestor(item.path, path) || pathsAreEqual(item.path, path) || pathsAreEqual(parentHoverPath, parentItemPath) && hoverIndex - 1 === itemIndex || independentCombinators && pathsAreEqual(parentHoverPath, parentItemPath) && hoverIndex === itemIndex - 1); }, collect: (monitor) => ({ isOver: monitor.canDrop() && monitor.isOver(), dropMonitorId: monitor.getHandlerId() ?? "", dropEffect: (monitor.getDropResult() ?? {}).dropEffect }), drop: () => ({ type: "inlineCombinator", path }) }), [path, independentCombinators] ); drop(dropRef); return { dropRef, dropMonitorId, isOver }; }; // src/hooks/useReactDnD.ts import { useEffect, useState } from "react"; import { messages } from "react-querybuilder"; var didWarnEnabledDndWithoutReactDnD = false; var useReactDnD = (dndParam) => { const [dnd, setDnd] = useState(dndParam ?? null); useEffect(() => { let didCancel = false; const getDnD = async () => { const [reactDnD, reactDnDHTML5Be] = await Promise.all([ import("react-dnd").catch(() => null), import("react-dnd-html5-backend").catch(() => null) ]); if (!didCancel) { if (reactDnD && reactDnDHTML5Be) { setDnd(() => ({ ...reactDnD, ...reactDnDHTML5Be })); } else { if (process.env.NODE_ENV !== "production" && !didWarnEnabledDndWithoutReactDnD) { console.error(messages.errorEnabledDndWithoutReactDnD); didWarnEnabledDndWithoutReactDnD = true; } } } }; if (!dnd) { getDnD(); } return () => { didCancel = true; }; }, []); return dnd; }; // src/hooks/useRuleDnD.ts import { useRef as useRef2 } from "react"; import { getParentPath as getParentPath3, isAncestor as isAncestor2, pathsAreEqual as pathsAreEqual2 } from "react-querybuilder"; // src/hooks/useDragCommon.ts import { getParentPath as getParentPath2 } from "react-querybuilder"; var useDragCommon = ({ type, path, disabled, moveRule, // Unused for now // independentCombinators, useDrag }) => useDrag( () => ({ type, item: { path }, canDrag: !disabled, collect: (monitor) => ({ isDragging: !disabled && monitor.isDragging(), dragMonitorId: monitor.getHandlerId() ?? "" }), end(item, monitor) { const dropResult = monitor.getDropResult(); if (!dropResult) return; const parentHoverPath = getParentPath2(dropResult.path); const hoverIndex = dropResult.path[dropResult.path.length - 1]; const destinationPath = dropResult.type === "ruleGroup" ? [...dropResult.path, 0] : dropResult.type === "inlineCombinator" ? [...parentHoverPath, hoverIndex] : [...parentHoverPath, hoverIndex + 1]; moveRule(item.path, destinationPath, dropResult.dropEffect === "copy"); } }), [disabled, path] ); // src/hooks/useRuleDnD.ts var useRuleDnD = ({ path, disabled, independentCombinators, moveRule, useDrag, useDrop }) => { const dndRef = useRef2(null); const dragRef = useRef2(null); const [{ isDragging, dragMonitorId }, drag, preview] = useDragCommon({ type: "rule", path, disabled, independentCombinators, moveRule, useDrag }); const [{ isOver, dropMonitorId, dropEffect }, drop] = useDrop( () => ({ accept: ["rule", "ruleGroup"], canDrop: (item) => { const parentHoverPath = getParentPath3(path); const parentItemPath = getParentPath3(item.path); const hoverIndex = path[path.length - 1]; const itemIndex = item.path[item.path.length - 1]; return !(isAncestor2(item.path, path) || pathsAreEqual2(parentHoverPath, parentItemPath) && (hoverIndex === itemIndex || hoverIndex === itemIndex - 1 || independentCombinators && hoverIndex === itemIndex - 2)); }, collect: (monitor) => ({ isOver: monitor.canDrop() && monitor.isOver(), dropMonitorId: monitor.getHandlerId() ?? "", dropEffect: (monitor.getDropResult() ?? {}).dropEffect }), // `dropEffect` gets added automatically to the object returned from `drop`: drop: () => ({ type: "rule", path }) }), [disabled, independentCombinators, moveRule, path] ); drag(dragRef); preview(drop(dndRef)); return { isDragging, dragMonitorId, isOver, dropMonitorId, dndRef, dragRef, dropEffect }; }; // src/hooks/useRuleGroupDnD.ts import { useRef as useRef3 } from "react"; import { getParentPath as getParentPath4, isAncestor as isAncestor3, pathsAreEqual as pathsAreEqual3 } from "react-querybuilder"; var useRuleGroupDnD = ({ disabled, path, independentCombinators, moveRule, useDrag, useDrop }) => { const previewRef = useRef3(null); const dragRef = useRef3(null); const dropRef = useRef3(null); const [{ isDragging, dragMonitorId }, drag, preview] = useDragCommon({ type: "ruleGroup", path, disabled, independentCombinators, moveRule, useDrag }); const [{ isOver, dropMonitorId, dropEffect }, drop] = useDrop( () => ({ accept: ["rule", "ruleGroup"], canDrop: (item) => { if (disabled) return false; const parentItemPath = getParentPath4(item.path); const itemIndex = item.path[item.path.length - 1]; return !(isAncestor3(item.path, path) || pathsAreEqual3(path, parentItemPath) && itemIndex === 0 || pathsAreEqual3(path, item.path)); }, collect: (monitor) => ({ isOver: monitor.canDrop() && monitor.isOver(), dropMonitorId: monitor.getHandlerId() ?? "", dropEffect: (monitor.getDropResult() ?? {}).dropEffect }), // `dropEffect` gets added automatically to the object returned from `drop`: drop: (_item, monitor) => monitor.getDropResult() ?? { type: "ruleGroup", path } }), [disabled, moveRule, path] ); if (path.length > 0) { drag(dragRef); preview(previewRef); } drop(dropRef); return { isDragging, dragMonitorId, isOver, dropMonitorId, previewRef, dragRef, dropRef, dropEffect }; }; // src/QueryBuilderDndContext.ts import { createContext } from "react"; import { defaultControlElements } from "react-querybuilder"; var { rule, ruleGroup, combinatorSelector } = defaultControlElements; var QueryBuilderDndContext = createContext({ baseControls: { rule, ruleGroup, combinatorSelector } }); QueryBuilderDndContext.displayName = "QueryBuilderDndContext"; // src/InlineCombinatorDnD.tsx var InlineCombinatorDnD = ({ component: CombinatorSelectorComponent, path, independentCombinators, ...props }) => { const { useDrop } = useContext(QueryBuilderDndContext); const { dropRef, dropMonitorId, isOver } = useInlineCombinatorDnD({ path, independentCombinators, useDrop }); const dndOver = isOver ? standardClassnames.dndOver : ""; const wrapperClassName = `${standardClassnames.betweenRules}${dndOver ? ` ${dndOver}` : ""}`; return /* @__PURE__ */ React.createElement( "div", { key: "dnd", ref: dropRef, className: wrapperClassName, "data-dropmonitorid": dropMonitorId, "data-testid": TestID.inlineCombinator }, /* @__PURE__ */ React.createElement(CombinatorSelectorComponent, { ...props, path, testID: TestID.combinators }) ); }; InlineCombinatorDnD.displayName = "InlineCombinatorDnD"; // src/QueryBuilderDnD.tsx import * as React4 from "react"; import { useContext as useContext4 } from "react"; import { QueryBuilderContext, useMergedContext, usePreferProp } from "react-querybuilder"; // src/RuleDnD.tsx import * as React2 from "react"; import { useContext as useContext2 } from "react"; var RuleDnD = (props) => { const rqbDndContext = useContext2(QueryBuilderDndContext); const { useDrag, useDrop } = rqbDndContext; const { path, disabled: disabledProp, parentDisabled, actions: { moveRule }, schema: { independentCombinators } } = props; const disabled = !!parentDisabled || !!disabledProp; const dndRefs = useRuleDnD({ path, disabled, independentCombinators, moveRule, useDrag, useDrop }); const { rule: BaseRuleComponent } = rqbDndContext.baseControls; return /* @__PURE__ */ React2.createElement(QueryBuilderDndContext.Provider, { value: rqbDndContext }, /* @__PURE__ */ React2.createElement(BaseRuleComponent, { ...props, ...dndRefs })); }; RuleDnD.displayName = "RuleDnD"; // src/RuleGroupDnD.tsx import * as React3 from "react"; import { useContext as useContext3 } from "react"; var RuleGroupDnD = (props) => { const rqbDndContext = useContext3(QueryBuilderDndContext); const { useDrag, useDrop } = rqbDndContext; const { path, disabled: disabledProp, parentDisabled, actions: { moveRule }, schema: { independentCombinators } } = props; const disabled = !!parentDisabled || !!disabledProp; const dndRefs = useRuleGroupDnD({ disabled, path, independentCombinators, moveRule, useDrag, useDrop }); const { ruleGroup: BaseRuleGroupComponent } = rqbDndContext.baseControls; return /* @__PURE__ */ React3.createElement(BaseRuleGroupComponent, { ...props, ...dndRefs }); }; RuleGroupDnD.displayName = "RuleGroupDnD"; // src/QueryBuilderDnD.tsx var QueryBuilderDnD = (props) => { const { controlClassnames, controlElements, debugMode, enableDragAndDrop: enableDragAndDropProp, enableMountQueryChange, translations } = props; const rqbContext = useMergedContext({ controlClassnames, controlElements, debugMode, enableDragAndDrop: enableDragAndDropProp ?? true, enableMountQueryChange, translations: translations ?? {} }); const { enableDragAndDrop } = rqbContext; const dnd = useReactDnD(props.dnd); const key = enableDragAndDrop && dnd ? "dnd" : "no-dnd"; if (!enableDragAndDrop || !dnd) { return /* @__PURE__ */ React4.createElement( QueryBuilderContext.Provider, { key, value: { ...rqbContext, enableDragAndDrop: false, debugMode } }, props.children ); } const { DndProvider, HTML5Backend } = dnd; return /* @__PURE__ */ React4.createElement(DndProvider, { key, backend: HTML5Backend, debugMode }, /* @__PURE__ */ React4.createElement( QueryBuilderContext.Provider, { key, value: { ...rqbContext, enableDragAndDrop, debugMode } }, /* @__PURE__ */ React4.createElement(QueryBuilderDndWithoutProvider, { dnd }, props.children) )); }; QueryBuilderDnD.displayName = "QueryBuilderDnD"; var QueryBuilderDndWithoutProvider = (props) => { const rqbContext = useContext4(QueryBuilderContext); const rqbDndContext = useContext4(QueryBuilderDndContext); const dnd = useReactDnD(props.dnd); const debugMode = usePreferProp(false, props.debugMode, rqbContext.debugMode); const enableDragAndDrop = usePreferProp( true, props.enableDragAndDrop, rqbContext.enableDragAndDrop ); const key = enableDragAndDrop && dnd ? "dnd" : "no-dnd"; if (!enableDragAndDrop || !dnd) { return /* @__PURE__ */ React4.createElement( QueryBuilderContext.Provider, { key, value: { ...rqbContext, enableDragAndDrop: false, debugMode } }, props.children ); } const { DndContext, useDrag, useDrop } = dnd; const baseControls = { rule: props.controlElements?.rule ?? rqbContext.controlElements?.rule ?? rqbDndContext.baseControls.rule, ruleGroup: props.controlElements?.ruleGroup ?? rqbContext.controlElements?.ruleGroup ?? rqbDndContext.baseControls.ruleGroup, combinatorSelector: props.controlElements?.combinatorSelector ?? rqbContext.controlElements?.combinatorSelector ?? rqbDndContext.baseControls.combinatorSelector }; const newContext = { ...rqbContext, controlElements: { ...rqbContext.controlElements, ruleGroup: RuleGroupDnD, rule: RuleDnD, inlineCombinator: InlineCombinatorDnD } }; return /* @__PURE__ */ React4.createElement(DndContext.Consumer, { key }, () => /* @__PURE__ */ React4.createElement(QueryBuilderContext.Provider, { key, value: newContext }, /* @__PURE__ */ React4.createElement(QueryBuilderDndContext.Provider, { value: { useDrag, useDrop, baseControls } }, props.children))); }; QueryBuilderDndWithoutProvider.displayName = "QueryBuilderDndWithoutProvider"; export { InlineCombinatorDnD, QueryBuilderDnD, QueryBuilderDndWithoutProvider, RuleDnD, RuleGroupDnD }; //# sourceMappingURL=react-querybuilder_dnd.mjs.map