@@ -14,6 +14,15 @@ import clamp from "clamp"
1414const getRandomId = ( ) => Math . random ( ) . toString ( ) . split ( "." ) [ 1 ]
1515
1616export default ( state : MainLayoutState , action : Action ) => {
17+ if (
18+ state . allowedArea &&
19+ [ "MOUSE_DOWN" , "MOUSE_UP" , "MOUSE_MOVE" ] . includes ( action . type )
20+ ) {
21+ const aa = state . allowedArea
22+ action . x = clamp ( action . x , aa . x , aa . x + aa . w )
23+ action . y = clamp ( action . y , aa . y , aa . y + aa . h )
24+ }
25+
1726 if ( action . type === "ON_CLS_ADDED" && action . cls && action . cls !== "" ) {
1827 const oldRegionClsList = state . regionClsList
1928 const newState = {
@@ -208,12 +217,7 @@ export default (state: MainLayoutState, action: Action) => {
208217 } )
209218 }
210219 case "MOUSE_MOVE ": {
211- let { x , y } = action
212- if ( state . allowedArea ) {
213- const aa = state . allowedArea
214- x = clamp ( x , aa . x , aa . x + aa . w )
215- y = clamp ( y , aa . x , aa . x + aa . w )
216- }
220+ const { x , y } = action
217221
218222 if ( ! state . mode ) return state
219223 if ( ! activeImage ) return state
@@ -359,13 +363,7 @@ export default (state: MainLayoutState, action: Action) => {
359363 }
360364 case "MOUSE_DOWN" : {
361365 if ( ! activeImage ) return state
362- let { x , y } = action
363-
364- if ( state . allowedArea ) {
365- const aa = state . allowedArea
366- x = clamp ( x , aa . x , aa . x + aa . w )
367- y = clamp ( y , aa . x , aa . x + aa . w )
368- }
366+ const { x , y } = action
369367
370368 state = setIn ( state , [ "mouseDownAt" ] , { x, y } )
371369
@@ -534,12 +532,7 @@ export default (state: MainLayoutState, action: Action) => {
534532 return setIn ( state , [ ...pathToActiveImage , "regions" ] , regions )
535533 }
536534 case "MOUSE_UP ": {
537- let { x , y } = action
538- if ( state . allowedArea ) {
539- const aa = state . allowedArea
540- x = clamp ( x , aa . x , aa . x + aa . w )
541- y = clamp ( y , aa . x , aa . x + aa . w )
542- }
535+ const { x , y } = action
543536
544537 const { mouseDownAt = { x, y } } = state
545538 if ( ! state . mode ) return state
0 commit comments