2 * Clipboard handler module: exports methods for handling all clipboard-related events in the
4 * @module xterm/handlers/Clipboard
8 import { ITerminal, ISelectionManager } from '../Interfaces';
10 interface IWindow extends Window {
12 getData(format: string): string;
13 setData(format: string, data: string);
17 declare var window: IWindow;
19 const SPACE_CHAR = String.fromCharCode(32);
20 const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160);
21 const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g');
24 * Prepares text copied from terminal selection, to be saved in the clipboard by:
25 * 1. stripping all trailing white spaces
26 * 2. converting all non-breaking spaces to regular spaces
27 * @param {string} text The copied text that needs processing for storing in clipboard
30 export function prepareTextForClipboard(text: string): string {
31 // TODO: Pass an unjoined string array into this function so not splitting is needed
32 return text.split('\n').map(line => {
33 return line.replace(ALL_NON_BREAKING_SPACE_REGEX, SPACE_CHAR);
38 * Prepares text to be pasted into the terminal by normalizing the line endings
39 * @param text The pasted text that needs processing before inserting into the terminal
41 export function prepareTextForTerminal(text: string, isMSWindows: boolean): string {
43 return text.replace(/\r?\n/g, '\n');
49 * Binds copy functionality to the given terminal.
50 * @param {ClipboardEvent} ev The original copy event to be handled
52 export function copyHandler(ev: ClipboardEvent, term: ITerminal, selectionManager: ISelectionManager) {
53 // We cast `window` to `any` type, because TypeScript has not declared the `clipboardData`
54 // property that we use below for Internet Explorer.
55 let text = prepareTextForClipboard(selectionManager.selectionText);
57 if (term.browser.isMSIE) {
58 window.clipboardData.setData('Text', text);
60 ev.clipboardData.setData('text/plain', text);
63 ev.preventDefault(); // Prevent or the original text will be copied.
67 * Redirect the clipboard's data to the terminal's input handler.
68 * @param {ClipboardEvent} ev The original paste event to be handled
69 * @param {Terminal} term The terminal on which to apply the handled paste event
71 export function pasteHandler(ev: ClipboardEvent, term: ITerminal) {
76 let dispatchPaste = function(text) {
77 text = prepareTextForTerminal(text, term.browser.isMSWindows);
79 term.textarea.value = '';
80 term.emit('paste', text);
82 return term.cancel(ev);
85 if (term.browser.isMSIE) {
86 if (window.clipboardData) {
87 text = window.clipboardData.getData('Text');
91 if (ev.clipboardData) {
92 text = ev.clipboardData.getData('text/plain');
99 * Bind to right-click event and allow right-click copy and paste.
100 * @param ev The original right click event to be handled
101 * @param term The terminal on which to apply the handled paste event
102 * @param selectionManager The terminal's selection manager.
104 export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager) {
105 // Bring textarea at the cursor position
106 textarea.style.position = 'fixed';
107 textarea.style.width = '20px';
108 textarea.style.height = '20px';
109 textarea.style.left = (ev.clientX - 10) + 'px';
110 textarea.style.top = (ev.clientY - 10) + 'px';
111 textarea.style.zIndex = '1000';
113 // Get textarea ready to copy from the context menu
114 textarea.value = prepareTextForClipboard(selectionManager.selectionText);
118 // Reset the terminal textarea's styling
119 setTimeout(function () {
120 textarea.style.position = null;
121 textarea.style.width = null;
122 textarea.style.height = null;
123 textarea.style.left = null;
124 textarea.style.top = null;
125 textarea.style.zIndex = null;