Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created April 19, 2021 02:58
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 willmendesneto/d6f3873dc45bfe9a1f0640389e5bdd40 to your computer and use it in GitHub Desktop.
Save willmendesneto/d6f3873dc45bfe9a1f0640389e5bdd40 to your computer and use it in GitHub Desktop.
TinyMCE Editor configuration
// This file lives in `editor/editor.js`
import React, { useEffect, useState } from "react";
import { Editor as TinyMCEEditor } from "@tinymce/tinymce-react";
// Default Editor configuration to be mixed with the ones passed
// from the consumers
const defaults = {
init: {
width: "100%",
height: "300",
menubar: false,
branding: false,
plugins: ["link image", "table paste"],
toolbar: "undo redo | bold italic | alignleft aligncenter alignright"
}
};
export const Editor = ({ onEditorChange, value, init, id }) => {
const [configHasChanged, setRerenderEditor] = useState(false);
const [internalInit, setInternalInit] = useState(init);
// Unfortunately, the easiest way to make sure the editor will be updated and respect React lifecycle
// is by forcing a remount in case of configuration changes.
// Since this won't happen that often, it shouldn't be a problem in your app
const forceEditorRerender = () => {
setRerenderEditor(true);
requestAnimationFrame(() => setRerenderEditor(false));
};
useEffect(() => {
if (JSON.stringify(init) !== JSON.stringify(internalInit)) {
setInternalInit(init);
forceEditorRerender();
}
}, [init]);
return configHasChanged ? null : (
<TinyMCEEditor
id={id}
init={{
...defaults.init,
...internalInit,
id,
}}
value={value}
onEditorChange={onEditorChange}
/>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment