DEV Community

Konnor Rogers
Konnor Rogers

Posted on

10 5

Adding additional actions to Trix

It's not documented how to add additional "actions" to Trix.

Current actions can be found here:

https://github.com/basecamp/trix/blob/7940a9a3b7129f8190ef37e086809260d7ccfe32/src/trix/controllers/editor_controller.coffee#L301-L318

But how do we make additional actions?

It appears an action is an object:

{
  test: Boolean,
  perform: void
}
Enter fullscreen mode Exit fullscreen mode

So test is like "hey should we perform the action?"

and perform is what gets called if test === true. Seems reasonable.

Now to the hard part, I couldn't find any docs to add an additional Trix action.

But, in sleuthing through the console I found this:

document.querySelector("trix-editor").editorController.actions

/* 
Object { 
  attachFiles: Object { test: test(), perform: perform() }
  decreaseNestingLevel: Object { test: test(), perform: perform() }
  increaseNestingLevel: Object { test: test(), perform: perform() }
  link: Object { test: test() }
  redo: Object { test: test(), perform: perform() }
  undo: Object { test: test(), perform: perform() }
}
*/
Enter fullscreen mode Exit fullscreen mode

So it appears we can add additional actions by tapping into the editorController.actions on a currently active instance of "trix-editor".

So a simple example to add an action may look like this:

document.addEventListener('trix-initialize', updateActions);

function updateActions() {
  const editors = document.querySelectorAll("trix-editor")
  const myAction = { test: true, perform: console.log("Hi!") }
  editors.forEach((editor) => Object.assign(editor.editorController.actions, { myAction })
}
Enter fullscreen mode Exit fullscreen mode

And now when we add an item to the toolbar, we should be able to do something like this to trigger our action:

<button data-trix-action="myAction"></button>
Enter fullscreen mode Exit fullscreen mode

This is a small precursor to me exploring table support, but figured I'd share as it took a while to track down!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
adrianvalenz profile image
Adrian Valenzuela

Nice work!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay