Bundling the TinyMCE .zip package with the React framework
Tiny does not recommend bundling the tinymce package. Bundling TinyMCE can be complex and error prone.
|
The Official TinyMCE React component integrates TinyMCE into React projects. This procedure creates a basic React application containing a TinyMCE editor.
For examples of the TinyMCE integration, visit the tinymce-react storybook.
Prerequisites
This procedure requires Node.js (and npm).
Procedure
-
Use the Vite package and the React SWC plugin to create a new React project named
tinymce-react-demo.npm create vite@5 tinymce-react-demo -- --template react-swc -
Change to the newly created directory.
cd tinymce-react-demo -
Install the
@tinymce/tinymce-reactandscript-loaderpackages and save them to yourpackage.jsonwith--save.npm install --save @tinymce/tinymce-react script-loader -
Unzip the content of the
tinymce/jsfolder from the TinyMCE zip into thesrcfolder. Afterwards the directory listing should be similar to below:>tree -L 2 srcsrc ├── App.css ├── App.js ├── App.test.js ├── index.css ├── index.js ├── logo.svg ├── reportWebVitals.js ├── setupTests.js └── tinymce ├── icons ├── langs ├── license.txt ├── models ├── plugins ├── skins ├── themes ├── tinymce.d.ts └── tinymce.min.js -
Using a text editor, open
./eslintrc.cjsand add'src/tinymce'to theignorePatternsarray.Diff of.eslintrc.jsdiff --git a/.eslintrc.cjs b/.eslintrc.cjs index 3b3b3b3..4b4b4b4 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -7,7 +7,7 @@ modules.exports = { 'plugin:react/jsx-runtime', 'plugin:react-hooks/recommended', ], - ignorePatterns: ['dist', '.eslintrc.cjs'], + ignorePatterns: ['dist', '.eslintrc.cjs', 'src/tinymce'], parserOptions: { ecmaVersion: 'latest', sourceType: 'module' }, settings: { react: { version: '18.2' } }, plugins: ['react-refresh'], -
Using a text editor, create
./src/BundledEditor.jsxand set the contents to:import { Editor } from '@tinymce/tinymce-react'; // TinyMCE so the global var exists import './tinymce/tinymce.min.js'; // DOM model import './tinymce/models/dom/model.min.js' // Theme import './tinymce/themes/silver/theme.min.js'; // Toolbar icons import './tinymce/icons/default/icons.min.js'; // Editor styles import './tinymce/skins/ui/oxide/skin.min.js'; // importing the plugin index.js file. // if you use a plugin that is not listed here the editor will fail to load import './tinymce/plugins/advlist'; import './tinymce/plugins/anchor'; import './tinymce/plugins/autolink'; import './tinymce/plugins/autoresize'; import './tinymce/plugins/autosave'; import './tinymce/plugins/charmap'; import './tinymce/plugins/code'; import './tinymce/plugins/codesample'; import './tinymce/plugins/directionality'; import './tinymce/plugins/emoticons'; import './tinymce/plugins/fullscreen'; import './tinymce/plugins/help'; import './tinymce/plugins/image'; import './tinymce/plugins/importcss'; import './tinymce/plugins/insertdatetime'; import './tinymce/plugins/link'; import './tinymce/plugins/lists'; import './tinymce/plugins/media'; import './tinymce/plugins/nonbreaking'; import './tinymce/plugins/pagebreak'; import './tinymce/plugins/preview'; import './tinymce/plugins/quickbars'; import './tinymce/plugins/save'; import './tinymce/plugins/searchreplace'; import './tinymce/plugins/table'; import './tinymce/plugins/visualblocks'; import './tinymce/plugins/visualchars'; import './tinymce/plugins/wordcount'; // importing plugin resources import './tinymce/plugins/emoticons/js/emojis.js'; // Content styles, including inline UI like fake cursors import './tinymce/skins/content/default/content.js'; import './tinymce/skins/ui/oxide/content.js'; export default function BundledEditor(props) { return ( <Editor licenseKey='your-lisense-key' {...props} /> ); } -
Update the
licenseKeyoption in the editor element and include your License Key. -
Using a text editor, open
./src/App.jsxand replace the contents with:import { useRef } from 'react'; import BundledEditor from './BundledEditor' import './App.css'; export default function App() { const editorRef = useRef(null); const log = () => { if (editorRef.current) { console.log(editorRef.current.getContent()); } }; return ( <> <BundledEditor onInit={(_evt, editor) => editorRef.current = editor} initialValue='<p>This is the initial content of the editor.</p>' init={{ height: 500, menubar: false, plugins: [ 'advlist', 'anchor', 'autolink', 'help', 'image', 'link', 'lists', 'searchreplace', 'table', 'wordcount' ], toolbar: 'undo redo | blocks | ' + 'bold italic forecolor | alignleft aligncenter ' + 'alignright alignjustify | bullist numlist outdent indent | ' + 'removeformat | help', content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }' }} /> <button onClick={log}>Log editor content</button> </> ); } -
Test the application using the Node.js development server.
-
To start the development server, navigate to the
tinymce-react-demodirectory and run:npm run dev -
To stop the development server, select on the command line or command prompt and press Ctrl+C.
-
Deploying the application to a HTTP server
The application will require further configuration before it can be deployed to a production environment. For information on configuring the application for deployment, see: Building for Production or Deploying a Static Site.
To deploy the application to a local HTTP Server:
-
Navigate to the
tinymce-react-demodirectory and run:npm run build -
You can optionally preview the production build by running:
npm run preview -
Copy the contents of the
tinymce-react-demo/distdirectory to the root directory of the web server.
The application has now been deployed on the web server.
| Additional configuration is required to deploy the application outside the web server root directory, such as http://localhost:<port>/my_react_application. |
Next Steps
-
For information on bundling, see: Introduction to bundling TinyMCE
-
For examples of the TinyMCE integration, see: the tinymce-react storybook.
-
For information on customizing:
-
TinyMCE integration, see: TinyMCE React integration technical reference.
-
TinyMCE, see: Basic setup.
-
The React application, see: Getting Started with Vite or the React documentation.
-