/* eslint-disable react/no-multi-comp */ import React, { useRef, useState, useEffect, ReactElement } from 'react'; import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor'; import createInlineToolbarPlugin, { Separator, } from '@draft-js-plugins/inline-toolbar'; import { ItalicButton, BoldButton, UnderlineButton, CodeButton, HeadlineOneButton, HeadlineTwoButton, HeadlineThreeButton, UnorderedListButton, OrderedListButton, BlockquoteButton, CodeBlockButton, SubButton, SupButton, } from '@draft-js-plugins/buttons'; import editorStyles from './editorStyles.css'; const HeadlinesPicker = (props): ReactElement => { const onWindowClick = (): void => // Call `onOverrideContent` again with `undefined` // so the toolbar can show its regular content again. props.onOverrideContent(undefined); useEffect(() => { const timeout = setTimeout(() => { window.addEventListener('click', onWindowClick); }); return () => { if (timeout) { clearTimeout(timeout); } window.removeEventListener('click', onWindowClick); }; }); const buttons = [HeadlineOneButton, HeadlineTwoButton, HeadlineThreeButton]; return (
{buttons.map(( Button, i // eslint-disable-next-line ) => ( // eslint-disable-next-line react/no-array-index-key
); }; const HeadlinesButton = ({ onOverrideContent }): ReactElement => { // When using a click event inside overridden content, mouse down // events needs to be prevented so the focus stays in the editor // and the toolbar remains visible onMouseDown = (event) => event.preventDefault() const onMouseDown = (event): void => event.preventDefault(); const onClick = (): void => // A button can call `onOverrideContent` to replace the content // of the toolbar. This can be useful for displaying sub // menus or requesting additional information from the user. onOverrideContent(HeadlinesPicker); return (
); }; const inlineToolbarPlugin = createInlineToolbarPlugin(); const { InlineToolbar } = inlineToolbarPlugin; const plugins = [inlineToolbarPlugin]; const text = 'In this editor a toolbar shows up once you select part of the text …'; const CustomInlineToolbarEditor = (): ReactElement => { const [editorState, setEditorState] = useState( createEditorStateWithText(text) ); const editor = useRef(); const onChange = (value): void => { setEditorState(value); }; const focus = (): void => { editor.current.focus(); }; return (
{ editor.current = element; }} /> { // may be use React.Fragment instead of div to improve perfomance after React 16 (externalProps) => (
) }
); }; export default CustomInlineToolbarEditor;