How to use TinyMCE with rails webpacks and stimulusjs
Tags: tinymce, rails, stimulus, webpacker
Published: 2021-01-29 04:54:45

---
title: How to use TinyMCE with rails webpacks and stimulusjs
published: true
description: 
tags: tinymce, rails, stimulusjs, webpacker
cover_image: https://camo.githubusercontent.com/893cce88ac7ccd9879ebc37d140be9017074488abdef43577f064507f78febd2/68747470733a2f2f7777772e74696e792e636c6f75642f73746f726167652f6769746875622d726561646d652d696d616765732f74696e796d63652d656469746f722e706e67
---

Assumed you already setup rails with webpack and stimulusjs 2.0

### 1. Install TinyMCE

```sh
yarn add tinymce
yarn add copy-webpack-plugin@6.4.1
```

### 2. Edit config/webpack/enviroment.js and configure copy webpack plugin


```js

const CopyPlugin = require('copy-webpack-plugin')

environment.plugins.prepend(
  'Environment',
  new webpack.EnvironmentPlugin({
    NODE_ENV: process.env.NODE_ENV,
    ASSET_PATH: environment.config.output.publicPath
  })
)

environment.plugins.prepend(
  'Copy',
  new CopyPlugin({
    patterns: [
      {
        context: './node_modules/tinymce/',
        from: '**/*.(min.js|min.css|woff)',
        to: './tinymce/[path][name].[ext]'
      }
    ]
  })
)
```

### 3. Create a stimulus controller, tinymce_controller.js
```js
// Import TinyMCE
import tinymce from 'tinymce/tinymce'

import { Controller } from 'stimulus'
export default class extends Controller {
  static targets = ['input']

  initialize () {
    this.defaults = {
      base_url: process.env.ASSET_PATH + 'tinymce',
      plugins:
        'print preview paste importcss autolink autosave save directionality code visualblocks visualchars fullscreen image link media table charmap hr pagebreak nonbreaking anchor toc advlist lists imagetools textpattern noneditable help charmap quickbars',
      menubar: 'file edit view insert format tools table',
      toolbar:
        'undo redo | bold italic underline strikethrough | fontselect fontsizeselect formatselect | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist preview | forecolor backcolor removeformat | pagebreak | charmap | insertfile image media link anchor | ltr rtl fullscreen',
      toolbar_sticky: true,
      suffix: '.min'
      //skin: (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'oxide-dark' : 'oxide'),
      //content_css: (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'default')
    }
  }

  connect () {
    // Initialize the app
    let config = Object.assign({ target: this.inputTarget }, this.defaults)
    tinymce.init(config)
  }

  disconnect () {
    tinymce.remove()
  }
}

```

### 4. Example usage in rails form

- Standard form

```erb
<%= form_with model: @object do |form| %>
  <div data-controller='tinymce'
     <%= form.text_area :intro, data: { tinymce_target: 'input' } %>
  </div>
<% end %>
```

- If you using simple form

```ruby
# create tinymce_input.rb in app/inputs
class TinymceInput < SimpleForm::Inputs::Base
  enable :placeholder, :maxlength, :minlength

  def input(wrapper_options = nil)
    options[:wrapper_html][:data] = { controller: :tinymce }
    input_html_options[:data] = { tinymce_target: 'input' }
    input_html_options[:class] = 'tinymce'
    input_html_options[:rows] ||= 20
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
    @builder.text_area(attribute_name, merged_input_options)
  end
end

```

```erb
<%= simple_for(@object) do |f| %>
  <%= f.input :intro,  as: :tinymce %>
<% end %>
```

### 5. Done :smile: :v: