Denis Omerovic
Denis Omerovic

Reputation: 1440

Detect mouse click on text selection but ignore when selecting text?

Is there a way to detect mouse click or mouse up event on text selection but actually skip when selecting text?

https://jsfiddle.net/chille1987/ctm4rd7n/

<div id="editor">
    <p>What is Lorem Ipsum?</p>

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>

</div>

<div id="status"></div>

And here is my js code

$(function() {
    $('#editor').on('mouseup', function(e) {
        let selection = window.getSelection().toString().length;
        let targetContainsSelection = e.target.contains(window.getSelection().baseNode);

        if(selection > 0 && targetContainsSelection) {
            $('#status').text('true');
        } else {
            $('#status').text('false');
        }
    });
})

Disired solution is to status text to be true only when i click on selected text but not when I'm selecting.

Upvotes: 5

Views: 2159

Answers (1)

Titus
Titus

Reputation: 22474

You can add a mousedown listener and keep track if when the action was started (click or selection) there was a selection. Here is an example:

$(function() {
  let hasSelection = false;
  $('#editor').on('mousedown', function() {
    hasSelection = document.getSelection().toString().length;
  }).on('mouseup', function(e) {
    if (hasSelection) {
      let selection = window.getSelection().toString().length;
      let targetContainsSelection = window.getSelection().containsNode(e.target, true);

      if (selection > 0 && targetContainsSelection) {
        $('#status').text('TRUE').css('color', 'green');
      }
    } else {
      $('#status').text('FALSE').css('color', 'red');
    }
  });
});
#status {
  font-weight: bolder;
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="status">FALSE</div>
<div id="editor">
  <p>What is Lorem Ipsum?</p>

  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>

</div>

I've changed e.target.contains(window.getSelection().baseNode); to window.getSelection().containsNode(e.target, true); because e.target.contains(window.getSelection().baseNode); seems to always be false in FireFox.

Upvotes: 5

Related Questions