2021-03-02 16:46:48 -05:00
|
|
|
import { html, Component, createRef } from "../lib/index.js";
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
|
|
export default class Composer extends Component {
|
|
|
|
state = {
|
|
|
|
text: "",
|
|
|
|
};
|
|
|
|
textInput = createRef();
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2020-06-26 08:57:34 -04:00
|
|
|
this.handleInput = this.handleInput.bind(this);
|
2020-06-18 08:23:08 -04:00
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
2020-06-29 06:36:17 -04:00
|
|
|
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
|
2020-06-18 08:23:08 -04:00
|
|
|
this.handleWindowKeyDown = this.handleWindowKeyDown.bind(this);
|
|
|
|
}
|
|
|
|
|
2020-06-26 08:57:34 -04:00
|
|
|
handleInput(event) {
|
2020-06-18 08:23:08 -04:00
|
|
|
this.setState({ [event.target.name]: event.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.props.onSubmit(this.state.text);
|
|
|
|
this.setState({ text: "" });
|
|
|
|
}
|
|
|
|
|
2020-06-29 06:36:17 -04:00
|
|
|
handleInputKeyDown(event) {
|
|
|
|
if (!this.props.autocomplete || event.key !== "Tab") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var text = this.state.text;
|
|
|
|
var i;
|
|
|
|
for (i = text.length - 1; i >= 0; i--) {
|
|
|
|
if (text[i] === " ") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var prefix = text.slice(i + 1);
|
|
|
|
if (!prefix) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var repl = this.props.autocomplete(prefix);
|
|
|
|
if (!repl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
text = text.slice(0, i + 1) + repl;
|
|
|
|
this.setState({ text });
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
handleWindowKeyDown(event) {
|
2020-06-29 06:36:17 -04:00
|
|
|
if (document.activeElement === document.body && event.key === "/" && !this.state.text) {
|
2020-06-18 08:23:08 -04:00
|
|
|
event.preventDefault();
|
|
|
|
this.setState({ text: "/" }, () => {
|
|
|
|
this.focus();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener("keydown", this.handleWindowKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener("keydown", this.handleWindowKeyDown);
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
2020-07-13 03:45:30 -04:00
|
|
|
if (!this.textInput.current) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
document.activeElement.blur(); // in case we're read-only
|
|
|
|
this.textInput.current.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return html`
|
2020-06-26 08:57:34 -04:00
|
|
|
<form id="composer" class="${this.props.readOnly && !this.state.text ? "read-only" : ""}" onInput=${this.handleInput} onSubmit=${this.handleSubmit}>
|
2021-05-27 18:16:05 -04:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
name="text"
|
|
|
|
ref=${this.textInput}
|
|
|
|
value=${this.state.text}
|
|
|
|
autocomplete="off"
|
|
|
|
placeholder="Type a message"
|
2021-05-31 12:16:49 -04:00
|
|
|
enterkeyhint="send"
|
2021-05-27 18:16:05 -04:00
|
|
|
onKeyDown=${this.handleInputKeyDown}
|
|
|
|
/>
|
2020-06-18 08:23:08 -04:00
|
|
|
</form>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|