const ENTITY_TYPE = 'MY_CUSTOM_ENTITY'; const contentState = DraftJS.ContentState.createFromText('Hello World'); const contentStateWithEntity = contentState.createEntity( ENTITY_TYPE, 'MUTABLE', { data: 'my custom data' } ); const entityKey = contentStateWithEntity.getLastCreatedEntityKey(); const contentStateWithText = DraftJS.Modifier.insertText( contentStateWithEntity, contentStateWithEntity.getSelection(), 'Hello World', null, entityKey ); const editorState = DraftJS.EditorState.createWithContent(contentStateWithText);
const ENTITY_TYPE = 'LINK'; const contentState = DraftJS.ContentState.createFromText('Click here to go to Google'); const contentStateWithEntity = contentState.createEntity( ENTITY_TYPE, 'MUTABLE', { url: 'https://www.google.com' } ); const entityKey = contentStateWithEntity.getLastCreatedEntityKey(); const selectionState = DraftJS.SelectionState.createEmpty(contentStateWithEntity.getFirstBlock().getKey()); const contentStateWithText = DraftJS.Modifier.replaceText( contentStateWithEntity, selectionState, 'Google', null, entityKey ); const editorState = DraftJS.EditorState.createWithContent(contentStateWithText);This example shows how to create a Link entity using the ENTITY_TYPE of "LINK". We add the link URL to the entity and apply it to the text "Google" in the editorState. In conclusion, JavaScript draft-js Entity is a package library that provides a way for developers to create entities in their Rich Text Editor. With entities, you can add custom data or functionality to the text in the editor. It is a powerful and flexible tool that can be used in a wide variety of applications.