From db5bfc75251c0646018123a801903e3fcdb1ddee Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 20 Sep 2023 14:33:09 -0400 Subject: [PATCH 01/18] 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 --- .eslintrc.json | 2 + README.md | 1 + addons/xterm-addon-clipboard/.gitignore | 2 + addons/xterm-addon-clipboard/.npmignore | 29 ++++++ addons/xterm-addon-clipboard/LICENSE | 19 ++++ addons/xterm-addon-clipboard/README.md | 52 +++++++++++ addons/xterm-addon-clipboard/package.json | 29 ++++++ .../src/ClipboardAddon.ts | 21 +++++ .../src/ClipboardProvider.ts | 35 ++++++++ .../xterm-addon-clipboard/src/tsconfig.json | 36 ++++++++ .../test/ClipboardAddon.api.ts | 86 ++++++++++++++++++ .../xterm-addon-clipboard/test/tsconfig.json | 22 +++++ addons/xterm-addon-clipboard/tsconfig.json | 8 ++ .../typings/xterm-addon-clipboard.d.ts | 35 ++++++++ .../xterm-addon-clipboard/webpack.config.js | 31 +++++++ addons/xterm-addon-clipboard/yarn.lock | 8 ++ bin/publish.js | 1 + demo/client.ts | 48 ++++++---- demo/tsconfig.json | 1 + src/browser/Terminal.ts | 26 +++++- src/browser/TestUtils.test.ts | 8 +- src/browser/public/Terminal.ts | 8 +- src/common/InputHandler.test.ts | 88 ++++++++++++++++++- src/common/InputHandler.ts | 60 ++++++++++++- src/common/Types.d.ts | 13 ++- src/common/services/OptionsService.ts | 5 +- src/common/services/Services.ts | 1 + test/api/TestUtils.ts | 5 +- test/playwright/TestUtils.ts | 2 + tsconfig.all.json | 1 + typings/xterm.d.ts | 44 ++++++++++ 31 files changed, 695 insertions(+), 32 deletions(-) create mode 100644 addons/xterm-addon-clipboard/.gitignore create mode 100644 addons/xterm-addon-clipboard/.npmignore create mode 100644 addons/xterm-addon-clipboard/LICENSE create mode 100644 addons/xterm-addon-clipboard/README.md create mode 100644 addons/xterm-addon-clipboard/package.json create mode 100644 addons/xterm-addon-clipboard/src/ClipboardAddon.ts create mode 100644 addons/xterm-addon-clipboard/src/ClipboardProvider.ts create mode 100644 addons/xterm-addon-clipboard/src/tsconfig.json create mode 100644 addons/xterm-addon-clipboard/test/ClipboardAddon.api.ts create mode 100644 addons/xterm-addon-clipboard/test/tsconfig.json create mode 100644 addons/xterm-addon-clipboard/tsconfig.json create mode 100644 addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts create mode 100644 addons/xterm-addon-clipboard/webpack.config.js create mode 100644 addons/xterm-addon-clipboard/yarn.lock diff --git a/.eslintrc.json b/.eslintrc.json index 0ccafafbf4..a6f2d2ed45 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,6 +18,8 @@ "addons/xterm-addon-attach/test/tsconfig.json", "addons/xterm-addon-canvas/src/tsconfig.json", "addons/xterm-addon-canvas/test/tsconfig.json", + "addons/xterm-addon-clipboard/src/tsconfig.json", + "addons/xterm-addon-clipboard/test/tsconfig.json", "addons/xterm-addon-fit/src/tsconfig.json", "addons/xterm-addon-fit/test/tsconfig.json", "addons/xterm-addon-image/src/tsconfig.json", diff --git a/README.md b/README.md index 0abcd32cf5..39cca88acc 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ terminal.loadAddon(new WebLinksAddon()); The xterm.js team maintains the following addons, but anyone can build them: - [`xterm-addon-attach`](https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-attach): Attaches to a server running a process via a websocket +- [`xterm-addon-clipboard`](https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-clipboard): Access the browser's clipboard - [`xterm-addon-fit`](https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-fit): Fits the terminal to the containing element - [`xterm-addon-search`](https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-search): Adds search functionality - [`xterm-addon-web-links`](https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-web-links): Adds web link detection and interaction diff --git a/addons/xterm-addon-clipboard/.gitignore b/addons/xterm-addon-clipboard/.gitignore new file mode 100644 index 0000000000..a9f4ed5456 --- /dev/null +++ b/addons/xterm-addon-clipboard/.gitignore @@ -0,0 +1,2 @@ +lib +node_modules \ No newline at end of file diff --git a/addons/xterm-addon-clipboard/.npmignore b/addons/xterm-addon-clipboard/.npmignore new file mode 100644 index 0000000000..b203232aff --- /dev/null +++ b/addons/xterm-addon-clipboard/.npmignore @@ -0,0 +1,29 @@ +# Blacklist - exclude everything except npm defaults such as LICENSE, etc +* +!*/ + +# Whitelist - lib/ +!lib/**/*.d.ts + +!lib/**/*.js +!lib/**/*.js.map + +!lib/**/*.css + +# Whitelist - src/ +!src/**/*.ts +!src/**/*.d.ts + +!src/**/*.js +!src/**/*.js.map + +!src/**/*.css + +# Blacklist - src/ test files +src/**/*.test.ts +src/**/*.test.d.ts +src/**/*.test.js +src/**/*.test.js.map + +# Whitelist - typings/ +!typings/*.d.ts diff --git a/addons/xterm-addon-clipboard/LICENSE b/addons/xterm-addon-clipboard/LICENSE new file mode 100644 index 0000000000..b6c38b1547 --- /dev/null +++ b/addons/xterm-addon-clipboard/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023, The xterm.js authors (https://github.com/xtermjs/xterm.js) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/addons/xterm-addon-clipboard/README.md b/addons/xterm-addon-clipboard/README.md new file mode 100644 index 0000000000..4515d191dc --- /dev/null +++ b/addons/xterm-addon-clipboard/README.md @@ -0,0 +1,52 @@ +## xterm-addon-clipboard + +An addon for [xterm.js](https://github.com/xtermjs/xterm.js) that enables accessing the system clipboard. This addon requires xterm.js v4+. + +### Install + +```bash +npm install --save xterm-addon-clipboard +``` + +### Usage + +```ts +import { Terminal } from 'xterm'; +import { ClipboardAddon } from 'xterm-addon-clipboard'; + +const terminal = new Terminal(); +const clipboardAddon = new ClipboardAddon(); +terminal.loadAddon(clipboardAddon); +``` + +To use a custom clipboard provider + +```ts +import { Terminal, IClipboardProvider, ClipboardSelection } from 'xterm'; +import { ClipboardAddon } from 'xterm-addon-clipboard'; + +function b64Encode(data: string): string { + // Base64 encode impl +} + +function b64Decode(data: string): string { + // Base64 decode impl +} + +class MyCustomClipboardProvider implements IClipboardProvider { + private _data: string + public readText(selection: ClipboardSelection): Promise { + return Promise.resolve(b64Encode(this._data)); + } + public writeText(selection: ClipboardSelection, data: string): Promise { + this._data = b64Decode(data); + return Promise.resolve(); + } +} + +const terminal = new Terminal(); +const clipboardAddon = new ClipboardAddon(new MyCustomClipboardProvider()); +terminal.loadAddon(clipboardAddon); +``` + +See the full [API](https://github.com/xtermjs/xterm.js/blob/master/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts) for more advanced usage. diff --git a/addons/xterm-addon-clipboard/package.json b/addons/xterm-addon-clipboard/package.json new file mode 100644 index 0000000000..2929d90f40 --- /dev/null +++ b/addons/xterm-addon-clipboard/package.json @@ -0,0 +1,29 @@ +{ + "name": "xterm-addon-clipboard", + "version": "0.1.0", + "author": { + "name": "The xterm.js authors", + "url": "https://xtermjs.org/" + }, + "main": "lib/xterm-addon-clipboard.js", + "types": "typings/xterm-addon-clipboard.d.ts", + "repository": "https://github.com/xtermjs/xterm.js", + "license": "MIT", + "keywords": [ + "terminal", + "xterm", + "xterm.js" + ], + "scripts": { + "build": "../../node_modules/.bin/tsc -p .", + "prepackage": "npm run build", + "package": "../../node_modules/.bin/webpack", + "prepublishOnly": "npm run package" + }, + "peerDependencies": { + "xterm": "^5.3.0" + }, + "dependencies": { + "js-base64": "^3.7.5" + } +} diff --git a/addons/xterm-addon-clipboard/src/ClipboardAddon.ts b/addons/xterm-addon-clipboard/src/ClipboardAddon.ts new file mode 100644 index 0000000000..5a89751ffb --- /dev/null +++ b/addons/xterm-addon-clipboard/src/ClipboardAddon.ts @@ -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(); + } +} diff --git a/addons/xterm-addon-clipboard/src/ClipboardProvider.ts b/addons/xterm-addon-clipboard/src/ClipboardProvider.ts new file mode 100644 index 0000000000..a28ef8d5c6 --- /dev/null +++ b/addons/xterm-addon-clipboard/src/ClipboardProvider.ts @@ -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 { + if (selection !== 'c') { + return Promise.resolve(''); + } + return navigator.clipboard.readText().then((text) => + Base64.encode(text)); + } + public writeText(selection: ClipboardSelection, data: string): Promise { + 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); + } +} diff --git a/addons/xterm-addon-clipboard/src/tsconfig.json b/addons/xterm-addon-clipboard/src/tsconfig.json new file mode 100644 index 0000000000..7f87b44538 --- /dev/null +++ b/addons/xterm-addon-clipboard/src/tsconfig.json @@ -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" + } + ] +} diff --git a/addons/xterm-addon-clipboard/test/ClipboardAddon.api.ts b/addons/xterm-addon-clipboard/test/ClipboardAddon.api.ts new file mode 100644 index 0000000000..d6eea6aba6 --- /dev/null +++ b/addons/xterm-addon-clipboard/test/ClipboardAddon.api.ts @@ -0,0 +1,86 @@ +/** + * Copyright (c) 2023 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { assert } from 'chai'; +import { openTerminal, launchBrowser, writeSync, getBrowserType } from '../../../out-test/api/TestUtils'; +import { Browser, BrowserContext, Page } from '@playwright/test'; + +const APP = 'http://127.0.0.1:3001/test'; + +let browser: Browser; +let context: BrowserContext; +let page: Page; +const width = 800; +const height = 600; + +describe('ClipboardAddon', () => { + before(async function (): Promise { + browser = await launchBrowser({ + // Enable clipboard access in firefox, mainly for readText + firefoxUserPrefs: { + // eslint-disable-next-line @typescript-eslint/naming-convention + 'dom.events.testing.asyncClipboard': true, + // eslint-disable-next-line @typescript-eslint/naming-convention + 'dom.events.asyncClipboard.readText': true + } + }); + context = await browser.newContext(); + if (getBrowserType().name() !== 'webkit') { + // Enable clipboard access in chromium without user gesture + context.grantPermissions(['clipboard-read', 'clipboard-write']); + } + page = await context.newPage(); + await page.setViewportSize({ width, height }); + await page.goto(APP); + await openTerminal(page, { allowClipboardAccess: true }); + await page.evaluate(` + window.clipboardAddon = new ClipboardAddon(); + window.term.loadAddon(window.clipboardAddon); + `); + }); + + after(() => { + browser.close(); + }); + + beforeEach(async () => { + await page.evaluate(`window.term.reset()`); + }); + + const testDataEncoded = 'aGVsbG8gd29ybGQ='; + const testDataDecoded = 'hello world'; + + describe('write data', async function (): Promise { + it('simple string', async () => { + await writeSync(page, `\x1b]52;c;${testDataEncoded}\x07`); + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), testDataDecoded); + }); + it('invalid base64 string', async () => { + await writeSync(page, `\x1b]52;c;${testDataEncoded}invalid\x07`); + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); + }); + it('empty string', async () => { + await writeSync(page, `\x1b]52;c;\x07`); + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); + }); + }); + + describe('read data', async function (): Promise { + it('simple string', async () => { + await page.evaluate(` + window.data = []; + window.term.onData(e => data.push(e)); + `); + await page.evaluate(() => window.navigator.clipboard.writeText('hello world')); + await writeSync(page, `\x1b]52;c;?\x07`); + assert.deepEqual(await page.evaluate(`window.data`), [testDataEncoded]); + }); + it('clear clipboard', async () => { + await writeSync(page, `\x1b]52;c;!\x07`); + await writeSync(page, `\x1b]52;c;?\x07`); + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); + }); + }); +}); diff --git a/addons/xterm-addon-clipboard/test/tsconfig.json b/addons/xterm-addon-clipboard/test/tsconfig.json new file mode 100644 index 0000000000..1e5ab21e1a --- /dev/null +++ b/addons/xterm-addon-clipboard/test/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es2015", + "lib": [ + "es2015" + ], + "rootDir": ".", + "outDir": "../out-test", + "sourceMap": true, + "removeComments": true, + "strict": true, + "types": [ + "../../../node_modules/@types/mocha", + "../../../node_modules/@types/node", + ] + }, + "include": [ + "./**/*", + "../../../typings/xterm.d.ts" + ] +} \ No newline at end of file diff --git a/addons/xterm-addon-clipboard/tsconfig.json b/addons/xterm-addon-clipboard/tsconfig.json new file mode 100644 index 0000000000..2d820dd1a6 --- /dev/null +++ b/addons/xterm-addon-clipboard/tsconfig.json @@ -0,0 +1,8 @@ +{ + "files": [], + "include": [], + "references": [ + { "path": "./src" }, + { "path": "./test" } + ] +} diff --git a/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts b/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts new file mode 100644 index 0000000000..71d4f20d72 --- /dev/null +++ b/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2023 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { Terminal, ITerminalAddon, IClipboardProvider, ClipboardSelection } from 'xterm'; + +declare module 'xterm-addon-clipboard' { + export class ClipboardProvider implements IClipboardProvider{ + public readText(selection: ClipboardSelection): Promise; + public writeText(selection: ClipboardSelection, data: string): Promise; + } + + /** + * An xterm.js addon that enables accessing the system clipboard from + * xterm.js. + */ + export class ClipboardAddon implements ITerminalAddon { + /** + * Creates a new clipboard addon. + */ + constructor(_provider: IClipboardProvider); + + /** + * Activates the addon + * @param terminal The terminal the addon is being loaded in. + */ + public activate(terminal: Terminal): void; + + /** + * Disposes the addon. + */ + public dispose(): void + } +} diff --git a/addons/xterm-addon-clipboard/webpack.config.js b/addons/xterm-addon-clipboard/webpack.config.js new file mode 100644 index 0000000000..0def5bbc85 --- /dev/null +++ b/addons/xterm-addon-clipboard/webpack.config.js @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2023 The xterm.js authors. All rights reserved. + * @license MIT + */ + +const path = require('path'); + +const addonName = 'ClipboardAddon'; +const mainFile = 'xterm-addon-clipboard.js'; + +module.exports = { + entry: `./out/${addonName}.js`, + devtool: 'source-map', + module: { + rules: [ + { + test: /\.js$/, + use: ["source-map-loader"], + enforce: "pre", + exclude: /node_modules/ + } + ] + }, + output: { + filename: mainFile, + path: path.resolve('./lib'), + library: addonName, + libraryTarget: 'umd' + }, + mode: 'production' +}; diff --git a/addons/xterm-addon-clipboard/yarn.lock b/addons/xterm-addon-clipboard/yarn.lock new file mode 100644 index 0000000000..de7e5b432e --- /dev/null +++ b/addons/xterm-addon-clipboard/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +js-base64@^3.7.5: + version "3.7.5" + resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.5.tgz#21e24cf6b886f76d6f5f165bfcd69cc55b9e3fca" + integrity sha512-3MEt5DTINKqfScXKfJFrRbxkrnk2AxPWGBL/ycjz4dK8iqiSJ06UxD8jh8xuh6p10TX4t2+7FsBYVxxQbMg+qA== diff --git a/bin/publish.js b/bin/publish.js index e9c7c3e810..909877f53b 100644 --- a/bin/publish.js +++ b/bin/publish.js @@ -29,6 +29,7 @@ if (changedFiles.some(e => e.search(/^addons\//) === -1)) { const addonPackageDirs = [ path.resolve(__dirname, '../addons/xterm-addon-attach'), path.resolve(__dirname, '../addons/xterm-addon-canvas'), + path.resolve(__dirname, '../addons/xterm-addon-clipboard'), path.resolve(__dirname, '../addons/xterm-addon-fit'), // path.resolve(__dirname, '../addons/xterm-addon-image'), path.resolve(__dirname, '../addons/xterm-addon-ligatures'), diff --git a/demo/client.ts b/demo/client.ts index a1145ab9a8..78685b87aa 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -12,6 +12,7 @@ import { Terminal } from '../out/browser/public/Terminal'; import { AttachAddon } from '../addons/xterm-addon-attach/out/AttachAddon'; import { CanvasAddon } from '../addons/xterm-addon-canvas/out/CanvasAddon'; +import { ClipboardAddon } from '../addons/xterm-addon-clipboard/out/ClipboardAddon'; import { FitAddon } from '../addons/xterm-addon-fit/out/FitAddon'; import { SearchAddon, ISearchOptions } from '../addons/xterm-addon-search/out/SearchAddon'; import { SerializeAddon } from '../addons/xterm-addon-serialize/out/SerializeAddon'; @@ -32,6 +33,7 @@ if ('WebAssembly' in window) { // Use webpacked version (yarn package) // import { Terminal } from '../lib/xterm'; // import { AttachAddon } from 'xterm-addon-attach'; +// import { ClipboardAddon } from 'xterm-addon-clipboard'; // import { FitAddon } from 'xterm-addon-fit'; // import { ImageAddon } from 'xterm-addon-image'; // import { SearchAddon, ISearchOptions } from 'xterm-addon-search'; @@ -51,6 +53,7 @@ export interface IWindowWithTerminal extends Window { Terminal?: typeof TerminalType; // eslint-disable-line @typescript-eslint/naming-convention AttachAddon?: typeof AttachAddon; // eslint-disable-line @typescript-eslint/naming-convention CanvasAddon?: typeof CanvasAddon; // eslint-disable-line @typescript-eslint/naming-convention + ClipboardAddon?: typeof ClipboardAddon; // eslint-disable-line @typescript-eslint/naming-convention FitAddon?: typeof FitAddon; // eslint-disable-line @typescript-eslint/naming-convention ImageAddon?: typeof ImageAddonType; // eslint-disable-line @typescript-eslint/naming-convention SearchAddon?: typeof SearchAddon; // eslint-disable-line @typescript-eslint/naming-convention @@ -70,7 +73,7 @@ let socket; let pid; let autoResize: boolean = true; -type AddonType = 'attach' | 'canvas' | 'fit' | 'image' | 'search' | 'serialize' | 'unicode11' | 'unicodeGraphemes' | 'webLinks' | 'webgl' | 'ligatures'; +type AddonType = 'attach' | 'canvas' | 'clipboard' | 'fit' | 'image' | 'search' | 'serialize' | 'unicode11' | 'unicodeGraphemes' | 'webLinks' | 'webgl' | 'ligatures'; interface IDemoAddon { name: T; @@ -78,35 +81,38 @@ interface IDemoAddon { ctor: ( T extends 'attach' ? typeof AttachAddon : T extends 'canvas' ? typeof CanvasAddon : - T extends 'fit' ? typeof FitAddon : - T extends 'image' ? typeof ImageAddonType : - T extends 'search' ? typeof SearchAddon : - T extends 'serialize' ? typeof SerializeAddon : - T extends 'webLinks' ? typeof WebLinksAddon : - T extends 'unicode11' ? typeof Unicode11Addon : - T extends 'unicodeGraphemes' ? typeof UnicodeGraphemesAddon : - T extends 'ligatures' ? typeof LigaturesAddon : + T extends 'clipboard' ? typeof ClipboardAddon : + T extends 'fit' ? typeof FitAddon : + T extends 'image' ? typeof ImageAddonType : + T extends 'search' ? typeof SearchAddon : + T extends 'serialize' ? typeof SerializeAddon : + T extends 'webLinks' ? typeof WebLinksAddon : + T extends 'unicode11' ? typeof Unicode11Addon : + T extends 'unicodeGraphemes' ? typeof UnicodeGraphemesAddon : + T extends 'ligatures' ? typeof LigaturesAddon : typeof WebglAddon ); instance?: ( T extends 'attach' ? AttachAddon : T extends 'canvas' ? CanvasAddon : - T extends 'fit' ? FitAddon : - T extends 'image' ? ImageAddonType : - T extends 'search' ? SearchAddon : - T extends 'serialize' ? SerializeAddon : - T extends 'webLinks' ? WebLinksAddon : - T extends 'webgl' ? WebglAddon : - T extends 'unicode11' ? typeof Unicode11Addon : - T extends 'unicodeGraphemes' ? typeof UnicodeGraphemesAddon : - T extends 'ligatures' ? typeof LigaturesAddon : - never + T extends 'clipboard' ? ClipboardAddon : + T extends 'fit' ? FitAddon : + T extends 'image' ? ImageAddonType : + T extends 'search' ? SearchAddon : + T extends 'serialize' ? SerializeAddon : + T extends 'webLinks' ? WebLinksAddon : + T extends 'webgl' ? WebglAddon : + T extends 'unicode11' ? typeof Unicode11Addon : + T extends 'unicodeGraphemes' ? typeof UnicodeGraphemesAddon : + T extends 'ligatures' ? typeof LigaturesAddon : + never ); } const addons: { [T in AddonType]: IDemoAddon } = { attach: { name: 'attach', ctor: AttachAddon, canChange: false }, canvas: { name: 'canvas', ctor: CanvasAddon, canChange: true }, + clipboard: { name: 'clipboard', ctor: ClipboardAddon, canChange: true }, fit: { name: 'fit', ctor: FitAddon, canChange: false }, image: { name: 'image', ctor: ImageAddon, canChange: true }, search: { name: 'search', ctor: SearchAddon, canChange: true }, @@ -179,6 +185,7 @@ const disposeRecreateButtonHandler: () => void = () => { socket = null; addons.attach.instance = undefined; addons.canvas.instance = undefined; + addons.clipboard.instance = undefined; addons.fit.instance = undefined; addons.image.instance = undefined; addons.search.instance = undefined; @@ -228,6 +235,7 @@ if (document.location.pathname === '/test') { window.Terminal = Terminal; window.AttachAddon = AttachAddon; window.CanvasAddon = CanvasAddon; + window.ClipboardAddon = ClipboardAddon; window.FitAddon = FitAddon; window.ImageAddon = ImageAddon; window.SearchAddon = SearchAddon; @@ -287,6 +295,7 @@ function createTerminal(): void { addons.fit.instance = new FitAddon(); addons.image.instance = new ImageAddon(); addons.unicodeGraphemes.instance = new UnicodeGraphemesAddon(); + addons.clipboard.instance = new ClipboardAddon(); try { // try to start with webgl renderer (might throw on older safari/webkit) addons.webgl.instance = new WebglAddon(); } catch (e) { @@ -299,6 +308,7 @@ function createTerminal(): void { typedTerm.loadAddon(addons.serialize.instance); typedTerm.loadAddon(addons.unicodeGraphemes.instance); typedTerm.loadAddon(addons.webLinks.instance); + typedTerm.loadAddon(addons.clipboard.instance); window.term = term; // Expose `term` to window for debugging purposes term.onResize((size: { cols: number, rows: number }) => { diff --git a/demo/tsconfig.json b/demo/tsconfig.json index f728f20e9c..d4c2abd42a 100644 --- a/demo/tsconfig.json +++ b/demo/tsconfig.json @@ -7,6 +7,7 @@ "baseUrl": ".", "paths": { "xterm-addon-attach": ["../addons/xterm-addon-attach"], + "xterm-addon-clipboard": ["../addons/xterm-addon-clipboard"], "xterm-addon-fit": ["../addons/xterm-addon-fit"], "xterm-addon-image": ["../addons/xterm-addon-image"], "xterm-addon-search": ["../addons/xterm-addon-search"], diff --git a/src/browser/Terminal.ts b/src/browser/Terminal.ts index 18af3e4e2a..edbdda851d 100644 --- a/src/browser/Terminal.ts +++ b/src/browser/Terminal.ts @@ -46,7 +46,7 @@ import { CoreTerminal } from 'common/CoreTerminal'; import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter'; import { MutableDisposable, toDisposable } from 'common/Lifecycle'; import * as Browser from 'common/Platform'; -import { ColorRequestType, CoreMouseAction, CoreMouseButton, CoreMouseEventType, IColorEvent, ITerminalOptions, KeyboardResultType, ScrollSource, SpecialColorIndex } from 'common/Types'; +import { ColorRequestType, CoreMouseAction, CoreMouseButton, CoreMouseEventType, IClipboardEvent, IColorEvent, ITerminalOptions, KeyboardResultType, ScrollSource, SpecialColorIndex } from 'common/Types'; import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { IBuffer } from 'common/buffer/Types'; import { C0, C1_ESCAPED } from 'common/data/EscapeSequences'; @@ -54,7 +54,7 @@ import { evaluateKeyboardEvent } from 'common/input/Keyboard'; import { toRgbString } from 'common/input/XParseColor'; import { DecorationService } from 'common/services/DecorationService'; import { IDecorationService } from 'common/services/Services'; -import { IDecoration, IDecorationOptions, IDisposable, ILinkProvider, IMarker } from 'xterm'; +import { IDecoration, IDecorationOptions, IDisposable, ILinkProvider, IMarker, IClipboardProvider } from 'xterm'; import { WindowsOptionsReportType } from '../common/InputHandler'; import { AccessibilityManager } from './AccessibilityManager'; @@ -119,6 +119,7 @@ export class Terminal extends CoreTerminal implements ITerminal { public viewport: IViewport | undefined; private _compositionHelper: ICompositionHelper | undefined; private _accessibilityManager: MutableDisposable = this.register(new MutableDisposable()); + private _clipboardProvider: IClipboardProvider | undefined; private readonly _onCursorMove = this.register(new EventEmitter()); public readonly onCursorMove = this._onCursorMove.event; @@ -163,6 +164,7 @@ export class Terminal extends CoreTerminal implements ITerminal { this.register(this._inputHandler.onRequestReset(() => this.reset())); this.register(this._inputHandler.onRequestWindowsOptionsReport(type => this._reportWindowsOptions(type))); this.register(this._inputHandler.onColor((event) => this._handleColorEvent(event))); + this.register(this._inputHandler.onClipboard((event) => this._handleClipboardEvent(event))); this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove)); this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange)); this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter)); @@ -881,6 +883,14 @@ export class Terminal extends CoreTerminal implements ITerminal { return this.linkifier2.registerLinkProvider(linkProvider); } + public registerClipboardProvider(provider: IClipboardProvider): void { + this._clipboardProvider = provider; + } + + public deregisterClipboardProvider(): void { + this._clipboardProvider = undefined; + } + public registerCharacterJoiner(handler: CharacterJoinerHandler): number { if (!this._characterJoinerService) { throw new Error('Terminal must be opened first'); @@ -1281,6 +1291,18 @@ export class Terminal extends CoreTerminal implements ITerminal { } } + private _handleClipboardEvent(ev: IClipboardEvent): void { + if (!this._clipboardProvider) { + return; + } + if (ev.data === '?') { + this._clipboardProvider.readText(ev.selection).then(data => + this.coreService.triggerDataEvent(data)); + return; + } + this._clipboardProvider.writeText(ev.selection, ev.data); + } + // TODO: Remove cancel function and cancelEvents option public cancel(ev: Event, force?: boolean): boolean | undefined { if (!this.options.cancelEvents && !force) { diff --git a/src/browser/TestUtils.test.ts b/src/browser/TestUtils.test.ts index 7b464a3387..5a96f0af4c 100644 --- a/src/browser/TestUtils.test.ts +++ b/src/browser/TestUtils.test.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IDisposable, IMarker, ILinkProvider, IDecorationOptions, IDecoration } from 'xterm'; +import { IDisposable, IMarker, ILinkProvider, IDecorationOptions, IDecoration, IClipboardProvider } from 'xterm'; import { IEvent, EventEmitter } from 'common/EventEmitter'; import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService, IMouseService, IRenderService, ISelectionService, IThemeService } from 'browser/services/Services'; import { IRenderDimensions, IRenderer, IRequestRedrawEvent } from 'browser/renderer/shared/Types'; @@ -104,6 +104,12 @@ export class MockTerminal implements ITerminal { public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { throw new Error('Method not implemented.'); } + public registerClipboardProvider(provider: IClipboardProvider): void { + throw new Error('Method not implemented.'); + } + public deregisterClipboardProvider(): void { + throw new Error('Method not implemented.'); + } public hasSelection(): boolean { throw new Error('Method not implemented.'); } diff --git a/src/browser/public/Terminal.ts b/src/browser/public/Terminal.ts index 2c75d7b808..c7495b21c0 100644 --- a/src/browser/public/Terminal.ts +++ b/src/browser/public/Terminal.ts @@ -13,7 +13,7 @@ import { AddonManager } from 'common/public/AddonManager'; import { BufferNamespaceApi } from 'common/public/BufferNamespaceApi'; import { ParserApi } from 'common/public/ParserApi'; import { UnicodeApi } from 'common/public/UnicodeApi'; -import { IBufferNamespace as IBufferNamespaceApi, IDecoration, IDecorationOptions, IDisposable, ILinkProvider, ILocalizableStrings, IMarker, IModes, IParser, ITerminalAddon, Terminal as ITerminalApi, ITerminalInitOnlyOptions, IUnicodeHandling } from 'xterm'; +import { IBufferNamespace as IBufferNamespaceApi, IClipboardProvider, IDecoration, IDecorationOptions, IDisposable, ILinkProvider, ILocalizableStrings, IMarker, IModes, IParser, ITerminalAddon, Terminal as ITerminalApi, ITerminalInitOnlyOptions, IUnicodeHandling } from 'xterm'; /** * The set of options that only have an effect when set in the Terminal constructor. @@ -168,6 +168,12 @@ export class Terminal extends Disposable implements ITerminalApi { this._verifyPositiveIntegers(decorationOptions.x ?? 0, decorationOptions.width ?? 0, decorationOptions.height ?? 0); return this._core.registerDecoration(decorationOptions); } + public registerClipboardProvider(provider: IClipboardProvider): void { + this._core.registerClipboardProvider(provider); + } + public deregisterClipboardProvider(): void { + this._core.deregisterClipboardProvider(); + } public hasSelection(): boolean { return this._core.hasSelection(); } diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index 8f9a988f14..0ae912a5f9 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -5,7 +5,7 @@ import { assert } from 'chai'; import { InputHandler } from 'common/InputHandler'; -import { IBufferLine, IAttributeData, IColorEvent, ColorIndex, ColorRequestType, SpecialColorIndex } from 'common/Types'; +import { IBufferLine, IAttributeData, IColorEvent, ColorIndex, ColorRequestType, SpecialColorIndex, IClipboardEvent, ClipboardEventType } from 'common/Types'; import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { CellData } from 'common/buffer/CellData'; import { Attributes, BgFlags, UnderlineStyle } from 'common/buffer/Constants'; @@ -17,7 +17,7 @@ import { DEFAULT_OPTIONS } from 'common/services/OptionsService'; import { clone } from 'common/Clone'; import { BufferService } from 'common/services/BufferService'; import { CoreService } from 'common/services/CoreService'; - +import { ClipboardSelection } from 'xterm'; function getCursor(bufferService: IBufferService): number[] { return [ @@ -1982,6 +1982,88 @@ describe('InputHandler', () => { assert.deepEqual(stack, [[{ type: ColorRequestType.SET, index: 0, color: [170, 187, 204] }, { type: ColorRequestType.SET, index: 123, color: [0, 17, 34] }]]); stack.length = 0; }); + describe('52: manipulate selection data', async () => { + const testDataRaw = 'hello world'; + const testDataB64 = 'aGVsbG8gd29ybGQ='; + optionsService.options.allowClipboardAccess = true; + const stack: IClipboardEvent[] = []; + inputHandler.onClipboard(ev => stack.push(ev)); + await inputHandler.parseP(`\x1b]52;c;\x07`); + await inputHandler.parseP(`\x1b]52;c;${testDataRaw}\x07`); + await inputHandler.parseP(`\x1b]52;c;${testDataB64}\x07`); + await inputHandler.parseP(`\x1b]52;c;${testDataB64}invalid\x07`); + await inputHandler.parseP(`\x1b]52;c;!\x07`); + await inputHandler.parseP(`\x1b]52;c;?\x07`); + await inputHandler.parseP(`\x1b]52;p;\x07`); + await inputHandler.parseP(`\x1b]52;p;${testDataRaw}\x07`); + await inputHandler.parseP(`\x1b]52;p;${testDataB64}\x07`); + await inputHandler.parseP(`\x1b]52;p;${testDataB64}invalid\x07`); + await inputHandler.parseP(`\x1b]52;p;!\x07`); + await inputHandler.parseP(`\x1b]52;p;?\x07`); + assert.deepEqual(stack, [ + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.SYSTEM, + data: '' + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.SYSTEM, + data: testDataRaw + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.SYSTEM, + data: testDataB64 + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.SYSTEM, + data: testDataB64+'invalid' + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.SYSTEM, + data: '!' + }, + { + type: ClipboardEventType.REPORT, + selection: ClipboardSelection.SYSTEM, + data: '?' + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.PRIMARY, + data: '' + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.PRIMARY, + data: testDataRaw + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.PRIMARY, + data: testDataB64 + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.PRIMARY, + data: testDataB64+'invalid' + }, + { + type: ClipboardEventType.SET, + selection: ClipboardSelection.PRIMARY, + data: '!' + }, + { + type: ClipboardEventType.REPORT, + selection: ClipboardSelection.PRIMARY, + data: '?' + } + ]); + stack.length = 0; + }); it('104: restore events', async () => { const stack: IColorEvent[] = []; inputHandler.onColor(ev => stack.push(ev)); @@ -1994,7 +2076,7 @@ describe('InputHandler', () => { stack.length = 0; // full ANSI table restore await inputHandler.parseP('\x1b]104\x07'); - assert.deepEqual(stack, [[{ type: ColorRequestType.RESTORE}]]); + assert.deepEqual(stack, [[{ type: ColorRequestType.RESTORE }]]); }); it('10: FG set & query events', async () => { diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts index b5c91bfba7..cb46bccf1c 100644 --- a/src/common/InputHandler.ts +++ b/src/common/InputHandler.ts @@ -4,7 +4,7 @@ * @license MIT */ -import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IColorEvent, IParseStack, ColorIndex, ColorRequestType, SpecialColorIndex } from 'common/Types'; +import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IColorEvent, IParseStack, ColorIndex, ColorRequestType, SpecialColorIndex, IClipboardEvent, ClipboardEventType } from 'common/Types'; import { C0, C1 } from 'common/data/EscapeSequences'; import { CHARSETS, DEFAULT_CHARSET } from 'common/data/Charsets'; import { EscapeSequenceParser } from 'common/parser/EscapeSequenceParser'; @@ -22,6 +22,7 @@ import { OscHandler } from 'common/parser/OscParser'; import { DcsHandler } from 'common/parser/DcsParser'; import { IBuffer } from 'common/buffer/Types'; import { parseColor } from 'common/input/XParseColor'; +import { ClipboardSelection } from 'xterm'; /** * Map collect to glevel. Used in `selectCharset`. @@ -159,6 +160,8 @@ export class InputHandler extends Disposable implements IInputHandler { public readonly onTitleChange = this._onTitleChange.event; private readonly _onColor = this.register(new EventEmitter()); public readonly onColor = this._onColor.event; + private readonly _onClipboard = this.register(new EventEmitter()); + public readonly onClipboard = this._onClipboard.event; private _parseStack: IParseStack = { paused: false, @@ -320,6 +323,7 @@ export class InputHandler extends Disposable implements IInputHandler { // 50 - Set Font to Pt. // 51 - reserved for Emacs shell. // 52 - Manipulate Selection Data. + this._parser.registerOscHandler(52, new OscHandler(data => this.setOrReportClipboard(data))); // 104 ; c - Reset Color Number c. this._parser.registerOscHandler(104, new OscHandler(data => this.restoreIndexedColor(data))); // 105 ; c - Reset Special Color Number c. @@ -3081,6 +3085,60 @@ export class InputHandler extends Disposable implements IInputHandler { return this._setOrReportSpecialColor(data, 2); } + private _setOrReportClipboard(data: string): boolean { + if (!this._optionsService.options.allowClipboardAccess) { + return true; + } + const args = data.split(';'); + if (args.length < 2) { + return true; + } + const pc = args[0]; + const pd = args[1]; + if (pd.length === 0) { + return true; + } + switch (pc) { + case ClipboardSelection.SYSTEM: + case ClipboardSelection.PRIMARY: + this._onClipboard.fire({ + type: pd === '?' ? ClipboardEventType.REPORT : ClipboardEventType.SET, + selection: pc, + data: pd + }); + break; + } + return true; + } + + /** + * OSC 52 ; ; | ST - set or query selection and clipboard data + * + * Test case: + * + * ```sh + * printf "\e]52;c;%s\a" "$(echo -n "Hello, World" | base64)" + * ``` + * + * @vt: #Y OSC 52 "Manipulate Selection Data" "OSC 52 ; Pc ; Pd BEL" "Set or query selection and clipboard data." + * Pc is the selection name. Can be one of: + * - `c` - clipboard + * - `p` - primary + * - `q` - secondary + * - `s` - select + * - `0-7` - cut-buffers 0-7 + * + * Only the `c` selection (clipboard) is supported by xterm.js. The browser + * Clipboard API only supports the clipboard selection. + * + * Pd is the base64 encoded data. + * If Pd is `?`, the terminal returns the current clipboard contents. + * If Pd is neither base64 encoded nor `?`, then the clipboard is cleared. + */ + public setOrReportClipboard(data: string): boolean { + return this._setOrReportClipboard(data); + } + /** * OSC 104 ; ST - restore ANSI color * diff --git a/src/common/Types.d.ts b/src/common/Types.d.ts index fc8fdf4e61..9146bb671b 100644 --- a/src/common/Types.d.ts +++ b/src/common/Types.d.ts @@ -9,7 +9,7 @@ import { Attributes, UnderlineStyle } from 'common/buffer/Constants'; // eslint- import { IBufferSet } from 'common/buffer/Types'; import { IParams } from 'common/parser/Types'; import { ICoreMouseService, ICoreService, IOptionsService, IUnicodeService } from 'common/services/Services'; -import { IFunctionIdentifier, ITerminalOptions as IPublicTerminalOptions } from 'xterm'; +import { ClipboardSelection as ClipboardSelection, IFunctionIdentifier, ITerminalOptions as IPublicTerminalOptions } from 'xterm'; export interface ICoreTerminal { coreMouseService: ICoreMouseService; @@ -445,6 +445,16 @@ export interface IColorRestoreRequest { } export type IColorEvent = (IColorReportRequest | IColorSetRequest | IColorRestoreRequest)[]; +export const enum ClipboardEventType { + REPORT = 0, + SET = 1 +} + +export interface IClipboardEvent { + type: ClipboardEventType; + selection: ClipboardSelection; + data: string; +} /** * Calls the parser and handles actions generated by the parser. @@ -515,6 +525,7 @@ export interface IInputHandler { /** OSC 10 */ setOrReportFgColor(data: string): boolean; /** OSC 11 */ setOrReportBgColor(data: string): boolean; /** OSC 12 */ setOrReportCursorColor(data: string): boolean; + /** OSC 52 */ setOrReportClipboard(data: string): boolean; /** OSC 104 */ restoreIndexedColor(data: string): boolean; /** OSC 110 */ restoreFgColor(data: string): boolean; /** OSC 111 */ restoreBgColor(data: string): boolean; diff --git a/src/common/services/OptionsService.ts b/src/common/services/OptionsService.ts index 3c572445ed..5907d2da0d 100644 --- a/src/common/services/OptionsService.ts +++ b/src/common/services/OptionsService.ts @@ -52,7 +52,8 @@ export const DEFAULT_OPTIONS: Readonly> = { convertEol: false, termName: 'xterm', cancelEvents: false, - overviewRulerWidth: 0 + overviewRulerWidth: 0, + allowClipboardAccess: false }; const FONT_WEIGHT_OPTIONS: Extract[] = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; @@ -160,7 +161,7 @@ export class OptionsService extends Disposable implements IOptionsService { break; case 'cursorWidth': value = Math.floor(value); - // Fall through for bounds check + // Fall through for bounds check case 'lineHeight': case 'tabStopWidth': if (value < 1) { diff --git a/src/common/services/Services.ts b/src/common/services/Services.ts index 52c2a79fae..b68189af0f 100644 --- a/src/common/services/Services.ts +++ b/src/common/services/Services.ts @@ -206,6 +206,7 @@ export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '50 export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'off'; export interface ITerminalOptions { + allowClipboardAccess?: boolean; allowProposedApi?: boolean; allowTransparency?: boolean; altClickMovesCursor?: boolean; diff --git a/test/api/TestUtils.ts b/test/api/TestUtils.ts index 288a3c0719..a6bf51fc45 100644 --- a/test/api/TestUtils.ts +++ b/test/api/TestUtils.ts @@ -74,9 +74,10 @@ export function getBrowserType(): playwright.BrowserType { +export function launchBrowser(opts?: playwright.LaunchOptions): Promise { const browserType = getBrowserType(); - const options: Record = { + const options: playwright.LaunchOptions = { + ...opts, headless: process.argv.includes('--headless') }; diff --git a/test/playwright/TestUtils.ts b/test/playwright/TestUtils.ts index 0a51757fac..6b5547d7b4 100644 --- a/test/playwright/TestUtils.ts +++ b/test/playwright/TestUtils.ts @@ -77,6 +77,8 @@ type TerminalProxyCustomOverrides = 'buffer' | ( 'attachCustomKeyEventHandler' | 'registerLinkProvider' | 'registerCharacterJoiner' | + 'registerClipboardProvider' | + 'deregisterClipboardProvider' | 'deregisterCharacterJoiner' | 'loadAddon' ); diff --git a/tsconfig.all.json b/tsconfig.all.json index 5d8af629c0..d09638e43b 100644 --- a/tsconfig.all.json +++ b/tsconfig.all.json @@ -9,6 +9,7 @@ { "path": "./test/playwright" }, { "path": "./addons/xterm-addon-attach" }, { "path": "./addons/xterm-addon-canvas" }, + { "path": "./addons/xterm-addon-clipboard" }, { "path": "./addons/xterm-addon-fit" }, { "path": "./addons/xterm-addon-image" }, { "path": "./addons/xterm-addon-ligatures" }, diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 3f603b9e19..10b8939e3a 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -24,6 +24,12 @@ declare module 'xterm' { * An object containing options for the terminal. */ export interface ITerminalOptions { + /** + * Whether to allow clipboard access. When false, any access to the + * clipboard is ignored. The default is false. + */ + allowClipboardAccess?: boolean; + /** * Whether to allow the use of proposed API. When false, any usage of APIs * marked as experimental/proposed will throw an error. The default is @@ -1061,6 +1067,19 @@ declare module 'xterm' { */ registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined; + /** + * Registers a clipboard provider, allowing custom handling of clipboard + * selection events. This is used primarily to enable accessing the + * clipboard to read/write clipboard data. + * @param provider The provider to register. + */ + registerClipboardProvider(provider: IClipboardProvider): void; + + /** + * Deregisters the active clipboard provider. + */ + deregisterClipboardProvider(): void; + /** * Gets whether the terminal has an active selection. */ @@ -1842,4 +1861,29 @@ declare module 'xterm' { */ readonly wraparoundMode: boolean; } + + export interface IClipboardProvider { + /** + * Gets the clipboard content. + * @param selection The clipboard selection to read. + * @returns A promise that resolves with the base64 encoded data. + */ + readText(selection: ClipboardSelection): Promise; + + /** + * Sets the clipboard content. + * @param selection The clipboard selection to set. + * @param data The base64 encoded data to set. If the data is invalid base64, the clipboard is + * cleared. + */ + writeText(selection: ClipboardSelection, data: string): Promise; + } + + /** + * Clipboard selection type. + */ + export const enum ClipboardSelection { + SYSTEM = 'c', + PRIMARY = 'p', + } } From b22762b47209e884af4e22cc1c8f59e78c78984e Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 21 Sep 2023 10:26:46 -0400 Subject: [PATCH 02/18] Fix tsconfigs, IDisposable, and add comments --- addons/xterm-addon-clipboard/package.json | 2 +- .../xterm-addon-clipboard/src/ClipboardAddon.ts | 9 ++++----- addons/xterm-addon-clipboard/src/tsconfig.json | 2 +- addons/xterm-addon-clipboard/test/tsconfig.json | 6 +++--- src/browser/Terminal.ts | 11 ++++++----- src/browser/TestUtils.test.ts | 5 +---- src/browser/public/Terminal.ts | 7 ++----- test/playwright/TestUtils.ts | 1 - typings/xterm.d.ts | 17 ++++++++--------- 9 files changed, 26 insertions(+), 34 deletions(-) diff --git a/addons/xterm-addon-clipboard/package.json b/addons/xterm-addon-clipboard/package.json index 2929d90f40..ccdbf59987 100644 --- a/addons/xterm-addon-clipboard/package.json +++ b/addons/xterm-addon-clipboard/package.json @@ -7,7 +7,7 @@ }, "main": "lib/xterm-addon-clipboard.js", "types": "typings/xterm-addon-clipboard.d.ts", - "repository": "https://github.com/xtermjs/xterm.js", + "repository": "https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-clipboard", "license": "MIT", "keywords": [ "terminal", diff --git a/addons/xterm-addon-clipboard/src/ClipboardAddon.ts b/addons/xterm-addon-clipboard/src/ClipboardAddon.ts index 5a89751ffb..51c113db20 100644 --- a/addons/xterm-addon-clipboard/src/ClipboardAddon.ts +++ b/addons/xterm-addon-clipboard/src/ClipboardAddon.ts @@ -4,18 +4,17 @@ */ import { ClipboardProvider } from './ClipboardProvider'; -import { IClipboardProvider, ITerminalAddon, Terminal } from 'xterm'; +import { IClipboardProvider, IDisposable, ITerminalAddon, Terminal } from 'xterm'; export class ClipboardAddon implements ITerminalAddon { - private _terminal: Terminal | undefined; + private _disposable: IDisposable | undefined; constructor(private _provider: IClipboardProvider = new ClipboardProvider()) {} public activate(terminal: Terminal): void { - this._terminal = terminal; - terminal.registerClipboardProvider(this._provider); + this._disposable = terminal.registerClipboardProvider(this._provider); } public dispose(): void { - this._terminal?.deregisterClipboardProvider(); + return this._disposable?.dispose(); } } diff --git a/addons/xterm-addon-clipboard/src/tsconfig.json b/addons/xterm-addon-clipboard/src/tsconfig.json index 7f87b44538..55cdc7c5de 100644 --- a/addons/xterm-addon-clipboard/src/tsconfig.json +++ b/addons/xterm-addon-clipboard/src/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "module": "commonjs", - "target": "es2017", + "target": "es2021", "sourceMap": true, "outDir": "../out", "rootDir": ".", diff --git a/addons/xterm-addon-clipboard/test/tsconfig.json b/addons/xterm-addon-clipboard/test/tsconfig.json index 1e5ab21e1a..ffa1c5fa72 100644 --- a/addons/xterm-addon-clipboard/test/tsconfig.json +++ b/addons/xterm-addon-clipboard/test/tsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { "module": "commonjs", - "target": "es2015", + "target": "es2021", "lib": [ - "es2015" + "es2021" ], "rootDir": ".", "outDir": "../out-test", @@ -19,4 +19,4 @@ "./**/*", "../../../typings/xterm.d.ts" ] -} \ No newline at end of file +} diff --git a/src/browser/Terminal.ts b/src/browser/Terminal.ts index edbdda851d..12963b4c8e 100644 --- a/src/browser/Terminal.ts +++ b/src/browser/Terminal.ts @@ -883,12 +883,13 @@ export class Terminal extends CoreTerminal implements ITerminal { return this.linkifier2.registerLinkProvider(linkProvider); } - public registerClipboardProvider(provider: IClipboardProvider): void { + public registerClipboardProvider(provider: IClipboardProvider): IDisposable { this._clipboardProvider = provider; - } - - public deregisterClipboardProvider(): void { - this._clipboardProvider = undefined; + return { + dispose: () => { + this._clipboardProvider = undefined; + } + }; } public registerCharacterJoiner(handler: CharacterJoinerHandler): number { diff --git a/src/browser/TestUtils.test.ts b/src/browser/TestUtils.test.ts index 5a96f0af4c..e38017e11b 100644 --- a/src/browser/TestUtils.test.ts +++ b/src/browser/TestUtils.test.ts @@ -104,10 +104,7 @@ export class MockTerminal implements ITerminal { public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { throw new Error('Method not implemented.'); } - public registerClipboardProvider(provider: IClipboardProvider): void { - throw new Error('Method not implemented.'); - } - public deregisterClipboardProvider(): void { + public registerClipboardProvider(provider: IClipboardProvider): IDisposable { throw new Error('Method not implemented.'); } public hasSelection(): boolean { diff --git a/src/browser/public/Terminal.ts b/src/browser/public/Terminal.ts index c7495b21c0..808519a5d9 100644 --- a/src/browser/public/Terminal.ts +++ b/src/browser/public/Terminal.ts @@ -168,11 +168,8 @@ export class Terminal extends Disposable implements ITerminalApi { this._verifyPositiveIntegers(decorationOptions.x ?? 0, decorationOptions.width ?? 0, decorationOptions.height ?? 0); return this._core.registerDecoration(decorationOptions); } - public registerClipboardProvider(provider: IClipboardProvider): void { - this._core.registerClipboardProvider(provider); - } - public deregisterClipboardProvider(): void { - this._core.deregisterClipboardProvider(); + public registerClipboardProvider(provider: IClipboardProvider): IDisposable { + return this._core.registerClipboardProvider(provider); } public hasSelection(): boolean { return this._core.hasSelection(); diff --git a/test/playwright/TestUtils.ts b/test/playwright/TestUtils.ts index 6b5547d7b4..d58aa88ec4 100644 --- a/test/playwright/TestUtils.ts +++ b/test/playwright/TestUtils.ts @@ -78,7 +78,6 @@ type TerminalProxyCustomOverrides = 'buffer' | ( 'registerLinkProvider' | 'registerCharacterJoiner' | 'registerClipboardProvider' | - 'deregisterClipboardProvider' | 'deregisterCharacterJoiner' | 'loadAddon' ); diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 10b8939e3a..0ab9071a7b 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -1073,12 +1073,7 @@ declare module 'xterm' { * clipboard to read/write clipboard data. * @param provider The provider to register. */ - registerClipboardProvider(provider: IClipboardProvider): void; - - /** - * Deregisters the active clipboard provider. - */ - deregisterClipboardProvider(): void; + registerClipboardProvider(provider: IClipboardProvider): IDisposable; /** * Gets whether the terminal has an active selection. @@ -1873,14 +1868,18 @@ declare module 'xterm' { /** * Sets the clipboard content. * @param selection The clipboard selection to set. - * @param data The base64 encoded data to set. If the data is invalid base64, the clipboard is - * cleared. + * @param data The base64 encoded data to set. If the data is invalid + * base64, the clipboard is cleared. */ writeText(selection: ClipboardSelection, data: string): Promise; } /** - * Clipboard selection type. + * Clipboard selection type. This is used to specify which selection buffer to + * read or write to. + * - SYSTEM `c`: The system clipboard. + * - PRIMARY `p`: The primary clipboard. This is provided for compatibility + * with Linux X11. */ export const enum ClipboardSelection { SYSTEM = 'c', From 092aeaeb6fc466babb412905a5d2acf9c5605012 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 21 Sep 2023 10:33:44 -0400 Subject: [PATCH 03/18] Fix rename clipboard selection for clarity --- .../src/ClipboardProvider.ts | 6 ++--- .../typings/xterm-addon-clipboard.d.ts | 6 ++--- src/common/InputHandler.test.ts | 26 +++++++++---------- src/common/InputHandler.ts | 6 ++--- src/common/Types.d.ts | 4 +-- typings/xterm.d.ts | 6 ++--- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/addons/xterm-addon-clipboard/src/ClipboardProvider.ts b/addons/xterm-addon-clipboard/src/ClipboardProvider.ts index a28ef8d5c6..e7f9ff8a57 100644 --- a/addons/xterm-addon-clipboard/src/ClipboardProvider.ts +++ b/addons/xterm-addon-clipboard/src/ClipboardProvider.ts @@ -4,7 +4,7 @@ */ import { Base64 } from 'js-base64'; -import { ClipboardSelection, IClipboardProvider } from 'xterm'; +import { ClipboardSelectionType, IClipboardProvider } from 'xterm'; export class ClipboardProvider implements IClipboardProvider { constructor( @@ -14,14 +14,14 @@ export class ClipboardProvider implements IClipboardProvider { */ public limit = 1000000 // 1MB ){} - public readText(selection: ClipboardSelection): Promise { + public readText(selection: ClipboardSelectionType): Promise { if (selection !== 'c') { return Promise.resolve(''); } return navigator.clipboard.readText().then((text) => Base64.encode(text)); } - public writeText(selection: ClipboardSelection, data: string): Promise { + public writeText(selection: ClipboardSelectionType, data: string): Promise { if (selection !== 'c' || (this.limit > 0 && data.length > this.limit)) { return Promise.resolve(); } diff --git a/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts b/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts index 71d4f20d72..c64cf6256f 100644 --- a/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts +++ b/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts @@ -3,12 +3,12 @@ * @license MIT */ -import { Terminal, ITerminalAddon, IClipboardProvider, ClipboardSelection } from 'xterm'; +import { Terminal, ITerminalAddon, IClipboardProvider, ClipboardSelection as ClipboardSelectionType } from 'xterm'; declare module 'xterm-addon-clipboard' { export class ClipboardProvider implements IClipboardProvider{ - public readText(selection: ClipboardSelection): Promise; - public writeText(selection: ClipboardSelection, data: string): Promise; + public readText(selection: ClipboardSelectionType): Promise; + public writeText(selection: ClipboardSelectionType, data: string): Promise; } /** diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index 0ae912a5f9..f5bf1dde24 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -17,7 +17,7 @@ import { DEFAULT_OPTIONS } from 'common/services/OptionsService'; import { clone } from 'common/Clone'; import { BufferService } from 'common/services/BufferService'; import { CoreService } from 'common/services/CoreService'; -import { ClipboardSelection } from 'xterm'; +import { ClipboardSelectionType } from 'xterm'; function getCursor(bufferService: IBufferService): number[] { return [ @@ -2003,62 +2003,62 @@ describe('InputHandler', () => { assert.deepEqual(stack, [ { type: ClipboardEventType.SET, - selection: ClipboardSelection.SYSTEM, + selection: ClipboardSelectionType.SYSTEM, data: '' }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.SYSTEM, + selection: ClipboardSelectionType.SYSTEM, data: testDataRaw }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.SYSTEM, + selection: ClipboardSelectionType.SYSTEM, data: testDataB64 }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.SYSTEM, + selection: ClipboardSelectionType.SYSTEM, data: testDataB64+'invalid' }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.SYSTEM, + selection: ClipboardSelectionType.SYSTEM, data: '!' }, { type: ClipboardEventType.REPORT, - selection: ClipboardSelection.SYSTEM, + selection: ClipboardSelectionType.SYSTEM, data: '?' }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.PRIMARY, + selection: ClipboardSelectionType.PRIMARY, data: '' }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.PRIMARY, + selection: ClipboardSelectionType.PRIMARY, data: testDataRaw }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.PRIMARY, + selection: ClipboardSelectionType.PRIMARY, data: testDataB64 }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.PRIMARY, + selection: ClipboardSelectionType.PRIMARY, data: testDataB64+'invalid' }, { type: ClipboardEventType.SET, - selection: ClipboardSelection.PRIMARY, + selection: ClipboardSelectionType.PRIMARY, data: '!' }, { type: ClipboardEventType.REPORT, - selection: ClipboardSelection.PRIMARY, + selection: ClipboardSelectionType.PRIMARY, data: '?' } ]); diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts index cb46bccf1c..7fd5ad24b3 100644 --- a/src/common/InputHandler.ts +++ b/src/common/InputHandler.ts @@ -22,7 +22,7 @@ import { OscHandler } from 'common/parser/OscParser'; import { DcsHandler } from 'common/parser/DcsParser'; import { IBuffer } from 'common/buffer/Types'; import { parseColor } from 'common/input/XParseColor'; -import { ClipboardSelection } from 'xterm'; +import { ClipboardSelectionType } from 'xterm'; /** * Map collect to glevel. Used in `selectCharset`. @@ -3099,8 +3099,8 @@ export class InputHandler extends Disposable implements IInputHandler { return true; } switch (pc) { - case ClipboardSelection.SYSTEM: - case ClipboardSelection.PRIMARY: + case ClipboardSelectionType.SYSTEM: + case ClipboardSelectionType.PRIMARY: this._onClipboard.fire({ type: pd === '?' ? ClipboardEventType.REPORT : ClipboardEventType.SET, selection: pc, diff --git a/src/common/Types.d.ts b/src/common/Types.d.ts index 9146bb671b..accd624328 100644 --- a/src/common/Types.d.ts +++ b/src/common/Types.d.ts @@ -9,7 +9,7 @@ import { Attributes, UnderlineStyle } from 'common/buffer/Constants'; // eslint- import { IBufferSet } from 'common/buffer/Types'; import { IParams } from 'common/parser/Types'; import { ICoreMouseService, ICoreService, IOptionsService, IUnicodeService } from 'common/services/Services'; -import { ClipboardSelection as ClipboardSelection, IFunctionIdentifier, ITerminalOptions as IPublicTerminalOptions } from 'xterm'; +import { ClipboardSelectionType as ClipboardSelectionType, IFunctionIdentifier, ITerminalOptions as IPublicTerminalOptions } from 'xterm'; export interface ICoreTerminal { coreMouseService: ICoreMouseService; @@ -452,7 +452,7 @@ export const enum ClipboardEventType { export interface IClipboardEvent { type: ClipboardEventType; - selection: ClipboardSelection; + selection: ClipboardSelectionType; data: string; } diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index 0ab9071a7b..ba5b163088 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -1863,7 +1863,7 @@ declare module 'xterm' { * @param selection The clipboard selection to read. * @returns A promise that resolves with the base64 encoded data. */ - readText(selection: ClipboardSelection): Promise; + readText(selection: ClipboardSelectionType): Promise; /** * Sets the clipboard content. @@ -1871,7 +1871,7 @@ declare module 'xterm' { * @param data The base64 encoded data to set. If the data is invalid * base64, the clipboard is cleared. */ - writeText(selection: ClipboardSelection, data: string): Promise; + writeText(selection: ClipboardSelectionType, data: string): Promise; } /** @@ -1881,7 +1881,7 @@ declare module 'xterm' { * - PRIMARY `p`: The primary clipboard. This is provided for compatibility * with Linux X11. */ - export const enum ClipboardSelection { + export const enum ClipboardSelectionType { SYSTEM = 'c', PRIMARY = 'p', } From d245e90185a4a9e8e626658ec949164351a7a489 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 21 Sep 2023 12:05:14 -0400 Subject: [PATCH 04/18] Add playwright terminal test --- test/playwright/Terminal.test.ts | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/playwright/Terminal.test.ts b/test/playwright/Terminal.test.ts index 19f6eb315e..13c0f8d84b 100644 --- a/test/playwright/Terminal.test.ts +++ b/test/playwright/Terminal.test.ts @@ -770,6 +770,60 @@ test.describe('API Integration Tests', () => { }); }); + test.describe('registerClipboardProvider', () => { + async function registerClipboardProvider(ctx: ITestContext): Promise { + await ctx.page.evaluate(`window.clipboard = ''`); + await ctx.page.evaluate(`window.term._disposables.push( + window.term.registerClipboardProvider({ + readText: (selection) => { + return Promise.resolve(window.clipboard); + }, + writeText: (selection, text) => { + window.clipboard = text; + return Promise.resolve(); + } + }) + )`); + } + test('should register clipboard provider', async () => { + await openTerminal(ctx, { allowClipboardAccess: true }); + await registerClipboardProvider(ctx); + await ctx.page.evaluate(`window.term.dispose()`); + }); + test('should ignore clipboard when no provider is registered', async () => { + await openTerminal(ctx, { allowClipboardAccess: true }); + await ctx.proxy.write('\x1b]52;c;foobar\x07'); + strictEqual(await ctx.page.evaluate(`window.clipboard`), ''); + await ctx.page.evaluate(`window.term.dispose()`); + }); + test('should ignore clipboard when allowClipboardAccess is false', async () => { + await openTerminal(ctx, { allowClipboardAccess: false }); + await registerClipboardProvider(ctx); + await ctx.proxy.write('\x1b]52;c;foobar\x07'); + strictEqual(await ctx.page.evaluate(`window.clipboard`), ''); + await ctx.page.evaluate(`window.term.dispose()`); + }); + test('should save to clipboard when writeText is called', async () => { + await openTerminal(ctx, { allowClipboardAccess: true }); + await registerClipboardProvider(ctx); + await ctx.proxy.write('\x1b]52;c;foobar\x07'); + strictEqual(await ctx.page.evaluate(`window.clipboard`), 'foobar'); + await ctx.page.evaluate(`window.term.dispose()`); + }); + test('should read from clipboard when readText is called', async () => { + await openTerminal(ctx, { allowClipboardAccess: true }); + await registerClipboardProvider(ctx); + await ctx.page.evaluate(` + window.data = []; + window.term.onData(e => data.push(e)); + `); + await ctx.proxy.write('\x1b]52;c;foobar\x07'); + await ctx.proxy.write('\x1b]52;c;?\x07'); + deepStrictEqual(await ctx.page.evaluate(`window.data`), ['foobar']); + await ctx.page.evaluate(`window.term.dispose()`); + }); + }); + test.describe('registerLinkProvider', () => { test('should fire provideLinks when hovering cells', async () => { await openTerminal(ctx); From aed94e53177d7797a63c2aa9bac11180821090d8 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:45:21 -0700 Subject: [PATCH 05/18] xterm-addon-clipboard to scoped module --- .eslintrc.json | 4 ++-- README.md | 2 +- .../.gitignore | 0 .../.npmignore | 0 .../{xterm-addon-clipboard => addon-clipboard}/LICENSE | 0 .../README.md | 10 +++++----- .../package.json | 8 ++++---- .../src/ClipboardAddon.ts | 2 +- .../src/ClipboardProvider.ts | 2 +- .../src/tsconfig.json | 0 .../test/ClipboardAddon.api.ts | 0 .../test/tsconfig.json | 0 .../tsconfig.json | 0 .../typings/addon-clipboard.d.ts} | 2 +- .../webpack.config.js | 2 +- .../yarn.lock | 0 bin/publish.js | 2 +- demo/client.ts | 4 ++-- demo/tsconfig.json | 2 +- src/common/InputHandler.test.ts | 2 +- src/common/InputHandler.ts | 2 +- tsconfig.all.json | 2 +- 22 files changed, 23 insertions(+), 23 deletions(-) rename addons/{xterm-addon-clipboard => addon-clipboard}/.gitignore (100%) rename addons/{xterm-addon-clipboard => addon-clipboard}/.npmignore (100%) rename addons/{xterm-addon-clipboard => addon-clipboard}/LICENSE (100%) rename addons/{xterm-addon-clipboard => addon-clipboard}/README.md (81%) rename addons/{xterm-addon-clipboard => addon-clipboard}/package.json (77%) rename addons/{xterm-addon-clipboard => addon-clipboard}/src/ClipboardAddon.ts (95%) rename addons/{xterm-addon-clipboard => addon-clipboard}/src/ClipboardProvider.ts (93%) rename addons/{xterm-addon-clipboard => addon-clipboard}/src/tsconfig.json (100%) rename addons/{xterm-addon-clipboard => addon-clipboard}/test/ClipboardAddon.api.ts (100%) rename addons/{xterm-addon-clipboard => addon-clipboard}/test/tsconfig.json (100%) rename addons/{xterm-addon-clipboard => addon-clipboard}/tsconfig.json (100%) rename addons/{xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts => addon-clipboard/typings/addon-clipboard.d.ts} (95%) rename addons/{xterm-addon-clipboard => addon-clipboard}/webpack.config.js (92%) rename addons/{xterm-addon-clipboard => addon-clipboard}/yarn.lock (100%) diff --git a/.eslintrc.json b/.eslintrc.json index d1092b09cb..5baa916625 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,8 +18,8 @@ "addons/addon-attach/test/tsconfig.json", "addons/addon-canvas/src/tsconfig.json", "addons/addon-canvas/test/tsconfig.json", - "addons/xterm-addon-clipboard/src/tsconfig.json", - "addons/xterm-addon-clipboard/test/tsconfig.json", + "addons/addon-clipboard/src/tsconfig.json", + "addons/addon-clipboard/test/tsconfig.json", "addons/addon-fit/src/tsconfig.json", "addons/addon-fit/test/tsconfig.json", "addons/addon-image/src/tsconfig.json", diff --git a/README.md b/README.md index 9ac2c00996..7f7dc8322f 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ The xterm.js team maintains the following addons, but anyone can build them: - [`@xterm/addon-attach`](https://github.com/xtermjs/xterm.js/tree/master/addons/addon-attach): Attaches to a server running a process via a websocket - [`@xterm/addon-canvas`](https://github.com/xtermjs/xterm.js/tree/master/addons/addon-canvas): Renders xterm.js using a `canvas` element's 2d context -- [`xterm-addon-clipboard`](https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-clipboard): Access the browser's clipboard +- [`@xterm/addon-clipboard`](https://github.com/xtermjs/xterm.js/tree/master/addons/addon-clipboard): Access the browser's clipboard - [`@xterm/addon-fit`](https://github.com/xtermjs/xterm.js/tree/master/addons/addon-fit): Fits the terminal to the containing element - [`@xterm/addon-image`](https://github.com/xtermjs/xterm.js/tree/master/addons/addon-image): Adds image support - [`@xterm/addon-search`](https://github.com/xtermjs/xterm.js/tree/master/addons/addon-search): Adds search functionality diff --git a/addons/xterm-addon-clipboard/.gitignore b/addons/addon-clipboard/.gitignore similarity index 100% rename from addons/xterm-addon-clipboard/.gitignore rename to addons/addon-clipboard/.gitignore diff --git a/addons/xterm-addon-clipboard/.npmignore b/addons/addon-clipboard/.npmignore similarity index 100% rename from addons/xterm-addon-clipboard/.npmignore rename to addons/addon-clipboard/.npmignore diff --git a/addons/xterm-addon-clipboard/LICENSE b/addons/addon-clipboard/LICENSE similarity index 100% rename from addons/xterm-addon-clipboard/LICENSE rename to addons/addon-clipboard/LICENSE diff --git a/addons/xterm-addon-clipboard/README.md b/addons/addon-clipboard/README.md similarity index 81% rename from addons/xterm-addon-clipboard/README.md rename to addons/addon-clipboard/README.md index 4515d191dc..087cf0990f 100644 --- a/addons/xterm-addon-clipboard/README.md +++ b/addons/addon-clipboard/README.md @@ -1,18 +1,18 @@ -## xterm-addon-clipboard +## @xterm/addon-clipboard An addon for [xterm.js](https://github.com/xtermjs/xterm.js) that enables accessing the system clipboard. This addon requires xterm.js v4+. ### Install ```bash -npm install --save xterm-addon-clipboard +npm install --save @xterm/addon-clipboard ``` ### Usage ```ts import { Terminal } from 'xterm'; -import { ClipboardAddon } from 'xterm-addon-clipboard'; +import { ClipboardAddon } from '@xterm/addon-clipboard'; const terminal = new Terminal(); const clipboardAddon = new ClipboardAddon(); @@ -23,7 +23,7 @@ To use a custom clipboard provider ```ts import { Terminal, IClipboardProvider, ClipboardSelection } from 'xterm'; -import { ClipboardAddon } from 'xterm-addon-clipboard'; +import { ClipboardAddon } from '@xterm/addon-clipboard'; function b64Encode(data: string): string { // Base64 encode impl @@ -49,4 +49,4 @@ const clipboardAddon = new ClipboardAddon(new MyCustomClipboardProvider()); terminal.loadAddon(clipboardAddon); ``` -See the full [API](https://github.com/xtermjs/xterm.js/blob/master/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts) for more advanced usage. +See the full [API](https://github.com/xtermjs/xterm.js/blob/master/addons/addon-clipboard/typings/addon-clipboard.d.ts) for more advanced usage. diff --git a/addons/xterm-addon-clipboard/package.json b/addons/addon-clipboard/package.json similarity index 77% rename from addons/xterm-addon-clipboard/package.json rename to addons/addon-clipboard/package.json index ccdbf59987..2e7cd1eb0a 100644 --- a/addons/xterm-addon-clipboard/package.json +++ b/addons/addon-clipboard/package.json @@ -1,13 +1,13 @@ { - "name": "xterm-addon-clipboard", + "name": "@xterm/addon-clipboard", "version": "0.1.0", "author": { "name": "The xterm.js authors", "url": "https://xtermjs.org/" }, - "main": "lib/xterm-addon-clipboard.js", - "types": "typings/xterm-addon-clipboard.d.ts", - "repository": "https://github.com/xtermjs/xterm.js/tree/master/addons/xterm-addon-clipboard", + "main": "lib/addon-clipboard.js", + "types": "typings/addon-clipboard.d.ts", + "repository": "https://github.com/xtermjs/xterm.js/tree/master/addons/addon-clipboard", "license": "MIT", "keywords": [ "terminal", diff --git a/addons/xterm-addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts similarity index 95% rename from addons/xterm-addon-clipboard/src/ClipboardAddon.ts rename to addons/addon-clipboard/src/ClipboardAddon.ts index 51c113db20..ccb75545b5 100644 --- a/addons/xterm-addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -4,7 +4,7 @@ */ import { ClipboardProvider } from './ClipboardProvider'; -import { IClipboardProvider, IDisposable, ITerminalAddon, Terminal } from 'xterm'; +import { IClipboardProvider, IDisposable, ITerminalAddon, Terminal } from '@xterm/xterm'; export class ClipboardAddon implements ITerminalAddon { private _disposable: IDisposable | undefined; diff --git a/addons/xterm-addon-clipboard/src/ClipboardProvider.ts b/addons/addon-clipboard/src/ClipboardProvider.ts similarity index 93% rename from addons/xterm-addon-clipboard/src/ClipboardProvider.ts rename to addons/addon-clipboard/src/ClipboardProvider.ts index e7f9ff8a57..c14dbe57cf 100644 --- a/addons/xterm-addon-clipboard/src/ClipboardProvider.ts +++ b/addons/addon-clipboard/src/ClipboardProvider.ts @@ -4,7 +4,7 @@ */ import { Base64 } from 'js-base64'; -import { ClipboardSelectionType, IClipboardProvider } from 'xterm'; +import { ClipboardSelectionType, IClipboardProvider } from '@xterm/xterm'; export class ClipboardProvider implements IClipboardProvider { constructor( diff --git a/addons/xterm-addon-clipboard/src/tsconfig.json b/addons/addon-clipboard/src/tsconfig.json similarity index 100% rename from addons/xterm-addon-clipboard/src/tsconfig.json rename to addons/addon-clipboard/src/tsconfig.json diff --git a/addons/xterm-addon-clipboard/test/ClipboardAddon.api.ts b/addons/addon-clipboard/test/ClipboardAddon.api.ts similarity index 100% rename from addons/xterm-addon-clipboard/test/ClipboardAddon.api.ts rename to addons/addon-clipboard/test/ClipboardAddon.api.ts diff --git a/addons/xterm-addon-clipboard/test/tsconfig.json b/addons/addon-clipboard/test/tsconfig.json similarity index 100% rename from addons/xterm-addon-clipboard/test/tsconfig.json rename to addons/addon-clipboard/test/tsconfig.json diff --git a/addons/xterm-addon-clipboard/tsconfig.json b/addons/addon-clipboard/tsconfig.json similarity index 100% rename from addons/xterm-addon-clipboard/tsconfig.json rename to addons/addon-clipboard/tsconfig.json diff --git a/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts b/addons/addon-clipboard/typings/addon-clipboard.d.ts similarity index 95% rename from addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts rename to addons/addon-clipboard/typings/addon-clipboard.d.ts index c64cf6256f..651e0a9ae9 100644 --- a/addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts +++ b/addons/addon-clipboard/typings/addon-clipboard.d.ts @@ -5,7 +5,7 @@ import { Terminal, ITerminalAddon, IClipboardProvider, ClipboardSelection as ClipboardSelectionType } from 'xterm'; -declare module 'xterm-addon-clipboard' { +declare module '@xterm/addon-clipboard' { export class ClipboardProvider implements IClipboardProvider{ public readText(selection: ClipboardSelectionType): Promise; public writeText(selection: ClipboardSelectionType, data: string): Promise; diff --git a/addons/xterm-addon-clipboard/webpack.config.js b/addons/addon-clipboard/webpack.config.js similarity index 92% rename from addons/xterm-addon-clipboard/webpack.config.js rename to addons/addon-clipboard/webpack.config.js index 0def5bbc85..c00191abfb 100644 --- a/addons/xterm-addon-clipboard/webpack.config.js +++ b/addons/addon-clipboard/webpack.config.js @@ -6,7 +6,7 @@ const path = require('path'); const addonName = 'ClipboardAddon'; -const mainFile = 'xterm-addon-clipboard.js'; +const mainFile = 'addon-clipboard.js'; module.exports = { entry: `./out/${addonName}.js`, diff --git a/addons/xterm-addon-clipboard/yarn.lock b/addons/addon-clipboard/yarn.lock similarity index 100% rename from addons/xterm-addon-clipboard/yarn.lock rename to addons/addon-clipboard/yarn.lock diff --git a/bin/publish.js b/bin/publish.js index 989b849df5..fdf9f49795 100644 --- a/bin/publish.js +++ b/bin/publish.js @@ -29,7 +29,7 @@ if (changedFiles.some(e => e.search(/^addons\//) === -1)) { const addonPackageDirs = [ path.resolve(__dirname, '../addons/addon-attach'), path.resolve(__dirname, '../addons/addon-canvas'), - path.resolve(__dirname, '../addons/xterm-addon-clipboard'), + path.resolve(__dirname, '../addons/addon-clipboard'), path.resolve(__dirname, '../addons/addon-fit'), path.resolve(__dirname, '../addons/addon-image'), path.resolve(__dirname, '../addons/addon-ligatures'), diff --git a/demo/client.ts b/demo/client.ts index f88dcff128..99ac08363a 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -12,7 +12,7 @@ import { Terminal } from '../out/browser/public/Terminal'; import { AttachAddon } from '../addons/addon-attach/out/AttachAddon'; import { CanvasAddon } from '../addons/addon-canvas/out/CanvasAddon'; -import { ClipboardAddon } from '../addons/xterm-addon-clipboard/out/ClipboardAddon'; +import { ClipboardAddon } from '../addons/addon-clipboard/out/ClipboardAddon'; import { FitAddon } from '../addons/addon-fit/out/FitAddon'; import { SearchAddon, ISearchOptions } from '../addons/addon-search/out/SearchAddon'; import { SerializeAddon } from '../addons/addon-serialize/out/SerializeAddon'; @@ -33,7 +33,7 @@ if ('WebAssembly' in window) { // Use webpacked version (yarn package) // import { Terminal } from '../lib/xterm'; // import { AttachAddon } from '@xterm/addon-attach'; -// import { ClipboardAddon } from 'xterm-addon-clipboard'; +// import { ClipboardAddon } from '@xterm/addon-clipboard'; // import { FitAddon } from '@xterm/addon-fit'; // import { ImageAddon } from '@xterm/addon-image'; // import { SearchAddon, ISearchOptions } from '@xterm/addon-search'; diff --git a/demo/tsconfig.json b/demo/tsconfig.json index ac318daa01..2e72c5016c 100644 --- a/demo/tsconfig.json +++ b/demo/tsconfig.json @@ -7,7 +7,7 @@ "baseUrl": ".", "paths": { "addon-attach": ["../addons/addon-attach"], - "xterm-addon-clipboard": ["../addons/xterm-addon-clipboard"], + "xterm-addon-clipboard": ["../addons/addon-clipboard"], "addon-fit": ["../addons/addon-fit"], "addon-image": ["../addons/addon-image"], "addon-search": ["../addons/addon-search"], diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index f5bf1dde24..7815cef503 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -17,7 +17,7 @@ import { DEFAULT_OPTIONS } from 'common/services/OptionsService'; import { clone } from 'common/Clone'; import { BufferService } from 'common/services/BufferService'; import { CoreService } from 'common/services/CoreService'; -import { ClipboardSelectionType } from 'xterm'; +import { ClipboardSelectionType } from '@xterm/xterm'; function getCursor(bufferService: IBufferService): number[] { return [ diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts index b38bffa1dc..8bc725fc28 100644 --- a/src/common/InputHandler.ts +++ b/src/common/InputHandler.ts @@ -22,7 +22,7 @@ import { OscHandler } from 'common/parser/OscParser'; import { DcsHandler } from 'common/parser/DcsParser'; import { IBuffer } from 'common/buffer/Types'; import { parseColor } from 'common/input/XParseColor'; -import { ClipboardSelectionType } from 'xterm'; +import { ClipboardSelectionType } from '@xterm/xterm'; /** * Map collect to glevel. Used in `selectCharset`. diff --git a/tsconfig.all.json b/tsconfig.all.json index 2092992c51..d40761f337 100644 --- a/tsconfig.all.json +++ b/tsconfig.all.json @@ -9,7 +9,7 @@ { "path": "./test/playwright" }, { "path": "./addons/addon-attach" }, { "path": "./addons/addon-canvas" }, - { "path": "./addons/xterm-addon-clipboard" }, + { "path": "./addons/addon-clipboard" }, { "path": "./addons/addon-fit" }, { "path": "./addons/addon-image" }, { "path": "./addons/addon-ligatures" }, From bdcba308df2e9d01a30b4e7a62c86e146d5bc0e8 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Fri, 3 Nov 2023 12:44:19 -0700 Subject: [PATCH 06/18] Upload clipboard addon artifacts --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0a64b6a7cc..65b155a499 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,8 @@ jobs: ./addons/addon-attach/out-test/* \ ./addons/addon-canvas/out/* \ ./addons/addon-canvas/out-test/* \ + ./addons/addon-clipboard/out/* \ + ./addons/addon-clipboard/out-test/* \ ./addons/addon-fit/out/* \ ./addons/addon-fit/out-test/* \ ./addons/addon-image/out/* \ From 94648eb4e9ef1743d39c6d1cd7fac2e0e4cef83f Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 02:47:26 +0300 Subject: [PATCH 07/18] Update addon-clipboard --- addons/addon-clipboard/.gitignore | 2 +- addons/addon-clipboard/package.json | 2 +- addons/addon-clipboard/src/ClipboardAddon.ts | 117 +++++++++++++++++- .../addon-clipboard/src/ClipboardProvider.ts | 35 ------ addons/addon-clipboard/src/tsconfig.json | 19 ++- .../test/ClipboardAddon.api.ts | 2 +- addons/addon-clipboard/test/tsconfig.json | 3 +- .../typings/addon-clipboard.d.ts | 79 ++++++++++-- addons/addon-clipboard/yarn.lock | 6 +- 9 files changed, 201 insertions(+), 64 deletions(-) delete mode 100644 addons/addon-clipboard/src/ClipboardProvider.ts diff --git a/addons/addon-clipboard/.gitignore b/addons/addon-clipboard/.gitignore index a9f4ed5456..3063f07d55 100644 --- a/addons/addon-clipboard/.gitignore +++ b/addons/addon-clipboard/.gitignore @@ -1,2 +1,2 @@ lib -node_modules \ No newline at end of file +node_modules diff --git a/addons/addon-clipboard/package.json b/addons/addon-clipboard/package.json index 2e7cd1eb0a..06d0730aa1 100644 --- a/addons/addon-clipboard/package.json +++ b/addons/addon-clipboard/package.json @@ -21,7 +21,7 @@ "prepublishOnly": "npm run package" }, "peerDependencies": { - "xterm": "^5.3.0" + "@xterm/xterm": "^5.5.0" }, "dependencies": { "js-base64": "^3.7.5" diff --git a/addons/addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts index ccb75545b5..1a8c6101d4 100644 --- a/addons/addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -3,18 +3,125 @@ * @license MIT */ -import { ClipboardProvider } from './ClipboardProvider'; -import { IClipboardProvider, IDisposable, ITerminalAddon, Terminal } from '@xterm/xterm'; +import type { IDisposable, ITerminalAddon, Terminal } from '@xterm/xterm'; +import { type IClipboardProvider, ClipboardSelectionType } from '@xterm/addon-clipboard'; +import { Base64 as JSBase64 } from 'js-base64'; export class ClipboardAddon implements ITerminalAddon { - private _disposable: IDisposable | undefined; - constructor(private _provider: IClipboardProvider = new ClipboardProvider()) {} + private readonly _provider: IClipboardProvider; + private _terminal?: Terminal; + private _disposable?: IDisposable; + + constructor(provider: IClipboardProvider = new ClipboardProvider()) { + this._provider = provider; + } public activate(terminal: Terminal): void { - this._disposable = terminal.registerClipboardProvider(this._provider); + this._disposable = terminal.parser.registerOscHandler(52, this._setOrReportClipboard); + this._terminal = terminal; } public dispose(): void { return this._disposable?.dispose(); } + + private _setOrReportClipboard(data: string): boolean | Promise { + const args = data.split(';'); + if (args.length < 2) { + return true; + } + + const pc = args[0]; + const pd = args[1]; + if (pd.length === 0) { + return true; + } + + switch (pc) { + case ClipboardSelectionType.SYSTEM: + case ClipboardSelectionType.PRIMARY: + try { + if (pd === '?') { + // Report clipboard + return this._provider.readText(pc).then(data => { + this._terminal?.input(data, false); + return true; + }); + } + return this._provider.writeText(pc, pd).then(() => true); + } catch (e) { + console.error(e); + } + } + + return true; + } +} + +export class ClipboardProvider implements IClipboardProvider { + private _base64: IBase64; + public limit: number; + + constructor( + /** + * The base64 encoder/decoder to use. + */ + base64: IBase64 = new Base64(), + + /** + * The maximum amount of data that can be copied to the clipboard. + * Zero means no limit. + */ + limit: number = 0 // unlimited + ){ + this._base64 = base64; + this.limit = limit; + } + + public readText(selection: ClipboardSelectionType): Promise { + if (selection !== 'c') { + return Promise.resolve(''); + } + return navigator.clipboard.readText().then(this._base64.encodeText); + } + + public writeText(selection: ClipboardSelectionType, data: string): Promise { + if (selection !== 'c' || (this.limit > 0 && data.length > this.limit)) { + return Promise.resolve(); + } + try { + const text = this._base64.decodeText(data); + return navigator.clipboard.writeText(text); + } catch { + // clear the clipboard if the data is not valid base64 + return navigator.clipboard.writeText(''); + } + } +} + +export interface IBase64 { + /** + * Converts a utf-8 string to a base64 string. + * @param data The utf-8 string to convert to base64 string. + */ + encodeText(data: string): string; + + /** + * Converts a base64 string to a utf-8 string. + * @param data The base64 string to convert to utf-8 string. + */ + decodeText(data: string): string; +} + +export class Base64 implements IBase64 { + public encodeText(data: string): string { + return JSBase64.encode(data); + } + public decodeText(data: string): string { + const text = JSBase64.decode(data); + if (!JSBase64.isValid(data) || JSBase64.encode(text) !== data) { + return ''; + } + return text; + } } diff --git a/addons/addon-clipboard/src/ClipboardProvider.ts b/addons/addon-clipboard/src/ClipboardProvider.ts deleted file mode 100644 index c14dbe57cf..0000000000 --- a/addons/addon-clipboard/src/ClipboardProvider.ts +++ /dev/null @@ -1,35 +0,0 @@ -/** - * 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 { - if (selection !== 'c') { - return Promise.resolve(''); - } - return navigator.clipboard.readText().then((text) => - Base64.encode(text)); - } - public writeText(selection: ClipboardSelectionType, data: string): Promise { - 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); - } -} diff --git a/addons/addon-clipboard/src/tsconfig.json b/addons/addon-clipboard/src/tsconfig.json index 55cdc7c5de..b610728084 100644 --- a/addons/addon-clipboard/src/tsconfig.json +++ b/addons/addon-clipboard/src/tsconfig.json @@ -2,22 +2,24 @@ "compilerOptions": { "module": "commonjs", "target": "es2021", - "sourceMap": true, - "outDir": "../out", + "lib": [ + "dom", + "es2015" + ], "rootDir": ".", + "outDir": "../out", + "sourceMap": true, + "removeComments": true, "strict": true, - "noUnusedLocals": true, - "preserveWatchOutput": true, "types": [ "../../../node_modules/@types/mocha" ], - "baseUrl": ".", "paths": { "browser/*": [ "../../../src/browser/*" ], - "common/*": [ - "../../../src/common/*" + "@xterm/addon-clipboard": [ + "../typings/addon-clipboard.d.ts" ] } }, @@ -28,9 +30,6 @@ "references": [ { "path": "../../../src/browser" - }, - { - "path": "../../../src/common" } ] } diff --git a/addons/addon-clipboard/test/ClipboardAddon.api.ts b/addons/addon-clipboard/test/ClipboardAddon.api.ts index d6eea6aba6..962cb3f218 100644 --- a/addons/addon-clipboard/test/ClipboardAddon.api.ts +++ b/addons/addon-clipboard/test/ClipboardAddon.api.ts @@ -34,7 +34,7 @@ describe('ClipboardAddon', () => { page = await context.newPage(); await page.setViewportSize({ width, height }); await page.goto(APP); - await openTerminal(page, { allowClipboardAccess: true }); + await openTerminal(page); await page.evaluate(` window.clipboardAddon = new ClipboardAddon(); window.term.loadAddon(window.clipboardAddon); diff --git a/addons/addon-clipboard/test/tsconfig.json b/addons/addon-clipboard/test/tsconfig.json index ffa1c5fa72..67ad42b720 100644 --- a/addons/addon-clipboard/test/tsconfig.json +++ b/addons/addon-clipboard/test/tsconfig.json @@ -3,7 +3,7 @@ "module": "commonjs", "target": "es2021", "lib": [ - "es2021" + "es2015" ], "rootDir": ".", "outDir": "../out-test", @@ -13,6 +13,7 @@ "types": [ "../../../node_modules/@types/mocha", "../../../node_modules/@types/node", + "../../../out-test/api/TestUtils" ] }, "include": [ diff --git a/addons/addon-clipboard/typings/addon-clipboard.d.ts b/addons/addon-clipboard/typings/addon-clipboard.d.ts index 651e0a9ae9..06cb370370 100644 --- a/addons/addon-clipboard/typings/addon-clipboard.d.ts +++ b/addons/addon-clipboard/typings/addon-clipboard.d.ts @@ -3,14 +3,9 @@ * @license MIT */ -import { Terminal, ITerminalAddon, IClipboardProvider, ClipboardSelection as ClipboardSelectionType } from 'xterm'; +import { Terminal, ITerminalAddon } from '@xterm/xterm'; declare module '@xterm/addon-clipboard' { - export class ClipboardProvider implements IClipboardProvider{ - public readText(selection: ClipboardSelectionType): Promise; - public writeText(selection: ClipboardSelectionType, data: string): Promise; - } - /** * An xterm.js addon that enables accessing the system clipboard from * xterm.js. @@ -19,7 +14,7 @@ declare module '@xterm/addon-clipboard' { /** * Creates a new clipboard addon. */ - constructor(_provider: IClipboardProvider); + constructor(provider?: IClipboardProvider); /** * Activates the addon @@ -32,4 +27,74 @@ declare module '@xterm/addon-clipboard' { */ public dispose(): void } + + /** + * The clipboard provider interface that enables xterm.js to access the system clipboard. + */ + export class ClipboardProvider implements IClipboardProvider{ + /** + * Creates a new clipboard provider. + * @param _base64 The base64 encoder/decoder to use. + */ + constructor(base64?: IBase64, limit?: number); + + /** + * Reads text from the clipboard. + * @param selection The selection type to read from. + * @returns A promise that resolves with the text from the clipboard. + */ + public readText(selection: ClipboardSelectionType): Promise; + + /** + * Writes text to the clipboard. + * @param selection The selection type to write to. + * @param data The text to write to the clipboard. + * @returns A promise that resolves when the text has been written to the clipboard. + */ + public writeText(selection: ClipboardSelectionType, data: string): Promise; + } + + + export interface IBase64 { + /** + * Converts a utf-8 string to a base64 string. + * @param data The utf-8 string to convert to base64 string. + */ + encodeText(data: string): string; + + /** + * Converts a base64 string to a utf-8 string. + * @param data The base64 string to convert to utf-8 string. + */ + decodeText(data: string): string; + } + + export interface IClipboardProvider { + /** + * Gets the clipboard content. + * @param selection The clipboard selection to read. + * @returns A promise that resolves with the base64 encoded data. + */ + readText(selection: ClipboardSelectionType): Promise; + + /** + * Sets the clipboard content. + * @param selection The clipboard selection to set. + * @param data The base64 encoded data to set. If the data is invalid + * base64, the clipboard is cleared. + */ + writeText(selection: ClipboardSelectionType, data: string): Promise; + } + + /** + * Clipboard selection type. This is used to specify which selection buffer to + * read or write to. + * - SYSTEM `c`: The system clipboard. + * - PRIMARY `p`: The primary clipboard. This is provided for compatibility + * with Linux X11. + */ + export const enum ClipboardSelectionType { + SYSTEM = 'c', + PRIMARY = 'p', + } } diff --git a/addons/addon-clipboard/yarn.lock b/addons/addon-clipboard/yarn.lock index de7e5b432e..01d54e3673 100644 --- a/addons/addon-clipboard/yarn.lock +++ b/addons/addon-clipboard/yarn.lock @@ -3,6 +3,6 @@ js-base64@^3.7.5: - version "3.7.5" - resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.5.tgz#21e24cf6b886f76d6f5f165bfcd69cc55b9e3fca" - integrity sha512-3MEt5DTINKqfScXKfJFrRbxkrnk2AxPWGBL/ycjz4dK8iqiSJ06UxD8jh8xuh6p10TX4t2+7FsBYVxxQbMg+qA== + version "3.7.7" + resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-3.7.7.tgz#e51b84bf78fbf5702b9541e2cb7bfcb893b43e79" + integrity sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw== From fad97bc4d509b0d683b6d5c1cd49f7dedfe95140 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 02:47:56 +0300 Subject: [PATCH 08/18] Clean up --- src/browser/Terminal.ts | 27 +-------- src/browser/TestUtils.test.ts | 5 +- src/browser/public/Terminal.ts | 5 +- src/common/InputHandler.test.ts | 85 +-------------------------- src/common/InputHandler.ts | 60 +------------------ src/common/Types.d.ts | 14 +---- src/common/services/OptionsService.ts | 3 +- src/common/services/Services.ts | 1 - test/playwright/Terminal.test.ts | 54 ----------------- test/playwright/TestUtils.ts | 1 - typings/xterm.d.ts | 43 -------------- 11 files changed, 8 insertions(+), 290 deletions(-) diff --git a/src/browser/Terminal.ts b/src/browser/Terminal.ts index 2af253b537..0e945aa92b 100644 --- a/src/browser/Terminal.ts +++ b/src/browser/Terminal.ts @@ -46,7 +46,7 @@ import { CoreTerminal } from 'common/CoreTerminal'; import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter'; import { MutableDisposable, toDisposable } from 'common/Lifecycle'; import * as Browser from 'common/Platform'; -import { ColorRequestType, CoreMouseAction, CoreMouseButton, CoreMouseEventType, IClipboardEvent, IColorEvent, ITerminalOptions, KeyboardResultType, ScrollSource, SpecialColorIndex } from 'common/Types'; +import { ColorRequestType, CoreMouseAction, CoreMouseButton, CoreMouseEventType, IColorEvent, ITerminalOptions, KeyboardResultType, ScrollSource, SpecialColorIndex } from 'common/Types'; import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { IBuffer } from 'common/buffer/Types'; import { C0, C1_ESCAPED } from 'common/data/EscapeSequences'; @@ -54,7 +54,7 @@ import { evaluateKeyboardEvent } from 'common/input/Keyboard'; import { toRgbString } from 'common/input/XParseColor'; import { DecorationService } from 'common/services/DecorationService'; import { IDecorationService } from 'common/services/Services'; -import { IDecoration, IDecorationOptions, IDisposable, ILinkProvider, IMarker, IClipboardProvider } from '@xterm/xterm'; +import { IDecoration, IDecorationOptions, IDisposable, ILinkProvider, IMarker } from '@xterm/xterm'; import { WindowsOptionsReportType } from '../common/InputHandler'; import { AccessibilityManager } from './AccessibilityManager'; import { LinkProviderService } from 'browser/services/LinkProviderService'; @@ -121,7 +121,6 @@ export class Terminal extends CoreTerminal implements ITerminal { public viewport: IViewport | undefined; private _compositionHelper: ICompositionHelper | undefined; private _accessibilityManager: MutableDisposable = this.register(new MutableDisposable()); - private _clipboardProvider: IClipboardProvider | undefined; private readonly _onCursorMove = this.register(new EventEmitter()); public readonly onCursorMove = this._onCursorMove.event; @@ -167,7 +166,6 @@ export class Terminal extends CoreTerminal implements ITerminal { this.register(this._inputHandler.onRequestReset(() => this.reset())); this.register(this._inputHandler.onRequestWindowsOptionsReport(type => this._reportWindowsOptions(type))); this.register(this._inputHandler.onColor((event) => this._handleColorEvent(event))); - this.register(this._inputHandler.onClipboard((event) => this._handleClipboardEvent(event))); this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove)); this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange)); this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter)); @@ -905,15 +903,6 @@ export class Terminal extends CoreTerminal implements ITerminal { return this._linkProviderService.registerLinkProvider(linkProvider); } - public registerClipboardProvider(provider: IClipboardProvider): IDisposable { - this._clipboardProvider = provider; - return { - dispose: () => { - this._clipboardProvider = undefined; - } - }; - } - public registerCharacterJoiner(handler: CharacterJoinerHandler): number { if (!this._characterJoinerService) { throw new Error('Terminal must be opened first'); @@ -1314,18 +1303,6 @@ export class Terminal extends CoreTerminal implements ITerminal { } } - private _handleClipboardEvent(ev: IClipboardEvent): void { - if (!this._clipboardProvider) { - return; - } - if (ev.data === '?') { - this._clipboardProvider.readText(ev.selection).then(data => - this.coreService.triggerDataEvent(data)); - return; - } - this._clipboardProvider.writeText(ev.selection, ev.data); - } - // TODO: Remove cancel function and cancelEvents option public cancel(ev: Event, force?: boolean): boolean | undefined { if (!this.options.cancelEvents && !force) { diff --git a/src/browser/TestUtils.test.ts b/src/browser/TestUtils.test.ts index 09199ae02c..c7c8438cc0 100644 --- a/src/browser/TestUtils.test.ts +++ b/src/browser/TestUtils.test.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { IDisposable, IMarker, ILinkProvider, IDecorationOptions, IDecoration, IClipboardProvider } from '@xterm/xterm'; +import { IDisposable, IMarker, ILinkProvider, IDecorationOptions, IDecoration } from '@xterm/xterm'; import { IEvent, EventEmitter } from 'common/EventEmitter'; import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService, IMouseService, IRenderService, ISelectionService, IThemeService } from 'browser/services/Services'; import { IRenderDimensions, IRenderer, IRequestRedrawEvent } from 'browser/renderer/shared/Types'; @@ -111,9 +111,6 @@ export class MockTerminal implements ITerminal { public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { throw new Error('Method not implemented.'); } - public registerClipboardProvider(provider: IClipboardProvider): IDisposable { - throw new Error('Method not implemented.'); - } public hasSelection(): boolean { throw new Error('Method not implemented.'); } diff --git a/src/browser/public/Terminal.ts b/src/browser/public/Terminal.ts index 7589828015..a6349225df 100644 --- a/src/browser/public/Terminal.ts +++ b/src/browser/public/Terminal.ts @@ -13,7 +13,7 @@ import { AddonManager } from 'common/public/AddonManager'; import { BufferNamespaceApi } from 'common/public/BufferNamespaceApi'; import { ParserApi } from 'common/public/ParserApi'; import { UnicodeApi } from 'common/public/UnicodeApi'; -import { IBufferNamespace as IBufferNamespaceApi, IClipboardProvider, IDecoration, IDecorationOptions, IDisposable, ILinkProvider, ILocalizableStrings, IMarker, IModes, IParser, ITerminalAddon, Terminal as ITerminalApi, ITerminalInitOnlyOptions, IUnicodeHandling } from '@xterm/xterm'; +import { IBufferNamespace as IBufferNamespaceApi, IDecoration, IDecorationOptions, IDisposable, ILinkProvider, ILocalizableStrings, IMarker, IModes, IParser, ITerminalAddon, Terminal as ITerminalApi, ITerminalInitOnlyOptions, IUnicodeHandling } from '@xterm/xterm'; /** * The set of options that only have an effect when set in the Terminal constructor. @@ -174,9 +174,6 @@ export class Terminal extends Disposable implements ITerminalApi { this._verifyPositiveIntegers(decorationOptions.x ?? 0, decorationOptions.width ?? 0, decorationOptions.height ?? 0); return this._core.registerDecoration(decorationOptions); } - public registerClipboardProvider(provider: IClipboardProvider): IDisposable { - return this._core.registerClipboardProvider(provider); - } public hasSelection(): boolean { return this._core.hasSelection(); } diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index 7815cef503..d52077bf48 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -5,7 +5,7 @@ import { assert } from 'chai'; import { InputHandler } from 'common/InputHandler'; -import { IBufferLine, IAttributeData, IColorEvent, ColorIndex, ColorRequestType, SpecialColorIndex, IClipboardEvent, ClipboardEventType } from 'common/Types'; +import { IBufferLine, IAttributeData, IColorEvent, ColorIndex, ColorRequestType, SpecialColorIndex } from 'common/Types'; import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { CellData } from 'common/buffer/CellData'; import { Attributes, BgFlags, UnderlineStyle } from 'common/buffer/Constants'; @@ -17,7 +17,6 @@ import { DEFAULT_OPTIONS } from 'common/services/OptionsService'; import { clone } from 'common/Clone'; import { BufferService } from 'common/services/BufferService'; import { CoreService } from 'common/services/CoreService'; -import { ClipboardSelectionType } from '@xterm/xterm'; function getCursor(bufferService: IBufferService): number[] { return [ @@ -1982,88 +1981,6 @@ describe('InputHandler', () => { assert.deepEqual(stack, [[{ type: ColorRequestType.SET, index: 0, color: [170, 187, 204] }, { type: ColorRequestType.SET, index: 123, color: [0, 17, 34] }]]); stack.length = 0; }); - describe('52: manipulate selection data', async () => { - const testDataRaw = 'hello world'; - const testDataB64 = 'aGVsbG8gd29ybGQ='; - optionsService.options.allowClipboardAccess = true; - const stack: IClipboardEvent[] = []; - inputHandler.onClipboard(ev => stack.push(ev)); - await inputHandler.parseP(`\x1b]52;c;\x07`); - await inputHandler.parseP(`\x1b]52;c;${testDataRaw}\x07`); - await inputHandler.parseP(`\x1b]52;c;${testDataB64}\x07`); - await inputHandler.parseP(`\x1b]52;c;${testDataB64}invalid\x07`); - await inputHandler.parseP(`\x1b]52;c;!\x07`); - await inputHandler.parseP(`\x1b]52;c;?\x07`); - await inputHandler.parseP(`\x1b]52;p;\x07`); - await inputHandler.parseP(`\x1b]52;p;${testDataRaw}\x07`); - await inputHandler.parseP(`\x1b]52;p;${testDataB64}\x07`); - await inputHandler.parseP(`\x1b]52;p;${testDataB64}invalid\x07`); - await inputHandler.parseP(`\x1b]52;p;!\x07`); - await inputHandler.parseP(`\x1b]52;p;?\x07`); - assert.deepEqual(stack, [ - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.SYSTEM, - data: '' - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.SYSTEM, - data: testDataRaw - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.SYSTEM, - data: testDataB64 - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.SYSTEM, - data: testDataB64+'invalid' - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.SYSTEM, - data: '!' - }, - { - type: ClipboardEventType.REPORT, - selection: ClipboardSelectionType.SYSTEM, - data: '?' - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.PRIMARY, - data: '' - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.PRIMARY, - data: testDataRaw - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.PRIMARY, - data: testDataB64 - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.PRIMARY, - data: testDataB64+'invalid' - }, - { - type: ClipboardEventType.SET, - selection: ClipboardSelectionType.PRIMARY, - data: '!' - }, - { - type: ClipboardEventType.REPORT, - selection: ClipboardSelectionType.PRIMARY, - data: '?' - } - ]); - stack.length = 0; - }); it('104: restore events', async () => { const stack: IColorEvent[] = []; inputHandler.onColor(ev => stack.push(ev)); diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts index 8bc725fc28..a4b8c64b02 100644 --- a/src/common/InputHandler.ts +++ b/src/common/InputHandler.ts @@ -4,7 +4,7 @@ * @license MIT */ -import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IColorEvent, IParseStack, ColorIndex, ColorRequestType, SpecialColorIndex, IClipboardEvent, ClipboardEventType } from 'common/Types'; +import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IColorEvent, IParseStack, ColorIndex, ColorRequestType, SpecialColorIndex } from 'common/Types'; import { C0, C1 } from 'common/data/EscapeSequences'; import { CHARSETS, DEFAULT_CHARSET } from 'common/data/Charsets'; import { EscapeSequenceParser } from 'common/parser/EscapeSequenceParser'; @@ -22,7 +22,6 @@ import { OscHandler } from 'common/parser/OscParser'; import { DcsHandler } from 'common/parser/DcsParser'; import { IBuffer } from 'common/buffer/Types'; import { parseColor } from 'common/input/XParseColor'; -import { ClipboardSelectionType } from '@xterm/xterm'; /** * Map collect to glevel. Used in `selectCharset`. @@ -160,8 +159,6 @@ export class InputHandler extends Disposable implements IInputHandler { public readonly onTitleChange = this._onTitleChange.event; private readonly _onColor = this.register(new EventEmitter()); public readonly onColor = this._onColor.event; - private readonly _onClipboard = this.register(new EventEmitter()); - public readonly onClipboard = this._onClipboard.event; private _parseStack: IParseStack = { paused: false, @@ -323,7 +320,6 @@ export class InputHandler extends Disposable implements IInputHandler { // 50 - Set Font to Pt. // 51 - reserved for Emacs shell. // 52 - Manipulate Selection Data. - this._parser.registerOscHandler(52, new OscHandler(data => this.setOrReportClipboard(data))); // 104 ; c - Reset Color Number c. this._parser.registerOscHandler(104, new OscHandler(data => this.restoreIndexedColor(data))); // 105 ; c - Reset Special Color Number c. @@ -3081,60 +3077,6 @@ export class InputHandler extends Disposable implements IInputHandler { return this._setOrReportSpecialColor(data, 2); } - private _setOrReportClipboard(data: string): boolean { - if (!this._optionsService.options.allowClipboardAccess) { - return true; - } - const args = data.split(';'); - if (args.length < 2) { - return true; - } - const pc = args[0]; - const pd = args[1]; - if (pd.length === 0) { - return true; - } - switch (pc) { - case ClipboardSelectionType.SYSTEM: - case ClipboardSelectionType.PRIMARY: - this._onClipboard.fire({ - type: pd === '?' ? ClipboardEventType.REPORT : ClipboardEventType.SET, - selection: pc, - data: pd - }); - break; - } - return true; - } - - /** - * OSC 52 ; ; | ST - set or query selection and clipboard data - * - * Test case: - * - * ```sh - * printf "\e]52;c;%s\a" "$(echo -n "Hello, World" | base64)" - * ``` - * - * @vt: #Y OSC 52 "Manipulate Selection Data" "OSC 52 ; Pc ; Pd BEL" "Set or query selection and clipboard data." - * Pc is the selection name. Can be one of: - * - `c` - clipboard - * - `p` - primary - * - `q` - secondary - * - `s` - select - * - `0-7` - cut-buffers 0-7 - * - * Only the `c` selection (clipboard) is supported by xterm.js. The browser - * Clipboard API only supports the clipboard selection. - * - * Pd is the base64 encoded data. - * If Pd is `?`, the terminal returns the current clipboard contents. - * If Pd is neither base64 encoded nor `?`, then the clipboard is cleared. - */ - public setOrReportClipboard(data: string): boolean { - return this._setOrReportClipboard(data); - } - /** * OSC 104 ; ST - restore ANSI color * diff --git a/src/common/Types.d.ts b/src/common/Types.d.ts index 99815badaa..251a09f614 100644 --- a/src/common/Types.d.ts +++ b/src/common/Types.d.ts @@ -9,7 +9,7 @@ import { Attributes, UnderlineStyle } from 'common/buffer/Constants'; // eslint- import { IBufferSet } from 'common/buffer/Types'; import { IParams } from 'common/parser/Types'; import { ICoreMouseService, ICoreService, IOptionsService, IUnicodeService } from 'common/services/Services'; -import { ClipboardSelectionType as ClipboardSelectionType, IFunctionIdentifier, ITerminalOptions as IPublicTerminalOptions } from '@xterm/xterm'; +import { IFunctionIdentifier, ITerminalOptions as IPublicTerminalOptions } from '@xterm/xterm'; export interface ICoreTerminal { coreMouseService: ICoreMouseService; @@ -447,17 +447,6 @@ export interface IColorRestoreRequest { } export type IColorEvent = (IColorReportRequest | IColorSetRequest | IColorRestoreRequest)[]; -export const enum ClipboardEventType { - REPORT = 0, - SET = 1 -} - -export interface IClipboardEvent { - type: ClipboardEventType; - selection: ClipboardSelectionType; - data: string; -} - /** * Calls the parser and handles actions generated by the parser. */ @@ -527,7 +516,6 @@ export interface IInputHandler { /** OSC 10 */ setOrReportFgColor(data: string): boolean; /** OSC 11 */ setOrReportBgColor(data: string): boolean; /** OSC 12 */ setOrReportCursorColor(data: string): boolean; - /** OSC 52 */ setOrReportClipboard(data: string): boolean; /** OSC 104 */ restoreIndexedColor(data: string): boolean; /** OSC 110 */ restoreFgColor(data: string): boolean; /** OSC 111 */ restoreBgColor(data: string): boolean; diff --git a/src/common/services/OptionsService.ts b/src/common/services/OptionsService.ts index b80c667657..3c2d967848 100644 --- a/src/common/services/OptionsService.ts +++ b/src/common/services/OptionsService.ts @@ -54,8 +54,7 @@ export const DEFAULT_OPTIONS: Readonly> = { convertEol: false, termName: 'xterm', cancelEvents: false, - overviewRulerWidth: 0, - allowClipboardAccess: false + overviewRulerWidth: 0 }; const FONT_WEIGHT_OPTIONS: Extract[] = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; diff --git a/src/common/services/Services.ts b/src/common/services/Services.ts index f774979595..210a0afb08 100644 --- a/src/common/services/Services.ts +++ b/src/common/services/Services.ts @@ -206,7 +206,6 @@ export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '50 export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'off'; export interface ITerminalOptions { - allowClipboardAccess?: boolean; allowProposedApi?: boolean; allowTransparency?: boolean; altClickMovesCursor?: boolean; diff --git a/test/playwright/Terminal.test.ts b/test/playwright/Terminal.test.ts index 13c0f8d84b..19f6eb315e 100644 --- a/test/playwright/Terminal.test.ts +++ b/test/playwright/Terminal.test.ts @@ -770,60 +770,6 @@ test.describe('API Integration Tests', () => { }); }); - test.describe('registerClipboardProvider', () => { - async function registerClipboardProvider(ctx: ITestContext): Promise { - await ctx.page.evaluate(`window.clipboard = ''`); - await ctx.page.evaluate(`window.term._disposables.push( - window.term.registerClipboardProvider({ - readText: (selection) => { - return Promise.resolve(window.clipboard); - }, - writeText: (selection, text) => { - window.clipboard = text; - return Promise.resolve(); - } - }) - )`); - } - test('should register clipboard provider', async () => { - await openTerminal(ctx, { allowClipboardAccess: true }); - await registerClipboardProvider(ctx); - await ctx.page.evaluate(`window.term.dispose()`); - }); - test('should ignore clipboard when no provider is registered', async () => { - await openTerminal(ctx, { allowClipboardAccess: true }); - await ctx.proxy.write('\x1b]52;c;foobar\x07'); - strictEqual(await ctx.page.evaluate(`window.clipboard`), ''); - await ctx.page.evaluate(`window.term.dispose()`); - }); - test('should ignore clipboard when allowClipboardAccess is false', async () => { - await openTerminal(ctx, { allowClipboardAccess: false }); - await registerClipboardProvider(ctx); - await ctx.proxy.write('\x1b]52;c;foobar\x07'); - strictEqual(await ctx.page.evaluate(`window.clipboard`), ''); - await ctx.page.evaluate(`window.term.dispose()`); - }); - test('should save to clipboard when writeText is called', async () => { - await openTerminal(ctx, { allowClipboardAccess: true }); - await registerClipboardProvider(ctx); - await ctx.proxy.write('\x1b]52;c;foobar\x07'); - strictEqual(await ctx.page.evaluate(`window.clipboard`), 'foobar'); - await ctx.page.evaluate(`window.term.dispose()`); - }); - test('should read from clipboard when readText is called', async () => { - await openTerminal(ctx, { allowClipboardAccess: true }); - await registerClipboardProvider(ctx); - await ctx.page.evaluate(` - window.data = []; - window.term.onData(e => data.push(e)); - `); - await ctx.proxy.write('\x1b]52;c;foobar\x07'); - await ctx.proxy.write('\x1b]52;c;?\x07'); - deepStrictEqual(await ctx.page.evaluate(`window.data`), ['foobar']); - await ctx.page.evaluate(`window.term.dispose()`); - }); - }); - test.describe('registerLinkProvider', () => { test('should fire provideLinks when hovering cells', async () => { await openTerminal(ctx); diff --git a/test/playwright/TestUtils.ts b/test/playwright/TestUtils.ts index 93179eac13..1427578c11 100644 --- a/test/playwright/TestUtils.ts +++ b/test/playwright/TestUtils.ts @@ -78,7 +78,6 @@ type TerminalProxyCustomOverrides = 'buffer' | ( 'attachCustomWheelEventHandler' | 'registerLinkProvider' | 'registerCharacterJoiner' | - 'registerClipboardProvider' | 'deregisterCharacterJoiner' | 'loadAddon' ); diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index b7beb2f7ae..330082890b 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -24,12 +24,6 @@ declare module '@xterm/xterm' { * An object containing options for the terminal. */ export interface ITerminalOptions { - /** - * Whether to allow clipboard access. When false, any access to the - * clipboard is ignored. The default is false. - */ - allowClipboardAccess?: boolean; - /** * Whether to allow the use of proposed API. When false, any usage of APIs * marked as experimental/proposed will throw an error. The default is @@ -1130,14 +1124,6 @@ declare module '@xterm/xterm' { */ registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined; - /** - * Registers a clipboard provider, allowing custom handling of clipboard - * selection events. This is used primarily to enable accessing the - * clipboard to read/write clipboard data. - * @param provider The provider to register. - */ - registerClipboardProvider(provider: IClipboardProvider): IDisposable; - /** * Gets whether the terminal has an active selection. */ @@ -1919,33 +1905,4 @@ declare module '@xterm/xterm' { */ readonly wraparoundMode: boolean; } - - export interface IClipboardProvider { - /** - * Gets the clipboard content. - * @param selection The clipboard selection to read. - * @returns A promise that resolves with the base64 encoded data. - */ - readText(selection: ClipboardSelectionType): Promise; - - /** - * Sets the clipboard content. - * @param selection The clipboard selection to set. - * @param data The base64 encoded data to set. If the data is invalid - * base64, the clipboard is cleared. - */ - writeText(selection: ClipboardSelectionType, data: string): Promise; - } - - /** - * Clipboard selection type. This is used to specify which selection buffer to - * read or write to. - * - SYSTEM `c`: The system clipboard. - * - PRIMARY `p`: The primary clipboard. This is provided for compatibility - * with Linux X11. - */ - export const enum ClipboardSelectionType { - SYSTEM = 'c', - PRIMARY = 'p', - } } From a70918700774754286bc9e6bd7f03e02a3f07173 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 04:51:55 +0300 Subject: [PATCH 09/18] Update addon-clipboard readme --- addons/addon-clipboard/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/addons/addon-clipboard/README.md b/addons/addon-clipboard/README.md index 087cf0990f..7fef805ca4 100644 --- a/addons/addon-clipboard/README.md +++ b/addons/addon-clipboard/README.md @@ -1,6 +1,7 @@ ## @xterm/addon-clipboard -An addon for [xterm.js](https://github.com/xtermjs/xterm.js) that enables accessing the system clipboard. This addon requires xterm.js v4+. +An addon for [xterm.js](https://github.com/xtermjs/xterm.js) that enables +accessing the system clipboard. This addon requires xterm.js v4+. ### Install @@ -22,8 +23,8 @@ terminal.loadAddon(clipboardAddon); To use a custom clipboard provider ```ts -import { Terminal, IClipboardProvider, ClipboardSelection } from 'xterm'; -import { ClipboardAddon } from '@xterm/addon-clipboard'; +import { Terminal } from '@xterm/xterm'; +import { ClipboardAddon, IClipboardProvider, ClipboardSelectionType } from '@xterm/addon-clipboard'; function b64Encode(data: string): string { // Base64 encode impl @@ -35,10 +36,10 @@ function b64Decode(data: string): string { class MyCustomClipboardProvider implements IClipboardProvider { private _data: string - public readText(selection: ClipboardSelection): Promise { + public readText(selection: ClipboardSelectionType): Promise { return Promise.resolve(b64Encode(this._data)); } - public writeText(selection: ClipboardSelection, data: string): Promise { + public writeText(selection: ClipboardSelectionType, data: string): Promise { this._data = b64Decode(data); return Promise.resolve(); } From ce9e92f8e5a189d8b8fc9457e3bfcd66d3ed01dd Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 05:52:21 +0300 Subject: [PATCH 10/18] Fix OSC52 sequence response --- addons/addon-clipboard/src/ClipboardAddon.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/addons/addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts index 1a8c6101d4..6a04188ee6 100644 --- a/addons/addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -8,12 +8,11 @@ import { type IClipboardProvider, ClipboardSelectionType } from '@xterm/addon-cl import { Base64 as JSBase64 } from 'js-base64'; export class ClipboardAddon implements ITerminalAddon { - private readonly _provider: IClipboardProvider; private _terminal?: Terminal; private _disposable?: IDisposable; - constructor(provider: IClipboardProvider = new ClipboardProvider()) { - this._provider = provider; + constructor(private _provider: IClipboardProvider = new ClipboardProvider()) { + this._provider = _provider; } public activate(terminal: Terminal): void { @@ -44,7 +43,7 @@ export class ClipboardAddon implements ITerminalAddon { if (pd === '?') { // Report clipboard return this._provider.readText(pc).then(data => { - this._terminal?.input(data, false); + this._terminal?.input(`\x1b]52;${pc};${data}\x07`, false); return true; }); } From 00514d41b6937eea3d8d82ff673720c0f1448f86 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 12:45:55 +0300 Subject: [PATCH 11/18] Undo format --- src/common/InputHandler.test.ts | 3 ++- src/common/Types.d.ts | 1 + src/common/services/OptionsService.ts | 2 +- test/api/TestUtils.ts | 5 ++--- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index d52077bf48..8f9a988f14 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -18,6 +18,7 @@ import { clone } from 'common/Clone'; import { BufferService } from 'common/services/BufferService'; import { CoreService } from 'common/services/CoreService'; + function getCursor(bufferService: IBufferService): number[] { return [ bufferService.buffer.x, @@ -1993,7 +1994,7 @@ describe('InputHandler', () => { stack.length = 0; // full ANSI table restore await inputHandler.parseP('\x1b]104\x07'); - assert.deepEqual(stack, [[{ type: ColorRequestType.RESTORE }]]); + assert.deepEqual(stack, [[{ type: ColorRequestType.RESTORE}]]); }); it('10: FG set & query events', async () => { diff --git a/src/common/Types.d.ts b/src/common/Types.d.ts index 251a09f614..17c7231a22 100644 --- a/src/common/Types.d.ts +++ b/src/common/Types.d.ts @@ -447,6 +447,7 @@ export interface IColorRestoreRequest { } export type IColorEvent = (IColorReportRequest | IColorSetRequest | IColorRestoreRequest)[]; + /** * Calls the parser and handles actions generated by the parser. */ diff --git a/src/common/services/OptionsService.ts b/src/common/services/OptionsService.ts index 3c2d967848..0375f6addb 100644 --- a/src/common/services/OptionsService.ts +++ b/src/common/services/OptionsService.ts @@ -169,7 +169,7 @@ export class OptionsService extends Disposable implements IOptionsService { break; case 'cursorWidth': value = Math.floor(value); - // Fall through for bounds check + // Fall through for bounds check case 'lineHeight': case 'tabStopWidth': if (value < 1) { diff --git a/test/api/TestUtils.ts b/test/api/TestUtils.ts index cee59cceb1..9ebf67b6a1 100644 --- a/test/api/TestUtils.ts +++ b/test/api/TestUtils.ts @@ -74,10 +74,9 @@ export function getBrowserType(): playwright.BrowserType { +export function launchBrowser(): Promise { const browserType = getBrowserType(); - const options: playwright.LaunchOptions = { - ...opts, + const options: Record = { headless: process.argv.includes('--headless') }; From 6ac75d88c4cdbe69680a731ded991000dd2fe41a Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 12:46:23 +0300 Subject: [PATCH 12/18] Fix demo tsconfig addon name --- demo/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/tsconfig.json b/demo/tsconfig.json index 2e72c5016c..4114f6d6dc 100644 --- a/demo/tsconfig.json +++ b/demo/tsconfig.json @@ -7,7 +7,7 @@ "baseUrl": ".", "paths": { "addon-attach": ["../addons/addon-attach"], - "xterm-addon-clipboard": ["../addons/addon-clipboard"], + "addon-clipboard": ["../addons/addon-clipboard"], "addon-fit": ["../addons/addon-fit"], "addon-image": ["../addons/addon-image"], "addon-search": ["../addons/addon-search"], From acc0a2ef781574880d1b5f90c6bd7476741e569a Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 8 Apr 2024 12:53:17 +0300 Subject: [PATCH 13/18] Support passing playwright browser options --- test/api/TestUtils.ts | 5 +++-- test/playwright/TestUtils.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/api/TestUtils.ts b/test/api/TestUtils.ts index 9ebf67b6a1..cee59cceb1 100644 --- a/test/api/TestUtils.ts +++ b/test/api/TestUtils.ts @@ -74,9 +74,10 @@ export function getBrowserType(): playwright.BrowserType { +export function launchBrowser(opts?: playwright.LaunchOptions): Promise { const browserType = getBrowserType(); - const options: Record = { + const options: playwright.LaunchOptions = { + ...opts, headless: process.argv.includes('--headless') }; diff --git a/test/playwright/TestUtils.ts b/test/playwright/TestUtils.ts index 1427578c11..79408d4180 100644 --- a/test/playwright/TestUtils.ts +++ b/test/playwright/TestUtils.ts @@ -492,9 +492,10 @@ export function getBrowserType(): playwright.BrowserType { +export function launchBrowser(opts?: playwright.LaunchOptions): Promise { const browserType = getBrowserType(); - const options: Record = { + const options: playwright.LaunchOptions = { + ...opts, headless: process.argv.includes('--headless') }; From 8a9cb37273d3d74f21e29c2b97ab2c1cb3203960 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 18 Apr 2024 13:09:27 +0300 Subject: [PATCH 14/18] Tidy --- addons/addon-clipboard/package.json | 2 +- addons/addon-clipboard/src/ClipboardAddon.ts | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/addons/addon-clipboard/package.json b/addons/addon-clipboard/package.json index 06d0730aa1..af433f9368 100644 --- a/addons/addon-clipboard/package.json +++ b/addons/addon-clipboard/package.json @@ -21,7 +21,7 @@ "prepublishOnly": "npm run package" }, "peerDependencies": { - "@xterm/xterm": "^5.5.0" + "@xterm/xterm": "^5.4.0" }, "dependencies": { "js-base64": "^3.7.5" diff --git a/addons/addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts index 6a04188ee6..bfc2779dc4 100644 --- a/addons/addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -11,13 +11,11 @@ export class ClipboardAddon implements ITerminalAddon { private _terminal?: Terminal; private _disposable?: IDisposable; - constructor(private _provider: IClipboardProvider = new ClipboardProvider()) { - this._provider = _provider; - } + constructor(private _provider: IClipboardProvider = new ClipboardProvider()) {} public activate(terminal: Terminal): void { - this._disposable = terminal.parser.registerOscHandler(52, this._setOrReportClipboard); this._terminal = terminal; + this._disposable = terminal.parser.registerOscHandler(52, this._setOrReportClipboard); } public dispose(): void { @@ -42,7 +40,7 @@ export class ClipboardAddon implements ITerminalAddon { try { if (pd === '?') { // Report clipboard - return this._provider.readText(pc).then(data => { + return this._provider.readText(pc).then((data) => { this._terminal?.input(`\x1b]52;${pc};${data}\x07`, false); return true; }); @@ -72,7 +70,7 @@ export class ClipboardProvider implements IClipboardProvider { * Zero means no limit. */ limit: number = 0 // unlimited - ){ + ) { this._base64 = base64; this.limit = limit; } From 5238461f3f1cc91789d4e1571a9a891383fd0425 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 18 Apr 2024 23:17:17 +0300 Subject: [PATCH 15/18] Update ClipboardAddon.ts --- addons/addon-clipboard/src/ClipboardAddon.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts index bfc2779dc4..c7c9022f0f 100644 --- a/addons/addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -15,7 +15,7 @@ export class ClipboardAddon implements ITerminalAddon { public activate(terminal: Terminal): void { this._terminal = terminal; - this._disposable = terminal.parser.registerOscHandler(52, this._setOrReportClipboard); + this._disposable = terminal.parser.registerOscHandler(52, data => this._setOrReportClipboard(data)); } public dispose(): void { From 87169ee3acd0d082197facb41e54f8f6e541429e Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 19 Apr 2024 09:59:35 +0300 Subject: [PATCH 16/18] fix: tests --- addons/addon-clipboard/test/ClipboardAddon.api.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/addon-clipboard/test/ClipboardAddon.api.ts b/addons/addon-clipboard/test/ClipboardAddon.api.ts index 962cb3f218..298ef0c32f 100644 --- a/addons/addon-clipboard/test/ClipboardAddon.api.ts +++ b/addons/addon-clipboard/test/ClipboardAddon.api.ts @@ -75,12 +75,12 @@ describe('ClipboardAddon', () => { `); await page.evaluate(() => window.navigator.clipboard.writeText('hello world')); await writeSync(page, `\x1b]52;c;?\x07`); - assert.deepEqual(await page.evaluate(`window.data`), [testDataEncoded]); + assert.deepEqual(await page.evaluate(`window.data`), [`\x1b]52;c;${testDataEncoded}\x07`]); }); it('clear clipboard', async () => { await writeSync(page, `\x1b]52;c;!\x07`); await writeSync(page, `\x1b]52;c;?\x07`); - assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), '\x1b]52;c;\x07'); }); }); }); From 9ebfb11ac8a7644adc7ed83ba0dde683142e4dc9 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 19 Apr 2024 10:52:38 +0300 Subject: [PATCH 17/18] Fix tests --- addons/addon-clipboard/src/ClipboardAddon.ts | 4 ---- addons/addon-clipboard/test/ClipboardAddon.api.ts | 6 ++++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/addons/addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts index c7c9022f0f..b6dad8a539 100644 --- a/addons/addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -30,10 +30,6 @@ export class ClipboardAddon implements ITerminalAddon { const pc = args[0]; const pd = args[1]; - if (pd.length === 0) { - return true; - } - switch (pc) { case ClipboardSelectionType.SYSTEM: case ClipboardSelectionType.PRIMARY: diff --git a/addons/addon-clipboard/test/ClipboardAddon.api.ts b/addons/addon-clipboard/test/ClipboardAddon.api.ts index 298ef0c32f..4ef768e8e7 100644 --- a/addons/addon-clipboard/test/ClipboardAddon.api.ts +++ b/addons/addon-clipboard/test/ClipboardAddon.api.ts @@ -6,6 +6,7 @@ import { assert } from 'chai'; import { openTerminal, launchBrowser, writeSync, getBrowserType } from '../../../out-test/api/TestUtils'; import { Browser, BrowserContext, Page } from '@playwright/test'; +import { beforeEach } from 'mocha'; const APP = 'http://127.0.0.1:3001/test'; @@ -62,6 +63,7 @@ describe('ClipboardAddon', () => { assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); }); it('empty string', async () => { + await writeSync(page, `\x1b]52;c;${testDataEncoded}\x07`); await writeSync(page, `\x1b]52;c;\x07`); assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); }); @@ -75,12 +77,12 @@ describe('ClipboardAddon', () => { `); await page.evaluate(() => window.navigator.clipboard.writeText('hello world')); await writeSync(page, `\x1b]52;c;?\x07`); - assert.deepEqual(await page.evaluate(`window.data`), [`\x1b]52;c;${testDataEncoded}\x07`]); + assert.deepEqual(await page.evaluate('window.data'), [`\x1b]52;c;${testDataEncoded}\x07`]); }); it('clear clipboard', async () => { await writeSync(page, `\x1b]52;c;!\x07`); await writeSync(page, `\x1b]52;c;?\x07`); - assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), '\x1b]52;c;\x07'); + assert.deepEqual(await page.evaluate(() => window.navigator.clipboard.readText()), ''); }); }); }); From 0562f32af608d754487d957269f2729396a22b23 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Sat, 20 Apr 2024 11:20:16 -0400 Subject: [PATCH 18/18] Apply suggestions --- addons/addon-clipboard/src/ClipboardAddon.ts | 105 +++++++----------- .../typings/addon-clipboard.d.ts | 83 ++++++++------ 2 files changed, 89 insertions(+), 99 deletions(-) diff --git a/addons/addon-clipboard/src/ClipboardAddon.ts b/addons/addon-clipboard/src/ClipboardAddon.ts index b6dad8a539..58425924f5 100644 --- a/addons/addon-clipboard/src/ClipboardAddon.ts +++ b/addons/addon-clipboard/src/ClipboardAddon.ts @@ -4,14 +4,17 @@ */ import type { IDisposable, ITerminalAddon, Terminal } from '@xterm/xterm'; -import { type IClipboardProvider, ClipboardSelectionType } from '@xterm/addon-clipboard'; +import { type IClipboardProvider, ClipboardSelectionType, type IBase64 } from '@xterm/addon-clipboard'; import { Base64 as JSBase64 } from 'js-base64'; export class ClipboardAddon implements ITerminalAddon { private _terminal?: Terminal; private _disposable?: IDisposable; - constructor(private _provider: IClipboardProvider = new ClipboardProvider()) {} + constructor( + private _base64: IBase64 = new Base64(), + private _provider: IClipboardProvider = new BrowserClipboardProvider() + ) {} public activate(terminal: Terminal): void { this._terminal = terminal; @@ -22,90 +25,66 @@ export class ClipboardAddon implements ITerminalAddon { return this._disposable?.dispose(); } + private _readText(sel: ClipboardSelectionType, data: string): void { + const b64 = this._base64.encodeText(data); + this._terminal?.input(`\x1b]52;${sel};${b64}\x07`, false); + } + private _setOrReportClipboard(data: string): boolean | Promise { const args = data.split(';'); if (args.length < 2) { return true; } - const pc = args[0]; + const pc = args[0] as ClipboardSelectionType; const pd = args[1]; - switch (pc) { - case ClipboardSelectionType.SYSTEM: - case ClipboardSelectionType.PRIMARY: - try { - if (pd === '?') { - // Report clipboard - return this._provider.readText(pc).then((data) => { - this._terminal?.input(`\x1b]52;${pc};${data}\x07`, false); - return true; - }); - } - return this._provider.writeText(pc, pd).then(() => true); - } catch (e) { - console.error(e); - } + if (pd === '?') { + const text = this._provider.readText(pc); + + // Report clipboard + if (text instanceof Promise) { + return text.then((data) => { + this._readText(pc, data); + return true; + }); + } + + this._readText(pc, text); + return true; } - return true; - } -} + // Clear clipboard if text is not a base64 encoded string. + let text = ''; + try { + text = this._base64.decodeText(pd); + } catch {} -export class ClipboardProvider implements IClipboardProvider { - private _base64: IBase64; - public limit: number; - constructor( - /** - * The base64 encoder/decoder to use. - */ - base64: IBase64 = new Base64(), - - /** - * The maximum amount of data that can be copied to the clipboard. - * Zero means no limit. - */ - limit: number = 0 // unlimited - ) { - this._base64 = base64; - this.limit = limit; + const result = this._provider.writeText(pc, text); + if (result instanceof Promise) { + return result.then(() => true); + } + + return true; } +} - public readText(selection: ClipboardSelectionType): Promise { +export class BrowserClipboardProvider implements IClipboardProvider { + public async readText(selection: ClipboardSelectionType): Promise { if (selection !== 'c') { return Promise.resolve(''); } - return navigator.clipboard.readText().then(this._base64.encodeText); + return navigator.clipboard.readText(); } - public writeText(selection: ClipboardSelectionType, data: string): Promise { - if (selection !== 'c' || (this.limit > 0 && data.length > this.limit)) { + public async writeText(selection: ClipboardSelectionType, text: string): Promise { + if (selection !== 'c') { return Promise.resolve(); } - try { - const text = this._base64.decodeText(data); - return navigator.clipboard.writeText(text); - } catch { - // clear the clipboard if the data is not valid base64 - return navigator.clipboard.writeText(''); - } + return navigator.clipboard.writeText(text); } } -export interface IBase64 { - /** - * Converts a utf-8 string to a base64 string. - * @param data The utf-8 string to convert to base64 string. - */ - encodeText(data: string): string; - - /** - * Converts a base64 string to a utf-8 string. - * @param data The base64 string to convert to utf-8 string. - */ - decodeText(data: string): string; -} - export class Base64 implements IBase64 { public encodeText(data: string): string { return JSBase64.encode(data); diff --git a/addons/addon-clipboard/typings/addon-clipboard.d.ts b/addons/addon-clipboard/typings/addon-clipboard.d.ts index 06cb370370..f37748fadf 100644 --- a/addons/addon-clipboard/typings/addon-clipboard.d.ts +++ b/addons/addon-clipboard/typings/addon-clipboard.d.ts @@ -29,32 +29,17 @@ declare module '@xterm/addon-clipboard' { } /** - * The clipboard provider interface that enables xterm.js to access the system clipboard. + * Clipboard selection type. This is used to specify which selection buffer to + * read or write to. + * - SYSTEM `c`: The system clipboard. + * - PRIMARY `p`: The primary clipboard. This is provided for compatibility + * with Linux X11. */ - export class ClipboardProvider implements IClipboardProvider{ - /** - * Creates a new clipboard provider. - * @param _base64 The base64 encoder/decoder to use. - */ - constructor(base64?: IBase64, limit?: number); - - /** - * Reads text from the clipboard. - * @param selection The selection type to read from. - * @returns A promise that resolves with the text from the clipboard. - */ - public readText(selection: ClipboardSelectionType): Promise; - - /** - * Writes text to the clipboard. - * @param selection The selection type to write to. - * @param data The text to write to the clipboard. - * @returns A promise that resolves when the text has been written to the clipboard. - */ - public writeText(selection: ClipboardSelectionType, data: string): Promise; + export const enum ClipboardSelectionType { + SYSTEM = 'c', + PRIMARY = 'p', } - export interface IBase64 { /** * Converts a utf-8 string to a base64 string. @@ -65,36 +50,62 @@ declare module '@xterm/addon-clipboard' { /** * Converts a base64 string to a utf-8 string. * @param data The base64 string to convert to utf-8 string. + * @throws An error if the input is not valid base64. */ decodeText(data: string): string; } + /** + * A default Base64 encoding and decoding type. + **/ + export class Base64 implements IBase64 { + /** + * Converts a utf-8 string to a base64 string. + * @param data The utf-8 string to convert to base64 string. + */ + public encodeText(data: string): string; + + /** + * Converts a base64 string to a utf-8 string. + * @param data The base64 string to convert to utf-8 string. + * @throws An error if the input is not valid base64. + */ + public decodeText(data: string): string; + } + export interface IClipboardProvider { /** * Gets the clipboard content. * @param selection The clipboard selection to read. - * @returns A promise that resolves with the base64 encoded data. + * @returns A promise that resolves with clipboard selection data. */ - readText(selection: ClipboardSelectionType): Promise; + readText(selection: ClipboardSelectionType): string | Promise; /** * Sets the clipboard content. * @param selection The clipboard selection to set. - * @param data The base64 encoded data to set. If the data is invalid - * base64, the clipboard is cleared. + * @param data The clipboard text to write. */ - writeText(selection: ClipboardSelectionType, data: string): Promise; + writeText(selection: ClipboardSelectionType, text: string): void | Promise; } /** - * Clipboard selection type. This is used to specify which selection buffer to - * read or write to. - * - SYSTEM `c`: The system clipboard. - * - PRIMARY `p`: The primary clipboard. This is provided for compatibility - * with Linux X11. + * The clipboard provider interface that enables xterm.js to access the system clipboard. */ - export const enum ClipboardSelectionType { - SYSTEM = 'c', - PRIMARY = 'p', + export class BrowserClipboardProvider implements IClipboardProvider{ + /** + * Reads text from the clipboard. + * @param selection The selection type to read from. + * @returns A promise that resolves with the text from the clipboard. + */ + public readText(selection: ClipboardSelectionType): Promise; + + /** + * Writes text to the clipboard. + * @param selection The selection type to write to. + * @param data The text to write to the clipboard. + * @returns A promise that resolves when the text has been written to the clipboard. + */ + public writeText(selection: ClipboardSelectionType, data: string): Promise; } }