@@ -38,11 +38,12 @@ const clickTolerance = 5
3838const clickInterval = 250
3939
4040class Selection {
41- constructor ( node , { global = false , longPressThreshold = 250 } = { } ) {
41+ constructor ( node , { global = false , longPressThreshold = 250 , validContainers = [ ] } = { } ) {
4242 this . isDetached = false
4343 this . container = node
4444 this . globalMouse = ! node || global
4545 this . longPressThreshold = longPressThreshold
46+ this . validContainers = validContainers
4647
4748 this . _listeners = Object . create ( null )
4849
@@ -303,6 +304,20 @@ class Selection {
303304 }
304305 }
305306
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+
306321 _handleTerminatingEvent ( e ) {
307322 const { pageX, pageY } = getEventCoordinates ( e )
308323
@@ -314,12 +329,13 @@ class Selection {
314329 if ( ! this . _initialEventData ) return
315330
316331 let inRoot = ! this . container || contains ( this . container ( ) , e . target )
332+ let isWithinValidContainer = this . _isWithinValidContainer ( e )
317333 let bounds = this . _selectRect
318334 let click = this . isClick ( pageX , pageY )
319335
320336 this . _initialEventData = null
321337
322- if ( e . key === 'Escape' ) {
338+ if ( e . key === 'Escape' || ! isWithinValidContainer ) {
323339 return this . emit ( 'reset' )
324340 }
325341
0 commit comments