pjc-fields 0.0.54 → 0.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1986,28 +1986,64 @@ class PjcMoedaFieldComponent {
1986
1986
  onKeyDown(event) {
1987
1987
  if (this.disabled || this.readonly())
1988
1988
  return;
1989
+ const input = event.target;
1990
+ const cursorPos = input.selectionStart ?? 0;
1991
+ const displayText = input.value;
1992
+ const digitsBeforeCursor = (displayText.substring(0, cursorPos).match(/\d/g) || []).length;
1993
+ const displayDigits = (displayText.match(/\d/g) || []).join('');
1989
1994
  if (/^\d$/.test(event.key)) {
1990
1995
  event.preventDefault();
1991
- if (this.cents() < 9_999_999_999) {
1992
- this.cents.update(c => c * 10 + Number.parseInt(event.key, 10));
1996
+ const digits = displayDigits.split('');
1997
+ digits.splice(digitsBeforeCursor, 0, event.key);
1998
+ const newCents = Number.parseInt(digits.join(''), 10) || 0;
1999
+ if (newCents <= 9_999_999_999) {
2000
+ this.cents.set(newCents);
1993
2001
  this.emitValue();
2002
+ this.restoreCursor(input, digitsBeforeCursor + 1);
1994
2003
  }
1995
2004
  }
1996
2005
  else if (event.key === 'Backspace') {
1997
2006
  event.preventDefault();
1998
- this.cents.update(c => Math.floor(c / 10));
1999
- this.emitValue();
2007
+ if (digitsBeforeCursor > 0) {
2008
+ const digits = displayDigits.split('');
2009
+ digits.splice(digitsBeforeCursor - 1, 1);
2010
+ this.cents.set(Number.parseInt(digits.join(''), 10) || 0);
2011
+ this.emitValue();
2012
+ this.restoreCursor(input, digitsBeforeCursor - 1);
2013
+ }
2000
2014
  }
2001
2015
  else if (event.key === 'Delete') {
2002
2016
  event.preventDefault();
2003
- this.cents.set(0);
2004
- this.emitValue();
2017
+ if (digitsBeforeCursor < displayDigits.length) {
2018
+ const digits = displayDigits.split('');
2019
+ digits.splice(digitsBeforeCursor, 1);
2020
+ this.cents.set(Number.parseInt(digits.join(''), 10) || 0);
2021
+ this.emitValue();
2022
+ this.restoreCursor(input, digitsBeforeCursor);
2023
+ }
2005
2024
  }
2006
2025
  else if (event.key.length === 1) {
2007
2026
  // Block all other printable characters (letters, symbols, etc.)
2008
2027
  event.preventDefault();
2009
2028
  }
2010
2029
  }
2030
+ restoreCursor(input, targetDigitPos) {
2031
+ requestAnimationFrame(() => {
2032
+ const newValue = input.value;
2033
+ let digitCount = 0;
2034
+ let newPos = newValue.length;
2035
+ for (let i = 0; i < newValue.length; i++) {
2036
+ if (/\d/.test(newValue[i])) {
2037
+ digitCount++;
2038
+ if (digitCount === targetDigitPos) {
2039
+ newPos = i + 1;
2040
+ break;
2041
+ }
2042
+ }
2043
+ }
2044
+ input.setSelectionRange(newPos, newPos);
2045
+ });
2046
+ }
2011
2047
  onPaste(event) {
2012
2048
  event.preventDefault();
2013
2049
  }