Skip to content

Commit

Permalink
Merge pull request #102 from inokawa/range-at-index
Browse files Browse the repository at this point in the history
Fix deprecated warning caused by range-at-index
  • Loading branch information
inokawa authored Aug 13, 2023
2 parents 5066bab + a17fd4e commit 4cd0f24
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 58 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module.exports = {
roots: ["<rootDir>/src"],
testEnvironment: "jsdom",
transform: {
"^.+\\.(ts|tsx)$": "ts-jest",
"^.+\\.(js|ts|tsx)$": "ts-jest",
},
};
48 changes: 0 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
"size": "size-limit",
"prepublishOnly": "npm run typedoc && rimraf lib && npm run build"
},
"dependencies": {
"range-at-index": "^1.0.4"
},
"dependencies": {},
"devDependencies": {
"@babel/plugin-transform-react-pure-annotations": "7.22.5",
"@playwright/test": "^1.36.1",
Expand Down
5 changes: 2 additions & 3 deletions src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
memo,
RefObject,
} from "react";
// @ts-expect-error no type definition
import rangeAtIndex from "range-at-index";
import rangeAtIndex from "./vendor/range-at-index";
import {
hasPercentageUnit,
isSafari,
Expand Down Expand Up @@ -269,7 +268,7 @@ export const RichInput = forwardRef<RichInputHandle, RichInputProps>(
});
} else {
const range = rangeAtIndex(
backdropRef[refKey],
backdropRef[refKey]!,
selectionStart,
selectionStart + 1
) as Range;
Expand Down
5 changes: 2 additions & 3 deletions src/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
memo,
RefObject,
} from "react";
// @ts-expect-error no type definition
import rangeAtIndex from "range-at-index";
import rangeAtIndex from "./vendor/range-at-index";
import { hasPercentageUnit, stopPropagation, syncBackdropStyle } from "./dom";
import { SelectionRange, initSelectionStore } from "./selection";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
Expand Down Expand Up @@ -247,7 +246,7 @@ export const RichTextarea = forwardRef<RichTextareaHandle, RichTextareaProps>(
});
} else {
const range = rangeAtIndex(
backdropRef[refKey],
backdropRef[refKey]!,
selectionStart,
selectionStart + 1
) as Range;
Expand Down
8 changes: 8 additions & 0 deletions src/vendor/range-at-index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare function RangeAtIndex(
el: HTMLElement,
index: number,
offset: number,
range?: Range
): Range;

export default RangeAtIndex;
53 changes: 53 additions & 0 deletions src/vendor/range-at-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* range-at-index
* https://github.com/webmodules/range-at-index/
* Nathan Rajlich
*
* Forked from version 1.0.4; includes the following modifications:
* 1) Change module.exports to export default.
**/

/**
* Returns a Range instance selecting text within HTML Element `el`,
* at the given `start` and `end` offsets.
*
* @param {HTMLElement} el - DOM element to select text within
* @public
*/

function RangeAtIndex(el, index, offset, range) {
var doc = el.ownerDocument;
if (!range) range = doc.createRange();

let iterator = doc.createNodeIterator(el, NodeFilter.SHOW_TEXT, null, false);

let start = {};
let end = {};
let node, val, len;

while ((node = iterator.nextNode())) {
val = node.nodeValue;
len = val.length;

if (!start.node && len > index) {
start.node = node;
start.offset = index;
}

if (!end.node && len >= offset) {
end.node = node;
end.offset = offset;
}

index -= len;
offset -= len;
}

// update the range with the start and end offsets
if (start.node) range.setStart(start.node, start.offset);
if (end.node) range.setEnd(end.node, end.offset);

return range;
}

export default RangeAtIndex;

0 comments on commit 4cd0f24

Please sign in to comment.