Skip to content

Instantly share code, notes, and snippets.

@karanjariwala
Created March 17, 2019 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karanjariwala/ec5e56ee79afebcde9a833c3af3d29be to your computer and use it in GitHub Desktop.
Save karanjariwala/ec5e56ee79afebcde9a833c3af3d29be to your computer and use it in GitHub Desktop.
Document Editor using Froala
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>
);
});
@stormfar
Copy link

Useful snippet! One correction: this.setState on line 37 should just be setState.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment