<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sagar Gurung</title>
    <description>The latest articles on DEV Community by Sagar Gurung (@sagar_gurung_7).</description>
    <link>https://dev.to/sagar_gurung_7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1837121%2Fbcec9824-e99c-4e8b-abbf-c3b302e37fa7.jpg</url>
      <title>DEV Community: Sagar Gurung</title>
      <link>https://dev.to/sagar_gurung_7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sagar_gurung_7"/>
    <language>en</language>
    <item>
      <title>Integrate Draw.io and MathType with TinyMCE in NextJs Application</title>
      <dc:creator>Sagar Gurung</dc:creator>
      <pubDate>Mon, 29 Jul 2024 04:16:46 +0000</pubDate>
      <link>https://dev.to/sagar_gurung_7/integrate-drawio-and-mathtype-with-tinymce-in-nextjs-application-1lbd</link>
      <guid>https://dev.to/sagar_gurung_7/integrate-drawio-and-mathtype-with-tinymce-in-nextjs-application-1lbd</guid>
      <description>&lt;p&gt;In this article, we will explore how to integrate the Draw.io and MathType with TinyMCE rich text editor for diagram creation, writing mathematical expressions within a React application. For ease, I have used mantine components library. You can check this out on &lt;a href="https://mantine.dev/" rel="noopener noreferrer"&gt;mantine.dev&lt;/a&gt;. This integration allows users to create and embed diagrams directly into their content using a seamless workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;p&gt;First, ensure you have a React project set up. If not, you can create one using Create React App:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app tinymce-drawio-integration
cd tinymce-drawio-integration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Installing Dependencies
&lt;/h2&gt;

&lt;p&gt;Next, we need to install TinyMCE, Draw.io, and other necessary packages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @tinymce/tinymce-react react-drawio @mantine/hooks @mantine/core @wiris/mathtype-tinymce6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you'll need to create a new component for our integration. Let's name it Editor.tsx.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Editor Component
&lt;/h2&gt;

&lt;p&gt;In Editor.tsx, we will implement the TinyMCE editor and integrate the Draw.io diagram editor within a modal. Here’s the complete code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { FC, useRef, useState } from "react";
import { Editor as TinyMCEEditor } from "@tinymce/tinymce-react";
import { DrawIoEmbed, DrawIoEmbedRef, EventExport } from "react-drawio";
import { useDisclosure } from "@mantine/hooks";
import { Button, Modal } from "@mantine/core";

