fix js imports

This commit is contained in:
Chris Wanstrath 2025-10-01 22:16:38 -07:00
parent 8bfb6f2105
commit c8704cf8fd
18 changed files with 76 additions and 69 deletions

View File

@ -73,6 +73,7 @@
a { a {
color: var(--cyan); color: var(--cyan);
display: inline-block;
} }
a:visited { a:visited {

View File

@ -1,18 +1,21 @@
//// ////
// temporary hack for browser commands // temporary hack for browser commands
import { scrollback, content } from "./dom.js" import type { CommandOutput } from "../shared/types"
import { resize } from "./resize.js" import { scrollback, content } from "./dom"
import { autoScroll } from "./scrollback.js" import { resize } from "./resize"
import { sessionId } from "./session.js" import { autoScroll } from "./scrollback"
import { send } from "./websocket.js" import { sessionId } from "./session"
import { send } from "./websocket"
export const commands: string[] = [] export const commands: string[] = []
export const browserCommands: Record<string, (...args: string[]) => void> = { export const browserCommands: Record<string, (...args: string[]) => void | Promise<void> | CommandOutput> = {
"browser-session": () => sessionId, "browser-session": () => sessionId,
clear: () => scrollback.innerHTML = "", clear: () => scrollback.innerHTML = "",
commands: () => commands.join(" "), commands: () => {
return { html: "<div>" + commands.map(cmd => `<a href="#help ${cmd}">${cmd}</a>`).join("") + "</div>" }
},
fullscreen: () => document.body.requestFullscreen(), fullscreen: () => document.body.requestFullscreen(),
mode: (mode?: string) => { mode: (mode?: string) => {
if (!mode) { if (!mode) {
@ -33,4 +36,5 @@ export function cacheCommands(cmds: string[]) {
commands.push(...cmds) commands.push(...cmds)
commands.push(...Object.keys(browserCommands)) commands.push(...Object.keys(browserCommands))
commands.sort() commands.sort()
console.log("CMDS", commands)
} }

View File

@ -1,8 +1,8 @@
//// ////
// tab completion // tab completion
import { cmdInput } from "./dom.js" import { cmdInput } from "./dom"
import { commands } from "./commands.js" import { commands } from "./commands"
export function initCompletion() { export function initCompletion() {
cmdInput.addEventListener("keydown", handleCompletion) cmdInput.addEventListener("keydown", handleCompletion)

View File

@ -1,7 +1,7 @@
//// ////
// Blinking c64 cursor // Blinking c64 cursor
import { cmdInput, $ } from "./dom.js" import { cmdInput, $ } from "./dom"
const cursor = "Û" const cursor = "Û"
let cmdCursor: HTMLTextAreaElement let cmdCursor: HTMLTextAreaElement

View File

@ -1,3 +1,7 @@
////
// Hack... works with the `upload` command.
// Dragging a file to the terminal fills in the <input type="file"/> on screen.
export function initDrop() { export function initDrop() {
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
document.body.addEventListener(eventName, preventDefaults, false); document.body.addEventListener(eventName, preventDefaults, false);

View File

@ -1,6 +1,6 @@
import { scrollback } from "./dom.js" import { scrollback } from "./dom"
import { send } from "./websocket.js" import { send } from "./websocket"
import { focusInput } from "./focus.js" import { focusInput } from "./focus"
const INDENT_SIZE = 2 const INDENT_SIZE = 2

View File

@ -1,7 +1,7 @@
//// ////
// We try to keep the command textbox focused at all times. // We try to keep the command textbox focused at all times.
import { cmdInput } from "./dom.js" import { cmdInput } from "./dom"
export function initFocus() { export function initFocus() {
window.addEventListener("click", focusHandler) window.addEventListener("click", focusHandler)

View File

@ -1,10 +1,10 @@
//// ////
// All forms are submitted via ajax. // All forms are submitted via ajax.
import type { CommandResult, CommandOutput } from "../shared/types.js" import type { CommandResult, CommandOutput } from "../shared/types"
import { sessionId } from "./session.js" import { sessionId } from "./session"
import { setStatus, replaceOutput } from "./scrollback.js" import { setStatus, replaceOutput } from "./scrollback"
import { focusInput } from "./focus.js" import { focusInput } from "./focus"
export function initForm() { export function initForm() {
document.addEventListener("submit", submitHandler) document.addEventListener("submit", submitHandler)

View File

@ -1,10 +1,10 @@
import type { Message } from "../shared/types.js" import type { Message } from "../shared/types"
import { GameContext, type InputState } from "../shared/game.js" import { GameContext, type InputState } from "../shared/game"
import { focusInput } from "./focus.js" import { focusInput } from "./focus"
import { $$ } from "./dom.js" import { $$ } from "./dom"
import { randomId } from "../shared/utils.js" import { randomId } from "../shared/utils"
import { setStatus, addOutput, insert } from "./scrollback.js" import { setStatus, addOutput, insert } from "./scrollback"
import { browserCommands } from "./commands.js" import { browserCommands } from "./commands"
const FPS = 30 const FPS = 30
const HEIGHT = 540 const HEIGHT = 540

View File

@ -1,7 +1,7 @@
//// ////
// Command input history storage and navigation. // Command input history storage and navigation.
import { cmdInput, cmdLine } from "./dom.js" import { cmdInput, cmdLine } from "./dom"
const history: string[] = ["one", "two", "three"] const history: string[] = ["one", "two", "three"]
let idx = -1 let idx = -1

View File

@ -1,11 +1,11 @@
import { runCommand } from "./shell.js" import { runCommand } from "./shell"
import { focusInput } from "./focus.js" import { focusInput } from "./focus"
export function initHyperlink() { export function initHyperlink() {
window.addEventListener("click", handleClick) window.addEventListener("click", handleClick)
} }
function handleClick(e: MouseEvent) { async function handleClick(e: MouseEvent) {
const target = e.target const target = e.target
if (!(target instanceof HTMLElement)) return if (!(target instanceof HTMLElement)) return
@ -18,7 +18,7 @@ function handleClick(e: MouseEvent) {
if (href.startsWith("#")) { if (href.startsWith("#")) {
e.preventDefault() e.preventDefault()
runCommand(href.slice(1)) await runCommand(href.slice(1))
focusInput() focusInput()
} }
} }

View File

@ -1,17 +1,16 @@
//// ////
// Terminal input is handled by a <textarea> and friends. // Terminal input is handled by a <textarea> and friends.
import { cmdInput, cmdLine } from "./dom.js" import { cmdInput, cmdLine } from "./dom"
import { runCommand } from "./shell.js" import { runCommand } from "./shell"
import { resetHistory } from "./history.js" import { resetHistory } from "./history"
import { countChar } from "../shared/utils.js"
export function initInput() { export function initInput() {
cmdInput.addEventListener("keydown", inputHandler) cmdInput.addEventListener("keydown", inputHandler)
cmdInput.addEventListener("paste", pasteHandler) cmdInput.addEventListener("paste", pasteHandler)
} }
function inputHandler(event: KeyboardEvent) { async function inputHandler(event: KeyboardEvent) {
const target = event.target as HTMLElement const target = event.target as HTMLElement
if (target?.id !== cmdInput.id) return if (target?.id !== cmdInput.id) return
@ -25,7 +24,7 @@ function inputHandler(event: KeyboardEvent) {
cmdLine.dataset.extended = "true" cmdLine.dataset.extended = "true"
} else if (event.key === "Enter") { } else if (event.key === "Enter") {
event.preventDefault() event.preventDefault()
runCommand(cmdInput.value) await runCommand(cmdInput.value)
clearInput() clearInput()
} }

View File

@ -1,17 +1,17 @@
import { initCompletion } from "./completion.js" import { initCompletion } from "./completion"
import { initCursor } from "./cursor.js" import { initCursor } from "./cursor"
import { initDrop } from "./drop.js" import { initDrop } from "./drop"
import { initEditor } from "./editor.js" import { initEditor } from "./editor"
import { initFocus } from "./focus.js" import { initFocus } from "./focus"
import { initForm } from "./form.js" import { initForm } from "./form"
import { initGamepad } from "./gamepad.js" import { initGamepad } from "./gamepad"
import { initHistory } from "./history.js" import { initHistory } from "./history"
import { initHyperlink } from "./hyperlink.js" import { initHyperlink } from "./hyperlink"
import { initInput } from "./input.js" import { initInput } from "./input"
import { initResize } from "./resize.js" import { initResize } from "./resize"
import { initScrollback } from "./scrollback.js" import { initScrollback } from "./scrollback"
import { startVramCounter } from "./vram.js" import { startVramCounter } from "./vram"
import { startConnection } from "./websocket.js" import { startConnection } from "./websocket"
initCompletion() initCompletion()
initCursor() initCursor()

View File

@ -2,9 +2,9 @@
// The scrollback shows your history of interacting with the shell. // The scrollback shows your history of interacting with the shell.
// input, output, etc // input, output, etc
import type { Message, CommandOutput, CommandResult } from "../shared/types.js" import type { Message, CommandOutput, CommandResult } from "../shared/types"
import { scrollback, cmdInput, $$ } from "./dom.js" import { scrollback, cmdInput, $$ } from "./dom"
import { randomId } from "../shared/utils.js" import { randomId } from "../shared/utils"
type InputStatus = "waiting" | "streaming" | "ok" | "error" type InputStatus = "waiting" | "streaming" | "ok" | "error"

View File

@ -2,6 +2,6 @@
// Each browser tab is a shell session. This means you can run multiple sessions // Each browser tab is a shell session. This means you can run multiple sessions
// in the same browser. // in the same browser.
import { randomId } from "../shared/utils.js" import { randomId } from "../shared/utils"
export const sessionId = randomId() export const sessionId = randomId()

View File

@ -1,17 +1,17 @@
//// ////
// The shell runs on the server and processes input, returning output. // The shell runs on the server and processes input, returning output.
import { addInput, setStatus, addOutput } from "./scrollback.js" import { addInput, setStatus, addOutput } from "./scrollback"
import { send } from "./websocket.js" import { send } from "./websocket"
import { randomId } from "../shared/utils.js" import { randomId } from "../shared/utils"
import { addToHistory } from "./history.js" import { addToHistory } from "./history"
import { browserCommands } from "./commands.js" import { browserCommands } from "./commands"
export function runCommand(input: string) { export async function runCommand(input: string) {
if (!input.trim()) return if (!input.trim()) return
if (input.includes(";")) { if (input.includes(";")) {
input.split(";").forEach(cmd => runCommand(cmd.trim())) input.split(";").forEach(async cmd => await runCommand(cmd.trim()))
return return
} }
@ -23,9 +23,8 @@ export function runCommand(input: string) {
const [cmd = "", ...args] = input.split(" ") const [cmd = "", ...args] = input.split(" ")
if (browserCommands[cmd]) { if (browserCommands[cmd]) {
const result = browserCommands[cmd](...args) const result = await browserCommands[cmd](...args)
if (typeof result === "string") if (result) addOutput(id, result)
addOutput(id, result)
setStatus(id, "ok") setStatus(id, "ok")
} else { } else {
send({ id, type: "input", data: input }) send({ id, type: "input", data: input })

View File

@ -1,7 +1,7 @@
//// ////
// Fun vram counter at startup. // Fun vram counter at startup.
import { $ } from "./dom.js" import { $ } from "./dom"
const vramCounter = $("vram-size")! const vramCounter = $("vram-size")!

View File

@ -1,10 +1,10 @@
//// ////
// The terminal communicates with the shell via websockets. // The terminal communicates with the shell via websockets.
import type { Message } from "../shared/types.js" import type { Message } from "../shared/types"
import { sessionId } from "./session.js" import { sessionId } from "./session"
import { dispatchMessage } from "./dispatch.js" import { dispatchMessage } from "./dispatch"
import { addErrorMessage } from "./scrollback.js" import { addErrorMessage } from "./scrollback"
const MAX_RETRIES = 5 const MAX_RETRIES = 5
let retries = 0 let retries = 0