@gjermundgaraba/clankermux 0.1.1 → 0.3.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 +64 -0
- package/dist/client/keyboard.d.ts +41 -0
- package/dist/client/keyboard.js +60 -0
- package/dist/client/mount.d.ts +18 -6
- package/dist/client/mount.js +287 -15
- package/dist/client/profile.d.ts +4 -0
- package/dist/client/profile.js +11 -0
- package/dist/client/worker.d.ts +36 -3
- package/dist/client/worker.js +166 -9
- 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/proto/messages.d.ts +73 -8
- package/dist/renderer/index.d.ts +8 -2
- package/dist/renderer/index.js +57 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,6 +45,23 @@ const terminal = mountTerminal({
|
|
|
45
45
|
|
|
46
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
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
|
+
|
|
48
65
|
## `onLink`
|
|
49
66
|
|
|
50
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.
|
|
@@ -95,3 +112,50 @@ Nothing fires for:
|
|
|
95
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.
|
|
96
113
|
|
|
97
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.
|
|
@@ -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,5 +1,3 @@
|
|
|
1
|
-
/** The page side of a terminal: canvas, keyboard, paste and resize, with
|
|
2
|
-
* the engine in a worker running `runWorker`. */
|
|
3
1
|
import type { OnExit, Scheme, Session } from "../proto/index.ts";
|
|
4
2
|
import type { Link } from "./worker.ts";
|
|
5
3
|
/** The terminal's default colours as CSS colours (hex or `rgb()`). */
|
|
@@ -12,7 +10,8 @@ export interface MountOptions {
|
|
|
12
10
|
/** A module worker whose script calls `runWorker(self)`; the terminal
|
|
13
11
|
* owns it from here. */
|
|
14
12
|
worker: Worker;
|
|
15
|
-
/** 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. */
|
|
16
15
|
canvas: HTMLCanvasElement;
|
|
17
16
|
/** The element whose size sets the grid. */
|
|
18
17
|
container: HTMLElement;
|
|
@@ -26,14 +25,21 @@ export interface MountOptions {
|
|
|
26
25
|
font: string;
|
|
27
26
|
/** Read again on every report. */
|
|
28
27
|
colors: () => Colors;
|
|
29
|
-
/** The colour scheme reported to the session
|
|
30
|
-
* 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. */
|
|
31
32
|
scheme?: () => Scheme;
|
|
32
33
|
/** A line of text for each change, for display. */
|
|
33
34
|
onStatus?: (text: string) => void;
|
|
34
35
|
/** Each change of the link to the session, to switch on. */
|
|
35
36
|
onLink?: (link: Link) => void;
|
|
36
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;
|
|
37
43
|
}
|
|
38
44
|
export interface CreateOptions {
|
|
39
45
|
/** `keep`: the session stays listed as `exited` when its program
|
|
@@ -55,7 +61,13 @@ export interface MountedTerminal {
|
|
|
55
61
|
/** Removes the attached session, which must be `exited` or `restored`
|
|
56
62
|
* (else `running`); the link ends with `removed`. */
|
|
57
63
|
remove(): Promise<void>;
|
|
58
|
-
/**
|
|
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. */
|
|
59
71
|
report(): void;
|
|
60
72
|
paste(text: string): void;
|
|
61
73
|
/** Removes the listeners and terminates the worker, which ends its
|
package/dist/client/mount.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CanvasRenderer } from "../renderer/index.js";
|
|
2
2
|
import { ClientError } from "./connection.js";
|
|
3
|
-
import {
|
|
4
|
-
import { profileFrom } from "./profile.js";
|
|
3
|
+
import { Typing } from "./keyboard.js";
|
|
4
|
+
import { profileFrom, schemeOf } from "./profile.js";
|
|
5
5
|
/** Cell metrics from the font. */
|
|
6
6
|
function measure(font) {
|
|
7
7
|
const ctx = document.createElement("canvas").getContext("2d");
|
|
@@ -23,7 +23,7 @@ function measure(font) {
|
|
|
23
23
|
export function mountTerminal(options) {
|
|
24
24
|
const { worker, canvas, container, colors } = options;
|
|
25
25
|
const light = matchMedia("(prefers-color-scheme: light)");
|
|
26
|
-
const scheme = options.scheme ?? (() => (light.matches
|
|
26
|
+
const scheme = options.scheme ?? (() => schemeOf(getComputedStyle(canvas).colorScheme, light.matches));
|
|
27
27
|
const metrics = measure(options.font);
|
|
28
28
|
const ctx = canvas.getContext("2d");
|
|
29
29
|
const renderer = new CanvasRenderer(ctx, metrics, { cursor: colors().cursor });
|
|
@@ -79,6 +79,42 @@ export function mountTerminal(options) {
|
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
const failure = (error) => error.kind ? new ClientError(error.kind, error.message, error.server) : new Error(error.message);
|
|
82
|
+
/** The selection's text, for a copy. */
|
|
83
|
+
let selected;
|
|
84
|
+
/** The clipboard write a release started, waiting for the worker to say
|
|
85
|
+
* what was selected. */
|
|
86
|
+
let copying;
|
|
87
|
+
/** Starts the copy of what a release selected. Safari writes to the
|
|
88
|
+
* clipboard only within the user's gesture, which is over before the
|
|
89
|
+
* worker answers: the write starts here with the text to come, and
|
|
90
|
+
* fails, leaving the clipboard alone, when nothing was selected. */
|
|
91
|
+
function startCopy() {
|
|
92
|
+
copying?.reject();
|
|
93
|
+
copying = undefined;
|
|
94
|
+
if (typeof ClipboardItem === "undefined" || !navigator.clipboard?.write)
|
|
95
|
+
return;
|
|
96
|
+
let settle;
|
|
97
|
+
const text = new Promise((resolve, reject) => {
|
|
98
|
+
settle = {
|
|
99
|
+
resolve: (value) => resolve(new Blob([value], { type: "text/plain" })),
|
|
100
|
+
reject: () => reject(new Error("nothing selected")),
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
navigator.clipboard.write([new ClipboardItem({ "text/plain": text })]).catch(() => { });
|
|
104
|
+
const timer = setTimeout(() => settle.reject(), 2000);
|
|
105
|
+
copying = {
|
|
106
|
+
resolve: (value) => {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
settle.resolve(value);
|
|
109
|
+
},
|
|
110
|
+
reject: () => {
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
settle.reject();
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/** Rows the view is scrolled back, as `onScroll` last reported. */
|
|
117
|
+
let scrolled = 0;
|
|
82
118
|
worker.onmessage = (e) => {
|
|
83
119
|
const message = e.data;
|
|
84
120
|
switch (message.type) {
|
|
@@ -97,7 +133,14 @@ export function mountTerminal(options) {
|
|
|
97
133
|
waiter?.resolve(message.session);
|
|
98
134
|
break;
|
|
99
135
|
}
|
|
100
|
-
case "frame":
|
|
136
|
+
case "frame": {
|
|
137
|
+
if (message.frame.cursor)
|
|
138
|
+
cursorAt = { x: message.frame.cursor.x, y: message.frame.cursor.y };
|
|
139
|
+
const { total, offset, len } = message.frame.scrollbar;
|
|
140
|
+
if (total - offset - len !== scrolled) {
|
|
141
|
+
scrolled = total - offset - len;
|
|
142
|
+
options.onScroll?.(scrolled);
|
|
143
|
+
}
|
|
101
144
|
fitCanvas(message.frame.cols, message.frame.rows);
|
|
102
145
|
if (fullNext && !message.frame.full) {
|
|
103
146
|
// The backing store was recreated; a partial frame cannot fill it.
|
|
@@ -108,33 +151,257 @@ export function mountTerminal(options) {
|
|
|
108
151
|
renderer.draw(message.frame);
|
|
109
152
|
}
|
|
110
153
|
break;
|
|
154
|
+
}
|
|
111
155
|
case "sessions":
|
|
112
156
|
options.onSessions?.(message.sessions, message.current);
|
|
113
157
|
break;
|
|
114
158
|
case "clipboard":
|
|
115
159
|
navigator.clipboard?.writeText(message.text).catch(() => { });
|
|
116
160
|
break;
|
|
161
|
+
case "selection": {
|
|
162
|
+
selected = message.text;
|
|
163
|
+
if (!message.copy)
|
|
164
|
+
break;
|
|
165
|
+
const copy = copying;
|
|
166
|
+
copying = undefined;
|
|
167
|
+
if (message.text === undefined)
|
|
168
|
+
copy?.reject();
|
|
169
|
+
else if (copy)
|
|
170
|
+
copy.resolve(message.text);
|
|
171
|
+
else
|
|
172
|
+
navigator.clipboard?.writeText(message.text).catch(() => { });
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
117
175
|
}
|
|
118
176
|
};
|
|
119
|
-
for
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
177
|
+
/** Where the input element goes for a composition: the cursor's cell,
|
|
178
|
+
* as the latest frame that had the cursor in view placed it. */
|
|
179
|
+
let cursorAt = { x: 0, y: 0 };
|
|
180
|
+
// An input method composes only into an editable element: a textarea
|
|
181
|
+
// after the canvas takes the focus, invisible but at the cursor so the
|
|
182
|
+
// input method's window opens there, and shows the composition while
|
|
183
|
+
// there is one.
|
|
184
|
+
const input = document.createElement("textarea");
|
|
185
|
+
input.setAttribute("aria-label", "Terminal input");
|
|
186
|
+
input.setAttribute("autocapitalize", "off");
|
|
187
|
+
input.setAttribute("autocomplete", "off");
|
|
188
|
+
input.setAttribute("autocorrect", "off");
|
|
189
|
+
input.spellcheck = false;
|
|
190
|
+
input.tabIndex = -1;
|
|
191
|
+
const hidden = { opacity: "0", width: "1px", height: "1px" };
|
|
192
|
+
Object.assign(input.style, {
|
|
193
|
+
...hidden,
|
|
194
|
+
position: "absolute",
|
|
195
|
+
margin: "0",
|
|
196
|
+
padding: "0",
|
|
197
|
+
border: "0",
|
|
198
|
+
outline: "none",
|
|
199
|
+
resize: "none",
|
|
200
|
+
overflow: "hidden",
|
|
201
|
+
whiteSpace: "pre",
|
|
202
|
+
pointerEvents: "none",
|
|
203
|
+
font: metrics.font,
|
|
204
|
+
lineHeight: `${metrics.cellHeight}px`,
|
|
205
|
+
caretColor: "transparent",
|
|
206
|
+
});
|
|
207
|
+
canvas.after(input);
|
|
208
|
+
/** Moves the input element to the cursor, and shows it while composing. */
|
|
209
|
+
function placeInput(composing) {
|
|
210
|
+
// Its position is against its containing block: a positioned
|
|
211
|
+
// ancestor, or else the document.
|
|
212
|
+
const cell = canvas.getBoundingClientRect();
|
|
213
|
+
const parent = input.offsetParent;
|
|
214
|
+
let left = cell.left + window.scrollX;
|
|
215
|
+
let top = cell.top + window.scrollY;
|
|
216
|
+
if (parent && getComputedStyle(parent).position !== "static") {
|
|
217
|
+
const box = parent.getBoundingClientRect();
|
|
218
|
+
left = cell.left - box.left - parent.clientLeft + parent.scrollLeft;
|
|
219
|
+
top = cell.top - box.top - parent.clientTop + parent.scrollTop;
|
|
220
|
+
}
|
|
221
|
+
input.style.left = `${left + cursorAt.x * metrics.cellWidth}px`;
|
|
222
|
+
input.style.top = `${top + cursorAt.y * metrics.cellHeight}px`;
|
|
223
|
+
if (!composing) {
|
|
224
|
+
Object.assign(input.style, hidden);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const { fg, bg } = colors();
|
|
228
|
+
Object.assign(input.style, {
|
|
229
|
+
opacity: "1",
|
|
230
|
+
width: `${Math.max(1, grid.cols - cursorAt.x) * metrics.cellWidth}px`,
|
|
231
|
+
height: `${metrics.cellHeight}px`,
|
|
232
|
+
color: fg,
|
|
233
|
+
background: bg,
|
|
234
|
+
});
|
|
126
235
|
}
|
|
127
|
-
canvas.addEventListener("
|
|
236
|
+
canvas.addEventListener("focus", () => input.focus({ preventScroll: true }), { signal });
|
|
237
|
+
const typing = new Typing(keyboard);
|
|
238
|
+
const typed = (what) => send("text" in what ? { type: "text", text: what.text } : { type: "key", input: what.key });
|
|
239
|
+
input.addEventListener("keydown", (e) => {
|
|
240
|
+
// Ctrl+Shift+C copies a selection, where Ctrl+C is the terminal's;
|
|
241
|
+
// without one it is a key.
|
|
242
|
+
if (e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey && e.code === "KeyC" && selected) {
|
|
243
|
+
navigator.clipboard?.writeText(selected).catch(() => { });
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const key = typing.keydown(e);
|
|
248
|
+
if (key) {
|
|
249
|
+
send({ type: "key", input: key });
|
|
250
|
+
e.preventDefault();
|
|
251
|
+
}
|
|
252
|
+
else if (e.isComposing || e.key === "Dead" || e.keyCode === 229) {
|
|
253
|
+
// At the cursor before the input method opens its window; shown
|
|
254
|
+
// once a composition starts.
|
|
255
|
+
placeInput(false);
|
|
256
|
+
}
|
|
257
|
+
}, { signal });
|
|
258
|
+
input.addEventListener("keyup", (e) => {
|
|
259
|
+
const key = typing.keyup(e);
|
|
260
|
+
if (!key)
|
|
261
|
+
return;
|
|
262
|
+
send({ type: "key", input: key });
|
|
263
|
+
e.preventDefault();
|
|
264
|
+
}, { signal });
|
|
265
|
+
input.addEventListener("beforeinput", (e) => {
|
|
266
|
+
const what = typing.beforeinput(e);
|
|
267
|
+
if (!what)
|
|
268
|
+
return;
|
|
269
|
+
typed(what);
|
|
270
|
+
e.preventDefault();
|
|
271
|
+
}, { signal });
|
|
272
|
+
input.addEventListener("compositionstart", () => placeInput(true), { signal });
|
|
273
|
+
input.addEventListener("blur", () => placeInput(false), { signal });
|
|
274
|
+
input.addEventListener("compositionend", (e) => {
|
|
275
|
+
const what = typing.compositionend(e.data);
|
|
276
|
+
if (what)
|
|
277
|
+
typed(what);
|
|
278
|
+
placeInput(false);
|
|
279
|
+
// Emptied once the composition has left it.
|
|
280
|
+
setTimeout(() => (input.value = ""));
|
|
281
|
+
}, { signal });
|
|
282
|
+
input.addEventListener("input", (e) => {
|
|
283
|
+
if (!e.isComposing)
|
|
284
|
+
input.value = "";
|
|
285
|
+
}, { signal });
|
|
286
|
+
input.addEventListener("paste", (e) => {
|
|
128
287
|
const text = e.clipboardData?.getData("text");
|
|
129
288
|
if (text)
|
|
130
289
|
send({ type: "paste", text });
|
|
131
290
|
e.preventDefault();
|
|
132
291
|
}, { signal });
|
|
133
|
-
|
|
134
|
-
|
|
292
|
+
// The browser's copy (Cmd+C, Ctrl+C in a menu) copies the selection.
|
|
293
|
+
// WebKit offers Copy on an empty textarea only when `beforecopy` is
|
|
294
|
+
// cancelled.
|
|
295
|
+
input.addEventListener("beforecopy", (e) => {
|
|
296
|
+
if (selected !== undefined)
|
|
297
|
+
e.preventDefault();
|
|
298
|
+
}, { signal });
|
|
299
|
+
input.addEventListener("copy", (e) => {
|
|
300
|
+
if (selected === undefined || !e.clipboardData)
|
|
301
|
+
return;
|
|
302
|
+
e.clipboardData.setData("text/plain", selected);
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
}, { signal });
|
|
305
|
+
/** The pointer in cells of the grid, fractions included, kept inside it. */
|
|
306
|
+
function cellAt(e) {
|
|
307
|
+
const rect = canvas.getBoundingClientRect();
|
|
308
|
+
const inside = (value, cells) => Math.min(Math.max(value, 0), Math.max(cells - 0.01, 0));
|
|
309
|
+
return {
|
|
310
|
+
x: inside((e.clientX - rect.left) / metrics.cellWidth, grid.cols),
|
|
311
|
+
y: inside((e.clientY - rect.top) / metrics.cellHeight, grid.rows),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
// Selection: press, drag (once a frame) and release, followed outside
|
|
315
|
+
// the canvas until the button is up. A release the page never saw (the
|
|
316
|
+
// button let go over another window) ends the drag at the next move or
|
|
317
|
+
// press.
|
|
318
|
+
let endDrag;
|
|
319
|
+
canvas.addEventListener("mousedown", (e) => {
|
|
320
|
+
e.preventDefault();
|
|
321
|
+
input.focus({ preventScroll: true });
|
|
322
|
+
if (e.button !== 0)
|
|
323
|
+
return;
|
|
324
|
+
endDrag?.(cellAt(e), e.timeStamp);
|
|
325
|
+
send({ type: "select", action: "press", at: cellAt(e), time: e.timeStamp });
|
|
326
|
+
let pending;
|
|
327
|
+
let frame = 0;
|
|
328
|
+
const flush = () => {
|
|
329
|
+
cancelAnimationFrame(frame);
|
|
330
|
+
frame = 0;
|
|
331
|
+
if (pending)
|
|
332
|
+
send({ type: "select", action: "drag", at: pending, time: 0 });
|
|
333
|
+
pending = undefined;
|
|
334
|
+
};
|
|
335
|
+
const end = (at, time) => {
|
|
336
|
+
flush();
|
|
337
|
+
window.removeEventListener("mousemove", move);
|
|
338
|
+
window.removeEventListener("mouseup", up);
|
|
339
|
+
endDrag = undefined;
|
|
340
|
+
startCopy();
|
|
341
|
+
send({ type: "select", action: "release", at, time });
|
|
342
|
+
};
|
|
343
|
+
const move = (m) => {
|
|
344
|
+
if ((m.buttons & 1) === 0)
|
|
345
|
+
return end(cellAt(m), m.timeStamp);
|
|
346
|
+
pending = cellAt(m);
|
|
347
|
+
frame ||= requestAnimationFrame(flush);
|
|
348
|
+
};
|
|
349
|
+
const up = (u) => {
|
|
350
|
+
if (u.button === 0)
|
|
351
|
+
end(cellAt(u), u.timeStamp);
|
|
352
|
+
};
|
|
353
|
+
endDrag = end;
|
|
354
|
+
window.addEventListener("mousemove", move, { signal });
|
|
355
|
+
window.addEventListener("mouseup", up, { signal });
|
|
356
|
+
}, { signal });
|
|
357
|
+
// The wheel scrolls the history in whole rows; what a trackpad sends in
|
|
358
|
+
// pixels adds up until it makes one. Ctrl+wheel stays the browser's zoom.
|
|
359
|
+
let wheeled = 0;
|
|
360
|
+
canvas.addEventListener("wheel", (e) => {
|
|
361
|
+
if (e.ctrlKey)
|
|
362
|
+
return;
|
|
363
|
+
e.preventDefault();
|
|
364
|
+
const rows = e.deltaMode === WheelEvent.DOM_DELTA_LINE
|
|
365
|
+
? e.deltaY
|
|
366
|
+
: e.deltaMode === WheelEvent.DOM_DELTA_PAGE
|
|
367
|
+
? e.deltaY * grid.rows
|
|
368
|
+
: e.deltaY / metrics.cellHeight;
|
|
369
|
+
wheeled += rows;
|
|
370
|
+
const whole = Math.trunc(wheeled);
|
|
371
|
+
if (whole === 0)
|
|
372
|
+
return;
|
|
373
|
+
wheeled -= whole;
|
|
374
|
+
send({ type: "scroll", rows: whole });
|
|
375
|
+
}, { passive: false, signal });
|
|
376
|
+
/** The profile last reported, to tell when it changed. */
|
|
377
|
+
let reported = "";
|
|
378
|
+
const profile = () => {
|
|
379
|
+
const current = profileFrom(colors(), scheme());
|
|
380
|
+
reported = JSON.stringify(current);
|
|
381
|
+
return current;
|
|
382
|
+
};
|
|
383
|
+
/** Whether the user is looking at this page: it is visible and its
|
|
384
|
+
* window has the focus. Only then does the session get its size, so
|
|
385
|
+
* of several clients the one in use sets it. */
|
|
386
|
+
const active = () => document.visibilityState === "visible" && document.hasFocus();
|
|
387
|
+
const report = () => send({ type: "viewport", size: viewport(), profile: profile(), active: active() });
|
|
135
388
|
const resizes = new ResizeObserver(report);
|
|
136
389
|
resizes.observe(container);
|
|
137
390
|
light.addEventListener("change", report, { signal });
|
|
391
|
+
// Shown or focused: the session takes this page's size again; hidden or
|
|
392
|
+
// blurred: the worker stops reporting.
|
|
393
|
+
document.addEventListener("visibilitychange", report, { signal });
|
|
394
|
+
window.addEventListener("focus", report, { signal });
|
|
395
|
+
window.addEventListener("blur", report, { signal });
|
|
396
|
+
// A page pins its scheme (and its colours with it) with an attribute or
|
|
397
|
+
// a class on the canvas or an ancestor, which no event announces.
|
|
398
|
+
const restyles = new MutationObserver(() => {
|
|
399
|
+
if (JSON.stringify(profileFrom(colors(), scheme())) !== reported)
|
|
400
|
+
report();
|
|
401
|
+
});
|
|
402
|
+
for (let element = canvas; element; element = element.parentElement) {
|
|
403
|
+
restyles.observe(element, { attributes: true });
|
|
404
|
+
}
|
|
138
405
|
// Zoom or a move to another screen changes the pixel ratio: the canvas
|
|
139
406
|
// is refitted on the next frame; the cell size in pixels is re-reported.
|
|
140
407
|
const watchRatio = () => {
|
|
@@ -150,7 +417,8 @@ export function mountTerminal(options) {
|
|
|
150
417
|
wasm: new URL(options.wasm, location.href).toString(),
|
|
151
418
|
url: options.url,
|
|
152
419
|
viewport: viewport(),
|
|
153
|
-
profile:
|
|
420
|
+
profile: profile(),
|
|
421
|
+
active: active(),
|
|
154
422
|
session: options.session,
|
|
155
423
|
});
|
|
156
424
|
return {
|
|
@@ -161,12 +429,16 @@ export function mountTerminal(options) {
|
|
|
161
429
|
},
|
|
162
430
|
end: async () => void (await request((id) => ({ type: "end", id }))),
|
|
163
431
|
remove: async () => void (await request((id) => ({ type: "remove", id }))),
|
|
432
|
+
focus: () => input.focus({ preventScroll: true }),
|
|
433
|
+
selection: () => selected,
|
|
164
434
|
report,
|
|
165
435
|
paste: (text) => send({ type: "paste", text }),
|
|
166
436
|
dispose: () => {
|
|
167
437
|
disposed = true;
|
|
168
438
|
listening.abort();
|
|
169
439
|
resizes.disconnect();
|
|
440
|
+
restyles.disconnect();
|
|
441
|
+
input.remove();
|
|
170
442
|
worker.onmessage = null;
|
|
171
443
|
worker.terminate();
|
|
172
444
|
for (const waiter of pending.values())
|
package/dist/client/profile.d.ts
CHANGED
|
@@ -2,6 +2,10 @@ import type { Profile, Scheme } from "../proto/index.ts";
|
|
|
2
2
|
/** `#rrggbb` from a CSS colour as a custom property holds it (hex, or
|
|
3
3
|
* `rgb()` when the stylesheet writes it that way); undefined otherwise. */
|
|
4
4
|
export declare function hexColor(value: string): string | undefined;
|
|
5
|
+
/** The scheme of an element whose computed `color-scheme` is
|
|
6
|
+
* `colorScheme`: the one it names when it names one of `light` and
|
|
7
|
+
* `dark`, else (`normal`, or both) the page's preference. */
|
|
8
|
+
export declare function schemeOf(colorScheme: string, prefersLight: boolean): Scheme;
|
|
5
9
|
/** The profile a page with these colours and scheme reports. */
|
|
6
10
|
export declare function profileFrom(colors: {
|
|
7
11
|
fg: string;
|
package/dist/client/profile.js
CHANGED
|
@@ -14,6 +14,17 @@ export function hexColor(value) {
|
|
|
14
14
|
const channel = (s) => Math.min(255, Number(s)).toString(16).padStart(2, "0");
|
|
15
15
|
return `#${channel(rgb[1])}${channel(rgb[2])}${channel(rgb[3])}`;
|
|
16
16
|
}
|
|
17
|
+
/** The scheme of an element whose computed `color-scheme` is
|
|
18
|
+
* `colorScheme`: the one it names when it names one of `light` and
|
|
19
|
+
* `dark`, else (`normal`, or both) the page's preference. */
|
|
20
|
+
export function schemeOf(colorScheme, prefersLight) {
|
|
21
|
+
const names = colorScheme.split(/\s+/);
|
|
22
|
+
const light = names.includes("light");
|
|
23
|
+
const dark = names.includes("dark");
|
|
24
|
+
if (light !== dark)
|
|
25
|
+
return light ? "light" : "dark";
|
|
26
|
+
return prefersLight ? "light" : "dark";
|
|
27
|
+
}
|
|
17
28
|
/** The profile a page with these colours and scheme reports. */
|
|
18
29
|
export function profileFrom(colors, scheme) {
|
|
19
30
|
const profile = { scheme };
|