Skip to content

Commit fd351a5

Browse files
ppotleo
authored andcommitted
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
1 parent 77597da commit fd351a5

File tree

6 files changed

+39
-4
lines changed

6 files changed

+39
-4
lines changed

app/config-default.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ module.exports = {
6464
env: {},
6565

6666
// set to false for no bell
67-
bell: 'SOUND'
67+
bell: 'SOUND',
68+
69+
// if true, selected text will automatically be copied to the clipboard
70+
copyOnSelect: false
6871

6972
// URL to custom bell
7073
// bellSoundURL: 'http://example.com/bell.mp3',

lib/components/term.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export default class Term extends Component {
4545
this.term.prefs_.set('audible-bell-sound', '');
4646
}
4747

48+
if (props.copyOnSelect) {
49+
this.term.prefs_.set('copy-on-select', true);
50+
} else {
51+
this.term.prefs_.set('copy-on-select', false);
52+
}
53+
4854
this.term.onTerminalReady = () => {
4955
const io = this.term.io.push();
5056
io.onVTKeystroke = io.sendString = props.onData;
@@ -215,6 +221,12 @@ export default class Term extends Component {
215221
} else {
216222
this.term.prefs_.set('audible-bell-sound', '');
217223
}
224+
225+
if (this.props.copyOnSelect) {
226+
this.term.prefs_.set('copy-on-select', true);
227+
} else {
228+
this.term.prefs_.set('copy-on-select', false);
229+
}
218230
}
219231

220232
componentWillUnmount () {

lib/components/terms.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export default class Terms extends Component {
145145
onData: this.bind(this.props.onData, null, uid),
146146
onURLAbort: this.bind(this.props.onURLAbort, null, uid),
147147
bell: this.props.bell,
148-
bellSoundURL: this.props.bellSoundURL
148+
bellSoundURL: this.props.bellSoundURL,
149+
copyOnSelect: this.props.copyOnSelect
149150
});
150151
return <div
151152
key={`d${uid}`}

lib/containers/terms.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const TermsContainer = connect(
3232
foregroundColor: state.ui.foregroundColor,
3333
backgroundColor: state.ui.backgroundColor,
3434
bell: state.ui.bell,
35-
bellSoundURL: state.ui.bellSoundURL
35+
bellSoundURL: state.ui.bellSoundURL,
36+
copyOnSelect: state.ui.copyOnSelect
3637
};
3738
},
3839
(dispatch) => {

lib/hterm.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { hterm, lib } from 'hterm-umdjs';
22

33
hterm.defaultStorage = new lib.Storage.Memory();
44

5+
// clear selection range of current selected term view
6+
// Fix event when terminal text is selected and keyboard action is invoked
7+
hterm.Terminal.prototype.clearSelection = function () {
8+
this.document_.getSelection().removeAllRanges();
9+
};
10+
511
// override double click behavior to copy
612
const oldMouse = hterm.Terminal.prototype.onMouse_;
713
hterm.Terminal.prototype.onMouse_ = function (e) {
@@ -85,6 +91,11 @@ hterm.Keyboard.prototype.onKeyDown_ = function (e) {
8591

8692
if (e.metaKey || e.altKey) {
8793
return;
94+
} else {
95+
// Test for valid keys in order to clear the terminal selection
96+
if ((!e.ctrlKey || e.code !== 'ControlLeft') && !e.shiftKey && e.code !== 'CapsLock') {
97+
this.terminal.clearSelection();
98+
}
8899
}
89100
return oldKeyDown.call(this, e);
90101
};
@@ -93,6 +104,8 @@ const oldKeyPress = hterm.Keyboard.prototype.onKeyPress_;
93104
hterm.Keyboard.prototype.onKeyPress_ = function (e) {
94105
if (e.metaKey) {
95106
return;
107+
} else {
108+
this.terminal.clearSelection();
96109
}
97110
return oldKeyPress.call(this, e);
98111
};

lib/reducers/ui.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ const initial = Immutable({
7070
updateVersion: null,
7171
updateNotes: null,
7272
bell: 'SOUND',
73-
bellSoundURL: 'lib-resource:hterm/audio/bell'
73+
bellSoundURL: 'lib-resource:hterm/audio/bell',
74+
copyOnSelect: false
7475
});
7576

7677
const reducer = (state = initial, action) => {
@@ -138,6 +139,10 @@ const reducer = (state = initial, action) => {
138139
ret.bellSoundURL = config.bellSoundURL || initial.bellSoundURL;
139140
}
140141

142+
if (null !== config.copyOnSelect) {
143+
ret.copyOnSelect = config.copyOnSelect;
144+
}
145+
141146
if (null != config.colors) {
142147
if (Array.isArray(config.colors)) {
143148
const stateColors = Array.isArray(state.colors)

0 commit comments

Comments
 (0)