keyframekit 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +129 -126
- package/dist/KeyframeKit.d.ts +138 -138
- package/dist/KeyframeKit.js +293 -293
- package/docs/.vitepress/components/Playground/Playground.js +242 -242
- package/docs/.vitepress/components/Playground/Playground.vue +206 -206
- package/docs/.vitepress/components/Playground/defaultExample.js +175 -175
- package/docs/.vitepress/components/Playground/interFont.js +14 -14
- package/docs/.vitepress/components/Playground/themes/githubDark.js +401 -401
- package/docs/.vitepress/components/Playground/themes/githubLight.js +398 -398
- package/docs/.vitepress/components/Playground/themes.js +24 -24
- package/docs/.vitepress/config.ts +84 -69
- package/docs/.vitepress/referenceNavigation.ts +37 -37
- package/docs/.vitepress/theme/base-styles.css +100 -91
- package/docs/.vitepress/theme/env.d.ts +5 -5
- package/docs/.vitepress/theme/index.ts +39 -39
- package/docs/docs/index.md +142 -141
- package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.d.ts +138 -138
- package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.js +293 -293
- package/docs/docs/reference/_media/LICENSE +21 -21
- package/docs/docs/reference/classes/KeyframeEffectParameters.md +95 -95
- package/docs/docs/reference/classes/ParsedKeyframes.md +49 -49
- package/docs/docs/reference/index.md +20 -20
- package/docs/docs/reference/interfaces/KeyframesFactory.md +151 -151
- package/docs/docs/reference/navigation.json +63 -63
- package/docs/docs/reference/type-aliases/KeyframeArgument.md +9 -9
- package/docs/docs/reference/type-aliases/KeyframesFactorySource.md +9 -9
- package/docs/docs/reference/type-aliases/ParsedKeyframesRules.md +15 -15
- package/docs/docs/reference/variables/default.md +7 -7
- package/docs/package.json +25 -25
- package/docs/typedoc/plugin-param-names.js +51 -51
- package/docs/typedoc.json +62 -62
- package/package.json +37 -37
- package/src/KeyframeKit.ts +508 -508
- package/tsconfig.json +47 -47
- package/vercel.json +12 -12
package/dist/KeyframeKit.js
CHANGED
|
@@ -1,294 +1,294 @@
|
|
|
1
|
-
//
|
|
2
|
-
// KeyframeKit
|
|
3
|
-
// Ben Hatsor
|
|
4
|
-
//
|
|
5
|
-
// See README.md for usage.
|
|
6
|
-
//
|
|
7
|
-
const CHARS = {
|
|
8
|
-
PERCENT_SIGN: '%',
|
|
9
|
-
HYPHEN_MINUS: '-',
|
|
10
|
-
DOUBLE_HYPHEN_MINUS: '--',
|
|
11
|
-
WEBKIT_PREFIX: '-webkit-'
|
|
12
|
-
};
|
|
13
|
-
class KeyframesFactory {
|
|
14
|
-
Error = {
|
|
15
|
-
KeyframesRuleNameTypeError: class KeyframesRuleNameTypeError extends TypeError {
|
|
16
|
-
message = `Keyframes rule name must be a string.`;
|
|
17
|
-
},
|
|
18
|
-
SourceTypeError: class SourceTypeError extends TypeError {
|
|
19
|
-
message = `Source must be either a Document, a ShadowRoot or a CSSStyleSheet instance.`;
|
|
20
|
-
},
|
|
21
|
-
StyleSheetImportError: class StyleSheetImportError extends Error {
|
|
22
|
-
message = `The stylesheet could not be imported.`;
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
async getDocumentStyleSheetsOnLoad({ document = window.document } = {}) {
|
|
26
|
-
await waitForDocumentLoad({
|
|
27
|
-
document: document
|
|
28
|
-
});
|
|
29
|
-
return document.styleSheets;
|
|
30
|
-
}
|
|
31
|
-
/** @remarks
|
|
32
|
-
* Note: `@import` rules won't be resolved in imported stylesheets.
|
|
33
|
-
* See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418. */
|
|
34
|
-
async importStyleSheet(url) {
|
|
35
|
-
const resp = await fetch(url);
|
|
36
|
-
if (!resp.ok) {
|
|
37
|
-
throw new this.Error.StyleSheetImportError();
|
|
38
|
-
}
|
|
39
|
-
const respText = await resp.text();
|
|
40
|
-
// remove file name from URL to get base URL
|
|
41
|
-
const baseURL = url.split('/').slice(0, -1).join('/');
|
|
42
|
-
const styleSheet = new CSSStyleSheet({
|
|
43
|
-
baseURL: baseURL
|
|
44
|
-
});
|
|
45
|
-
await styleSheet.replace(respText);
|
|
46
|
-
return styleSheet;
|
|
47
|
-
}
|
|
48
|
-
/** @param obj.of The name of the `@keyframes` rule to get keyframes from. */
|
|
49
|
-
getStyleSheetKeyframes({ of: ruleName, in: source }) {
|
|
50
|
-
if (typeof ruleName !== 'string') {
|
|
51
|
-
throw new this.Error.KeyframesRuleNameTypeError();
|
|
52
|
-
}
|
|
53
|
-
if (source instanceof StyleSheetList) {
|
|
54
|
-
return this.#getStyleSheetKeyframesInStyleSheetList({
|
|
55
|
-
of: ruleName,
|
|
56
|
-
styleSheetList: source
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
else if (source instanceof CSSStyleSheet) {
|
|
60
|
-
return this.#getStyleSheetKeyframesInStyleSheet({
|
|
61
|
-
of: ruleName,
|
|
62
|
-
styleSheet: source
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
throw new this.Error.SourceTypeError();
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
#getStyleSheetKeyframesInStyleSheetList({ of: ruleName, styleSheetList }) {
|
|
70
|
-
for (const styleSheet of styleSheetList) {
|
|
71
|
-
const keyframesRule = this.#getStyleSheetKeyframesInStyleSheet({
|
|
72
|
-
of: ruleName,
|
|
73
|
-
styleSheet: styleSheet
|
|
74
|
-
});
|
|
75
|
-
if (keyframesRule !== undefined) {
|
|
76
|
-
return keyframesRule;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
#getStyleSheetKeyframesInStyleSheet({ of: ruleName, styleSheet }) {
|
|
81
|
-
for (const rule of styleSheet.cssRules) {
|
|
82
|
-
if (!(rule instanceof CSSKeyframesRule)) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
if (rule.name === ruleName) {
|
|
86
|
-
const keyframes = this.parseKeyframesRule({
|
|
87
|
-
rule: rule
|
|
88
|
-
});
|
|
89
|
-
return keyframes;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
getAllStyleSheetKeyframesRules({ in: source }) {
|
|
94
|
-
if (source instanceof StyleSheetList) {
|
|
95
|
-
return this.#getAllStyleSheetKeyframesRulesInStyleSheetList({
|
|
96
|
-
styleSheetList: source
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
else if (source instanceof CSSStyleSheet) {
|
|
100
|
-
return this.#getAllStyleSheetKeyframesRulesInStyleSheet({
|
|
101
|
-
styleSheet: source
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
throw new this.Error.SourceTypeError();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
#getAllStyleSheetKeyframesRulesInStyleSheetList({ styleSheetList }) {
|
|
109
|
-
let keyframesRules = {};
|
|
110
|
-
for (const styleSheet of styleSheetList) {
|
|
111
|
-
const styleSheetKeyframesRules = this.#getAllStyleSheetKeyframesRulesInStyleSheet({
|
|
112
|
-
styleSheet: styleSheet
|
|
113
|
-
});
|
|
114
|
-
keyframesRules = {
|
|
115
|
-
...keyframesRules,
|
|
116
|
-
...styleSheetKeyframesRules
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
return keyframesRules;
|
|
120
|
-
}
|
|
121
|
-
#getAllStyleSheetKeyframesRulesInStyleSheet({ styleSheet }) {
|
|
122
|
-
let keyframesRules = {};
|
|
123
|
-
for (const rule of styleSheet.cssRules) {
|
|
124
|
-
if (!(rule instanceof CSSKeyframesRule)) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
const keyframes = this.parseKeyframesRule({
|
|
128
|
-
rule: rule
|
|
129
|
-
});
|
|
130
|
-
keyframesRules[rule.name] = keyframes;
|
|
131
|
-
}
|
|
132
|
-
return keyframesRules;
|
|
133
|
-
}
|
|
134
|
-
parseKeyframesRule({ rule: keyframes }) {
|
|
135
|
-
let parsedKeyframes = [];
|
|
136
|
-
for (const keyframe of keyframes) {
|
|
137
|
-
// remove trailing '%'
|
|
138
|
-
/// https://drafts.csswg.org/css-animations/#dom-csskeyframerule-keytext
|
|
139
|
-
const percentString = removeSuffix({
|
|
140
|
-
of: keyframe.keyText,
|
|
141
|
-
suffix: CHARS.PERCENT_SIGN
|
|
142
|
-
});
|
|
143
|
-
const percent = Number(percentString);
|
|
144
|
-
const offset = percent / 100;
|
|
145
|
-
let parsedProperties = {};
|
|
146
|
-
for (const propertyName of keyframe.style) {
|
|
147
|
-
/// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue
|
|
148
|
-
const propertyValue = keyframe.style.getPropertyValue(propertyName);
|
|
149
|
-
/// https://drafts.csswg.org/web-animations-1/#ref-for-animation-property-name-to-idl-attribute-name%E2%91%A0
|
|
150
|
-
const attributeName = this.#animationPropertyNameToIDLAttributeName(propertyName);
|
|
151
|
-
parsedProperties[attributeName] = propertyValue;
|
|
152
|
-
}
|
|
153
|
-
const parsedKeyframe = {
|
|
154
|
-
...parsedProperties,
|
|
155
|
-
offset: offset
|
|
156
|
-
};
|
|
157
|
-
parsedKeyframes.push(parsedKeyframe);
|
|
158
|
-
}
|
|
159
|
-
const parsedKeyframesInstance = new ParsedKeyframes(parsedKeyframes);
|
|
160
|
-
return parsedKeyframesInstance;
|
|
161
|
-
}
|
|
162
|
-
/** https://drafts.csswg.org/web-animations-1/#animation-property-name-to-idl-attribute-name */
|
|
163
|
-
#animationPropertyNameToIDLAttributeName(property) {
|
|
164
|
-
if (this.#isCustomPropertyName(property))
|
|
165
|
-
return property;
|
|
166
|
-
if (property === 'float')
|
|
167
|
-
return 'cssFloat';
|
|
168
|
-
if (property === 'offset')
|
|
169
|
-
return 'cssOffset';
|
|
170
|
-
// https://drafts.csswg.org/cssom/#ref-for-supported-css-property%E2%91%A2
|
|
171
|
-
const lowercaseFirst = this.#isWebkitCasedAttribute(property);
|
|
172
|
-
return this.#cssPropertyToIDLAttribute(property, lowercaseFirst);
|
|
173
|
-
}
|
|
174
|
-
/** https://drafts.csswg.org/cssom/#css-property-to-idl-attribute */
|
|
175
|
-
#cssPropertyToIDLAttribute(property, lowercaseFirst = false) {
|
|
176
|
-
let output = '';
|
|
177
|
-
let uppercaseNext = false;
|
|
178
|
-
if (lowercaseFirst) {
|
|
179
|
-
property = property.slice(1);
|
|
180
|
-
}
|
|
181
|
-
for (const c of property) {
|
|
182
|
-
if (c === CHARS.HYPHEN_MINUS) {
|
|
183
|
-
uppercaseNext = true;
|
|
184
|
-
}
|
|
185
|
-
else if (uppercaseNext) {
|
|
186
|
-
uppercaseNext = false;
|
|
187
|
-
output += c.toUpperCase();
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
output += c;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return output;
|
|
194
|
-
}
|
|
195
|
-
/** https://drafts.csswg.org/css-variables-2/#typedef-custom-property-name */
|
|
196
|
-
#isCustomPropertyName(property) {
|
|
197
|
-
return property.startsWith(CHARS.DOUBLE_HYPHEN_MINUS) &&
|
|
198
|
-
property !== CHARS.DOUBLE_HYPHEN_MINUS;
|
|
199
|
-
}
|
|
200
|
-
/** https://drafts.csswg.org/cssom/#ref-for-supported-css-property%E2%91%A2 */
|
|
201
|
-
#isWebkitCasedAttribute(property) {
|
|
202
|
-
return property.startsWith(CHARS.WEBKIT_PREFIX);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
export default new KeyframesFactory();
|
|
206
|
-
/** https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface */
|
|
207
|
-
export class KeyframeEffectParameters {
|
|
208
|
-
keyframes;
|
|
209
|
-
options;
|
|
210
|
-
/**
|
|
211
|
-
* @param obj.keyframes [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats)
|
|
212
|
-
* @param obj.options [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
213
|
-
*/
|
|
214
|
-
constructor({ keyframes, options = {} }) {
|
|
215
|
-
this.keyframes = keyframes;
|
|
216
|
-
this.options = this.#parseOptionsArg(options);
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* @param obj.target An element to attach the animation to.
|
|
220
|
-
* @param obj.options Additional keyframe effect options. Can override existing keys.
|
|
221
|
-
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
222
|
-
*
|
|
223
|
-
* @see Specifications:
|
|
224
|
-
* - https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface
|
|
225
|
-
* - https://drafts.csswg.org/web-animations-1/#the-animation-interface
|
|
226
|
-
*/
|
|
227
|
-
toAnimation({ target, options: additionalOptions = {}, timeline = document.timeline }) {
|
|
228
|
-
additionalOptions = this.#parseOptionsArg(additionalOptions);
|
|
229
|
-
// override existing option keys with additional options
|
|
230
|
-
const options = {
|
|
231
|
-
...this.options, ...additionalOptions
|
|
232
|
-
};
|
|
233
|
-
const keyframeEffect = new KeyframeEffect(target, this.keyframes, options);
|
|
234
|
-
const animation = new Animation(keyframeEffect, timeline);
|
|
235
|
-
return animation;
|
|
236
|
-
}
|
|
237
|
-
/** https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-keyframeeffect-target-keyframes-options-options
|
|
238
|
-
https://drafts.csswg.org/web-animations-1/#dom-effecttiming-duration */
|
|
239
|
-
#parseOptionsArg(options) {
|
|
240
|
-
if (typeof options === 'number') {
|
|
241
|
-
return { duration: options };
|
|
242
|
-
}
|
|
243
|
-
return options;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
export class ParsedKeyframes {
|
|
247
|
-
keyframes;
|
|
248
|
-
constructor(keyframes) {
|
|
249
|
-
this.keyframes = keyframes;
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* @param options
|
|
253
|
-
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
254
|
-
*/
|
|
255
|
-
toKeyframeEffect(options) {
|
|
256
|
-
let keyframeEffect;
|
|
257
|
-
// convert (required) nullable to optional
|
|
258
|
-
if (options !== null) {
|
|
259
|
-
keyframeEffect = new KeyframeEffectParameters({
|
|
260
|
-
keyframes: this.keyframes,
|
|
261
|
-
options: options
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
keyframeEffect = new KeyframeEffectParameters({
|
|
266
|
-
keyframes: this.keyframes
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
return keyframeEffect;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
// MARK: - Util
|
|
273
|
-
function removeSuffix({ of: string, suffix }) {
|
|
274
|
-
return string.slice(0, -suffix.length);
|
|
275
|
-
}
|
|
276
|
-
async function waitForDocumentLoad({ document }) {
|
|
277
|
-
if (document.readyState === 'complete') {
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
const { promise, resolve } = Promise.withResolvers();
|
|
281
|
-
function onReadyStateChange() {
|
|
282
|
-
if (document.readyState === 'complete') {
|
|
283
|
-
resolve(null);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
const listener = [
|
|
287
|
-
'readystatechange',
|
|
288
|
-
onReadyStateChange
|
|
289
|
-
];
|
|
290
|
-
document.addEventListener(...listener);
|
|
291
|
-
await promise;
|
|
292
|
-
document.removeEventListener(...listener);
|
|
293
|
-
}
|
|
1
|
+
//
|
|
2
|
+
// KeyframeKit
|
|
3
|
+
// Ben Hatsor
|
|
4
|
+
//
|
|
5
|
+
// See README.md for usage.
|
|
6
|
+
//
|
|
7
|
+
const CHARS = {
|
|
8
|
+
PERCENT_SIGN: '%',
|
|
9
|
+
HYPHEN_MINUS: '-',
|
|
10
|
+
DOUBLE_HYPHEN_MINUS: '--',
|
|
11
|
+
WEBKIT_PREFIX: '-webkit-'
|
|
12
|
+
};
|
|
13
|
+
class KeyframesFactory {
|
|
14
|
+
Error = {
|
|
15
|
+
KeyframesRuleNameTypeError: class KeyframesRuleNameTypeError extends TypeError {
|
|
16
|
+
message = `Keyframes rule name must be a string.`;
|
|
17
|
+
},
|
|
18
|
+
SourceTypeError: class SourceTypeError extends TypeError {
|
|
19
|
+
message = `Source must be either a Document, a ShadowRoot or a CSSStyleSheet instance.`;
|
|
20
|
+
},
|
|
21
|
+
StyleSheetImportError: class StyleSheetImportError extends Error {
|
|
22
|
+
message = `The stylesheet could not be imported.`;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
async getDocumentStyleSheetsOnLoad({ document = window.document } = {}) {
|
|
26
|
+
await waitForDocumentLoad({
|
|
27
|
+
document: document
|
|
28
|
+
});
|
|
29
|
+
return document.styleSheets;
|
|
30
|
+
}
|
|
31
|
+
/** @remarks
|
|
32
|
+
* Note: `@import` rules won't be resolved in imported stylesheets.
|
|
33
|
+
* See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418. */
|
|
34
|
+
async importStyleSheet(url) {
|
|
35
|
+
const resp = await fetch(url);
|
|
36
|
+
if (!resp.ok) {
|
|
37
|
+
throw new this.Error.StyleSheetImportError();
|
|
38
|
+
}
|
|
39
|
+
const respText = await resp.text();
|
|
40
|
+
// remove file name from URL to get base URL
|
|
41
|
+
const baseURL = url.split('/').slice(0, -1).join('/');
|
|
42
|
+
const styleSheet = new CSSStyleSheet({
|
|
43
|
+
baseURL: baseURL
|
|
44
|
+
});
|
|
45
|
+
await styleSheet.replace(respText);
|
|
46
|
+
return styleSheet;
|
|
47
|
+
}
|
|
48
|
+
/** @param obj.of The name of the `@keyframes` rule to get keyframes from. */
|
|
49
|
+
getStyleSheetKeyframes({ of: ruleName, in: source }) {
|
|
50
|
+
if (typeof ruleName !== 'string') {
|
|
51
|
+
throw new this.Error.KeyframesRuleNameTypeError();
|
|
52
|
+
}
|
|
53
|
+
if (source instanceof StyleSheetList) {
|
|
54
|
+
return this.#getStyleSheetKeyframesInStyleSheetList({
|
|
55
|
+
of: ruleName,
|
|
56
|
+
styleSheetList: source
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
else if (source instanceof CSSStyleSheet) {
|
|
60
|
+
return this.#getStyleSheetKeyframesInStyleSheet({
|
|
61
|
+
of: ruleName,
|
|
62
|
+
styleSheet: source
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
throw new this.Error.SourceTypeError();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
#getStyleSheetKeyframesInStyleSheetList({ of: ruleName, styleSheetList }) {
|
|
70
|
+
for (const styleSheet of styleSheetList) {
|
|
71
|
+
const keyframesRule = this.#getStyleSheetKeyframesInStyleSheet({
|
|
72
|
+
of: ruleName,
|
|
73
|
+
styleSheet: styleSheet
|
|
74
|
+
});
|
|
75
|
+
if (keyframesRule !== undefined) {
|
|
76
|
+
return keyframesRule;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
#getStyleSheetKeyframesInStyleSheet({ of: ruleName, styleSheet }) {
|
|
81
|
+
for (const rule of styleSheet.cssRules) {
|
|
82
|
+
if (!(rule instanceof CSSKeyframesRule)) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (rule.name === ruleName) {
|
|
86
|
+
const keyframes = this.parseKeyframesRule({
|
|
87
|
+
rule: rule
|
|
88
|
+
});
|
|
89
|
+
return keyframes;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
getAllStyleSheetKeyframesRules({ in: source }) {
|
|
94
|
+
if (source instanceof StyleSheetList) {
|
|
95
|
+
return this.#getAllStyleSheetKeyframesRulesInStyleSheetList({
|
|
96
|
+
styleSheetList: source
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else if (source instanceof CSSStyleSheet) {
|
|
100
|
+
return this.#getAllStyleSheetKeyframesRulesInStyleSheet({
|
|
101
|
+
styleSheet: source
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
throw new this.Error.SourceTypeError();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
#getAllStyleSheetKeyframesRulesInStyleSheetList({ styleSheetList }) {
|
|
109
|
+
let keyframesRules = {};
|
|
110
|
+
for (const styleSheet of styleSheetList) {
|
|
111
|
+
const styleSheetKeyframesRules = this.#getAllStyleSheetKeyframesRulesInStyleSheet({
|
|
112
|
+
styleSheet: styleSheet
|
|
113
|
+
});
|
|
114
|
+
keyframesRules = {
|
|
115
|
+
...keyframesRules,
|
|
116
|
+
...styleSheetKeyframesRules
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return keyframesRules;
|
|
120
|
+
}
|
|
121
|
+
#getAllStyleSheetKeyframesRulesInStyleSheet({ styleSheet }) {
|
|
122
|
+
let keyframesRules = {};
|
|
123
|
+
for (const rule of styleSheet.cssRules) {
|
|
124
|
+
if (!(rule instanceof CSSKeyframesRule)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const keyframes = this.parseKeyframesRule({
|
|
128
|
+
rule: rule
|
|
129
|
+
});
|
|
130
|
+
keyframesRules[rule.name] = keyframes;
|
|
131
|
+
}
|
|
132
|
+
return keyframesRules;
|
|
133
|
+
}
|
|
134
|
+
parseKeyframesRule({ rule: keyframes }) {
|
|
135
|
+
let parsedKeyframes = [];
|
|
136
|
+
for (const keyframe of keyframes) {
|
|
137
|
+
// remove trailing '%'
|
|
138
|
+
/// https://drafts.csswg.org/css-animations/#dom-csskeyframerule-keytext
|
|
139
|
+
const percentString = removeSuffix({
|
|
140
|
+
of: keyframe.keyText,
|
|
141
|
+
suffix: CHARS.PERCENT_SIGN
|
|
142
|
+
});
|
|
143
|
+
const percent = Number(percentString);
|
|
144
|
+
const offset = percent / 100;
|
|
145
|
+
let parsedProperties = {};
|
|
146
|
+
for (const propertyName of keyframe.style) {
|
|
147
|
+
/// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue
|
|
148
|
+
const propertyValue = keyframe.style.getPropertyValue(propertyName);
|
|
149
|
+
/// https://drafts.csswg.org/web-animations-1/#ref-for-animation-property-name-to-idl-attribute-name%E2%91%A0
|
|
150
|
+
const attributeName = this.#animationPropertyNameToIDLAttributeName(propertyName);
|
|
151
|
+
parsedProperties[attributeName] = propertyValue;
|
|
152
|
+
}
|
|
153
|
+
const parsedKeyframe = {
|
|
154
|
+
...parsedProperties,
|
|
155
|
+
offset: offset
|
|
156
|
+
};
|
|
157
|
+
parsedKeyframes.push(parsedKeyframe);
|
|
158
|
+
}
|
|
159
|
+
const parsedKeyframesInstance = new ParsedKeyframes(parsedKeyframes);
|
|
160
|
+
return parsedKeyframesInstance;
|
|
161
|
+
}
|
|
162
|
+
/** https://drafts.csswg.org/web-animations-1/#animation-property-name-to-idl-attribute-name */
|
|
163
|
+
#animationPropertyNameToIDLAttributeName(property) {
|
|
164
|
+
if (this.#isCustomPropertyName(property))
|
|
165
|
+
return property;
|
|
166
|
+
if (property === 'float')
|
|
167
|
+
return 'cssFloat';
|
|
168
|
+
if (property === 'offset')
|
|
169
|
+
return 'cssOffset';
|
|
170
|
+
// https://drafts.csswg.org/cssom/#ref-for-supported-css-property%E2%91%A2
|
|
171
|
+
const lowercaseFirst = this.#isWebkitCasedAttribute(property);
|
|
172
|
+
return this.#cssPropertyToIDLAttribute(property, lowercaseFirst);
|
|
173
|
+
}
|
|
174
|
+
/** https://drafts.csswg.org/cssom/#css-property-to-idl-attribute */
|
|
175
|
+
#cssPropertyToIDLAttribute(property, lowercaseFirst = false) {
|
|
176
|
+
let output = '';
|
|
177
|
+
let uppercaseNext = false;
|
|
178
|
+
if (lowercaseFirst) {
|
|
179
|
+
property = property.slice(1);
|
|
180
|
+
}
|
|
181
|
+
for (const c of property) {
|
|
182
|
+
if (c === CHARS.HYPHEN_MINUS) {
|
|
183
|
+
uppercaseNext = true;
|
|
184
|
+
}
|
|
185
|
+
else if (uppercaseNext) {
|
|
186
|
+
uppercaseNext = false;
|
|
187
|
+
output += c.toUpperCase();
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
output += c;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return output;
|
|
194
|
+
}
|
|
195
|
+
/** https://drafts.csswg.org/css-variables-2/#typedef-custom-property-name */
|
|
196
|
+
#isCustomPropertyName(property) {
|
|
197
|
+
return property.startsWith(CHARS.DOUBLE_HYPHEN_MINUS) &&
|
|
198
|
+
property !== CHARS.DOUBLE_HYPHEN_MINUS;
|
|
199
|
+
}
|
|
200
|
+
/** https://drafts.csswg.org/cssom/#ref-for-supported-css-property%E2%91%A2 */
|
|
201
|
+
#isWebkitCasedAttribute(property) {
|
|
202
|
+
return property.startsWith(CHARS.WEBKIT_PREFIX);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
export default new KeyframesFactory();
|
|
206
|
+
/** https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface */
|
|
207
|
+
export class KeyframeEffectParameters {
|
|
208
|
+
keyframes;
|
|
209
|
+
options;
|
|
210
|
+
/**
|
|
211
|
+
* @param obj.keyframes [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats)
|
|
212
|
+
* @param obj.options [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
213
|
+
*/
|
|
214
|
+
constructor({ keyframes, options = {} }) {
|
|
215
|
+
this.keyframes = keyframes;
|
|
216
|
+
this.options = this.#parseOptionsArg(options);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @param obj.target An element to attach the animation to.
|
|
220
|
+
* @param obj.options Additional keyframe effect options. Can override existing keys.
|
|
221
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
222
|
+
*
|
|
223
|
+
* @see Specifications:
|
|
224
|
+
* - https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface
|
|
225
|
+
* - https://drafts.csswg.org/web-animations-1/#the-animation-interface
|
|
226
|
+
*/
|
|
227
|
+
toAnimation({ target, options: additionalOptions = {}, timeline = document.timeline }) {
|
|
228
|
+
additionalOptions = this.#parseOptionsArg(additionalOptions);
|
|
229
|
+
// override existing option keys with additional options
|
|
230
|
+
const options = {
|
|
231
|
+
...this.options, ...additionalOptions
|
|
232
|
+
};
|
|
233
|
+
const keyframeEffect = new KeyframeEffect(target, this.keyframes, options);
|
|
234
|
+
const animation = new Animation(keyframeEffect, timeline);
|
|
235
|
+
return animation;
|
|
236
|
+
}
|
|
237
|
+
/** https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-keyframeeffect-target-keyframes-options-options
|
|
238
|
+
https://drafts.csswg.org/web-animations-1/#dom-effecttiming-duration */
|
|
239
|
+
#parseOptionsArg(options) {
|
|
240
|
+
if (typeof options === 'number') {
|
|
241
|
+
return { duration: options };
|
|
242
|
+
}
|
|
243
|
+
return options;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
export class ParsedKeyframes {
|
|
247
|
+
keyframes;
|
|
248
|
+
constructor(keyframes) {
|
|
249
|
+
this.keyframes = keyframes;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @param options
|
|
253
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
254
|
+
*/
|
|
255
|
+
toKeyframeEffect(options) {
|
|
256
|
+
let keyframeEffect;
|
|
257
|
+
// convert (required) nullable to optional
|
|
258
|
+
if (options !== null) {
|
|
259
|
+
keyframeEffect = new KeyframeEffectParameters({
|
|
260
|
+
keyframes: this.keyframes,
|
|
261
|
+
options: options
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
keyframeEffect = new KeyframeEffectParameters({
|
|
266
|
+
keyframes: this.keyframes
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
return keyframeEffect;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
// MARK: - Util
|
|
273
|
+
function removeSuffix({ of: string, suffix }) {
|
|
274
|
+
return string.slice(0, -suffix.length);
|
|
275
|
+
}
|
|
276
|
+
async function waitForDocumentLoad({ document }) {
|
|
277
|
+
if (document.readyState === 'complete') {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
281
|
+
function onReadyStateChange() {
|
|
282
|
+
if (document.readyState === 'complete') {
|
|
283
|
+
resolve(null);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
const listener = [
|
|
287
|
+
'readystatechange',
|
|
288
|
+
onReadyStateChange
|
|
289
|
+
];
|
|
290
|
+
document.addEventListener(...listener);
|
|
291
|
+
await promise;
|
|
292
|
+
document.removeEventListener(...listener);
|
|
293
|
+
}
|
|
294
294
|
//# sourceMappingURL=KeyframeKit.js.map
|