Skip to content

Commit f945ce8

Browse files
author
heshenglei
committed
refactor: 去除animation namespace等
1 parent 86294d0 commit f945ce8

File tree

3 files changed

+105
-96
lines changed

3 files changed

+105
-96
lines changed

src/model/animation.ts

Lines changed: 82 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Animation {
77
[path: string]: {
88
startValue: any
99
targetValue: any
10-
options: Animation.StartOptions<any>
10+
options: AnimationStartOptions<any>
1111
}
1212
} = {}
1313

@@ -17,14 +17,14 @@ export class Animation {
1717
return Object.keys(this.ids)
1818
}
1919

20-
start<T extends Animation.TargetValue>(
20+
start<T extends AnimationTargetValue>(
2121
path: string | string[],
2222
targetValue: T,
23-
options: Animation.StartOptions<T> = {},
23+
options: AnimationStartOptions<T> = {},
2424
delim = '/',
2525
): () => void {
2626
const startValue = this.cell.getPropByPath<T>(path)
27-
const localOptions = ObjectExt.defaults(options, Animation.defaultOptions)
27+
const localOptions = ObjectExt.defaults(options, defaultOptions)
2828
const timing = this.getTiming(localOptions.timing)
2929
const interpolate = this.getInterp<T>(
3030
localOptions.interp,
@@ -45,7 +45,9 @@ export class Animation {
4545

4646
if (
4747
dir === 'normal' ||
48+
// alternate 时迭代次数为奇数时正向,偶数时反向
4849
(dir === 'alternate' && !isOdd) ||
50+
// alternate-reverse 时迭代次数为 偶数时正向,奇数时反向
4951
(dir === 'alternate-reverse' && isOdd)
5052
) {
5153
return timeFraction
@@ -64,27 +66,28 @@ export class Animation {
6466
}
6567

6668
const iterate = () => {
67-
const now = new Date().getTime()
69+
const now = Date.now()
6870
if (startTime === 0) {
6971
startTime = now
7072
}
7173

74+
const { duration, iterations, fill } = localOptions
7275
const elapsed = now - startTime
73-
const iterationIndex = Math.floor(elapsed / localOptions.duration)
74-
const localTime = elapsed % localOptions.duration
75-
const timeFraction = localTime / localOptions.duration
76+
const iterationIndex = Math.floor(elapsed / duration)
77+
const localTime = elapsed % duration
78+
const timeFraction = localTime / duration
7679

7780
let progress = calculateDirectionProgress(timeFraction, iterationIndex)
7881

79-
if (iterationIndex < localOptions.iterations) {
82+
if (iterationIndex < iterations) {
8083
this.ids[key] = requestAnimationFrame(iterate)
8184

8285
updateProgressState(progress)
8386
} else {
8487
// 动画结束
85-
if (localOptions.fill === 'forwards') {
88+
if (fill === 'forwards') {
8689
progress = calculateDirectionProgress(1, iterationIndex - 1)
87-
} else if (localOptions.fill === 'none') {
90+
} else if (fill === 'none') {
8891
progress = 0
8992
}
9093
progress = Math.round(progress)
@@ -109,12 +112,12 @@ export class Animation {
109112
options.start && options.start(this.getArgs<T>(key))
110113
}, options.delay)
111114

112-
return this.stop.bind(this, path, delim, options)
115+
return this.stop.bind(this, path, options, delim)
113116
}
114117

115-
stop<T extends Animation.TargetValue>(
118+
stop<T extends AnimationTargetValue>(
116119
path: string | string[],
117-
options: Animation.StopOptions<T> = {},
120+
options: AnimationStopOptions<T> = {},
118121
delim = '/',
119122
) {
120123
const paths = Array.isArray(path) ? path : path.split(delim)
@@ -158,7 +161,7 @@ export class Animation {
158161
return typeof timing === 'string' ? Timing[timing] : timing
159162
}
160163

161-
private getInterp<T extends Animation.TargetValue>(
164+
private getInterp<T extends AnimationTargetValue>(
162165
interp: Interp.Definition<any> | undefined,
163166
startValue: T,
164167
targetValue: T,
@@ -185,9 +188,9 @@ export class Animation {
185188
)
186189
}
187190

188-
private getArgs<T extends Animation.TargetValue>(
191+
private getArgs<T extends AnimationTargetValue>(
189192
key: string,
190-
): Animation.CallbackArgs<T> {
193+
): AnimationCallbackArgs<T> {
191194
const data = this.cache[key]
192195
return {
193196
path: key,
@@ -198,75 +201,73 @@ export class Animation {
198201
}
199202
}
200203

201-
export namespace Animation {
202-
export interface BaseOptions {
203-
delay: number
204-
duration: number
205-
timing: Timing.Names | Timing.Definition
206-
iterations?: number
207-
direction?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'
208-
fill?: 'none' | 'forwards'
209-
}
204+
export interface AnimationBaseOptions {
205+
delay: number
206+
duration: number
207+
timing: Timing.Names | Timing.Definition
208+
iterations?: number
209+
direction?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'
210+
fill?: 'none' | 'forwards'
211+
}
210212

211-
export type TargetValue = string | number | KeyValue<number>
213+
export type AnimationTargetValue = string | number | KeyValue<number>
212214

213-
export interface CallbackArgs<T> {
214-
cell: Cell
215-
path: string
216-
startValue: T
217-
targetValue: T
218-
}
215+
export interface AnimationCallbackArgs<T> {
216+
cell: Cell
217+
path: string
218+
startValue: T
219+
targetValue: T
220+
}
219221

220-
export interface ProgressArgs<T> extends CallbackArgs<T> {
221-
progress: number
222-
currentValue: T
223-
}
222+
export interface AnimationProgressArgs<T> extends AnimationCallbackArgs<T> {
223+
progress: number
224+
currentValue: T
225+
}
224226

225-
export interface StopArgs<T> extends CallbackArgs<T> {
226-
jumpedToEnd?: boolean
227-
}
227+
export interface AnimationStopArgs<T> extends AnimationCallbackArgs<T> {
228+
jumpedToEnd?: boolean
229+
}
228230

229-
export interface StartOptions<T>
230-
extends Partial<BaseOptions>,
231-
StopOptions<T> {
232-
interp?: Interp.Definition<any>
233-
/**
234-
* A function to call when the animation begins.
235-
*/
236-
start?: (options: CallbackArgs<T>) => void
237-
/**
238-
* A function to be called after each step of the animation, only once per
239-
* animated element regardless of the number of animated properties.
240-
*/
241-
progress?: (options: ProgressArgs<T>) => void
242-
}
231+
export interface AnimationStartOptions<T>
232+
extends Partial<AnimationBaseOptions>,
233+
AnimationStopOptions<T> {
234+
interp?: Interp.Definition<any>
235+
/**
236+
* A function to call when the animation begins.
237+
*/
238+
start?: (options: AnimationCallbackArgs<T>) => void
239+
/**
240+
* A function to be called after each step of the animation, only once per
241+
* animated element regardless of the number of animated properties.
242+
*/
243+
progress?: (options: AnimationProgressArgs<T>) => void
244+
}
243245

244-
export interface StopOptions<T> {
245-
/**
246-
* A Boolean indicating whether to complete the animation immediately.
247-
* Defaults to `false`.
248-
*/
249-
jumpedToEnd?: boolean
250-
/**
251-
* A function that is called once the animation completes.
252-
*/
253-
complete?: (options: CallbackArgs<T>) => void
254-
/**
255-
* A function to be called when the animation stops.
256-
*/
257-
stop?: (options: StopArgs<T>) => void
258-
/**
259-
* A function to be called when the animation completes or stops.
260-
*/
261-
finish?: (options: CallbackArgs<T>) => void
262-
}
246+
export interface AnimationStopOptions<T> {
247+
/**
248+
* A Boolean indicating whether to complete the animation immediately.
249+
* Defaults to `false`.
250+
*/
251+
jumpedToEnd?: boolean
252+
/**
253+
* A function that is called once the animation completes.
254+
*/
255+
complete?: (options: AnimationCallbackArgs<T>) => void
256+
/**
257+
* A function to be called when the animation stops.
258+
*/
259+
stop?: (options: AnimationStopArgs<T>) => void
260+
/**
261+
* A function to be called when the animation completes or stops.
262+
*/
263+
finish?: (options: AnimationCallbackArgs<T>) => void
264+
}
263265

264-
export const defaultOptions: BaseOptions = {
265-
delay: 10,
266-
duration: 100,
267-
timing: 'linear',
268-
iterations: 1,
269-
direction: 'normal',
270-
fill: 'none',
271-
}
266+
export const defaultOptions: AnimationBaseOptions = {
267+
delay: 10,
268+
duration: 100,
269+
timing: 'linear',
270+
iterations: 1,
271+
direction: 'normal',
272+
fill: 'none',
272273
}

src/model/cell.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ import {
2121
} from '../registry'
2222
import type { CellView } from '../view'
2323
import type { MarkupType } from '../view/markup'
24-
import { Animation } from './animation'
24+
import {
25+
Animation,
26+
type AnimationCallbackArgs,
27+
type AnimationProgressArgs,
28+
type AnimationStartOptions,
29+
type AnimationStopArgs,
30+
type AnimationStopOptions,
31+
type AnimationTargetValue,
32+
} from './animation'
2533
import type { Edge } from './edge'
2634
import type { Model } from './model'
2735
import type { Node } from './node'
@@ -1092,19 +1100,19 @@ export class Cell<
10921100
transition<K extends keyof Properties>(
10931101
path: K,
10941102
target: Properties[K],
1095-
options?: Animation.StartOptions<Properties[K]>,
1103+
options?: AnimationStartOptions<Properties[K]>,
10961104
delim?: string,
10971105
): () => void
1098-
transition<T extends Animation.TargetValue>(
1106+
transition<T extends AnimationTargetValue>(
10991107
path: string | string[],
11001108
target: T,
1101-
options?: Animation.StartOptions<T>,
1109+
options?: AnimationStartOptions<T>,
11021110
delim?: string,
11031111
): () => void
1104-
transition<T extends Animation.TargetValue>(
1112+
transition<T extends AnimationTargetValue>(
11051113
path: string | string[],
11061114
target: T,
1107-
options: Animation.StartOptions<T> = {},
1115+
options: AnimationStartOptions<T> = {},
11081116
delim = '/',
11091117
) {
11101118
return this.animation.start(
@@ -1118,9 +1126,9 @@ export class Cell<
11181126
)
11191127
}
11201128

1121-
stopTransition<T extends Animation.TargetValue>(
1129+
stopTransition<T extends AnimationTargetValue>(
11221130
path: string | string[],
1123-
options?: Animation.StopOptions<T>,
1131+
options?: AnimationStopOptions<T>,
11241132
delim = '/',
11251133
) {
11261134
this.animation.stop(path, options, delim)
@@ -1604,11 +1612,11 @@ export namespace Cell {
16041612

16051613
export namespace Cell {
16061614
export interface EventArgs {
1607-
'transition:start': Animation.CallbackArgs<Animation.TargetValue>
1608-
'transition:progress': Animation.ProgressArgs<Animation.TargetValue>
1609-
'transition:complete': Animation.CallbackArgs<Animation.TargetValue>
1610-
'transition:stop': Animation.StopArgs<Animation.TargetValue>
1611-
'transition:finish': Animation.CallbackArgs<Animation.TargetValue>
1615+
'transition:start': AnimationCallbackArgs<AnimationTargetValue>
1616+
'transition:progress': AnimationProgressArgs<AnimationTargetValue>
1617+
'transition:complete': AnimationCallbackArgs<AnimationTargetValue>
1618+
'transition:stop': AnimationStopArgs<AnimationTargetValue>
1619+
'transition:finish': AnimationCallbackArgs<AnimationTargetValue>
16121620

16131621
// common
16141622
'change:*': ChangeAnyKeyArgs

src/model/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { Angle, Point, Rectangle } from '../geometry'
1111
import { Registry } from '../registry/registry'
1212
import { Markup, type MarkupType } from '../view/markup'
13-
import type { Animation } from './animation'
13+
import type { AnimationStartOptions } from './animation'
1414
import { Cell } from './cell'
1515
import type { Edge } from './edge'
1616
import { PortManager } from './port'
@@ -1036,7 +1036,7 @@ export namespace Node {
10361036
}
10371037

10381038
export interface TranslateOptions extends Cell.TranslateOptions {
1039-
transition?: boolean | Animation.StartOptions<Point.PointLike>
1039+
transition?: boolean | AnimationStartOptions<Point.PointLike>
10401040
restrict?: Rectangle.RectangleLike | null
10411041
exclude?: Cell[]
10421042
}

0 commit comments

Comments
 (0)