Merge pull request #1114 from nikonso/feature-778
Select the word underneath the cursor on contextmenu
This commit is contained in:
@@ -148,6 +148,7 @@ namespace methods_core {
|
|||||||
const r24: string = t.getOption('fontWeight');
|
const r24: string = t.getOption('fontWeight');
|
||||||
const r25: string = t.getOption('fontWeightBold');
|
const r25: string = t.getOption('fontWeightBold');
|
||||||
const r26: boolean = t.getOption('allowTransparency');
|
const r26: boolean = t.getOption('allowTransparency');
|
||||||
|
const r27: boolean = t.getOption('rightClickSelectsWord');
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
const t: Terminal = new Terminal();
|
const t: Terminal = new Terminal();
|
||||||
@@ -187,6 +188,7 @@ namespace methods_core {
|
|||||||
t.setOption('fontFamily', 'foo');
|
t.setOption('fontFamily', 'foo');
|
||||||
t.setOption('theme', {background: '#ff0000'});
|
t.setOption('theme', {background: '#ff0000'});
|
||||||
t.setOption('macOptionIsMeta', true);
|
t.setOption('macOptionIsMeta', true);
|
||||||
|
t.setOption('rightClickSelectsWord', false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
namespace scrolling {
|
namespace scrolling {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class TestSelectionManager extends SelectionManager {
|
|||||||
public get model(): SelectionModel { return this._model; }
|
public get model(): SelectionModel { return this._model; }
|
||||||
|
|
||||||
public selectLineAt(line: number): void { this._selectLineAt(line); }
|
public selectLineAt(line: number): void { this._selectLineAt(line); }
|
||||||
public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords); }
|
public selectWordAt(coords: [number, number]): void { this._selectWordAt(coords, true); }
|
||||||
|
|
||||||
// Disable DOM interaction
|
// Disable DOM interaction
|
||||||
public enable(): void {}
|
public enable(): void {}
|
||||||
|
|||||||
+41
-5
@@ -253,6 +253,37 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
|
|||||||
this.emit('refresh', { start: this._model.finalSelectionStart, end: this._model.finalSelectionEnd });
|
this.emit('refresh', { start: this._model.finalSelectionStart, end: this._model.finalSelectionEnd });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the current click was inside the current selection
|
||||||
|
* @param event The mouse event
|
||||||
|
*/
|
||||||
|
public isClickInSelection(event: MouseEvent): boolean {
|
||||||
|
const coords = this._getMouseBufferCoords(event);
|
||||||
|
const start = this._model.finalSelectionStart;
|
||||||
|
const end = this._model.finalSelectionEnd;
|
||||||
|
|
||||||
|
if (!start || !end) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (coords[1] > start[1] && coords[1] < end[1]) ||
|
||||||
|
(start[1] === end[1] && coords[1] === start[1] && coords[0] > start[0] && coords[0] < end[0]) ||
|
||||||
|
(start[1] < end[1] && coords[1] === end[1] && coords[0] < end[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selects word at the current mouse event coordinates.
|
||||||
|
* @param event The mouse event.
|
||||||
|
*/
|
||||||
|
public selectWordAtCursor(event: MouseEvent): void {
|
||||||
|
const coords = this._getMouseBufferCoords(event);
|
||||||
|
if (coords) {
|
||||||
|
this._selectWordAt(coords, false);
|
||||||
|
this._model.selectionEnd = null;
|
||||||
|
this.refresh(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects all text within the terminal.
|
* Selects all text within the terminal.
|
||||||
*/
|
*/
|
||||||
@@ -445,7 +476,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
|
|||||||
const coords = this._getMouseBufferCoords(event);
|
const coords = this._getMouseBufferCoords(event);
|
||||||
if (coords) {
|
if (coords) {
|
||||||
this._activeSelectionMode = SelectionMode.WORD;
|
this._activeSelectionMode = SelectionMode.WORD;
|
||||||
this._selectWordAt(coords);
|
this._selectWordAt(coords, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -602,7 +633,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
|
|||||||
* Gets positional information for the word at the coordinated specified.
|
* Gets positional information for the word at the coordinated specified.
|
||||||
* @param coords The coordinates to get the word at.
|
* @param coords The coordinates to get the word at.
|
||||||
*/
|
*/
|
||||||
private _getWordAt(coords: [number, number]): IWordPosition {
|
private _getWordAt(coords: [number, number], allowWhitespaceOnlySelection: boolean): IWordPosition {
|
||||||
const bufferLine = this._buffer.lines.get(coords[1]);
|
const bufferLine = this._buffer.lines.get(coords[1]);
|
||||||
if (!bufferLine) {
|
if (!bufferLine) {
|
||||||
return null;
|
return null;
|
||||||
@@ -708,15 +739,20 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
|
|||||||
- leftLongCharOffset // The number of additional chars left of the initial char added by columns with strings longer than 1 (emojis)
|
- leftLongCharOffset // The number of additional chars left of the initial char added by columns with strings longer than 1 (emojis)
|
||||||
- rightLongCharOffset); // The number of additional chars right of the initial char (inclusive) added by columns with strings longer than 1 (emojis)
|
- rightLongCharOffset); // The number of additional chars right of the initial char (inclusive) added by columns with strings longer than 1 (emojis)
|
||||||
|
|
||||||
|
if (!allowWhitespaceOnlySelection && line.slice(startIndex, endIndex).trim() === '') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return { start, length };
|
return { start, length };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects the word at the coordinates specified.
|
* Selects the word at the coordinates specified.
|
||||||
* @param coords The coordinates to get the word at.
|
* @param coords The coordinates to get the word at.
|
||||||
|
* @param allowWhitespaceOnlySelection If whitespace should be selected
|
||||||
*/
|
*/
|
||||||
protected _selectWordAt(coords: [number, number]): void {
|
protected _selectWordAt(coords: [number, number], allowWhitespaceOnlySelection: boolean): void {
|
||||||
const wordPosition = this._getWordAt(coords);
|
const wordPosition = this._getWordAt(coords, allowWhitespaceOnlySelection);
|
||||||
if (wordPosition) {
|
if (wordPosition) {
|
||||||
this._model.selectionStart = [wordPosition.start, coords[1]];
|
this._model.selectionStart = [wordPosition.start, coords[1]];
|
||||||
this._model.selectionStartLength = wordPosition.length;
|
this._model.selectionStartLength = wordPosition.length;
|
||||||
@@ -728,7 +764,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager
|
|||||||
* @param coords The coordinates to get the word at.
|
* @param coords The coordinates to get the word at.
|
||||||
*/
|
*/
|
||||||
private _selectToWordAt(coords: [number, number]): void {
|
private _selectToWordAt(coords: [number, number]): void {
|
||||||
const wordPosition = this._getWordAt(coords);
|
const wordPosition = this._getWordAt(coords, true);
|
||||||
if (wordPosition) {
|
if (wordPosition) {
|
||||||
this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]];
|
this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]];
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -87,7 +87,8 @@ const DEFAULT_OPTIONS: ITerminalOptions = {
|
|||||||
useFlowControl: false,
|
useFlowControl: false,
|
||||||
allowTransparency: false,
|
allowTransparency: false,
|
||||||
tabStopWidth: 8,
|
tabStopWidth: 8,
|
||||||
theme: null
|
theme: null,
|
||||||
|
rightClickSelectsWord: Browser.isMac
|
||||||
// programFeatures: false,
|
// programFeatures: false,
|
||||||
// focusKeys: false,
|
// focusKeys: false,
|
||||||
};
|
};
|
||||||
@@ -506,12 +507,12 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
|
|||||||
// Firefox doesn't appear to fire the contextmenu event on right click
|
// Firefox doesn't appear to fire the contextmenu event on right click
|
||||||
on(this.element, 'mousedown', (event: MouseEvent) => {
|
on(this.element, 'mousedown', (event: MouseEvent) => {
|
||||||
if (event.button === 2) {
|
if (event.button === 2) {
|
||||||
rightClickHandler(event, this.textarea, this.selectionManager);
|
rightClickHandler(event, this.textarea, this.selectionManager, this.options.rightClickSelectsWord);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
on(this.element, 'contextmenu', (event: MouseEvent) => {
|
on(this.element, 'contextmenu', (event: MouseEvent) => {
|
||||||
rightClickHandler(event, this.textarea, this.selectionManager);
|
rightClickHandler(event, this.textarea, this.selectionManager, this.options.rightClickSelectsWord);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -285,6 +285,8 @@ export interface ISelectionManager {
|
|||||||
disable(): void;
|
disable(): void;
|
||||||
enable(): void;
|
enable(): void;
|
||||||
setSelection(row: number, col: number, length: number): void;
|
setSelection(row: number, col: number, length: number): void;
|
||||||
|
isClickInSelection(event: MouseEvent): boolean;
|
||||||
|
selectWordAtCursor(event: MouseEvent): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ILinkifier extends IEventEmitter {
|
export interface ILinkifier extends IEventEmitter {
|
||||||
|
|||||||
@@ -116,10 +116,15 @@ export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextA
|
|||||||
* @param ev The original right click event to be handled.
|
* @param ev The original right click event to be handled.
|
||||||
* @param textarea The terminal's textarea.
|
* @param textarea The terminal's textarea.
|
||||||
* @param selectionManager The terminal's selection manager.
|
* @param selectionManager The terminal's selection manager.
|
||||||
|
* @param shouldSelectWord If true and there is no selection the current word will be selected
|
||||||
*/
|
*/
|
||||||
export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager): void {
|
export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager, shouldSelectWord: boolean): void {
|
||||||
moveTextAreaUnderMouseCursor(ev, textarea);
|
moveTextAreaUnderMouseCursor(ev, textarea);
|
||||||
|
|
||||||
|
if (shouldSelectWord && !selectionManager.isClickInSelection(ev)) {
|
||||||
|
selectionManager.selectWordAtCursor(ev);
|
||||||
|
}
|
||||||
|
|
||||||
// Get textarea ready to copy from the context menu
|
// Get textarea ready to copy from the context menu
|
||||||
textarea.value = selectionManager.selectionText;
|
textarea.value = selectionManager.selectionText;
|
||||||
textarea.select();
|
textarea.select();
|
||||||
|
|||||||
Vendored
+8
-2
@@ -93,6 +93,12 @@ declare module 'xterm' {
|
|||||||
*/
|
*/
|
||||||
macOptionIsMeta?: boolean;
|
macOptionIsMeta?: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether to select the word under the cursor on right click, this is
|
||||||
|
* standard behavior in a lot of macOS applications.
|
||||||
|
*/
|
||||||
|
rightClickSelectsWord?: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number of rows in the terminal.
|
* The number of rows in the terminal.
|
||||||
*/
|
*/
|
||||||
@@ -427,7 +433,7 @@ declare module 'xterm' {
|
|||||||
* Retrieves an option's value from the terminal.
|
* Retrieves an option's value from the terminal.
|
||||||
* @param key The option key.
|
* @param key The option key.
|
||||||
*/
|
*/
|
||||||
getOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'debug' | 'disableStdin' | 'enableBold' | 'macOptionIsMeta' | 'popOnBell' | 'screenKeys' | 'useFlowControl' | 'visualBell'): boolean;
|
getOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'debug' | 'disableStdin' | 'enableBold' | 'macOptionIsMeta' | 'rightClickSelectsWord' | 'popOnBell' | 'screenKeys' | 'useFlowControl' | 'visualBell'): boolean;
|
||||||
/**
|
/**
|
||||||
* Retrieves an option's value from the terminal.
|
* Retrieves an option's value from the terminal.
|
||||||
* @param key The option key.
|
* @param key The option key.
|
||||||
@@ -478,7 +484,7 @@ declare module 'xterm' {
|
|||||||
* @param key The option key.
|
* @param key The option key.
|
||||||
* @param value The option value.
|
* @param value The option value.
|
||||||
*/
|
*/
|
||||||
setOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'debug' | 'disableStdin' | 'enableBold' | 'macOptionIsMeta' | 'popOnBell' | 'screenKeys' | 'useFlowControl' | 'visualBell', value: boolean): void;
|
setOption(key: 'allowTransparency' | 'cancelEvents' | 'convertEol' | 'cursorBlink' | 'debug' | 'disableStdin' | 'enableBold' | 'macOptionIsMeta' | 'popOnBell' | 'rightClickSelectsWord' | 'screenKeys' | 'useFlowControl' | 'visualBell', value: boolean): void;
|
||||||
/**
|
/**
|
||||||
* Sets an option on the terminal.
|
* Sets an option on the terminal.
|
||||||
* @param key The option key.
|
* @param key The option key.
|
||||||
|
|||||||
Reference in New Issue
Block a user