import { html, Component, createRef } from "../lib/index.js";
export default class Composer extends Component {
state = {
text: "",
};
textInput = createRef();
lastAutocomplete = null;
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 carretPos = input.selectionStart;
let text = this.state.text;
let autocomplete;
if (this.lastAutocomplete && this.lastAutocomplete.text === text && this.lastAutocomplete.carretPos === carretPos) {
autocomplete = this.lastAutocomplete;
} else {
this.lastAutocomplete = null;
let wordStart;
for (wordStart = carretPos - 1; wordStart >= 0; wordStart--) {
if (text[wordStart] === " ") {
break;
}
}
wordStart++;
let wordEnd;
for (wordEnd = carretPos; wordEnd < text.length; wordEnd++) {
if (text[wordEnd] === " ") {
break;
}
}
let word = text.slice(wordStart, wordEnd);
if (!word) {
return;
}
let replacements = this.props.autocomplete(word);
if (replacements.length === 0) {
return;
}
autocomplete = {
text,
carretPos: input.selectionStart,
prefix: text.slice(0, wordStart),
suffix: text.slice(wordEnd),
replacements,
replIndex: -1,
};
}
let n = autocomplete.replacements.length;
if (event.shiftKey) {
autocomplete.replIndex--;
} else {
autocomplete.replIndex++;
}
autocomplete.replIndex = (autocomplete.replIndex + n) % n;
let repl = autocomplete.replacements[autocomplete.replIndex];
if (!autocomplete.prefix && !autocomplete.suffix) {
if (repl.startsWith("/")) {
repl += " ";
} else {
repl += ": ";
}
}
autocomplete.text = autocomplete.prefix + repl + autocomplete.suffix;
autocomplete.carretPos = autocomplete.prefix.length + repl.length;
input.value = autocomplete.text;
input.selectionStart = autocomplete.carretPos;
input.selectionEnd = input.selectionStart;
this.lastAutocomplete = autocomplete;
this.setState({ text: autocomplete.text });
}
handleWindowKeyDown(event) {
// If an or