From 280f14e23953b73dc6c041ef60083f140bb609b0 Mon Sep 17 00:00:00 2001 From: Derrick Pelletier Date: Sun, 22 Oct 2017 14:16:52 -0700 Subject: [PATCH] Pasting a copied file or dir provides full path to resource (#2364) * Added a utility for processing clipboard data * using paste processing utility in term component to expand filepath * removed linux case * moved active tab to guard so only process when active * commenting paste event handler for clarity --- lib/components/term.js | 23 +++++++++++++++++++++++ lib/utils/paste.js | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 lib/utils/paste.js diff --git a/lib/components/term.js b/lib/components/term.js index 87d0d0fe..57581e1b 100644 --- a/lib/components/term.js +++ b/lib/components/term.js @@ -5,6 +5,7 @@ import {PureComponent} from '../base-components'; import terms from '../terms'; import returnKey from '../utils/keymaps'; import CommandRegistry from '../command-registry'; +import processClipboard from '../utils/paste'; // map old hterm constants to xterm.js const CURSOR_STYLES = { @@ -22,6 +23,7 @@ export default class Term extends PureComponent { this.termRect = null; this.onOpen = this.onOpen.bind(this); this.onWindowResize = this.onWindowResize.bind(this); + this.onWindowPaste = this.onWindowPaste.bind(this); this.onTermRef = this.onTermRef.bind(this); this.onTermWrapperRef = this.onTermWrapperRef.bind(this); } @@ -77,6 +79,10 @@ export default class Term extends PureComponent { passive: true }); + window.addEventListener('paste', this.onWindowPaste, { + capture: true + }); + terms[this.props.uid] = this; } @@ -110,6 +116,19 @@ export default class Term extends PureComponent { this.fitResize(); } + // intercepting paste event for any necessary processing of + // clipboard data, if result is falsy, paste event continues + onWindowPaste(e) { + if (!this.props.isTermActive) return; + + const processed = processClipboard(); + if (processed) { + e.preventDefault(); + e.stopPropagation(); + this.term.send(processed); + } + } + write(data) { this.term.write(data); } @@ -198,6 +217,10 @@ export default class Term extends PureComponent { window.removeEventListener('resize', this.onWindowResize, { passive: true }); + + window.removeEventListener('paste', this.onWindowPaste, { + capture: true + }); } template(css) { diff --git a/lib/utils/paste.js b/lib/utils/paste.js new file mode 100644 index 00000000..5309271b --- /dev/null +++ b/lib/utils/paste.js @@ -0,0 +1,21 @@ +import {clipboard} from 'electron'; + +const getPath = platform => { + switch (platform) { + case 'darwin': { + const filepath = clipboard.read('public.file-url'); + return filepath.replace('file://', ''); + } + case 'win32': { + const filepath = clipboard.read('FileNameW'); + return filepath.replace(new RegExp(String.fromCharCode(0), 'g'), ''); + } + // linux already pastes full path + default: + return null; + } +}; + +export default function processClipboard() { + return getPath(process.platform); +}