import { html, Component, createRef } from "../lib/index.js";
export default class Composer extends Component {
state = {
text: "",
};
textInput = createRef();
constructor(props) {
super(props);
this.handleInput = this.handleInput.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInputKeyDown = this.handleInputKeyDown.bind(this);
this.handleWindowKeyDown = this.handleWindowKeyDown.bind(this);
}
handleInput(event) {
this.setState({ [event.target.name]: event.target.value });
if (this.props.readOnly && event.target.name === "text" && !event.target.value) {
event.target.blur();
}
}
handleSubmit(event) {
event.preventDefault();
this.props.onSubmit(this.state.text);
this.setState({ text: "" });
}
handleInputKeyDown(event) {
let input = event.target;
if (!this.props.autocomplete || event.key !== "Tab") {
return;
}
if (input.selectionStart !== input.selectionEnd) {
return;
}
event.preventDefault();
let carretIndex = input.selectionStart;
let text = this.state.text;
let wordStart;
for (wordStart = carretIndex - 1; wordStart >= 0; wordStart--) {
if (text[wordStart] === " ") {
break;
}
}
wordStart++;
let wordEnd;
for (wordEnd = carretIndex; wordEnd < text.length; wordEnd++) {
if (text[wordEnd] === " ") {
break;
}
}
let word = text.slice(wordStart, wordEnd);
if (!word) {
return;
}
let repl = this.props.autocomplete(word);
if (!repl) {
return;
}
text = text.slice(0, wordStart) + repl + text.slice(wordEnd);
input.value = text;
input.selectionStart = wordStart + repl.length;
input.selectionEnd = input.selectionStart;
this.setState({ text });
}
handleWindowKeyDown(event) {
// If an or