Provide clear selection of text in terminal view (#608)

* Permit clearSelection on text enter and mouse selection. Fix #591

* Add config for copyOnSelect

* Update with descriptive comment
This commit is contained in:
Philippe Potvin 2016-08-13 17:03:44 -04:00 committed by Leo Lamprecht
parent 77597da1d3
commit fd351a5b93
6 changed files with 39 additions and 4 deletions

View file

@ -64,7 +64,10 @@ module.exports = {
env: {},
// set to false for no bell
bell: 'SOUND'
bell: 'SOUND',
// if true, selected text will automatically be copied to the clipboard
copyOnSelect: false
// URL to custom bell
// bellSoundURL: 'http://example.com/bell.mp3',

View file

@ -45,6 +45,12 @@ export default class Term extends Component {
this.term.prefs_.set('audible-bell-sound', '');
}
if (props.copyOnSelect) {
this.term.prefs_.set('copy-on-select', true);
} else {
this.term.prefs_.set('copy-on-select', false);
}
this.term.onTerminalReady = () => {
const io = this.term.io.push();
io.onVTKeystroke = io.sendString = props.onData;
@ -215,6 +221,12 @@ export default class Term extends Component {
} else {
this.term.prefs_.set('audible-bell-sound', '');
}
if (this.props.copyOnSelect) {
this.term.prefs_.set('copy-on-select', true);
} else {
this.term.prefs_.set('copy-on-select', false);
}
}
componentWillUnmount () {

View file

@ -145,7 +145,8 @@ export default class Terms extends Component {
onData: this.bind(this.props.onData, null, uid),
onURLAbort: this.bind(this.props.onURLAbort, null, uid),
bell: this.props.bell,
bellSoundURL: this.props.bellSoundURL
bellSoundURL: this.props.bellSoundURL,
copyOnSelect: this.props.copyOnSelect
});
return <div
key={`d${uid}`}

View file

@ -32,7 +32,8 @@ const TermsContainer = connect(
foregroundColor: state.ui.foregroundColor,
backgroundColor: state.ui.backgroundColor,
bell: state.ui.bell,
bellSoundURL: state.ui.bellSoundURL
bellSoundURL: state.ui.bellSoundURL,
copyOnSelect: state.ui.copyOnSelect
};
},
(dispatch) => {

View file

@ -2,6 +2,12 @@ import { hterm, lib } from 'hterm-umdjs';
hterm.defaultStorage = new lib.Storage.Memory();
// clear selection range of current selected term view
// Fix event when terminal text is selected and keyboard action is invoked
hterm.Terminal.prototype.clearSelection = function () {
this.document_.getSelection().removeAllRanges();
};
// override double click behavior to copy
const oldMouse = hterm.Terminal.prototype.onMouse_;
hterm.Terminal.prototype.onMouse_ = function (e) {
@ -85,6 +91,11 @@ hterm.Keyboard.prototype.onKeyDown_ = function (e) {
if (e.metaKey || e.altKey) {
return;
} else {
// Test for valid keys in order to clear the terminal selection
if ((!e.ctrlKey || e.code !== 'ControlLeft') && !e.shiftKey && e.code !== 'CapsLock') {
this.terminal.clearSelection();
}
}
return oldKeyDown.call(this, e);
};
@ -93,6 +104,8 @@ const oldKeyPress = hterm.Keyboard.prototype.onKeyPress_;
hterm.Keyboard.prototype.onKeyPress_ = function (e) {
if (e.metaKey) {
return;
} else {
this.terminal.clearSelection();
}
return oldKeyPress.call(this, e);
};

View file

@ -70,7 +70,8 @@ const initial = Immutable({
updateVersion: null,
updateNotes: null,
bell: 'SOUND',
bellSoundURL: 'lib-resource:hterm/audio/bell'
bellSoundURL: 'lib-resource:hterm/audio/bell',
copyOnSelect: false
});
const reducer = (state = initial, action) => {
@ -138,6 +139,10 @@ const reducer = (state = initial, action) => {
ret.bellSoundURL = config.bellSoundURL || initial.bellSoundURL;
}
if (null !== config.copyOnSelect) {
ret.copyOnSelect = config.copyOnSelect;
}
if (null != config.colors) {
if (Array.isArray(config.colors)) {
const stateColors = Array.isArray(state.colors)