MCPcopy
hub / github.com/xtermjs/xterm.js / ClipboardAddon

Class ClipboardAddon

addons/addon-clipboard/src/ClipboardAddon.ts:10–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import { Base64 as JSBase64 } from 'js-base64';
9
10export class ClipboardAddon implements ITerminalAddon {
11 private _terminal?: Terminal;
12 private _disposable?: IDisposable;
13
14 constructor(
15 private _base64: IBase64 = new Base64(),
16 private _provider: IClipboardProvider = new BrowserClipboardProvider()
17 ) {}
18
19 public activate(terminal: Terminal): void {
20 this._terminal = terminal;
21 this._disposable = terminal.parser.registerOscHandler(52, data => this._setOrReportClipboard(data));
22 }
23
24 public dispose(): void {
25 return this._disposable?.dispose();
26 }
27
28 private _readText(sel: ClipboardSelectionType, data: string): void {
29 const b64 = this._base64.encodeText(data);
30 this._terminal?.input(`\x1b]52;${sel};${b64}\x07`, false);
31 }
32
33 private _setOrReportClipboard(data: string): boolean | Promise<boolean> {
34 const args = data.split(';');
35 if (args.length < 2) {
36 return true;
37 }
38
39 const pc = args[0] as ClipboardSelectionType;
40 const pd = args[1];
41 if (pd === '?') {
42 const text = this._provider.readText(pc);
43
44 // Report clipboard
45 if (text instanceof Promise) {
46 return text.then((data) => {
47 this._readText(pc, data);
48 return true;
49 });
50 }
51
52 this._readText(pc, text);
53 return true;
54 }
55
56 // Clear clipboard if text is not a base64 encoded string.
57 let text = '';
58 try {
59 text = this._base64.decodeText(pd);
60 } catch {}
61
62
63 const result = this._provider.writeText(pc, text);
64 if (result instanceof Promise) {
65 return result.then(() => true);
66 }
67

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected