@@ -38,11 +38,12 @@ const clickTolerance = 5
38
38
const clickInterval = 250
39
39
40
40
class Selection {
41
- constructor ( node , { global = false , longPressThreshold = 250 } = { } ) {
41
+ constructor ( node , { global = false , longPressThreshold = 250 , validContainers = [ ] } = { } ) {
42
42
this . isDetached = false
43
43
this . container = node
44
44
this . globalMouse = ! node || global
45
45
this . longPressThreshold = longPressThreshold
46
+ this . validContainers = validContainers
46
47
47
48
this . _listeners = Object . create ( null )
48
49
@@ -303,6 +304,20 @@ class Selection {
303
304
}
304
305
}
305
306
307
+ // Check whether provided event target element
308
+ // - is contained within a valid container
309
+ _isWithinValidContainer ( e ) {
310
+ const eventTarget = e . target ;
311
+ const containers = this . validContainers ;
312
+
313
+ if ( ! containers || ! containers . length || ! eventTarget ) {
314
+ return true ;
315
+ }
316
+
317
+ return containers . some (
318
+ ( target ) => ! ! eventTarget . closest ( target ) ) ;
319
+ }
320
+
306
321
_handleTerminatingEvent ( e ) {
307
322
const { pageX, pageY } = getEventCoordinates ( e )
308
323
@@ -314,12 +329,13 @@ class Selection {
314
329
if ( ! this . _initialEventData ) return
315
330
316
331
let inRoot = ! this . container || contains ( this . container ( ) , e . target )
332
+ let isWithinValidContainer = this . _isWithinValidContainer ( e )
317
333
let bounds = this . _selectRect
318
334
let click = this . isClick ( pageX , pageY )
319
335
320
336
this . _initialEventData = null
321
337
322
- if ( e . key === 'Escape' ) {
338
+ if ( e . key === 'Escape' || ! isWithinValidContainer ) {
323
339
return this . emit ( 'reset' )
324
340
}
325
341
0 commit comments