Debian Codegraph @

node/@xterm/xterm/src/browser/Clipboard.ts

raw
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, IOptionsService } 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 */
43 export function handlePasteEvent(ev: ClipboardEvent, textarea: HTMLTextAreaElement, coreService: ICoreService, optionsService: IOptionsService): void {
44 ev.stopPropagation();
45 if (ev.clipboardData) {
46 const text = ev.clipboardData.getData('text/plain');
47 paste(text, textarea, coreService, optionsService);
48 }
49 }
50
51 export function paste(text: string, textarea: HTMLTextAreaElement, coreService: ICoreService, optionsService: IOptionsService): void {
52 text = prepareTextForTerminal(text);
53 text = bracketTextForPaste(text, coreService.decPrivateModes.bracketedPasteMode && optionsService.rawOptions.ignoreBracketedPasteMode !== true);
54 coreService.triggerDataEvent(text, true);
55 textarea.value = '';
56 }
57
58 /**
59 * Moves the textarea under the mouse cursor and focuses it.
60 * @param ev The original right click event to be handled.
61 * @param textarea The terminal's textarea.
62 */
63 export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement, screenElement: HTMLElement): void {
64
65 // Calculate textarea position relative to the screen element
66 const pos = screenElement.getBoundingClientRect();
67 const left = ev.clientX - pos.left - 10;
68 const top = ev.clientY - pos.top - 10;
69
70 // Bring textarea at the cursor position
71 textarea.style.width = '20px';
72 textarea.style.height = '20px';
73 textarea.style.left = `${left}px`;
74 textarea.style.top = `${top}px`;
75 textarea.style.zIndex = '1000';
76
77 textarea.focus();
78 }
79
80 /**
81 * Bind to right-click event and allow right-click copy and paste.
82 */
83 export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, screenElement: HTMLElement, selectionService: ISelectionService, shouldSelectWord: boolean): void {
84 moveTextAreaUnderMouseCursor(ev, textarea, screenElement);
85
86 if (shouldSelectWord) {
87 selectionService.rightClickSelect(ev);
88 }
89
90 // Get textarea ready to copy from the context menu
91 textarea.value = selectionService.selectionText;
92 textarea.select();
93 }