Squire icon indicating copy to clipboard operation
Squire copied to clipboard

modifyBlocks produces an extra block when user selects a block via double-click

Open Caffeinix opened this issue 7 years ago • 1 comments

When modifyBlocks is called on a selection that was created by double-clicking on a line of text in the editor, the DocumentFragment passed to the modify function contains both the selected block and the unselected block after it.

This doesn't happen if you drag and select to the end of the line. It looks like it can be worked around by ignoring the last block produced by modifyBlocks if and only if the selected text ends in a newline character.

Repro

  1. Type at least two lines of text into the editor.
  2. Double-click the first one to select it.
  3. Call modifyBlocks() and examine the document fragment passed to the modify function.

Expected:

The document fragment should have one block, a div containing the selected text.

Actual:

The document fragment has two blocks: the expected one, and also the one after it.

Caffeinix avatar Feb 17 '18 02:02 Caffeinix

Better problem definition: this happens if the Range produced by a user selection has its start at offset 0 within one element, and its end at offset 0 within the next element, which seems to happen (in Chrome) if you triple-click (not necessarily double-click) on a line of text. It can be checked for with the following predicate:

editor.getSelection().endOffset == 0 &&
    editor.getSelection().endContainer != editor.getSelection().startContainer

You can then ignore the last block produced by modifyBlocks as mentioned above.

Caffeinix avatar Feb 28 '18 20:02 Caffeinix