Search code examples
quill

Removing inline formats in Quill


I'm having some trouble getting removeFormat to work as it should. Funnily, I thought I had this working a couple days ago; but now when I check it it's not right. It is removing the block-level formatting regardless of where the selection is. A simple example, using the Quill Quickstart with a few modifications:

  var editor = new Quill('#editor', {
    modules: { toolbar: '#toolbar' },
    theme: 'snow'
  });
  let Block = Quill.import('blots/block');
let Parchment = Quill.import('parchment');

	class BlockquoteBlot extends Block { }
BlockquoteBlot.scope = Parchment.Scope.BLOCK;
	BlockquoteBlot.blotName = 'blockquote';
	BlockquoteBlot.tagName = 'blockquote';

	Quill.register(BlockquoteBlot);
	let quill = new Quill('#editor');

  $('#italic-button').click(function() {
    quill.format('italic', true);
  });
  $('#bold-button').click(function() {
    quill.format('bold', true);
  });
  $('#blockquote-button').click(function() {
    quill.format('blockquote', true);
  });
  $('.cust-clear').click(function(ev) {
    var range = quill.getSelection();
    quill.removeFormat(range.index, range.length);
  });
<link href="https://cdn.quilljs.com/1.0.3/quill.snow.css" rel="stylesheet"/>
<script src="https://cdn.quilljs.com/1.0.3/quill.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Create the toolbar container -->
<div id="toolbar">
  <button id="bold-button" class="ql-bold">Bold</button>
  <button id="italic-button" class="ql-italic">Italic</button>
  <button id="blockquote-button" class="ql-blockquote">Blockquote</button>
  <button class="cust-clear" title="Clear Formatting">X</button>
</div>

<!-- Create the editor container -->
<div id="editor">
  <p>Hello World!</p>
</div>

In this example, if I apply bold to "Hello" and make the entire line a blockquote, all looks as it should. If I then select "Hello" and click my X button to remove the formatting, it removes the blockquote formatting even though I'm nowhere near a "\n" character. Am I misunderstanding removeFormat, or is there an error in how I've created my BlockquoteBlot? I took that code mostly from the Medium demo on the Quill site, but I found in some cases I needed to specifically define the scope so that the tag would be recognized as block (that may not be necessary for this demo, but I'm including it in case it poses an issue).


Solution

  • The way removeFormat is supposed to work currently does remove all block formats a user highlights, even if it is not across the "\n" character. This makes more sense when the user is selecting multiple lines Issue #649 but perhaps it should not work this way when there is only one line partially selected. I've created a Github Issue to discuss this. Please feel free to chime in.