]>
Commit | Line | Data |
---|---|---|
4d328268 DI |
1 | /** |
2 | * @license MIT | |
3 | */ | |
4 | ||
5 | import { CharMeasure } from './CharMeasure'; | |
6 | ||
c323e0ad DI |
7 | /** |
8 | * Gets coordinates within the terminal for a particular mouse event. The result | |
9 | * is returned as an array in the form [x, y] instead of an object as it's a | |
10 | * little faster and this function is used in some low level code. | |
11 | * @param event The mouse event. | |
12 | * @param rowContainer The terminal's row container. | |
13 | * @param charMeasure The char measure object used to determine character sizes. | |
14 | */ | |
4d328268 | 15 | export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure): [number, number] { |
70e35de5 | 16 | // Ignore browsers that don't support MouseEvent.pageX |
4d328268 DI |
17 | if (event.pageX == null) { |
18 | return null; | |
19 | } | |
20 | ||
21 | let x = event.pageX; | |
22 | let y = event.pageY; | |
23 | let el = rowContainer; | |
24 | ||
70e35de5 DI |
25 | // Converts the coordinates from being relative to the document to being |
26 | // relative to the terminal. | |
4d328268 DI |
27 | while (el && el !== self.document.documentElement) { |
28 | x -= el.offsetLeft; | |
29 | y -= el.offsetTop; | |
30 | el = 'offsetParent' in el ? <HTMLElement>el.offsetParent : <HTMLElement>el.parentElement; | |
31 | } | |
32 | ||
70e35de5 | 33 | // Convert to cols/rows |
4d328268 DI |
34 | x = Math.ceil(x / charMeasure.width); |
35 | y = Math.ceil(y / charMeasure.height); | |
36 | ||
37 | return [x, y]; | |
38 | } | |
39 | ||
c323e0ad DI |
40 | /** |
41 | * Gets coordinates within the terminal for a particular mouse event, wrapping | |
42 | * them to the bounds of the terminal and adding 32 to both the x and y values | |
43 | * as expected by xterm. | |
44 | * @param event The mouse event. | |
45 | * @param rowContainer The terminal's row container. | |
46 | * @param charMeasure The char measure object used to determine character sizes. | |
47 | * @param colCount The number of columns in the terminal. | |
48 | * @param rowCount The number of rows in the terminal. | |
49 | */ | |
4d328268 DI |
50 | export function getRawByteCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure, colCount: number, rowCount: number): { x: number, y: number } { |
51 | const coords = getCoords(event, rowContainer, charMeasure); | |
52 | let x = coords[0]; | |
53 | let y = coords[1]; | |
54 | ||
70e35de5 DI |
55 | // Ensure coordinates are within the terminal viewport. |
56 | x = Math.min(Math.max(x, 0), colCount); | |
57 | y = Math.min(Math.max(y, 0), rowCount); | |
4d328268 | 58 | |
70e35de5 | 59 | // xterm sends raw bytes and starts at 32 (SP) for each. |
4d328268 DI |
60 | x += 32; |
61 | y += 32; | |
62 | ||
63 | return { x, y }; | |
64 | } |