trix: Custom color-tool formatting is lost after load

when I create a custom text-color button:

    Trix.config.textAttributes.red = {
          style: { color: "red" },
          parser: function(element) {
              return element.style.color === "red"
          },
          inheritable: true
      };

      addEventListener("trix-initialize", function(event) {
          var buttonHTML = '<button type="button" data-trix-attribute="red">RED</button>';
          event.target.toolbarElement.
          querySelector(".trix-button-group.trix-button-group--text-tools").
          insertAdjacentHTML("beforeend", buttonHTML)
      });

everything works fine, but when I try to load this text again, it loads only plain text into editor, without formatting <span>s with “red” color.

What I should please do?

  • Trix version: 1.1.1
  • Browser name and version: Chrome
  • Operating system: Windows

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

The likely problem here is the order in which the scripts are loaded.

These 2 scripts before the <trix-editor input="trix-input"></trix-editor>

<script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.0/trix.js"></script>
    <script>
      Trix.config.textAttributes.red = {
        style: { color: "red" },
        parser: element => element.style.color == "red",
        inheritable: true
      }
      addEventListener("trix-initialize", event => {
        const buttons = event.target.toolbarElement.querySelector(".trix-button-group--text-tools")
        buttons.insertAdjacentHTML("beforeend", `
          <button type="button" class="trix-button" data-trix-attribute="red" tabindex="-1">RED</button>
        `)
      })
    </script>

This does not work

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.0/trix.css">

</head>

<body>
  <input type="hidden" id="trix-input" value="<div>abc <span style=&quot;color: red;&quot;>123</span></div>">
  <trix-editor input="trix-input"></trix-editor>
</body>
<footer>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.0/trix.js"></script>
  <script>
    Trix.config.textAttributes.red = {
      style: { color: "red" },
      parser: element => element.style.color == "red",
      inheritable: true
    }
    addEventListener("trix-initialize", event => {
      const buttons = event.target.toolbarElement.querySelector(".trix-button-group--text-tools")
      buttons.insertAdjacentHTML("beforeend", `
          <button type="button" class="trix-button" data-trix-attribute="red" tabindex="-1">RED</button>
        `)
    })
  </script>
</footer>

</html>