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
This commit is contained in:
Derrick Pelletier 2017-10-22 14:16:52 -07:00 committed by Albin Ekblom
parent e29651bbe3
commit 280f14e239
2 changed files with 44 additions and 0 deletions

View file

@ -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) {

21
lib/utils/paste.js Normal file
View file

@ -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);
}