shrub/pkg/interface/src/views/apps/term/components/input.js

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-05-01 05:54:12 +03:00
import React, { Component } from 'react';
import { Row, Box, BaseInput } from '@tlon/indigo-react';
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);
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);
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) {
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;
}
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};
else if (key.length === 1) belt = {txt: [key]};
else belt = null;
2020-05-01 05:54:12 +03:00
if (belt && e.getModifierState('Control')) {
if (belt.txt !== undefined) belt = {ctl: belt.txt[0]};
} else
if (belt &&
(e.getModifierState('Meta') || e.getModifierState('Alt'))) {
if (belt.bac !== undefined) belt = {met: 'bac'};
2020-05-01 05:54:12 +03:00
}
if (belt !== null) {
this.props.api.belt(belt);
2020-05-01 05:54:12 +03:00
}
e.preventDefault();
}
paste(e) {
const clipboardData = e.clipboardData || window.clipboardData;
const clipboardText = clipboardData.getData('Text');
this.props.api.belt({ txt: [...clipboardText] });
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() {
const line = this.props.line;
let prompt = 'connecting...';
if (line) {
if (line.lin) {
prompt = line.lin.join('');
}
//TODO render prompt style
else if (line.klr) {
prompt = line.klr.reduce((l, p) => (l + p.text.join('')), '');
}
}
2020-08-18 02:15:35 +03:00
return (
<Row flexGrow='1' position='relative'>
<Box flexShrink='0' className="w-100">
<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;