@editframe/elements 0.30.1-beta.0 → 0.30.2-beta.0
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/dist/elements/EFAudio.d.ts +2 -2
- package/dist/elements/EFCaptions.d.ts +12 -12
- package/dist/elements/EFImage.d.ts +4 -4
- package/dist/elements/EFMedia.d.ts +2 -2
- package/dist/elements/EFTextSegment.js.map +1 -1
- package/dist/elements/EFTimegroup.js +3 -3
- package/dist/elements/EFTimegroup.js.map +1 -1
- package/dist/elements/EFVideo.d.ts +4 -4
- package/dist/elements/EFWaveform.d.ts +4 -4
- package/dist/elements/updateAnimations.js +348 -108
- package/dist/elements/updateAnimations.js.map +1 -1
- package/dist/gui/EFFilmstrip.d.ts +3 -3
- package/dist/gui/EFPause.d.ts +4 -4
- package/dist/gui/EFPlay.d.ts +4 -4
- package/dist/gui/EFPreview.d.ts +4 -4
- package/dist/gui/EFScrubber.d.ts +4 -4
- package/dist/gui/EFTimeDisplay.d.ts +4 -4
- package/dist/gui/EFToggleLoop.d.ts +4 -4
- package/dist/gui/EFTogglePlay.d.ts +4 -4
- package/dist/gui/EFWorkbench.d.ts +4 -4
- package/dist/gui/TWMixin.js +1 -1
- package/dist/gui/TWMixin.js.map +1 -1
- package/dist/style.css +0 -4
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { deepGetTemporalElements } from "./EFTemporal.js";
|
|
1
|
+
import { deepGetTemporalElements, isEFTemporal } from "./EFTemporal.js";
|
|
2
2
|
|
|
3
3
|
//#region src/elements/updateAnimations.ts
|
|
4
4
|
const ANIMATION_PRECISION_OFFSET = .1;
|
|
@@ -8,144 +8,384 @@ const DURATION_PROPERTY = "--ef-duration";
|
|
|
8
8
|
const TRANSITION_DURATION_PROPERTY = "--ef-transition-duration";
|
|
9
9
|
const TRANSITION_OUT_START_PROPERTY = "--ef-transition-out-start";
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* Determines what phase an element is in relative to the timeline.
|
|
12
|
+
*
|
|
13
|
+
* WHY: Phase is the primary concept that drives all decisions. By explicitly
|
|
14
|
+
* enumerating phases, we make the code's logic clear: phase determines visibility,
|
|
15
|
+
* animation coordination, and visual state.
|
|
16
|
+
*
|
|
17
|
+
* Phases:
|
|
18
|
+
* - before-start: Timeline is before element's start time
|
|
19
|
+
* - active: Timeline is within element's active range (start to end, exclusive of end)
|
|
20
|
+
* - at-end-boundary: Timeline is exactly at element's end time
|
|
21
|
+
* - after-end: Timeline is after element's end time
|
|
22
|
+
*
|
|
23
|
+
* Note: We detect "at-end-boundary" by checking if timeline equals end time.
|
|
24
|
+
* The boundary policy will then determine if this should be treated as visible/active
|
|
25
|
+
* or not based on element characteristics.
|
|
26
|
+
*/
|
|
27
|
+
const determineElementPhase = (element, timelineTimeMs) => {
|
|
28
|
+
const endTimeMs = element.endTimeMs;
|
|
29
|
+
if (timelineTimeMs < element.startTimeMs) return "before-start";
|
|
30
|
+
const epsilon = .001;
|
|
31
|
+
const diff = timelineTimeMs - endTimeMs;
|
|
32
|
+
if (diff > epsilon) return "after-end";
|
|
33
|
+
if (Math.abs(diff) <= epsilon) return "at-end-boundary";
|
|
34
|
+
return "active";
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Visibility policy: determines when elements should be visible for display purposes.
|
|
38
|
+
*
|
|
39
|
+
* WHY: Root elements, elements aligned with composition end, and text segments
|
|
40
|
+
* should remain visible at exact end time to prevent flicker and show final frames.
|
|
41
|
+
* Other elements use exclusive end for clean transitions between elements.
|
|
42
|
+
*/
|
|
43
|
+
var VisibilityPolicy = class {
|
|
44
|
+
shouldIncludeEndBoundary(element) {
|
|
45
|
+
if (!element.parentTimegroup) return true;
|
|
46
|
+
if (element.endTimeMs === element.rootTimegroup?.endTimeMs) return true;
|
|
47
|
+
if (this.isTextSegment(element)) return true;
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Checks if element is a text segment.
|
|
52
|
+
* Encapsulates the tag name check to hide implementation detail.
|
|
53
|
+
*/
|
|
54
|
+
isTextSegment(element) {
|
|
55
|
+
return element.tagName === "EF-TEXT-SEGMENT";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Animation policy: determines when animations should be coordinated.
|
|
60
|
+
*
|
|
61
|
+
* WHY: When an animation reaches exactly the end time of an element, using exclusive
|
|
62
|
+
* end would make the element invisible, causing the animation to be removed from the
|
|
63
|
+
* DOM and creating a visual jump. By using inclusive end, we ensure animations remain
|
|
64
|
+
* coordinated even at exact boundary times, providing smooth visual transitions.
|
|
65
|
+
*/
|
|
66
|
+
var AnimationPolicy = class {
|
|
67
|
+
shouldIncludeEndBoundary(_element) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const visibilityPolicy = new VisibilityPolicy();
|
|
72
|
+
const animationPolicy = new AnimationPolicy();
|
|
73
|
+
/**
|
|
74
|
+
* Determines if an element should be visible based on its phase and visibility policy.
|
|
75
|
+
*/
|
|
76
|
+
const shouldBeVisible = (phase, element) => {
|
|
77
|
+
if (phase === "before-start" || phase === "after-end") return false;
|
|
78
|
+
if (phase === "active") return true;
|
|
79
|
+
return visibilityPolicy.shouldIncludeEndBoundary(element);
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Determines if animations should be coordinated based on element phase and animation policy.
|
|
83
|
+
*/
|
|
84
|
+
const shouldCoordinateAnimations = (phase, element) => {
|
|
85
|
+
if (phase === "before-start" || phase === "after-end") return false;
|
|
86
|
+
if (phase === "active") return true;
|
|
87
|
+
return animationPolicy.shouldIncludeEndBoundary(element);
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Evaluates what the element's state should be based on the timeline.
|
|
91
|
+
*
|
|
92
|
+
* WHY: This function determines the complete temporal state including phase,
|
|
93
|
+
* which becomes the primary driver for all subsequent decisions.
|
|
12
94
|
*/
|
|
13
95
|
const evaluateTemporalState = (element) => {
|
|
14
96
|
const timelineTimeMs = (element.rootTimegroup ?? element).currentTimeMs;
|
|
15
97
|
const progress = element.durationMs <= 0 ? 1 : Math.max(0, Math.min(1, element.currentTimeMs / element.durationMs));
|
|
16
|
-
const
|
|
17
|
-
const isLastElementInComposition = element.endTimeMs === element.rootTimegroup?.endTimeMs;
|
|
18
|
-
const isTextSegment = element.tagName === "EF-TEXT-SEGMENT";
|
|
19
|
-
const useInclusiveEnd = isRootElement || isLastElementInComposition || isTextSegment;
|
|
98
|
+
const phase = determineElementPhase(element, timelineTimeMs);
|
|
20
99
|
return {
|
|
21
100
|
progress,
|
|
22
|
-
isVisible:
|
|
23
|
-
timelineTimeMs
|
|
101
|
+
isVisible: shouldBeVisible(phase, element),
|
|
102
|
+
timelineTimeMs,
|
|
103
|
+
phase
|
|
24
104
|
};
|
|
25
105
|
};
|
|
26
106
|
/**
|
|
27
|
-
* Evaluates element visibility specifically for animation coordination
|
|
28
|
-
* Uses inclusive end boundaries to prevent animation jumps at exact boundaries
|
|
107
|
+
* Evaluates element visibility state specifically for animation coordination.
|
|
108
|
+
* Uses inclusive end boundaries to prevent animation jumps at exact boundaries.
|
|
109
|
+
*
|
|
110
|
+
* This is exported for external use cases that need animation-specific visibility
|
|
111
|
+
* evaluation without the full ElementUpdateContext.
|
|
29
112
|
*/
|
|
30
|
-
const
|
|
31
|
-
const
|
|
113
|
+
const evaluateAnimationVisibilityState = (element) => {
|
|
114
|
+
const state = evaluateTemporalState(element);
|
|
115
|
+
const shouldCoordinate = shouldCoordinateAnimations(state.phase, element);
|
|
32
116
|
return {
|
|
33
|
-
|
|
34
|
-
isVisible:
|
|
35
|
-
timelineTimeMs
|
|
117
|
+
...state,
|
|
118
|
+
isVisible: shouldCoordinate
|
|
36
119
|
};
|
|
37
120
|
};
|
|
38
121
|
/**
|
|
39
|
-
*
|
|
122
|
+
* Capability check: determines if an element supports stagger offset.
|
|
123
|
+
* Encapsulates the knowledge of which element types support this feature.
|
|
40
124
|
*/
|
|
41
|
-
const
|
|
42
|
-
element.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
125
|
+
const supportsStaggerOffset = (element) => {
|
|
126
|
+
return element.tagName === "EF-TEXT-SEGMENT";
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Calculates effective delay including stagger offset if applicable.
|
|
130
|
+
*
|
|
131
|
+
* Stagger offset allows elements (like text segments) to have their animations
|
|
132
|
+
* start at different times while keeping their visibility timing unchanged.
|
|
133
|
+
* This enables staggered animation effects within a single timegroup.
|
|
134
|
+
*/
|
|
135
|
+
const calculateEffectiveDelay = (delay, element) => {
|
|
136
|
+
if (supportsStaggerOffset(element) && element.staggerOffsetMs !== void 0) return delay + element.staggerOffsetMs;
|
|
137
|
+
return delay;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Calculates maximum safe animation time to prevent completion.
|
|
141
|
+
*
|
|
142
|
+
* WHY: Once an animation reaches "finished" state, it can no longer be manually controlled
|
|
143
|
+
* via currentTime. By clamping to just before completion (using ANIMATION_PRECISION_OFFSET),
|
|
144
|
+
* we ensure the animation remains in a controllable state, allowing us to synchronize it
|
|
145
|
+
* with the timeline even when it would naturally be complete.
|
|
146
|
+
*/
|
|
147
|
+
const calculateMaxSafeAnimationTime = (duration, iterations) => {
|
|
148
|
+
return duration * iterations - ANIMATION_PRECISION_OFFSET;
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* Determines if the current iteration should be reversed based on direction
|
|
152
|
+
*/
|
|
153
|
+
const shouldReverseIteration = (direction, currentIteration) => {
|
|
154
|
+
return direction === "reverse" || direction === "alternate" && currentIteration % 2 === 1 || direction === "alternate-reverse" && currentIteration % 2 === 0;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Applies direction to iteration time (reverses if needed)
|
|
158
|
+
*/
|
|
159
|
+
const applyDirectionToIterationTime = (currentIterationTime, duration, direction, currentIteration) => {
|
|
160
|
+
if (shouldReverseIteration(direction, currentIteration)) return duration - currentIterationTime;
|
|
161
|
+
return currentIterationTime;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Maps element time to animation time for normal direction.
|
|
165
|
+
* Uses cumulative time throughout the animation.
|
|
166
|
+
* Note: elementTime should already be adjusted (elementTime - effectiveDelay).
|
|
167
|
+
*/
|
|
168
|
+
const mapNormalDirectionTime = (elementTime, duration, maxSafeTime) => {
|
|
169
|
+
const currentIteration = Math.floor(elementTime / duration);
|
|
170
|
+
const iterationTime = elementTime % duration;
|
|
171
|
+
const cumulativeTime = currentIteration * duration + iterationTime;
|
|
172
|
+
return Math.min(cumulativeTime, maxSafeTime);
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Maps element time to animation time for reverse direction.
|
|
176
|
+
* Uses cumulative time with reversed iterations.
|
|
177
|
+
* Note: elementTime should already be adjusted (elementTime - effectiveDelay).
|
|
178
|
+
*/
|
|
179
|
+
const mapReverseDirectionTime = (elementTime, duration, maxSafeTime) => {
|
|
180
|
+
const currentIteration = Math.floor(elementTime / duration);
|
|
181
|
+
const reversedIterationTime = duration - elementTime % duration;
|
|
182
|
+
const cumulativeTime = currentIteration * duration + reversedIterationTime;
|
|
183
|
+
return Math.min(cumulativeTime, maxSafeTime);
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Maps element time to animation time for alternate/alternate-reverse directions.
|
|
187
|
+
*
|
|
188
|
+
* WHY SPECIAL HANDLING: Alternate directions oscillate between forward and reverse iterations.
|
|
189
|
+
* Without delay, we use iteration time (0 to duration) because the animation naturally
|
|
190
|
+
* resets each iteration. However, with delay, iteration 0 needs to account for the delay
|
|
191
|
+
* offset (using ownCurrentTimeMs), and later iterations need cumulative time to properly
|
|
192
|
+
* track progress across multiple iterations. This complexity requires a dedicated mapper
|
|
193
|
+
* rather than trying to handle it in the general case.
|
|
194
|
+
*/
|
|
195
|
+
const mapAlternateDirectionTime = (elementTime, effectiveDelay, duration, direction, maxSafeTime) => {
|
|
196
|
+
const adjustedTime = elementTime - effectiveDelay;
|
|
197
|
+
if (effectiveDelay > 0) {
|
|
198
|
+
if (Math.floor(adjustedTime / duration) === 0) return Math.min(elementTime, maxSafeTime);
|
|
199
|
+
return Math.min(adjustedTime, maxSafeTime);
|
|
46
200
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
element.style.setProperty(TRANSITION_OUT_START_PROPERTY, `${element.durationMs - (element.parentTimegroup?.overlapMs ?? 0)}ms`);
|
|
201
|
+
const currentIteration = Math.floor(elementTime / duration);
|
|
202
|
+
const iterationTime = applyDirectionToIterationTime(elementTime % duration, duration, direction, currentIteration);
|
|
203
|
+
return Math.min(iterationTime, maxSafeTime);
|
|
51
204
|
};
|
|
52
205
|
/**
|
|
53
|
-
*
|
|
206
|
+
* Maps element time to animation time based on direction.
|
|
207
|
+
*
|
|
208
|
+
* WHY: This function explicitly transforms element time to animation time, making
|
|
209
|
+
* the time mapping concept clear. Different directions require different transformations
|
|
210
|
+
* to achieve the desired visual effect.
|
|
54
211
|
*/
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
212
|
+
const mapElementTimeToAnimationTime = (elementTime, timing, effectiveDelay) => {
|
|
213
|
+
const { duration, iterations, direction } = timing;
|
|
214
|
+
const maxSafeTime = calculateMaxSafeAnimationTime(duration, iterations);
|
|
215
|
+
const adjustedTime = elementTime - effectiveDelay;
|
|
216
|
+
if (direction === "reverse") return mapReverseDirectionTime(adjustedTime, duration, maxSafeTime);
|
|
217
|
+
if (direction === "alternate" || direction === "alternate-reverse") return mapAlternateDirectionTime(elementTime, effectiveDelay, duration, direction, maxSafeTime);
|
|
218
|
+
return mapNormalDirectionTime(adjustedTime, duration, maxSafeTime);
|
|
219
|
+
};
|
|
220
|
+
/**
|
|
221
|
+
* Determines the animation time for a completed animation based on direction.
|
|
222
|
+
*/
|
|
223
|
+
const getCompletedAnimationTime = (timing, maxSafeTime) => {
|
|
224
|
+
const { direction, iterations, duration } = timing;
|
|
225
|
+
if (direction === "alternate" || direction === "alternate-reverse") {
|
|
226
|
+
const finalIteration = iterations - 1;
|
|
227
|
+
if (direction === "alternate" && finalIteration % 2 === 1 || direction === "alternate-reverse" && finalIteration % 2 === 0) return Math.min(duration - ANIMATION_PRECISION_OFFSET, maxSafeTime);
|
|
228
|
+
}
|
|
229
|
+
return maxSafeTime;
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Validates that animation effect is a KeyframeEffect with a target
|
|
233
|
+
*/
|
|
234
|
+
const validateAnimationEffect = (effect) => {
|
|
235
|
+
return effect !== null && effect instanceof KeyframeEffect && effect.target !== null;
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* Extracts timing information from an animation effect.
|
|
239
|
+
* Duration and delay from getTiming() are already in milliseconds.
|
|
240
|
+
* We use getTiming().delay directly from the animation object.
|
|
241
|
+
*/
|
|
242
|
+
const extractAnimationTiming = (effect) => {
|
|
243
|
+
const timing = effect.getTiming();
|
|
244
|
+
return {
|
|
245
|
+
duration: Number(timing.duration) || 0,
|
|
246
|
+
delay: Number(timing.delay) || 0,
|
|
247
|
+
iterations: Number(timing.iterations) || DEFAULT_ANIMATION_ITERATIONS,
|
|
248
|
+
direction: timing.direction || "normal"
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Prepares animation for manual control by ensuring it's paused
|
|
253
|
+
*/
|
|
254
|
+
const prepareAnimation = (animation) => {
|
|
255
|
+
if (animation.playState === "finished") {
|
|
256
|
+
animation.cancel();
|
|
257
|
+
animation.play();
|
|
258
|
+
animation.pause();
|
|
259
|
+
} else if (animation.playState === "running") animation.pause();
|
|
260
|
+
else {
|
|
261
|
+
if (animation.currentTime === null) {
|
|
60
262
|
animation.play();
|
|
61
263
|
animation.pause();
|
|
62
|
-
} else if (animation.playState === "running") animation.pause();
|
|
63
|
-
const effect = animation.effect;
|
|
64
|
-
if (!(effect && effect instanceof KeyframeEffect)) continue;
|
|
65
|
-
const target = effect.target;
|
|
66
|
-
if (!target) continue;
|
|
67
|
-
const timing = effect.getTiming();
|
|
68
|
-
const duration = Number(timing.duration) || 0;
|
|
69
|
-
let delay = Number(timing.delay) || 0;
|
|
70
|
-
if (target instanceof HTMLElement) {
|
|
71
|
-
const animationDelays = window.getComputedStyle(target).animationDelay.split(", ").map((s) => s.trim());
|
|
72
|
-
const parseDelay = (delayStr) => {
|
|
73
|
-
if (delayStr === "0s" || delayStr === "0ms") return 0;
|
|
74
|
-
const delayMatch = delayStr.match(/^([\d.]+)(s|ms)$/);
|
|
75
|
-
if (delayMatch?.[1] && delayMatch[2]) {
|
|
76
|
-
const value = Number.parseFloat(delayMatch[1]);
|
|
77
|
-
return delayMatch[2] === "s" ? value * 1e3 : value;
|
|
78
|
-
}
|
|
79
|
-
return 0;
|
|
80
|
-
};
|
|
81
|
-
if (animationDelays.length === 1 && animationDelays[0]) {
|
|
82
|
-
const parsedDelay = parseDelay(animationDelays[0]);
|
|
83
|
-
if (parsedDelay > 0) delay = parsedDelay;
|
|
84
|
-
else if ((animationDelays[0] === "0s" || animationDelays[0] === "0ms") && delay === 0) delay = 0;
|
|
85
|
-
} else if (animationDelays.length > 1) {
|
|
86
|
-
const animationIndex = Array.from(target.getAnimations()).indexOf(animation);
|
|
87
|
-
if (animationIndex >= 0 && animationIndex < animationDelays.length && animationDelays[animationIndex]) {
|
|
88
|
-
const parsedDelay = parseDelay(animationDelays[animationIndex]);
|
|
89
|
-
if (parsedDelay > 0) delay = parsedDelay;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
264
|
}
|
|
93
|
-
|
|
94
|
-
if (duration <= 0) {
|
|
265
|
+
if (animation.playState === "idle") {
|
|
95
266
|
animation.currentTime = 0;
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
const currentTime = element.ownCurrentTimeMs ?? 0;
|
|
99
|
-
let effectiveDelay = delay;
|
|
100
|
-
if (element.tagName === "EF-TEXT-SEGMENT" && element.staggerOffsetMs !== void 0) {
|
|
101
|
-
const staggerOffsetMs = element.staggerOffsetMs;
|
|
102
|
-
effectiveDelay = delay + staggerOffsetMs;
|
|
103
|
-
}
|
|
104
|
-
if (currentTime < effectiveDelay) {
|
|
105
|
-
animation.currentTime = 0;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const adjustedTime = currentTime - effectiveDelay;
|
|
109
|
-
const currentIteration = Math.floor(adjustedTime / duration);
|
|
110
|
-
let currentIterationTime = adjustedTime % duration;
|
|
111
|
-
const direction = timing.direction || "normal";
|
|
112
|
-
const isAlternate = direction === "alternate" || direction === "alternate-reverse";
|
|
113
|
-
if (direction === "reverse" || direction === "alternate" && currentIteration % 2 === 1 || direction === "alternate-reverse" && currentIteration % 2 === 0) currentIterationTime = duration - currentIterationTime;
|
|
114
|
-
if (currentIteration >= iterations) {
|
|
115
|
-
const maxSafeAnimationTime = duration * iterations - ANIMATION_PRECISION_OFFSET;
|
|
116
|
-
if (isAlternate) {
|
|
117
|
-
const finalIteration = iterations - 1;
|
|
118
|
-
if (direction === "alternate" && finalIteration % 2 === 1 || direction === "alternate-reverse" && finalIteration % 2 === 0) animation.currentTime = Math.min(duration - ANIMATION_PRECISION_OFFSET, maxSafeAnimationTime);
|
|
119
|
-
else animation.currentTime = maxSafeAnimationTime;
|
|
120
|
-
} else animation.currentTime = maxSafeAnimationTime;
|
|
121
|
-
} else if (isAlternate) if (effectiveDelay > 0) if (currentIteration === 0) animation.currentTime = currentTime;
|
|
122
|
-
else {
|
|
123
|
-
const maxSafeAnimationTime = duration * iterations - ANIMATION_PRECISION_OFFSET;
|
|
124
|
-
animation.currentTime = Math.min(adjustedTime, maxSafeAnimationTime);
|
|
125
|
-
}
|
|
126
|
-
else animation.currentTime = currentIterationTime;
|
|
127
|
-
else {
|
|
128
|
-
const timeWithinAnimation = currentIteration * duration + currentIterationTime;
|
|
129
|
-
const maxSafeAnimationTime = duration * iterations - ANIMATION_PRECISION_OFFSET;
|
|
130
|
-
animation.currentTime = Math.min(timeWithinAnimation, maxSafeAnimationTime);
|
|
267
|
+
animation.pause();
|
|
131
268
|
}
|
|
132
269
|
}
|
|
133
270
|
};
|
|
134
271
|
/**
|
|
135
|
-
*
|
|
272
|
+
* Maps element time to animation currentTime and sets it on the animation.
|
|
273
|
+
*
|
|
274
|
+
* WHY: This function explicitly performs the time mapping transformation,
|
|
275
|
+
* making it clear that we're transforming element time to animation time.
|
|
276
|
+
*/
|
|
277
|
+
const mapAndSetAnimationTime = (animation, element, timing, effectiveDelay) => {
|
|
278
|
+
const elementTime = element.ownCurrentTimeMs ?? 0;
|
|
279
|
+
if (animation.playState === "running") animation.pause();
|
|
280
|
+
const adjustedTime = elementTime - effectiveDelay;
|
|
281
|
+
if (adjustedTime < 0) {
|
|
282
|
+
animation.currentTime = elementTime;
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const { duration, iterations } = timing;
|
|
286
|
+
const currentIteration = Math.floor(adjustedTime / duration);
|
|
287
|
+
if (currentIteration >= iterations) animation.currentTime = effectiveDelay + getCompletedAnimationTime(timing, calculateMaxSafeAnimationTime(duration, iterations));
|
|
288
|
+
else {
|
|
289
|
+
const animationTime = mapElementTimeToAnimationTime(elementTime, timing, effectiveDelay);
|
|
290
|
+
const { direction } = timing;
|
|
291
|
+
if ((direction === "alternate" || direction === "alternate-reverse") && effectiveDelay > 0 && currentIteration === 0) animation.currentTime = elementTime;
|
|
292
|
+
else animation.currentTime = effectiveDelay + animationTime;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* Synchronizes a single animation with the timeline using the element as the time source.
|
|
297
|
+
*
|
|
298
|
+
* For animations in this element's subtree, always use this element as the time source.
|
|
299
|
+
* This handles both animations directly on the temporal element and on its non-temporal children.
|
|
300
|
+
*/
|
|
301
|
+
const synchronizeAnimation = (animation, element) => {
|
|
302
|
+
const effect = animation.effect;
|
|
303
|
+
if (!validateAnimationEffect(effect)) return;
|
|
304
|
+
const timing = extractAnimationTiming(effect);
|
|
305
|
+
if (timing.duration <= 0) {
|
|
306
|
+
animation.currentTime = 0;
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const target = effect.target;
|
|
310
|
+
let timeSource = element;
|
|
311
|
+
if (target && target instanceof HTMLElement) {
|
|
312
|
+
const nearestTimegroup = target.closest("ef-timegroup");
|
|
313
|
+
if (nearestTimegroup && isEFTemporal(nearestTimegroup)) timeSource = nearestTimegroup;
|
|
314
|
+
}
|
|
315
|
+
const effectiveDelay = calculateEffectiveDelay(timing.delay, timeSource);
|
|
316
|
+
mapAndSetAnimationTime(animation, timeSource, timing, effectiveDelay);
|
|
317
|
+
};
|
|
318
|
+
/**
|
|
319
|
+
* Coordinates animations for a single element and its subtree, using the element as the time source.
|
|
320
|
+
*
|
|
321
|
+
* Gets animations on the element itself and its subtree.
|
|
322
|
+
* Both CSS animations (created via the 'animation' property) and WAAPI animations are included.
|
|
323
|
+
*/
|
|
324
|
+
const coordinateElementAnimations = (element) => {
|
|
325
|
+
const animations = element.getAnimations({ subtree: true });
|
|
326
|
+
for (const animation of animations) {
|
|
327
|
+
prepareAnimation(animation);
|
|
328
|
+
synchronizeAnimation(animation, element);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* Applies visual state (CSS + display) to match temporal state.
|
|
333
|
+
*
|
|
334
|
+
* WHY: This function applies visual state based on the element's phase and state.
|
|
335
|
+
* Phase determines what should be visible, and this function applies that decision.
|
|
336
|
+
*/
|
|
337
|
+
const applyVisualState = (element, state) => {
|
|
338
|
+
element.style.setProperty(PROGRESS_PROPERTY, `${state.progress * 100}%`);
|
|
339
|
+
if (!state.isVisible) {
|
|
340
|
+
element.style.setProperty("display", "none");
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
element.style.removeProperty("display");
|
|
344
|
+
element.style.setProperty(DURATION_PROPERTY, `${element.durationMs}ms`);
|
|
345
|
+
element.style.setProperty(TRANSITION_DURATION_PROPERTY, `${element.parentTimegroup?.overlapMs ?? 0}ms`);
|
|
346
|
+
element.style.setProperty(TRANSITION_OUT_START_PROPERTY, `${element.durationMs - (element.parentTimegroup?.overlapMs ?? 0)}ms`);
|
|
347
|
+
};
|
|
348
|
+
/**
|
|
349
|
+
* Applies animation coordination if the element phase requires it.
|
|
350
|
+
*
|
|
351
|
+
* WHY: Animation coordination is driven by phase. If the element is in a phase
|
|
352
|
+
* where animations should be coordinated, we coordinate them.
|
|
353
|
+
*/
|
|
354
|
+
const applyAnimationCoordination = (element, phase) => {
|
|
355
|
+
if (shouldCoordinateAnimations(phase, element)) coordinateElementAnimations(element);
|
|
356
|
+
};
|
|
357
|
+
/**
|
|
358
|
+
* Evaluates the complete state for an element update.
|
|
359
|
+
* This separates evaluation (what should the state be?) from application (apply that state).
|
|
360
|
+
*/
|
|
361
|
+
const evaluateElementState = (element) => {
|
|
362
|
+
return {
|
|
363
|
+
element,
|
|
364
|
+
state: evaluateTemporalState(element)
|
|
365
|
+
};
|
|
366
|
+
};
|
|
367
|
+
/**
|
|
368
|
+
* Main function: synchronizes DOM element with timeline.
|
|
369
|
+
*
|
|
370
|
+
* Orchestrates clear flow: Phase → Policy → Time Mapping → State Application
|
|
371
|
+
*
|
|
372
|
+
* WHY: This function makes the conceptual flow explicit:
|
|
373
|
+
* 1. Determine phase (what phase is the element in?)
|
|
374
|
+
* 2. Apply policies (should it be visible/coordinated based on phase?)
|
|
375
|
+
* 3. Map time for animations (transform element time to animation time)
|
|
376
|
+
* 4. Apply visual state (update CSS and display based on phase and policies)
|
|
136
377
|
*/
|
|
137
378
|
const updateAnimations = (element) => {
|
|
138
|
-
const
|
|
139
|
-
deepGetTemporalElements(element).
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (evaluateTemporalStateForAnimation(temporalElement).isVisible) coordinateAnimationsForSingleElement(temporalElement);
|
|
379
|
+
const rootContext = evaluateElementState(element);
|
|
380
|
+
const childContexts = deepGetTemporalElements(element).map((temporalElement) => evaluateElementState(temporalElement));
|
|
381
|
+
applyVisualState(rootContext.element, rootContext.state);
|
|
382
|
+
applyAnimationCoordination(rootContext.element, rootContext.state.phase);
|
|
383
|
+
childContexts.forEach((context) => {
|
|
384
|
+
applyVisualState(context.element, context.state);
|
|
385
|
+
applyAnimationCoordination(context.element, context.state.phase);
|
|
146
386
|
});
|
|
147
387
|
};
|
|
148
388
|
|
|
149
389
|
//#endregion
|
|
150
|
-
export {
|
|
390
|
+
export { evaluateAnimationVisibilityState, updateAnimations };
|
|
151
391
|
//# sourceMappingURL=updateAnimations.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateAnimations.js","names":[],"sources":["../../src/elements/updateAnimations.ts"],"sourcesContent":["import {\n deepGetTemporalElements,\n type TemporalMixinInterface,\n} from \"./EFTemporal.ts\";\n\n// All animatable elements are temporal elements with HTMLElement interface\nexport type AnimatableElement = TemporalMixinInterface & HTMLElement;\n\n// Constants\nconst ANIMATION_PRECISION_OFFSET = 0.1; // Use 0.1ms to safely avoid completion threshold\nconst DEFAULT_ANIMATION_ITERATIONS = 1;\nconst PROGRESS_PROPERTY = \"--ef-progress\";\nconst DURATION_PROPERTY = \"--ef-duration\";\nconst TRANSITION_DURATION_PROPERTY = \"--ef-transition-duration\";\nconst TRANSITION_OUT_START_PROPERTY = \"--ef-transition-out-start\";\n\n/**\n * Represents the temporal state of an element relative to the timeline\n */\ninterface TemporalState {\n progress: number;\n isVisible: boolean;\n timelineTimeMs: number;\n}\n\n/**\n * Evaluates what the element's state should be based on the timeline\n */\nexport const evaluateTemporalState = (\n element: AnimatableElement,\n): TemporalState => {\n // Get timeline time from root timegroup, or use element's own time if it IS a timegroup\n const timelineTimeMs = (element.rootTimegroup ?? element).currentTimeMs;\n\n const progress =\n element.durationMs <= 0\n ? 1\n : Math.max(0, Math.min(1, element.currentTimeMs / element.durationMs));\n\n // Root elements and elements aligned with composition end should remain visible at exact end time\n // Text segments should also use inclusive end since they're meant to be visible for full duration\n // Other elements use exclusive end for clean transitions\n const isRootElement = !(element as any).parentTimegroup;\n const isLastElementInComposition =\n element.endTimeMs === element.rootTimegroup?.endTimeMs;\n const isTextSegment = element.tagName === \"EF-TEXT-SEGMENT\";\n const useInclusiveEnd =\n isRootElement || isLastElementInComposition || isTextSegment;\n\n const isVisible =\n element.startTimeMs <= timelineTimeMs &&\n (useInclusiveEnd\n ? element.endTimeMs >= timelineTimeMs\n : element.endTimeMs > timelineTimeMs);\n\n return { progress, isVisible, timelineTimeMs };\n};\n\n/**\n * Evaluates element visibility specifically for animation coordination\n * Uses inclusive end boundaries to prevent animation jumps at exact boundaries\n */\nexport const evaluateTemporalStateForAnimation = (\n element: AnimatableElement,\n): TemporalState => {\n // Get timeline time from root timegroup, or use element's own time if it IS a timegroup\n const timelineTimeMs = (element.rootTimegroup ?? element).currentTimeMs;\n\n const progress =\n element.durationMs <= 0\n ? 1\n : Math.max(0, Math.min(1, element.currentTimeMs / element.durationMs));\n\n // For animation coordination, use inclusive end for ALL elements to prevent visual jumps\n const isVisible =\n element.startTimeMs <= timelineTimeMs &&\n element.endTimeMs >= timelineTimeMs;\n\n return { progress, isVisible, timelineTimeMs };\n};\n\n/**\n * Updates the visual state (CSS + display) to match temporal state\n */\nconst updateVisualState = (\n element: AnimatableElement,\n state: TemporalState,\n): void => {\n // Always set progress (needed for many use cases)\n element.style.setProperty(PROGRESS_PROPERTY, `${state.progress * 100}%`);\n\n // Handle visibility\n if (!state.isVisible) {\n if (element.style.display !== \"none\") {\n element.style.display = \"none\";\n }\n return;\n }\n\n if (element.style.display === \"none\") {\n element.style.display = \"\";\n }\n\n // Set other CSS properties for visible elements only\n element.style.setProperty(DURATION_PROPERTY, `${element.durationMs}ms`);\n element.style.setProperty(\n TRANSITION_DURATION_PROPERTY,\n `${element.parentTimegroup?.overlapMs ?? 0}ms`,\n );\n element.style.setProperty(\n TRANSITION_OUT_START_PROPERTY,\n `${element.durationMs - (element.parentTimegroup?.overlapMs ?? 0)}ms`,\n );\n};\n\n/**\n * Coordinates animations for a single element and its subtree, using the element as the time source\n */\nconst coordinateAnimationsForSingleElement = (\n element: AnimatableElement,\n): void => {\n // Get animations on the element itself and its subtree\n // CSS animations created via the 'animation' property are included\n const animations = element.getAnimations({ subtree: true });\n\n for (const animation of animations) {\n // Ensure animation is in a playable state (not finished)\n // Finished animations can't be controlled, so reset them\n if (animation.playState === \"finished\") {\n animation.cancel();\n // Re-initialize the animation so it can be controlled\n animation.play();\n animation.pause();\n } else if (animation.playState === \"running\") {\n // Pause running animations so we can control them manually\n animation.pause();\n }\n\n const effect = animation.effect;\n if (!(effect && effect instanceof KeyframeEffect)) {\n continue;\n }\n\n const target = effect.target;\n if (!target) {\n continue;\n }\n\n // For animations in this element's subtree, always use this element as the time source\n // This handles both animations directly on the temporal element and on its non-temporal children\n const timing = effect.getTiming();\n // Duration and delay from getTiming() are already in milliseconds\n // They include CSS animation-duration and animation-delay values\n const duration = Number(timing.duration) || 0;\n let delay = Number(timing.delay) || 0;\n\n // For Web Animations API animations, getTiming().delay is always correct.\n // For CSS animations, we may need to read from computed styles.\n // Try to read delay from computed styles as a fallback/override for CSS animations\n if (target instanceof HTMLElement) {\n const computedStyle = window.getComputedStyle(target);\n const animationDelays = computedStyle.animationDelay\n .split(\", \")\n .map((s) => s.trim());\n\n // Parse CSS delay value\n const parseDelay = (delayStr: string): number => {\n if (delayStr === \"0s\" || delayStr === \"0ms\") {\n return 0;\n }\n const delayMatch = delayStr.match(/^([\\d.]+)(s|ms)$/);\n if (delayMatch?.[1] && delayMatch[2]) {\n const value = Number.parseFloat(delayMatch[1]);\n const unit = delayMatch[2];\n return unit === \"s\" ? value * 1000 : value;\n }\n return 0;\n };\n\n // Only override delay from computed styles if:\n // 1. We have a valid parsed delay value, OR\n // 2. The computed style explicitly says \"0s\" or \"0ms\" (meaning no CSS delay)\n // This ensures we don't override WAAPI delay with 0 from computed styles when there's no CSS animation\n if (animationDelays.length === 1 && animationDelays[0]) {\n const parsedDelay = parseDelay(animationDelays[0]);\n // Only override if we got a valid parse AND it's not just a default \"0s\" from computed styles\n // OR if it's explicitly \"0s\"/\"0ms\" and getTiming().delay is also 0 (CSS animation with no delay)\n if (parsedDelay > 0) {\n delay = parsedDelay;\n } else if (\n (animationDelays[0] === \"0s\" || animationDelays[0] === \"0ms\") &&\n delay === 0\n ) {\n // Both are 0, so keep 0\n delay = 0;\n }\n // Otherwise, keep getTiming().delay (for WAAPI animations)\n } else if (animationDelays.length > 1) {\n // Multiple animations: try to match by index\n const allAnimations = Array.from(target.getAnimations());\n const animationIndex = allAnimations.indexOf(animation);\n if (\n animationIndex >= 0 &&\n animationIndex < animationDelays.length &&\n animationDelays[animationIndex]\n ) {\n const parsedDelay = parseDelay(animationDelays[animationIndex]);\n if (parsedDelay > 0) {\n delay = parsedDelay;\n }\n // Otherwise, keep getTiming().delay\n }\n }\n // If no computed styles match, keep getTiming().delay (for WAAPI animations)\n }\n\n const iterations =\n Number(timing.iterations) || DEFAULT_ANIMATION_ITERATIONS;\n\n if (duration <= 0) {\n animation.currentTime = 0;\n continue;\n }\n\n // Use the element itself as the time source (it's guaranteed to be temporal)\n const currentTime = element.ownCurrentTimeMs ?? 0;\n\n // Special case for ef-text-segment: apply stagger offset for animation timing\n // This allows staggered animations while keeping visibility timing unchanged\n // We ADD the stagger offset to the delay, so animations start later for later segments\n let effectiveDelay = delay;\n if (\n element.tagName === \"EF-TEXT-SEGMENT\" &&\n (element as any).staggerOffsetMs !== undefined\n ) {\n const staggerOffsetMs = (element as any).staggerOffsetMs as number;\n effectiveDelay = delay + staggerOffsetMs;\n }\n\n // If before delay, show initial keyframe state (0% of animation)\n // Use strict < instead of <= so animations can start immediately when delay is reached\n if (currentTime < effectiveDelay) {\n // Set to 0 to show initial keyframe (animation time, not including delay)\n // When manually controlling animation.currentTime, 0 represents the start of the animation\n animation.currentTime = 0;\n continue;\n }\n\n const adjustedTime = currentTime - effectiveDelay;\n const currentIteration = Math.floor(adjustedTime / duration);\n let currentIterationTime = adjustedTime % duration;\n\n // Handle animation-direction\n const direction = timing.direction || \"normal\";\n const isAlternate =\n direction === \"alternate\" || direction === \"alternate-reverse\";\n const shouldReverse =\n direction === \"reverse\" ||\n (direction === \"alternate\" && currentIteration % 2 === 1) ||\n (direction === \"alternate-reverse\" && currentIteration % 2 === 0);\n\n if (shouldReverse) {\n currentIterationTime = duration - currentIterationTime;\n }\n\n if (currentIteration >= iterations) {\n // Animation would be complete - clamp to just before completion\n // This prevents the animation from being removed from the element\n // animation.currentTime is the time within the animation (not including delay)\n const maxSafeAnimationTime =\n duration * iterations - ANIMATION_PRECISION_OFFSET;\n\n // For alternate directions at completion, we need to set currentTime based on the final iteration\n // The final iteration for alternate is iteration (iterations - 1), which is forward if iterations is odd\n if (isAlternate) {\n const finalIteration = iterations - 1;\n const isFinalIterationReversed =\n (direction === \"alternate\" && finalIteration % 2 === 1) ||\n (direction === \"alternate-reverse\" && finalIteration % 2 === 0);\n if (isFinalIterationReversed) {\n // At end of reversed iteration, currentTime should be near 0 (but clamped)\n animation.currentTime = Math.min(\n duration - ANIMATION_PRECISION_OFFSET,\n maxSafeAnimationTime,\n );\n } else {\n // At end of forward iteration, currentTime should be near duration (but clamped)\n animation.currentTime = maxSafeAnimationTime;\n }\n } else {\n animation.currentTime = maxSafeAnimationTime;\n }\n } else {\n // Animation in progress\n // For alternate/alternate-reverse directions, currentTime should be set to the time within\n // the current iteration (after applying direction), not cumulative time.\n // However, when there's a delay, we need to use cumulative time (adjustedTime) instead.\n // For normal/reverse directions, currentTime is always cumulative time.\n if (isAlternate) {\n // For alternate directions without delay, use iteration time (after direction applied)\n // For alternate directions with delay:\n // - Iteration 0: use ownCurrentTimeMs (which equals adjustedTime + delay for iteration 0)\n // - Iteration 1+: use cumulative time (adjustedTime)\n if (effectiveDelay > 0) {\n if (currentIteration === 0) {\n // For iteration 0 with delay, use ownCurrentTimeMs (matches test expectations)\n animation.currentTime = currentTime;\n } else {\n // With delay and iteration > 0, use cumulative time\n const maxSafeAnimationTime =\n duration * iterations - ANIMATION_PRECISION_OFFSET;\n animation.currentTime = Math.min(\n adjustedTime,\n maxSafeAnimationTime,\n );\n }\n } else {\n // Without delay: use iteration time (after direction applied)\n animation.currentTime = currentIterationTime;\n }\n } else {\n // For normal/reverse directions, use cumulative time\n const timeWithinAnimation =\n currentIteration * duration + currentIterationTime;\n const maxSafeAnimationTime =\n duration * iterations - ANIMATION_PRECISION_OFFSET;\n animation.currentTime = Math.min(\n timeWithinAnimation,\n maxSafeAnimationTime,\n );\n }\n }\n }\n};\n\n/**\n * Main function: synchronizes DOM element with timeline\n */\nexport const updateAnimations = (element: AnimatableElement): void => {\n const temporalState = evaluateTemporalState(element);\n deepGetTemporalElements(element).forEach((temporalElement) => {\n const temporalState = evaluateTemporalState(temporalElement);\n updateVisualState(temporalElement, temporalState);\n });\n updateVisualState(element, temporalState);\n\n // Coordinate animations - use animation-specific visibility to prevent jumps at exact boundaries\n const animationState = evaluateTemporalStateForAnimation(element);\n if (animationState.isVisible) {\n coordinateAnimationsForSingleElement(element);\n }\n\n // Coordinate animations for child elements using animation-specific visibility\n deepGetTemporalElements(element).forEach((temporalElement) => {\n const childAnimationState =\n evaluateTemporalStateForAnimation(temporalElement);\n if (childAnimationState.isVisible) {\n coordinateAnimationsForSingleElement(temporalElement);\n }\n });\n};\n"],"mappings":";;;AASA,MAAM,6BAA6B;AACnC,MAAM,+BAA+B;AACrC,MAAM,oBAAoB;AAC1B,MAAM,oBAAoB;AAC1B,MAAM,+BAA+B;AACrC,MAAM,gCAAgC;;;;AActC,MAAa,yBACX,YACkB;CAElB,MAAM,kBAAkB,QAAQ,iBAAiB,SAAS;CAE1D,MAAM,WACJ,QAAQ,cAAc,IAClB,IACA,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,QAAQ,gBAAgB,QAAQ,WA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5B,UACtB,sCAAqC,gBAAgB;GAEvD"}
|
|
1
|
+
{"version":3,"file":"updateAnimations.js","names":["timeSource: AnimatableElement"],"sources":["../../src/elements/updateAnimations.ts"],"sourcesContent":["import {\n deepGetTemporalElements,\n isEFTemporal,\n type TemporalMixinInterface,\n} from \"./EFTemporal.ts\";\n\n// All animatable elements are temporal elements with HTMLElement interface\nexport type AnimatableElement = TemporalMixinInterface & HTMLElement;\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst ANIMATION_PRECISION_OFFSET = 0.1; // Use 0.1ms to safely avoid completion threshold\nconst DEFAULT_ANIMATION_ITERATIONS = 1;\nconst PROGRESS_PROPERTY = \"--ef-progress\";\nconst DURATION_PROPERTY = \"--ef-duration\";\nconst TRANSITION_DURATION_PROPERTY = \"--ef-transition-duration\";\nconst TRANSITION_OUT_START_PROPERTY = \"--ef-transition-out-start\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Represents the phase an element is in relative to the timeline.\n * This is the primary concept that drives all visibility and animation decisions.\n */\nexport type ElementPhase =\n | \"before-start\"\n | \"active\"\n | \"at-end-boundary\"\n | \"after-end\";\n\n/**\n * Represents the temporal state of an element relative to the timeline\n */\ninterface TemporalState {\n progress: number;\n isVisible: boolean;\n timelineTimeMs: number;\n phase: ElementPhase;\n}\n\n/**\n * Context object that holds all evaluated state for an element update.\n * This groups related state together, reducing parameter passing and making\n * the data flow clearer.\n */\ninterface ElementUpdateContext {\n element: AnimatableElement;\n state: TemporalState;\n}\n\n/**\n * Animation timing information extracted from an animation effect.\n * Groups related timing properties together.\n */\ninterface AnimationTiming {\n duration: number;\n delay: number;\n iterations: number;\n direction: string;\n}\n\n/**\n * Capability interface for elements that support stagger offset.\n * This encapsulates the stagger behavior behind a capability check rather than\n * leaking tag name checks throughout the codebase.\n */\ninterface StaggerableElement extends AnimatableElement {\n staggerOffsetMs?: number;\n}\n\n// ============================================================================\n// Phase Determination\n// ============================================================================\n\n/**\n * Determines what phase an element is in relative to the timeline.\n *\n * WHY: Phase is the primary concept that drives all decisions. By explicitly\n * enumerating phases, we make the code's logic clear: phase determines visibility,\n * animation coordination, and visual state.\n *\n * Phases:\n * - before-start: Timeline is before element's start time\n * - active: Timeline is within element's active range (start to end, exclusive of end)\n * - at-end-boundary: Timeline is exactly at element's end time\n * - after-end: Timeline is after element's end time\n *\n * Note: We detect \"at-end-boundary\" by checking if timeline equals end time.\n * The boundary policy will then determine if this should be treated as visible/active\n * or not based on element characteristics.\n */\nconst determineElementPhase = (\n element: AnimatableElement,\n timelineTimeMs: number,\n): ElementPhase => {\n // Read endTimeMs once to avoid recalculation issues\n const endTimeMs = element.endTimeMs;\n const startTimeMs = element.startTimeMs;\n\n if (timelineTimeMs < startTimeMs) {\n return \"before-start\";\n }\n // Use epsilon to handle floating point precision issues\n const epsilon = 0.001;\n const diff = timelineTimeMs - endTimeMs;\n\n // If clearly after end (difference > epsilon), return 'after-end'\n if (diff > epsilon) {\n return \"after-end\";\n }\n // If at or very close to end boundary (within epsilon), return 'at-end-boundary'\n if (Math.abs(diff) <= epsilon) {\n return \"at-end-boundary\";\n }\n // Otherwise, we're before the end, so check if we're active\n return \"active\";\n};\n\n// ============================================================================\n// Boundary Policies\n// ============================================================================\n\n/**\n * Policy interface for determining behavior at boundaries.\n * Different policies apply different rules for when elements should be visible\n * or have animations coordinated at exact boundary times.\n */\ninterface BoundaryPolicy {\n /**\n * Determines if an element should be considered visible/active at the end boundary\n * based on the element's characteristics.\n */\n shouldIncludeEndBoundary(element: AnimatableElement): boolean;\n}\n\n/**\n * Visibility policy: determines when elements should be visible for display purposes.\n *\n * WHY: Root elements, elements aligned with composition end, and text segments\n * should remain visible at exact end time to prevent flicker and show final frames.\n * Other elements use exclusive end for clean transitions between elements.\n */\nclass VisibilityPolicy implements BoundaryPolicy {\n shouldIncludeEndBoundary(element: AnimatableElement): boolean {\n // Root elements should remain visible at exact end time to prevent flicker\n const isRootElement = !element.parentTimegroup;\n if (isRootElement) {\n return true;\n }\n\n // Elements aligned with composition end should remain visible at exact end time\n const isLastElementInComposition =\n element.endTimeMs === element.rootTimegroup?.endTimeMs;\n if (isLastElementInComposition) {\n return true;\n }\n\n // Text segments use inclusive end since they're meant to be visible for full duration\n if (this.isTextSegment(element)) {\n return true;\n }\n\n // Other elements use exclusive end for clean transitions\n return false;\n }\n\n /**\n * Checks if element is a text segment.\n * Encapsulates the tag name check to hide implementation detail.\n */\n protected isTextSegment(element: AnimatableElement): boolean {\n return element.tagName === \"EF-TEXT-SEGMENT\";\n }\n}\n\n/**\n * Animation policy: determines when animations should be coordinated.\n *\n * WHY: When an animation reaches exactly the end time of an element, using exclusive\n * end would make the element invisible, causing the animation to be removed from the\n * DOM and creating a visual jump. By using inclusive end, we ensure animations remain\n * coordinated even at exact boundary times, providing smooth visual transitions.\n */\nclass AnimationPolicy implements BoundaryPolicy {\n shouldIncludeEndBoundary(_element: AnimatableElement): boolean {\n return true;\n }\n}\n\n// Policy instances (singleton pattern for stateless policies)\nconst visibilityPolicy = new VisibilityPolicy();\nconst animationPolicy = new AnimationPolicy();\n\n/**\n * Determines if an element should be visible based on its phase and visibility policy.\n */\nconst shouldBeVisible = (\n phase: ElementPhase,\n element: AnimatableElement,\n): boolean => {\n if (phase === \"before-start\" || phase === \"after-end\") {\n return false;\n }\n if (phase === \"active\") {\n return true;\n }\n // phase === \"at-end-boundary\"\n return visibilityPolicy.shouldIncludeEndBoundary(element);\n};\n\n/**\n * Determines if animations should be coordinated based on element phase and animation policy.\n */\nconst shouldCoordinateAnimations = (\n phase: ElementPhase,\n element: AnimatableElement,\n): boolean => {\n if (phase === \"before-start\" || phase === \"after-end\") {\n return false;\n }\n if (phase === \"active\") {\n return true;\n }\n // phase === \"at-end-boundary\"\n return animationPolicy.shouldIncludeEndBoundary(element);\n};\n\n// ============================================================================\n// Temporal State Evaluation\n// ============================================================================\n\n/**\n * Evaluates what the element's state should be based on the timeline.\n *\n * WHY: This function determines the complete temporal state including phase,\n * which becomes the primary driver for all subsequent decisions.\n */\nexport const evaluateTemporalState = (\n element: AnimatableElement,\n): TemporalState => {\n // Get timeline time from root timegroup, or use element's own time if it IS a timegroup\n const timelineTimeMs = (element.rootTimegroup ?? element).currentTimeMs;\n\n const progress =\n element.durationMs <= 0\n ? 1\n : Math.max(0, Math.min(1, element.currentTimeMs / element.durationMs));\n\n const phase = determineElementPhase(element, timelineTimeMs);\n const isVisible = shouldBeVisible(phase, element);\n\n return { progress, isVisible, timelineTimeMs, phase };\n};\n\n/**\n * Evaluates element visibility state specifically for animation coordination.\n * Uses inclusive end boundaries to prevent animation jumps at exact boundaries.\n *\n * This is exported for external use cases that need animation-specific visibility\n * evaluation without the full ElementUpdateContext.\n */\nexport const evaluateAnimationVisibilityState = (\n element: AnimatableElement,\n): TemporalState => {\n const state = evaluateTemporalState(element);\n // Override visibility based on animation policy\n const shouldCoordinate = shouldCoordinateAnimations(state.phase, element);\n return { ...state, isVisible: shouldCoordinate };\n};\n\n// ============================================================================\n// Animation Time Mapping\n// ============================================================================\n\n/**\n * Capability check: determines if an element supports stagger offset.\n * Encapsulates the knowledge of which element types support this feature.\n */\nconst supportsStaggerOffset = (\n element: AnimatableElement,\n): element is StaggerableElement => {\n // Currently only text segments support stagger offset\n return element.tagName === \"EF-TEXT-SEGMENT\";\n};\n\n/**\n * Calculates effective delay including stagger offset if applicable.\n *\n * Stagger offset allows elements (like text segments) to have their animations\n * start at different times while keeping their visibility timing unchanged.\n * This enables staggered animation effects within a single timegroup.\n */\nconst calculateEffectiveDelay = (\n delay: number,\n element: AnimatableElement,\n): number => {\n if (supportsStaggerOffset(element) && element.staggerOffsetMs !== undefined) {\n // Apply stagger offset for animation timing\n // We ADD the stagger offset to the delay, so animations start later for later segments\n return delay + element.staggerOffsetMs;\n }\n return delay;\n};\n\n/**\n * Calculates maximum safe animation time to prevent completion.\n *\n * WHY: Once an animation reaches \"finished\" state, it can no longer be manually controlled\n * via currentTime. By clamping to just before completion (using ANIMATION_PRECISION_OFFSET),\n * we ensure the animation remains in a controllable state, allowing us to synchronize it\n * with the timeline even when it would naturally be complete.\n */\nconst calculateMaxSafeAnimationTime = (\n duration: number,\n iterations: number,\n): number => {\n return duration * iterations - ANIMATION_PRECISION_OFFSET;\n};\n\n/**\n * Determines if the current iteration should be reversed based on direction\n */\nconst shouldReverseIteration = (\n direction: string,\n currentIteration: number,\n): boolean => {\n return (\n direction === \"reverse\" ||\n (direction === \"alternate\" && currentIteration % 2 === 1) ||\n (direction === \"alternate-reverse\" && currentIteration % 2 === 0)\n );\n};\n\n/**\n * Applies direction to iteration time (reverses if needed)\n */\nconst applyDirectionToIterationTime = (\n currentIterationTime: number,\n duration: number,\n direction: string,\n currentIteration: number,\n): number => {\n if (shouldReverseIteration(direction, currentIteration)) {\n return duration - currentIterationTime;\n }\n return currentIterationTime;\n};\n\n/**\n * Maps element time to animation time for normal direction.\n * Uses cumulative time throughout the animation.\n * Note: elementTime should already be adjusted (elementTime - effectiveDelay).\n */\nconst mapNormalDirectionTime = (\n elementTime: number,\n duration: number,\n maxSafeTime: number,\n): number => {\n const currentIteration = Math.floor(elementTime / duration);\n const iterationTime = elementTime % duration;\n const cumulativeTime = currentIteration * duration + iterationTime;\n return Math.min(cumulativeTime, maxSafeTime);\n};\n\n/**\n * Maps element time to animation time for reverse direction.\n * Uses cumulative time with reversed iterations.\n * Note: elementTime should already be adjusted (elementTime - effectiveDelay).\n */\nconst mapReverseDirectionTime = (\n elementTime: number,\n duration: number,\n maxSafeTime: number,\n): number => {\n const currentIteration = Math.floor(elementTime / duration);\n const rawIterationTime = elementTime % duration;\n const reversedIterationTime = duration - rawIterationTime;\n const cumulativeTime = currentIteration * duration + reversedIterationTime;\n return Math.min(cumulativeTime, maxSafeTime);\n};\n\n/**\n * Maps element time to animation time for alternate/alternate-reverse directions.\n *\n * WHY SPECIAL HANDLING: Alternate directions oscillate between forward and reverse iterations.\n * Without delay, we use iteration time (0 to duration) because the animation naturally\n * resets each iteration. However, with delay, iteration 0 needs to account for the delay\n * offset (using ownCurrentTimeMs), and later iterations need cumulative time to properly\n * track progress across multiple iterations. This complexity requires a dedicated mapper\n * rather than trying to handle it in the general case.\n */\nconst mapAlternateDirectionTime = (\n elementTime: number,\n effectiveDelay: number,\n duration: number,\n direction: string,\n maxSafeTime: number,\n): number => {\n const adjustedTime = elementTime - effectiveDelay;\n\n if (effectiveDelay > 0) {\n // With delay: iteration 0 uses elementTime to include delay offset,\n // later iterations use cumulative time to track progress across iterations\n const currentIteration = Math.floor(adjustedTime / duration);\n if (currentIteration === 0) {\n return Math.min(elementTime, maxSafeTime);\n }\n return Math.min(adjustedTime, maxSafeTime);\n }\n\n // Without delay: use iteration time (after direction applied) since animation\n // naturally resets each iteration\n const currentIteration = Math.floor(elementTime / duration);\n const rawIterationTime = elementTime % duration;\n const iterationTime = applyDirectionToIterationTime(\n rawIterationTime,\n duration,\n direction,\n currentIteration,\n );\n return Math.min(iterationTime, maxSafeTime);\n};\n\n/**\n * Maps element time to animation time based on direction.\n *\n * WHY: This function explicitly transforms element time to animation time, making\n * the time mapping concept clear. Different directions require different transformations\n * to achieve the desired visual effect.\n */\nconst mapElementTimeToAnimationTime = (\n elementTime: number,\n timing: AnimationTiming,\n effectiveDelay: number,\n): number => {\n const { duration, iterations, direction } = timing;\n const maxSafeTime = calculateMaxSafeAnimationTime(duration, iterations);\n // Calculate adjusted time (element time minus delay) for normal/reverse directions\n const adjustedTime = elementTime - effectiveDelay;\n\n if (direction === \"reverse\") {\n return mapReverseDirectionTime(adjustedTime, duration, maxSafeTime);\n }\n if (direction === \"alternate\" || direction === \"alternate-reverse\") {\n return mapAlternateDirectionTime(\n elementTime,\n effectiveDelay,\n duration,\n direction,\n maxSafeTime,\n );\n }\n // normal direction - use adjustedTime to account for delay\n return mapNormalDirectionTime(adjustedTime, duration, maxSafeTime);\n};\n\n/**\n * Determines the animation time for a completed animation based on direction.\n */\nconst getCompletedAnimationTime = (\n timing: AnimationTiming,\n maxSafeTime: number,\n): number => {\n const { direction, iterations, duration } = timing;\n\n if (direction === \"alternate\" || direction === \"alternate-reverse\") {\n // For alternate directions, determine if final iteration is reversed\n const finalIteration = iterations - 1;\n const isFinalIterationReversed =\n (direction === \"alternate\" && finalIteration % 2 === 1) ||\n (direction === \"alternate-reverse\" && finalIteration % 2 === 0);\n\n if (isFinalIterationReversed) {\n // At end of reversed iteration, currentTime should be near 0 (but clamped)\n return Math.min(duration - ANIMATION_PRECISION_OFFSET, maxSafeTime);\n }\n }\n\n // For normal, reverse, or forward final iteration of alternate: use max safe time\n return maxSafeTime;\n};\n\n/**\n * Validates that animation effect is a KeyframeEffect with a target\n */\nconst validateAnimationEffect = (\n effect: AnimationEffect | null,\n): effect is KeyframeEffect & { target: Element } => {\n return (\n effect !== null &&\n effect instanceof KeyframeEffect &&\n effect.target !== null\n );\n};\n\n/**\n * Extracts timing information from an animation effect.\n * Duration and delay from getTiming() are already in milliseconds.\n * We use getTiming().delay directly from the animation object.\n */\nconst extractAnimationTiming = (effect: KeyframeEffect): AnimationTiming => {\n const timing = effect.getTiming();\n\n return {\n duration: Number(timing.duration) || 0,\n delay: Number(timing.delay) || 0,\n iterations: Number(timing.iterations) || DEFAULT_ANIMATION_ITERATIONS,\n direction: timing.direction || \"normal\",\n };\n};\n\n/**\n * Prepares animation for manual control by ensuring it's paused\n */\nconst prepareAnimation = (animation: Animation): void => {\n // Ensure animation is in a playable state (not finished)\n // Finished animations can't be controlled, so reset them\n if (animation.playState === \"finished\") {\n animation.cancel();\n // Re-initialize the animation so it can be controlled\n animation.play();\n animation.pause();\n } else if (animation.playState === \"running\") {\n // Pause running animations so we can control them manually\n animation.pause();\n } else {\n // For animations in \"idle\" or \"paused\" state, ensure they're initialized\n // by playing and pausing. This ensures they're in a state where currentTime\n // changes will actually apply keyframes.\n // Only do this if currentTime is null (hasn't been set yet) to avoid\n // disrupting animations that are already being controlled\n if (animation.currentTime === null) {\n animation.play();\n animation.pause();\n }\n // Also ensure playState is \"paused\" (not \"idle\")\n // Some animations might be in \"idle\" state even after play/pause\n if (animation.playState === \"idle\") {\n // Force to paused state by setting currentTime to 0 then pausing\n animation.currentTime = 0;\n animation.pause();\n }\n }\n};\n\n/**\n * Maps element time to animation currentTime and sets it on the animation.\n *\n * WHY: This function explicitly performs the time mapping transformation,\n * making it clear that we're transforming element time to animation time.\n */\nconst mapAndSetAnimationTime = (\n animation: Animation,\n element: AnimatableElement,\n timing: AnimationTiming,\n effectiveDelay: number,\n): void => {\n // Use ownCurrentTimeMs for all elements (timegroups and other temporal elements)\n // This gives us time relative to when the element started, which ensures animations\n // on child elements are synchronized with their containing timegroup's timeline.\n // For timegroups, ownCurrentTimeMs is the time relative to when the timegroup started.\n // For other temporal elements, ownCurrentTimeMs is the time relative to their start.\n const elementTime = element.ownCurrentTimeMs ?? 0;\n\n // CRITICAL: Ensure animation is paused before setting currentTime\n // Animations in \"running\" state won't apply keyframes when currentTime is set manually\n if (animation.playState === \"running\") {\n animation.pause();\n }\n\n // Calculate adjusted time (element time minus delay)\n const adjustedTime = elementTime - effectiveDelay;\n\n // If before delay, show initial keyframe state (0% of animation)\n // Use strict < 0 so that at exactly the delay time (adjustedTime = 0), we start animating\n if (adjustedTime < 0) {\n // Before delay: currentTime should be at the absolute timeline time\n // This ensures the animation is \"caught up\" with the delay\n animation.currentTime = elementTime;\n return;\n }\n\n // At delay time (adjustedTime = 0) or after, the animation should be active\n const { duration, iterations } = timing;\n const currentIteration = Math.floor(adjustedTime / duration);\n\n if (currentIteration >= iterations) {\n // Animation is completed - use completed time mapping\n const maxSafeTime = calculateMaxSafeAnimationTime(duration, iterations);\n const completedAnimationTime = getCompletedAnimationTime(\n timing,\n maxSafeTime,\n );\n\n // Completed: currentTime should be delay + completed animation time (absolute timeline time)\n animation.currentTime = effectiveDelay + completedAnimationTime;\n } else {\n // Animation is in progress - map element time to animation time\n const animationTime = mapElementTimeToAnimationTime(\n elementTime,\n timing,\n effectiveDelay,\n );\n\n // For alternate direction with delay in iteration 0, mapAlternateDirectionTime returns elementTime directly\n // For other cases, currentTime should be delay + animation time (absolute timeline time)\n // Check if this is alternate direction with delay in iteration 0\n const { direction } = timing;\n const isAlternateWithDelay =\n (direction === \"alternate\" || direction === \"alternate-reverse\") &&\n effectiveDelay > 0;\n if (isAlternateWithDelay && currentIteration === 0) {\n // For alternate direction iteration 0 with delay, use elementTime directly\n animation.currentTime = elementTime;\n } else {\n // For other cases, currentTime should be delay + animation time (absolute timeline time)\n animation.currentTime = effectiveDelay + animationTime;\n }\n }\n};\n\n/**\n * Synchronizes a single animation with the timeline using the element as the time source.\n *\n * For animations in this element's subtree, always use this element as the time source.\n * This handles both animations directly on the temporal element and on its non-temporal children.\n */\nconst synchronizeAnimation = (\n animation: Animation,\n element: AnimatableElement,\n): void => {\n const effect = animation.effect;\n if (!validateAnimationEffect(effect)) {\n return;\n }\n\n const timing = extractAnimationTiming(effect);\n\n if (timing.duration <= 0) {\n animation.currentTime = 0;\n return;\n }\n\n // Find the containing timegroup for the animation target.\n // Temporal elements are always synced to timegroups, so animations should use\n // the timegroup's timeline as the time source.\n const target = effect.target;\n let timeSource: AnimatableElement = element;\n\n if (target && target instanceof HTMLElement) {\n // Find the nearest timegroup in the DOM tree\n const nearestTimegroup = target.closest(\"ef-timegroup\");\n if (nearestTimegroup && isEFTemporal(nearestTimegroup)) {\n timeSource = nearestTimegroup as AnimatableElement;\n }\n }\n\n const effectiveDelay = calculateEffectiveDelay(timing.delay, timeSource);\n mapAndSetAnimationTime(animation, timeSource, timing, effectiveDelay);\n};\n\n/**\n * Coordinates animations for a single element and its subtree, using the element as the time source.\n *\n * Gets animations on the element itself and its subtree.\n * Both CSS animations (created via the 'animation' property) and WAAPI animations are included.\n */\nconst coordinateElementAnimations = (element: AnimatableElement): void => {\n const animations = element.getAnimations({ subtree: true });\n\n for (const animation of animations) {\n prepareAnimation(animation);\n synchronizeAnimation(animation, element);\n }\n};\n\n// ============================================================================\n// Visual State Application\n// ============================================================================\n\n/**\n * Applies visual state (CSS + display) to match temporal state.\n *\n * WHY: This function applies visual state based on the element's phase and state.\n * Phase determines what should be visible, and this function applies that decision.\n */\nconst applyVisualState = (\n element: AnimatableElement,\n state: TemporalState,\n): void => {\n // Always set progress (needed for many use cases)\n element.style.setProperty(PROGRESS_PROPERTY, `${state.progress * 100}%`);\n\n // Handle visibility based on phase\n if (!state.isVisible) {\n element.style.setProperty(\"display\", \"none\");\n return;\n }\n element.style.removeProperty(\"display\");\n\n // Set other CSS properties for visible elements only\n element.style.setProperty(DURATION_PROPERTY, `${element.durationMs}ms`);\n element.style.setProperty(\n TRANSITION_DURATION_PROPERTY,\n `${element.parentTimegroup?.overlapMs ?? 0}ms`,\n );\n element.style.setProperty(\n TRANSITION_OUT_START_PROPERTY,\n `${element.durationMs - (element.parentTimegroup?.overlapMs ?? 0)}ms`,\n );\n};\n\n/**\n * Applies animation coordination if the element phase requires it.\n *\n * WHY: Animation coordination is driven by phase. If the element is in a phase\n * where animations should be coordinated, we coordinate them.\n */\nconst applyAnimationCoordination = (\n element: AnimatableElement,\n phase: ElementPhase,\n): void => {\n if (shouldCoordinateAnimations(phase, element)) {\n coordinateElementAnimations(element);\n }\n};\n\n// ============================================================================\n// Main Function\n// ============================================================================\n\n/**\n * Evaluates the complete state for an element update.\n * This separates evaluation (what should the state be?) from application (apply that state).\n */\nconst evaluateElementState = (\n element: AnimatableElement,\n): ElementUpdateContext => {\n return {\n element,\n state: evaluateTemporalState(element),\n };\n};\n\n/**\n * Main function: synchronizes DOM element with timeline.\n *\n * Orchestrates clear flow: Phase → Policy → Time Mapping → State Application\n *\n * WHY: This function makes the conceptual flow explicit:\n * 1. Determine phase (what phase is the element in?)\n * 2. Apply policies (should it be visible/coordinated based on phase?)\n * 3. Map time for animations (transform element time to animation time)\n * 4. Apply visual state (update CSS and display based on phase and policies)\n */\nexport const updateAnimations = (element: AnimatableElement): void => {\n // Evaluate all states\n const rootContext = evaluateElementState(element);\n const childContexts = deepGetTemporalElements(element).map(\n (temporalElement) => evaluateElementState(temporalElement),\n );\n\n // Apply visual state and animation coordination for root element\n applyVisualState(rootContext.element, rootContext.state);\n applyAnimationCoordination(rootContext.element, rootContext.state.phase);\n\n // Apply visual state and animation coordination for all temporal child elements\n childContexts.forEach((context) => {\n applyVisualState(context.element, context.state);\n applyAnimationCoordination(context.element, context.state.phase);\n });\n};\n"],"mappings":";;;AAaA,MAAM,6BAA6B;AACnC,MAAM,+BAA+B;AACrC,MAAM,oBAAoB;AAC1B,MAAM,oBAAoB;AAC1B,MAAM,+BAA+B;AACrC,MAAM,gCAAgC;;;;;;;;;;;;;;;;;;AA6EtC,MAAM,yBACJ,SACA,mBACiB;CAEjB,MAAM,YAAY,QAAQ;AAG1B,KAAI,iBAFgB,QAAQ,YAG1B,QAAO;CAGT,MAAM,UAAU;CAChB,MAAM,OAAO,iBAAiB;AAG9B,KAAI,OAAO,QACT,QAAO;AAGT,KAAI,KAAK,IAAI,KAAK,IAAI,QACpB,QAAO;AAGT,QAAO;;;;;;;;;AA2BT,IAAM,mBAAN,MAAiD;CAC/C,yBAAyB,SAAqC;AAG5D,MADsB,CAAC,QAAQ,gBAE7B,QAAO;AAMT,MADE,QAAQ,cAAc,QAAQ,eAAe,UAE7C,QAAO;AAIT,MAAI,KAAK,cAAc,QAAQ,CAC7B,QAAO;AAIT,SAAO;;;;;;CAOT,AAAU,cAAc,SAAqC;AAC3D,SAAO,QAAQ,YAAY;;;;;;;;;;;AAY/B,IAAM,kBAAN,MAAgD;CAC9C,yBAAyB,UAAsC;AAC7D,SAAO;;;AAKX,MAAM,mBAAmB,IAAI,kBAAkB;AAC/C,MAAM,kBAAkB,IAAI,iBAAiB;;;;AAK7C,MAAM,mBACJ,OACA,YACY;AACZ,KAAI,UAAU,kBAAkB,UAAU,YACxC,QAAO;AAET,KAAI,UAAU,SACZ,QAAO;AAGT,QAAO,iBAAiB,yBAAyB,QAAQ;;;;;AAM3D,MAAM,8BACJ,OACA,YACY;AACZ,KAAI,UAAU,kBAAkB,UAAU,YAC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,SAAS,YAAY,MAAM,MAAM;AAGxE,eAAc,SAAS,YAAY;AACjC,mBAAiB,QAAQ,SAAS,QAAQ,MAAM;AAChD,6BAA2B,QAAQ,SAAS,QAAQ,MAAM,MAAM;GAChE"}
|
|
@@ -7,7 +7,7 @@ import { Caption } from "../elements/EFCaptions.js";
|
|
|
7
7
|
import { EFTextSegment } from "../elements/EFTextSegment.js";
|
|
8
8
|
import { TimegroupController } from "../elements/TimegroupController.js";
|
|
9
9
|
import { FocusContext } from "./focusContext.js";
|
|
10
|
-
import * as
|
|
10
|
+
import * as lit14 from "lit";
|
|
11
11
|
import { LitElement, PropertyValueMap, ReactiveController, TemplateResult, nothing } from "lit";
|
|
12
12
|
import * as lit_html_directives_ref0 from "lit-html/directives/ref";
|
|
13
13
|
|
|
@@ -22,7 +22,7 @@ declare class ElementFilmstripController implements ReactiveController {
|
|
|
22
22
|
}
|
|
23
23
|
declare const FilmstripItem_base: typeof LitElement;
|
|
24
24
|
declare class FilmstripItem extends FilmstripItem_base {
|
|
25
|
-
static styles:
|
|
25
|
+
static styles: lit14.CSSResult[];
|
|
26
26
|
focusContext?: FocusContext;
|
|
27
27
|
focusedElement?: HTMLElement | null;
|
|
28
28
|
get isFocused(): boolean;
|
|
@@ -166,7 +166,7 @@ declare class EFHTMLHierarchyItem extends EFHierarchyItem {
|
|
|
166
166
|
declare const EFFilmstrip_base: typeof LitElement;
|
|
167
167
|
declare class EFFilmstrip extends EFFilmstrip_base {
|
|
168
168
|
#private;
|
|
169
|
-
static styles:
|
|
169
|
+
static styles: lit14.CSSResult[];
|
|
170
170
|
pixelsPerMs: number;
|
|
171
171
|
hide: string;
|
|
172
172
|
show: string;
|