|
| 1 | +/** |
| 2 | + * @class HtmlTerminal |
| 3 | + * |
| 4 | + * This class is a very basic implementation of a "terminal" in the browser. |
| 5 | + * It provides simple functions like "write" and an "input" Callback. |
| 6 | + * |
| 7 | + * @license AGPL-2.0 |
| 8 | + * @author Alexaner Wunschik <https://github.com/mojoaxel> |
| 9 | + */ |
| 10 | +class HtmlTerminal { |
| 11 | + |
| 12 | + /** |
| 13 | + * Input callback. |
| 14 | + * If the prompt is activated by calling the input function |
| 15 | + * a callback is defined. If this member is not set this means |
| 16 | + * the prompt is not active. |
| 17 | + * |
| 18 | + * @private |
| 19 | + * @type {function} |
| 20 | + */ |
| 21 | + #inputCallback = undefined; |
| 22 | + |
| 23 | + /** |
| 24 | + * A html element to show a "prompt". |
| 25 | + * |
| 26 | + * @private |
| 27 | + * @type {HTMLElement} |
| 28 | + */ |
| 29 | + #$prompt = undefined; |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructor |
| 33 | + * Creates a basic terminal simulation on the provided HTMLElement. |
| 34 | + * |
| 35 | + * @param {HTMLElement} $output - a dom element |
| 36 | + */ |
| 37 | + constructor($output) { |
| 38 | + // Store the output DOM element in a local variable. |
| 39 | + this.$output = $output; |
| 40 | + |
| 41 | + // Clear terminal. |
| 42 | + this.clear(); |
| 43 | + |
| 44 | + // Add the call "terminal" to the $output element. |
| 45 | + this.$output.classList.add('terminal'); |
| 46 | + |
| 47 | + // Create a prompt element. |
| 48 | + // This element gets added if input is needed |
| 49 | + this.#$prompt = document.createElement("span"); |
| 50 | + this.#$prompt.setAttribute("id", "prompt"); |
| 51 | + this.#$prompt.innerText = ""; |
| 52 | + |
| 53 | + //TODO: this handler shouls be only on the propt element and only active if cursor is visible |
| 54 | + document.addEventListener("keyup", this.#handleKey.bind(this)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Creates a new HTMLElement with the given text content. |
| 59 | + * This element than gets added to the $output as a new "line". |
| 60 | + * |
| 61 | + * @private |
| 62 | + * @memberof MinimalTerminal |
| 63 | + * @param {String} text - text that should be displayed in the new "line". |
| 64 | + * @returns {HTMLElement} return a new DOM Element <pre class="line"></pre> |
| 65 | + */ |
| 66 | + #newLine(text) { |
| 67 | + const $lineNode = document.createElement("pre"); |
| 68 | + $lineNode.classList.add("line"); |
| 69 | + $lineNode.innerText = text; |
| 70 | + return $lineNode; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * TODO |
| 75 | + * |
| 76 | + * @private |
| 77 | + * @param {*} e |
| 78 | + */ |
| 79 | + #handleKey(e) { |
| 80 | + // if no input-callback is defined |
| 81 | + if (!this.#inputCallback) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (e.keyCode === 13 /* ENTER */) { |
| 86 | + // create a new line with the text input and remove the prompt |
| 87 | + const text = this.#$prompt.innerText; |
| 88 | + this.write(text + "\n"); |
| 89 | + this.#$prompt.innerText = ""; |
| 90 | + this.#$prompt.remove(); |
| 91 | + |
| 92 | + // return the inputed text |
| 93 | + this.#inputCallback(text); |
| 94 | + |
| 95 | + // remove the callback and the key handler |
| 96 | + this.#inputCallback = undefined; |
| 97 | + } else if (e.keyCode === 8 /* BACKSPACE */) { |
| 98 | + this.#$prompt.innerText = this.#$prompt.innerText.slice(0, -1); |
| 99 | + } else if ( |
| 100 | + e.keyCode == 16 // "Shift" |
| 101 | + || e.keyCode == 17 // "Control" |
| 102 | + || e.keyCode == 20 // "CapsLock" |
| 103 | + || !e.key.match(/^[a-z0-9!"§#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]$/i) |
| 104 | + ) { |
| 105 | + // ignore non-visible characters |
| 106 | + return e; |
| 107 | + } else { |
| 108 | + this.#$prompt.innerHtml = ''; |
| 109 | + const key = e.shiftKey ? e.key.toUpperCase() : e.key; |
| 110 | + this.#$prompt.innerText = this.#$prompt.innerText + key; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Clear the terminal. |
| 116 | + * Remove all lines. |
| 117 | + * |
| 118 | + * @public |
| 119 | + */ |
| 120 | + clear() { |
| 121 | + this.$output.innerText = ""; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * TODO: |
| 126 | + * |
| 127 | + * @public |
| 128 | + * @param {*} htmlContent |
| 129 | + */ |
| 130 | + inserHtml(htmlContent) { |
| 131 | + const $htmlNode = document.createElement("div"); |
| 132 | + $htmlNode.innerHTML = htmlContent; |
| 133 | + this.$output.appendChild($htmlNode); |
| 134 | + document.body.scrollTo(0, document.body.scrollHeight); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Write a text to the terminal. |
| 139 | + * By default there is no linebreak at the end of a new line |
| 140 | + * except the line ensd with a "\n". |
| 141 | + * If the given text has multible linebreaks, multibe lines are inserted. |
| 142 | + * |
| 143 | + * @public |
| 144 | + * @param {string} text |
| 145 | + */ |
| 146 | + write(text) { |
| 147 | + if (!text || text.length <= 0) { |
| 148 | + // empty line |
| 149 | + this.$output.appendChild(document.createElement("br")); |
| 150 | + } else if (text.endsWith("\n")) { |
| 151 | + // single line with linebrank |
| 152 | + const $lineNode = this.#newLine(text); |
| 153 | + this.$output.appendChild(this.#newLine(text)); |
| 154 | + this.$output.appendChild(document.createElement("br")); |
| 155 | + } else if (text.includes("\n")) { |
| 156 | + // multible lines |
| 157 | + const lines = text.split("\n"); |
| 158 | + lines.forEach((line) => { |
| 159 | + this.write(line); |
| 160 | + }); |
| 161 | + } else { |
| 162 | + // single line |
| 163 | + this.$output.appendChild(this.#newLine(text)); |
| 164 | + } |
| 165 | + |
| 166 | + // scroll to the buttom of the page |
| 167 | + document.body.scrollTo(0, document.body.scrollHeight); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Like "write" but with a newline at the end. |
| 172 | + * |
| 173 | + * @public |
| 174 | + * @param {*} text |
| 175 | + */ |
| 176 | + writeln(text) { |
| 177 | + this.write(text + "\n"); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Query from user input. |
| 182 | + * This is done by adding a input-element at the end of the terminal, |
| 183 | + * that showes a prompt and a blinking cursor. |
| 184 | + * If a key is pressed the input is added to the prompt element. |
| 185 | + * The input ends with a linebreak. |
| 186 | + * |
| 187 | + * @public |
| 188 | + * @param {*} callback |
| 189 | + */ |
| 190 | + input(callback) { |
| 191 | + // show prompt with a blinking prompt |
| 192 | + this.$output.appendChild(this.#$prompt); |
| 193 | + this.#inputCallback = callback; |
| 194 | + } |
| 195 | +} |
0 commit comments