From 945c514da25e48acf4d3f4beedaff665acdaa74c Mon Sep 17 00:00:00 2001 From: xinglu01 <15245457817@163.com> Date: Wed, 15 Jan 2025 10:34:12 +0800 Subject: [PATCH] feat(uitl): handle new Date change new TinyDate --- .../disabled-mode-year.component.ts | 3 +- src/carousel/carousel.component.ts | 46 +++++++++---------- src/core/request-animation.ts | 4 +- src/date-picker/date-picker.config.ts | 12 ++--- src/form/examples/full/full.component.ts | 4 +- .../examples/reactive/reactive.component.ts | 3 +- src/tree/examples/async/async.component.ts | 5 +- src/upload/upload.service.ts | 14 +++--- 8 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/calendar/examples/disabled-mode-year/disabled-mode-year.component.ts b/src/calendar/examples/disabled-mode-year/disabled-mode-year.component.ts index 6143b5fdbb..b5a46169b7 100644 --- a/src/calendar/examples/disabled-mode-year/disabled-mode-year.component.ts +++ b/src/calendar/examples/disabled-mode-year/disabled-mode-year.component.ts @@ -1,11 +1,12 @@ import { Component, OnInit } from '@angular/core'; +import { TinyDate } from 'ngx-tethys/util'; @Component({ selector: 'thy-calendar-disabled-mode-year-example', templateUrl: './disabled-mode-year.component.html' }) export class ThyCalendarDisabledYearExampleComponent implements OnInit { - date = new Date(); + date = new TinyDate()?.nativeDate; constructor() {} diff --git a/src/carousel/carousel.component.ts b/src/carousel/carousel.component.ts index 53e333e336..8e13954c92 100644 --- a/src/carousel/carousel.component.ts +++ b/src/carousel/carousel.component.ts @@ -1,4 +1,7 @@ +import { Platform } from '@angular/cdk/platform'; +import { NgTemplateOutlet } from '@angular/common'; import { + AfterContentInit, AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef, @@ -6,40 +9,37 @@ import { ContentChildren, ElementRef, EventEmitter, + inject, Input, + NgZone, + numberAttribute, + OnChanges, + OnDestroy, OnInit, Output, QueryList, Renderer2, - ViewChild, - TemplateRef, - ViewEncapsulation, - AfterContentInit, - OnChanges, SimpleChanges, - NgZone, - OnDestroy, - numberAttribute, - inject + TemplateRef, + ViewChild, + ViewEncapsulation } from '@angular/core'; -import { Platform } from '@angular/cdk/platform'; +import { ThyDot } from 'ngx-tethys/dot'; +import { ThyIcon } from 'ngx-tethys/icon'; +import { coerceBooleanProperty, isNumber, TinyDate } from 'ngx-tethys/util'; +import { fromEvent, Subject } from 'rxjs'; +import { debounceTime, takeUntil } from 'rxjs/operators'; import { ThyCarouselItemDirective } from './carousel-item.directive'; +import { ThyCarouselService } from './carousel.service'; +import { ThyCarouselFadeEngine, ThyCarouselNoopEngine, ThyCarouselSlideEngine } from './engine'; import { + ThyCarouselEffect, ThyCarouselEngine, - ThyDistanceVector, + ThyCarouselPause, ThyCarouselSwitchData, - ThyCarouselEffect, ThyCarouselTrigger, - ThyCarouselPause + ThyDistanceVector } from './typings'; -import { ThyCarouselSlideEngine, ThyCarouselNoopEngine, ThyCarouselFadeEngine } from './engine'; -import { fromEvent, Subject } from 'rxjs'; -import { debounceTime, takeUntil } from 'rxjs/operators'; -import { ThyCarouselService } from './carousel.service'; -import { ThyIcon } from 'ngx-tethys/icon'; -import { ThyDot } from 'ngx-tethys/dot'; -import { NgTemplateOutlet } from '@angular/common'; -import { coerceBooleanProperty, isNumber } from 'ngx-tethys/util'; /** * 走马灯组件 @@ -235,7 +235,7 @@ export class ThyCarousel implements OnInit, AfterViewInit, AfterContentInit, OnC onDrag(event: TouchEvent | MouseEvent): void { if (!this.isDragging && !this.isTransitioning && this.thyTouchable) { - const mouseDownTime = new Date().getTime(); + const mouseDownTime = new TinyDate().getTime(); let mouseUpTime: number; this.clearScheduledTransition(); this.wrapperDomRect = this.wrapperEl.getBoundingClientRect(); @@ -249,7 +249,7 @@ export class ThyCarousel implements OnInit, AfterViewInit, AfterContentInit, OnC () => {}, () => { if (this.isDragging) { - mouseUpTime = new Date().getTime(); + mouseUpTime = new TinyDate().getTime(); const holdDownTime = mouseUpTime - mouseDownTime; // Fast enough to switch to the next frame // or diff --git a/src/core/request-animation.ts b/src/core/request-animation.ts index cd40f6544a..0742a9721b 100644 --- a/src/core/request-animation.ts +++ b/src/core/request-animation.ts @@ -1,9 +1,11 @@ +import { TinyDate } from 'ngx-tethys/util'; + const availablePrefixes = ['moz', 'ms', 'webkit']; function requestAnimationFramePolyfill(): typeof requestAnimationFrame { let lastTime = 0; return function (callback: FrameRequestCallback): number { - const currTime = new Date().getTime(); + const currTime = new TinyDate().getTime(); const timeToCall = Math.max(0, 16 - (currTime - lastTime)); const id = setTimeout(() => { callback(currTime + timeToCall); diff --git a/src/date-picker/date-picker.config.ts b/src/date-picker/date-picker.config.ts index baa687ffd5..6d7dc9b27c 100644 --- a/src/date-picker/date-picker.config.ts +++ b/src/date-picker/date-picker.config.ts @@ -24,15 +24,15 @@ export const DEFAULT_DATE_PICKER_CONFIG: ThyDatePickerConfig = { return [ { title: '今天', - value: startOfDay(new Date()).getTime() + value: startOfDay(new TinyDate().getTime()).getTime() }, { title: '明天', - value: startOfDay(addDays(new Date(), 1)).getTime() + value: startOfDay(addDays(new TinyDate().getTime(), 1)).getTime() }, { title: '下周', - value: startOfWeek(addWeeks(new Date(), 1), { weekStartsOn: 1 }).getTime() + value: startOfWeek(addWeeks(new TinyDate().getTime(), 1), { weekStartsOn: 1 }).getTime() } ]; }, @@ -72,15 +72,15 @@ export function useDatePickerDefaultConfig(): ThyDatePickerConfig { return [ { title: locale().today, - value: startOfDay(new Date()).getTime() + value: startOfDay(new TinyDate().getTime()).getTime() }, { title: locale().tomorrow, - value: startOfDay(addDays(new Date(), 1)).getTime() + value: startOfDay(addDays(new TinyDate().getTime(), 1)).getTime() }, { title: locale().nextWeek, - value: startOfWeek(addWeeks(new Date(), 1), { weekStartsOn: 1 }).getTime() + value: startOfWeek(addWeeks(new TinyDate().getTime(), 1), { weekStartsOn: 1 }).getTime() } ]; }, diff --git a/src/form/examples/full/full.component.ts b/src/form/examples/full/full.component.ts index b4e66c7fbb..2c95605166 100644 --- a/src/form/examples/full/full.component.ts +++ b/src/form/examples/full/full.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { ThyFormValidatorConfig, ThyFormDirective } from 'ngx-tethys/form'; +import { TinyDate } from 'ngx-tethys/util'; const provinceCities = [ { @@ -93,7 +93,7 @@ export class ThyFormFullExampleComponent implements OnInit { } ]; - date = { date: new Date(), with_time: 0 }; + date = { date: new TinyDate()?.nativeDate, with_time: 0 }; dateNull: number = null; diff --git a/src/form/examples/reactive/reactive.component.ts b/src/form/examples/reactive/reactive.component.ts index c49ccd2e27..6b3a9effe1 100644 --- a/src/form/examples/reactive/reactive.component.ts +++ b/src/form/examples/reactive/reactive.component.ts @@ -2,6 +2,7 @@ import { ThyFormValidatorConfig, ThyValidateOn } from 'ngx-tethys/form'; import { Component, OnInit, inject } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { TinyDate } from 'ngx-tethys/util'; const provinceCities = [ { @@ -102,7 +103,7 @@ export class ThyFormReactiveExampleComponent implements OnInit { } ]; - date = { date: new Date(), with_time: 0 }; + date = { date: new TinyDate().nativeDate, with_time: 0 }; dateNull: number = null; diff --git a/src/tree/examples/async/async.component.ts b/src/tree/examples/async/async.component.ts index 4b9897e3e6..dddd48e923 100644 --- a/src/tree/examples/async/async.component.ts +++ b/src/tree/examples/async/async.component.ts @@ -1,5 +1,6 @@ import { Component, ViewChild } from '@angular/core'; import { ThyTree, ThyTreeEmitEvent, ThyTreeNodeData } from 'ngx-tethys/tree'; +import { TinyDate } from 'ngx-tethys/util'; @Component({ selector: 'thy-tree-async-example', @@ -38,8 +39,8 @@ export class ThyTreeAsyncExampleComponent { setTimeout( () => resolve([ - { title: 'Child Node', key: `${new Date().getTime()}-0` }, - { title: 'Child Node', key: `${new Date().getTime()}-1` } + { title: 'Child Node', key: `${new TinyDate().getTime()}-0` }, + { title: 'Child Node', key: `${new TinyDate().getTime()}-1` } ]), 500 ); diff --git a/src/upload/upload.service.ts b/src/upload/upload.service.ts index 31c46cb2c9..9e4a51ae75 100644 --- a/src/upload/upload.service.ts +++ b/src/upload/upload.service.ts @@ -1,9 +1,9 @@ -import { coerceArray, humanizeBytes } from 'ngx-tethys/util'; +import { coerceArray, humanizeBytes, TinyDate } from 'ngx-tethys/util'; import { from, Observable, Subscriber } from 'rxjs'; import { map, mergeMap, tap } from 'rxjs/operators'; import { XhrFactory } from '@angular/common'; -import { Injectable, inject } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; export enum ThyUploadStatus { pending = 'pending', @@ -151,7 +151,7 @@ export class ThyUploadService { private xhrFactory = inject(XhrFactory); private secondsToHuman(sec: number): string { - return new Date(sec * 1000).toISOString().slice(11, 19); + return new TinyDate(new Date(sec * 1000)).nativeDate.toISOString().slice(11, 19); } private normalizeUploadFiles(uploadFiles: ThyUploadFile | ThyUploadFile[]) { @@ -168,7 +168,7 @@ export class ThyUploadService { private uploadByXhr(observer: Subscriber, uploadFile: ThyUploadFile) { const xhr = this.xhrFactory.build(); - const time: number = new Date().getTime(); + const time: number = new TinyDate().getTime(); let speed = 0; let estimatedTime: number | null = null; @@ -184,9 +184,9 @@ export class ThyUploadService { if (percentage === 100) { percentage = 99; } - const diff = new Date().getTime() - time; + const diff = new TinyDate().getTime() - time; speed = Math.round((event.loaded / diff) * 1000); - const progressStartTime = (uploadFile.progress && uploadFile.progress.startTime) || new Date().getTime(); + const progressStartTime = (uploadFile.progress && uploadFile.progress.startTime) || new TinyDate().getTime(); estimatedTime = Math.ceil((event.total - event.loaded) / speed); uploadFile.progress.status = ThyUploadStatus.uploading; @@ -205,7 +205,7 @@ export class ThyUploadService { const onReadyStateChange = () => { if (xhr.readyState === XMLHttpRequest.DONE) { - const speedTime = (new Date().getTime() - uploadFile.progress.startTime) * 1000; + const speedTime = (new TinyDate().getTime() - uploadFile.progress.startTime) * 1000; const speedAverage = Math.round(uploadFile.nativeFile.size / speedTime); uploadFile.progress.status = ThyUploadStatus.done;