Skip to content

Commit 73ec8a8

Browse files
authored
Merge pull request #1912 from mofux/contextmenu_fix
Make textarea positioning work with css transformations
2 parents 682efc7 + ace855d commit 73ec8a8

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

src/Terminal.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,12 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
566566
// Firefox doesn't appear to fire the contextmenu event on right click
567567
this.register(addDisposableDomListener(this.element, 'mousedown', (event: MouseEvent) => {
568568
if (event.button === 2) {
569-
rightClickHandler(event, this.textarea, this.selectionManager, this.options.rightClickSelectsWord);
569+
rightClickHandler(event, this, this.selectionManager, this.options.rightClickSelectsWord);
570570
}
571571
}));
572572
} else {
573573
this.register(addDisposableDomListener(this.element, 'contextmenu', (event: MouseEvent) => {
574-
rightClickHandler(event, this.textarea, this.selectionManager, this.options.rightClickSelectsWord);
574+
rightClickHandler(event, this, this.selectionManager, this.options.rightClickSelectsWord);
575575
}));
576576
}
577577

@@ -583,7 +583,7 @@ export class Terminal extends EventEmitter implements ITerminal, IDisposable, II
583583
// that the regular click event doesn't fire for the middle mouse button.
584584
this.register(addDisposableDomListener(this.element, 'auxclick', (event: MouseEvent) => {
585585
if (event.button === 1) {
586-
moveTextAreaUnderMouseCursor(event, this.textarea);
586+
moveTextAreaUnderMouseCursor(event, this);
587587
}
588588
}));
589589
}

src/ui/Clipboard.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,32 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal): void {
8585
* @param ev The original right click event to be handled.
8686
* @param textarea The terminal's textarea.
8787
*/
88-
export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextAreaElement): void {
88+
export function moveTextAreaUnderMouseCursor(ev: MouseEvent, term: ITerminal): void {
89+
90+
// Calculate textarea position relative to the screen element
91+
const pos = term.screenElement.getBoundingClientRect();
92+
const left = ev.clientX - pos.left - 10;
93+
const top = ev.clientY - pos.top - 10;
94+
8995
// Bring textarea at the cursor position
90-
textarea.style.position = 'fixed';
91-
textarea.style.width = '20px';
92-
textarea.style.height = '20px';
93-
textarea.style.left = (ev.clientX - 10) + 'px';
94-
textarea.style.top = (ev.clientY - 10) + 'px';
95-
textarea.style.zIndex = '1000';
96+
term.textarea.style.position = 'absolute';
97+
term.textarea.style.width = '20px';
98+
term.textarea.style.height = '20px';
99+
term.textarea.style.left = `${left}px`;
100+
term.textarea.style.top = `${top}px`;
101+
term.textarea.style.zIndex = '1000';
96102

97-
textarea.focus();
103+
term.textarea.focus();
98104

99105
// Reset the terminal textarea's styling
100106
// Timeout needs to be long enough for click event to be handled.
101107
setTimeout(() => {
102-
textarea.style.position = null;
103-
textarea.style.width = null;
104-
textarea.style.height = null;
105-
textarea.style.left = null;
106-
textarea.style.top = null;
107-
textarea.style.zIndex = null;
108+
term.textarea.style.position = null;
109+
term.textarea.style.width = null;
110+
term.textarea.style.height = null;
111+
term.textarea.style.left = null;
112+
term.textarea.style.top = null;
113+
term.textarea.style.zIndex = null;
108114
}, 200);
109115
}
110116

@@ -115,14 +121,14 @@ export function moveTextAreaUnderMouseCursor(ev: MouseEvent, textarea: HTMLTextA
115121
* @param selectionManager The terminal's selection manager.
116122
* @param shouldSelectWord If true and there is no selection the current word will be selected
117123
*/
118-
export function rightClickHandler(ev: MouseEvent, textarea: HTMLTextAreaElement, selectionManager: ISelectionManager, shouldSelectWord: boolean): void {
119-
moveTextAreaUnderMouseCursor(ev, textarea);
124+
export function rightClickHandler(ev: MouseEvent, term: ITerminal, selectionManager: ISelectionManager, shouldSelectWord: boolean): void {
125+
moveTextAreaUnderMouseCursor(ev, term);
120126

121127
if (shouldSelectWord && !selectionManager.isClickInSelection(ev)) {
122128
selectionManager.selectWordAtCursor(ev);
123129
}
124130

125131
// Get textarea ready to copy from the context menu
126-
textarea.value = selectionManager.selectionText;
127-
textarea.select();
132+
term.textarea.value = selectionManager.selectionText;
133+
term.textarea.select();
128134
}

0 commit comments

Comments
 (0)