import { deepFlat } from "@daybrush/utils"; import * as React from "react"; import { useKeycon } from "react-keycon"; import Selecto from "react-selecto"; import Moveable, { MoveableTargetGroupsType } from "@/react-moveable"; import { GroupManager, TargetList } from "@moveable/helper"; export default function App() { const { isKeydown: isCommand } = useKeycon({ keys: "meta" }); const { isKeydown: isShift } = useKeycon({ keys: "shift" }); const groupManagerRef = React.useRef(); const [targets, setTargets] = React.useState([]); const moveableRef = React.useRef(null); const selectoRef = React.useRef(null); const cubes = []; for (let i = 0; i < 30; ++i) { cubes.push(i); } const setSelectedTargets = React.useCallback((nextTargetes: MoveableTargetGroupsType) => { selectoRef.current!.setSelectedTargets(deepFlat(nextTargetes)); setTargets(nextTargetes); }, []); React.useEffect(() => { // [[0, 1], 2], 3, 4, [5, 6], 7, 8, 9 const elements = selectoRef.current!.getSelectableElements(); groupManagerRef.current = new GroupManager([ [[elements[0], elements[1]], elements[2]], [elements[5], elements[6], elements[7]], ], elements); }, []); return
{ if (!e.moveableTarget) { setSelectedTargets([]); return; } if (e.isDouble) { const childs = groupManagerRef!.current!.selectSubChilds(targets, e.moveableTarget); setSelectedTargets(childs.targets()); return; } if (e.isTrusted) { selectoRef.current!.clickTarget(e.inputEvent, e.moveableTarget); } }} onDrag={e => { e.target.style.transform = e.transform; }} onRenderGroup={e => { e.events.forEach(ev => { ev.target.style.cssText += ev.cssText; }); }} > { const moveable = moveableRef.current!; const target = e.inputEvent.target; // Must have use deep flat const flatted = targets.flat(3) as Array; if ( moveable.isMoveableElement(target) || flatted.some(t => t === target || t.contains(target)) ) { e.stop(); } }} onSelectEnd={e => { const { isDragStartEnd, isClick, added, removed, inputEvent, } = e; const moveable = moveableRef.current!; if (isDragStartEnd) { inputEvent.preventDefault(); moveable.waitToChangeTarget().then(() => { moveable.dragStart(inputEvent); }); } const groupManager = groupManagerRef.current!; let nextChilds: TargetList; if (isDragStartEnd || isClick) { if (isCommand) { nextChilds = groupManager.selectSingleChilds(targets, added, removed); } else { nextChilds = groupManager.selectCompletedChilds(targets, added, removed, isShift); } } else { nextChilds = groupManager.selectSameDepthChilds(targets, added, removed); } e.currentTarget.setSelectedTargets(nextChilds.flatten()); setSelectedTargets(nextChilds.targets()); }} >

[[0, 1], 2] is group

[5, 6, 7] is group

{cubes.map(i =>
{i}
)}
; }