|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 The xterm.js authors. All rights reserved. |
| 3 | + * @license MIT |
| 4 | + */ |
| 5 | + |
| 6 | +import type { IDisposable, ITerminalAddon, Terminal } from '@xterm/xterm'; |
| 7 | +import { type IClipboardProvider, ClipboardSelectionType, type IBase64 } from '@xterm/addon-clipboard'; |
| 8 | +import { Base64 as JSBase64 } from 'js-base64'; |
| 9 | + |
| 10 | +export 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 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export class BrowserClipboardProvider implements IClipboardProvider { |
| 73 | + public async readText(selection: ClipboardSelectionType): Promise<string> { |
| 74 | + if (selection !== 'c') { |
| 75 | + return Promise.resolve(''); |
| 76 | + } |
| 77 | + return navigator.clipboard.readText(); |
| 78 | + } |
| 79 | + |
| 80 | + public async writeText(selection: ClipboardSelectionType, text: string): Promise<void> { |
| 81 | + if (selection !== 'c') { |
| 82 | + return Promise.resolve(); |
| 83 | + } |
| 84 | + return navigator.clipboard.writeText(text); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export class Base64 implements IBase64 { |
| 89 | + public encodeText(data: string): string { |
| 90 | + return JSBase64.encode(data); |
| 91 | + } |
| 92 | + public decodeText(data: string): string { |
| 93 | + const text = JSBase64.decode(data); |
| 94 | + if (!JSBase64.isValid(data) || JSBase64.encode(text) !== data) { |
| 95 | + return ''; |
| 96 | + } |
| 97 | + return text; |
| 98 | + } |
| 99 | +} |
0 commit comments