-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from bregenspan/add-context-menu
Add context menu to quickly filter bundles (resolves #241)
- Loading branch information
Showing
9 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
.container { | ||
font: var(--main-font); | ||
position: absolute; | ||
padding: 0; | ||
border-radius: 4px; | ||
background: #fff; | ||
border: 1px solid #aaa; | ||
list-style: none; | ||
opacity: 1; | ||
white-space: nowrap; | ||
visibility: visible; | ||
transition: opacity .2s ease, visibility .2s ease; | ||
} | ||
|
||
.hidden { | ||
opacity: 0; | ||
visibility: hidden; | ||
} |
This file contains 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,121 @@ | ||
/** @jsx h */ | ||
import {h} from 'preact'; | ||
import cls from 'classnames'; | ||
import ContextMenuItem from './ContextMenuItem'; | ||
import PureComponent from '../lib/PureComponent'; | ||
import {store} from '../store'; | ||
import {elementIsOutside} from '../utils'; | ||
|
||
import s from './ContextMenu.css'; | ||
|
||
export default class ContextMenu extends PureComponent { | ||
componentDidMount() { | ||
this.boundingRect = this.node.getBoundingClientRect(); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (this.props.visible && !prevProps.visible) { | ||
document.addEventListener('mousedown', this.handleDocumentMousedown, true); | ||
} else if (prevProps.visible && !this.props.visible) { | ||
document.removeEventListener('mousedown', this.handleDocumentMousedown, true); | ||
} | ||
} | ||
|
||
render() { | ||
const {visible} = this.props; | ||
const containerClassName = cls({ | ||
[s.container]: true, | ||
[s.hidden]: !visible | ||
}); | ||
const multipleChunksSelected = store.selectedChunks.length > 1; | ||
return ( | ||
<ul ref={this.saveNode} className={containerClassName} style={this.getStyle()}> | ||
<ContextMenuItem disabled={!multipleChunksSelected} | ||
onClick={this.handleClickHideChunk}> | ||
Hide chunk | ||
</ContextMenuItem> | ||
<ContextMenuItem disabled={!multipleChunksSelected} | ||
onClick={this.handleClickFilterToChunk}> | ||
Hide all other chunks | ||
</ContextMenuItem> | ||
<hr/> | ||
<ContextMenuItem disabled={store.allChunksSelected} | ||
onClick={this.handleClickShowAllChunks}> | ||
Show all chunks | ||
</ContextMenuItem> | ||
</ul> | ||
); | ||
} | ||
|
||
handleClickHideChunk = () => { | ||
const {chunk: selectedChunk} = this.props; | ||
if (selectedChunk && selectedChunk.label) { | ||
const filteredChunks = store.selectedChunks.filter(chunk => chunk.label !== selectedChunk.label); | ||
store.selectedChunks = filteredChunks; | ||
} | ||
this.hide(); | ||
} | ||
|
||
handleClickFilterToChunk = () => { | ||
const {chunk: selectedChunk} = this.props; | ||
if (selectedChunk && selectedChunk.label) { | ||
const filteredChunks = store.allChunks.filter(chunk => chunk.label === selectedChunk.label); | ||
store.selectedChunks = filteredChunks; | ||
} | ||
this.hide(); | ||
} | ||
|
||
handleClickShowAllChunks = () => { | ||
store.selectedChunks = store.allChunks; | ||
this.hide(); | ||
} | ||
|
||
/** | ||
* Handle document-wide `mousedown` events to detect clicks | ||
* outside the context menu. | ||
* @param {MouseEvent} e - DOM mouse event object | ||
* @returns {void} | ||
*/ | ||
handleDocumentMousedown = (e) => { | ||
const isSecondaryClick = e.ctrlKey || e.button === 2; | ||
if (!isSecondaryClick && elementIsOutside(e.target, this.node)) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
this.hide(); | ||
} | ||
} | ||
|
||
hide() { | ||
if (this.props.onHide) { | ||
this.props.onHide(); | ||
} | ||
} | ||
|
||
saveNode = node => (this.node = node); | ||
|
||
getStyle() { | ||
const {boundingRect} = this; | ||
|
||
// Upon the first render of this component, we don't yet know | ||
// its dimensions, so can't position it yet | ||
if (!boundingRect) return; | ||
|
||
const {coords} = this.props; | ||
|
||
const pos = { | ||
left: coords.x, | ||
top: coords.y | ||
}; | ||
|
||
if (pos.left + boundingRect.width > window.innerWidth) { | ||
// Shifting horizontally | ||
pos.left = window.innerWidth - boundingRect.width; | ||
} | ||
|
||
if (pos.top + boundingRect.height > window.innerHeight) { | ||
// Flipping vertically | ||
pos.top = coords.y - boundingRect.height; | ||
} | ||
return pos; | ||
} | ||
} |
This file contains 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,19 @@ | ||
.item { | ||
cursor: pointer; | ||
margin: 0; | ||
padding: 8px 14px; | ||
user-select: none; | ||
} | ||
|
||
.item:hover { | ||
background: #ffefd7; | ||
} | ||
|
||
.disabled { | ||
cursor: default; | ||
color: gray; | ||
} | ||
|
||
.item.disabled:hover { | ||
background: transparent; | ||
} |
This file contains 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,17 @@ | ||
/** @jsx h */ | ||
import {h} from 'preact'; | ||
import cls from 'classnames'; | ||
import s from './ContextMenuItem.css'; | ||
|
||
function noop() { | ||
return false; | ||
} | ||
|
||
export default function ContextMenuItem({children, disabled, onClick}) { | ||
const className = cls({ | ||
[s.item]: true, | ||
[s.disabled]: disabled | ||
}); | ||
const handler = disabled ? noop : onClick; | ||
return (<li className={className} onClick={handler}>{children}</li>); | ||
} |
This file contains 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 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 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 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