]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Fix right-click for Firefox and IE
authorParis Kasidiaris <paris@sourcelair.com>
Fri, 4 Nov 2016 16:33:04 +0000 (18:33 +0200)
committerParis Kasidiaris <paris@sourcelair.com>
Fri, 4 Nov 2016 16:33:04 +0000 (18:33 +0200)
Fix #310

src/handlers/Clipboard.js
src/utils/Browser.js [new file with mode: 0644]
src/xterm.js

index cfc77c338f958e2fb2685da91717fd83b0f78fd2..e3ad8704388f102664f78b92ec427bfa999fbd10 100644 (file)
@@ -86,7 +86,7 @@ function pasteHandler(ev, term) {
  */
 function rightClickHandler(ev, term) {
   var s = document.getSelection(),
-      sText = prepareTextForClipboard(s.toString()),
+      selectedText = prepareTextForClipboard(s.toString()),
       clickIsOnSelection = false;
 
   if (s.rangeCount) {
@@ -102,23 +102,27 @@ function rightClickHandler(ev, term) {
         (x > rect.left) && (x < rect.right) &&
         (y > rect.top) && (y < rect.bottom)
       );
-      // If we clicked on selection and selection is not a single space,
-      // then mark the right click as copy-only. We check for the single
-      // space selection, as this can happen when clicking on an &nbsp;
-      // and there is not much pointing in copying a single space.
-      if (clickIsOnSelection && (sText !== ' ')) {
+
+      if (clickIsOnSelection) {
         break;
       }
     }
+    // If we clicked on selection and selection is not a single space,
+    // then mark the right click as copy-only. We check for the single
+    // space selection, as this can happen when clicking on an &nbsp;
+    // and there is not much pointing in copying a single space.
+    if (selectedText.match(/^\s$/) || !selectedText.length) {
+      clickIsOnSelection = false;
+    }
   }
 
   // Bring textarea at the cursor position
   if (!clickIsOnSelection) {
     term.textarea.style.position = 'fixed';
-    term.textarea.style.width = '10px';
-    term.textarea.style.height = '10px';
-    term.textarea.style.left = x + 'px';
-    term.textarea.style.top = y + 'px';
+    term.textarea.style.width = '20px';
+    term.textarea.style.height = '20px';
+    term.textarea.style.left = (x - 10) + 'px';
+    term.textarea.style.top = (y - 10) + 'px';
     term.textarea.style.zIndex = 1000;
     term.textarea.focus();
 
@@ -130,7 +134,7 @@ function rightClickHandler(ev, term) {
       term.textarea.style.left = null;
       term.textarea.style.top = null;
       term.textarea.style.zIndex = null;
-    }, 1);
+    }, 4);
   }
 }
 
diff --git a/src/utils/Browser.js b/src/utils/Browser.js
new file mode 100644 (file)
index 0000000..bc78968
--- /dev/null
@@ -0,0 +1,17 @@
+/**
+ * xterm.js: xterm, in the browser
+ * Copyright (c) 2016, SourceLair Private Company <www.sourcelair.com> (MIT License)
+ */
+
+/**
+ * Browser utilities module. This module contains attributes and methods to help with
+ * identifying the current browser and platform.
+ * @module xterm/utils/Browser
+ */
+
+
+let userAgent = navigator.userAgent;
+let platform = navigator.platform;
+
+export let isFirefox = !!~userAgent.indexOf('Firefox');
+export let isMSIE = !!~userAgent.indexOf('MSIE');
index ac570683ab81308945ec9ff5a9b4e0e5487729ac..e1840f2000f2a7fcfc71f72a3fe816716446017d 100644 (file)
@@ -35,6 +35,7 @@ import { CompositionHelper } from './CompositionHelper.js';
 import { EventEmitter } from './EventEmitter.js';
 import { Viewport } from './Viewport.js';
 import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard.js';
+import { isFirefox, isMSIE } from './utils/Browser';
 
 /**
  * Terminal Emulation References:
@@ -446,9 +447,21 @@ Terminal.prototype.initGlobal = function() {
   on(this.textarea, 'paste', function (ev) {
     pasteHandler.call(this, ev, term);
   });
-  on(this.element, 'contextmenu', function (ev) {
+
+
+  function rightClickHandlerWrapper (ev) {
     rightClickHandler.call(this, ev, term);
-  });
+  }
+
+  if (isFirefox || isMSIE) {
+    on(this.element, 'mousedown', function (ev) {
+      if (ev.button == 2) {
+        rightClickHandlerWrapper(ev);
+      }
+    });
+  } else {
+    on(this.element, 'contextmenu', rightClickHandlerWrapper);
+  }
 };
 
 /**
@@ -523,11 +536,6 @@ Terminal.prototype.open = function(parent) {
   this.document = this.parent.ownerDocument;
   this.body = this.document.getElementsByTagName('body')[0];
 
-  // Parse User-Agent
-  if (this.context.navigator && this.context.navigator.userAgent) {
-    this.isMSIE = !!~this.context.navigator.userAgent.indexOf('MSIE');
-  }
-
   // Find the users platform. We use this to interpret the meta key
   // and ISO third level shifts.
   // http://stackoverflow.com/q/19877924/577598
@@ -843,7 +851,7 @@ Terminal.prototype.bindMouse = function() {
           ? ev.which - 1
         : null;
 
-        if (self.isMSIE) {
+        if (isMSIE) {
           button = button === 1 ? 0 : button === 4 ? 1 : button;
         }
         break;