diff --git a/packages/abc/cell/cell.service.ts b/packages/abc/cell/cell.service.ts index 2210a5059..d319d8c50 100644 --- a/packages/abc/cell/cell.service.ts +++ b/packages/abc/cell/cell.service.ts @@ -3,7 +3,7 @@ import { DomSanitizer } from '@angular/platform-browser'; import { map, Observable, of } from 'rxjs'; import { yn } from '@delon/theme'; -import { AlainCellConfig, AlainConfigService } from '@delon/util/config'; +import { AlainConfigService } from '@delon/util/config'; import { formatDate } from '@delon/util/date-time'; import { CurrencyService, formatMask } from '@delon/util/format'; import { deepMerge } from '@delon/util/other'; @@ -25,12 +25,22 @@ export class CellService { private readonly nzI18n = inject(NzI18nService); private readonly currency = inject(CurrencyService); private readonly dom = inject(DomSanitizer); - private globalOptions!: AlainCellConfig; + private readonly configSrv = inject(AlainConfigService); + private globalOptions = this.configSrv.merge('cell', { + date: { format: 'yyyy-MM-dd HH:mm:ss' }, + img: { size: 32 }, + default: { text: '-' } + })!; private widgets: { [key: string]: CellWidget } = { date: { type: 'fn', ref: (value, opt) => { - return { text: formatDate(value as string, opt.date!.format!, this.nzI18n.getDateLocale()) }; + return { + text: formatDate(value as string, opt.date!.format!, { + locale: this.nzI18n.getDateLocale(), + customFormat: this.configSrv.get('themePipe')?.dateFormatCustom + }) + }; } }, mega: { @@ -66,14 +76,6 @@ export class CellService { } }; - constructor(configSrv: AlainConfigService) { - this.globalOptions = configSrv.merge('cell', { - date: { format: 'yyyy-MM-dd HH:mm:ss' }, - img: { size: 32 }, - default: { text: '-' } - })!; - } - registerWidget(key: string, widget: Type): void { this.widgets[key] = { type: 'widget', ref: widget }; } diff --git a/packages/theme/src/pipes/date/date.pipe.ts b/packages/theme/src/pipes/date/date.pipe.ts index c23936b62..4b1564280 100644 --- a/packages/theme/src/pipes/date/date.pipe.ts +++ b/packages/theme/src/pipes/date/date.pipe.ts @@ -11,8 +11,10 @@ export class DatePipe implements PipeTransform { transform(value: Date | string | number, formatString?: string | null): string { const formatStr = formatString ?? this.cog?.dateFormat ?? 'yyyy-MM-dd HH:mm'; - if (this.cog?.custom) return this.cog.custom(value, formatStr); - return formatDate(value, formatStr, this.nzI18n.getDateLocale()); + return formatDate(value, formatStr, { + locale: this.nzI18n.getDateLocale(), + customFormat: this.cog?.dateFormatCustom + }); } } diff --git a/packages/util/config/theme/index.ts b/packages/util/config/theme/index.ts index 8ba6b19c7..e30d7107d 100644 --- a/packages/util/config/theme/index.ts +++ b/packages/util/config/theme/index.ts @@ -1,3 +1,4 @@ export * from './http.type'; export * from './responsive.type'; export * from './i18n.type'; +export * from './pipe.type'; diff --git a/packages/util/config/theme/pipe.type.ts b/packages/util/config/theme/pipe.type.ts index 0e0687e5b..d3771ae1d 100644 --- a/packages/util/config/theme/pipe.type.ts +++ b/packages/util/config/theme/pipe.type.ts @@ -1,4 +1,10 @@ export interface AlainThemePipeConfig { dateFormat?: string; - custom?: (value: Date | string | number, formatString?: string | null) => string; + dateFormatCustom?: AlainThemePipeDateFormatCustom; } + +export type AlainThemePipeDateFormatCustom = ( + value: Date | string | number, + formatString?: string | null, + options?: { locale?: Locale } +) => string; diff --git a/packages/util/date-time/time.ts b/packages/util/date-time/time.ts index 726f25c2d..03de48943 100644 --- a/packages/util/date-time/time.ts +++ b/packages/util/date-time/time.ts @@ -17,6 +17,7 @@ import { formatDistanceToNow } from 'date-fns'; +import type { AlainThemePipeDateFormatCustom } from '@delon/util/config'; import type { NzSafeAny } from 'ng-zorro-antd/core/types'; import { DateLocale } from 'ng-zorro-antd/i18n'; @@ -129,10 +130,14 @@ export function toDate(value?: Date | string | number | null, options?: string | * @param formatString Please refer to [date-fnd format](https://date-fns.org/v2.30.0/docs/format) for string format * @param dateLocale Recommended to be consistent with NG-ZORRO by using `inject(NZ_DATE_LOCALE)` */ -export function formatDate(value: Date | string | number, formatString: string, dateLocale?: DateLocale): string { +export function formatDate( + value: Date | string | number, + formatString: string, + options?: { locale?: DateLocale; customFormat?: AlainThemePipeDateFormatCustom } +): string { value = toDate(value); if (isNaN(value as NzSafeAny)) return ''; - const langOpt = { locale: dateLocale }; + const langOpt = { locale: options?.locale }; return formatString === 'fn' ? formatDistanceToNow(value, langOpt) : format(value, formatString, langOpt); }