简体   繁体   English

将React组件添加到TinyMCE编辑器内容

[英]Add React Component to TinyMCE Editor content

We are building an editor and we would like to use TimyMCE and React for it. 我们正在构建一个编辑器,我们想使用TimyMCE和React。 We have scenarios where on an event we have to add template based content to the Editor. 在某些情况下,我们必须将基于模板的内容添加到编辑器中。 I am planning to design the templates as React component. 我打算将模板设计为React组件。

So, with TinyMCE and React, how do I add react component to the TinyMCE editor. 因此,使用TinyMCE和React,如何将react组件添加到TinyMCE编辑器中。

    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>);
    }
}

In the above code at " handleAddContent " I am trying to add <TemplateComp> to the editor, but it's get's rendered as [object] [Object]. 在上面的代码中,“ handleAddContent ”中,我试图将<TemplateComp>添加到编辑器中,但是将其呈现为[object] [Object]。 I understand string concatenation does that. 我知道字符串串联可以做到这一点。 TinyMCE package being used - https://github.com/tinymce/tinymce-react . 使用的TinyMCE软件包-https: //github.com/tinymce/tinymce-react

But how do I add component to editor? 但是如何将组件添加到编辑器?

Concatenating string with JSX expression (which in fact is javascript object tree of virtual nodes) in 将字符串与JSX表达式(实际上是虚拟节点的javascript对象树)串联在一起

prevState.content + <TemplateComp>Added Content</TemplateComp>

results in that you have described. 您所描述的结果。 You have to convert virtual DOM nodes to HTML, eg in following way: 您必须将虚拟DOM节点转换为HTML,例如,通过以下方式:

prevState.content + ReactDOMServer.renderToStaticMarkup(<TemplateComp>Added Content</TemplateComp>)

this will work, albeit it is one way route how to statically render React component to Tiny MCE editor. 这是可行的,尽管这是将静态组件静态呈现到Tiny MCE编辑器的一种方法。 If you really want to place React component with its lifecycle to rich text editor consider using Draft.js or a component based on it instead of Tiny MCE. 如果您真的想将React组件及其生命周期放置到RTF编辑器中,请考虑使用Draft.js基于它的组件而不是Tiny MCE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM