|
1 | 1 | import{Layers}from"constants/Layers";
|
2 |
| -importReact,{ReactNode}from"react"; |
| 2 | +importReact,{ReactNode,useCallback,useRef,useState,useEffect}from"react"; |
3 | 3 |
|
4 | 4 | exporttypeCheckSelectFn=(
|
5 | 5 | item?:HTMLDivElement|null,
|
@@ -29,159 +29,154 @@ interface SectionState {
|
29 | 29 | mouseDown:boolean;
|
30 | 30 | selectionBox?:Rect;
|
31 | 31 | startPoint?:Point;
|
| 32 | +appendMode:boolean; |
32 | 33 | }
|
33 | 34 |
|
34 |
| -constInitialState={ |
| 35 | +constcreateInitialState=():SectionState=>({ |
35 | 36 | mouseDown:false,
|
36 | 37 | appendMode:false,
|
37 | 38 | selectionBox:undefined,
|
38 | 39 | startPoint:undefined,
|
39 |
| -}; |
40 |
| - |
41 |
| -classDragSelectorCompextendsReact.Component<SectionProps,SectionState>{ |
42 |
| -privatereadonlyselectAreaRef:React.RefObject<HTMLDivElement>; |
43 |
| - |
44 |
| -constructor(props:SectionProps){ |
45 |
| -super(props); |
46 |
| -this.selectAreaRef=React.createRef<HTMLDivElement>(); |
47 |
| -this.state=InitialState; |
48 |
| -this._onMouseMove=this._onMouseMove.bind(this); |
49 |
| -this._onMouseUp=this._onMouseUp.bind(this); |
50 |
| -} |
51 |
| - |
52 |
| -componentWillUnmount():void{ |
53 |
| -window.document.removeEventListener("mousemove",this._onMouseMove); |
54 |
| -window.document.removeEventListener("mouseup",this._onMouseUp); |
55 |
| -} |
56 |
| - |
57 |
| -_onMouseDown(e:React.MouseEvent<HTMLDivElement>){ |
58 |
| -if(e.button===2||e.nativeEvent.which===2){ |
59 |
| -return; |
60 |
| -} |
61 |
| -letnextState:SectionState={mouseDown:false}; |
62 |
| -nextState.mouseDown=true; |
63 |
| -nextState.startPoint={ |
64 |
| -x:e.pageX-(this.selectAreaRef.current?.getBoundingClientRect().left??0), |
65 |
| -y:e.pageY-(this.selectAreaRef.current?.getBoundingClientRect().top??0), |
| 40 | +}); |
| 41 | + |
| 42 | +exportconstDragSelector=React.memo((props:SectionProps)=>{ |
| 43 | +constselectAreaRef=useRef<HTMLDivElement>(null); |
| 44 | +const[state,setState]=useState<SectionState>(createInitialState()); |
| 45 | +constmountedRef=useRef(true); |
| 46 | + |
| 47 | +// Cleanup on unmount |
| 48 | +useEffect(()=>{ |
| 49 | +return()=>{ |
| 50 | +mountedRef.current=false; |
| 51 | +// Clean up any remaining event listeners |
| 52 | +window.document.removeEventListener("mousemove",handleMouseMove); |
| 53 | +window.document.removeEventListener("mouseup",handleMouseUp); |
| 54 | +}; |
| 55 | +},[]); |
| 56 | + |
| 57 | +consthandleMouseMove=useCallback((e:MouseEvent)=>{ |
| 58 | +if(!mountedRef.current||!state.mouseDown)return; |
| 59 | + |
| 60 | +constendPoint={ |
| 61 | +x:e.pageX-(selectAreaRef.current?.getBoundingClientRect().left??0), |
| 62 | +y:e.pageY-(selectAreaRef.current?.getBoundingClientRect().top??0), |
66 | 63 | };
|
67 |
| -this.setState(nextState); |
68 |
| -window.document.addEventListener("mousemove",this._onMouseMove); |
69 |
| -window.document.addEventListener("mouseup",this._onMouseUp); |
70 |
| -this.props.onMouseDown(); |
71 |
| -} |
72 |
| - |
73 |
| -_onMouseUp(){ |
74 |
| -window.document.removeEventListener("mousemove",this._onMouseMove); |
75 |
| -window.document.removeEventListener("mouseup",this._onMouseUp); |
76 |
| -this.props.onMouseUp(); |
77 |
| -this.setState(InitialState); |
78 |
| -} |
79 |
| - |
80 |
| -_onMouseMove(e:MouseEvent){ |
81 |
| -if(this.state.mouseDown){ |
82 |
| -letendPoint={ |
83 |
| -x:e.pageX-(this.selectAreaRef.current?.getBoundingClientRect().left??0), |
84 |
| -y:e.pageY-(this.selectAreaRef.current?.getBoundingClientRect().top??0), |
85 |
| -}; |
86 |
| -this.setState({ |
87 |
| -selectionBox:this._calculateSelectionBox(this.state.startPoint,endPoint), |
88 |
| -}); |
| 64 | + |
| 65 | +setState(prevState=>({ |
| 66 | + ...prevState, |
| 67 | +selectionBox:calculateSelectionBox(prevState.startPoint,endPoint), |
| 68 | +})); |
| 69 | + |
| 70 | +// Clean up selection properly |
| 71 | +constselection=window.getSelection(); |
| 72 | +if(selection){ |
| 73 | +selection.removeAllRanges(); |
89 | 74 | }
|
90 |
| -// Disable selection of text during mouse movement |
91 |
| -varselection=window.getSelection(); |
92 |
| -selection!.removeAllRanges(); |
93 |
| -selection=null; |
94 |
| -this.props.onMouseMove(this.childrenViewCheckFunc); |
95 |
| -} |
96 |
| - |
97 |
| -rectIntersect=( |
| 75 | + |
| 76 | +props.onMouseMove(childrenViewCheckFunc); |
| 77 | +},[state.mouseDown,state.startPoint,props.onMouseMove]); |
| 78 | + |
| 79 | +consthandleMouseUp=useCallback(()=>{ |
| 80 | +if(!mountedRef.current)return; |
| 81 | + |
| 82 | +window.document.removeEventListener("mousemove",handleMouseMove); |
| 83 | +window.document.removeEventListener("mouseup",handleMouseUp); |
| 84 | +props.onMouseUp(); |
| 85 | +setState(createInitialState()); |
| 86 | +},[handleMouseMove,props.onMouseUp]); |
| 87 | + |
| 88 | +consthandleMouseDown=useCallback((e:React.MouseEvent<HTMLDivElement>)=>{ |
| 89 | +if(!mountedRef.current||e.button===2||e.nativeEvent.which===2)return; |
| 90 | + |
| 91 | +conststartPoint={ |
| 92 | +x:e.pageX-(selectAreaRef.current?.getBoundingClientRect().left??0), |
| 93 | +y:e.pageY-(selectAreaRef.current?.getBoundingClientRect().top??0), |
| 94 | +}; |
| 95 | + |
| 96 | +setState({ |
| 97 | +mouseDown:true, |
| 98 | + startPoint, |
| 99 | +selectionBox:undefined, |
| 100 | +appendMode:false, |
| 101 | +}); |
| 102 | + |
| 103 | +window.document.addEventListener("mousemove",handleMouseMove); |
| 104 | +window.document.addEventListener("mouseup",handleMouseUp); |
| 105 | +props.onMouseDown(); |
| 106 | +},[handleMouseMove,handleMouseUp,props.onMouseDown]); |
| 107 | + |
| 108 | +constrectIntersect=useCallback(( |
98 | 109 | selectionBox:Rect|undefined,
|
99 | 110 | item:HTMLElement|null|undefined
|
100 | 111 | ):boolean=>{
|
101 |
| -if(!selectionBox||!item){ |
102 |
| -returnfalse; |
103 |
| -} |
| 112 | +if(!selectionBox||!item||!selectAreaRef.current)returnfalse; |
| 113 | + |
| 114 | +constcontainerRect=selectAreaRef.current.getBoundingClientRect(); |
104 | 115 | constitemBox={
|
105 |
| -top: |
106 |
| -item.getBoundingClientRect().top- |
107 |
| -(this.selectAreaRef.current?.getBoundingClientRect().top??0), |
108 |
| -left: |
109 |
| -item.getBoundingClientRect().left- |
110 |
| -(this.selectAreaRef.current?.getBoundingClientRect().left??0), |
| 116 | +top:item.getBoundingClientRect().top-containerRect.top, |
| 117 | +left:item.getBoundingClientRect().left-containerRect.left, |
111 | 118 | width:item.getBoundingClientRect().width,
|
112 | 119 | height:item.getBoundingClientRect().height,
|
113 | 120 | };
|
| 121 | + |
114 | 122 | return(
|
115 | 123 | selectionBox.left<=itemBox.left+itemBox.width&&
|
116 | 124 | selectionBox.left+selectionBox.width>=itemBox.left&&
|
117 | 125 | selectionBox.top<=itemBox.top+itemBox.height&&
|
118 | 126 | selectionBox.top+selectionBox.height>=itemBox.top
|
119 | 127 | );
|
120 |
| -}; |
| 128 | +},[]); |
121 | 129 |
|
122 |
| -childrenViewCheckFunc=( |
| 130 | +constchildrenViewCheckFunc=useCallback(( |
123 | 131 | item?:HTMLDivElement|null,
|
124 | 132 | afterCheck?:(checkResult:boolean)=>void
|
125 | 133 | )=>{
|
126 |
| -constresult=this.rectIntersect(this.state.selectionBox,item); |
127 |
| -if(!!afterCheck){ |
| 134 | +constresult=rectIntersect(state.selectionBox,item); |
| 135 | +if(afterCheck){ |
128 | 136 | afterCheck(result);
|
129 | 137 | }
|
130 | 138 | returnresult;
|
131 |
| -}; |
| 139 | +},[state.selectionBox,rectIntersect]); |
132 | 140 |
|
133 |
| -render(){ |
134 |
| -return( |
135 |
| -<div |
136 |
| -ref={this.selectAreaRef} |
137 |
| -onMouseDown={this._onMouseDown.bind(this)} |
138 |
| -style={{position:"relative"}} |
139 |
| -> |
140 |
| -{this.props.children} |
141 |
| -{this.renderSelectionBox()} |
142 |
| -</div> |
143 |
| -); |
144 |
| -} |
145 |
| - |
146 |
| -renderSelectionBox(){ |
147 |
| -if( |
148 |
| -!this.state.mouseDown|| |
149 |
| -!this.state.startPoint|| |
150 |
| -!this.state.selectionBox|| |
151 |
| -!this.selectAreaRef.current |
152 |
| -){ |
| 141 | +constcalculateSelectionBox=useCallback((startPoint:Point|undefined,endPoint:Point)=>{ |
| 142 | +if(!state.mouseDown||!startPoint||!endPoint)returnundefined; |
| 143 | + |
| 144 | +return{ |
| 145 | +left:Math.min(startPoint.x,endPoint.x), |
| 146 | +top:Math.min(startPoint.y,endPoint.y), |
| 147 | +width:Math.abs(startPoint.x-endPoint.x), |
| 148 | +height:Math.abs(startPoint.y-endPoint.y), |
| 149 | +}; |
| 150 | +},[state.mouseDown]); |
| 151 | + |
| 152 | +constrenderSelectionBox=useCallback(()=>{ |
| 153 | +if(!state.mouseDown||!state.startPoint||!state.selectionBox||!selectAreaRef.current){ |
153 | 154 | returnnull;
|
154 | 155 | }
|
| 156 | + |
155 | 157 | return(
|
156 | 158 | <div
|
157 | 159 | style={{
|
158 | 160 | background:"rgba(51, 119, 255, 0.1)",
|
159 | 161 | position:"absolute",
|
160 | 162 | zIndex:Layers.dragSelectBox,
|
161 |
| -left:this.state.selectionBox.left, |
162 |
| -top:this.state.selectionBox.top, |
163 |
| -height:this.state.selectionBox.height, |
164 |
| -width:this.state.selectionBox.width, |
| 163 | +left:state.selectionBox.left, |
| 164 | +top:state.selectionBox.top, |
| 165 | +height:state.selectionBox.height, |
| 166 | +width:state.selectionBox.width, |
165 | 167 | }}
|
166 | 168 | />
|
167 | 169 | );
|
168 |
| -} |
169 |
| - |
170 |
| -_calculateSelectionBox(startPoint:Point|undefined,endPoint:Point){ |
171 |
| -if(!this.state.mouseDown||!startPoint||!endPoint){ |
172 |
| -returnundefined; |
173 |
| -} |
174 |
| -letleft=Math.min(startPoint.x,endPoint.x); |
175 |
| -lettop=Math.min(startPoint.y,endPoint.y); |
176 |
| -letwidth=Math.abs(startPoint.x-endPoint.x); |
177 |
| -letheight=Math.abs(startPoint.y-endPoint.y); |
178 |
| -return{ |
179 |
| -left:left, |
180 |
| -top:top, |
181 |
| -width:width, |
182 |
| -height:height, |
183 |
| -}; |
184 |
| -} |
185 |
| -} |
186 |
| - |
187 |
| -exportconstDragSelector=React.memo(DragSelectorComp); |
| 170 | +},[state.mouseDown,state.startPoint,state.selectionBox]); |
| 171 | + |
| 172 | +return( |
| 173 | +<div |
| 174 | +ref={selectAreaRef} |
| 175 | +onMouseDown={handleMouseDown} |
| 176 | +style={{position:"relative"}} |
| 177 | +> |
| 178 | +{props.children} |
| 179 | +{renderSelectionBox()} |
| 180 | +</div> |
| 181 | +); |
| 182 | +}); |