Skip to content

Commit

Permalink
fix: cursor pos bug + minor refact #33
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Z Király authored and Lev Z Király committed Nov 24, 2023
1 parent 129c63a commit 46ca4e4
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions components/input-extended.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,29 @@ const specChars = [
];
let cursorPosition = 0;
const Type = (htmlChar: string): void => {
const InsertChar = async (htmlChar: string): Promise<void> => {
let decodedChar = parseHtmlEntities(htmlChar);
myString.value =
myString.value.substring(0, cursorPosition) +
decodedChar +
myString.value.substring(cursorPosition, myString.value.length);
cursorPosition += decodedChar.length;
await nextTick();
setCursorPosition(cursorPosition);
};
const setCursorPosition = (pos: number) => {
if (inputElement.value.createTextRange) {
if (typeof inputElement.value.createTextRange !== "undefined") {
var range = inputElement.value.createTextRange();
range.move("character", pos);
range.select();
}
if (inputElement.value.selectionStart) {
} else if (typeof inputElement.value.setSelectionRange !== "undefined") {
inputElement.value.focus();
inputElement.value.setSelectionRange(pos, pos);
} else if (typeof inputElement.value.selectionStart !== "undefined") {
inputElement.value.focus();
inputElement.value.selectionStart = pos;
inputElement.value.selectionEnd = pos;
}
};
Expand All @@ -68,7 +72,7 @@ function parseHtmlEntities(str: string) {
}
const registerCursorPosition = (e: Event) => {
cursorPosition = e.target.selectionStart;
cursorPosition = Number((e.target as HTMLInputElement).selectionStart);
};
</script>

Expand All @@ -79,7 +83,7 @@ const registerCursorPosition = (e: Event) => {
v-for="(c, i) in specChars"
:key="i"
class="bg-gray-300 px-4 py-2 font-bold text-gray-800 hover:bg-gray-400"
@click="Type(c)"
@click="InsertChar(c)"
v-html="c"
></button>
</div>
Expand Down

0 comments on commit 46ca4e4

Please sign in to comment.