import React from 'react'; import GuideLines from './guidelines'; import { linearTransform2D, Point2D, selection2DToRange2D } from '../../utilities'; import { SelectionRectProps, SelectionRectState } from './types'; /** * Provides a transparent selection rectangle which allows users to select a rectangular region of an SVG. Mouse events * are enabled while the user is holding down the control or shift key. Releasing the mouse triggers a callback with * information about the selected region; releasing the key during selection cancels the selection. */ class SelectionRect extends React.Component { private _onKeyDown: (e: KeyboardEvent) => void; private _onKeyUp: (e: KeyboardEvent) => void; constructor(props: SelectionRectProps) { super(props); this.state = { selecting: false, selection: [], transform: SelectionRect.transform(props) }; this._onKeyDown = this.onKeyDown.bind(this); this._onKeyUp = this.onKeyUp.bind(this); } private mouseLocation(e: React.MouseEvent): Point2D { const r = (e.target as SVGRectElement).getBoundingClientRect(); return { x: (e.clientX - r.left) * this.props.dimensions.width / r.width, y: (e.clientY - r.top) * this.props.dimensions.height / r.height }; } private static transform(props: SelectionRectProps) { return linearTransform2D({ x: { start: 0, end: props.dimensions.width }, y: { start: props.dimensions.height, end: 0 } }, props.domain); } static getDerivedStateFromProps(props: SelectionRectProps, state: SelectionRectState): SelectionRectState { return { ...state, transform: SelectionRect.transform(props) }; } componentDidMount() { document.addEventListener('keydown', this._onKeyDown); document.addEventListener('keyup', this._onKeyUp); } componentWillUnmount() { document.removeEventListener('keydown', this._onKeyDown, false); document.removeEventListener('keyup', this._onKeyUp, false); } private onKeyDown(e: KeyboardEvent) { if (e.key === 'Control' || e.key === 'Shift') this.setState({ selecting: true }); } private onKeyUp(e: KeyboardEvent) { if (e.key === 'Control' || e.key === 'Shift') this.setState({ selecting: false, selectionStart: null, selection: [] }); } private onMouseDown(e: React.MouseEvent) { if (!this.state.selecting) { return; } if (this.props.freeform) this.setState({ selection: [ this.mouseLocation(e) ] }); else this.setState({ selectionStart: this.mouseLocation(e) }); } private onMouseMove(e: React.MouseEvent) { this.props.onMouseMove && this.props.onMouseMove(e, this.mouseLocation(e), this.state.transform(this.mouseLocation(e))); if (this.props.freeform && this.state.selection.length > 0) this.setState({ selection: [ ...this.state.selection, this.mouseLocation(e) ] }); else this.setState({ mouseLocation: this.mouseLocation(e) }); } private onMouseOut() { this.setState({ selection: [], mouseLocation: null }); } private onMouseUp(e: React.MouseEvent) { if (this.props.freeform && this.state.selection) this.props.onFreeformSelectionEnd && this.props.onFreeformSelectionEnd( this.state.selection.map(this.state.transform).concat(this.state.transform(this.mouseLocation(e))) ); else if (this.state.selectionStart) this.props.onSelectionEnd && this.props.onSelectionEnd( selection2DToRange2D({ start: this.state.transform(this.state.selectionStart), end: this.state.transform(this.mouseLocation(e)) }) ); this.setState({ selecting: false, selection: [], selectionStart: null }); } private selectionPath() { if (!this.state.selection || this.state.selection.length === 0) return ""; return `M ${this.state.selection[0].x} ${this.state.selection[0].y} ` + this.state.selection.slice(1).map( (point: Point2D) => ( `L ${point.x} ${point.y} ` )).join(" "); } render() { return ( <> {this.state.selecting && ( this.props.freeform ? ( this.state.selection && ( ) ) : ( this.state.selectionStart && this.state.mouseLocation && ( <> {this.props.withGuideLines && ( )} ) ) )} {this.props.withGuideLines && this.state.mouseLocation && ( )} ); } } export default SelectionRect;