我们正在构建一个编辑器,并希望使用TinyMCE和React。我们有一些场景需要在事件发生时将基于模板的内容添加到编辑器中。我计划将模板设计为React组件。
那么,如何在TinyMCE编辑器中添加React组件呢?请提供相应的方法。
在上面的代码中,在 "
但是我该如何将组件添加到编辑器中呢?
那么,如何在TinyMCE编辑器中添加React组件呢?请提供相应的方法。
export class AppEditor extends React.Component<iEditorProps, iEditorState> {
innerEditor: React.RefObject<Editor>;
constructor(props: iEditorProps) {
super(props);
this.state = {
content: ''
};
this.innerEditor = React.createRef();
}
handleChange = (content) => {
this.setState({ content });
}
handleAddContent = (e) => {
this.setState(prevState => {
return { content: prevState.content + <TemplateComp>Added Content</TemplateComp> }
});
}
render() {
return (
<div>
<Editor ref={this.innerEditor} inline onEditorChange={this.handleChange} value={this.state.content} />
</div>);
}
}
在上面的代码中,在 "
handleAddContent
"处,我试图向编辑器添加 <TemplateComp>
,但它被渲染为 [object] [Object]。我明白字符串连接会导致这种情况。正在使用 TinyMCE 包 - https://github.com/tinymce/tinymce-react。但是我该如何将组件添加到编辑器中呢?