Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not get editor references when Multiple tinymce on the same page #46

Closed
maneeshbhunwal123 opened this issue Dec 30, 2018 · 1 comment

Comments

@maneeshbhunwal123
Copy link

I need to get editor reference since i am trying to implement wordpress like image insert feature, where image insert code will be owned by me and I will use the command
editorRef.insertContent('<a href="${image_url}">document</a>');

But when I have multiple instances of tinymce i am not able to get editorRef correctly.
Here is my code

import React from 'react';
import { Editor } from '@tinymce/tinymce-react';

class CustomWisivigComponent extends React.Component {
    constructor(props) {
        super(props);
        this.putWisivigRef = this.putWisivigRef.bind(this);
        this.returnCmp = <Editor
            apiKey={"myKey"}
            init={{
                setup: (editor) => this.putWisivigRef(editor),
                height: 700,
                plugins: 'image table link code',
                selector: "textarea",  // change this value according to your HTML
                default_link_target: "_blank"

            }}
            initialValue={this.props.value}
            onEditorChange={(event) => {
                this.props.onChange(event)
            }}
        />
    }

    putWisivigRef(ref) {
        console.log("setting editorRef to "+ref.id + " in component with props="+JSON.stringify(this.props))
        this.setState({editorRef: ref})
    }

    onImageInsert(propd) {
        console.log(this.state.editorRef)
    }

    render() {

        return (
            <div>
                {this.returnCmp}
            </div>
        );

    }
}

export default CustomWisivigComponent;

And I am trying to render it like this

<CustomWisivigComponent id="1"/>
  <CustomWisivigComponent id="2"/>

The problem is when i execute the code putWisivigRef is called with two references but the context of this is missed and always the putWisivigRef of first component is called. And hence when I try to insert something in the first component, it gets inserted into second (since first one's reference gets overridden) and when I try to insert in 2nd it results into null pointer, since it was never called. I tried passing this from the setup: (editor) => this.putWisivigRef(editor) as well but that too did not work.

when we render the above form it logs

screen shot 2018-12-30 at 3 09 48 pm

but ideally it should have been

setting editorRef to tiny-react_10038262721546162755458 in component with props={"id":"1"}
setting editorRef to tiny-react_19726510231546162755460 in component with props={"id":"2"}

@fyrkant
Copy link
Contributor

fyrkant commented Jan 7, 2019

Duplicate of #43

@fyrkant fyrkant closed this as completed Jan 7, 2019
@fyrkant fyrkant marked this as a duplicate of #43 Jan 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants