Composer: fixes #419 jumpy cursor
This commit is contained in:
parent
f71a1e4ce1
commit
bd12226a84
2 changed files with 24 additions and 5 deletions
|
@ -159,6 +159,16 @@ export default class AutosuggestTextarea extends ImmutablePureComponent {
|
||||||
this.textarea.focus();
|
this.textarea.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shouldComponentUpdate(nextProps, nextState) {
|
||||||
|
// Skip updating when lastToken changes so the cursor doesn't jump around
|
||||||
|
// due to re-rendering unnecessarily
|
||||||
|
if (this.state.lastToken !== nextState.lastToken) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return super.shouldComponentUpdate(nextProps, nextState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps, prevState) {
|
componentDidUpdate(prevProps, prevState) {
|
||||||
const { suggestions } = this.props;
|
const { suggestions } = this.props;
|
||||||
if (suggestions !== prevProps.suggestions && suggestions.size > 0 && prevState.suggestionsHidden && prevState.focused) {
|
if (suggestions !== prevProps.suggestions && suggestions.size > 0 && prevState.suggestionsHidden && prevState.focused) {
|
||||||
|
|
|
@ -160,11 +160,6 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
this.props.onChangeSpoilerText(e.target.value);
|
this.props.onChangeSpoilerText(e.target.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
doFocus = () => {
|
|
||||||
if (!this.autosuggestTextarea) return;
|
|
||||||
this.autosuggestTextarea.textarea.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
|
@ -219,8 +214,22 @@ class ComposeForm extends ImmutablePureComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
maybeUpdateCursor = prevProps => {
|
||||||
|
const shouldUpdate = [
|
||||||
|
// Autosuggest has been updated and
|
||||||
|
// the cursor position explicitly set
|
||||||
|
this.props.focusDate !== prevProps.focusDate,
|
||||||
|
typeof this.props.caretPosition === 'number',
|
||||||
|
].every(Boolean);
|
||||||
|
|
||||||
|
if (shouldUpdate) {
|
||||||
|
this.setCursor(this.props.caretPosition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps) {
|
componentDidUpdate(prevProps) {
|
||||||
this.maybeUpdateFocus(prevProps);
|
this.maybeUpdateFocus(prevProps);
|
||||||
|
this.maybeUpdateCursor(prevProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
Loading…
Reference in a new issue