dequoter/frontend/src/controllers/commands_controller.js
Leon Mika 3cb5795ca5
All checks were successful
Build / build (push) Successful in 4m27s
Added some more commands and mades some quality of life improvements
2026-01-25 11:00:40 +11:00

81 lines
2.1 KiB
JavaScript

import {ListProcessors} from "../../wailsjs/go/main/App";
import { Controller } from "@hotwired/stimulus"
import { textProcessor } from "../services.js";
export class CommandsController extends Controller {
static targets = [
"commandInput",
"commandSelect",
];
async connect() {
this._lastCommand = null;
let processors = await ListProcessors();
processors.forEach((processor) => {
let option = document.createElement("option");
option.value = processor.name;
option.text = processor.label;
this.commandSelectTarget.appendChild(option);
});
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();
}
focusSelect(ev) {
this.commandSelectTarget.focus();
}
runCommand(ev) {
ev.preventDefault();
textProcessor.runTextCommand(this.commandSelectTarget.value);
this._lastCommand = this.commandSelectTarget.value;
this.element.close();
}
rerunLastCommand(ev) {
ev.preventDefault();
if (this._lastCommand === null) {
return;
}
textProcessor.runTextCommand(this._lastCommand);
}
_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;
}
}
}