UNPKG

similiquedicta

Version:
56 lines (50 loc) 1.75 kB
import React, { Component } from 'react'; import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor'; import createInlineToolbarPlugin from '@draft-js-plugins/inline-toolbar'; import createLinkPlugin from '@draft-js-plugins/anchor'; import { ItalicButton, BoldButton, UnderlineButton, } from '@draft-js-plugins/buttons'; import editorStyles from './editorStyles.module.css'; const linkPlugin = createLinkPlugin(); const inlineToolbarPlugin = createInlineToolbarPlugin(); const { InlineToolbar } = inlineToolbarPlugin; const plugins = [inlineToolbarPlugin, linkPlugin]; const text = 'Try selecting a part of this text and click on the link button in the toolbar that appears …'; export default class SimpleLinkPluginEditor extends Component { state = { editorState: createEditorStateWithText(text), }; onChange = (editorState) => this.setState({ editorState }); focus = () => this.editor.focus(); render() { return ( <div className={editorStyles.editor} onClick={this.focus}> <Editor editorState={this.state.editorState} onChange={this.onChange} plugins={plugins} ref={(element) => { this.editor = element; }} /> <InlineToolbar> { // may be use React.Fragment instead of div to improve perfomance after React 16 (externalProps) => ( <div> <BoldButton {...externalProps} /> <ItalicButton {...externalProps} /> <UnderlineButton {...externalProps} /> <linkPlugin.LinkButton {...externalProps} /> </div> ) } </InlineToolbar> </div> ); } }