@webscopeio/react-console
Advanced tools
@@ -17,4 +17,2 @@ /** | ||
inputStyle?: object; | ||
history?: Array<string>; | ||
onAddHistoryItem?: (entry: string) => void; | ||
}; | ||
@@ -25,5 +23,2 @@ declare type State = { | ||
input: string; | ||
historyPosition: number; | ||
reverseSearchString?: string; | ||
reverseSearchPosition: number; | ||
}; | ||
@@ -33,3 +28,2 @@ export default class ReactConsole extends React.Component<Props, State> { | ||
wrapperRef: any; | ||
reverseStringRef: any; | ||
static defaultProps: { | ||
@@ -47,5 +41,2 @@ prompt: string; | ||
commandInProgress: boolean; | ||
reverseSearchString: undefined; | ||
historyPosition: number; | ||
reverseSearchPosition: number; | ||
}; | ||
@@ -55,78 +46,7 @@ componentDidMount(): void; | ||
scrollToBottom: () => void; | ||
/** | ||
* Get filtered history entries based on reverse search string | ||
*/ | ||
private getReverseHistory; | ||
/** | ||
* Takes current text of a main input and generates a string that will be outputted as a log. | ||
*/ | ||
private getCurrentTextSnapshot; | ||
private onSubmit; | ||
onSubmit: (e: any) => Promise<void>; | ||
render(): JSX.Element; | ||
/** | ||
* Reverse search input handler | ||
* @param event | ||
*/ | ||
private onReverseStringInputChange; | ||
/** | ||
* Invoked when pressed ctrl+r and already in a reverse search mode. | ||
*/ | ||
private nextReverseSearch; | ||
/** | ||
* Helper function that sets the history preview based on a requested history index. | ||
* @param historyIndex | ||
*/ | ||
private executeNextReverseSearch; | ||
/** | ||
* This function sets a given history entry as a main input element content. | ||
* It's called when changing the preview of a 'current' history entry e.g. by ctrl+r call, or | ||
* when pressing up/down arrow. | ||
* @param historyPosition | ||
*/ | ||
private setPreviewPosition; | ||
/** | ||
* Enables reverse search. | ||
* The side effect is that we focus o reverse search input. | ||
*/ | ||
private onReverseSearch; | ||
/** | ||
* When reverse search is confirmed, we disable reverse search mode and keep the result. | ||
* @param e | ||
*/ | ||
private onReverseSearchSubmit; | ||
/** | ||
* Main input change handler. | ||
* @param event | ||
*/ | ||
private onInputChange; | ||
/** | ||
* Helper function to determine whether reverse search is active or not. | ||
*/ | ||
private isReverseSearchOn; | ||
/** | ||
* Disables reverse search mode. | ||
* @param keepPreviewString - determines whether the result of a reverse search should be kept or not | ||
*/ | ||
private disableReverseSearch; | ||
/** | ||
* onKeyDown implementation of a reverse search input. | ||
* @param event | ||
*/ | ||
private onReverseKeyDown; | ||
/** | ||
* onKeyDown implementation of a main input. | ||
* @param event | ||
*/ | ||
private onKeyDown; | ||
/** | ||
* Focuses console input. | ||
* Whenever an user clicks on a terminal, we want to focus an actual input where he/she can type. | ||
*/ | ||
onInputChange: (e: any) => void; | ||
focusConsole: () => void; | ||
/** | ||
* Calls onAddHistoryItem property and sets historyPosition to a default value. | ||
* @param inputString | ||
*/ | ||
private addHistoryEntry; | ||
} | ||
export {}; |
import { createElement, Component } from 'react'; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Copyright (c) Microsoft Corporation. | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
@@ -44,6 +44,7 @@ /* global Reflect, Promise */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -176,3 +177,2 @@ }); | ||
_this.wrapperRef = null; | ||
_this.reverseStringRef = null; | ||
_this.state = { | ||
@@ -182,5 +182,2 @@ input: '', | ||
commandInProgress: false, | ||
reverseSearchString: undefined, | ||
historyPosition: Infinity, | ||
reverseSearchPosition: Infinity, | ||
}; | ||
@@ -195,26 +192,8 @@ _this.clear = function () { | ||
}; | ||
/** | ||
* Get filtered history entries based on reverse search string | ||
*/ | ||
_this.getReverseHistory = function () { | ||
var reverseSearchString = _this.state.reverseSearchString; | ||
return _this.props.history === undefined ? | ||
[] | ||
: _this.props.history.map(function (entry) { return (reverseSearchString === undefined || reverseSearchString === '') ? | ||
// @ts-ignore | ||
false : entry.indexOf(reverseSearchString) !== -1; }); | ||
}; | ||
/** | ||
* Takes current text of a main input and generates a string that will be outputted as a log. | ||
*/ | ||
_this.getCurrentTextSnapshot = function () { | ||
var prompt = _this.props.prompt; | ||
var inputString = _this.state.input; | ||
return prompt + "\u00A0" + inputString; | ||
}; | ||
_this.onSubmit = function (e) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, inputString, log, _b, cmd, args, command, ret, cmdNotFound; | ||
var _a, prompt, inputString, log, _b, cmd, args, command, ret, cmdNotFound; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
prompt = this.props.prompt; | ||
e.preventDefault(); | ||
@@ -225,3 +204,3 @@ inputString = this.state.input; | ||
} | ||
log = this.getCurrentTextSnapshot(); | ||
log = prompt + "\u00A0" + inputString; | ||
if (inputString === '') { | ||
@@ -235,3 +214,2 @@ this.setState({ | ||
} | ||
this.addHistoryEntry(inputString); | ||
_b = inputString.split(" "), cmd = _b[0], args = _b.slice(1); | ||
@@ -267,165 +245,7 @@ if (cmd === 'clear') { | ||
}); }; | ||
/** | ||
* Reverse search input handler | ||
* @param event | ||
*/ | ||
_this.onReverseStringInputChange = function (event) { | ||
_this.onInputChange = function (e) { | ||
_this.setState({ | ||
reverseSearchString: event.target.value, | ||
}, function () { | ||
var history = _this.getReverseHistory(); | ||
var historyIndex = history.lastIndexOf(true); | ||
_this.executeNextReverseSearch(historyIndex); | ||
input: e.target.value, | ||
}); | ||
}; | ||
/** | ||
* Invoked when pressed ctrl+r and already in a reverse search mode. | ||
*/ | ||
_this.nextReverseSearch = function () { | ||
var history = _this.getReverseHistory(); | ||
var endOffset = Math.max(0, _this.state.reverseSearchPosition - 1); // so that we don't go from the end again | ||
var historyIndex = history.lastIndexOf(true, endOffset); | ||
_this.executeNextReverseSearch(historyIndex); | ||
}; | ||
/** | ||
* Helper function that sets the history preview based on a requested history index. | ||
* @param historyIndex | ||
*/ | ||
_this.executeNextReverseSearch = function (historyIndex) { | ||
_this.setState({ | ||
reverseSearchPosition: historyIndex, | ||
}); | ||
if (historyIndex !== -1) { | ||
_this.setPreviewPosition(historyIndex); | ||
} | ||
if (_this.state.reverseSearchString === '') { | ||
_this.setPreviewPosition(Infinity); | ||
} | ||
}; | ||
/** | ||
* This function sets a given history entry as a main input element content. | ||
* It's called when changing the preview of a 'current' history entry e.g. by ctrl+r call, or | ||
* when pressing up/down arrow. | ||
* @param historyPosition | ||
*/ | ||
_this.setPreviewPosition = function (historyPosition) { | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
_this.setState({ | ||
historyPosition: historyPosition, | ||
input: _this.props.history[historyPosition] || '', | ||
}); | ||
}; | ||
/** | ||
* Enables reverse search. | ||
* The side effect is that we focus o reverse search input. | ||
*/ | ||
_this.onReverseSearch = function () { | ||
// we enabled reverse search | ||
_this.setState({ | ||
reverseSearchString: '', | ||
}, function () { | ||
_this.reverseStringRef.focus(); | ||
}); | ||
}; | ||
/** | ||
* When reverse search is confirmed, we disable reverse search mode and keep the result. | ||
* @param e | ||
*/ | ||
_this.onReverseSearchSubmit = function (event) { | ||
event.preventDefault(); | ||
_this.disableReverseSearch(); | ||
}; | ||
/** | ||
* Main input change handler. | ||
* @param event | ||
*/ | ||
_this.onInputChange = function (event) { | ||
_this.setState({ | ||
input: event.target.value, | ||
}); | ||
}; | ||
/** | ||
* Helper function to determine whether reverse search is active or not. | ||
*/ | ||
_this.isReverseSearchOn = function () { return _this.state.reverseSearchString !== undefined; }; | ||
/** | ||
* Disables reverse search mode. | ||
* @param keepPreviewString - determines whether the result of a reverse search should be kept or not | ||
*/ | ||
_this.disableReverseSearch = function (keepPreviewString) { | ||
if (keepPreviewString === void 0) { keepPreviewString = true; } | ||
_this.setState({ | ||
reverseSearchString: undefined, | ||
}); | ||
if (!keepPreviewString) { | ||
_this.setState({ | ||
input: '', | ||
}); | ||
} | ||
setTimeout(function () { | ||
_this.inputRef.focus(); | ||
}); | ||
}; | ||
/** | ||
* onKeyDown implementation of a reverse search input. | ||
* @param event | ||
*/ | ||
_this.onReverseKeyDown = function (event) { | ||
if (event.which === 38 || event.which === 40) { // up or down | ||
_this.disableReverseSearch(); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 67 && event.ctrlKey) { // ctrl + c | ||
_this.disableReverseSearch(false); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 82 && event.ctrlKey) { // ctrl + r | ||
_this.nextReverseSearch(); | ||
event.preventDefault(); | ||
} | ||
}; | ||
/** | ||
* onKeyDown implementation of a main input. | ||
* @param event | ||
*/ | ||
_this.onKeyDown = function (event) { | ||
if (event.which === 38) { // key up | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
var currentPos = Math.min(_this.state.historyPosition, _this.props.history.length); | ||
var historyPosition = Math.max(0, currentPos - 1); | ||
_this.setPreviewPosition(historyPosition); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 40) { | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
var historyPosition = Math.min(_this.props.history.length, _this.state.historyPosition + 1); | ||
_this.setPreviewPosition(historyPosition); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 82 && event.ctrlKey) { // ctrl + r | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
_this.onReverseSearch(); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 67 && event.ctrlKey) { // ctrl + c | ||
_this.setState({ | ||
output: _this.state.output.concat([_this.getCurrentTextSnapshot()]), | ||
input: '', | ||
}); | ||
_this.scrollToBottom(); | ||
event.preventDefault(); | ||
} | ||
}; | ||
/** | ||
* Focuses console input. | ||
* Whenever an user clicks on a terminal, we want to focus an actual input where he/she can type. | ||
*/ | ||
_this.focusConsole = function () { | ||
@@ -438,15 +258,2 @@ if (_this.inputRef) { | ||
}; | ||
/** | ||
* Calls onAddHistoryItem property and sets historyPosition to a default value. | ||
* @param inputString | ||
*/ | ||
_this.addHistoryEntry = function (inputString) { | ||
var onAddHistoryItem = _this.props.onAddHistoryItem; | ||
if (typeof onAddHistoryItem === 'function') { | ||
onAddHistoryItem(inputString); | ||
} | ||
_this.setState({ | ||
historyPosition: Infinity, | ||
}); | ||
}; | ||
return _this; | ||
@@ -461,7 +268,2 @@ } | ||
} | ||
if (this.props.history !== undefined) { | ||
this.setState({ | ||
historyPosition: this.props.history.length, | ||
}); | ||
} | ||
}; | ||
@@ -483,6 +285,3 @@ ReactConsole.prototype.render = function () { | ||
"\u00A0"), | ||
createElement("input", { disabled: this.state.commandInProgress || this.isReverseSearchOn(), ref: function (ref) { return _this.inputRef = ref; }, autoFocus: autoFocus, value: this.state.input, onChange: this.onInputChange, onKeyDown: this.onKeyDown, autoComplete: 'off', spellCheck: false, autoCapitalize: 'false', name: "input", className: classnames([styles.input, inputClassName]), style: inputStyle }))), | ||
this.isReverseSearchOn() && createElement("form", { onSubmit: this.onReverseSearchSubmit }, | ||
"bck-i-search: ", | ||
createElement("input", { value: this.state.reverseSearchString, ref: function (ref) { return _this.reverseStringRef = ref; }, onKeyDown: this.onReverseKeyDown, className: classnames([styles.input, inputClassName]), onChange: this.onReverseStringInputChange })))); | ||
createElement("input", { disabled: this.state.commandInProgress, ref: function (ref) { return _this.inputRef = ref; }, autoFocus: autoFocus, value: this.state.input, onChange: this.onInputChange, autoComplete: 'off', spellCheck: false, autoCapitalize: 'false', name: "input", className: classnames([styles.input, inputClassName]), style: inputStyle }))))); | ||
}; | ||
@@ -489,0 +288,0 @@ ReactConsole.defaultProps = { |
@@ -8,14 +8,14 @@ 'use strict'; | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
this file except in compliance with the License. You may obtain a copy of the | ||
License at http://www.apache.org/licenses/LICENSE-2.0 | ||
Copyright (c) Microsoft Corporation. | ||
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
MERCHANTABLITY OR NON-INFRINGEMENT. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
See the Apache Version 2.0 License for specific language governing permissions | ||
and limitations under the License. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
@@ -49,6 +49,7 @@ /* global Reflect, Promise */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
@@ -181,3 +182,2 @@ }); | ||
_this.wrapperRef = null; | ||
_this.reverseStringRef = null; | ||
_this.state = { | ||
@@ -187,5 +187,2 @@ input: '', | ||
commandInProgress: false, | ||
reverseSearchString: undefined, | ||
historyPosition: Infinity, | ||
reverseSearchPosition: Infinity, | ||
}; | ||
@@ -200,26 +197,8 @@ _this.clear = function () { | ||
}; | ||
/** | ||
* Get filtered history entries based on reverse search string | ||
*/ | ||
_this.getReverseHistory = function () { | ||
var reverseSearchString = _this.state.reverseSearchString; | ||
return _this.props.history === undefined ? | ||
[] | ||
: _this.props.history.map(function (entry) { return (reverseSearchString === undefined || reverseSearchString === '') ? | ||
// @ts-ignore | ||
false : entry.indexOf(reverseSearchString) !== -1; }); | ||
}; | ||
/** | ||
* Takes current text of a main input and generates a string that will be outputted as a log. | ||
*/ | ||
_this.getCurrentTextSnapshot = function () { | ||
var prompt = _this.props.prompt; | ||
var inputString = _this.state.input; | ||
return prompt + "\u00A0" + inputString; | ||
}; | ||
_this.onSubmit = function (e) { return __awaiter(_this, void 0, void 0, function () { | ||
var _a, inputString, log, _b, cmd, args, command, ret, cmdNotFound; | ||
var _a, prompt, inputString, log, _b, cmd, args, command, ret, cmdNotFound; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
prompt = this.props.prompt; | ||
e.preventDefault(); | ||
@@ -230,3 +209,3 @@ inputString = this.state.input; | ||
} | ||
log = this.getCurrentTextSnapshot(); | ||
log = prompt + "\u00A0" + inputString; | ||
if (inputString === '') { | ||
@@ -240,3 +219,2 @@ this.setState({ | ||
} | ||
this.addHistoryEntry(inputString); | ||
_b = inputString.split(" "), cmd = _b[0], args = _b.slice(1); | ||
@@ -272,165 +250,7 @@ if (cmd === 'clear') { | ||
}); }; | ||
/** | ||
* Reverse search input handler | ||
* @param event | ||
*/ | ||
_this.onReverseStringInputChange = function (event) { | ||
_this.onInputChange = function (e) { | ||
_this.setState({ | ||
reverseSearchString: event.target.value, | ||
}, function () { | ||
var history = _this.getReverseHistory(); | ||
var historyIndex = history.lastIndexOf(true); | ||
_this.executeNextReverseSearch(historyIndex); | ||
input: e.target.value, | ||
}); | ||
}; | ||
/** | ||
* Invoked when pressed ctrl+r and already in a reverse search mode. | ||
*/ | ||
_this.nextReverseSearch = function () { | ||
var history = _this.getReverseHistory(); | ||
var endOffset = Math.max(0, _this.state.reverseSearchPosition - 1); // so that we don't go from the end again | ||
var historyIndex = history.lastIndexOf(true, endOffset); | ||
_this.executeNextReverseSearch(historyIndex); | ||
}; | ||
/** | ||
* Helper function that sets the history preview based on a requested history index. | ||
* @param historyIndex | ||
*/ | ||
_this.executeNextReverseSearch = function (historyIndex) { | ||
_this.setState({ | ||
reverseSearchPosition: historyIndex, | ||
}); | ||
if (historyIndex !== -1) { | ||
_this.setPreviewPosition(historyIndex); | ||
} | ||
if (_this.state.reverseSearchString === '') { | ||
_this.setPreviewPosition(Infinity); | ||
} | ||
}; | ||
/** | ||
* This function sets a given history entry as a main input element content. | ||
* It's called when changing the preview of a 'current' history entry e.g. by ctrl+r call, or | ||
* when pressing up/down arrow. | ||
* @param historyPosition | ||
*/ | ||
_this.setPreviewPosition = function (historyPosition) { | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
_this.setState({ | ||
historyPosition: historyPosition, | ||
input: _this.props.history[historyPosition] || '', | ||
}); | ||
}; | ||
/** | ||
* Enables reverse search. | ||
* The side effect is that we focus o reverse search input. | ||
*/ | ||
_this.onReverseSearch = function () { | ||
// we enabled reverse search | ||
_this.setState({ | ||
reverseSearchString: '', | ||
}, function () { | ||
_this.reverseStringRef.focus(); | ||
}); | ||
}; | ||
/** | ||
* When reverse search is confirmed, we disable reverse search mode and keep the result. | ||
* @param e | ||
*/ | ||
_this.onReverseSearchSubmit = function (event) { | ||
event.preventDefault(); | ||
_this.disableReverseSearch(); | ||
}; | ||
/** | ||
* Main input change handler. | ||
* @param event | ||
*/ | ||
_this.onInputChange = function (event) { | ||
_this.setState({ | ||
input: event.target.value, | ||
}); | ||
}; | ||
/** | ||
* Helper function to determine whether reverse search is active or not. | ||
*/ | ||
_this.isReverseSearchOn = function () { return _this.state.reverseSearchString !== undefined; }; | ||
/** | ||
* Disables reverse search mode. | ||
* @param keepPreviewString - determines whether the result of a reverse search should be kept or not | ||
*/ | ||
_this.disableReverseSearch = function (keepPreviewString) { | ||
if (keepPreviewString === void 0) { keepPreviewString = true; } | ||
_this.setState({ | ||
reverseSearchString: undefined, | ||
}); | ||
if (!keepPreviewString) { | ||
_this.setState({ | ||
input: '', | ||
}); | ||
} | ||
setTimeout(function () { | ||
_this.inputRef.focus(); | ||
}); | ||
}; | ||
/** | ||
* onKeyDown implementation of a reverse search input. | ||
* @param event | ||
*/ | ||
_this.onReverseKeyDown = function (event) { | ||
if (event.which === 38 || event.which === 40) { // up or down | ||
_this.disableReverseSearch(); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 67 && event.ctrlKey) { // ctrl + c | ||
_this.disableReverseSearch(false); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 82 && event.ctrlKey) { // ctrl + r | ||
_this.nextReverseSearch(); | ||
event.preventDefault(); | ||
} | ||
}; | ||
/** | ||
* onKeyDown implementation of a main input. | ||
* @param event | ||
*/ | ||
_this.onKeyDown = function (event) { | ||
if (event.which === 38) { // key up | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
var currentPos = Math.min(_this.state.historyPosition, _this.props.history.length); | ||
var historyPosition = Math.max(0, currentPos - 1); | ||
_this.setPreviewPosition(historyPosition); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 40) { | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
var historyPosition = Math.min(_this.props.history.length, _this.state.historyPosition + 1); | ||
_this.setPreviewPosition(historyPosition); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 82 && event.ctrlKey) { // ctrl + r | ||
if (_this.props.history === undefined) { | ||
return; | ||
} | ||
_this.onReverseSearch(); | ||
event.preventDefault(); | ||
} | ||
else if (event.which === 67 && event.ctrlKey) { // ctrl + c | ||
_this.setState({ | ||
output: _this.state.output.concat([_this.getCurrentTextSnapshot()]), | ||
input: '', | ||
}); | ||
_this.scrollToBottom(); | ||
event.preventDefault(); | ||
} | ||
}; | ||
/** | ||
* Focuses console input. | ||
* Whenever an user clicks on a terminal, we want to focus an actual input where he/she can type. | ||
*/ | ||
_this.focusConsole = function () { | ||
@@ -443,15 +263,2 @@ if (_this.inputRef) { | ||
}; | ||
/** | ||
* Calls onAddHistoryItem property and sets historyPosition to a default value. | ||
* @param inputString | ||
*/ | ||
_this.addHistoryEntry = function (inputString) { | ||
var onAddHistoryItem = _this.props.onAddHistoryItem; | ||
if (typeof onAddHistoryItem === 'function') { | ||
onAddHistoryItem(inputString); | ||
} | ||
_this.setState({ | ||
historyPosition: Infinity, | ||
}); | ||
}; | ||
return _this; | ||
@@ -466,7 +273,2 @@ } | ||
} | ||
if (this.props.history !== undefined) { | ||
this.setState({ | ||
historyPosition: this.props.history.length, | ||
}); | ||
} | ||
}; | ||
@@ -488,6 +290,3 @@ ReactConsole.prototype.render = function () { | ||
"\u00A0"), | ||
React.createElement("input", { disabled: this.state.commandInProgress || this.isReverseSearchOn(), ref: function (ref) { return _this.inputRef = ref; }, autoFocus: autoFocus, value: this.state.input, onChange: this.onInputChange, onKeyDown: this.onKeyDown, autoComplete: 'off', spellCheck: false, autoCapitalize: 'false', name: "input", className: classnames([styles.input, inputClassName]), style: inputStyle }))), | ||
this.isReverseSearchOn() && React.createElement("form", { onSubmit: this.onReverseSearchSubmit }, | ||
"bck-i-search: ", | ||
React.createElement("input", { value: this.state.reverseSearchString, ref: function (ref) { return _this.reverseStringRef = ref; }, onKeyDown: this.onReverseKeyDown, className: classnames([styles.input, inputClassName]), onChange: this.onReverseStringInputChange })))); | ||
React.createElement("input", { disabled: this.state.commandInProgress, ref: function (ref) { return _this.inputRef = ref; }, autoFocus: autoFocus, value: this.state.input, onChange: this.onInputChange, autoComplete: 'off', spellCheck: false, autoCapitalize: 'false', name: "input", className: classnames([styles.input, inputClassName]), style: inputStyle }))))); | ||
}; | ||
@@ -494,0 +293,0 @@ ReactConsole.defaultProps = { |
{ | ||
"name": "@webscopeio/react-console", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "React component that emulates console behaviour", | ||
@@ -5,0 +5,0 @@ "author": "jvorcak", |
@@ -39,4 +39,2 @@ MIT-licensed console emulator in React. Documentation and more advanced features coming soon! | ||
| inputClassName | string | className for the input | | ||
| history | Array<string> | history array | | ||
| onAddHistoryItem | (entry: string) => void | callback called when a new history entry should be created | | ||
@@ -52,60 +50,41 @@ \*_are mandatory_ | ||
const App = () => { | ||
const [history, setHistory] = useState([ | ||
"echo hello world", | ||
"sleep 1000", | ||
"sleep 2000", | ||
"sleep 3000", | ||
"echo ola", | ||
"not found", | ||
]) | ||
return ( | ||
<div> | ||
<ReactConsole | ||
autoFocus | ||
welcomeMessage="Welcome" | ||
commands={{ | ||
history: { | ||
description: 'History', | ||
fn: () => new Promise(resolve => { | ||
resolve(`${history.join('\r\n')}`) | ||
}) | ||
}, | ||
echo: { | ||
description: 'Echo', | ||
fn: (...args) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(`${args.join(' ')}`) | ||
}, 2000) | ||
}) | ||
export default class App extends Component { | ||
render () { | ||
return ( | ||
<div> | ||
<ReactConsole | ||
autoFocus | ||
welcomeMessage="Welcome" | ||
commands={{ | ||
echo: { | ||
description: 'Echo', | ||
fn: (...args) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve(`${args.join(' ')}`) | ||
}, 2000) | ||
}) | ||
} | ||
}, | ||
test: { | ||
description: 'Test', | ||
fn: (...args) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve('Hello world \n\n hello \n') | ||
}, 2000) | ||
}) | ||
} | ||
} | ||
}, | ||
test: { | ||
description: 'Test', | ||
fn: (...args) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve('Hello world \n\n hello \n') | ||
}, 2000) | ||
}) | ||
} | ||
} | ||
}} | ||
/> | ||
</div> | ||
) | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
} | ||
export default App | ||
``` | ||
## History implementation | ||
You can provide your own history implementation by providing `onAddHistoryItem` and `history` properties. | ||
If you don't provide `history`, up/down arrows & reverse search won't work. | ||
## License | ||
IT © [jvorcak](https://github.com/jvorcak) | ||
MIT © [jvorcak](https://github.com/jvorcak) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
-100%89345
-31.48%584
-45.22%89
-19.09%