JS: Move JavaScript to commandbar subdirectory
This commit is contained in:
parent
ccd2a25920
commit
563693fb79
5 changed files with 0 additions and 0 deletions
57
assets/commandbar/js/commandExecutor.js
Normal file
57
assets/commandbar/js/commandExecutor.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
import { SetOptionHandler } from "./setHandler.js";
|
||||
|
||||
export class CommandExecutor {
|
||||
commandBar;
|
||||
#history = [];
|
||||
#handlers = new Map();
|
||||
|
||||
constructor(commandBar) {
|
||||
this.commandBar = commandBar;
|
||||
console.log(this.#handlers);
|
||||
this.addHandler(new SetOptionHandler());
|
||||
console.log(this.#handlers);
|
||||
}
|
||||
|
||||
addHandler(handler) {
|
||||
this.#handlers.set(handler.name, handler);
|
||||
}
|
||||
|
||||
executeCommand(command) {
|
||||
this.appendCommandToHistory(command);
|
||||
|
||||
const handler = this.#handlerForCommand(command);
|
||||
if (!handler) {
|
||||
throw new Error(`No handler for '${command.verb.valueOf()}' command`);
|
||||
}
|
||||
|
||||
let result;
|
||||
try {
|
||||
result = handler.execute(command, this.commandBar);
|
||||
} catch (e) {
|
||||
result = null;
|
||||
}
|
||||
|
||||
if (result !== undefined && !result) {
|
||||
console.error(`Command failed: ${command}`);
|
||||
}
|
||||
}
|
||||
|
||||
#handlerForCommand(command) {
|
||||
const verb = command.verb.valueOf();
|
||||
return this.#handlers.get(verb);
|
||||
}
|
||||
|
||||
// MARK: History
|
||||
|
||||
get historyLength() {
|
||||
return this.#history.length;
|
||||
}
|
||||
|
||||
appendCommandToHistory(command) {
|
||||
this.#history.push(command);
|
||||
}
|
||||
|
||||
commandAtHistoryIndex(index) {
|
||||
return this.#history[index];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue