Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ jobs:

strategy:
matrix:
os: [ ubuntu-20.04 ]
os: [ ubuntu-latest ]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6
- name: Use Node.js
uses: actions/setup-node@v4
uses: actions/setup-node@v6
with:
node-version: 20
node-version: 24
cache: 'npm'
- name: Project setup
uses: bpmn-io/actions/setup@latest
- name: Install dependencies
run: npm ci
- name: Build
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to [selection-ranges](https://github.com/nikku/selection-ran

___Note:__ Yet to be released changes appear here._

## 4.1.0

* `FEAT`: add type definitions

## 4.0.3

* `DEPS`: update to `dom-iterator@1.0.2` ([#7](https://github.com/nikku/selection-ranges/issues/7))
Expand Down
3 changes: 1 addition & 2 deletions karma.conf.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module.exports = function(karma) {

frameworks: [
'webpack',
'mocha',
'chai'
'mocha'
],

files: [
Expand Down
33 changes: 23 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import iterator from 'dom-iterator';

var selection = window.getSelection();

/**
* @typedef { { start: number, end: number } } SelectionRange
*/

/**
* Add selection / insert cursor.
*
* @param {Range} range
*/
export function applyRange(range) {
selection.removeAllRanges();
selection.addRange(range);
selection?.removeAllRanges();
selection?.addRange(range);
}


Expand All @@ -31,12 +34,12 @@ export function getWindowSelection() {
/**
* Return true if element is part of window selection.
*
* @param {Element} el
* @return {Boolean}
* @param {Element} el
* @return {boolean}
*/
export function isSelected(el) {

if (!selection.rangeCount) {
if (!selection?.rangeCount) {
return null;
}

Expand All @@ -56,11 +59,11 @@ export function isSelected(el) {
* Set cursor or selection position.
*
* @param {Element} el
* @param {SelectionRange} selection
* @param {SelectionRange} selectionRange
*/
export function setRange(el, selection) {
export function setRange(el, selectionRange) {

var range = createRange(el, selection);
var range = createRange(el, selectionRange);

applyRange(range);
}
Expand All @@ -69,7 +72,11 @@ export function setRange(el, selection) {
/**
* Get cursor or selection position.
*
* Returns `null` if the element is not currently selected
*
* @param {Element} el
*
* @return {SelectionRange|null}
*/
export function getRange(el) {

Expand Down Expand Up @@ -196,10 +203,10 @@ export function getRange(el) {
* Annotate the given text with markers based on the
* given range.
*
* @param {String} text
* @param {string} text
* @param {SelectionRange} range
*
* @return {String} annotated text
* @return {string} annotated text
*/
export function annotateRange(text, range) {
var str;
Expand All @@ -226,6 +233,12 @@ export function annotateRange(text, range) {

// helpers ///////////////////////////

/**
* @param {Element} el
* @param {SelectionRange} selection
*
* @return {Range}
*/
function createRange(el, selection) {

var start = selection.start;
Expand Down
Loading