xterm-addon-clipboard to scoped module
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { ClipboardProvider } from './ClipboardProvider';
|
||||
import { IClipboardProvider, IDisposable, ITerminalAddon, Terminal } from '@xterm/xterm';
|
||||
|
||||
export class ClipboardAddon implements ITerminalAddon {
|
||||
private _disposable: IDisposable | undefined;
|
||||
constructor(private _provider: IClipboardProvider = new ClipboardProvider()) {}
|
||||
|
||||
public activate(terminal: Terminal): void {
|
||||
this._disposable = terminal.registerClipboardProvider(this._provider);
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
return this._disposable?.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import { Base64 } from 'js-base64';
|
||||
import { ClipboardSelectionType, IClipboardProvider } from '@xterm/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: ClipboardSelectionType): Promise<string> {
|
||||
if (selection !== 'c') {
|
||||
return Promise.resolve('');
|
||||
}
|
||||
return navigator.clipboard.readText().then((text) =>
|
||||
Base64.encode(text));
|
||||
}
|
||||
public writeText(selection: ClipboardSelectionType, 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": "es2021",
|
||||
"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