Seleção de texto com paragrafo

by Ivan Ferrer

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<body>
        <div id="div1" style="border: 1px solid #000;">
            <div id="test-text">
                <h1> Hello How are you </h1>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry'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 teste 1.
                </p>
                 <p >
                    teste 2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry'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>
                 <p >
                    teste 2 Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry'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...

CSS

.highlight
            {
                background-color: yellow;
            }
            .hightlight-2
            {
                background-color: red;
            }
            #test-text::-moz-selection { /* Code for Firefox */
               
                background: yellow;
            }

            #test-text::selection {
               
                background: yellow;
            }

JavaScript

mouseXPosition = 0;
            $(document).ready(function () {
              
                $("#test-text").mousedown(function (e1) {
                    mouseXPosition = e1.pageX;//register the mouse down position
                });
                
                $('#test-text p').each(function(i, val){
                   $(this).attr('data-pid', 'p_'+i);
                })

                $("#test-text").mouseup(function (e2) {
                    var highlighted = false;
                    var selection = window.getSelection();
                    var selectedText = selection.toString();
                    var htmlSelected = window.getSelection().getRangeAt(0).cloneContents();
                    var startPoint = window.getSelection().getRangeAt(0).startOffset;
                    var endPoint = window.getSelection().getRangeAt(0).endOffset;
                    var anchorTag = selection.anchorNode.parentNode;
                    var current ={
                    first:null,
                    last:null
                    }
                   
                    
                    //anchorTag.setAttribute("data-pid", "current_1");
                    
                    console.log("parent node");
                    var focusTag = selection.focusNode.parentNode;
                   /*  if ((e2.pageX - mouseXPosition) < 0) { */
                        focusTag = selection.anchorNode.parentNode;
                        anchorTag = selection.focusNode.parentNode;
                   /*  } */
                     current.first = focusTag.getAttribute("data-pid");
                     current.last = anchorTag.getAttribute('data-pid');
                   // anchorTag.setAttribute("data-pid", "current_1");
                    console.log('t1',focusTag)
                     console.log('t2',anchorTag)
                     console.log('selection-text',selectedText)
                     console.log('selection-groups',htmlSelected)
                     ...