mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 23:47:33 +08:00
support changing numeral system
This commit is contained in:
@@ -2,6 +2,8 @@ import { ref } from 'vue';
|
||||
|
||||
import { useI18n } from '@/locales/helpers.ts';
|
||||
|
||||
import { NumeralSystem } from '@/core/numeral.ts';
|
||||
|
||||
import { removeAll } from '@/lib/common.ts';
|
||||
import logger from '@/lib/logger.ts';
|
||||
|
||||
@@ -19,6 +21,7 @@ export type GetValidFormattedValueFunction = (value: number, textualValue: strin
|
||||
|
||||
export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecimalCount: number, initValue: string, parseNumber: ParseNumberFunction, formatNumber: FormatNumberFunction, getValidFormattedValue: GetValidFormattedValueFunction) {
|
||||
const {
|
||||
getCurrentNumeralSystemType,
|
||||
getCurrentDecimalSeparator,
|
||||
getCurrentDigitGroupingSymbol
|
||||
} = useI18n();
|
||||
@@ -38,10 +41,11 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
return;
|
||||
}
|
||||
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const digitGroupingSymbol = getCurrentDigitGroupingSymbol();
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
if (!('0' <= e.key && e.key <= '9') && e.key !== '-' && e.key !== decimalSeparator) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(e.key) && !numeralSystem.isDigit(e.key) && e.key !== '-' && e.key !== decimalSeparator) {
|
||||
e.preventDefault();
|
||||
return;
|
||||
}
|
||||
@@ -86,7 +90,7 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
str = (negative ? '-0' : '0') + str;
|
||||
str = (negative ? `-${numeralSystem.digitZero}` : numeralSystem.digitZero) + str;
|
||||
target.value = str;
|
||||
currentValue.value = target.value;
|
||||
e.preventDefault();
|
||||
@@ -98,14 +102,14 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
|
||||
if (decimalIndex >= 0) {
|
||||
decimalLength = str.length - str.indexOf(decimalSeparator) - 1;
|
||||
} else if ((str.startsWith('0') && str.length >= 2) || (str.startsWith('-0') && str.length >= 3)) {
|
||||
} else if ((str.startsWith(numeralSystem.digitZero) && str.length >= 2) || (str.startsWith(`-${numeralSystem.digitZero}`) && str.length >= 3)) {
|
||||
const negative = str.charAt(0) === '-';
|
||||
|
||||
if (negative) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
while (str.charAt(0) === '0' && (str.length >= 2 || e.key !== '0')) {
|
||||
while (str.charAt(0) === numeralSystem.digitZero && (str.length >= 2 || e.key !== numeralSystem.digitZero)) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
|
||||
@@ -138,7 +142,7 @@ export function useCommonNumberInputBase(props: CommonNumberInputProps, maxDecim
|
||||
}
|
||||
} catch (ex) {
|
||||
logger.warn('cannot parse input number, original value is ' + str, ex);
|
||||
target.value = '0';
|
||||
target.value = numeralSystem.digitZero;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import { useI18n } from '@/locales/helpers.ts';
|
||||
import { type CommonNumberInputProps, useCommonNumberInputBase } from '@/components/base/CommonNumberInputBase.ts';
|
||||
|
||||
import { isNumber, replaceAll, removeAll } from '@/lib/common.ts';
|
||||
import { NumeralSystem } from '@/core/numeral.ts';
|
||||
|
||||
export interface NumberInputProps extends CommonNumberInputProps {
|
||||
minValue?: number;
|
||||
@@ -17,6 +18,7 @@ export interface NumberInputEmits {
|
||||
|
||||
export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmits) {
|
||||
const {
|
||||
getCurrentNumeralSystemType,
|
||||
getCurrentDecimalSeparator,
|
||||
getCurrentDigitGroupingSymbol
|
||||
} = useI18n();
|
||||
@@ -32,17 +34,20 @@ export function useNumberInputBase(props: NumberInputProps, emit: NumberInputEmi
|
||||
return 0;
|
||||
}
|
||||
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
let finalValue = '';
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (!('0' <= value[i] && value[i] <= '9') && value[i] !== '-' && value[i] !== decimalSeparator) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(value[i]) && !numeralSystem.isDigit(value[i]) && value[i] !== '-' && value[i] !== decimalSeparator) {
|
||||
break;
|
||||
}
|
||||
|
||||
finalValue += value[i];
|
||||
}
|
||||
|
||||
finalValue = numeralSystem.replaceLocalizedDigitsToWesternArabicDigits(finalValue);
|
||||
|
||||
if (decimalSeparator !== '.') {
|
||||
finalValue = replaceAll(finalValue, decimalSeparator, '.');
|
||||
}
|
||||
@@ -65,50 +70,72 @@ 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(value.toFixed(props.maxDecimalCount), '.', decimalSeparator);
|
||||
return replaceAll(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(value.toFixed(props.maxDecimalCount)), '.', decimalSeparator);
|
||||
} else {
|
||||
return replaceAll(value.toString(), '.', decimalSeparator);
|
||||
return replaceAll(numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(value.toString(10)), '.', decimalSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
return '0';
|
||||
return numeralSystem.digitZero;
|
||||
}
|
||||
|
||||
watch(() => props.modelValue, (newValue) => {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
const numericCurrentValue = parseNumber(currentValue.value);
|
||||
|
||||
if (newValue !== numericCurrentValue) {
|
||||
const newStringValue = getFormattedValue(newValue);
|
||||
|
||||
if (!(newStringValue === '0' && currentValue.value === '')) {
|
||||
if (!(newStringValue === numeralSystem.digitZero && currentValue.value === '')) {
|
||||
currentValue.value = newStringValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
watch(currentValue, (newValue) => {
|
||||
const numeralSystem = getCurrentNumeralSystemType();
|
||||
let actualNumeralSystem: NumeralSystem | undefined = undefined;
|
||||
let finalValue = '';
|
||||
|
||||
if (newValue) {
|
||||
const decimalSeparator = getCurrentDecimalSeparator();
|
||||
|
||||
for (let i = 0; i < newValue.length; i++) {
|
||||
if (!('0' <= newValue[i] && newValue[i] <= '9') && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
|
||||
break;
|
||||
if (newValue[0] === '-' || newValue[0] === decimalSeparator) {
|
||||
actualNumeralSystem = NumeralSystem.detect(newValue[1]);
|
||||
} else {
|
||||
actualNumeralSystem = NumeralSystem.detect(newValue[0]);
|
||||
}
|
||||
|
||||
if (actualNumeralSystem && (actualNumeralSystem.type === NumeralSystem.WesternArabicNumerals.type || actualNumeralSystem.type === numeralSystem.type)) {
|
||||
for (let i = 0; i < newValue.length; i++) {
|
||||
if (!NumeralSystem.WesternArabicNumerals.isDigit(newValue[i]) && !numeralSystem.isDigit(newValue[i]) && newValue[i] !== '-' && newValue[i] !== decimalSeparator) {
|
||||
break;
|
||||
}
|
||||
|
||||
finalValue += newValue[i];
|
||||
}
|
||||
|
||||
finalValue += newValue[i];
|
||||
finalValue = numeralSystem.replaceWesternArabicDigitsToLocalizedDigits(finalValue);
|
||||
} else if (newValue === '-' || newValue === decimalSeparator || newValue === `-${decimalSeparator}`) {
|
||||
finalValue = newValue;
|
||||
}
|
||||
}
|
||||
|
||||
if (finalValue !== newValue) {
|
||||
currentValue.value = finalValue;
|
||||
} else {
|
||||
const value: number = parseNumber(finalValue);
|
||||
let value: number = parseNumber(finalValue);
|
||||
|
||||
if (Number.isNaN(value) || !Number.isFinite(value)) {
|
||||
value = 0;
|
||||
}
|
||||
|
||||
emit('update:modelValue', value);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -35,7 +35,7 @@ export interface CommonPieChartProps {
|
||||
}
|
||||
|
||||
export function usePieChartBase(props: CommonPieChartProps) {
|
||||
const { formatAmountWithCurrency, formatPercent } = useI18n();
|
||||
const { formatAmountToLocalizedNumeralsWithCurrency, formatPercentToLocalizedNumerals } = useI18n();
|
||||
|
||||
const selectedIndex = ref<number>(0);
|
||||
|
||||
@@ -72,8 +72,8 @@ export function usePieChartBase(props: CommonPieChartProps) {
|
||||
sourceItem: item
|
||||
};
|
||||
|
||||
finalItem.displayPercent = formatPercent(finalItem.percent, 2, '<0.01');
|
||||
finalItem.displayValue = formatAmountWithCurrency(finalItem.value, props.defaultCurrency);
|
||||
finalItem.displayPercent = formatPercentToLocalizedNumerals(finalItem.percent, 2, '<0.01');
|
||||
finalItem.displayValue = formatAmountToLocalizedNumeralsWithCurrency(finalItem.value, props.defaultCurrency);
|
||||
|
||||
validItems.push(finalItem);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user