【问题标题】:Trix editor define custom attachment stylesTrix 编辑器定义自定义附件样式
【发布时间】:2019-03-15 14:13:29
【问题描述】:

我在 Trix 编辑器中添加了一张图片,生成了以下代码:

<figure 
    data-trix-attachment="{lots of data}" 
    data-trix-content-type="image/jpeg"
    data-trix-attributes="{'presentation':'gallery'}" 
    class="attachment attachment--preview attachment--jpg">
    <img src="http://myhost/myimage.jpg" width="5731" height="3821">
    <figcaption class="attachment__caption">
        <span class="attachment__name">cool.jpg</span> <span class="attachment__size">4.1 MB</span>
    </figcaption>
</figure>

当我在基于 Bootstrap 的页面上显示从编辑器生成的 HTML 时,图像显然会扩展屏幕(请参阅 widthheight),我想删除这些道具并分配 @ 987654324@ 上课。

所以基本上我想使用配置:

Trix.config.css.attachment = 'img-fluid'

但这确实 a) 不会将 attachment 类更改为 img-fluid 并且它也不会将更改应用于图像,而是应用于 figure

我想避免每次显示内容时都使用 jQuery,并遍历所有figures,然后在运行时操作图像的属性。

添加附件时是否有定义这些样式的解决方案?

【问题讨论】:

    标签: javascript html css trix


    【解决方案1】:

    Trix 不支持更改附件中的图像元素。一种方法是使用 MutationObserver 检查 Trix 编辑器中适用于 attributeschildListsubtree 的突变。

    如果我们有一个 widthheight attributes 突变到具有 img 目标节点的 figure 父节点,那么我们删除这些属性,我们可以将类 img-fluid 应用于第一个属性突变,例如width

    运行代码 sn-p 并尝试添加一些图像附件以查看或检查 HTML

    请阅读内联 cmets

    // Listen to trix-attachment-add event so we'll get rid of the progress bar just for this demo
    // Here we should upload the attachment and handle progress properly
    document.addEventListener("trix-attachment-add", event => {
      const { attachment } = event.attachment;
     
      // Get rid of the progress bar
      attachment.setUploadProgress(100)
    });
    
    
    // Get the Trix editor
    const editor = document.querySelector('trix-editor');
    
    // Instantiating an observer
    const observer = new MutationObserver(function (mutations) {
      mutations.forEach(({ type, target, attributeName }) => {
        
        // If the parent is a figure with an img target
        if (target.parentNode.tagName === 'FIGURE' && 
            target.nodeName === 'IMG')
        {
          if (type === 'attributes') {
            switch(attributeName) {
    
              // If we have attribute width
              case 'width':
                // Remove attribute width
                target.removeAttribute('width');
                // Add img-fluid only once
                target.classList.add('img-fluid');
                break;
    
              // If we have attribute height
              case 'height':
                // Remove attribute height
                target.removeAttribute('height');
                break;
            }
          }
    
          // Render images HTML code
          renderHtmlOutput();
        }
    
      });
    });
    
    // Observing Trix Editor
    observer.observe(editor, {
      attributes: true,
      childList: true,
      subtree: true
    });
    
    // Function to render every figure > img HTML code
    function renderHtmlOutput() {
      const images = editor.querySelectorAll('figure > img');
      let output = '';
    
      for(const image of images) {
        output += image.outerHTML.replace(/ /g, "\n  ") + "\n";
      }
    
      document.getElementById('output-html').textContent = output;
    }
    body {
      height: 100vh;
      margin: 0;
      flex-direction: column;
      display: flex;
    }
    #main {
      display: flex;
      flex-direction: row;
      flex: 1;
      margin: 10px;
    }
    
    #editor-container {
      flex: 3;
    }
    
    #output-container {
      flex: 2;
      margin-left: 20px;
      border-left: 1px solid lightgray;
      overflow: auto;
    }
    
    #output-html {
      margin: 0;
      padding: 10px;
      font-size: small;
      color: blue;
    }
    
    /* Hide some Trix buttons to free horizontal space */
    .trix-button--icon-increase-nesting-level,
    .trix-button--icon-decrease-nesting-level,
    .trix-button--icon-bullet-list,
    .trix-button--icon-number-list { display: none; }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.1/trix.js" integrity="sha256-2D+ZJyeHHlEMmtuQTVtXt1gl0zRLKr51OCxyFfmFIBM=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/trix/1.2.1/trix.css" integrity="sha256-yebzx8LjuetQ3l4hhQ5eNaOxVLgqaY1y8JcrXuJrAOg=" crossorigin="anonymous"/>
    
    <section id="main">
      <div id="editor-container">
        <form>
          <input id="editor" value="Editor content goes here" type="hidden" name="content">
          <trix-editor input="editor"></trix-editor>
        </form>
      </div>
      <div id="output-container">
        <pre id="output-html"></pre>
      </div>
    </section>

    【讨论】:

    • 你能用 react 解释一下吗? @christoslytras
    • @ertemishakk 在 React 中使用 MutationObserver 并没有什么特别之处,您只需使用使用 useRef 钩子制作的 refs 即可。如果你想让事情变得更简单,有一个钩子可以用于MutationObserver 本身,你可以在@rooks/use-mutation-observer 找到有关安装和使用的详细信息。
    • 为什么您的代码只将 img-fluid 添加到第一个添加的附件中? @christoslytras
    • @ertemishakk 它不会将它添加到第一个附件中,它会将它添加到找到的每个附件的一个属性中,你不明白@ 的目的是什么987654341@有吗?它用于属性,它写switch(attributeName),因此显然是用于图像属性;因此,我们只将类 img-fluid 添加到 width 属性,因为 我们只想添加一次,并且不会浪费 CPU 周期来添加两次已经添加的类存在。
    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多