Skip to content

Commit

Permalink
fix #512
Browse files Browse the repository at this point in the history
  • Loading branch information
uNmAnNeR committed Jul 19, 2023
1 parent 57c718c commit b09b2da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
37 changes: 21 additions & 16 deletions packages/imask/example.html
Expand Up @@ -3,7 +3,7 @@

<body>
<h1>IMask Core Demo</h1>
<input type="text" id="input" value="123">
<input type="text" id="input" value="0000">
<div>value: <span id="value"></span></div>
<div>unmasked: <span id="unmasked"></span></div>
<div contenteditable="true" id="ce"></div>
Expand All @@ -12,31 +12,36 @@ <h1>IMask Core Demo</h1>
<!-- <script src="https://unpkg.com/imask"></script> -->
<script type="text/javascript">
const opts = {
mask: `d $`,
blocks: {
d: {
mask: Number,
scale: 2,
signed: false,
thousandsSeparator: " ",
padFractionalZeros: false,
normalizeZeros: false,
radix: ",",
mapToRadix: [".", "б", "^", "ю", "&"]
}
},
lazy: false
mask: /^.*$/
};

const input = document.getElementById('input');
var result = document.getElementById('value');
var unmasked = document.getElementById('unmasked');
var imask = IMask(document.getElementById('input'), opts).on('accept', () => {
var imask = IMask(input, opts).on('accept', () => {
console.log(imask.value, imask.unmaskedValue, imask.typedValue);
result.innerHTML = imask.value;
unmasked.innerHTML = imask.unmaskedValue;
});


setTimeout(() => {
//HMR does pretty much this:
input.value = 1111
dispatchEvents(input)
}, 1000)

//straight from Angular's hmr-accept.js
function dispatchEvents() {
input.dispatchEvent(new Event('input', {
bubbles: true,
cancelable: true,
}));
input.blur();
input.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter' }));
}


// document.getElementById('input').addEventListener('focus', () => {
// imask.updateOptions({
// mask: mask.map((m) => ({
Expand Down
3 changes: 0 additions & 3 deletions packages/imask/src/controls/input.ts
Expand Up @@ -288,9 +288,6 @@ class InputMask<Opts extends FactoryArg> {
this._inputEvent = e;
this._abortUpdateCursor();

// fix strange IE behavior
if (!this._selection) return this.updateValue();

const details = new ActionDetails({
// new state
value: this.el.value,
Expand Down
6 changes: 6 additions & 0 deletions packages/imask/src/core/action-details.ts
Expand Up @@ -28,6 +28,12 @@ class ActionDetails {
while (this.value.slice(0, this.startChangePos) !== this.oldValue.slice(0, this.startChangePos)) {
--this.oldSelection.start;
}

// double check right part
while (this.value.slice(this.cursorPos) !== this.oldValue.slice(this.oldSelection.end)) {
if (this.value.length - this.cursorPos < this.oldValue.length - this.oldSelection.end) ++this.oldSelection.end;
else ++this.cursorPos;
}
}

/** Start changing position */
Expand Down

1 comment on commit b09b2da

@pytkin
Copy link

@pytkin pytkin commented on b09b2da Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code fixes my current bug with autofilling. Can you please make a new release with this changes @uNmAnNeR?

Please sign in to comment.