From 8a20f744e1abad9f8c25a886ff9396c4ac69def0 Mon Sep 17 00:00:00 2001 From: CRIMX Date: Fri, 27 Apr 2018 00:31:31 +0800 Subject: [PATCH] feat(content): add panel closing --- .../components/DictPanelPortal/index.tsx | 22 ++++++++++++++----- src/content/components/MenuBar/index.tsx | 8 ++++++- src/content/containers/DictPanelContainer.tsx | 3 ++- src/content/redux/modules/selection.ts | 18 +++++++++++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/content/components/DictPanelPortal/index.tsx b/src/content/components/DictPanelPortal/index.tsx index aa222ddf5..300af63d8 100644 --- a/src/content/components/DictPanelPortal/index.tsx +++ b/src/content/components/DictPanelPortal/index.tsx @@ -175,21 +175,23 @@ export default class DictPanelPortal extends React.Component) => { // prevent mousedown dragging e.preventDefault() + e.stopPropagation() // e is from iframe, so there is offset this.lastMouseX = e.clientX + this.state.x this.lastMouseY = e.clientY + this.state.y this.setState({ isDragging: true }) - window.addEventListener('mousemove', this.handleWindowMouseMove) - window.addEventListener('mouseup', this.handleDragEnd) + window.addEventListener('mousemove', this.handleWindowMouseMove, { capture: true }) + window.addEventListener('mouseup', this.handleDragEnd, { capture: true }) } handleDragEnd = () => { this.setState({ isDragging: false }) - window.removeEventListener('mousemove', this.handleWindowMouseMove) - window.removeEventListener('mouseup', this.handleDragEnd) + window.removeEventListener('mousemove', this.handleWindowMouseMove, { capture: true }) + window.removeEventListener('mouseup', this.handleDragEnd, { capture: true }) } handleWindowMouseMove = (e: MouseEvent) => { + e.stopPropagation() const { x, y } = this.state this.setState({ x: x + e.clientX - this.lastMouseX, @@ -200,6 +202,7 @@ export default class DictPanelPortal extends React.Component) => { + e.stopPropagation() const { x, y } = this.state this.setState({ x: x + x + e.clientX - this.lastMouseX, @@ -209,6 +212,12 @@ export default class DictPanelPortal extends React.Component) => { + if (key === 'Escape') { + this.props.closePanel() + } + } + render () { const { selection, config, isPinned, isMouseOnBowl } = this.props @@ -233,8 +242,9 @@ export default class DictPanelPortal extends React.Component {shouldShow ? { + const { closePanel, isPinned, pinPanel } = this.props + closePanel() + if (isPinned) { pinPanel() } + } + handleSearchBoxInput = (e: KeyboardEvent) => { this.text = e.currentTarget.value } @@ -166,7 +172,7 @@ export class MenuBar extends React.PureComponent {t('tipClosePanel')} diff --git a/src/content/containers/DictPanelContainer.tsx b/src/content/containers/DictPanelContainer.tsx index d0e7ce7ae..4061d01a2 100644 --- a/src/content/containers/DictPanelContainer.tsx +++ b/src/content/containers/DictPanelContainer.tsx @@ -2,6 +2,7 @@ import { connect } from 'react-redux' import DictPanelPortal, { DictPanelPortalProps, DictPanelPortalDispatchers } from '../components/DictPanelPortal' import { StoreState } from '../redux/modules' import { searchText } from '../redux/modules/dictionaries' +import { sendEmptySelection } from '../redux/modules/selection' import { addToNotebook, removeFromNotebook, pinPanel, showPanel } from '../redux/modules/widget' export const mapStateToProps = ({ @@ -29,7 +30,7 @@ export const mapDispatchToProps: { [k in keyof DictPanelPortalDispatchers]: Func pinPanel, shareImg: () => {/** @todo */}, - closePanel: () => {/** @todo */}, + closePanel: sendEmptySelection, } export default connect( diff --git a/src/content/redux/modules/selection.ts b/src/content/redux/modules/selection.ts index 40f0331e8..ec983c403 100644 --- a/src/content/redux/modules/selection.ts +++ b/src/content/redux/modules/selection.ts @@ -41,7 +41,7 @@ export default function reducer (state = initState, action): SelectionState { type Action = { type: Actions, payload?: any } /** When new selection is made */ -export function newSelection (selection): Action { +export function newSelection (selection: MsgSelection): Action { return { type: Actions.NEW_SELECTION, payload: selection } } @@ -55,6 +55,20 @@ type Dispatcher = ( export function listenSelection (): Dispatcher { return dispatch => { - message.self.addListener(MsgType.Selection, message => dispatch(newSelection(message))) + message.self.addListener( + MsgType.Selection, + message => dispatch(newSelection(message)), + ) } } + +export function sendEmptySelection (): Dispatcher { + return dispatch => dispatch(newSelection({ + type: MsgType.Selection, + selectionInfo: getDefaultSelectionInfo(), + mouseX: 0, + mouseY: 0, + dbClick: false, + ctrlKey: false, + })) +}