]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/handlers/Clipboard.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / handlers / Clipboard.ts
1 /**
2 * Clipboard handler module: exports methods for handling all clipboard-related events in the
3 * terminal.
4 * @module xterm/handlers/Clipboard
5 * @license MIT
6 */
7
8 import { ITerminal, ISelectionManager } from '../Interfaces';
9
10 interface IWindow extends Window {
11 clipboardData?: {
12 getData(format: string): string;
13 setData(format: string, data: string);
14 };
15 }
16
17 declare var window: IWindow;
18
19 /**
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
22 */
23 export function prepareTextForTerminal(text: string, isMSWindows: boolean): string {
24 if (isMSWindows) {
25 return text.replace(/\r?\n/g, '\r');
26 }
27 return text;
28 }
29
30 /**
31 * Binds copy functionality to the given terminal.
32 * @param {ClipboardEvent} ev The original copy event to be handled
33 */
34 export function copyHandler(ev: ClipboardEvent, term: ITerminal, selectionManager: ISelectionManager) {
35 if (term.browser.isMSIE) {
36 window.clipboardData.setData('Text', selectionManager.selectionText);
37 } else {
38 ev.clipboardData.setData('text/plain', selectionManager.selectionText);
39 }
40
41 // Prevent or the original text will be copied.
42 ev.preventDefault();
43 }
44
45 /**
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
49 */
50 export function pasteHandler(ev: ClipboardEvent, term: ITerminal) {
51 ev.stopPropagation();
52
53 let text: string;
54
55 let dispatchPaste = function(text) {
56 text = prepareTextForTerminal(text, term.browser.isMSWindows);
57 term.handler(text);
58 term.textarea.value = '';
59 term.emit('paste', text);
60
61 return term.cancel(ev);
62 };
63
64 if (term.browser.isMSIE) {
65 if (window.clipboardData) {
66 text = window.clipboardData.getData('Text');
67 dispatchPaste(text);
68 }
69 } else {
70 if (ev.clipboardData) {
71 text = ev.clipboardData.getData('text/plain');
72 dispatchPaste(text);
73 }
74 }
75 }
76
77 /**
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.
81 */
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';
90
91 textarea.focus();
92
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;
101 }, 4);
102 }
103
104 /**
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.
109 */
110 export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager) {
111 moveTextAreaUnderMouseCursor(ev, textarea);
112
113 // Get textarea ready to copy from the context menu
114 textarea.value = selectionManager.selectionText;
115 textarea.select();
116 }