@gjermundgaraba/clankermux 0.4.1 → 0.6.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.
@@ -1,10 +1,5 @@
1
- /** The page's copies to the clipboard, and what each came to. */
2
- /** How long a copy may take: the worker's answer and the write. */
3
- export const COPY_TIMEOUT_MS = 2000;
4
- /** Characters by code point, as a page counts them for display. */
5
- const length = (text) => [...text].length;
6
- /** One copy under way, reported once: when both its text's length and
7
- * the write's result are known, or at the timeout. */
1
+ const COPY_TIMEOUT_MS = 2000;
2
+ const codePointLength = (text) => [...text].length;
8
3
  class Attempt {
9
4
  source;
10
5
  report;
@@ -13,28 +8,23 @@ class Attempt {
13
8
  chars;
14
9
  result;
15
10
  timer;
16
- constructor(source, report, timeoutMs,
17
- /** Once, when it is reported or dropped. */
18
- ended,
19
- /** At the timeout, after the report. */
20
- timedOut) {
11
+ constructor(source, report, ended, timedOut) {
21
12
  this.source = source;
22
13
  this.report = report;
23
14
  this.ended = ended;
24
15
  this.timer = setTimeout(() => {
25
16
  this.finish({ ok: false, reason: "timeout" });
26
17
  timedOut?.();
27
- }, timeoutMs);
18
+ }, COPY_TIMEOUT_MS);
28
19
  }
29
20
  text(text) {
30
- this.chars = length(text);
21
+ this.chars = codePointLength(text);
31
22
  this.settle();
32
23
  }
33
24
  wrote(result) {
34
25
  this.result ??= result;
35
26
  this.settle();
36
27
  }
37
- /** Not a copy: nothing was selected, or a newer copy took its place. */
38
28
  drop() {
39
29
  if (this.done)
40
30
  return;
@@ -56,23 +46,17 @@ class Attempt {
56
46
  else
57
47
  this.report({ source: this.source, ...result, ...(chars !== undefined ? { chars } : {}) });
58
48
  }
59
- /** Follows a write's promise. */
60
49
  follow(write) {
61
50
  write.then(() => this.wrote({ ok: true }), (err) => this.wrote({ ok: false, reason: "refused", message: String(err) }));
62
51
  }
63
52
  }
64
- /** A mounted terminal's copies. */
65
53
  export class Copier {
66
54
  host;
67
- timeoutMs;
68
- /** The releases' copies waiting for the worker's answer, by number. */
69
- pending = new Map();
55
+ pending;
70
56
  next = 1;
71
- /** Every copy not yet reported or dropped, for `dispose`. */
72
57
  live = new Set();
73
- constructor(host, timeoutMs = COPY_TIMEOUT_MS) {
58
+ constructor(host) {
74
59
  this.host = host;
75
- this.timeoutMs = timeoutMs;
76
60
  }
77
61
  /** Starts the copy of what a release selected, and returns its number
78
62
  * for the worker's answer. Safari writes to the clipboard only within
@@ -83,18 +67,18 @@ export class Copier {
83
67
  * release's copy still waiting is dropped, so the clipboard gets the
84
68
  * latest. */
85
69
  start() {
86
- for (const earlier of this.pending.values())
87
- this.dropOne(earlier);
88
- this.pending.clear();
70
+ if (this.pending)
71
+ this.dropOne(this.pending);
89
72
  const id = this.next++;
90
73
  const pending = {
91
- // At the timeout the write is left without its text.
74
+ id,
92
75
  attempt: this.attempt("release", () => {
93
- this.pending.delete(id);
76
+ if (this.pending === pending)
77
+ this.pending = undefined;
94
78
  pending.text?.reject();
95
79
  }),
96
80
  };
97
- this.pending.set(id, pending);
81
+ this.pending = pending;
98
82
  const clipboard = this.host.clipboard();
99
83
  if (!this.host.item || !clipboard?.write)
100
84
  return id;
@@ -120,14 +104,11 @@ export class Copier {
120
104
  pending.attempt.follow(clipboard.write([item]));
121
105
  return id;
122
106
  }
123
- /** The worker's answer to release `id`: the selected text, or undefined
124
- * when nothing was selected, which is no copy. An answer to a copy that
125
- * was dropped or timed out is ignored. */
126
107
  answer(id, text) {
127
- const pending = this.pending.get(id);
128
- if (!pending)
108
+ const pending = this.pending;
109
+ if (pending?.id !== id)
129
110
  return;
130
- this.pending.delete(id);
111
+ this.pending = undefined;
131
112
  if (text === undefined)
132
113
  return this.dropOne(pending);
133
114
  pending.attempt.text(text);
@@ -136,26 +117,23 @@ export class Copier {
136
117
  else
137
118
  this.writeText(pending.attempt, text);
138
119
  }
139
- /** Copies `text` known now. */
140
120
  write(text, source) {
141
121
  const attempt = this.attempt(source);
142
122
  attempt.text(text);
143
123
  this.writeText(attempt, text);
144
124
  }
145
- /** A copy the browser made itself (the `copy` event's data). */
146
- copied(text, source) {
147
- this.host.report({ ok: true, source, chars: length(text) });
125
+ copied(text) {
126
+ this.host.report({ ok: true, source: "browser", chars: codePointLength(text) });
148
127
  }
149
- /** Drops every copy under way, unreported. */
150
128
  dispose() {
151
- for (const pending of this.pending.values())
152
- this.dropOne(pending);
153
- this.pending.clear();
129
+ if (this.pending)
130
+ this.dropOne(this.pending);
131
+ this.pending = undefined;
154
132
  for (const attempt of this.live)
155
133
  attempt.drop();
156
134
  }
157
135
  attempt(source, timedOut) {
158
- const attempt = new Attempt(source, this.host.report, this.timeoutMs, () => this.live.delete(attempt), timedOut);
136
+ const attempt = new Attempt(source, this.host.report, () => this.live.delete(attempt), timedOut);
159
137
  this.live.add(attempt);
160
138
  return attempt;
161
139
  }
@@ -1,12 +1,7 @@
1
- export { Attachment } from "./attachment.ts";
2
- export type { Action } from "./attachment.ts";
3
1
  export { Client, connect } from "./client.ts";
4
2
  export type { ConnectOptions, EventGap, SendInput, WaitOptions } from "./client.ts";
5
- export { ClientError, Connection, hello } from "./connection.ts";
6
- export type { ErrorKind, Transport } from "./connection.ts";
3
+ export { ClientError } from "./connection.ts";
7
4
  export type { Copy, CopyFailure, CopySource } from "./copy.ts";
8
- export { Osc52Scanner } from "./osc52.ts";
9
- export type { ClipboardWrite } from "./osc52.ts";
10
5
  export type { Colors, MountOptions, Terminal } from "./mount.ts";
11
6
  export type { Link, LinkEnd } from "./worker.ts";
12
7
  export type { Acked, Attention, AttachmentInfo, Catalog, CreateParams, Description, ErrorCode, Event, Exit, MethodInfo, Methods, OnExit, Page, Peer, Read, ReadFormat, ReadScope, Scheme, Sent, Session, SizePolicy, StatusRecord, StreamCursor, Tail, Until, Waited, } from "../proto/index.ts";
@@ -1,4 +1,2 @@
1
- export { Attachment } from "./attachment.js";
2
1
  export { Client, connect } from "./client.js";
3
- export { ClientError, Connection, hello } from "./connection.js";
4
- export { Osc52Scanner } from "./osc52.js";
2
+ export { ClientError } from "./connection.js";
@@ -1,5 +1,4 @@
1
1
  import { type KeyInput } from "../engine/index.ts";
2
- /** The parts of a `KeyboardEvent` the mapping reads. */
3
2
  export interface KeyEventLike {
4
3
  type: string;
5
4
  code: string;
@@ -10,56 +9,31 @@ export interface KeyEventLike {
10
9
  altKey: boolean;
11
10
  metaKey: boolean;
12
11
  isComposing: boolean;
13
- /** 229 while an input method processes the key. */
14
12
  keyCode: number;
15
13
  }
16
- /** The parts of an `InputEvent` the input element reads. */
17
14
  export interface InputEventLike {
18
15
  inputType: string;
19
16
  data: string | null;
20
17
  isComposing: boolean;
21
18
  }
22
- /** What typing comes to: a key for the key encoder, or text as typed. */
23
19
  export type Typed = {
24
20
  key: KeyInput;
25
21
  } | {
26
22
  text: string;
27
23
  };
28
- /** What the page knows about the keyboard: the layout's unmodified
29
- * character per `code` when the browser exposes it, and the platform. */
30
24
  export interface Keyboard {
31
25
  layout: ReadonlyMap<string, string> | undefined;
32
26
  mac: boolean;
33
27
  }
34
- /** Whether the browser keeps the key: its own shortcuts with the platform's
35
- * command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
36
- * the terminal's. */
37
28
  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
29
  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
30
  export declare class Typing {
50
31
  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;
32
+ private readonly pressesSent;
54
33
  constructor(keyboard: Keyboard);
55
- /** A key to send, or undefined to leave the event to the browser. */
56
34
  keydown(e: KeyEventLike): KeyInput | undefined;
57
35
  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
36
  beforeinput(e: InputEventLike): Typed | undefined;
62
- /** `compositionend`: the composed text. */
63
37
  compositionend(data: string | null): Typed | undefined;
64
38
  }
65
39
  export declare function keyInput(e: KeyEventLike, keyboard: Keyboard): KeyInput;
@@ -1,50 +1,34 @@
1
1
  import { isText } from "../engine/index.js";
2
- /** Whether the browser keeps the key: its own shortcuts with the platform's
3
- * command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
4
- * the terminal's. */
2
+ const IME_PROCESSING_KEY_CODE = 229;
3
+ function isTerminalPasteShortcut(e) {
4
+ return e.ctrlKey && e.shiftKey && e.code === "KeyV";
5
+ }
5
6
  export function browserKeeps(e) {
6
- return e.metaKey || (e.ctrlKey && e.shiftKey && e.code === "KeyV");
7
+ return e.metaKey || isTerminalPasteShortcut(e);
7
8
  }
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
9
  export function composing(e) {
12
- return e.isComposing || e.keyCode === 229 || e.key === "Process" || e.key === "Dead";
10
+ return e.isComposing || e.keyCode === IME_PROCESSING_KEY_CODE || e.key === "Process" || e.key === "Dead";
13
11
  }
14
- /** A key with no modifiers, pressed. */
15
12
  function pressed(code) {
16
13
  return { code, key: code, action: "press", shift: false, ctrl: false, alt: false, meta: false };
17
14
  }
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
15
  export class Typing {
26
16
  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();
17
+ pressesSent = new Set();
30
18
  constructor(keyboard) {
31
19
  this.keyboard = keyboard;
32
20
  }
33
- /** A key to send, or undefined to leave the event to the browser. */
34
21
  keydown(e) {
35
22
  if (browserKeeps(e) || composing(e))
36
23
  return undefined;
37
- this.down.add(e.code);
24
+ this.pressesSent.add(e.code);
38
25
  return keyInput(e, this.keyboard);
39
26
  }
40
27
  keyup(e) {
41
- if (!this.down.delete(e.code))
28
+ if (!this.pressesSent.delete(e.code))
42
29
  return undefined;
43
30
  return keyInput(e, this.keyboard);
44
31
  }
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
32
  beforeinput(e) {
49
33
  if (e.isComposing)
50
34
  return undefined;
@@ -60,7 +44,6 @@ export class Typing {
60
44
  return undefined;
61
45
  }
62
46
  }
63
- /** `compositionend`: the composed text. */
64
47
  compositionend(data) {
65
48
  return data ? { text: data } : undefined;
66
49
  }
@@ -80,13 +63,14 @@ export function keyInput(e, keyboard) {
80
63
  const unshifted = keyboard.layout?.get(e.code);
81
64
  if (unshifted !== undefined)
82
65
  input.unshifted = unshifted;
83
- // Alt composed the text when it differs from the key's own character
84
- // (Option on macOS, AltGr elsewhere). Without a layout to compare
85
- // against, macOS Option composes and other platforms' Alt modifies.
86
- if (e.altKey && isText(e.key)) {
87
- const composed = unshifted !== undefined ? e.key.toLowerCase() !== unshifted.toLowerCase() : keyboard.mac;
88
- if (composed)
89
- input.altComposed = true;
90
- }
66
+ if (altComposedText(e, unshifted, keyboard))
67
+ input.altComposed = true;
91
68
  return input;
92
69
  }
70
+ function altComposedText(e, unshifted, keyboard) {
71
+ if (!e.altKey || !isText(e.key))
72
+ return false;
73
+ if (unshifted === undefined)
74
+ return keyboard.mac;
75
+ return e.key.toLowerCase() !== unshifted.toLowerCase();
76
+ }
@@ -0,0 +1,10 @@
1
+ export type ListenerMode = "replayLatest" | "isolateErrors";
2
+ export declare class Listeners<T> {
3
+ private readonly all;
4
+ private latest;
5
+ private readonly mode;
6
+ constructor(mode?: ListenerMode);
7
+ add(listener: (value: T) => void): () => void;
8
+ emit(value: T): void;
9
+ clear(): void;
10
+ }
@@ -0,0 +1,35 @@
1
+ export class Listeners {
2
+ all = new Set();
3
+ latest;
4
+ mode;
5
+ constructor(mode = "replayLatest") {
6
+ this.mode = mode;
7
+ }
8
+ add(listener) {
9
+ this.all.add(listener);
10
+ if (this.latest)
11
+ listener(this.latest.value);
12
+ return () => void this.all.delete(listener);
13
+ }
14
+ emit(value) {
15
+ if (this.mode === "replayLatest") {
16
+ this.latest = { value };
17
+ for (const listener of this.all)
18
+ listener(value);
19
+ return;
20
+ }
21
+ for (const listener of this.all) {
22
+ try {
23
+ listener(value);
24
+ }
25
+ catch (err) {
26
+ queueMicrotask(() => {
27
+ throw err;
28
+ });
29
+ }
30
+ }
31
+ }
32
+ clear() {
33
+ this.all.clear();
34
+ }
35
+ }
@@ -75,11 +75,16 @@ export interface Terminal {
75
75
  * copy. A listener added later is not told of earlier copies. */
76
76
  onCopy(listener: (copy: Copy) => void): () => void;
77
77
  }
78
- /** The client's side of a terminal: its messages to the worker, and the
79
- * worker's for it. */
80
78
  export interface MountHost {
81
79
  send(message: ToWorker): void;
82
- receive(handler: ((message: ToPage) => void) | undefined): void;
80
+ disposed(): void;
83
81
  }
84
- /** Mounts a terminal as `id` of the client `host` serves. */
85
- export declare function mountOn(host: MountHost, id: number, options: MountOptions): Terminal;
82
+ type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
83
+ export type ToMount = DistributiveOmit<Extract<ToWorker, {
84
+ mount: number;
85
+ }>, "mount">;
86
+ export declare function mountOn(host: MountHost, id: number, options: MountOptions): {
87
+ terminal: Terminal;
88
+ receive: (message: ToPage) => void;
89
+ };
90
+ export {};