Skip to content

Commit f15d6d7

Browse files
author
Jonathan Yannes
committed
added copy/paste functionality
1 parent 56d08db commit f15d6d7

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-web-terminal",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "A web-based terminal facade built on reactjs",
55
"repository": {
66
"type": "git",

src/components/react-web-terminal/react-web-terminal-cursor.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ export default class WebTerminalCursor extends React.Component {
2222
this.setState(this.state);
2323
}
2424

25-
moveLeft() {
25+
moveLeft(amount = 1) {
2626
if (this.state.cursorPos > 0)
27-
this.setCursorPos(this.getCursorPos() - 1);
27+
this.setCursorPos(this.getCursorPos() - amount);
2828
}
2929

30-
moveRight() {
30+
moveRight(amount = 1) {
3131
if (this.state.cursorPos < this.getInput().length)
32-
this.setCursorPos(this.getCursorPos() + 1);
32+
this.setCursorPos(this.getCursorPos() + amount);
3333
}
3434

3535
moveToBegining() {

src/components/react-web-terminal/react-web-terminal-input-buffer.jsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ export default class WebTerminalInputBuffer extends React.Component {
1313
}
1414

1515
getInput() {
16-
return this.state.input;
16+
return this.state.input;
1717
}
1818

1919
resetInputBuffer() {
2020
this.state.input = '';
2121
this.cursor.moveToBegining();
2222
}
2323

24+
insertText(text) {
25+
this.state.input = this.getPreCursorStr() + text + this.cursor.getCursorChar() + this.getPostCursorStr();
26+
this.cursor.moveRight(text.length);
27+
this.setState(this.state);
28+
}
29+
2430
handleKeyDown(e) {
2531
const key = getKey(e);
2632
if (key === 'Enter') {
@@ -36,13 +42,24 @@ export default class WebTerminalInputBuffer extends React.Component {
3642
} else if (key === 'ArrowRight') {
3743
this.cursor.moveRight();
3844
this.forceUpdate();
45+
} else if (navigator.appVersion.indexOf('Mac') !== -1 && e.metaKey && key === 'c') {
46+
return;
47+
} else if (navigator.appVersion.indexOf('Mac') !== -1 && e.metaKey && key === 'v') {
48+
return;
49+
} else if (e.ctrlKey && key === 'c') {
50+
return;
51+
} else if (e.ctrlKey && key === 'v') {
52+
return;
3953
} else if (nonPrintableKeys.indexOf(key) === -1) {
40-
this.state.input = this.getPreCursorStr() + key + this.cursor.getCursorChar() + this.getPostCursorStr();
41-
this.cursor.moveRight();
42-
this.setState(this.state);
54+
this.insertText(key);
4355
}
4456
}
4557

58+
onPaste(e) {
59+
const pastedText = e.clipboardData.getData('Text');
60+
this.insertText(pastedText);
61+
}
62+
4663
onCommandEntered() {
4764
if (this.props.onCommandEntered) this.props.onCommandEntered();
4865
}

src/components/react-web-terminal/react-web-terminal-input.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export default class WebTerminalInput extends React.Component {
2323
this.buffer.handleKeyDown(e);
2424
}
2525

26+
onPaste(e) {
27+
this.buffer.onPaste(e);
28+
}
29+
2630
onCommandEntered() {
2731
if (this.props.onCommandEntered) this.props.onCommandEntered();
2832
}

src/components/react-web-terminal/react-web-terminal.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export default class WebTerminal extends React.Component {
5454
if (!this.keyStrokeMapHandler(e)) this.inputComp.handleKeyDown(e);
5555
}
5656

57+
onPaste(e) {
58+
this.inputComp.onPaste(e);
59+
}
60+
5761
onCommandEntered() {
5862
this.addToLog(this.inputComp.state.prompt + this.input(), 'react-web-terminal-input');
5963

@@ -82,7 +86,7 @@ export default class WebTerminal extends React.Component {
8286
});
8387

8488
return (
85-
<div tabIndex="-1" ref={(root) => { this.root = root }} className="react-web-terminal" onKeyDown={this.handleKeyDown.bind(this)}>
89+
<div tabIndex="-1" ref={(root) => { this.root = root }} className="react-web-terminal" onKeyDown={this.handleKeyDown.bind(this)} onPaste={this.onPaste.bind(this)}>
8690
{logNodes}
8791
<WebTerminalInput ref={(input) => { this.inputComp = input }} prompt={this.props.prompt} onCommandEntered={this.onCommandEntered.bind(this)} />
8892
</div>

0 commit comments

Comments
 (0)