@@ -9,6 +9,7 @@ import { saveToHistory } from "./history-handler.js"
99import colors from "../../colors"
1010import fixTwisted from "./fix-twisted"
1111import convertExpandingLineToPolygon from "./convert-expanding-line-to-polygon"
12+ import clamp from "clamp"
1213
1314const getRandomId = ( ) => Math . random ( ) . toString ( ) . split ( "." ) [ 1 ]
1415
@@ -207,7 +208,13 @@ export default (state: MainLayoutState, action: Action) => {
207208 } )
208209 }
209210 case "MOUSE_MOVE ": {
210- const { x , y } = action
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+ }
217+
211218 if ( ! state . mode ) return state
212219 if ( ! activeImage ) return state
213220 const { mouseDownAt } = state
@@ -352,18 +359,16 @@ export default (state: MainLayoutState, action: Action) => {
352359 }
353360 case "MOUSE_DOWN" : {
354361 if ( ! activeImage ) return state
355- const { x , y } = action
356- state = setIn ( state , [ "mouseDownAt" ] , { x, y } )
362+ let { x , y } = action
357363
358364 if ( state . allowedArea ) {
359- // TODO clamp x/y instead of giving up
360- // TODO or image bounds
361365 const aa = state . allowedArea
362- if ( x < aa . x || x > aa . x + aa . w || y < aa . y || y > aa . y + aa . h ) {
363- return state
364- }
366+ x = clamp ( x , aa . x , aa . x + aa . w )
367+ y = clamp ( y , aa . x , aa . x + aa . w )
365368 }
366369
370+ state = setIn ( state , [ "mouseDownAt" ] , { x, y } )
371+
367372 if ( state . mode ) {
368373 switch ( state . mode . mode ) {
369374 case "DRAW_POLYGON ": {
@@ -529,7 +534,13 @@ export default (state: MainLayoutState, action: Action) => {
529534 return setIn ( state , [ ...pathToActiveImage , "regions" ] , regions )
530535 }
531536 case "MOUSE_UP ": {
532- const { x , y } = action
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+ }
543+
533544 const { mouseDownAt = { x, y } } = state
534545 if ( ! state . mode ) return state
535546 state = setIn ( state , [ "mouseDownAt" ] , null )
0 commit comments