import { EditorState, RichUtils } from 'draft-js'; const setLink = (editorState: EditorState, url: string): EditorState => { const containsLink = RichUtils.currentBlockContainsLink(editorState); if (containsLink) return editorState; const contentState = editorState.getCurrentContent(); const contentStateWithLink = contentState.createEntity('LINK', 'MUTABLE', { url, }); const entityKey = contentStateWithLink.getLastCreatedEntityKey(); const nextEditorState = EditorState.set(editorState, { currentContent: contentStateWithLink, }); return RichUtils.toggleLink( nextEditorState, nextEditorState.getSelection(), entityKey ); }; export default setLink;