From fe1a78610382a82a018e99c2714538a3679d3bec Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 2 Aug 2024 22:00:22 +0800 Subject: [PATCH 1/5] feat(theme): support custom process of `_date` pipe --- packages/theme/src/pipes/date/date.pipe.ts | 7 +++++-- packages/util/config/theme/pipe.type.ts | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/theme/src/pipes/date/date.pipe.ts b/packages/theme/src/pipes/date/date.pipe.ts index 338eef413..c23936b62 100644 --- a/packages/theme/src/pipes/date/date.pipe.ts +++ b/packages/theme/src/pipes/date/date.pipe.ts @@ -7,9 +7,12 @@ import { NzI18nService } from 'ng-zorro-antd/i18n'; @Pipe({ name: '_date', standalone: true }) export class DatePipe implements PipeTransform { private nzI18n = inject(NzI18nService); - private defFormat = inject(AlainConfigService).get('themePipe')?.dateFormat ?? 'yyyy-MM-dd HH:mm'; + private cog = inject(AlainConfigService).get('themePipe'); transform(value: Date | string | number, formatString?: string | null): string { - return formatDate(value, formatString ?? this.defFormat, this.nzI18n.getDateLocale()); + 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()); } } diff --git a/packages/util/config/theme/pipe.type.ts b/packages/util/config/theme/pipe.type.ts index 546ebb8bd..0e0687e5b 100644 --- a/packages/util/config/theme/pipe.type.ts +++ b/packages/util/config/theme/pipe.type.ts @@ -1,3 +1,4 @@ export interface AlainThemePipeConfig { dateFormat?: string; + custom?: (value: Date | string | number, formatString?: string | null) => string; } From a42c78c52c0815ceb69404a1eac0ebe7974ab813 Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 2 Aug 2024 22:17:39 +0800 Subject: [PATCH 2/5] chore: change name --- packages/abc/cell/cell.service.ts | 24 ++++++++++++---------- packages/theme/src/pipes/date/date.pipe.ts | 6 ++++-- packages/util/config/theme/index.ts | 1 + packages/util/config/theme/pipe.type.ts | 8 +++++++- packages/util/date-time/time.ts | 9 ++++++-- 5 files changed, 32 insertions(+), 16 deletions(-) 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); } From 12a52517979a2afdce97433f7c1391214f61f36d Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 2 Aug 2024 22:24:43 +0800 Subject: [PATCH 3/5] chore: fix bash error --- scripts/ci/build-artifacts.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/ci/build-artifacts.sh b/scripts/ci/build-artifacts.sh index c639bcbfe..b5e070af8 100755 --- a/scripts/ci/build-artifacts.sh +++ b/scripts/ci/build-artifacts.sh @@ -92,10 +92,10 @@ echo "https://${ACCESS_TOKEN}:@github.com" > .git/credentials if [[ $(git ls-remote origin "refs/tags/${buildTagName}") ]]; then echo "removed tag because tag is already published" - git tag -d ${buildTagName} - git push --delete origin ${buildTagName} - git push origin :refs/tags/${buildTagName} - sleep 2 + git tag -d ${buildTagName} || true + git push --delete origin ${buildTagName} || true + git push origin :refs/tags/${buildTagName} || true + sleep 5 fi echo "Git configuration has been updated to match the last commit author. Publishing now.." From cc113040172f04eb88eafc49194a8cfecf08e16c Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 2 Aug 2024 22:29:52 +0800 Subject: [PATCH 4/5] chore: fix --- packages/util/date-time/time.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/util/date-time/time.ts b/packages/util/date-time/time.ts index 03de48943..1b3d40e5d 100644 --- a/packages/util/date-time/time.ts +++ b/packages/util/date-time/time.ts @@ -139,5 +139,7 @@ export function formatDate( if (isNaN(value as NzSafeAny)) return ''; const langOpt = { locale: options?.locale }; - return formatString === 'fn' ? formatDistanceToNow(value, langOpt) : format(value, formatString, langOpt); + return formatString === 'fn' + ? formatDistanceToNow(value, langOpt) + : (options?.customFormat ?? format)(value, formatString, langOpt); } From a08936dfcc09680c132f17759f259446b51c91cb Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 2 Aug 2024 22:48:25 +0800 Subject: [PATCH 5/5] chore: fix type --- packages/util/config/theme/pipe.type.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/util/config/theme/pipe.type.ts b/packages/util/config/theme/pipe.type.ts index d3771ae1d..209fcf6fc 100644 --- a/packages/util/config/theme/pipe.type.ts +++ b/packages/util/config/theme/pipe.type.ts @@ -4,7 +4,7 @@ export interface AlainThemePipeConfig { } export type AlainThemePipeDateFormatCustom = ( - value: Date | string | number, + value: Date, formatString?: string | null, options?: { locale?: Locale } ) => string;