mobiledoc-kit icon indicating copy to clipboard operation
mobiledoc-kit copied to clipboard

Triple-click + toggleSection affects visibly unselected section

Open kevinansfield opened this issue 7 years ago • 2 comments

When triple-clicking on a paragraph or drag-selecting where the pointer is released just underneath the selected text, the selected range's tail ends up at offset 0 on the following section. Any subsequent use of editor.toggleSection applies the formatting to the visibly selected paragraph but also unexpectedly to the following paragraph.

I'm not sure if this is expected behaviour or a bug, if it's a bug is it in the cursor handling or the toggleSection implementation? Anyway, to work around this I had to add some extra logic to my editor.cursorDidChange event handler:

_cursorDidChange(editor) {
    let {head, tail, direction} = editor.range;

    // do not include the tail section if it's offset is 0
    // fixes triple-click unexpectedly selecting two sections for section-level formatting
    if (direction === 1 && !editor.range.isCollapsed && tail.offset === 0) {
        let prevSection = tail.section.prev;
        let newRange = new MobiledocKit.Range(head, prevSection.tailPosition());

        return editor.run(postEditor => {
            postEditor.setRange(newRange);
        });
    }

    if (!editor.range.isCollapsed) {
        set(this, 'hasSelection', true);
    } else {
        set(this, 'hasSelection', false);
    }
}

Editor: ember-mobiledoc-editor Browser: Chrome 63/Safari 11 on MacOS 10.13.2 (High Sierra)

The extra selection is a little easier to see on Safari because the space between paragraphs is shown as selected. In Chrome (shown below) there's zero visual difference between selecting the just text of the paragraph and the selection expanding to offset 0 of the following section

mobiledoc-triple-click

kevinansfield avatar Dec 18 '17 10:12 kevinansfield

Yeah this strikes me as a bug, personally. My colleague just ran into it and was confounded. Thanks for posting the work around 🙏

sdhull avatar Jan 18 '19 01:01 sdhull

I believe this was fixed by https://github.com/bustle/mobiledoc-kit/pull/713, just ran a quick test with 0.12.4 and can't reproduce it anymore 🎉

whatthewhat avatar Mar 13 '20 21:03 whatthewhat