Skip to content

Instantly share code, notes, and snippets.

@ddelrio1986
Created February 8, 2018 14:31
Show Gist options
  • Save ddelrio1986/9491f5616d1cb0304bacd3922919f156 to your computer and use it in GitHub Desktop.
Save ddelrio1986/9491f5616d1cb0304bacd3922919f156 to your computer and use it in GitHub Desktop.
Extend entities in DraftJS when typing next to them.
/*
DraftJS Slack - #general
Jimj 02/08/2018 [8:13 AM]
@ddelrio Simplest solution I found for continuing entities is, in beforeInputHanded :
*/
const prev_char_space = this.getCharFromSelection() === ' '
const new_char_space = char === ' '
const prev_ent = this.getEntityAtSelection()
if (prev_ent && (!prev_char_space || !new_char_space)) {
const contentStateWithEntity = Modifier.insertText(
editorState.getCurrentContent(),
selection, char, null, prev_ent );
const newEditorState: Draft.EditorState = Draft.EditorState.push(
editorState, contentStateWithEntity, "insert-characters");
this.setState({ editorState: newEditorState });
return 'handled'
}
//out
if ( prev_ent && prev_char_space && new_char_space) {
let contentStateWithEntity = Modifier.replaceText( editorState.getCurrentContent(),
selection.merge({ anchorOffset: currentOffset - 1, focusOffset: currentOffset }),
' ', null, null
);
let newEditorState: Draft.EditorState = Draft.EditorState.push(
editorState, contentStateWithEntity, "insert-characters")
this.setState({ editorState: newEditorState });
return 'handled'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment