@gjermundgaraba/clankermux 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -0
- package/dist/client/index.d.ts +3 -2
- package/dist/client/keyboard.d.ts +41 -0
- package/dist/client/keyboard.js +60 -0
- package/dist/client/mount.d.ts +41 -10
- package/dist/client/mount.js +326 -16
- package/dist/client/profile.d.ts +4 -0
- package/dist/client/profile.js +11 -0
- package/dist/client/worker.d.ts +99 -5
- package/dist/client/worker.js +298 -24
- package/dist/engine/index.d.ts +34 -0
- package/dist/engine/index.js +102 -0
- package/dist/engine/render.d.ts +8 -1
- package/dist/engine/render.js +24 -3
- package/dist/engine/selection.d.ts +39 -0
- package/dist/engine/selection.js +114 -0
- package/dist/renderer/index.d.ts +8 -2
- package/dist/renderer/index.js +57 -16
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# @gjermundgaraba/clankermux
|
|
2
|
+
|
|
3
|
+
The browser client of [clankermux](https://github.com/gjermundgaraba/clankermux): a terminal on a canvas, its engine and connection in a module worker, speaking the clankermux protocol over the server's TLS WebSocket carrier.
|
|
4
|
+
|
|
5
|
+
| Subpath | Exports |
|
|
6
|
+
| --- | --- |
|
|
7
|
+
| `.` | `mountTerminal` and its types, `ClientError` |
|
|
8
|
+
| `./worker` | `runWorker`, for the worker module; no DOM types |
|
|
9
|
+
| `./ghostty-vt.wasm` | the engine build the server's engine id names |
|
|
10
|
+
| `./proto`, `./engine`, `./renderer` | the protocol messages, the engine and the canvas renderer |
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
// worker.ts, started as a module worker
|
|
14
|
+
import { runWorker } from "@gjermundgaraba/clankermux/worker";
|
|
15
|
+
runWorker(self);
|
|
16
|
+
|
|
17
|
+
// page
|
|
18
|
+
import { mountTerminal } from "@gjermundgaraba/clankermux";
|
|
19
|
+
import wasm from "@gjermundgaraba/clankermux/ghostty-vt.wasm?url";
|
|
20
|
+
const terminal = mountTerminal({
|
|
21
|
+
worker: new Worker(new URL("./worker.ts", import.meta.url), { type: "module" }),
|
|
22
|
+
canvas, container, wasm, url: "wss://127.0.0.1:7438/", session,
|
|
23
|
+
font: "14px monospace", colors: () => ({ fg: "#d8dee9", bg: "#282c34", cursor: "#ffffff" }),
|
|
24
|
+
onLink: (link) => console.log(link),
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## `MountedTerminal`
|
|
29
|
+
|
|
30
|
+
| Method | Does |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `attach(session)` | Detaches the current session and attaches `session`; after a connection-level end (below), connects again first. |
|
|
33
|
+
| `create(options?)` | Creates a session at the current size, attaches it, and resolves with it. `options.onExit`: `"keep"` keeps it listed as `exited` when its program exits; `"remove"` removes it `limits.remove_grace` after. Without it the server's default (`remove`) applies. |
|
|
34
|
+
| `end()` | Hangs up the attached session's processes (SIGHUP). Also resolves when the session is not running (`exited`, `restored`): there is nothing to end. |
|
|
35
|
+
| `remove()` | Removes the attached session, which must be `exited` or `restored`; a running one rejects with `running`. |
|
|
36
|
+
| `report()` | Reports the container's size, the colours and the scheme again. |
|
|
37
|
+
| `paste(text)` | Pastes, bracketed when the program asked for it. |
|
|
38
|
+
| `dispose()` | Removes the listeners and terminates the worker, which drops its connection; requests in flight reject. |
|
|
39
|
+
|
|
40
|
+
`create`, `end` and `remove` need a connection: call them after a `connected` link. They reject with a `ClientError`:
|
|
41
|
+
|
|
42
|
+
- with `kind: "server"` and `server.code` when the server refuses;
|
|
43
|
+
- with `kind: "closed"` when there is no connection, or after `dispose()`;
|
|
44
|
+
- with `kind: "protocol"` when the server sent a malformed frame meanwhile (the connection then ends with `ended closed`, `retry: true`).
|
|
45
|
+
|
|
46
|
+
`end` and `remove` reject with a plain `Error` when no session is attached. Since 0.1.1 `create` returns this promise, where it returned nothing and dropped a request it could not make; a caller that ignores it should `.catch` it. Closing a pane for good is `end()` for a running session and `remove()` for one that has exited, or that a previous server left `restored`. With `onExit: "remove"` an ended session also removes itself.
|
|
47
|
+
|
|
48
|
+
## Keys and composed text
|
|
49
|
+
|
|
50
|
+
The terminal's input element is a textarea the mount places after the canvas: an input method composes only into an editable element. It is invisible, out of the tab order (`tabIndex = -1`) and ignores the pointer. Focusing the canvas (a click, `canvas.focus()`, a tab stop the page gives it) or calling `focus()` focuses the textarea, so `document.activeElement` is the textarea while the terminal has the keyboard: test for focus with `container.contains(document.activeElement)`, not `=== canvas`. Key events bubble from it through the container as they did from the canvas.
|
|
51
|
+
|
|
52
|
+
- Keys go to the engine's key encoder, which follows the program's modes (cursor keys, the kitty keyboard protocol). The browser keeps Cmd (Meta) chords and Ctrl+Shift+V, its paste.
|
|
53
|
+
- A key that belongs to an input method is left to it: one that is composing (`isComposing`), that it processes (`Process`, key code 229), or a dead key (`Dead`). The composition is shown at the cursor, in the terminal's colours, while it lasts, and its text is sent when it ends (`compositionend`). A key that ended a composition sends no release.
|
|
54
|
+
- Text inserted with no key that sent it (a dead key's character, the emoji picker, dictation) is sent as it is inserted (`beforeinput` `insertText`); a line break or a backward delete from such a source is sent as Enter or Backspace.
|
|
55
|
+
- Composed text goes to the program as UTF-8, as typed text does, not as a paste.
|
|
56
|
+
|
|
57
|
+
A page that holds input back (a pause) holds these events, which the terminal acts on: on the input element `keydown`, `keyup`, `beforeinput`, `compositionstart`, `compositionend`, `paste`, `beforecopy` and `copy`; on the canvas `mousedown` (focus and selection; the drag continues on `window` `mousemove`/`mouseup`) and `wheel` (which sends arrow keys on the alternate screen). macOS press-and-hold accents send the base letter on its keydown and then the accented one, as in xterm.js.
|
|
58
|
+
|
|
59
|
+
Verified in headless Chrome on macOS through CDP (`Input.imeSetComposition`, `Input.insertText`, `Input.dispatchKeyEvent`): a Japanese composition sent once at its end, nothing during it; a dead key (Option+E, then é) with no Alt+E sent; an emoji inserted with no key; plain keys sent once. Not verified by hand with a real input method, in Safari or in Firefox.
|
|
60
|
+
|
|
61
|
+
## `onSessions`
|
|
62
|
+
|
|
63
|
+
`onSessions(sessions, current)` reports every session on the server, and which one the page shows, at each change: a session created, exited or removed, and a `session_changed`, which the server sends when a session's label, size, attachments, title or working directory changes. The title is the program's (OSC 0 or 2), the working directory the one it reports (OSC 7, as a path), each empty until set, and both cleared by a full reset. A status line takes them from the session whose `id` is `current`; the `session` of a `connected` link is the one `ATTACHED` described, and does not follow these changes.
|
|
64
|
+
|
|
65
|
+
## `onLink`
|
|
66
|
+
|
|
67
|
+
`onLink(link)` reports each change of the page's link to its session, through the connection and the attachment. `onStatus(text)` reports the same moments, and more, as text for display.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
type Link =
|
|
71
|
+
| { state: "connecting"; attempt: number }
|
|
72
|
+
| { state: "connected"; session: Session | undefined }
|
|
73
|
+
| { state: "ended"; reason: LinkEnd; code?: ErrorCode; message?: string; exit?: Exit; retry: boolean };
|
|
74
|
+
type LinkEnd = "closed" | "removed" | "stopping" | "exited" | "no_such_session" | "engine_mismatch" | "error";
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`retry` means the worker reconnects or attaches again by itself, so a `connecting` or `connected` follows. Otherwise nothing follows until the page calls `attach` or `create`. After a connection-level end (the last three rows of the first table) there is no connection: `create` rejects, and `attach` connects again (`connecting` from attempt 1), so "Retry" can be `attach`. An engine that failed to load needs a new mount.
|
|
78
|
+
|
|
79
|
+
| When | Link |
|
|
80
|
+
| --- | --- |
|
|
81
|
+
| A connection is opened: at mount, and at each reconnect | `connecting`, `attempt` from 1; it starts again at 1 once a server answers |
|
|
82
|
+
| A connection cannot be opened, or closes before `WELCOME` | nothing at once; the next `connecting` after a backoff |
|
|
83
|
+
| The connection closes | `ended closed`, `retry: true`, then `connecting` |
|
|
84
|
+
| The engine cannot be loaded | `ended error`, `retry: false` |
|
|
85
|
+
| The server refuses the handshake (`ERROR` before `WELCOME`, e.g. `version_mismatch`), or sends something else first | `ended error` with the code when there is one, `retry: false` |
|
|
86
|
+
| The server ends the connection (`ERROR` without an id, e.g. `protocol_error`) | `ended error` with the code, `retry: false` |
|
|
87
|
+
| `subscribe` fails on a live connection | `ended error`, `retry: false`; the worker closes the connection |
|
|
88
|
+
|
|
89
|
+
Once connected:
|
|
90
|
+
|
|
91
|
+
| When | Link |
|
|
92
|
+
| --- | --- |
|
|
93
|
+
| Connected with no session to attach | `connected`, `session: undefined` |
|
|
94
|
+
| A session's screen is shown: a snapshot's first screen, a resume, again after a gap, a re-attach or a reconnect | `connected` with the session as `ATTACHED` described it; `status` tells `running`, `restored` or `exited` |
|
|
95
|
+
| A snapshot shows a session that has exited already | `connected`, then at once `ended exited` with its `exit`, `retry: false` (a resume onto such a session gets its `EXITED`, the next row, instead) |
|
|
96
|
+
| The session's program exits (`EXITED`) | `ended exited` with `exit`, `retry: false` |
|
|
97
|
+
| `CLOSED removed` | `ended removed`, `retry: false`; the worker forgets the session |
|
|
98
|
+
| `CLOSED stopping` | `ended stopping`, `retry: true`; the connection's close follows, and the reconnect attaches the session again (as `restored`) |
|
|
99
|
+
| `OPEN` refused, or the attachment ended, with `no_such_session` or `engine_mismatch` | `ended` with that reason and code, `retry: false` |
|
|
100
|
+
| The attachment ended with `invalid_request` | `ended error`, `retry: false` |
|
|
101
|
+
| The attachment ended with another error (`too_slow`, a snapshot that failed, `internal`) | `ended error` with the code, `retry: true`; the worker attaches again after a backoff |
|
|
102
|
+
| The stream lost its place (an `OUTPUT` at an unexpected offset) | `ended error`, `retry: true`; the worker attaches again afresh |
|
|
103
|
+
| Any other fault in the attachment's frames | `ended error`, `retry: false` |
|
|
104
|
+
|
|
105
|
+
Nothing fires for:
|
|
106
|
+
|
|
107
|
+
- the errors that keep an attachment (`read_only`, `not_running`, `input_overflow`);
|
|
108
|
+
- a gap, until its new screen fires `connected`;
|
|
109
|
+
- `attach` or `create` before their screen;
|
|
110
|
+
- `dispose`.
|
|
111
|
+
|
|
112
|
+
`reason: "closed"` is the connection's close. The worker detaches without waiting for the server's `CLOSED closed`, so that frame never shows up as a link change.
|
|
113
|
+
|
|
114
|
+
"The server ended an answered connection with an error, and nothing will reconnect" is `ended` with `reason: "error"` and `retry: false` after a `connected`, with no `connecting` after it. An attached session is the latest `connected` with a `session`.
|
|
115
|
+
|
|
116
|
+
## Selection and copy
|
|
117
|
+
|
|
118
|
+
The mouse selects on the canvas, and what it selects is copied to the clipboard when the button comes up, with no keystroke:
|
|
119
|
+
|
|
120
|
+
- a drag selects from the pressed cell to the pointer's, by cells (the half of a cell the pointer passed decides the last one);
|
|
121
|
+
- a double click selects a word and a triple click a line (a soft-wrapped line whole), and a drag after them extends by words or lines;
|
|
122
|
+
- a drag that leaves the canvas goes on, clamped to its edges; it does not scroll the history.
|
|
123
|
+
|
|
124
|
+
The text is the engine's: soft-wrapped lines are joined, trailing blanks on each line trimmed, and lines end with `\n`. The browser's copy (Cmd+C, or the Copy menu) copies the selection again, through a `copy` event on the input element, and so does Ctrl+Shift+C (without a selection Ctrl+Shift+C goes to the program as a key, and Ctrl+C is always the program's). `selection()` returns the selected text. Paste is unchanged.
|
|
125
|
+
|
|
126
|
+
The selection is the engine's, drawn inverted, and the engine keeps it on its cells:
|
|
127
|
+
|
|
128
|
+
- output that scrolls the selected rows up moves the selection with them;
|
|
129
|
+
- output that writes over the selected cells, or erases them (`clear`), leaves it on those cells; the text it copies stays the text selected at the release;
|
|
130
|
+
- scrolling the view moves it with the text; it can reach into the history and beyond the view;
|
|
131
|
+
- a resize reflows it with the text.
|
|
132
|
+
|
|
133
|
+
It is cleared by a click (a press that selects nothing), by input sent to the session (a key that sends something, a paste, composed text; not a modifier alone), by a switch to the alternate screen (it is not back when the program leaves it), by a full reset (RIS), and by a new screen (attaching, a gap).
|
|
134
|
+
|
|
135
|
+
The copy on release starts within `mouseup`, as Safari requires: `navigator.clipboard.write` with a `ClipboardItem` whose text arrives when the worker has it, a few milliseconds later; when nothing was selected it fails and the clipboard is left alone. Where `ClipboardItem` is missing it is `writeText` once the text arrives, which a browser that wants the gesture still running may refuse; Cmd+C then copies. Verified in headless Chrome only. A `beforecopy` listener lets WebKit offer Copy on the empty input element while there is a selection. Where the browser keeps Ctrl+Shift+C for itself (a developer tools shortcut), Cmd+C or the Copy menu copies. Programs that ask for mouse reports get none from this client, so the mouse always selects.
|
|
136
|
+
|
|
137
|
+
## Scrollback
|
|
138
|
+
|
|
139
|
+
The terminal holds the session's history (the server sends what it keeps with the screen), and the view scrolls through it:
|
|
140
|
+
|
|
141
|
+
- the wheel over the canvas, in whole rows (a trackpad's pixels add up until they make one; Ctrl+wheel stays the browser's zoom);
|
|
142
|
+
- Shift+PageUp and Shift+PageDown, a screen at a time; Shift+Home and Shift+End, to the top of the history and back to the live screen.
|
|
143
|
+
|
|
144
|
+
Output while the view is scrolled back leaves it where it is. Any input that sends something (a key, a paste, composed text) brings it back to the live screen first; a modifier alone does not. The alternate screen of full-screen programs (`vim`, `less`) has no history: there Shift+PageUp and the rest go to the program as keys, and the wheel sends the up and down arrow keys while the program leaves alternate scroll (mode 1007) on, as it is by default, or nothing.
|
|
145
|
+
|
|
146
|
+
While the view is scrolled back the canvas draws a mark at its right edge, the view's share of the history and its place in it. `onScroll(rows)` reports the rows between the bottom of the view and the live screen's at each change: above 0 when scrolled back (it grows as output arrives meanwhile), 0 at the live screen.
|
|
147
|
+
|
|
148
|
+
## Size between clients
|
|
149
|
+
|
|
150
|
+
A session has one size, and the server applies the latest report (PROTOCOL.md, "Size"). The terminal reports its container's size, its colours and its scheme only while its page is visible and its window has the focus (`document.visibilityState === "visible"` and `document.hasFocus()`). It reports again when the page is shown or its window focused, even at an unchanged size, and when the session was resized to another client's size while the page kept its focus (the user went to another machine and came back), at the page's next input: a key or text that sends something, a paste, a click on the canvas, the wheel. So with the same session open in several tabs or on several machines, the session takes the size of the one you look at, and a hidden or unfocused one never resizes it: its `OPEN` carries no viewport, it sends no `VIEWPORT`, and its canvas shows the session's size, clipped or with a margin. `report()` follows the same rule. `create()` makes the session at the container's current size, focused or not. A headless browser counts only its newest page as visible and focused, and a terminal in an iframe has the focus only once the frame does; such a page does not size the session until then.
|
|
151
|
+
|
|
152
|
+
## Colour scheme
|
|
153
|
+
|
|
154
|
+
The terminal reports a colour scheme, `light` or `dark`, with its colours; programs read it through the colour scheme query (mode 2031). By default it is the canvas's own:
|
|
155
|
+
|
|
156
|
+
- the scheme the canvas's computed `color-scheme` names, when it names one of `light` and `dark` (`color-scheme: dark`, `only light`);
|
|
157
|
+
- otherwise (`normal`, `light dark`) the page's `prefers-color-scheme`.
|
|
158
|
+
|
|
159
|
+
It is reported again when the preference changes, and when an attribute changes on the canvas or an ancestor (a `class`, `style` or `data-*` that pins the page's scheme) and the scheme or the colours differ from the last report. A stylesheet swapped without an attribute change is not noticed; call `report()` after it.
|
|
160
|
+
|
|
161
|
+
To pin it, set `color-scheme` on the canvas (or an ancestor) in CSS, or pass `scheme: () => "dark"`, or a function of your own; it is read on every report.
|
package/dist/client/index.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export type { ErrorKind, Transport } from "./connection.ts";
|
|
|
5
5
|
export { Osc52Scanner } from "./osc52.ts";
|
|
6
6
|
export type { ClipboardWrite } from "./osc52.ts";
|
|
7
7
|
export { mountTerminal } from "./mount.ts";
|
|
8
|
-
export type { Colors, MountOptions, MountedTerminal } from "./mount.ts";
|
|
9
|
-
export type {
|
|
8
|
+
export type { Colors, CreateOptions, MountOptions, MountedTerminal } from "./mount.ts";
|
|
9
|
+
export type { Link, LinkEnd } from "./worker.ts";
|
|
10
|
+
export type { ErrorCode, Exit, OnExit, Scheme, Session } from "../proto/index.ts";
|
|
@@ -10,7 +10,21 @@ export interface KeyEventLike {
|
|
|
10
10
|
altKey: boolean;
|
|
11
11
|
metaKey: boolean;
|
|
12
12
|
isComposing: boolean;
|
|
13
|
+
/** 229 while an input method processes the key. */
|
|
14
|
+
keyCode: number;
|
|
13
15
|
}
|
|
16
|
+
/** The parts of an `InputEvent` the input element reads. */
|
|
17
|
+
export interface InputEventLike {
|
|
18
|
+
inputType: string;
|
|
19
|
+
data: string | null;
|
|
20
|
+
isComposing: boolean;
|
|
21
|
+
}
|
|
22
|
+
/** What typing comes to: a key for the key encoder, or text as typed. */
|
|
23
|
+
export type Typed = {
|
|
24
|
+
key: KeyInput;
|
|
25
|
+
} | {
|
|
26
|
+
text: string;
|
|
27
|
+
};
|
|
14
28
|
/** What the page knows about the keyboard: the layout's unmodified
|
|
15
29
|
* character per `code` when the browser exposes it, and the platform. */
|
|
16
30
|
export interface Keyboard {
|
|
@@ -21,4 +35,31 @@ export interface Keyboard {
|
|
|
21
35
|
* command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
|
|
22
36
|
* the terminal's. */
|
|
23
37
|
export declare function browserKeeps(e: KeyEventLike): boolean;
|
|
38
|
+
/** Whether the key belongs to an input method: it is composing, or
|
|
39
|
+
* processing the key (`Process`, key code 229), or the key is a dead key
|
|
40
|
+
* that starts a composition. Its text arrives at `compositionend`. */
|
|
41
|
+
export declare function composing(e: KeyEventLike): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Typing on the terminal's input element, a textarea: keys go to the key
|
|
44
|
+
* encoder, and text that no key sent goes as text. Keys the browser keeps
|
|
45
|
+
* and keys of a composition are left to the browser; a composition's text
|
|
46
|
+
* is sent when it ends, and text inserted without a key (a dead key's
|
|
47
|
+
* character, the emoji picker, dictation) as it is inserted.
|
|
48
|
+
*/
|
|
49
|
+
export declare class Typing {
|
|
50
|
+
private readonly keyboard;
|
|
51
|
+
/** Keys whose press was sent, so their release is too: a key that
|
|
52
|
+
* ended a composition sends no release of a press it never sent. */
|
|
53
|
+
private readonly down;
|
|
54
|
+
constructor(keyboard: Keyboard);
|
|
55
|
+
/** A key to send, or undefined to leave the event to the browser. */
|
|
56
|
+
keydown(e: KeyEventLike): KeyInput | undefined;
|
|
57
|
+
keyup(e: KeyEventLike): KeyInput | undefined;
|
|
58
|
+
/** `beforeinput`: what the element would insert or delete, when no key
|
|
59
|
+
* sent it. A composition's own steps wait for `compositionend`; a paste
|
|
60
|
+
* is the `paste` event's. */
|
|
61
|
+
beforeinput(e: InputEventLike): Typed | undefined;
|
|
62
|
+
/** `compositionend`: the composed text. */
|
|
63
|
+
compositionend(data: string | null): Typed | undefined;
|
|
64
|
+
}
|
|
24
65
|
export declare function keyInput(e: KeyEventLike, keyboard: Keyboard): KeyInput;
|
package/dist/client/keyboard.js
CHANGED
|
@@ -5,6 +5,66 @@ import { isText } from "../engine/index.js";
|
|
|
5
5
|
export function browserKeeps(e) {
|
|
6
6
|
return e.metaKey || (e.ctrlKey && e.shiftKey && e.code === "KeyV");
|
|
7
7
|
}
|
|
8
|
+
/** Whether the key belongs to an input method: it is composing, or
|
|
9
|
+
* processing the key (`Process`, key code 229), or the key is a dead key
|
|
10
|
+
* that starts a composition. Its text arrives at `compositionend`. */
|
|
11
|
+
export function composing(e) {
|
|
12
|
+
return e.isComposing || e.keyCode === 229 || e.key === "Process" || e.key === "Dead";
|
|
13
|
+
}
|
|
14
|
+
/** A key with no modifiers, pressed. */
|
|
15
|
+
function pressed(code) {
|
|
16
|
+
return { code, key: code, action: "press", shift: false, ctrl: false, alt: false, meta: false };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Typing on the terminal's input element, a textarea: keys go to the key
|
|
20
|
+
* encoder, and text that no key sent goes as text. Keys the browser keeps
|
|
21
|
+
* and keys of a composition are left to the browser; a composition's text
|
|
22
|
+
* is sent when it ends, and text inserted without a key (a dead key's
|
|
23
|
+
* character, the emoji picker, dictation) as it is inserted.
|
|
24
|
+
*/
|
|
25
|
+
export class Typing {
|
|
26
|
+
keyboard;
|
|
27
|
+
/** Keys whose press was sent, so their release is too: a key that
|
|
28
|
+
* ended a composition sends no release of a press it never sent. */
|
|
29
|
+
down = new Set();
|
|
30
|
+
constructor(keyboard) {
|
|
31
|
+
this.keyboard = keyboard;
|
|
32
|
+
}
|
|
33
|
+
/** A key to send, or undefined to leave the event to the browser. */
|
|
34
|
+
keydown(e) {
|
|
35
|
+
if (browserKeeps(e) || composing(e))
|
|
36
|
+
return undefined;
|
|
37
|
+
this.down.add(e.code);
|
|
38
|
+
return keyInput(e, this.keyboard);
|
|
39
|
+
}
|
|
40
|
+
keyup(e) {
|
|
41
|
+
if (!this.down.delete(e.code))
|
|
42
|
+
return undefined;
|
|
43
|
+
return keyInput(e, this.keyboard);
|
|
44
|
+
}
|
|
45
|
+
/** `beforeinput`: what the element would insert or delete, when no key
|
|
46
|
+
* sent it. A composition's own steps wait for `compositionend`; a paste
|
|
47
|
+
* is the `paste` event's. */
|
|
48
|
+
beforeinput(e) {
|
|
49
|
+
if (e.isComposing)
|
|
50
|
+
return undefined;
|
|
51
|
+
switch (e.inputType) {
|
|
52
|
+
case "insertText":
|
|
53
|
+
return e.data ? { text: e.data } : undefined;
|
|
54
|
+
case "insertLineBreak":
|
|
55
|
+
case "insertParagraph":
|
|
56
|
+
return { key: pressed("Enter") };
|
|
57
|
+
case "deleteContentBackward":
|
|
58
|
+
return { key: pressed("Backspace") };
|
|
59
|
+
default:
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** `compositionend`: the composed text. */
|
|
64
|
+
compositionend(data) {
|
|
65
|
+
return data ? { text: data } : undefined;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
8
68
|
export function keyInput(e, keyboard) {
|
|
9
69
|
const input = {
|
|
10
70
|
code: e.code,
|
package/dist/client/mount.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type { Scheme, Session } from "../proto/index.ts";
|
|
1
|
+
import type { OnExit, Scheme, Session } from "../proto/index.ts";
|
|
2
|
+
import type { Link } from "./worker.ts";
|
|
4
3
|
/** The terminal's default colours as CSS colours (hex or `rgb()`). */
|
|
5
4
|
export interface Colors {
|
|
6
5
|
fg: string;
|
|
@@ -11,7 +10,8 @@ export interface MountOptions {
|
|
|
11
10
|
/** A module worker whose script calls `runWorker(self)`; the terminal
|
|
12
11
|
* owns it from here. */
|
|
13
12
|
worker: Worker;
|
|
14
|
-
/** Drawn on
|
|
13
|
+
/** Drawn on. Focusing it focuses the terminal's input element, a
|
|
14
|
+
* textarea placed after it, which takes keys, paste and composed text. */
|
|
15
15
|
canvas: HTMLCanvasElement;
|
|
16
16
|
/** The element whose size sets the grid. */
|
|
17
17
|
container: HTMLElement;
|
|
@@ -25,22 +25,53 @@ export interface MountOptions {
|
|
|
25
25
|
font: string;
|
|
26
26
|
/** Read again on every report. */
|
|
27
27
|
colors: () => Colors;
|
|
28
|
-
/** The colour scheme reported to the session
|
|
29
|
-
* scheme
|
|
28
|
+
/** The colour scheme reported to the session. By default the canvas's
|
|
29
|
+
* own: the scheme its computed `color-scheme` names, when it names one
|
|
30
|
+
* of `light` and `dark`, else the page's preferred scheme. Read again
|
|
31
|
+
* on every report. */
|
|
30
32
|
scheme?: () => Scheme;
|
|
33
|
+
/** A line of text for each change, for display. */
|
|
31
34
|
onStatus?: (text: string) => void;
|
|
35
|
+
/** Each change of the link to the session, to switch on. */
|
|
36
|
+
onLink?: (link: Link) => void;
|
|
32
37
|
onSessions?: (sessions: Session[], current: string | undefined) => void;
|
|
38
|
+
/** The rows between the bottom of the view and the live screen's, when
|
|
39
|
+
* that changes: above 0 while the view is scrolled back into the
|
|
40
|
+
* history (and growing with output meanwhile), 0 when it shows the
|
|
41
|
+
* live screen again. The canvas draws a mark at its right edge too. */
|
|
42
|
+
onScroll?: (rows: number) => void;
|
|
43
|
+
}
|
|
44
|
+
export interface CreateOptions {
|
|
45
|
+
/** `keep`: the session stays listed as `exited` when its program
|
|
46
|
+
* exits; `remove`: it is removed `limits.remove_grace` after. The
|
|
47
|
+
* server's default (`remove`) when absent. */
|
|
48
|
+
onExit?: OnExit;
|
|
33
49
|
}
|
|
34
50
|
export interface MountedTerminal {
|
|
35
51
|
/** Detaches the current session and attaches `session`. */
|
|
36
52
|
attach(session: string): void;
|
|
37
|
-
/** Creates a session at the current size and attaches it
|
|
38
|
-
|
|
39
|
-
|
|
53
|
+
/** Creates a session at the current size and attaches it; resolves
|
|
54
|
+
* with it as created. Like `end` and `remove`, it needs a connection:
|
|
55
|
+
* call it after a `connected` link. */
|
|
56
|
+
create(options?: CreateOptions): Promise<Session>;
|
|
57
|
+
/** Hangs up the attached session's processes (SIGHUP); its exit
|
|
58
|
+
* follows as a link `ended` with reason `exited`. Resolves too when the
|
|
59
|
+
* session is not running (`exited` or `restored`). */
|
|
60
|
+
end(): Promise<void>;
|
|
61
|
+
/** Removes the attached session, which must be `exited` or `restored`
|
|
62
|
+
* (else `running`); the link ends with `removed`. */
|
|
63
|
+
remove(): Promise<void>;
|
|
64
|
+
/** Focuses the terminal's input element, as focusing the canvas does. */
|
|
65
|
+
focus(): void;
|
|
66
|
+
/** The selected text, as the last selection made copied it; undefined
|
|
67
|
+
* when nothing is selected. */
|
|
68
|
+
selection(): string | undefined;
|
|
69
|
+
/** Reports the container's size, the colours and the scheme again;
|
|
70
|
+
* to the session only while the page is visible and focused. */
|
|
40
71
|
report(): void;
|
|
41
72
|
paste(text: string): void;
|
|
42
73
|
/** Removes the listeners and terminates the worker, which ends its
|
|
43
|
-
* connection. */
|
|
74
|
+
* connection; requests in flight reject. */
|
|
44
75
|
dispose(): void;
|
|
45
76
|
}
|
|
46
77
|
export declare function mountTerminal(options: MountOptions): MountedTerminal;
|