克隆到dom后如何初始化Quill Editor

时间:2016-12-22 22:27:16

标签: jquery quill

我有一个相当复杂的页面设置,用户可以动态地将块添加到dom中。该块中有一个quill编辑器,这可以正常工作。但是,我还需要能够在将内容复制到dom区域后对其进行编辑。

即。点击"添加页面"使用羽毛笔编辑器向下滑动表单。保存此表单会将表单复制到页面的新部分并清除原始表单。现在,当单击编辑图标时,复制的表单向下滑动,他们应该可以编辑。但是Quill Editor不会加载。我可以在dom中看到复制的元素,显示:none - 很容易取消隐藏,但这样做,我可以看到div和工具栏,但无法编辑。

所以我想我需要在克隆的div上重新初始化Quill但是卡住了。

以下是我的滑动克隆内容的代码(有效,但不允许我通过Quill进行编辑。

在放入Quill之前我有一个很好的textarea,但我不希望用户能够写html,所以转移到Quill。

// edit already saved page item
$('.inserted').on('click', '.page-item .edit-image-copy', function(e)
{
    e.preventDefault();

    // slide open form to edit.
    $(this)
        .closest('.page-item')
        .find('.page-take')
        .slideToggle()
        .children()
        .toggle();

    // commenting this stops quill from being viewable
    // even so you can't edit text on the cloned Editor
    // need to somehow initialize it.
    $(this)
        .closest('.page-item')
        .find('.Qeditor, .ql-toolbar')
        .toggle();

    // how do I pass the value of this into quillTarget and use as variable in var quill  new Quill(quillTarget, Qoptions)
    var quillTarget = $(this).closest('.page-item').find('.Qeditor');

    console.log('quillTarget is ' + quillTarget);

    var quill = new Quill($(this).closest('.page-item').find('.Queditor'), Qoptions);
});

Cloned Div是此html块

中的 .page-item div
<div class="inserted ui-sortable" id="sortable">
    <div class="item page-item">
        <div class="handle-container">
            <i aria-hidden="true" class="fa fa-bars"></i>
        </div>
        <div class="text-icon">
            <i aria-hidden="true" class="fa fa-file-text"></i>
        </div>
        <div class="text-page">
            <p>Text Page</p>
        </div>
        <div class="imageTitleForm">
            <p class="edit-icons">
                <a class="edit-image-copy" href="#"><span class="icon"><img alt="" src="/images/pencil_icon.png"></span></a><a class="remove" href="#"><span class="icon"><img alt="" src="/images/x_icon.png"></span></a>
            </p>
        </div>
        <div class="page-take" style="display: block;">
            <!--Title-->
            <div class="form-group" style="display: block;">
                <label class="control-label" for="page-title">Title</label> <input class="form-control input-md page-title" id="textinput" name="page-title" placeholder="Title" type="text">
            </div><!-- Copy-->
            <div class="ql-toolbar ql-snow" style="display: none;">
                <span class="ql-formats"><span class="ql-header ql-picker"><span class="ql-picker-label"><svg viewbox="0 0 18 18">
                <polygon class="ql-stroke" points="7 11 9 13 11 11 7 11"></polygon>
                <polygon class="ql-stroke" points="7 7 9 5 11 7 7 7"></polygon></svg></span><span class="ql-picker-options"><span class="ql-picker-item" data-value="2"></span><span class="ql-picker-item ql-selected"></span></span></span><select class="ql-header" style="display: none;">
                    <option value="2"></option>
                    <option selected="selected"></option>
                </select></span><span class="ql-formats"><button class="ql-bold" type="button"><span class="ql-formats"><svg viewbox="0 0 18 18">
                <path class="ql-stroke" d="M5,4H9.5A2.5,2.5,0,0,1,12,6.5v0A2.5,2.5,0,0,1,9.5,9H5A0,0,0,0,1,5,9V4A0,0,0,0,1,5,4Z"></path>
                <path class="ql-stroke" d="M5,9h5.5A2.5,2.5,0,0,1,13,11.5v0A2.5,2.5,0,0,1,10.5,14H5a0,0,0,0,1,0,0V9A0,0,0,0,1,5,9Z"></path></svg></span><button class="ql-italic" type="button"><svg viewbox="0 0 18 18">
                <line class="ql-stroke" x1="7" x2="13" y1="4" y2="4"></line>
                <line class="ql-stroke" x1="5" x2="11" y1="14" y2="14"></line>
                <line class="ql-stroke" x1="8" x2="10" y1="14" y2="4"></line></svg></button><button class="ql-underline" type="button"><svg viewbox="0 0 18 18">
                <path class="ql-stroke" d="M5,3V9a4.012,4.012,0,0,0,4,4H9a4.012,4.012,0,0,0,4-4V3"></path>
                <rect class="ql-fill" height="1" rx="0.5" ry="0.5" width="12" x="3" y="15"></rect></svg></button></button></span>
            </div>
            <div class="Qeditor ql-container ql-snow" style="display: none;">
                <div class="ql-editor ql-blank" contenteditable="true" data-placeholder="Enter your copy here...">
                    <p><br></p>
                </div>
                <div class="ql-clipboard" contenteditable="true" tabindex="-1"></div>
                <div class="ql-tooltip ql-hidden">
                    <a class="ql-preview" href="about:blank" target="_blank"></a><input data-formula="e=mc^2" data-link="quilljs.com" data-video="Embed URL" type="text"><a class="ql-action"></a><a class="ql-remove"></a>
                </div>
            </div>
            <p class="buttons" style="display: block;">
                <button class="btn btn-primary save-button" href="#">Save</button> <button class="btn btn-primary">Cancel</button>
            </p>
            <p class="close-btn close-title-page" style="display: block;">
                <a href="#">Close Page</a>
            </p>
        </div>
    </div>
</div>    

请告知。

1 个答案:

答案 0 :(得分:2)

Quill需要一个DOM元素或ID字符串。 您正在传递一个jQuery对象。 请尝试使用此代码:

var quillTarget = $(this).closest('.page-item').find('.Qeditor').get(0);

实施例:

//var el = $('#editor-container');
var el = $('#editor-container').get(0);
var quill = new Quill(el, {
  modules: {
    toolbar: [
      [{ header: [1, 2, false] }],
      ['bold', 'italic', 'underline'],
      ['image', 'code-block']
    ]
  },
  placeholder: 'Compose an epic...',
  theme: 'snow'  // or 'bubble'
});
#editor-container {
  height: 375px;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="//cdn.quilljs.com/1.1.8/quill.js"></script>
<link href="//cdn.quilljs.com/1.1.8/quill.snow.css" rel="stylesheet"/>

<div id="editor-container">
</div>