Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

selection-ranges

nikku66.2kMIT4.0.3

Selection range manipulation for contenteditable elements

dom, browser, util, contenteditable, selection, selection range

readme

selection-ranges

CI

Manipulate selection ranges on contenteditable elements.

API

/**
 * Return the selection on the given element as {start, end}.
 *
 * @param {Element} el
 *
 * @return {Object} selection range or null if element is not selected
 */
getRange(el);
/**
 * Selects the given range on the specified element.
 *
 * @param {Element} el
 * @param {Object} range {start, end}
 */
setRange(el, range);
/**
 * Return true if element is part of window selection.
 *
 * @param  {Element}  el
 * @return {Boolean}
 */
isSelected(el);

Usage

import {
  getRange,
  setRange
} from 'selection-ranges';


var node = <div contenteditable />;

let range = getRange(node);
// a range such as { start: 5, end: 10 }
// or null if node is currently not selected

setRange(node, { start: 0, end: 30 });
// sets selection range to child nodes; does not focus node

Features

  • Works around browser issues and correctly handles <br/> and paragraph elements
  • Correctly handles out-of-bounds selections
  • selection-update - compute input selection updates on external content changes

License

MIT

changelog

Changelog

All notable changes to selection-ranges are documented here. We use semantic versioning for releases.

Unreleased

_Note: Yet to be released changes appear here._

4.0.3

4.0.2

  • FIX: correctly handle selection at end-of-line (#6)

4.0.1

  • FIX: correctly handle repeated visits to start container (#3)

4.0.0

  • FEAT: add exports field
  • FEAT: turn into ES module
  • CHORE: generate sourcemap
  • CHORE: update dependencies
  • CHORE: drop UMD distribution

Breaking Changes

  • Drop UMD distribution. Use ES module export instead.

3.0.3

  • CHORE: package LICENSE file

3.0.2

Republish of v3.0.1 with update changelog.

3.0.1

  • FIX: make babel a development dependency (#1)

3.0.0

Breaking Changes

  • FIX: remove browser field again; it confuses modern module bundlers. This partially reverts v2.1.0

2.1.0

  • CHORE: add browser field

2.0.0

  • CHORE: migrate to ES6
  • FEAT: provide pre-built distribution in dist folder (UMD export name is SelectionRanges)

1.2.3

  • FIX: correctly set range on empty node

1.2.2

  • FIX: correctly set range post paragraphs (IE 11)

1.2.1

  • FIX: ensure isSelected(el) supports IE 11

1.2.0

  • FEAT: support MS Edge style div containers

1.1.0

  • FEAT: add isSelected(el) util

1.0.1

  • CHORE: ignore development assets
  • CHORE: add CHANGELOG.md

1.0.0

  • initial implementation