This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const decorator = new CompositeDecorator([ | |
| { | |
| strategy: findLinkEntities, | |
| component: Link | |
| } | |
| ]); | |
| this.state = { | |
| inlineToolbar: { show: false }, | |
| editorState: EditorState.createEmpty(decorator) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| handleKeyCommand(command) { | |
| const { editorState } = this.state; | |
| const newState = RichUtils.handleKeyCommand(editorState, command); | |
| if (newState) { | |
| this.onChange(newState); | |
| return true; | |
| } | |
| return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Editor | |
| editorState={editorState} | |
| onChange={this.onChange} | |
| handleKeyCommand={this.handleKeyCommand} | |
| customStyleMap={customStyleMap} | |
| ref="editor" | |
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const customStyleMap = { | |
| HIGHLIGHT: { | |
| backgroundColor: 'palegreen', | |
| }, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| onMouseDown={(e) => { | |
| e.preventDefault(); | |
| onToggle(type.style); | |
| }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <InlineToolbar | |
| editorState={editorState} | |
| onToggle={this.toggleInlineStyle} | |
| position={inlineToolbar.position} | |
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| toggleInlineStyle(inlineStyle) { | |
| this.onChange( | |
| RichUtils.toggleInlineStyle( | |
| this.state.editorState, | |
| inlineStyle | |
| ) | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| onChange(editorState) { | |
| if (!editorState.getSelection().isCollapsed()) { | |
| const selectionRange = getSelectionRange(); | |
| if (!selectionRange) { | |
| this.setState({ inlineToolbar: { show: false } }); | |
| return; | |
| } | |
| const selectionCoords = getSelectionCoords(selectionRange); | |
| this.setState({ | |
| inlineToolbar: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const getSelectionCoords = (selectionRange) => { | |
| const editorBounds = document.getElementById('editor-container').getBoundingClientRect(); | |
| const rangeBounds = selectionRange.getBoundingClientRect(); | |
| const rangeWidth = rangeBounds.right - rangeBounds.left; | |
| // 107px is width of inline toolbar | |
| const offsetLeft = (rangeBounds.left - editorBounds.left) + (rangeWidth / 2) - (107 / 2); | |
| // 42px is height of inline toolbar | |
| const offsetTop = rangeBounds.top - editorBounds.top - 42; |