@@ -9,6 +9,7 @@ import { saveToHistory } from "./history-handler.js"
9
9
import colors from "../../colors"
10
10
import fixTwisted from "./fix-twisted"
11
11
import convertExpandingLineToPolygon from "./convert-expanding-line-to-polygon"
12
+ import clamp from "clamp"
12
13
13
14
const getRandomId = ( ) => Math . random ( ) . toString ( ) . split ( "." ) [ 1 ]
14
15
@@ -207,7 +208,13 @@ export default (state: MainLayoutState, action: Action) => {
207
208
} )
208
209
}
209
210
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
+
211
218
if ( ! state . mode ) return state
212
219
if ( ! activeImage ) return state
213
220
const { mouseDownAt } = state
@@ -352,18 +359,16 @@ export default (state: MainLayoutState, action: Action) => {
352
359
}
353
360
case "MOUSE_DOWN" : {
354
361
if ( ! activeImage ) return state
355
- const { x , y } = action
356
- state = setIn ( state , [ "mouseDownAt" ] , { x, y } )
362
+ let { x , y } = action
357
363
358
364
if ( state . allowedArea ) {
359
- // TODO clamp x/y instead of giving up
360
- // TODO or image bounds
361
365
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 )
365
368
}
366
369
370
+ state = setIn ( state , [ "mouseDownAt" ] , { x, y } )
371
+
367
372
if ( state . mode ) {
368
373
switch ( state . mode . mode ) {
369
374
case "DRAW_POLYGON ": {
@@ -529,7 +534,13 @@ export default (state: MainLayoutState, action: Action) => {
529
534
return setIn ( state , [ ...pathToActiveImage , "regions" ] , regions )
530
535
}
531
536
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
+
533
544
const { mouseDownAt = { x, y } } = state
534
545
if ( ! state . mode ) return state
535
546
state = setIn ( state , [ "mouseDownAt" ] , null )
0 commit comments