2025-09-06 00:26:50 +00:00
|
|
|
import { Controller } from "@hotwired/stimulus"
|
|
|
|
|
2025-09-06 01:26:54 +00:00
|
|
|
import { textProcessor } from "../services.js";
|
|
|
|
|
|
|
|
export class CommandsController extends Controller {
|
2025-09-06 00:26:50 +00:00
|
|
|
static targets = [
|
|
|
|
"commandInput",
|
|
|
|
"commandSelect",
|
|
|
|
];
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
this._options = Array.from(this.commandSelectTarget.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
showCommands(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.element.showModal();
|
|
|
|
|
|
|
|
this.commandInputTarget.setSelectionRange(0, this.commandInputTarget.value.length, "forward");
|
|
|
|
this.commandInputTarget.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyup(ev) {
|
|
|
|
this._filterOptions(this.commandInputTarget.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
dismissDialog(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.element.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
runCommand(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
|
2025-09-06 01:26:54 +00:00
|
|
|
textProcessor.runTextCommand(this.commandSelectTarget.value);
|
2025-09-06 00:26:50 +00:00
|
|
|
this.element.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
_filterOptions(filterText) {
|
|
|
|
let inputText = filterText.toLowerCase();
|
|
|
|
|
|
|
|
let visibleOptions = [];
|
|
|
|
for (let opt of this._options) {
|
|
|
|
if ((inputText === "") || (opt.innerText.toLowerCase().includes(inputText))) {
|
|
|
|
visibleOptions.push(opt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.commandSelectTarget.replaceChildren(...visibleOptions);
|
|
|
|
if (visibleOptions.length > 0) {
|
|
|
|
this.commandSelectTarget.selectedIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
2025-09-06 01:26:54 +00:00
|
|
|
}
|