|
| 1 | +import { Command } from "./Command"; |
| 2 | +import { getCommandFromInput, longestCommonPrefix } from "./helpers"; |
| 3 | +import { ChangeDirectory } from "./commands/ChangeDirectory"; |
| 4 | +import { Clear } from "./commands/Clear"; |
| 5 | +import { Help } from "./commands/Help"; |
| 6 | +import { Kitties } from "./commands/Kitties"; |
| 7 | +import { List } from "./commands/List"; |
| 8 | + |
| 9 | +export class Console { |
| 10 | + private inputElement = document.getElementById("prompt-input") as HTMLSpanElement; |
| 11 | + private promptBlur = document.getElementById("prompt-blur") as HTMLSpanElement; |
| 12 | + private consoleElement = document.getElementById("console") as HTMLDivElement; |
| 13 | + private nav = document.getElementsByTagName("nav")[0]; |
| 14 | + |
| 15 | + static commands: Command[] = [ |
| 16 | + new ChangeDirectory(), |
| 17 | + new Clear(), |
| 18 | + new Help(), |
| 19 | + new Kitties(), |
| 20 | + new List(), |
| 21 | + ]; |
| 22 | + |
| 23 | + public initialise(): void { |
| 24 | + // Focus prompt input on page load for browsers that don't support autofocus on contenteditable elements. |
| 25 | + this.inputElement.focus(); |
| 26 | + |
| 27 | + this.mirrorInputPromptToBlurredPrompt(); |
| 28 | + this.moveCaretToEndOnFocus(); |
| 29 | + this.listenForKeyboardInput(); |
| 30 | + this.focusPromptOnClick(); |
| 31 | + } |
| 32 | + |
| 33 | + private mirrorInputPromptToBlurredPrompt(): void { |
| 34 | + this.inputElement.addEventListener("input", () => { |
| 35 | + this.setInput(this.inputElement.textContent.replace(/\s/g, "\xA0")); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + private moveCaretToEndOnFocus(): void { |
| 40 | + this.inputElement.addEventListener("focusin", () => { |
| 41 | + this.moveCaretToEnd(); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + private listenForKeyboardInput(): void { |
| 46 | + /** |
| 47 | + * Handle enter key press to clear the prompt input. |
| 48 | + */ |
| 49 | + document.addEventListener("keydown", (event: KeyboardEvent) => { |
| 50 | + const input = this.inputElement.textContent.replace(/\xA0/g, " ").trim(); |
| 51 | + |
| 52 | + switch (event.key) { |
| 53 | + case "ArrowLeft": |
| 54 | + case "ArrowRight": |
| 55 | + case "ArrowUp": |
| 56 | + case "ArrowDown": |
| 57 | + event.preventDefault(); |
| 58 | + break; |
| 59 | + |
| 60 | + // Clear prompt input |
| 61 | + case "Enter": |
| 62 | + event.preventDefault(); |
| 63 | + this.onEnter(input); |
| 64 | + break; |
| 65 | + |
| 66 | + case "Tab": |
| 67 | + event.preventDefault(); |
| 68 | + this.onTab(input); |
| 69 | + break; |
| 70 | + |
| 71 | + // Remove focus from prompt input |
| 72 | + case "Escape": |
| 73 | + this.inputElement.blur(); |
| 74 | + break; |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private focusPromptOnClick(): void { |
| 80 | + /** |
| 81 | + * Focus prompt input when clicking on the header. |
| 82 | + */ |
| 83 | + this.nav.addEventListener("click", (event: MouseEvent) => { |
| 84 | + // Prevent focusing prompt input when clicking on a link. |
| 85 | + if (event.target instanceof HTMLAnchorElement) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + this.inputElement.focus(); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + private onEnter(input: string) { |
| 94 | + const { command, args } = getCommandFromInput(input); |
| 95 | + this.setInput(); |
| 96 | + this.clearOutput(); |
| 97 | + |
| 98 | + if (command) { |
| 99 | + command.execute(this, args); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + this.print("Command not found. Type `help` for a list of available commands."); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + private onTab(input: string) { |
| 108 | + if (input.length === 0) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + const matchingCommands = Console.commands.filter((command: Command) => |
| 113 | + command.name.startsWith(input), |
| 114 | + ); |
| 115 | + |
| 116 | + switch (matchingCommands.length) { |
| 117 | + case 0: |
| 118 | + return; |
| 119 | + |
| 120 | + case 1: |
| 121 | + const matchingCommand = matchingCommands[0]; |
| 122 | + if (input.length < matchingCommand.name.length) { |
| 123 | + this.inputElement.textContent = matchingCommand.name; |
| 124 | + this.promptBlur.textContent = matchingCommand.name; |
| 125 | + this.moveCaretToEnd(); |
| 126 | + } |
| 127 | + return; |
| 128 | + |
| 129 | + default: |
| 130 | + const matchingPrefix = longestCommonPrefix( |
| 131 | + matchingCommands.map((command: Command) => command.name), |
| 132 | + ); |
| 133 | + this.setInput(""); |
| 134 | + this.inputElement.textContent = matchingPrefix; |
| 135 | + this.promptBlur.textContent = matchingPrefix; |
| 136 | + this.moveCaretToEnd(); |
| 137 | + } |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + private moveCaretToEnd() { |
| 142 | + const range = document.createRange(); |
| 143 | + const selection = window.getSelection(); |
| 144 | + |
| 145 | + // Move caret to end of prompt input. |
| 146 | + range?.setStart(this.inputElement, this.inputElement.childNodes.length); |
| 147 | + range?.collapse(false); |
| 148 | + selection?.removeAllRanges(); |
| 149 | + selection?.addRange(range); |
| 150 | + } |
| 151 | + |
| 152 | + public print(...lines: string[]) { |
| 153 | + lines.forEach((line: string) => { |
| 154 | + const outputElement = document.createElement("pre"); |
| 155 | + outputElement.textContent = line; |
| 156 | + this.consoleElement.appendChild(outputElement); |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + public setInput(newInput: string = "") { |
| 161 | + this.inputElement.textContent = newInput; |
| 162 | + this.promptBlur.textContent = newInput; |
| 163 | + this.moveCaretToEnd(); |
| 164 | + } |
| 165 | + |
| 166 | + public clearOutput() { |
| 167 | + while (this.consoleElement?.firstChild) { |
| 168 | + this.consoleElement.removeChild(this.consoleElement.firstChild); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments