Skip to content

Commit

Permalink
Fix the situation where the cursor cannot be positioned under some s…
Browse files Browse the repository at this point in the history
…pecial types of input

Fix the situation where the cursor cannot be positioned under some special types of input
  • Loading branch information
lindelof committed Dec 27, 2019
1 parent a4d9982 commit d32e4d6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ npm install --save power-mode-input
## Usage

```jsx
import PowerModeInput from 'power-mode-input';
import PowerModeInput from "power-mode-input";

...
PowerModeInput.make(document.getElementById("obinput"));
const input = document.getElementById("obinput");
PowerModeInput.make(input);

// or
// close PowerModeInput
PowerModeInput.close(input);

// destroy PowerModeInput
PowerModeInput.destroy();

// another usage
PowerModeInput.make(".phone", {
height: 5,
tha: [0, 360],
Expand Down Expand Up @@ -88,6 +94,9 @@ key | describe |type | example

---

## Explanation
For some special types of input boxes, the cursor may not be positioned correctly. I am also looking for a solution to this problem. If you know, please let me know. [See here for specific reasons](https://stackoverflow.com/questions/21177489/selectionstart-selectionend-on-input-type-number-no-longer-allowed-in-chrome)

## Contribution
I very much hope that you can work with me to modify the code. I also have a lot of fun ideas. Maybe you can join me to implement it.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "power-mode-input",
"version": "1.0.1",
"version": "1.0.2",
"description": "Make your input box input very shocking",
"author": "lindelof",
"license": "MIT",
Expand Down
47 changes: 45 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,52 @@ const getGlobalPosition = element => {

const getCursorPosition = e => {
const { currentTarget: input } = e;
const { selectionEnd } = input;
const { start, end } = getSelection(input);
return getCursorXY(input, end);
};

const getSelection = el => {
let start = 0,
end = 0,
normalizedValue,
range,
textInputRange,
len,
endRange;

if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else if (document.selection) {
range = document.selection.createRange();

if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
endRange = el.createTextRange();
endRange.collapse(false);

if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;

if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
} else {
start = end = el.value.length;
}

return getCursorXY(input, selectionEnd);
return { start, end };
};

export default PowerModeInput;

0 comments on commit d32e4d6

Please sign in to comment.