Created
March 17, 2019 07:19
-
-
Save karanjariwala/ec5e56ee79afebcde9a833c3af3d29be to your computer and use it in GitHub Desktop.
Document Editor using Froala
This file contains 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 EditorComponent = forwardRef(({ defaultContent }, ref) => { | |
// State | |
const [state, setState] = useReducer( | |
(prevState, nextState) => ({ | |
...prevState, | |
...nextState | |
}), | |
{ | |
content: defaultContent || "" | |
} | |
); | |
const editor = useRef({}); | |
const initializeEditor = (e, editorRef) => { | |
editor.current = editorRef; | |
window.editor = editorRef; | |
}; | |
useImperativeHandle(ref, () => ({ | |
getContent | |
})); | |
const getContent = () => { | |
return editor.current.html.get(); | |
}; | |
const editorConfig = { | |
placeholder: "Edit Me", | |
documentReady: true, | |
events: { | |
"froalaEditor.initialized": initializeEditor | |
} | |
}; | |
const onContentChange = model => { | |
this.setState({ | |
content: model | |
}); | |
}; | |
return ( | |
<div className="main-editor-container"> | |
<FroalaEditor | |
config={editorConfig} | |
tag="textarea" | |
model={state.content} | |
onModelChange={onContentChange} | |
/> | |
</div> | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful snippet! One correction:
this.setState
on line 37 should just besetState
.