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

127 lines
3.2 KiB
TypeScript
Raw Normal View History

import { BaseInput, Box, Row } from '@tlon/indigo-react';
2020-05-01 05:54:12 +03:00
import React, { Component } from 'react';
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) {
const 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'>
2020-11-20 01:59:16 +03:00
<Box flexShrink='0' width='100%' color='black' fontSize='0'>
<BaseInput
autoFocus
autoCorrect="off"
autoCapitalize="off"
2020-11-20 01:59:16 +03:00
color='black'
minHeight='0'
display='inline-block'
width='100%'
spellCheck="false"
tabindex="0"
wrap="off"
2020-11-20 01:59:16 +03:00
className="mono"
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;