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;
20 * Prepares text to be pasted into the terminal by normalizing the line endings
21 * @param text The pasted text that needs processing before inserting into the terminal
23 export function prepareTextForTerminal(text: string, isMSWindows: boolean): string {
25 return text.replace(/\r?\n/g, '\r');
31 * Binds copy functionality to the given terminal.
32 * @param {ClipboardEvent} ev The original copy event to be handled
34 export function copyHandler(ev: ClipboardEvent, term: ITerminal, selectionManager: ISelectionManager) {
35 if (term.browser.isMSIE) {
36 window.clipboardData.setData('Text', selectionManager.selectionText);
38 ev.clipboardData.setData('text/plain', selectionManager.selectionText);
41 // Prevent or the original text will be copied.
46 * Redirect the clipboard's data to the terminal's input handler.
47 * @param {ClipboardEvent} ev The original paste event to be handled
48 * @param {Terminal} term The terminal on which to apply the handled paste event
50 export function pasteHandler(ev: ClipboardEvent, term: ITerminal) {
55 let dispatchPaste = function(text) {
56 text = prepareTextForTerminal(text, term.browser.isMSWindows);
58 term.textarea.value = '';
59 term.emit('paste', text);
61 return term.cancel(ev);
64 if (term.browser.isMSIE) {
65 if (window.clipboardData) {
66 text = window.clipboardData.getData('Text');
70 if (ev.clipboardData) {
71 text = ev.clipboardData.getData('text/plain');
78 * Moves the textarea under the mouse cursor and focuses it.
79 * @param ev The original right click event to be handled.
80 * @param textarea The terminal's textarea.
82 export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement) {
83 // Bring textarea at the cursor position
84 textarea.style.position = 'fixed';
85 textarea.style.width = '20px';
86 textarea.style.height = '20px';
87 textarea.style.left = (ev.clientX - 10) + 'px';
88 textarea.style.top = (ev.clientY - 10) + 'px';
89 textarea.style.zIndex = '1000';
93 // Reset the terminal textarea's styling
94 setTimeout(function () {
95 textarea.style.position = null;
96 textarea.style.width = null;
97 textarea.style.height = null;
98 textarea.style.left = null;
99 textarea.style.top = null;
100 textarea.style.zIndex = null;
105 * Bind to right-click event and allow right-click copy and paste.
106 * @param ev The original right click event to be handled.
107 * @param textarea The terminal's textarea.
108 * @param selectionManager The terminal's selection manager.
110 export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager) {
111 moveTextAreaUnderMouseCursor(ev, textarea);
113 // Get textarea ready to copy from the context menu
114 textarea.value = selectionManager.selectionText;