]> git.proxmox.com Git - mirror_xterm.js.git/commitdiff
Pull getCoords into a Mouse util module
authorDaniel Imms <daimms@microsoft.com>
Sun, 21 May 2017 18:50:01 +0000 (11:50 -0700)
committerDaniel Imms <daimms@microsoft.com>
Sun, 21 May 2017 18:50:01 +0000 (11:50 -0700)
This is part of the work to prepare for the upcoing selection changes

Related #207

src/utils/Mouse.ts [new file with mode: 0644]
src/xterm.js

diff --git a/src/utils/Mouse.ts b/src/utils/Mouse.ts
new file mode 100644 (file)
index 0000000..b5faa16
--- /dev/null
@@ -0,0 +1,49 @@
+/**
+ * @license MIT
+ */
+
+import { CharMeasure } from './CharMeasure';
+
+export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure): [number, number] {
+  // ignore browsers without pageX for now
+  if (event.pageX == null) {
+    return null;
+  }
+
+  let x = event.pageX;
+  let y = event.pageY;
+  let el = rowContainer;
+
+  // should probably check offsetParent
+  // but this is more portable
+  while (el && el !== self.document.documentElement) {
+    x -= el.offsetLeft;
+    y -= el.offsetTop;
+    el = 'offsetParent' in el ? <HTMLElement>el.offsetParent : <HTMLElement>el.parentElement;
+  }
+
+  // convert to cols/rows
+  x = Math.ceil(x / charMeasure.width);
+  y = Math.ceil(y / charMeasure.height);
+
+  return [x, y];
+}
+
+export function getRawByteCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure, colCount: number, rowCount: number): { x: number, y: number } {
+  const coords = getCoords(event, rowContainer, charMeasure);
+  let x = coords[0];
+  let y = coords[1];
+
+  // be sure to avoid sending bad positions to the program
+  if (x < 0) x = 0;
+  if (x > colCount) x = colCount;
+  if (y < 0) y = 0;
+  if (y > rowCount) y = rowCount;
+
+  // xterm sends raw bytes and
+  // starts at 32 (SP) for each.
+  x += 32;
+  y += 32;
+
+  return { x, y };
+}
index c632eb705f48a25d152a3f749b3d2156472d848e..f92a25f7b64b7751bc25d94f1b9f63323ce406df 100644 (file)
@@ -24,6 +24,7 @@ import { CharMeasure } from './utils/CharMeasure';
 import * as Browser from './utils/Browser';
 import * as Keyboard from './utils/Keyboard';
 import { CHARSETS } from './Charsets';
+import { getRawByteCoords } from './utils/Mouse';
 
 /**
  * Terminal Emulation References:
@@ -789,7 +790,7 @@ Terminal.prototype.bindMouse = function() {
     button = getButton(ev);
 
     // get mouse coordinates
-    pos = getCoords(ev);
+    pos = getRawByteCoords(ev, self.rowContainer, self.charMeasure, self.cols, self.rows);
     if (!pos) return;
 
     sendEvent(button, pos);
@@ -817,7 +818,7 @@ Terminal.prototype.bindMouse = function() {
     var button = pressed
     , pos;
 
-    pos = getCoords(ev);
+    pos = getRawByteCoords(ev, self.rowContainer, self.charMeasure, self.cols, self.rows);
     if (!pos) return;
 
     // buttons marked as motions
@@ -992,50 +993,6 @@ Terminal.prototype.bindMouse = function() {
     return button;
   }
 
-  // mouse coordinates measured in cols/rows
-  function getCoords(ev) {
-    var x, y, w, h, el;
-
-    // ignore browsers without pageX for now
-    if (ev.pageX == null) return;
-
-    x = ev.pageX;
-    y = ev.pageY;
-    el = self.element;
-
-    // should probably check offsetParent
-    // but this is more portable
-    while (el && el !== self.document.documentElement) {
-      x -= el.offsetLeft;
-      y -= el.offsetTop;
-      el = 'offsetParent' in el
-        ? el.offsetParent
-      : el.parentNode;
-    }
-
-    // convert to cols/rows
-    x = Math.ceil(x / self.charMeasure.width);
-    y = Math.ceil(y / self.charMeasure.height);
-
-    // be sure to avoid sending
-    // bad positions to the program
-    if (x < 0) x = 0;
-    if (x > self.cols) x = self.cols;
-    if (y < 0) y = 0;
-    if (y > self.rows) y = self.rows;
-
-    // xterm sends raw bytes and
-    // starts at 32 (SP) for each.
-    x += 32;
-    y += 32;
-
-    return {
-      x: x,
-      y: y,
-      type: 'wheel'
-    };
-  }
-
   on(el, 'mousedown', function(ev) {
     if (!self.mouseEvents) return;