mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 07:27:33 +08:00
code refactor
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ref } from 'vue';
|
||||
import { ref, computed } from 'vue';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
|
||||
@@ -28,6 +28,8 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
|
||||
const currentValue = ref<string>(initValue);
|
||||
|
||||
const numeralSystem = computed<NumeralSystem>(() => getCurrentNumeralSystemType());
|
||||
|
||||
function onKeyUpDown(e: KeyboardEvent): void {
|
||||
if (e.altKey || e.ctrlKey || e.metaKey || (e.key.indexOf('F') === 0 && (e.key.length === 2 || e.key.length === 3))
|
||||
|| e.key === 'ArrowLeft' || e.key === 'ArrowRight'
|
||||
@@ -41,11 +43,10 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
return;
|
||||
}
|
||||
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const digitGroupingSymbol = getCurrentDigitGroupingSymbol();
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(e.key) && !numeralSystem.isDigit(e.key) && e.key !== '-' && e.key !== decimalSeparator) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(e.key) && !numeralSystem.value.isDigit(e.key) && e.key !== '-' && e.key !== decimalSeparator) {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
@@ -90,7 +91,7 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
str = (negative ? `-${numeralSystem.digitZero}` : numeralSystem.digitZero) + str;
|
||||
str = (negative ? `-${numeralSystem.value.digitZero}` : numeralSystem.value.digitZero) + str;
|
||||
target.value = str;
|
||||
currentValue.value = target.value;
|
||||
e.preventDefault();
|
||||
@@ -102,14 +103,14 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
|
||||
if (decimalIndex >= 0) {
|
||||
decimalLength = str.length - str.indexOf(decimalSeparator) - 1;
|
||||
} else if ((str.startsWith(numeralSystem.digitZero) && str.length >= 2) || (str.startsWith(`-${numeralSystem.digitZero}`) && str.length >= 3)) {
|
||||
} else if ((str.startsWith(numeralSystem.value.digitZero) && str.length >= 2) || (str.startsWith(`-${numeralSystem.value.digitZero}`) && str.length >= 3)) {
|
||||
const negative = str.charAt(0) === '-';
|
||||
|
||||
if (negative) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
while (str.charAt(0) === numeralSystem.digitZero && (str.length >= 2 || e.key !== numeralSystem.digitZero)) {
|
||||
while (str.charAt(0) === numeralSystem.value.digitZero && (str.length >= 2 || e.key !== numeralSystem.value.digitZero)) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
@@ -142,7 +143,7 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
}
|
||||
} catch (ex) {
|
||||
logger.warn('cannot parse input number, original value is ' + str, ex);
|
||||
target.value = numeralSystem.digitZero;
|
||||
target.value = numeralSystem.value.digitZero;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { watch } from 'vue';
|
||||
import { computed, watch } from 'vue';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
import { type CommonNumberInputProps, useCommonNumberInputBase } from '@/components/base/CommonNumberInputBase.ts';
|
||||
@@ -29,24 +29,25 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
|
||||
onPaste
|
||||
} = useCommonNumberInputBase(props, props.maxDecimalCount ?? -1, getFormattedValue(props.modelValue), parseNumber, getFormattedValue, getValidFormattedValue);
|
||||
|
||||
const numeralSystem = computed<NumeralSystem>(() => getCurrentNumeralSystemType());
|
||||
|
||||
function parseNumber(value: string): number {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
let finalValue = '';
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(value[i]) && !numeralSystem.isDigit(value[i]) && value[i] !== '-' && value[i] !== decimalSeparator) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(value[i]) && !numeralSystem.value.isDigit(value[i]) && value[i] !== '-' && value[i] !== decimalSeparator) {
|
||||
break;
|
||||
}
|
||||
|
||||
finalValue += value[i];
|
||||
}
|
||||
|
||||
finalValue = numeralSystem.replaceLocalizedDigitsToWesternArabicDigits(finalValue);
|
||||
finalValue = numeralSystem.value.replaceLocalizedDigitsToWesternArabicDigits(finalValue);
|
||||
|
||||
if (decimalSeparator !== '.') {
|
||||
finalValue = replaceAll(finalValue, decimalSeparator, '.');
|
||||
@@ -70,36 +71,32 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
|
||||
}
|
||||
|
||||
function getFormattedValue(value: number): string {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
|
||||
if (!Number.isNaN(value) && Number.isFinite(value)) {
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
if (isNumber(props.maxDecimalCount) && props.maxDecimalCount >= 0) {
|
||||
return replaceAll(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(value.toFixed(props.maxDecimalCount)), '.', decimalSeparator);
|
||||
return replaceAll(numeralSystem.value.replaceWesternArabicDigitsToLocalizedDigits(value.toFixed(props.maxDecimalCount)), '.', decimalSeparator);
|
||||
} else {
|
||||
return replaceAll(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(value.toString(10)), '.', decimalSeparator);
|
||||
return replaceAll(numeralSystem.value.replaceWesternArabicDigitsToLocalizedDigits(value.toString(10)), '.', decimalSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
return numeralSystem.digitZero;
|
||||
return numeralSystem.value.digitZero;
|
||||
}
|
||||
|
||||
watch(() => props.modelValue, (newValue) => {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const numericCurrentValue = parseNumber(currentValue.value);
|
||||
|
||||
if (newValue !== numericCurrentValue) {
|
||||
const newStringValue = getFormattedValue(newValue);
|
||||
|
||||
if (!(newStringValue === numeralSystem.digitZero && currentValue.value === '')) {
|
||||
if (!(newStringValue === numeralSystem.value.digitZero && currentValue.value === '')) {
|
||||
currentValue.value = newStringValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
watch(currentValue, (newValue) => {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
let actualNumeralSystem: NumeralSystem | undefined = undefined;
|
||||
let finalValue = '';
|
||||
|
||||
@@ -112,16 +109,16 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
|
||||
actualNumeralSystem = NumeralSystem.detect(newValue[0]);
|
||||
}
|
||||
|
||||
if (actualNumeralSystem && (actualNumeralSystem.type === NumeralSystem.WesternArabicNumerals.type || actualNumeralSystem.type === numeralSystem.type)) {
|
||||
if (actualNumeralSystem && (actualNumeralSystem.type === NumeralSystem.WesternArabicNumerals.type || actualNumeralSystem.type === numeralSystem.value.type)) {
|
||||
for (let i = 0; i < newValue.length; i++) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(newValue[i]) && !numeralSystem.isDigit(newValue[i]) && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(newValue[i]) && !numeralSystem.value.isDigit(newValue[i]) && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
|
||||
break;
|
||||
}
|
||||
|
||||
finalValue += newValue[i];
|
||||
}
|
||||
|
||||
finalValue = numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(finalValue);
|
||||
finalValue = numeralSystem.value.replaceWesternArabicDigitsToLocalizedDigits(finalValue);
|
||||
} else if (newValue === '-' || newValue === decimalSeparator || newValue === `-${decimalSeparator}`) {
|
||||
finalValue = newValue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user