]> git.proxmox.com Git - mirror_xterm.js.git/blob - src/handlers/Clipboard.js
Move clipboard into its own module
[mirror_xterm.js.git] / src / handlers / Clipboard.js
1 /**
2 * xterm.js: xterm, in the browser
3 * Copyright (c) 2016, SourceLair Private Company <www.sourcelair.com> (MIT License)
4 */
5
6 /**
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
11 * @returns {string}
12 * @static
13 */
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
20 // to regular spaces.
21 var processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space);
22
23 return processedLine;
24 }).join('\n');
25
26 return processedText;
27 }
28
29 /**
30 * Binds copy functionality to the given terminal.
31 * @static
32 */
33 function copyHandler (ev) {
34 var copiedText = window.getSelection().toString(),
35 text = prepareTextForClipboard(copiedText);
36
37 ev.preventDefault(); // Prevent or the original text will be copied.
38 }
39
40 /**
41 * Bind to paste event and allow both keyboard and right-click pasting, without having the
42 * contentEditable value set to true.
43 */
44 function pasteHandler(ev, term) {
45 ev.stopPropagation();
46 if (ev.clipboardData) {
47 var text = ev.clipboardData.getData('text/plain');
48 term.handler(text);
49 term.textarea.value = '';
50 return term.cancel(ev);
51 }
52 }
53
54 function rightClickHandler(ev) {
55 var s = document.getSelection(),
56 sText = prepareTextForClipboard(s.toString()),
57 r = s.getRangeAt(0);
58
59 var x = ev.clientX,
60 y = ev.clientY;
61
62 var cr = r.getClientRects(),
63 clickIsOnSelection = false,
64 i, rect;
65
66 for (i=0; i<cr.length; i++) {
67 rect = cr[i];
68 clickIsOnSelection = (
69 (x > rect.left) && (x < rect.right) &&
70 (y > rect.top) && (y < rect.bottom)
71 );
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 &nbsp;
75 // and there is not much pointing in copying a single space.
76 // Single space is char
77 if (clickIsOnSelection && (sText !== ' ')) {
78 break;
79 }
80 }
81
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();
91
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;
100 }, 1);
101 }
102 }
103
104 export {
105 prepareTextForClipboard, copyHandler, pasteHandler, rightClickHandler
106 };