|
| 1 | +import { deepFlat } from "@daybrush/utils"; |
| 2 | +import * as React from "react"; |
| 3 | +import Selecto from "react-selecto"; |
| 4 | +import Moveable, { MoveableTargetGroupsType } from "@/react-moveable"; |
| 5 | +import { GroupManager, TargetList } from "@/helper"; |
| 6 | + |
| 7 | +export default function App() { |
| 8 | + const groupManager = React.useMemo<GroupManager>(() => new GroupManager([]), []); |
| 9 | + const [targets, setTargets] = React.useState<MoveableTargetGroupsType>([]); |
| 10 | + const moveableRef = React.useRef<Moveable>(null); |
| 11 | + const selectoRef = React.useRef<Selecto>(null); |
| 12 | + const cubes = []; |
| 13 | + |
| 14 | + for (let i = 0; i < 30; ++i) { |
| 15 | + cubes.push(i); |
| 16 | + } |
| 17 | + const setSelectedTargets = React.useCallback((nextTargetes: MoveableTargetGroupsType) => { |
| 18 | + selectoRef.current!.setSelectedTargets(deepFlat(nextTargetes)); |
| 19 | + setTargets(nextTargetes); |
| 20 | + }, []); |
| 21 | + React.useEffect(() => { |
| 22 | + // [[0, 1], 2], 3, 4, [5, 6], 7, 8, 9 |
| 23 | + const elements = selectoRef.current!.getSelectableElements(); |
| 24 | + |
| 25 | + groupManager.set([], elements); |
| 26 | + }, []); |
| 27 | + |
| 28 | + return <div className="root"> |
| 29 | + <div className="container"> |
| 30 | + <button onClick={() => { |
| 31 | + const nextGroup = groupManager.group(targets, true); |
| 32 | + |
| 33 | + if (nextGroup) { |
| 34 | + setTargets(nextGroup); |
| 35 | + } |
| 36 | + }}>Group</button> |
| 37 | + |
| 38 | + <button onClick={() => { |
| 39 | + const nextGroup = groupManager.ungroup(targets); |
| 40 | + |
| 41 | + if (nextGroup) { |
| 42 | + setTargets(nextGroup); |
| 43 | + } |
| 44 | + }}>Ungrooup</button> |
| 45 | + <Moveable |
| 46 | + ref={moveableRef} |
| 47 | + draggable={true} |
| 48 | + rotatable={true} |
| 49 | + scalable={true} |
| 50 | + target={targets} |
| 51 | + onDrag={e => { |
| 52 | + e.target.style.transform = e.transform; |
| 53 | + }} |
| 54 | + onRenderGroup={e => { |
| 55 | + e.events.forEach(ev => { |
| 56 | + ev.target.style.cssText += ev.cssText; |
| 57 | + }); |
| 58 | + }} |
| 59 | + ></Moveable> |
| 60 | + <Selecto |
| 61 | + ref={selectoRef} |
| 62 | + dragContainer={window} |
| 63 | + selectableTargets={[".selecto-area .cube"]} |
| 64 | + hitRate={0} |
| 65 | + selectByClick={true} |
| 66 | + selectFromInside={false} |
| 67 | + toggleContinueSelect={["shift"]} |
| 68 | + ratio={0} |
| 69 | + onDragStart={e => { |
| 70 | + const moveable = moveableRef.current!; |
| 71 | + const target = e.inputEvent.target; |
| 72 | + const flatted = deepFlat(targets); |
| 73 | + |
| 74 | + if ( |
| 75 | + target.tagName === "BUTTON" |
| 76 | + || moveable.isMoveableElement(target) |
| 77 | + || flatted.some(t => t === target || t.contains(target)) |
| 78 | + ) { |
| 79 | + e.stop(); |
| 80 | + } |
| 81 | + }} |
| 82 | + onSelectEnd={e => { |
| 83 | + const { |
| 84 | + isDragStart, |
| 85 | + added, |
| 86 | + removed, |
| 87 | + inputEvent, |
| 88 | + } = e; |
| 89 | + const moveable = moveableRef.current!; |
| 90 | + |
| 91 | + if (isDragStart) { |
| 92 | + inputEvent.preventDefault(); |
| 93 | + |
| 94 | + moveable.waitToChangeTarget().then(() => { |
| 95 | + moveable.dragStart(inputEvent); |
| 96 | + }); |
| 97 | + } |
| 98 | + let nextChilds: TargetList; |
| 99 | + |
| 100 | + if (isDragStart) { |
| 101 | + nextChilds = groupManager.selectCompletedChilds(targets, added, removed); |
| 102 | + } else { |
| 103 | + nextChilds = groupManager.selectSameDepthChilds(targets, added, removed); |
| 104 | + } |
| 105 | + |
| 106 | + e.currentTarget.setSelectedTargets(nextChilds.flatten()); |
| 107 | + setSelectedTargets(nextChilds.targets()); |
| 108 | + }} |
| 109 | + ></Selecto> |
| 110 | + <div className="elements selecto-area"> |
| 111 | + {cubes.map(i => <div className="cube" key={i}>{i}</div>)} |
| 112 | + </div> |
| 113 | + <div className="empty elements"></div> |
| 114 | + </div> |
| 115 | + </div>; |
| 116 | +} |
0 commit comments