CK Editor div tags are being replaced with p tags (React JS)

Viewed 561

I am using CK editor and passing the initial data to editor as shown below

import React from 'react';
import CKEditor from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';

classEditor extends React.Component{
constructor(props){
super(props)
this.state={
editorState:'<div class="main"><div class="child-1"><div>Iam inside </div></div><div class="child-2">Child 2</div></div>'
}
}
render(){
return           <CKEditor
                      editor={ClassicEditor}
                      data={this.state.editorState}
                      onInit={editor => {
                        // You can store the "editor" and use when it is needed.
                        console.log('Editor is ready to use!', editor);
                      }}
                      config={{
                        allowedContent:true,
                        extraAllowedContent:true
                      }}
                      onChange={(event, editor) => {
                        const data = editor.getData();
                        this.handleEditorChange(data);
                        console.log(data);
                      }}
                    />
}
}

when the OnChange handler is called it is changing the initially passed html to something like this

<p>Iam inside</p><p>Child 2</p>

How Can I stop CK Editor replacing div tags with p tags and allow customStyles and classNames

2 Answers

Nikhil Regarding your post, it seems like you are going to create a custom theme for your inline text in the editor.. I am not sure whether this is right. However, if so, you can customize the editor theme and use it for your purpose. CKEditor has powerful builtin function for it. In addition, if you want to customize the editor, you can do. Please have a look at the following links: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/basic-api.html#creating-an-editor https://ckeditor.com/docs/ckeditor5/latest/framework/guides/deep-dive/ui/theme-customization.html Hope this helpful.

config.protectedSource.push(/<p[^>]*><\/p>/g);

Use This Code In editorConfig

CKEDITOR.editorConfig = function (config) {
    config.protectedSource.push(/<i[^>]*><\/i>/g);  };
Related