import React from 'react' import { Editor, EditorState, convertToRaw, convertFromRaw, RawDraftContentState, ContentState } from 'draft-js' import 'draft-js/dist/Draft.css' import { usePageContext } from 'src/Page/Page.context' import { editable } from '../Editable' export const EditableText = editable< RawDraftContentState, React.DetailedHTMLProps, HTMLDivElement> >(({ value, onChange, ...props }) => { const pageContext = usePageContext() const getInitialState = () => { return value ? { editorState: EditorState.createWithContent(convertFromRaw(value)) } : { editorState: EditorState.createEmpty() } } const [editorState, setEditorState] = React.useState(getInitialState()) return (
{ onChange(convertToRaw(editorState.getCurrentContent())) setEditorState({ editorState }) }} />
) }) export const editableText = (defaultText = 'lorem ipsum') => convertToRaw(ContentState.createFromText(defaultText))