Giter Site home page Giter Site logo

david-uhlig / example-tinymce-rails7-turbo Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 34 KB

An example integration of TinyMCE in Rails 7 that doesn't break with Turbo Drive

License: MIT License

Ruby 73.25% JavaScript 5.48% CSS 2.18% HTML 19.10%
demo example rails ruby tinymce

example-tinymce-rails7-turbo's Introduction

Example Integration of TinyMCE in Rails 7 with Turbo Drive Support

This example project demonstrates the integration of the TinyMCE editor in Rails 7. It utilizes the well known tinymce-rails gem and offers full Turbo Drive support through an additional Stimulus controller.

Since Rails 7 the turbo-rails library is included by default. Turbo Drive intercepts link clicks and form submits. It makes sure that only the <body> part of the page is rerendered instead of the whole page.

This leads to TinyMCE not being properly detached and reattached when a Turbo Drive response is rendered. The textarea will appear without the TinyMCE editor. The Stimulus controller helps to reattach TinyMCE and respects the settings in config/tinymce.yml.

Instructions

Details about the integration can be found in this Gist.

Try out the example integration

1. Clone the repository

git clone https://github.com/david-uhlig/example-tinymce-rails7-turbo
cd example-tinymce-rails7-turbo/

Then run bundle install

2. Start the server

rails s --port 3000

Open http://localhost:3000 in your browser.

Sources

Notes

Without the tinymce-rails gem

The tinymce-rails gem is not strictly necessary. You may also use the Tiny Cloud CDN similarly as described here.

1. Add the following line to the <head> section of app/views/layouts/application.html.erb

<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>

2. Add the initialization to your Stimulus controller

    initialize() {
        this.defaults = {
            selector: '.tinymce',
            toolbar: [
                'styleselect | bold italic | undo redo',
                'image | link'
            ],
            plugins: 'lists link image table code help wordcount'
        }
    }

3. Replace the connect function in the Stimulus controller

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

The settings will no longer be read from config/tinymce.yml but from the Stimulus controller instead.

Using importmap

I would be very interested in a solution utilizing bin/importmap pin tinymce. This seems to be the favorable way going forward in Rails 7. I had no luck making it work, though.

example-tinymce-rails7-turbo's People

Contributors

david-uhlig avatar

Stargazers

andersen_fan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.