Skip to content

Instantly share code, notes, and snippets.

@levvsha
Last active July 1, 2018 07:28
Show Gist options
  • Select an option

  • Save levvsha/e1d93eff1bc17ee1c20e7584f70355c6 to your computer and use it in GitHub Desktop.

Select an option

Save levvsha/e1d93eff1bc17ee1c20e7584f70355c6 to your computer and use it in GitHub Desktop.
setLink() {
// getting url from prompt dialogue
const urlValue = prompt('Paste URL', '');
// getting current editor state
const { editorState } = this.state;
// getting current contentState
const contentState = editorState.getCurrentContent();
// creating Entity
const contentStateWithEntity = contentState.createEntity(
'LINK',
'SEGMENTED',
{ url: urlValue }
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
// updating currentContent property in editorState
const newEditorState = EditorState.set(editorState, {currentContent: contentStateWithEntity});
// generating and saving new editor state
this.setState({
editorState: RichUtils.toggleLink(
newEditorState,
newEditorState.getSelection(),
entityKey
)
}, () => {
setTimeout(() => this.focus(), 0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment