-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEditor.js
106 lines (96 loc) · 2.65 KB
/
Editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { debounce } from 'debounce';
import React, { Component } from 'react';
import FormControl from '@material-ui/core/FormControl';
import InputBase from '@material-ui/core/InputBase';
import { withStyles } from '@material-ui/core/styles';
const styles = theme => ({
textField: {
padding: theme.spacing.unit * 2,
display: 'flex',
flex: 1,
flexDirection: 'column'
},
inputBase: {
overflow: 'auto',
flex: 1,
'& > div:first-child': {
height: '100%'
}
},
input: {
//TODO(elmasse,dk) hmmm, arrgghh, eeehmmm... shhhh
height: '100% !important'
}
});
class Editor extends Component {
constructor(props) {
super(props);
this.state = {
value: props.text || '',
selectionStart: -1,
isLocalUpdate: false
};
}
render() {
const { classes, text, isAuthor } = this.props;
const disabled = !text && !isAuthor;
return (
<FormControl className={classes.textField}>
<InputBase
inputRef={input => (this.taRef = input)}
id="editor"
inputProps={{ className: classes.input }}
classes={{ root: classes.inputBase }}
multiline
placeholder={disabled ? 'Loading peer data...' : ''}
disabled={disabled}
onChange={this.onChange}
value={text}
autoComplete={'off'}
autoFocus={true}
/>
</FormControl>
);
}
setSelectionRange = (input, selectionStart, selectionEnd) => {
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
} else if (input.createTextRange) {
const range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
};
setCaretToPos = (input, pos) => {
this.setSelectionRange(input, pos, pos);
};
debouncedPeerValue = debounce(({ text }) => {
this.props.updatePeerValue({ text });
}, 200);
onChange = e => {
const { value, selectionStart } = e.target;
this.setState(
{
value,
selectionStart
},
() => {
if (this.state.selectionStart !== -1) {
this.setCaretToPos(this.taRef, this.state.selectionStart);
}
}
);
this.props.updatePeerValue({ text: value });
// Note(dk): a deboung will be better to not overload network
// but is making things difficult with the UX.
// Im going to revisit this strategy later, it requires a big refactor.
// this.debouncedPeerValue({ text: value });
};
}
/**
* Export.
*/
export default withStyles(styles)(Editor);