Files

54 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

2023-11-03 11:45:21 -07:00
## @xterm/addon-clipboard
2023-09-20 14:33:09 -04:00
2024-04-08 04:51:55 +03:00
An addon for [xterm.js](https://github.com/xtermjs/xterm.js) that enables
accessing the system clipboard. This addon requires xterm.js v4+.
2023-09-20 14:33:09 -04:00
### Install
```bash
2023-11-03 11:45:21 -07:00
npm install --save @xterm/addon-clipboard
2023-09-20 14:33:09 -04:00
```
### Usage
```ts
import { Terminal } from 'xterm';
2023-11-03 11:45:21 -07:00
import { ClipboardAddon } from '@xterm/addon-clipboard';
2023-09-20 14:33:09 -04:00
const terminal = new Terminal();
const clipboardAddon = new ClipboardAddon();
terminal.loadAddon(clipboardAddon);
```
To use a custom clipboard provider
```ts
2024-04-08 04:51:55 +03:00
import { Terminal } from '@xterm/xterm';
import { ClipboardAddon, IClipboardProvider, ClipboardSelectionType } from '@xterm/addon-clipboard';
2023-09-20 14:33:09 -04:00
function b64Encode(data: string): string {
// Base64 encode impl
}
function b64Decode(data: string): string {
// Base64 decode impl
}
class MyCustomClipboardProvider implements IClipboardProvider {
private _data: string
2024-04-08 04:51:55 +03:00
public readText(selection: ClipboardSelectionType): Promise<string> {
2023-09-20 14:33:09 -04:00
return Promise.resolve(b64Encode(this._data));
}
2024-04-08 04:51:55 +03:00
public writeText(selection: ClipboardSelectionType, data: string): Promise<void> {
2023-09-20 14:33:09 -04:00
this._data = b64Decode(data);
return Promise.resolve();
}
}
const terminal = new Terminal();
const clipboardAddon = new ClipboardAddon(new MyCustomClipboardProvider());
terminal.loadAddon(clipboardAddon);
```
2023-11-03 11:45:21 -07:00
See the full [API](https://github.com/xtermjs/xterm.js/blob/master/addons/addon-clipboard/typings/addon-clipboard.d.ts) for more advanced usage.