2 * Copyright (c) 2016 The xterm.js authors. All rights reserved.
6 import { ISelectionService } from 'browser/services/Services';
7 import { ICoreService } from 'common/services/Services';
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
13 export function prepareTextForTerminal(text: string): string {
14 return text.replace(/\r?\n/g, '\r');
18 * Bracket text for paste, if necessary, as per https://cirw.in/blog/bracketed-paste
19 * @param text The pasted text to bracket
21 export function bracketTextForPaste(text: string, bracketedPasteMode: boolean): string {
22 if (bracketedPasteMode) {
23 return '\x1b[200~' + text + '\x1b[201~';
29 * Binds copy functionality to the given terminal.
30 * @param ev The original copy event to be handled
32 export function copyHandler(ev: ClipboardEvent, selectionService: ISelectionService): void {
33 if (ev.clipboardData) {
34 ev.clipboardData.setData('text/plain', selectionService.selectionText);
36 // Prevent or the original text will be copied.
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
45 export function handlePasteEvent(ev: ClipboardEvent, textarea: HTMLTextAreaElement, bracketedPasteMode: boolean, coreService: ICoreService): void {
47 if (ev.clipboardData) {
48 const text = ev.clipboardData.getData('text/plain');
49 paste(text, textarea, bracketedPasteMode, coreService);
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);
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.
65 export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement, screenElement: HTMLElement): void {
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;
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';
82 // Reset the terminal textarea's styling
83 // Timeout needs to be long enough for click event to be handled.
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;
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
101 export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, screenElement: HTMLElement, selectionService: ISelectionService, shouldSelectWord: boolean): void {
102 moveTextAreaUnderMouseCursor(ev, textarea, screenElement);
104 if (shouldSelectWord && !selectionService.isClickInSelection(ev)) {
105 selectionService.selectWordAtCursor(ev);
108 // Get textarea ready to copy from the context menu
109 textarea.value = selectionService.selectionText;