-
Notifications
You must be signed in to change notification settings - Fork 3
/
asset.ts
176 lines (161 loc) · 3.89 KB
/
asset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { MaxSupported } from '@/constants';
import {
AudioAssetConfig,
VideoAssetConfig,
ImageAssetConfig,
TextAssetConfig,
TextStyleProps,
} from '@/types/config';
import { v4 as uuidv4 } from 'uuid';
import { TextStyleDefaultValues } from './config';
const validateMaxSupport = (
duration: number,
maxDuration: number,
attribute: string
): number => {
if (!duration) {
return 0;
} else if (duration > maxDuration) {
console.log(
`${attribute}: ${duration} is greater than max supported: ${maxDuration}`
);
}
return duration;
};
/**
* The base MediaAsset class
*/
class MediaAsset {
public assetId: string;
constructor(assetId: string) {
this.assetId = assetId;
}
}
/**
* VideoAsset class
*/
export class VideoAsset extends MediaAsset {
public start: number;
public end: number | null;
/**
* Initializes a AudioAsset instance
* @param assetId - The id of the audio asset.
* @param config - The configuration of the audio asset.
* @returns A new AudioAsset instance.
*/
constructor(assetId: string, config?: Partial<VideoAssetConfig>) {
const { start = 0, end = null } = config || {};
super(assetId);
this.start = start;
this.end = end;
}
toJSON() {
return {
assetId: this.assetId,
start: this.start,
end: this.end,
};
}
}
/**
* AudioAsset class
*/
export class AudioAsset extends MediaAsset {
public start: number;
public end: number | null;
public disableOtherTracks: boolean;
public fadeInDuration: number;
public fadeOutDuration: number;
/**
* Initializes a AudioAsset instance
* @param assetId - The id of the audio asset.
* @param config - The configuration of the audio asset.
* @returns A new AudioAsset instance.
*/
constructor(assetId: string, config?: Partial<AudioAssetConfig>) {
const {
start = 0,
end = null,
disableOtherTracks = true,
fadeInDuration = 0,
fadeOutDuration = 0,
} = config || {};
super(assetId);
this.start = start;
this.end = end;
this.disableOtherTracks = disableOtherTracks;
this.fadeInDuration = validateMaxSupport(
fadeInDuration,
MaxSupported.fadeDuration,
'fadeInDuration'
);
this.fadeOutDuration = validateMaxSupport(
fadeOutDuration,
MaxSupported.fadeDuration,
'fadeOutDuration'
);
}
toJSON() {
return {
assetId: this.assetId,
start: this.start,
disableOtherTracks: this.disableOtherTracks,
fadeInDuration: this.fadeInDuration,
fadeOutDuration: this.fadeOutDuration,
end: this.end,
};
}
}
export class ImageAsset extends MediaAsset {
public width: number | string;
public height: number | string;
public x: number | string;
public y: number | string;
public duration: number | null;
constructor(assetId: string, config?: Partial<ImageAssetConfig>) {
const {
width = 100,
height = 100,
x = 80,
y = 20,
duration = null,
} = config || {};
super(assetId);
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.duration = duration;
}
toJSON() {
return {
assetId: this.assetId,
width: this.width,
height: this.height,
x: this.x,
y: this.y,
duration: this.duration,
};
}
}
export class TextAsset extends MediaAsset {
public text: string;
public duration: number | null;
public style: Partial<TextStyleProps>;
constructor(config?: Partial<TextAssetConfig>) {
const { text = '', style = {}, duration = null } = config || {};
const assetId: string = `txt-${uuidv4()}`;
super(assetId);
this.text = text;
this.duration = duration;
this.style = Object.assign({}, TextStyleDefaultValues, style);
}
toJSON() {
return {
assetId: this.assetId,
text: this.text,
style: { ...this.style },
duration: this.duration,
};
}
}