]>
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 * Prepares text copied from terminal selection, to be saved in the clipboard by:
8 * 1. stripping all trailing white spaces
9 * 2. converting all non-breaking spaces to regular spaces
10 * @param {string} text The copied text that needs processing for storing in clipboard
14 function prepareTextForClipboard(text
) {
15 var space
= String
.fromCharCode(32),
16 nonBreakingSpace
= String
.fromCharCode(160),
17 allNonBreakingSpaces
= new RegExp(nonBreakingSpace
, 'g'),
18 processedText
= text
.split('\n').map(function (line
) {
19 // Strip all trailing white spaces and convert all non-breaking spaces
21 var processedLine
= line
.replace(/\s+$/g, '').replace(allNonBreakingSpaces
, space
);
30 * Binds copy functionality to the given terminal.
33 function copyHandler (ev
) {
34 var copiedText
= window
.getSelection().toString(),
35 text
= prepareTextForClipboard(copiedText
);
37 ev
.preventDefault(); // Prevent or the original text will be copied.
41 * Bind to paste event and allow both keyboard and right-click pasting, without having the
42 * contentEditable value set to true.
44 function pasteHandler(ev
, term
) {
46 if (ev
.clipboardData
) {
47 var text
= ev
.clipboardData
.getData('text/plain');
49 term
.textarea
.value
= '';
50 return term
.cancel(ev
);
54 function rightClickHandler(ev
) {
55 var s
= document
.getSelection(),
56 sText
= prepareTextForClipboard(s
.toString()),
62 var cr
= r
.getClientRects(),
63 clickIsOnSelection
= false,
66 for (i
=0; i
<cr
.length
; i
++) {
68 clickIsOnSelection
= (
69 (x
> rect
.left
) && (x
< rect
.right
) &&
70 (y
> rect
.top
) && (y
< rect
.bottom
)
72 // If we clicked on selection and selection is not a single space,
73 // then mark the right click as copy-only. We check for the single
74 // space selection, as this can happen when clicking on an
75 // and there is not much pointing in copying a single space.
76 // Single space is char
77 if (clickIsOnSelection
&& (sText
!== ' ')) {
82 // Bring textarea at the cursor position
83 if (!clickIsOnSelection
) {
84 term
.textarea
.style
.position
= 'fixed';
85 term
.textarea
.style
.width
= '10px';
86 term
.textarea
.style
.height
= '10px';
87 term
.textarea
.style
.left
= x
+ 'px';
88 term
.textarea
.style
.top
= y
+ 'px';
89 term
.textarea
.style
.zIndex
= 1000;
90 term
.textarea
.focus();
92 // Reset the terminal textarea's styling
93 setTimeout(function () {
94 term
.textarea
.style
.position
= null;
95 term
.textarea
.style
.width
= null;
96 term
.textarea
.style
.height
= null;
97 term
.textarea
.style
.left
= null;
98 term
.textarea
.style
.top
= null;
99 term
.textarea
.style
.zIndex
= null;
105 prepareTextForClipboard
, copyHandler
, pasteHandler
, rightClickHandler