Fix composition area link previews with multiline text

This commit is contained in:
Ken Powers 2019-08-22 16:42:03 -04:00 committed by Scott Nonnenberg
parent 6cd3165c1b
commit eec0fce62a

View file

@ -205,23 +205,23 @@ export const CompositionInput = ({
const updateExternalStateListeners = React.useCallback( const updateExternalStateListeners = React.useCallback(
(newState: EditorState) => { (newState: EditorState) => {
const plainText = newState.getCurrentContent().getPlainText(); const plainText = newState.getCurrentContent().getPlainText();
const currentBlockKey = newState.getSelection().getStartKey(); const cursorBlockKey = newState.getSelection().getStartKey();
const currentBlockIndex = editorStateRef.current const cursorBlockIndex = editorStateRef.current
.getCurrentContent() .getCurrentContent()
.getBlockMap() .getBlockMap()
.keySeq() .keySeq()
.findIndex(key => key === currentBlockKey); .findIndex(key => key === cursorBlockKey);
const caretLocation = newState const caretLocation = newState
.getCurrentContent() .getCurrentContent()
.getBlockMap() .getBlockMap()
.valueSeq() .valueSeq()
.toArray() .toArray()
.reduce((sum: number, block: ContentBlock, index: number) => { .reduce((sum: number, block: ContentBlock, currentIndex: number) => {
if (currentBlockIndex < index) { if (currentIndex < cursorBlockIndex) {
return sum + block.getText().length + 1; // +1 for newline return sum + block.getText().length + 1; // +1 for newline
} }
if (currentBlockIndex === index) { if (currentIndex === cursorBlockIndex) {
return sum + newState.getSelection().getStartOffset(); return sum + newState.getSelection().getStartOffset();
} }
@ -235,6 +235,7 @@ export const CompositionInput = ({
onDirtyChange(isDirty); onDirtyChange(isDirty);
} }
} }
if (onEditorStateChange) { if (onEditorStateChange) {
onEditorStateChange(plainText, caretLocation); onEditorStateChange(plainText, caretLocation);
} }