Skip to content

Commit

Permalink
chore: change name
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Aug 2, 2024
1 parent 0a830fe commit a42c78c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
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
6 changes: 4 additions & 2 deletions packages/theme/src/pipes/date/date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
}
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';
8 changes: 7 additions & 1 deletion packages/util/config/theme/pipe.type.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 7 additions & 2 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,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);
}

0 comments on commit a42c78c

Please sign in to comment.