Skip to content

Instantly share code, notes, and snippets.

@zhangshine
Last active November 23, 2024 15:22
Show Gist options
  • Save zhangshine/00607fa3fe89195ef0a0e88983174b37 to your computer and use it in GitHub Desktop.
Save zhangshine/00607fa3fe89195ef0a0e88983174b37 to your computer and use it in GitHub Desktop.
NextJs- React - Self hosted TinyMCE
  1. Install (TinyMCE 5.x)
npm install --save tinymce @tinymce/tinymce-react copy-webpack-plugin
  1. Copy static files(tinymce skins) to public folder. Edit file next.config.js
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
        config.plugins.push(new CopyPlugin([
            { from: path.join(__dirname, 'node_modules/tinymce/skins'), to: path.join(__dirname, 'public/assets/libs/tinymce/skins') }, /// Copy to public folder
        ]));
        return config
    },
    webpackDevMiddleware: config => {
        return config
    },
}
  1. React components file
if(typeof window !== 'undefined'){
    require ('tinymce/tinymce');
    require ('tinymce/themes/silver');
    require ('tinymce/plugins/advlist');
    require ('tinymce/plugins/autolink');
    require ('tinymce/plugins/lists');
    require ('tinymce/plugins/link');
    require ('tinymce/plugins/image');
    require ('tinymce/plugins/charmap');
    require ('tinymce/plugins/print');
    require ('tinymce/plugins/preview');
    require ('tinymce/plugins/anchor');
    require ('tinymce/plugins/searchreplace');
    require ('tinymce/plugins/visualblocks');
    require ('tinymce/plugins/code');
    require ('tinymce/plugins/fullscreen');
    require ('tinymce/plugins/insertdatetime');
    require ('tinymce/plugins/media');
    require ('tinymce/plugins/table');
    require ('tinymce/plugins/paste');
    require ('tinymce/plugins/code');
    require ('tinymce/plugins/help');
    require ('tinymce/plugins/wordcount');
}

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

function AReactComponent(props) {
    return (
        <Editor 
            value={props.content}
            init={{
                height: 500,
                menubar: false,
                plugins: [
                    'advlist autolink lists link image charmap print preview anchor',
                    'searchreplace visualblocks code fullscreen',
                    'insertdatetime media table paste code help wordcount'
                ],
                toolbar:
                    'undo redo | formatselect | bold italic backcolor | \
                    alignleft aligncenter alignright alignjustify | \
                    bullist numlist outdent indent | removeformat | \
                    table | help',
                skin_url: '/assets/libs/tinymce/skins/ui/oxide', // Static files path(step 2)
                content_css: '/assets/libs/tinymce/skins/content/default/content.min.css'  // Static files path(step 2)
            }}
            onEditorChange={props.handleOnEditorChange}      
        />
    );
}
@DjakaTechnology
Copy link

@CPoncet Also had same issue, thanks for the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment