Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme): support custom process of _date pipe #1822

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/abc/cell/cell.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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: {
Expand Down Expand Up @@ -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<unknown>): void {
this.widgets[key] = { type: 'widget', ref: widget };
}
Expand Down
9 changes: 7 additions & 2 deletions packages/theme/src/pipes/date/date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ 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';

return formatDate(value, formatStr, {
locale: this.nzI18n.getDateLocale(),
customFormat: this.cog?.dateFormatCustom
});
}
}
1 change: 1 addition & 0 deletions packages/util/config/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './http.type';
export * from './responsive.type';
export * from './i18n.type';
export * from './pipe.type';
7 changes: 7 additions & 0 deletions packages/util/config/theme/pipe.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export interface AlainThemePipeConfig {
dateFormat?: string;
dateFormatCustom?: AlainThemePipeDateFormatCustom;
}

export type AlainThemePipeDateFormatCustom = (
value: Date,
formatString?: string | null,
options?: { locale?: Locale }
) => string;
13 changes: 10 additions & 3 deletions packages/util/date-time/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -129,10 +130,16 @@ 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 };
return formatString === 'fn' ? formatDistanceToNow(value, langOpt) : format(value, formatString, langOpt);
const langOpt = { locale: options?.locale };
return formatString === 'fn'
? formatDistanceToNow(value, langOpt)
: (options?.customFormat ?? format)(value, formatString, langOpt);
}
8 changes: 4 additions & 4 deletions scripts/ci/build-artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.."
Expand Down
Loading