2016-07-06 15:18:01 -03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-06-30 18:12:23 -03:00
|
|
|
import fuzzy from 'fuzzy';
|
2016-07-05 13:48:52 -03:00
|
|
|
import _ from 'lodash';
|
2016-06-30 18:12:23 -03:00
|
|
|
import { connect } from 'react-redux';
|
2016-07-06 15:18:01 -03:00
|
|
|
import styles from './FindBar.css';
|
2016-06-30 18:12:23 -03:00
|
|
|
|
2016-07-06 15:18:01 -03:00
|
|
|
class FindBar extends Component {
|
2016-07-05 13:48:52 -03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-07-06 15:18:01 -03:00
|
|
|
this._compiledCommands = {};
|
2016-06-30 18:12:23 -03:00
|
|
|
this.state = {
|
2016-07-06 15:18:01 -03:00
|
|
|
value: this.props.initialValue,
|
|
|
|
isOpen: false,
|
|
|
|
highlightedIndex: 0,
|
2016-06-30 18:12:23 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
this.compileCommand = this.compileCommand.bind(this);
|
|
|
|
this.matchCommand = this.matchCommand.bind(this);
|
2016-07-06 15:18:01 -03:00
|
|
|
this.handleChange = this.handleChange.bind(this);
|
|
|
|
this.handleKeyDown = this.handleKeyDown.bind(this);
|
|
|
|
this.handleInputBlur = this.handleInputBlur.bind(this);
|
|
|
|
this.handleInputFocus = this.handleInputFocus.bind(this);
|
|
|
|
this.handleInputClick = this.handleInputClick.bind(this);
|
|
|
|
this.getSuggestions = _.memoize(this.getSuggestions);
|
|
|
|
this.highlightCommandFromMouse = this.highlightCommandFromMouse.bind(this);
|
|
|
|
this.selectCommandFromMouse = this.selectCommandFromMouse.bind(this);
|
|
|
|
this.setIgnoreBlur = this.setIgnoreBlur.bind(this);
|
|
|
|
this.renderMenu = this.renderMenu.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this._ignoreBlur = false;
|
2016-06-30 18:12:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2016-07-06 15:18:01 -03:00
|
|
|
this._compiledCommands = this.props.commands.map(this.compileCommand);
|
2016-06-30 18:12:23 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
_escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
|
|
|
_camelCaseToSpace(string) {
|
2016-07-06 15:18:01 -03:00
|
|
|
const result = string.replace(/([A-Z])/g, ' $1');
|
2016-06-30 18:12:23 -03:00
|
|
|
return result.charAt(0).toUpperCase() + result.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
compileCommand(command) {
|
|
|
|
let regexp = '';
|
|
|
|
let param;
|
|
|
|
|
|
|
|
const matcher = /\(:([a-zA-Z_$][a-zA-Z0-9_$]*)(?:(?: as )(.*))?\)/g;
|
|
|
|
const match = matcher.exec(command.pattern);
|
|
|
|
const token = command.pattern.slice(0, match.index);
|
|
|
|
regexp += this._escapeRegExp(command.pattern.slice(0, match.index));
|
|
|
|
|
|
|
|
if (match[1]) {
|
|
|
|
regexp += '(.*)';
|
|
|
|
param = { name:match[1], display:match[2] || this._camelCaseToSpace(match[1]) };
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.assign({}, command, {
|
|
|
|
regexp,
|
|
|
|
token,
|
|
|
|
param
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
matchCommand(string) {
|
|
|
|
let match;
|
|
|
|
const command = this.compiledCommands.find(command => {
|
|
|
|
match = string.match(RegExp(`^${command.regexp}`, 'i'));
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (command === null) return null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
command,
|
|
|
|
param: match[1] && match[1].trim()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-06 15:18:01 -03:00
|
|
|
handleChange(event) {
|
|
|
|
this.setState({
|
|
|
|
value: event.target.value,
|
|
|
|
}, () => {
|
|
|
|
this.props.onChange(event, this.state.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown(event) {
|
|
|
|
switch (event.key) {
|
|
|
|
case 'ArrowDown':
|
|
|
|
event.preventDefault();
|
|
|
|
let { highlightedIndex } = this.state;
|
|
|
|
let index = (
|
|
|
|
highlightedIndex === this.getSuggestions(this.state.value, this._compiledCommands).length - 1 ||
|
|
|
|
this.state.isOpen === false
|
|
|
|
) ? 0 : highlightedIndex + 1;
|
|
|
|
this.setState({
|
|
|
|
highlightedIndex: index,
|
|
|
|
isOpen: true,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'ArrowUp':
|
|
|
|
event.preventDefault();
|
|
|
|
let { highlightedIndex } = this.state;
|
|
|
|
let index = (
|
|
|
|
highlightedIndex === 0
|
|
|
|
) ? this.getSuggestions(this.state.value, this._compiledCommands).length - 1 : highlightedIndex - 1;
|
|
|
|
this.setState({
|
|
|
|
highlightedIndex: index,
|
|
|
|
isOpen: true,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'Enter':
|
|
|
|
if (this.state.isOpen) {
|
|
|
|
const command = this.getSuggestions(this.state.value, this._compiledCommands)[this.state.highlightedIndex];
|
|
|
|
this.setState({
|
|
|
|
value: command.token,
|
|
|
|
isOpen: false,
|
|
|
|
highlightedIndex: 0
|
|
|
|
}, () => {
|
|
|
|
this.refs.input.focus();
|
|
|
|
this.refs.input.setSelectionRange(
|
|
|
|
this.state.value.length,
|
|
|
|
this.state.value.length
|
|
|
|
);
|
|
|
|
this.props.onSelect(this.state.value, command);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Escape':
|
|
|
|
this.setState({
|
|
|
|
highlightedIndex: 0,
|
|
|
|
isOpen: false
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.setState({
|
|
|
|
highlightedIndex: 0,
|
|
|
|
isOpen: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputBlur() {
|
|
|
|
if (this._ignoreBlur) return;
|
|
|
|
this.setState({
|
|
|
|
isOpen: false,
|
|
|
|
highlightedIndex: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputFocus() {
|
|
|
|
if (this._ignoreBlur) return;
|
|
|
|
this.setState({ isOpen: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleInputClick() {
|
|
|
|
if (this.state.isOpen === false)
|
|
|
|
this.setState({ isOpen: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
getSuggestions(value, commands) {
|
|
|
|
const results = fuzzy.filter(value, commands, {
|
2016-06-30 18:12:23 -03:00
|
|
|
//pre: '<strong>',
|
|
|
|
//post: '</strong>',
|
2016-07-06 15:18:01 -03:00
|
|
|
extract: el => el.token
|
|
|
|
});
|
|
|
|
return results.map(result => result.original);
|
|
|
|
}
|
|
|
|
|
2016-07-05 13:48:52 -03:00
|
|
|
|
2016-07-06 15:18:01 -03:00
|
|
|
highlightCommandFromMouse(index) {
|
|
|
|
this.setState({ highlightedIndex: index });
|
2016-06-30 18:12:23 -03:00
|
|
|
}
|
|
|
|
|
2016-07-06 15:18:01 -03:00
|
|
|
selectCommandFromMouse(command) {
|
2016-06-30 18:12:23 -03:00
|
|
|
this.setState({
|
2016-07-06 15:18:01 -03:00
|
|
|
value: command.token,
|
|
|
|
isOpen: false,
|
|
|
|
highlightedIndex: 0
|
|
|
|
}, () => {
|
|
|
|
this.props.onSelect(this.state.value, command);
|
|
|
|
this.refs.input.focus();
|
|
|
|
this.setIgnoreBlur(false);
|
2016-06-30 18:12:23 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-06 15:18:01 -03:00
|
|
|
setIgnoreBlur(ignore) {
|
|
|
|
this._ignoreBlur = ignore;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderMenu() {
|
|
|
|
const commands = this.getSuggestions(this.state.value, this._compiledCommands).map((command, index) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={this.state.highlightedIndex === index ? styles.highlightedCommand : styles.command}
|
|
|
|
key={command.token.trim().replace(/[^a-z0-9]+/gi, '-')}
|
|
|
|
onMouseDown={() => this.setIgnoreBlur(true)}
|
|
|
|
onMouseEnter={() => this.highlightCommandFromMouse(index)}
|
|
|
|
onClick={() => this.selectCommandFromMouse(command)}
|
|
|
|
ref={`command-${index}`}
|
|
|
|
>{command.token}</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return <div className={styles.menu} children={commands} ref='menu'/>;
|
|
|
|
}
|
|
|
|
|
2016-06-30 18:12:23 -03:00
|
|
|
render() {
|
|
|
|
return (
|
2016-07-06 15:18:01 -03:00
|
|
|
<div className={styles.root}>
|
|
|
|
<div className={styles.inputArea}>
|
|
|
|
<input
|
|
|
|
ref="input"
|
|
|
|
className={styles.input}
|
|
|
|
onFocus={this.handleInputFocus}
|
|
|
|
onBlur={this.handleInputBlur}
|
|
|
|
onChange={(event) => this.handleChange(event)}
|
|
|
|
onKeyDown={(event) => this.handleKeyDown(event)}
|
|
|
|
onClick={this.handleInputClick}
|
|
|
|
value={this.state.value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{this.state.isOpen && this.renderMenu()}
|
|
|
|
</div>
|
2016-06-30 18:12:23 -03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FindBar.propTypes = {
|
2016-07-06 15:18:01 -03:00
|
|
|
commands: PropTypes.array,
|
|
|
|
initialValue: PropTypes.any,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onSelect: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
FindBar.defaultProps = {
|
|
|
|
initialValue: '',
|
|
|
|
onChange() {},
|
|
|
|
onSelect(value, command) {}
|
2016-06-30 18:12:23 -03:00
|
|
|
};
|
|
|
|
|
2016-07-06 15:18:01 -03:00
|
|
|
module.exports = FindBar;
|