Squire
Squire copied to clipboard
modifyBlocks produces an extra block when user selects a block via double-click
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
- Type at least two lines of text into the editor.
- Double-click the first one to select it.
- 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.
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.