]>
git.proxmox.com Git - mirror_xterm.js.git/blob - src/handlers/Clipboard.js
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2016, SourceLair Private Company <www.sourcelair.com> (MIT License)
7 * Clipboard handler module. This module contains methods for handling all
8 * clipboard-related events appropriately in the terminal.
9 * @module xterm/handlers/Clipboard
13 * Prepares text copied from terminal selection, to be saved in the clipboard by:
14 * 1. stripping all trailing white spaces
15 * 2. converting all non-breaking spaces to regular spaces
16 * @param {string} text The copied text that needs processing for storing in clipboard
19 function prepareTextForClipboard(text
) {
20 var space
= String
.fromCharCode(32),
21 nonBreakingSpace
= String
.fromCharCode(160),
22 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
23 processedText
= text
.split('\n').map(function (line
) {
24 // Strip all trailing white spaces and convert all non-breaking spaces
26 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
35 * Binds copy functionality to the given terminal.
36 * @param {ClipboardEvent} ev The original copy event to be handled
38 function copyHandler (ev
) {
39 var copiedText
= window
.getSelection().toString(),
40 text
= prepareTextForClipboard(copiedText
);
42 ev
.preventDefault(); // 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 function pasteHandler(ev
, term
) {
52 if (ev
.clipboardData
) {
53 var text
= ev
.clipboardData
.getData('text/plain');
55 term
.textarea
.value
= '';
56 return term
.cancel(ev
);
61 * Bind to right-click event and allow right-click copy and paste.
64 * If text is selected and right-click happens on selected text, then
65 * do nothing to allow seamless copying.
66 * If no text is selected or right-click is outside of the selection
67 * area, then bring the terminal's input below the cursor, in order to
68 * trigger the event on the textarea and allow-right click paste, without
69 * caring about disappearing selection.
70 * @param {ClipboardEvent} ev The original paste event to be handled
71 * @param {Terminal} term The terminal on which to apply the handled paste event
73 function rightClickHandler(ev
, term
) {
74 var s
= document
.getSelection(),
75 sText
= prepareTextForClipboard(s
.toString()),
81 var cr
= r
.getClientRects(),
82 clickIsOnSelection
= false,
85 for (i
=0; i
<cr
.length
; i
++) {
87 clickIsOnSelection
= (
88 (x
> rect
.left
) && (x
< rect
.right
) &&
89 (y
> rect
.top
) && (y
< rect
.bottom
)
91 // If we clicked on selection and selection is not a single space,
92 // then mark the right click as copy-only. We check for the single
93 // space selection, as this can happen when clicking on an
94 // and there is not much pointing in copying a single space.
95 // Single space is char
96 if (clickIsOnSelection
&& (sText
!== ' ')) {
101 // Bring textarea at the cursor position
102 if (!clickIsOnSelection
) {
103 term
.textarea
.style
.position
= 'fixed';
104 term
.textarea
.style
.width
= '10px';
105 term
.textarea
.style
.height
= '10px';
106 term
.textarea
.style
.left
= x
+ 'px';
107 term
.textarea
.style
.top
= y
+ 'px';
108 term
.textarea
.style
.zIndex
= 1000;
109 term
.textarea
.focus();
111 // Reset the terminal textarea's styling
112 setTimeout(function () {
113 term
.textarea
.style
.position
= null;
114 term
.textarea
.style
.width
= null;
115 term
.textarea
.style
.height
= null;
116 term
.textarea
.style
.left
= null;
117 term
.textarea
.style
.top
= null;
118 term
.textarea
.style
.zIndex
= null;
124 prepareTextForClipboard
, copyHandler
, pasteHandler
, rightClickHandler