xterm
[VSoRC/.git] / node_modules / xterm / src / browser / Clipboard.ts
1 /**
2  * Copyright (c) 2016 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 import { ISelectionService } from 'browser/services/Services';
7 import { ICoreService } from 'common/services/Services';
8
9 /**
10  * Prepares text to be pasted into the terminal by normalizing the line endings
11  * @param text The pasted text that needs processing before inserting into the terminal
12  */
13 export function prepareTextForTerminal(text: string): string {
14   return text.replace(/\r?\n/g, '\r');
15 }
16
17 /**
18  * Bracket text for paste, if necessary, as per https://cirw.in/blog/bracketed-paste
19  * @param text The pasted text to bracket
20  */
21 export function bracketTextForPaste(text: string, bracketedPasteMode: boolean): string {
22   if (bracketedPasteMode) {
23     return '\x1b[200~' + text + '\x1b[201~';
24   }
25   return text;
26 }
27
28 /**
29  * Binds copy functionality to the given terminal.
30  * @param ev The original copy event to be handled
31  */
32 export function copyHandler(ev: ClipboardEvent, selectionService: ISelectionService): void {
33   if (ev.clipboardData) {
34     ev.clipboardData.setData('text/plain', selectionService.selectionText);
35   }
36   // Prevent or the original text will be copied.
37   ev.preventDefault();
38 }
39
40 /**
41  * Redirect the clipboard's data to the terminal's input handler.
42  * @param ev The original paste event to be handled
43  * @param term The terminal on which to apply the handled paste event
44  */
45 export function handlePasteEvent(ev: ClipboardEvent, textarea: HTMLTextAreaElement, bracketedPasteMode: boolean, coreService: ICoreService): void {
46   ev.stopPropagation();
47   if (ev.clipboardData) {
48     const text = ev.clipboardData.getData('text/plain');
49     paste(text, textarea, bracketedPasteMode, coreService);
50   }
51 }
52
53 export function paste(text: string, textarea: HTMLTextAreaElement, bracketedPasteMode: boolean, coreService: ICoreService): void {
54   text = prepareTextForTerminal(text);
55   text = bracketTextForPaste(text, bracketedPasteMode);
56   coreService.triggerDataEvent(text, true);
57   textarea.value = '';
58 }
59
60 /**
61  * Moves the textarea under the mouse cursor and focuses it.
62  * @param ev The original right click event to be handled.
63  * @param textarea The terminal's textarea.
64  */
65 export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement, screenElement: HTMLElement): void {
66
67   // Calculate textarea position relative to the screen element
68   const pos = screenElement.getBoundingClientRect();
69   const left = ev.clientX - pos.left - 10;
70   const top = ev.clientY - pos.top - 10;
71
72   // Bring textarea at the cursor position
73   textarea.style.position = 'absolute';
74   textarea.style.width = '20px';
75   textarea.style.height = '20px';
76   textarea.style.left = `${left}px`;
77   textarea.style.top = `${top}px`;
78   textarea.style.zIndex = '1000';
79
80   textarea.focus();
81
82   // Reset the terminal textarea's styling
83   // Timeout needs to be long enough for click event to be handled.
84   setTimeout(() => {
85     textarea.style.position = null;
86     textarea.style.width = null;
87     textarea.style.height = null;
88     textarea.style.left = null;
89     textarea.style.top = null;
90     textarea.style.zIndex = null;
91   }, 200);
92 }
93
94 /**
95  * Bind to right-click event and allow right-click copy and paste.
96  * @param ev The original right click event to be handled.
97  * @param textarea The terminal's textarea.
98  * @param selectionService The terminal's selection manager.
99  * @param shouldSelectWord If true and there is no selection the current word will be selected
100  */
101 export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, screenElement: HTMLElement, selectionService: ISelectionService, shouldSelectWord: boolean): void {
102   moveTextAreaUnderMouseCursor(ev, textarea, screenElement);
103
104   if (shouldSelectWord && !selectionService.isClickInSelection(ev)) {
105     selectionService.selectWordAtCursor(ev);
106   }
107
108   // Get textarea ready to copy from the context menu
109   textarea.value = selectionService.selectionText;
110   textarea.select();
111 }