forked from xtermjs/xterm.js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to ANSI OSC52 sequence to manipulate selection and clipboard data. The sequence specs supports multiple clipboard selections but we only support the common ones, system and primary clipboard selections. This adds a new event listener to the common terminal module `onClipboard` to allow external implementations to hook into it. The addon uses the browser Clipboard API to read/write from and to the clipboard. The default `ClipboardProvider` uses the browser Clipboard API. This means it only supports read/write to and from the system clipboard. Reference: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands Fixes: xtermjs#3260 Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com>
- Loading branch information
1 parent
03f1336
commit 1ec167b
Showing
32 changed files
with
662 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ | |
"mochaExplorer.watch": [ | ||
"out/**/*.js", | ||
"addons/**/out/*.js" | ||
] | ||
], | ||
"editor.formatOnSave": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lib | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## 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); | ||
``` | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) 2023 The xterm.js authors. All rights reserved. | ||
* @license MIT | ||
*/ | ||
|
||
import { Base64 } from 'js-base64'; | ||
import { ClipboardSelection, IClipboardProvider } from 'xterm'; | ||
|
||
export class ClipboardProvider implements IClipboardProvider { | ||
constructor( | ||
/** | ||
* The maximum amount of data that can be copied to the clipboard. | ||
* Zero means no limit. | ||
*/ | ||
public limit = 1000000 // 1MB | ||
){} | ||
public readText(selection: ClipboardSelection): Promise<string> { | ||
if (selection !== 'c') { | ||
return Promise.resolve(''); | ||
} | ||
return navigator.clipboard.readText().then((text) => | ||
Base64.encode(text)); | ||
} | ||
public writeText(selection: ClipboardSelection, data: string): Promise<void> { | ||
if (selection !== 'c' || (this.limit > 0 && data.length > this.limit)) { | ||
return Promise.resolve(); | ||
} | ||
const text = Base64.decode(data); | ||
// clear the clipboard if the data is not valid base64 | ||
if (!Base64.isValid(data) || Base64.encode(text) !== data) { | ||
return navigator.clipboard.writeText(''); | ||
} | ||
return navigator.clipboard.writeText(text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* 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<any> { | ||
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<any> { | ||
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<any> { | ||
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]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ "path": "./src" }, | ||
{ "path": "./test" } | ||
] | ||
} |
35 changes: 35 additions & 0 deletions
35
addons/xterm-addon-clipboard/typings/xterm-addon-clipboard.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>; | ||
public writeText(selection: ClipboardSelection, data: string): Promise<void>; | ||
} | ||
|
||
/** | ||
* 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 | ||
} | ||
} |
Oops, something went wrong.