Skip to content

Commit

Permalink
Consolidate list formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshino committed Jul 3, 2024
1 parent d707aa1 commit 1054eed
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { TextInput } from '@sveltia/ui';
import { entryDraft } from '$lib/services/contents/draft';
import { getFieldDisplayValue } from '$lib/services/contents/entry';
import { getCanonicalLocale } from '$lib/services/contents/i18n';
import { getListFormatter } from '$lib/services/contents/i18n';
/**
* @type {LocaleCode}
Expand Down Expand Up @@ -55,8 +55,7 @@
$: ({ collectionName, fileName, currentValues } = $entryDraft ?? /** @type {EntryDraft} */ ({}));
$: valueMap = currentValues[locale];
$: canonicalLocale = getCanonicalLocale(locale);
$: listFormatter = new Intl.ListFormat(canonicalLocale, { style: 'narrow', type: 'conjunction' });
$: listFormatter = getListFormatter(locale);
/**
* Update {@link currentValue} based on the current values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { syncExpanderStates } from '$lib/services/contents/draft/editor';
import { updateListField } from '$lib/services/contents/draft/update';
import { getFieldDisplayValue } from '$lib/services/contents/entry';
import { defaultI18nConfig, getCanonicalLocale } from '$lib/services/contents/i18n';
import { defaultI18nConfig, getListFormatter } from '$lib/services/contents/i18n';
/**
* @type {LocaleCode}
Expand Down Expand Up @@ -86,8 +86,7 @@
$: ({ defaultLocale } = (collectionFile ?? collection)?._i18n ?? defaultI18nConfig);
$: isDuplicateField = locale !== defaultLocale && i18n === 'duplicate';
$: valueMap = currentValues[locale];
$: canonicalLocale = getCanonicalLocale(locale);
$: listFormatter = new Intl.ListFormat(canonicalLocale, { style: 'narrow', type: 'conjunction' });
$: listFormatter = getListFormatter(locale);
$: parentExpandedKeyPath = `${keyPath}#`;
$: parentExpanded = expanderStates?._[parentExpandedKeyPath] ?? true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { unflatten } from 'flat';
import FieldPreview from '$lib/components/contents/details/preview/field-preview.svelte';
import { entryDraft } from '$lib/services/contents/draft';
import { getCanonicalLocale } from '$lib/services/contents/i18n';
import { getListFormatter } from '$lib/services/contents/i18n';
/**
* @type {LocaleCode}
Expand Down Expand Up @@ -41,8 +41,7 @@
} = fieldConfig);
$: hasSubFields = !!(field ?? fields ?? types);
$: keyPathRegex = new RegExp(`^${escapeRegExp(keyPath)}\\.\\d+`);
$: canonicalLocale = getCanonicalLocale(locale);
$: listFormatter = new Intl.ListFormat(canonicalLocale, { style: 'narrow', type: 'conjunction' });
$: listFormatter = getListFormatter(locale);
$: items =
unflatten(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { syncExpanderStates } from '$lib/services/contents/draft/editor';
import { copyDefaultLocaleValues } from '$lib/services/contents/draft/update';
import { getFieldDisplayValue } from '$lib/services/contents/entry';
import { defaultI18nConfig, getCanonicalLocale } from '$lib/services/contents/i18n';
import { defaultI18nConfig, getListFormatter } from '$lib/services/contents/i18n';
/**
* @type {LocaleCode}
Expand Down Expand Up @@ -79,8 +79,7 @@
([_keyPath, value]) => !!_keyPath.startsWith(`${keyPath}.`) && value !== null,
);
$: canEdit = locale === defaultLocale || i18n !== false;
$: canonicalLocale = getCanonicalLocale(locale);
$: listFormatter = new Intl.ListFormat(canonicalLocale, { style: 'narrow', type: 'conjunction' });
$: listFormatter = getListFormatter(locale);
$: parentExpandedKeyPath = `${keyPath}#`;
$: parentExpanded = expanderStates?._[parentExpandedKeyPath] ?? true;
$: hasVariableTypes = Array.isArray(types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script>
import { getOptions } from '$lib/components/contents/details/widgets/relation/helper';
import { getEntriesByCollection, getFile } from '$lib/services/contents';
import { getCanonicalLocale } from '$lib/services/contents/i18n';
import { getListFormatter } from '$lib/services/contents/i18n';
/**
* @type {LocaleCode}
Expand Down Expand Up @@ -57,8 +57,7 @@
return value;
});
$: canonicalLocale = getCanonicalLocale(locale);
$: listFormatter = new Intl.ListFormat(canonicalLocale, { style: 'narrow', type: 'conjunction' });
$: listFormatter = getListFormatter(locale);
</script>
{#if refValues.length}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<script>
import { isObjectArray } from '@sveltia/utils/object';
import { getCanonicalLocale } from '$lib/services/contents/i18n';
import { getListFormatter } from '$lib/services/contents/i18n';
/**
* @type {LocaleCode}
Expand All @@ -27,8 +27,7 @@
$: ({ options, multiple } = fieldConfig);
$: hasLabels = isObjectArray(options);
$: canonicalLocale = getCanonicalLocale(locale);
$: listFormatter = new Intl.ListFormat(canonicalLocale, { style: 'narrow', type: 'conjunction' });
$: listFormatter = getListFormatter(locale);
/**
* Get the display label by value.
Expand Down
13 changes: 13 additions & 0 deletions src/lib/services/contents/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,16 @@ export const getLocaleLabel = (locale) => {
return locale;
}
};

/**
* Get a simple list formatter.
* @param {LocaleCode} locale - Locale code.
* @param {Partial<Intl.ListFormatOptions>} options - Format options.
* @returns {Intl.ListFormat} Formatter.
*/
export const getListFormatter = (locale, options = {}) =>
new Intl.ListFormat(getCanonicalLocale(locale), {
style: 'narrow',
type: 'conjunction',
...options,
});

0 comments on commit 1054eed

Please sign in to comment.