I was able to get it to work by using a ref
.
const MyForm = () => {
const [editorContent, setEditorContent] = useState("");
const editor = useRef(null);
const onButtonClick = () => {
setEditorContent(editor.current.props.value);
};
const handleEditorChange = (content) => {
setEditorContent(content);
};
return (
<>
<Editor
ref={editor}
apiKey={TINYMCEKEY}
value={editorContent}
init={{
height: 600,
menubar: false,
branding: false,
plugins: ["print"],
setup: function (editor) {
editor.ui.registry.addButton("testBTN", {
text: "My Button",
onAction: onButtonClick
});
},
toolbar1: "print | testBTN"
}}
onEditorChange={handleEditorChange}
/>
</>
);
};
So the idea is to pass a ref
to the Editor
and to retrieve the current content text value using this ref. You can then save it in state or do whatever you want with it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…