Skip to content

Commit 772ecb4

Browse files
authored
feat(theme): support custom process of _date pipe (#1822)
1 parent 6389ec1 commit 772ecb4

File tree

6 files changed

+42
-20
lines changed

6 files changed

+42
-20
lines changed

packages/abc/cell/cell.service.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { DomSanitizer } from '@angular/platform-browser';
33
import { map, Observable, of } from 'rxjs';
44

55
import { yn } from '@delon/theme';
6-
import { AlainCellConfig, AlainConfigService } from '@delon/util/config';
6+
import { AlainConfigService } from '@delon/util/config';
77
import { formatDate } from '@delon/util/date-time';
88
import { CurrencyService, formatMask } from '@delon/util/format';
99
import { deepMerge } from '@delon/util/other';
@@ -25,12 +25,22 @@ export class CellService {
2525
private readonly nzI18n = inject(NzI18nService);
2626
private readonly currency = inject(CurrencyService);
2727
private readonly dom = inject(DomSanitizer);
28-
private globalOptions!: AlainCellConfig;
28+
private readonly configSrv = inject(AlainConfigService);
29+
private globalOptions = this.configSrv.merge('cell', {
30+
date: { format: 'yyyy-MM-dd HH:mm:ss' },
31+
img: { size: 32 },
32+
default: { text: '-' }
33+
})!;
2934
private widgets: { [key: string]: CellWidget } = {
3035
date: {
3136
type: 'fn',
3237
ref: (value, opt) => {
33-
return { text: formatDate(value as string, opt.date!.format!, this.nzI18n.getDateLocale()) };
38+
return {
39+
text: formatDate(value as string, opt.date!.format!, {
40+
locale: this.nzI18n.getDateLocale(),
41+
customFormat: this.configSrv.get('themePipe')?.dateFormatCustom
42+
})
43+
};
3444
}
3545
},
3646
mega: {
@@ -66,14 +76,6 @@ export class CellService {
6676
}
6777
};
6878

69-
constructor(configSrv: AlainConfigService) {
70-
this.globalOptions = configSrv.merge('cell', {
71-
date: { format: 'yyyy-MM-dd HH:mm:ss' },
72-
img: { size: 32 },
73-
default: { text: '-' }
74-
})!;
75-
}
76-
7779
registerWidget(key: string, widget: Type<unknown>): void {
7880
this.widgets[key] = { type: 'widget', ref: widget };
7981
}

packages/theme/src/pipes/date/date.pipe.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import { NzI18nService } from 'ng-zorro-antd/i18n';
77
@Pipe({ name: '_date', standalone: true })
88
export class DatePipe implements PipeTransform {
99
private nzI18n = inject(NzI18nService);
10-
private defFormat = inject(AlainConfigService).get('themePipe')?.dateFormat ?? 'yyyy-MM-dd HH:mm';
10+
private cog = inject(AlainConfigService).get('themePipe');
1111

1212
transform(value: Date | string | number, formatString?: string | null): string {
13-
return formatDate(value, formatString ?? this.defFormat, this.nzI18n.getDateLocale());
13+
const formatStr = formatString ?? this.cog?.dateFormat ?? 'yyyy-MM-dd HH:mm';
14+
15+
return formatDate(value, formatStr, {
16+
locale: this.nzI18n.getDateLocale(),
17+
customFormat: this.cog?.dateFormatCustom
18+
});
1419
}
1520
}

packages/util/config/theme/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './http.type';
22
export * from './responsive.type';
33
export * from './i18n.type';
4+
export * from './pipe.type';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
export interface AlainThemePipeConfig {
22
dateFormat?: string;
3+
dateFormatCustom?: AlainThemePipeDateFormatCustom;
34
}
5+
6+
export type AlainThemePipeDateFormatCustom = (
7+
value: Date,
8+
formatString?: string | null,
9+
options?: { locale?: Locale }
10+
) => string;

packages/util/date-time/time.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
formatDistanceToNow
1818
} from 'date-fns';
1919

20+
import type { AlainThemePipeDateFormatCustom } from '@delon/util/config';
2021
import type { NzSafeAny } from 'ng-zorro-antd/core/types';
2122
import { DateLocale } from 'ng-zorro-antd/i18n';
2223

@@ -129,10 +130,16 @@ export function toDate(value?: Date | string | number | null, options?: string |
129130
* @param formatString Please refer to [date-fnd format](https://date-fns.org/v2.30.0/docs/format) for string format
130131
* @param dateLocale Recommended to be consistent with NG-ZORRO by using `inject(NZ_DATE_LOCALE)`
131132
*/
132-
export function formatDate(value: Date | string | number, formatString: string, dateLocale?: DateLocale): string {
133+
export function formatDate(
134+
value: Date | string | number,
135+
formatString: string,
136+
options?: { locale?: DateLocale; customFormat?: AlainThemePipeDateFormatCustom }
137+
): string {
133138
value = toDate(value);
134139
if (isNaN(value as NzSafeAny)) return '';
135140

136-
const langOpt = { locale: dateLocale };
137-
return formatString === 'fn' ? formatDistanceToNow(value, langOpt) : format(value, formatString, langOpt);
141+
const langOpt = { locale: options?.locale };
142+
return formatString === 'fn'
143+
? formatDistanceToNow(value, langOpt)
144+
: (options?.customFormat ?? format)(value, formatString, langOpt);
138145
}

scripts/ci/build-artifacts.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ echo "https://${ACCESS_TOKEN}:@github.com" > .git/credentials
9292

9393
if [[ $(git ls-remote origin "refs/tags/${buildTagName}") ]]; then
9494
echo "removed tag because tag is already published"
95-
git tag -d ${buildTagName}
96-
git push --delete origin ${buildTagName}
97-
git push origin :refs/tags/${buildTagName}
98-
sleep 2
95+
git tag -d ${buildTagName} || true
96+
git push --delete origin ${buildTagName} || true
97+
git push origin :refs/tags/${buildTagName} || true
98+
sleep 5
9999
fi
100100

101101
echo "Git configuration has been updated to match the last commit author. Publishing now.."

0 commit comments

Comments
 (0)