Add support to ANSI OSC52
Add support to ANSI OSC52 sequence to manipulate selection and clipboard data. The sequence specs supports multiple clipboard selections but we only support the common ones, system and primary clipboard selections. This adds a new event listener to the common terminal module `onClipboard` to allow external implementations to hook into it. The addon uses the browser Clipboard API to read/write from and to the clipboard. The default `ClipboardProvider` uses the browser Clipboard API. This means it only supports read/write to and from the system clipboard. Reference: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands Fixes: xtermjs#3260 Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { ClipboardProvider } from './ClipboardProvider';
|
||||
import { IClipboardProvider, ITerminalAddon, Terminal } from 'xterm';
|
||||
|
||||
export class ClipboardAddon implements ITerminalAddon {
|
||||
private _terminal: Terminal | undefined;
|
||||
constructor(private _provider: IClipboardProvider = new ClipboardProvider()) {}
|
||||
|
||||
public activate(terminal: Terminal): void {
|
||||
this._terminal = terminal;
|
||||
terminal.registerClipboardProvider(this._provider);
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
this._terminal?.deregisterClipboardProvider();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { Base64 } from 'js-base64';
|
||||
import { ClipboardSelection, IClipboardProvider } from 'xterm';
|
||||
|
||||
export class ClipboardProvider implements IClipboardProvider {
|
||||
constructor(
|
||||
/**
|
||||
* The maximum amount of data that can be copied to the clipboard.
|
||||
* Zero means no limit.
|
||||
*/
|
||||
public limit = 1000000 // 1MB
|
||||
){}
|
||||
public readText(selection: ClipboardSelection): Promise<string> {
|
||||
if (selection !== 'c') {
|
||||
return Promise.resolve('');
|
||||
}
|
||||
return navigator.clipboard.readText().then((text) =>
|
||||
Base64.encode(text));
|
||||
}
|
||||
public writeText(selection: ClipboardSelection, data: string): Promise<void> {
|
||||
if (selection !== 'c' || (this.limit > 0 && data.length > this.limit)) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
const text = Base64.decode(data);
|
||||
// clear the clipboard if the data is not valid base64
|
||||
if (!Base64.isValid(data) || Base64.encode(text) !== data) {
|
||||
return navigator.clipboard.writeText('');
|
||||
}
|
||||
return navigator.clipboard.writeText(text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "es2017",
|
||||
"sourceMap": true,
|
||||
"outDir": "../out",
|
||||
"rootDir": ".",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"preserveWatchOutput": true,
|
||||
"types": [
|
||||
"../../../node_modules/@types/mocha"
|
||||
],
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"browser/*": [
|
||||
"../../../src/browser/*"
|
||||
],
|
||||
"common/*": [
|
||||
"../../../src/common/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"./**/*",
|
||||
"../../../typings/xterm.d.ts"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../../src/browser"
|
||||
},
|
||||
{
|
||||
"path": "../../../src/common"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user