himanshu
himanshu

Reputation: 501

How to use MathType plugin in CKEditor 5 using ReactJS?

I have installed three packages:

  1. @ckeditor/ckeditor5-react
  2. @ckeditor/ckeditor5-build-classic
  3. @wiris/mathtype-ckeditor5/src/plugin

I'm able to setup simple ckeditor5 but don't know how to use MathType plugin in this editor.

Here is my sample code:

<CKEditor
  data={input.value}
  editor={ClassicEditor}
  onChange={(event, editor) => {
    return input.onChange(editor.getData());
  }}
/>;

Can anyone explain how i can use this? Thanks.

Upvotes: 8

Views: 5417

Answers (3)

Henok Tesfaye
Henok Tesfaye

Reputation: 9560

https://www.npmjs.com/package/ckeditor5-build-classic-mathtype, a package which customizes classic editor build, which adds mathtype plugin.

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

...

render() {
        return (
                <CKEditor
                    editor={ClassicEditor}
                    data="<p>Hello from CKEditor 5 with MathType!</p>"
                    onInit={editor => {
                        // You can store the "editor" and use when it is needed.
                        // console.log( 'Editor is ready to use!', editor );
                    }}
                />
        )
    }

Upvotes: 1

himanshu
himanshu

Reputation: 501

I have used MathType Editor in ReactJs using Javascript.

Here is the steps:

  1. Include in index.html
  2. render normal text area in the component
<textarea name="question" id="question" cols="100" rows="6"></textarea>
  1. use below code in componentDidMount function :
let ED = window.CKEDITOR;
    let mathElements = [
          'math',
          'maction',
          'maligngroup',
          'malignmark',
          'menclose',
          'merror',
          'mfenced',
          'mfrac',
          'mglyph',
          'mi',
          'mlabeledtr',
          'mlongdiv',
          'mmultiscripts',
          'mn',
          'mo',
          'mover',
          'mpadded',
          'mphantom',
          'mroot',
          'mrow',
          'ms',
          'mscarries',
          'mscarry',
          'msgroup',
          'msline',
          'mspace',
          'msqrt',
          'msrow',
          'mstack',
          'mstyle',
          'msub',
          'msup',
          'msubsup',
          'mtable',
          'mtd',
          'mtext',
          'mtr',
          'munder',
          'munderover',
          'semantics',
          'annotation',
          'annotation-xml'
      ];

    ED.plugins.addExternal( 'ckeditor_wiris',  'https://www.wiris.net/demo/plugins/ckeditor/', 'plugin.js' );

    ED.replace( 'question', {
        extraPlugins: 'ckeditor_wiris',
        // For now, MathType is incompatible with CKEditor file upload plugins.
        removePlugins: 'filetools,uploadimage,uploadwidget,uploadfile,filebrowser,easyimage',
        height: 320,
        // Update the ACF configuration with MathML syntax.
        extraAllowedContent: mathElements.join( ' ' ) + '(*)[*]{*};img[data-mathml,data-custom-editor,role](Wirisformula)'
    } );


If you want to check Ml tags in the react component. Need to load a script in the component:

const script = document.createElement("script");
script.src ='https://www.wiris.net/demo/plugins/app/WIRISplugins.js?viewer=image;
script.async = true;
document.body.appendChild(script);

Upvotes: 0

Idriss AIT HAFID
Idriss AIT HAFID

Reputation: 301

Here's a link you should see to understand how to add a plugin to ckeditor.

TL;DR: You should create a new build containing your plugin (in your case MathType plugin), the easiest way to do it is using their online builder, then you can use that build that you generated instead of @ckeditor/ckeditor5-build-classic for example.

I have already done this work and published it to npm, you can install it with:

npm install ckeditor5-classic-with-mathtype

Here's an example of using it with react:

import CKEditor from '@ckeditor/ckeditor5-react';
import ClassicEditor from 'ckeditor5-classic-with-mathtype';

...

render() {
        return (
                <CKEditor
                    editor={ClassicEditor}
                    config={{
                        toolbar: {
                            items: [
                                'heading', 'MathType', 'ChemType',
                                '|',
                                'bold',
                                'italic',
                                'link',
                                'bulletedList',
                                'numberedList',
                                'imageUpload',
                                'mediaEmbed',
                                'insertTable',
                                'blockQuote',
                                'undo',
                                'redo'
                            ]
                        },
                    }}
                    data="<p>Hello from CKEditor 5 with MathType!</p>"
                    onInit={editor => {
                        // You can store the "editor" and use when it is needed.
                        // console.log( 'Editor is ready to use!', editor );
                    }}
                />
        );
    }

Upvotes: 10

Related Questions