terminal: Make mouse paste work with xterm.js.
This makes the xterm.js backend behave the same as the existing hterm
backend:
- Disable the right-click context menu.
- Paste on mousedown event.
- Middle click pastes.
- Right click pastes if enabled in preferences.
- Don't paste while in mouse tracking mode.
Change-Id: I56cda037e07e2a55d00168d18b405ad502b1470d
Reviewed-on: https://chromium-review.googlesource.com/c/apps/libapps/+/3920392
Reviewed-by: Jason Lin <lxj@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/terminal/js/terminal_emulator.js b/terminal/js/terminal_emulator.js
index 34debc2..ccdec56 100644
--- a/terminal/js/terminal_emulator.js
+++ b/terminal/js/terminal_emulator.js
@@ -415,6 +415,27 @@
// TODO: Make a11y work. Maybe we can just use hterm.AccessibilityReader.
this.notificationCenter_ = new hterm.NotificationCenter(document.body);
+ // Block right-click context menu from popping up.
+ elem.addEventListener('contextmenu', (e) => e.preventDefault());
+
+ // Add a handler for pasting with the mouse.
+ elem.addEventListener('mousedown', async (e) => {
+ if (this.term.modes.mouseTrackingMode !== 'none') {
+ // xterm.js is in mouse mode and will handle the event.
+ return;
+ }
+ const MIDDLE = 1;
+ const RIGHT = 2;
+ if (e.button === MIDDLE || (e.button === RIGHT &&
+ this.prefs_.getBoolean('mouse-right-click-paste'))) {
+ // Paste.
+ if (navigator.clipboard && navigator.clipboard.readText) {
+ const text = await navigator.clipboard.readText();
+ this.term.paste(text);
+ }
+ }
+ });
+
this.onTerminalReady();
})();
}