better formating

This commit is contained in:
Jan Prochazka
2025-12-03 18:21:04 +01:00
parent 910f9cadfe
commit 30b4c85c5a
2 changed files with 126 additions and 115 deletions

View File

@@ -45,14 +45,15 @@ export function hexStringToArray(inputString) {
export function base64ToHex(base64String) { export function base64ToHex(base64String) {
const binaryString = atob(base64String); const binaryString = atob(base64String);
const hexString = Array.from(binaryString, c => const hexString = Array.from(binaryString, c => c.charCodeAt(0).toString(16).padStart(2, '0')).join('');
c.charCodeAt(0).toString(16).padStart(2, '0')
).join('');
return '0x' + hexString.toUpperCase(); return '0x' + hexString.toUpperCase();
}; }
export function hexToBase64(hexString) { export function hexToBase64(hexString) {
const binaryString = hexString.match(/.{1,2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join(''); const binaryString = hexString
.match(/.{1,2}/g)
.map(byte => String.fromCharCode(parseInt(byte, 16)))
.join('');
return btoa(binaryString); return btoa(binaryString);
} }
@@ -68,9 +69,9 @@ export function parseCellValue(value, editorTypes?: DataEditorTypesBehaviour) {
if (mHex) { if (mHex) {
return { return {
$binary: { $binary: {
base64: hexToBase64(value.substring(2)) base64: hexToBase64(value.substring(2)),
} },
} };
} }
} }
@@ -206,6 +207,20 @@ function formatNumberCustomSeparator(value, thousandsSeparator) {
return decPart ? `${intPartWithSeparator}.${decPart}` : intPartWithSeparator; return decPart ? `${intPartWithSeparator}.${decPart}` : intPartWithSeparator;
} }
function formatCellNumber(value, gridFormattingOptions?: { thousandsSeparator?: string }) {
const separator = gridFormattingOptions?.thousandsSeparator;
if (_isNumber(value)) {
if (separator === 'none' || (value < 1000 && value > -1000)) return value.toString();
if (separator === 'system') return value.toLocaleString();
}
// fallback for system locale
if (separator === 'space' || separator === 'system') return formatNumberCustomSeparator(value.toString(), ' ');
if (separator === 'narrowspace') return formatNumberCustomSeparator(value.toString(), '\u202F');
if (separator === 'comma') return formatNumberCustomSeparator(value.toString(), ',');
if (separator === 'dot') return formatNumberCustomSeparator(value.toString(), '.');
return value.toString();
}
export function stringifyCellValue( export function stringifyCellValue(
value, value,
intent: intent:
@@ -276,13 +291,13 @@ export function stringifyCellValue(
} }
if (value?.$bigint) { if (value?.$bigint) {
return { return {
value: value.$bigint, value: formatCellNumber(value.$bigint, gridFormattingOptions),
gridStyle: 'valueCellStyle', gridStyle: 'valueCellStyle',
}; };
} }
if (typeof value === 'bigint') { if (typeof value === 'bigint') {
return { return {
value: value.toString(), value: formatCellNumber(value.toString(), gridFormattingOptions),
gridStyle: 'valueCellStyle', gridStyle: 'valueCellStyle',
}; };
} }
@@ -358,14 +373,7 @@ export function stringifyCellValue(
switch (intent) { switch (intent) {
case 'gridCellIntent': case 'gridCellIntent':
const separator = gridFormattingOptions?.thousandsSeparator; const separator = gridFormattingOptions?.thousandsSeparator;
let formattedValue; return { value: formatCellNumber(value, gridFormattingOptions), gridStyle: 'valueCellStyle' };
if (separator === 'none' || (value < 1000 && value > -1000)) formattedValue = value.toString();
else if (separator === 'system') formattedValue = value.toLocaleString();
else if (separator === 'space') formattedValue = formatNumberCustomSeparator(value.toString(), ' ');
else if (separator === 'nobreakspace') formattedValue = formatNumberCustomSeparator(value.toString(), '\u202F');
else if (separator === 'comma') formattedValue = formatNumberCustomSeparator(value.toString(), ',');
else if (separator === 'dot') formattedValue = formatNumberCustomSeparator(value.toString(), '.');
return { value: formattedValue, gridStyle: 'valueCellStyle' };
default: default:
return { value: value.toString() }; return { value: value.toString() };
} }

View File

@@ -1,11 +1,9 @@
<script lang="ts"> <script lang="ts">
import FormCheckboxField from "../forms/FormCheckboxField.svelte"; import FormCheckboxField from '../forms/FormCheckboxField.svelte';
import FormSelectField from "../forms/FormSelectField.svelte"; import FormSelectField from '../forms/FormSelectField.svelte';
import FormTextField from "../forms/FormTextField.svelte"; import FormTextField from '../forms/FormTextField.svelte';
import { _t } from "../translations"; import { _t } from '../translations';
import { isProApp } from "../utility/proTools"; import { isProApp } from '../utility/proTools';
</script> </script>
<div class="wrapper"> <div class="wrapper">
@@ -31,12 +29,17 @@ defaultValue="100"
label={_t('settings.dataGrid.thousandsSeparator', { defaultMessage: 'Thousands separator for numbers' })} label={_t('settings.dataGrid.thousandsSeparator', { defaultMessage: 'Thousands separator for numbers' })}
name="dataGrid.thousandsSeparatorChar" name="dataGrid.thousandsSeparatorChar"
isNative isNative
defaultValue='none' defaultValue="none"
options={[ options={[
{ value: 'none', label: _t('settings.dataGrid.thousandsSeparator.none', { defaultMessage: 'None' }) }, { value: 'none', label: _t('settings.dataGrid.thousandsSeparator.none', { defaultMessage: 'None' }) },
{ value: 'system', label: _t('settings.dataGrid.thousandsSeparator.system', { defaultMessage: 'System' }) }, { value: 'system', label: _t('settings.dataGrid.thousandsSeparator.system', { defaultMessage: 'System' }) },
{ value: 'space', label: _t('settings.dataGrid.thousandsSeparator.space', { defaultMessage: 'Space' }) }, { value: 'space', label: _t('settings.dataGrid.thousandsSeparator.space', { defaultMessage: 'Space' }) },
{ value: 'nobreakspace', label: _t('settings.dataGrid.thousandsSeparator.narrowNoBreakSpace', { defaultMessage: 'Narrow no-break space' }) }, {
value: 'narrowspace',
label: _t('settings.dataGrid.thousandsSeparator.narrowSpace', {
defaultMessage: 'Narrow space',
}),
},
{ value: 'comma', label: _t('settings.dataGrid.thousandsSeparator.comma', { defaultMessage: 'Comma (,)' }) }, { value: 'comma', label: _t('settings.dataGrid.thousandsSeparator.comma', { defaultMessage: 'Comma (,)' }) },
{ value: 'dot', label: _t('settings.dataGrid.thousandsSeparator.dot', { defaultMessage: 'Dot (.)' }) }, { value: 'dot', label: _t('settings.dataGrid.thousandsSeparator.dot', { defaultMessage: 'Dot (.)' }) },
]} ]}