This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Repository navigation
File patch selection and context menu interaction #853
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e17cd25
Use a capturing event handler on document to process contextmenu events
smashwilson d99db74
Dispose any accumulated contextmenu handlers after each test
smashwilson 8aa1cac
It's delete() not remove()
smashwilson 86169ea
Dispose any context menu handlers on package deactivation
smashwilson af9b623
Use a ContextMenuInterceptor component within LineView
smashwilson f0983e6
Call .stopPropagation() before setState is invoked
smashwilson c8fd02c
Use setImmediate instead of requestAnimationFrame for the context men…
smashwilson 7f0f43b
:fire: unnecessary onContextMenu attribute
smashwilson b1c950c
Dispatch simulated mouse events to the line element within a LineView
smashwilson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| export default class ContextMenuInterceptor extends React.Component { | ||
| static propTypes = { | ||
| onWillShowContextMenu: PropTypes.func.isRequired, | ||
| children: PropTypes.element.isRequired, | ||
| } | ||
|
|
||
| static registration = new Map() | ||
|
|
||
| static handle(event) { | ||
| for (const [element, callback] of ContextMenuInterceptor.registration) { | ||
| if (element.contains(event.target)) { | ||
| callback(event); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static dispose() { | ||
| document.removeEventListener('contextmenu', contextMenuHandler, {capture: true}); | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| // Helpfully, addEventListener dedupes listeners for us. | ||
| document.addEventListener('contextmenu', contextMenuHandler, {capture: true}); | ||
| ContextMenuInterceptor.registration.set(this.element, this.props.onWillShowContextMenu); | ||
| } | ||
|
|
||
| render() { | ||
| return <div ref={e => { this.element = e; }}>{this.props.children}</div>; | ||
| } | ||
|
|
||
| componentWillUnmount() { | ||
| ContextMenuInterceptor.registration.delete(this.element); | ||
| } | ||
| } | ||
|
|
||
| function contextMenuHandler(event) { | ||
| ContextMenuInterceptor.handle(event); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import React from 'react'; | ||
| import {mount} from 'enzyme'; | ||
|
|
||
| import ContextMenuInterceptor from '../lib/context-menu-interceptor'; | ||
|
|
||
| class SampleComponent extends React.Component { | ||
| render() { | ||
| return ( | ||
| <div className="parent"> | ||
| <div className="child"> | ||
| This element has content. | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| describe('ContextMenuInterceptor', function() { | ||
| let rootElement, rootHandler, rootHandlerCalled; | ||
|
|
||
| beforeEach(function() { | ||
| rootHandlerCalled = false; | ||
| rootHandler = event => { | ||
| rootHandlerCalled = true; | ||
| event.preventDefault(); | ||
| return false; | ||
| }; | ||
| document.addEventListener('contextmenu', rootHandler); | ||
|
|
||
| rootElement = document.createElement('div'); | ||
| document.body.appendChild(rootElement); | ||
| }); | ||
|
|
||
| afterEach(function() { | ||
| document.removeEventListener('contextmenu', rootHandler); | ||
| ContextMenuInterceptor.dispose(); | ||
| rootElement.remove(); | ||
| }); | ||
|
|
||
| it('responds to native contextmenu events before they reach the document root', function() { | ||
| let interceptorHandlerCalled = false; | ||
| let rootHandlerCalledFirst = false; | ||
| const handler = () => { | ||
| interceptorHandlerCalled = true; | ||
| rootHandlerCalledFirst = rootHandlerCalled; | ||
| }; | ||
|
|
||
| const wrapper = mount( | ||
| <ContextMenuInterceptor onWillShowContextMenu={handler}> | ||
| <SampleComponent /> | ||
| </ContextMenuInterceptor>, | ||
| {attachTo: rootElement}, | ||
| ); | ||
|
|
||
| const targetDOMNode = wrapper.find('.child').getDOMNode(); | ||
| const event = new MouseEvent('contextmenu', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| targetDOMNode.dispatchEvent(event); | ||
|
|
||
| assert.isTrue(interceptorHandlerCalled); | ||
| assert.isFalse(rootHandlerCalledFirst); | ||
| assert.isTrue(rootHandlerCalled); | ||
| }); | ||
|
|
||
| it('can prevent event propagation', function() { | ||
| let interceptorHandlerCalled = false; | ||
| const handler = () => { | ||
| interceptorHandlerCalled = true; | ||
| event.stopPropagation(); | ||
| }; | ||
|
|
||
| const wrapper = mount( | ||
| <ContextMenuInterceptor onWillShowContextMenu={handler}> | ||
| <SampleComponent /> | ||
| </ContextMenuInterceptor>, | ||
| {attachTo: rootElement}, | ||
| ); | ||
|
|
||
| const targetDOMNode = wrapper.find('.child').getDOMNode(); | ||
| const event = new MouseEvent('contextmenu', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| targetDOMNode.dispatchEvent(event); | ||
|
|
||
| assert.isTrue(interceptorHandlerCalled); | ||
| assert.isFalse(rootHandlerCalled); | ||
| }); | ||
|
|
||
| it('ignores contextmenu events from other children', function() { | ||
| let interceptorHandlerCalled = false; | ||
| const handler = () => { | ||
| interceptorHandlerCalled = true; | ||
| }; | ||
|
|
||
| const wrapper = mount( | ||
| <div> | ||
| <ContextMenuInterceptor onWillShowContextMenu={handler}> | ||
| <SampleComponent /> | ||
| </ContextMenuInterceptor> | ||
| <div className="unrelated"> | ||
| <div className="otherNode"> | ||
| This is another div. | ||
| </div> | ||
| </div> | ||
| </div>, | ||
| {attachTo: rootElement}, | ||
| ); | ||
|
|
||
| const targetDOMNode = wrapper.find('.otherNode').getDOMNode(); | ||
| const event = new MouseEvent('contextmenu', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| targetDOMNode.dispatchEvent(event); | ||
|
|
||
| assert.isFalse(interceptorHandlerCalled); | ||
| assert.isTrue(rootHandlerCalled); | ||
| }); | ||
|
|
||
| it('cleans itself up on .dispose()', function() { | ||
| let interceptorHandlerCalled = false; | ||
| const handler = () => { | ||
| interceptorHandlerCalled = true; | ||
| }; | ||
|
|
||
| const wrapper = mount( | ||
| <ContextMenuInterceptor onWillShowContextMenu={handler}> | ||
| <SampleComponent /> | ||
| </ContextMenuInterceptor>, | ||
| {attachTo: rootElement}, | ||
| ); | ||
|
|
||
| ContextMenuInterceptor.dispose(); | ||
|
|
||
| const targetDOMNode = wrapper.find('.child').getDOMNode(); | ||
| const event = new MouseEvent('contextmenu', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| targetDOMNode.dispatchEvent(event); | ||
|
|
||
| assert.isFalse(interceptorHandlerCalled); | ||
| assert.isTrue(rootHandlerCalled); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure I understand why this is necessary, if registrations only happen on mount and are removed on unmount.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ContextMenuInterceptoruses a single native event listener attached to thedocumentthat's shared among all instances. Each instance registers and deregisters an element in a Map, but the event listener itself isn't removed until the staticContextMenuInterceptor.dispose()method is called. This bit ☝️ ensures that we don't leak listeners on package deactivation.... Although, this might be overkill. I could just register a new document listener for each instance and check
element.includes(event.target)? I was intending to minimize the number of listeners we register but maybe a single listener that does an O(n) operation on a Map isn't any better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gave the simplification here a shot and our test suite leaked a ton of event listeners, because Enzyme doesn't trigger
componentWillUnmount. I think I'm just going to leave it as -is with the static Map.