const Editor = () =&amp;gt; {
  const [opened, { open, close }] = useDisclosure(false);
  const [editorContent, setEditorContent] = useState("");
  const drawioRef = useRef&amp;lt;DrawIoEmbedRef&amp;gt;(null);

  const exportData = () =&amp;gt; {
    if (drawioRef.current) {
      drawioRef.current.exportDiagram({
        format: "xmlsvg",
      });
    }
  };

  const handleEditorChange = (content: string) =&amp;gt; {
    setEditorContent(content);
  };

  const handleSaveDrawio = (data: EventExport) =&amp;gt; {
    setEditorContent(
      (prevState) =&amp;gt;
        prevState + `&amp;lt;img src="${data.data}" alt="Drawio Diagram"/&amp;gt;`
    );
    close();
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;TinyMCEEditor
        id={id}
        apiKey="YOUR_TINYMCE_API_KEY"
        value={editorContent}
        init={{
          height: 500,
          draggable_modal: true,
          extended_valid_elements: "*[.*]",
          plugins: [
            "advlist",
            "autolink",
            "lists",
            "link",
            "image",
            "charmap",
            "preview",
            "anchor",
            "searchreplace",
            "visualblocks",
            "code",
            "fullscreen",
            "insertdatetime",
            "media",
            "table",
            "help",
            "wordcount",
            "tiny_mce_wiris",
          ],
          toolbar:
            "undo redo | formatselect | bold italic backcolor | \
            alignleft aligncenter alignright alignjustify | \
            bullist numlist outdent indent | removeformat | help | image | table | drawio | tiny_mce_wiris_formulaEditor | tiny_mce_wiris_formulaEditorChemistry",
          setup: (editor) =&amp;gt; {
            editor.ui.registry.addButton("drawio", {
              text: "Draw",
              onAction: () =&amp;gt; {
                open();
              },
            });
          },
          external_plugins: {
            tiny_mce_wiris: `http://localhost:3000/tinymce_plugins/mathtype/mathtype-tinymce6/plugin.min.js`,
          },
        }}
        onEditorChange={handleEditorChange}
      /&amp;gt;
      &amp;lt;Modal
        opened={opened}
        onClose={close}
        size="80vw"
        withCloseButton={false}
      &amp;gt;
        &amp;lt;div className="h-[80vh] relative"&amp;gt;
          &amp;lt;Button
            className="bg-accent absolute right-14 top-4"
            onClick={exportData}
            size="compact-xs"
          &amp;gt;
            Export
          &amp;lt;/Button&amp;gt;
          &amp;lt;DrawIoEmbed
            ref={drawioRef}
            onExport={handleSaveDrawio}
            urlParameters={{
              ui: "kennedy",
              spin: true,
              noSaveBtn: true,
              libraries: true,
              saveAndExit: false,
              noExitBtn: true,
            }}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/Modal&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Editor;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;1.&lt;strong&gt;TinyMCE Editor&lt;/strong&gt;: We configure the TinyMCE editor with various plugins and a custom toolbar. The setup function adds a custom button for opening the Draw.io modal.&lt;/p&gt;

&lt;p&gt;2.&lt;strong&gt;Draw.io Integration&lt;/strong&gt;: We use the react-drawio package to embed Draw.io. The DrawIoEmbed component is wrapped inside a Modal from @mantine/core.&lt;/p&gt;

&lt;p&gt;3.&lt;strong&gt;State Management&lt;/strong&gt;: The component maintains the editor content in the editorContent state. The useDisclosure hook from @mantine/hooks manages the modal's open and close state.&lt;/p&gt;

&lt;p&gt;4.&lt;strong&gt;Export and Embed Diagram&lt;/strong&gt;: When the user exports the diagram, it triggers the exportData function, which calls the exportDiagram method from the DrawIoEmbedRef. The resulting SVG data is then embedded into the TinyMCE editor content.&lt;/p&gt;

&lt;p&gt;5.&lt;strong&gt;MathType Integration&lt;/strong&gt;: Copy the mathType plugin installed in node_modules, paste it inside public folder and provide the path to plugin in editor external_plugins section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;external_plugins: {
tiny_mce_wiris:`http://localhost:3000/tinymce_plugins/mathtype/math 
 type-tinymce6/plugin.min.js`,
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Customizing the Integration&lt;/strong&gt;&lt;br&gt;
1.&lt;strong&gt;API Key&lt;/strong&gt;: Replace "YOUR_TINYMCE_API_KEY" with your TinyMCE API key.&lt;br&gt;
2.&lt;strong&gt;UI and Features&lt;/strong&gt;: Adjust the urlParameters in the DrawIoEmbed component to customize the Draw.io editor interface.&lt;/p&gt;
&lt;h2&gt;
  
  
  Running the Application
&lt;/h2&gt;

&lt;p&gt;After setting up the component, you can now use it within your application. Import and use MyComponent in your main application file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom";
import Editor from "./Editor";

const App = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;React TinyMCE and Draw.io Integration&amp;lt;/h1&amp;gt;
      &amp;lt;Editor /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

ReactDOM.render(&amp;lt;App /&amp;gt;, document.getElementById("root"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, start your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the TinyMCE editor with a custom "Draw" button. Clicking this button will open the Draw.io editor in a modal, allowing you to create and embed diagrams seamlessly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Integrating TinyMCE and Draw.io in a React application enhances the user experience by combining rich text editing with powerful diagram creation capabilities. This integration can be particularly useful for applications requiring detailed content creation, such as documentation tools, educational platforms, and collaborative workspaces.&lt;/p&gt;

</description>
      <category>drawio</category>
      <category>nextjs</category>
      <category>react</category>
      <category>tinymce</category>
    </item>
  </channel>
</rss>
