简体   繁体   English

羽毛笔编辑器-无法设置点击数据

[英]Quill Editor - unable to set data on click

I am using quill editor and inside the bootstrap modal and its work fine while inserting the data in data-* attribute in the form of JSON 我正在使用羽毛笔编辑器并在引导程序模式内使用它,并且在以JSON形式将数据插入data- *属性中时工作正常

So what's the problem : 所以有什么问题 :

The problem is when I tried to set the data in the quill editor using setContent api it does not work 问题是,当我尝试使用setContent api在主轴编辑器中设置数据时,它不起作用

/*
        ====================
            Quill Editor
        ====================
    */

    var quill = new Quill('#editor-container', {
      modules: {
        toolbar: [
          [{ header: [1, 2, false] }],
          ['bold', 'italic', 'underline'],
          ['image', 'code-block']
        ]
      },
      placeholder: 'Compose an epic...',
      theme: 'snow'  // or 'bubble'
    });

Here is my jsfiddle ( before using this read the instruction below ) 这是我的jsfiddle (在使用前请阅读以下说明)

How to use my fiddle: 如何使用我的小提琴:

  1. Click on the add element button. 单击添加元素按钮。
  2. Type anything inside the quill editor 在主轴编辑器中输入任何内容
  3. Hit the save change button. 点击保存更改按钮。
  4. Now you will see an element prepend dynamically using js 现在您将看到一个使用js动态添加的元素
  5. Inspect the Dynamic Element. 检查动态元素。 You will see the data-text attribute with quill json data. 您将看到带有羽毛笔json数据的data-text属性。
  6. Now, Click on the Dynamic Element it opens the modal now the problem occurs it does not set the content value that it is fetching form data-* attribute. 现在,单击“动态元素”,它会打开模态,现在出现问题,它没有设置正在获取表单data- *属性的内容值。

The trick was converting the JSON string back into a JSON object: 诀窍是将JSON字符串转换回JSON对象:


From this : var dataText = $(this).attr('data-text'); 从这里开始var dataText = $(this).attr('data-text');

To this : var dataText = JSON.parse($(this).attr('data-text')); 为此var dataText = JSON.parse($(this).attr('data-text'));


Updated Fiddle 更新小提琴


 var quill = new Quill('#editor-container', { modules: { toolbar: [ [{ header: [1, 2, false] }], ['bold', 'italic', 'underline'], ['image', 'code-block'] ] }, placeholder: 'Compose an epic...', theme: 'snow' }); function modalclick() { $(".dynamic-element").on('click', function(event) { var dataText = JSON.parse($(this).attr('data-text')); $('#exampleModalLong').modal('show'); quill.setContents(dataText); console.log(dataText); }) } $("#addElement").on('click', function(event) { var delta = quill.getContents(); var $_textDelta = JSON.stringify(delta); console.log($_textDelta); $html = "<div class='dynamic-element' data-text='" + $_textDelta + "'>" + "<div>dynamic-element</div>" + "</div>"; $(".element").prepend($html); modalclick(); $('#exampleModalLong').modal('hide'); }) //modalclick(); $("#openmodal").on('click', function(event) { $('#exampleModalLong').modal('show'); }) $('#exampleModalLong').on('hidden.bs.modal', function(e) { quill.deleteText(0, 2000); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet"/> <!-- Button trigger modal --> <input type="button" id="openmodal" class="btn btn-info btn-rounded mb-4" value="Add element"> <div class="element"> </div> <!-- Modal --> <div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <div id="editor-container"> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" id="addElement" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM