Composer: Always place cursor at the end of text
This commit is contained in:
parent
5633bf4317
commit
188067ac48
|
@ -158,56 +158,25 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
this.props.onChangeSpoilerText(e.target.value);
|
this.props.onChangeSpoilerText(e.target.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
makeCursorData = opts => {
|
doFocus = () => {
|
||||||
const { didFocus, isReply, didSuggestion } = opts;
|
if (!this.autosuggestTextarea) return;
|
||||||
|
this.autosuggestTextarea.textarea.focus();
|
||||||
// Mentions
|
|
||||||
if (didFocus && isReply) return [
|
|
||||||
this.props.text.search(/\s/) + 1,
|
|
||||||
this.props.text.length,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Autosuggestions
|
|
||||||
if (didFocus && didSuggestion) return [
|
|
||||||
this.props.caretPosition,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Delete & redraft
|
|
||||||
if (didFocus) return [
|
|
||||||
this.props.text.length,
|
|
||||||
];
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setCursor = (start, end = start) => {
|
setCursor = (start, end = start) => {
|
||||||
if (!this.autosuggestTextarea) return;
|
if (!this.autosuggestTextarea) return;
|
||||||
this.autosuggestTextarea.textarea.setSelectionRange(start, end);
|
this.autosuggestTextarea.textarea.setSelectionRange(start, end);
|
||||||
this.autosuggestTextarea.textarea.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
updateCursor = prevProps => {
|
|
||||||
const cursorData = this.makeCursorData({
|
|
||||||
didFocus: this.props.focusDate !== prevProps.focusDate,
|
|
||||||
isReply: this.props.preselectDate !== prevProps.preselectDate,
|
|
||||||
didSuggestion: typeof this.props.caretPosition === 'number',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (cursorData) this.setCursor(...cursorData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
document.addEventListener('click', this.handleClick, true);
|
document.addEventListener('click', this.handleClick, true);
|
||||||
|
this.setCursor(this.props.text.length); // Set cursor at end
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
document.removeEventListener('click', this.handleClick, true);
|
document.removeEventListener('click', this.handleClick, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps) {
|
|
||||||
this.updateCursor(prevProps);
|
|
||||||
}
|
|
||||||
|
|
||||||
setAutosuggestTextarea = (c) => {
|
setAutosuggestTextarea = (c) => {
|
||||||
this.autosuggestTextarea = c;
|
this.autosuggestTextarea = c;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue