* Helper function for shell initialization
(consoleNode, target, frameID)
| 92 | * Helper function for shell initialization |
| 93 | */ |
| 94 | function openShell(consoleNode, target, frameID) { |
| 95 | promptForPin(); |
| 96 | if (consoleNode) { |
| 97 | slideToggle(consoleNode); |
| 98 | return consoleNode; |
| 99 | } |
| 100 | let historyPos = 0; |
| 101 | const history = [""]; |
| 102 | const consoleElement = createConsole(); |
| 103 | const output = createConsoleOutput(); |
| 104 | const form = createConsoleInputForm(); |
| 105 | const command = createConsoleInput(); |
| 106 | |
| 107 | target.parentNode.appendChild(consoleElement); |
| 108 | consoleElement.append(output); |
| 109 | consoleElement.append(form); |
| 110 | form.append(command); |
| 111 | command.focus(); |
| 112 | slideToggle(consoleElement); |
| 113 | |
| 114 | form.addEventListener("submit", (e) => { |
| 115 | handleConsoleSubmit(e, command, frameID).then((consoleOutput) => { |
| 116 | output.append(consoleOutput); |
| 117 | command.focus(); |
| 118 | consoleElement.scrollTo(0, consoleElement.scrollHeight); |
| 119 | const old = history.pop(); |
| 120 | history.push(command.value); |
| 121 | if (typeof old !== "undefined") { |
| 122 | history.push(old); |
| 123 | } |
| 124 | historyPos = history.length - 1; |
| 125 | command.value = ""; |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | command.addEventListener("keydown", (e) => { |
| 130 | if (e.key === "l" && e.ctrlKey) { |
| 131 | output.innerText = "--- screen cleared ---"; |
| 132 | } else if (e.key === "ArrowUp" || e.key === "ArrowDown") { |
| 133 | // Handle up arrow and down arrow. |
| 134 | if (e.key === "ArrowUp" && historyPos > 0) { |
| 135 | e.preventDefault(); |
| 136 | historyPos--; |
| 137 | } else if (e.key === "ArrowDown" && historyPos < history.length - 1) { |
| 138 | historyPos++; |
| 139 | } |
| 140 | command.value = history[historyPos]; |
| 141 | } |
| 142 | return false; |
| 143 | }); |
| 144 | |
| 145 | return consoleElement; |
| 146 | } |
| 147 | |
| 148 | function addEventListenersToElements(elements, event, listener) { |
| 149 | elements.forEach((el) => el.addEventListener(event, listener)); |
no test coverage detected