code refactor

This commit is contained in:
MaysWind
2025-08-29 00:43:34 +08:00
parent 8f6adaa417
commit b79ffafaee
7 changed files with 49 additions and 54 deletions
+8 -7
View File
@@ -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;
}
}
+12 -15
View File
@@ -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;
}
+6 -5
View File
@@ -48,9 +48,12 @@ const emit = defineEmits<{
const { getCurrentNumeralSystemType } = useI18n();
const codes = ref<PinCode[]>([]);
const pinCodeInputs = useTemplateRef<HTMLInputElement[]>('pin-code-input');
const codes = ref<PinCode[]>([]);
const numeralSystem = computed<NumeralSystem>(() => getCurrentNumeralSystemType());
const finalPinCode = computed<string>(() => {
let ret = '';
@@ -154,8 +157,6 @@ function setNextFocus(index: number): void {
}
function onKeydown(index: number, event: KeyboardEvent): void {
const numeralSystem = getCurrentNumeralSystemType();
if (event.altKey || (event.key.indexOf('F') === 0 && (event.key.length === 2 || event.key.length === 3))) {
return;
}
@@ -221,8 +222,8 @@ function onKeydown(index: number, event: KeyboardEvent): void {
if (NumeralSystem.WesternArabicNumerals.isDigit(event.key)) {
digit = event.key;
} else if (numeralSystem.isDigit(event.key)) {
digit = numeralSystem.replaceLocalizedDigitsToWesternArabicDigits(event.key);
} else if (numeralSystem.value.isDigit(event.key)) {
digit = numeralSystem.value.replaceLocalizedDigitsToWesternArabicDigits(event.key);
}
if (digit) {
+10 -15
View File
@@ -152,6 +152,8 @@ const rules = [
const currentFormula = ref<string>('');
const formulaMode = ref<boolean>(false);
const numeralSystem = computed<NumeralSystem>(() => getCurrentNumeralSystemType());
const prependText = computed<string | undefined>(() => {
if (!props.currency || !props.showCurrency) {
return '';
@@ -205,7 +207,6 @@ function enterFormulaMode(): void {
function calculateFormula(): void {
const systemDecimalSeparator = DecimalSeparator.Dot.symbol;
const numeralSystem = getCurrentNumeralSystemType();
const decimalSeparator = getCurrentDecimalSeparator();
let finalFormula = currentFormula.value;
@@ -216,7 +217,7 @@ function calculateFormula(): void {
finalFormula = replaceAll(currentFormula.value, decimalSeparator, systemDecimalSeparator);
}
finalFormula = numeralSystem.replaceLocalizedDigitsToWesternArabicDigits(finalFormula);
finalFormula = numeralSystem.value.replaceLocalizedDigitsToWesternArabicDigits(finalFormula);
const calculatedValue = evaluateExpression(finalFormula);
if (isNumber(calculatedValue)) {
@@ -275,13 +276,11 @@ function getInitedFormattedValue(value: number, flipNegative?: boolean): string
}
function getFormattedValue(value: number): string {
const numeralSystem = getCurrentNumeralSystemType();
if (!Number.isNaN(value) && Number.isFinite(value)) {
return formatAmountToLocalizedNumeralsWithoutDigitGrouping(value, props.currency);
}
return numeralSystem.digitZero;
return numeralSystem.value.digitZero;
}
function getDisplayCurrencyPrependAndAppendText(): CurrencyPrependAndAppendText | null {
@@ -292,19 +291,17 @@ function getDisplayCurrencyPrependAndAppendText(): CurrencyPrependAndAppendText
}
watch(() => props.currency, () => {
const numeralSystem = getCurrentNumeralSystemType();
const newStringValue = getInitedFormattedValue(props.modelValue, props.flipNegative);
if (!(newStringValue === numeralSystem.digitZero && currentValue.value === '')) {
if (!(newStringValue === numeralSystem.value.digitZero && currentValue.value === '')) {
currentValue.value = newStringValue;
}
});
watch(() => props.flipNegative, (newValue) => {
const numeralSystem = getCurrentNumeralSystemType();
const newStringValue = getInitedFormattedValue(props.modelValue, newValue);
if (!(newStringValue === numeralSystem.digitZero && currentValue.value === '')) {
if (!(newStringValue === numeralSystem.value.digitZero && currentValue.value === '')) {
currentValue.value = newStringValue;
}
});
@@ -314,20 +311,18 @@ watch(() => props.modelValue, (newValue) => {
newValue = -newValue;
}
const numeralSystem = getCurrentNumeralSystemType();
const numericCurrentValue = parseAmountFromLocalizedNumerals(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 = '';
@@ -340,16 +335,16 @@ watch(currentValue, (newValue) => {
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;
}
+4 -3
View File
@@ -108,6 +108,8 @@ const previousValue = ref<string>('');
const currentSymbol = ref<string>('');
const currentValue = ref<string>(getInitedStringValue(props.modelValue, props.flipNegative));
const numeralSystem = computed<NumeralSystem>(() => getCurrentNumeralSystemType());
const digits = computed<string[]>(() => getAllLocalizedDigits());
const decimalSeparator = computed<string>(() => getCurrentDecimalSeparator());
@@ -120,8 +122,7 @@ const supportDecimalSeparator = computed<boolean>(() => {
});
const currentDisplay = computed<string>(() => {
const numeralSystem = getCurrentNumeralSystemType();
const finalPreviousValue = appendDigitGroupingSymbolAndDecimalSeparator(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(previousValue.value));
const finalPreviousValue = appendDigitGroupingSymbolAndDecimalSeparator(numeralSystem.value.replaceWesternArabicDigitsToLocalizedDigits(previousValue.value));
let finalCurrentValue = currentValue.value;
let currentValueSuffix = '';
@@ -130,7 +131,7 @@ const currentDisplay = computed<string>(() => {
currentValueSuffix = decimalSeparator.value;
}
finalCurrentValue = appendDigitGroupingSymbolAndDecimalSeparator(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(finalCurrentValue));
finalCurrentValue = appendDigitGroupingSymbolAndDecimalSeparator(numeralSystem.value.replaceWesternArabicDigitsToLocalizedDigits(finalCurrentValue));
if (currentValueSuffix) {
finalCurrentValue += currentValueSuffix;