2020-05-01 05:54:12 +03:00
|
|
|
import React, { Component } from 'react';
|
2020-11-10 02:45:08 +03:00
|
|
|
import { Row, Box, BaseInput } from '@tlon/indigo-react';
|
2020-08-15 01:04:38 +03:00
|
|
|
import { cite } from '~/logic/lib/util';
|
|
|
|
import { Spinner } from '~/views/components/Spinner';
|
2020-05-01 05:54:12 +03:00
|
|
|
|
|
|
|
export class Input extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-11-09 22:27:41 +03:00
|
|
|
this.state = {};
|
2020-05-01 05:54:12 +03:00
|
|
|
this.keyPress = this.keyPress.bind(this);
|
2020-11-07 00:32:13 +03:00
|
|
|
this.paste = this.paste.bind(this);
|
2020-10-24 02:25:44 +03:00
|
|
|
this.click = this.click.bind(this);
|
2020-05-01 05:54:12 +03:00
|
|
|
this.inputRef = React.createRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2020-08-18 02:15:35 +03:00
|
|
|
if (
|
|
|
|
!document.activeElement == document.body
|
|
|
|
|| document.activeElement == this.inputRef.current
|
|
|
|
) {
|
|
|
|
this.inputRef.current.focus();
|
2020-05-01 05:54:12 +03:00
|
|
|
this.inputRef.current.setSelectionRange(this.props.cursor, this.props.cursor);
|
|
|
|
}
|
2020-08-18 02:15:35 +03:00
|
|
|
}
|
2020-05-01 05:54:12 +03:00
|
|
|
|
2020-08-18 02:15:35 +03:00
|
|
|
keyPress(e) {
|
2020-10-24 02:25:44 +03:00
|
|
|
let key = e.key;
|
2020-11-07 00:32:13 +03:00
|
|
|
// let paste and leap events pass
|
2020-05-01 05:54:12 +03:00
|
|
|
if ((e.getModifierState('Control') || event.getModifierState('Meta'))
|
2020-11-07 00:32:13 +03:00
|
|
|
&& (e.key === 'v' || e.key === '/')) {
|
2020-05-01 05:54:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-24 02:25:44 +03:00
|
|
|
let belt = null;
|
|
|
|
if (key === 'ArrowLeft') belt = {aro: 'l'};
|
|
|
|
else if (key === 'ArrowRight') belt = {aro: 'r'};
|
|
|
|
else if (key === 'ArrowUp') belt = {aro: 'u'};
|
|
|
|
else if (key === 'ArrowDown') belt = {aro: 'd'};
|
|
|
|
else if (key === 'Backspace') belt = {bac: null};
|
|
|
|
else if (key === 'Delete') belt = {del: null};
|
|
|
|
else if (key === 'Tab') belt = {ctl: 'i'};
|
|
|
|
else if (key === 'Enter') belt = {ret: null};
|
2020-11-10 22:42:28 +03:00
|
|
|
else if (key.length === 1) belt = {txt: [key]};
|
2020-10-24 02:25:44 +03:00
|
|
|
else belt = null;
|
2020-05-01 05:54:12 +03:00
|
|
|
|
2020-10-24 02:25:44 +03:00
|
|
|
if (belt && e.getModifierState('Control')) {
|
2020-11-10 22:42:28 +03:00
|
|
|
if (belt.txt !== undefined) belt = {ctl: belt.txt[0]};
|
2020-10-24 02:25:44 +03:00
|
|
|
} else
|
|
|
|
if (belt &&
|
|
|
|
(e.getModifierState('Meta') || e.getModifierState('Alt'))) {
|
|
|
|
if (belt.bac !== undefined) belt = {met: 'bac'};
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-24 02:25:44 +03:00
|
|
|
if (belt !== null) {
|
|
|
|
this.props.api.belt(belt);
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-24 02:25:44 +03:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
paste(e) {
|
|
|
|
const clipboardData = e.clipboardData || window.clipboardData;
|
2020-11-10 22:42:28 +03:00
|
|
|
const clipboardText = clipboardData.getData('Text');
|
|
|
|
this.props.api.belt({ txt: [...clipboardText] });
|
2020-10-24 02:25:44 +03:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
click(e) {
|
|
|
|
// prevent desynced cursor movement
|
|
|
|
e.preventDefault();
|
|
|
|
e.target.setSelectionRange(this.props.cursor, this.props.cursor);
|
2020-05-01 05:54:12 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 02:15:35 +03:00
|
|
|
render() {
|
2020-10-24 02:25:44 +03:00
|
|
|
const line = this.props.line;
|
|
|
|
let prompt = 'connecting...';
|
|
|
|
if (line) {
|
|
|
|
if (line.lin) {
|
2020-11-10 22:42:28 +03:00
|
|
|
prompt = line.lin.join('');
|
2020-10-24 02:25:44 +03:00
|
|
|
}
|
|
|
|
//TODO render prompt style
|
|
|
|
else if (line.klr) {
|
2020-11-10 22:42:28 +03:00
|
|
|
prompt = line.klr.reduce((l, p) => (l + p.text.join('')), '');
|
2020-10-24 02:25:44 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 02:15:35 +03:00
|
|
|
return (
|
2020-11-10 02:45:08 +03:00
|
|
|
<Row flexGrow='1' position='relative'>
|
2020-11-10 21:35:42 +03:00
|
|
|
<Box flexShrink='0' className="w-100">
|
2020-11-10 02:45:08 +03:00
|
|
|
<BaseInput
|
|
|
|
autoFocus
|
|
|
|
autoCorrect="off"
|
|
|
|
autoCapitalize="off"
|
|
|
|
spellCheck="false"
|
|
|
|
tabindex="0"
|
|
|
|
wrap="off"
|
|
|
|
className="mono ml1 flex-auto dib w-100"
|
|
|
|
id="term"
|
|
|
|
cursor={this.props.cursor}
|
|
|
|
onKeyDown={this.keyPress}
|
|
|
|
onClick={this.click}
|
|
|
|
onPaste={this.paste}
|
|
|
|
ref={this.inputRef}
|
|
|
|
defaultValue="connecting..."
|
|
|
|
value={prompt}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Row>
|
2020-05-01 05:54:12 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Input;
|