| 1 |
/** |
| 2 |
* Copyright (c) 2022 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { ITerminal } from 'browser/Types'; |
| 7 |
import { ISelectionRenderModel } from 'browser/renderer/shared/Types'; |
| 8 |
import { Terminal } from '@xterm/xterm'; |
| 9 |
|
| 10 |
class SelectionRenderModel implements ISelectionRenderModel { |
| 11 |
public hasSelection!: boolean; |
| 12 |
public columnSelectMode!: boolean; |
| 13 |
public viewportStartRow!: number; |
| 14 |
public viewportEndRow!: number; |
| 15 |
public viewportCappedStartRow!: number; |
| 16 |
public viewportCappedEndRow!: number; |
| 17 |
public startCol!: number; |
| 18 |
public endCol!: number; |
| 19 |
public selectionStart: [number, number] | undefined; |
| 20 |
public selectionEnd: [number, number] | undefined; |
| 21 |
|
| 22 |
constructor() { |
| 23 |
this.clear(); |
| 24 |
} |
| 25 |
|
| 26 |
public clear(): void { |
| 27 |
this.hasSelection = false; |
| 28 |
this.columnSelectMode = false; |
| 29 |
this.viewportStartRow = 0; |
| 30 |
this.viewportEndRow = 0; |
| 31 |
this.viewportCappedStartRow = 0; |
| 32 |
this.viewportCappedEndRow = 0; |
| 33 |
this.startCol = 0; |
| 34 |
this.endCol = 0; |
| 35 |
this.selectionStart = undefined; |
| 36 |
this.selectionEnd = undefined; |
| 37 |
} |
| 38 |
|
| 39 |
public update(terminal: ITerminal, start: [number, number] | undefined, end: [number, number] | undefined, columnSelectMode: boolean = false): void { |
| 40 |
this.selectionStart = start; |
| 41 |
this.selectionEnd = end; |
| 42 |
// Selection does not exist |
| 43 |
if (!start || !end || (start[0] === end[0] && start[1] === end[1])) { |
| 44 |
this.clear(); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
// Translate from buffer position to viewport position |
| 49 |
const viewportY = terminal.buffers.active.ydisp; |
| 50 |
const viewportStartRow = start[1] - viewportY; |
| 51 |
const viewportEndRow = end[1] - viewportY; |
| 52 |
const viewportCappedStartRow = Math.max(viewportStartRow, 0); |
| 53 |
const viewportCappedEndRow = Math.min(viewportEndRow, terminal.rows - 1); |
| 54 |
|
| 55 |
// No need to draw the selection |
| 56 |
if (viewportCappedStartRow >= terminal.rows || viewportCappedEndRow < 0) { |
| 57 |
this.clear(); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
this.hasSelection = true; |
| 62 |
this.columnSelectMode = columnSelectMode; |
| 63 |
this.viewportStartRow = viewportStartRow; |
| 64 |
this.viewportEndRow = viewportEndRow; |
| 65 |
this.viewportCappedStartRow = viewportCappedStartRow; |
| 66 |
this.viewportCappedEndRow = viewportCappedEndRow; |
| 67 |
this.startCol = start[0]; |
| 68 |
this.endCol = end[0]; |
| 69 |
} |
| 70 |
|
| 71 |
public isCellSelected(terminal: Terminal, x: number, y: number): boolean { |
| 72 |
if (!this.hasSelection) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
y -= terminal.buffer.active.viewportY; |
| 76 |
if (this.columnSelectMode) { |
| 77 |
if (this.startCol <= this.endCol) { |
| 78 |
return x >= this.startCol && y >= this.viewportCappedStartRow && |
| 79 |
x < this.endCol && y <= this.viewportCappedEndRow; |
| 80 |
} |
| 81 |
return x < this.startCol && y >= this.viewportCappedStartRow && |
| 82 |
x >= this.endCol && y <= this.viewportCappedEndRow; |
| 83 |
} |
| 84 |
return (y > this.viewportStartRow && y < this.viewportEndRow) || |
| 85 |
(this.viewportStartRow === this.viewportEndRow && y === this.viewportStartRow && x >= this.startCol && x < this.endCol) || |
| 86 |
(this.viewportStartRow < this.viewportEndRow && y === this.viewportEndRow && x < this.endCol) || |
| 87 |
(this.viewportStartRow < this.viewportEndRow && y === this.viewportStartRow && x >= this.startCol); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
export function createSelectionRenderModel(): ISelectionRenderModel { |
| 92 |
return new SelectionRenderModel(); |
| 93 |
} |