简体   繁体   English

富文本格式的HTML编辑/不带html标签的修改(查询)

[英]Rich Text Edit HTML / Modify without html tags (Quill)

I am not sure if I am thinking about this the right way. 我不确定我是否在考虑正确的方法。 I have a Div which has text. 我有一个有文字的Div。 I want to be able to edit this text with Quill then save it back to the Div with the formatting. 我希望能够使用Quill编辑此文本,然后使用格式将其保存回Div。 This is all good but then if I try to edit it again it pulls in all the HTML formatting tags. 一切都很好,但是如果我再次尝试对其进行编辑,它将提取所有HTML格式标记。

I think I am getting the content the wrong way or something. 我认为我以错误的方式或某些方式获取了内容。 I have been messing with the delta but not understanding how that could be used to "edit" the text if there is an update/mistake. 我一直在弄乱增量,但不了解如果有更新/错误,如何将其用于“编辑”文本。

Here is the code. 这是代码。 You can see the codepen here. 您可以在此处查看代码笔。 https://codepen.io/anon/pen/OwbRbQ https://codepen.io/anon/pen/OwbRbQ

//Create container
var container = $('.editor').get(0);

//Setup Quill
var quill = new Quill(container, {
    modules: {
    clipboard: {
      matchVisual: false
    },
        toolbar: '#toolbar-container'
    },
    placeholder: 'Compose an epic...',
    theme: 'snow'
});

//Edit functions
$("#editPage").click(function () {
    var $this = $(this);
    var introText = document.getElementById('introText');

    var existingContent = $('#introText').html();

    if ($this.attr('editing') != '1') {
        $this.html('<i class="fas fa-search"></i> Preview').attr('editing', 1);

        //Set content on edit/save
        quill.setContents([
            {
                insert: existingContent
            }
        ]);

    } else {
        $this.html("<i class='fas fa-pencil-alt'></i> Modify this page").removeAttr('editing');
        delta = quill.getContents();

        var html = quill.root.innerHTML;
        introText.innerHTML = html;
    }

});

Thanks for any help. 谢谢你的帮助。

I tried an hour to fix your code and I found the conclusion that the problem is in your Quill library which either you or me could not fix it. 我花了一个小时来修复您的代码,但得出的结论是问题出在您的Quill库中,您或我都无法修复它。 Therefore, the only party who can fix the library code is the library developer themselves. 因此,唯一可以修复库代码的一方是库开发人员。 If you notice in var html = quill.root.innerHTML; 如果您在var html = quill.root.innerHTML;注意到var html = quill.root.innerHTML; the content already changed at there. 内容已经在那里更改。

The only way to fix it for me is doing the trick that substracts the content from the quill editor which operated by its library. 为我修复此错误的唯一方法是执行从库的库管理器中减去内容的技巧。 The code will be something like this: 该代码将是这样的:

var container = $('.editor').get(0);

var quill = new Quill(container, {
    modules: {
    clipboard: {
      matchVisual: false
    },
        toolbar: '#toolbar-container'
    },
    placeholder: 'Compose an epic...',
    theme: 'snow'
});
$("#editPage").click(function () {
    var $this = $(this);
    var introText = document.getElementById('introText');

    var existingContent = $('#introText').html();

    if ($this.attr('editing') != '1') {
        $this.html('<i class="fas fa-search"></i> Preview').attr('editing', 1);


        quill.setContents([
            {
                insert: existingContent
            }
        ]);



    } else {
        $this.html("<i class='fas fa-pencil-alt'></i> Modify this page").removeAttr('editing');
        delta = quill.getContents();

        var html = quill.root.innerHTML;
         html = substr(html);
        introText.innerHTML = html;

    }

});
function substr(content) {
  var str = "";
  if ((content[0] == "<") && (content[1] == "p") && (content[2] == ">")) {
    for (i=3;i<=content.length-5;i++) str += content[i];
  }
  else {
    str = content;
  }
  return str;
}

You cannot find an absolute solution without other tricks unless you have the source code of Quill library and do trial and error for changing this line var html = quill.root.innerHTML; 除非您拥有Quill库的源代码并为更改此行进行反复试验,否则您将无法找到其他技巧var html = quill.root.innerHTML;

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

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