Customizing the Trix editor in a Rails app (Action Text)
I noticed an issue yesterday in the production website for my Ruby on Rails app, Anki Books. I had recently migrated the website from being served from a laptop I had to an Azure VM, and to save compute I also somewhat tactically added caching to the public facing controller actions. The issue that I realized yesterday was that due to an inline script using a nonce for the Content Security Policy, the cached action result was including an outdated nonce, so the CSP was failing which was causing all of the JavaScript on the page to not work.
The nonce was needed for a script that was being used to change the Trix configuration. It was something I worked on over a year ago, but after trying other approaches that I think would have been better but I couldnât get to work, ended up an ERB partial _trix_config.erb which was being included in the layout of the app in the <head>:
I never really felt that approach was an ideal way to do it, but it worked, at least until adding the caching and realizing that the outdated nonce served from the cache was breaking the JavaScript. Since the nonce is only needed for inline JavaScript like this, I just had to change the approach so that it wasnât an inline script and a nonce wasnât necessary.
So today I tried for a while to get this in a Stimulus controller that in the connect() method would add the event listener, but I found it didnât work well. The behavior seemed to vary on different page refreshes as if there were some race conditions. It seemed like the trix-before-initialize event was firing before the Stimulus controller connected sometimes, so the event was never observed and the Trix object wasnât changed. But it turned out that simply putting this in app/javascript/application.js worked well:
I was surprised it worked without the event listener as I thought that was necessary from my previous experience, and I feel like when I was working on this the first time, the above would have been the first thing I tried. Ibelieve with this way itâs changing the Trix configuration for all instances of the Trix editor though. It may be that if you needed different Trix editors to have different configuration, this wouldnât work, and listening to the trix-before-initialize event could be a way to do it that would let you have different configurations.
The main change this is making is adding h2 through h6 buttons to the editor. The other Trix editor changes I am making are changes to its DOM elements, which is done by a Stimulus controller connected to a div that wraps where Action Text renders the editor:
And so the changes to the DOM itself happen with this controller, which involves some changes that are very specific to my app, such as setting the y position of the toolbar to just below the header, but also adding the additional heading buttons and giving the redo button a keyboard shortcut.
Unfortunately, most of the work here was from a while ago so it is harder for me to describe what parts of the Trix API I am using in a helpful way.
This is what the Trix editor toolbar ends up looking like in my app (the toolbar is mainly what is being changed with the above):
This shows how using the added heading buttons keep the consistent behavior of disabling the other block-level buttons: