dom-range-diff

a function that provides the range difference between two DOM Ranges

Usage no npm install needed!

<script type="module">
  import domRangeDiff from 'https://cdn.skypack.dev/dom-range-diff';
</script>

README

dom-range-diff

a micro function to provide the difference between two DOM Ranges.

Usage

use es6 syntax

import domRangeDiff from 'dom-range-diff';

var array = [];

window.addEventListener('mouseup', function(e) {
  /* if some text is selected save the range */
  if (!window.getSelection().isCollapsed) {
    array.push(window.getSelection().getRangeAt(0));

    /* when two ranges were selected, extract the difference between the first and the second, then apply it */
    if(array.length === 2) {
      window.getSelection().removeAllRanges();
      domRangeDiff(array[0], array[1]).map(range => {
        window.getSelection().addRange(range);
      });
      array = [];
    }
  }
});