Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 1 | import { queryAll, extend, createStyleSheet, matches, closest } from '../utils/util.js' |
| 2 | import { FRAGMENT_STYLE_REGEX } from '../utils/constants.js' |
| 3 | |
| 4 | // Counter used to generate unique IDs for auto-animated elements |
| 5 | let autoAnimateCounter = 0; |
| 6 | |
| 7 | /** |
| 8 | * Automatically animates matching elements across |
| 9 | * slides with the [data-auto-animate] attribute. |
| 10 | */ |
| 11 | export default class AutoAnimate { |
| 12 | |
| 13 | constructor( Reveal ) { |
| 14 | |
| 15 | this.Reveal = Reveal; |
| 16 | |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Runs an auto-animation between the given slides. |
| 21 | * |
| 22 | * @param {HTMLElement} fromSlide |
| 23 | * @param {HTMLElement} toSlide |
| 24 | */ |
| 25 | run( fromSlide, toSlide ) { |
| 26 | |
| 27 | // Clean up after prior animations |
| 28 | this.reset(); |
| 29 | |
| 30 | let allSlides = this.Reveal.getSlides(); |
| 31 | let toSlideIndex = allSlides.indexOf( toSlide ); |
| 32 | let fromSlideIndex = allSlides.indexOf( fromSlide ); |
| 33 | |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 34 | // Ensure that; |
| 35 | // 1. Both slides exist. |
| 36 | // 2. Both slides are auto-animate targets with the same |
| 37 | // data-auto-animate-id value (including null if absent on both). |
| 38 | // 3. data-auto-animate-restart isn't set on the physically latter |
| 39 | // slide (independent of slide direction). |
| 40 | if( fromSlide && toSlide && fromSlide.hasAttribute( 'data-auto-animate' ) && toSlide.hasAttribute( 'data-auto-animate' ) |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 41 | && fromSlide.getAttribute( 'data-auto-animate-id' ) === toSlide.getAttribute( 'data-auto-animate-id' ) |
| 42 | && !( toSlideIndex > fromSlideIndex ? toSlide : fromSlide ).hasAttribute( 'data-auto-animate-restart' ) ) { |
| 43 | |
| 44 | // Create a new auto-animate sheet |
| 45 | this.autoAnimateStyleSheet = this.autoAnimateStyleSheet || createStyleSheet(); |
| 46 | |
| 47 | let animationOptions = this.getAutoAnimateOptions( toSlide ); |
| 48 | |
| 49 | // Set our starting state |
| 50 | fromSlide.dataset.autoAnimate = 'pending'; |
| 51 | toSlide.dataset.autoAnimate = 'pending'; |
| 52 | |
| 53 | // Flag the navigation direction, needed for fragment buildup |
| 54 | animationOptions.slideDirection = toSlideIndex > fromSlideIndex ? 'forward' : 'backward'; |
| 55 | |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 56 | // If the from-slide is hidden because it has moved outside |
| 57 | // the view distance, we need to temporarily show it while |
| 58 | // measuring |
| 59 | let fromSlideIsHidden = fromSlide.style.display === 'none'; |
| 60 | if( fromSlideIsHidden ) fromSlide.style.display = this.Reveal.getConfig().display; |
| 61 | |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 62 | // Inject our auto-animate styles for this transition |
| 63 | let css = this.getAutoAnimatableElements( fromSlide, toSlide ).map( elements => { |
| 64 | return this.autoAnimateElements( elements.from, elements.to, elements.options || {}, animationOptions, autoAnimateCounter++ ); |
| 65 | } ); |
| 66 | |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 67 | if( fromSlideIsHidden ) fromSlide.style.display = 'none'; |
| 68 | |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 69 | // Animate unmatched elements, if enabled |
| 70 | if( toSlide.dataset.autoAnimateUnmatched !== 'false' && this.Reveal.getConfig().autoAnimateUnmatched === true ) { |
| 71 | |
| 72 | // Our default timings for unmatched elements |
| 73 | let defaultUnmatchedDuration = animationOptions.duration * 0.8, |
| 74 | defaultUnmatchedDelay = animationOptions.duration * 0.2; |
| 75 | |
| 76 | this.getUnmatchedAutoAnimateElements( toSlide ).forEach( unmatchedElement => { |
| 77 | |
| 78 | let unmatchedOptions = this.getAutoAnimateOptions( unmatchedElement, animationOptions ); |
| 79 | let id = 'unmatched'; |
| 80 | |
| 81 | // If there is a duration or delay set specifically for this |
| 82 | // element our unmatched elements should adhere to those |
| 83 | if( unmatchedOptions.duration !== animationOptions.duration || unmatchedOptions.delay !== animationOptions.delay ) { |
| 84 | id = 'unmatched-' + autoAnimateCounter++; |
| 85 | css.push( `[data-auto-animate="running"] [data-auto-animate-target="${id}"] { transition: opacity ${unmatchedOptions.duration}s ease ${unmatchedOptions.delay}s; }` ); |
| 86 | } |
| 87 | |
| 88 | unmatchedElement.dataset.autoAnimateTarget = id; |
| 89 | |
| 90 | }, this ); |
| 91 | |
| 92 | // Our default transition for unmatched elements |
| 93 | css.push( `[data-auto-animate="running"] [data-auto-animate-target="unmatched"] { transition: opacity ${defaultUnmatchedDuration}s ease ${defaultUnmatchedDelay}s; }` ); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | // Setting the whole chunk of CSS at once is the most |
| 98 | // efficient way to do this. Using sheet.insertRule |
| 99 | // is multiple factors slower. |
| 100 | this.autoAnimateStyleSheet.innerHTML = css.join( '' ); |
| 101 | |
| 102 | // Start the animation next cycle |
| 103 | requestAnimationFrame( () => { |
| 104 | if( this.autoAnimateStyleSheet ) { |
| 105 | // This forces our newly injected styles to be applied in Firefox |
| 106 | getComputedStyle( this.autoAnimateStyleSheet ).fontWeight; |
| 107 | |
| 108 | toSlide.dataset.autoAnimate = 'running'; |
| 109 | } |
| 110 | } ); |
| 111 | |
| 112 | this.Reveal.dispatchEvent({ |
| 113 | type: 'autoanimate', |
| 114 | data: { |
| 115 | fromSlide, |
| 116 | toSlide, |
| 117 | sheet: this.autoAnimateStyleSheet |
| 118 | } |
| 119 | }); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Rolls back all changes that we've made to the DOM so |
| 127 | * that as part of animating. |
| 128 | */ |
| 129 | reset() { |
| 130 | |
| 131 | // Reset slides |
| 132 | queryAll( this.Reveal.getRevealElement(), '[data-auto-animate]:not([data-auto-animate=""])' ).forEach( element => { |
| 133 | element.dataset.autoAnimate = ''; |
| 134 | } ); |
| 135 | |
| 136 | // Reset elements |
| 137 | queryAll( this.Reveal.getRevealElement(), '[data-auto-animate-target]' ).forEach( element => { |
| 138 | delete element.dataset.autoAnimateTarget; |
| 139 | } ); |
| 140 | |
| 141 | // Remove the animation sheet |
| 142 | if( this.autoAnimateStyleSheet && this.autoAnimateStyleSheet.parentNode ) { |
| 143 | this.autoAnimateStyleSheet.parentNode.removeChild( this.autoAnimateStyleSheet ); |
| 144 | this.autoAnimateStyleSheet = null; |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Creates a FLIP animation where the `to` element starts out |
| 151 | * in the `from` element position and animates to its original |
| 152 | * state. |
| 153 | * |
| 154 | * @param {HTMLElement} from |
| 155 | * @param {HTMLElement} to |
| 156 | * @param {Object} elementOptions Options for this element pair |
| 157 | * @param {Object} animationOptions Options set at the slide level |
| 158 | * @param {String} id Unique ID that we can use to identify this |
| 159 | * auto-animate element in the DOM |
| 160 | */ |
| 161 | autoAnimateElements( from, to, elementOptions, animationOptions, id ) { |
| 162 | |
| 163 | // 'from' elements are given a data-auto-animate-target with no value, |
| 164 | // 'to' elements are are given a data-auto-animate-target with an ID |
| 165 | from.dataset.autoAnimateTarget = ''; |
| 166 | to.dataset.autoAnimateTarget = id; |
| 167 | |
| 168 | // Each element may override any of the auto-animate options |
| 169 | // like transition easing, duration and delay via data-attributes |
| 170 | let options = this.getAutoAnimateOptions( to, animationOptions ); |
| 171 | |
| 172 | // If we're using a custom element matcher the element options |
| 173 | // may contain additional transition overrides |
| 174 | if( typeof elementOptions.delay !== 'undefined' ) options.delay = elementOptions.delay; |
| 175 | if( typeof elementOptions.duration !== 'undefined' ) options.duration = elementOptions.duration; |
| 176 | if( typeof elementOptions.easing !== 'undefined' ) options.easing = elementOptions.easing; |
| 177 | |
| 178 | let fromProps = this.getAutoAnimatableProperties( 'from', from, elementOptions ), |
| 179 | toProps = this.getAutoAnimatableProperties( 'to', to, elementOptions ); |
| 180 | |
| 181 | // Maintain fragment visibility for matching elements when |
| 182 | // we're navigating forwards, this way the viewer won't need |
| 183 | // to step through the same fragments twice |
| 184 | if( to.classList.contains( 'fragment' ) ) { |
| 185 | |
| 186 | // Don't auto-animate the opacity of fragments to avoid |
| 187 | // conflicts with fragment animations |
| 188 | delete toProps.styles['opacity']; |
| 189 | |
| 190 | if( from.classList.contains( 'fragment' ) ) { |
| 191 | |
| 192 | let fromFragmentStyle = ( from.className.match( FRAGMENT_STYLE_REGEX ) || [''] )[0]; |
| 193 | let toFragmentStyle = ( to.className.match( FRAGMENT_STYLE_REGEX ) || [''] )[0]; |
| 194 | |
| 195 | // Only skip the fragment if the fragment animation style |
| 196 | // remains unchanged |
| 197 | if( fromFragmentStyle === toFragmentStyle && animationOptions.slideDirection === 'forward' ) { |
| 198 | to.classList.add( 'visible', 'disabled' ); |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | // If translation and/or scaling are enabled, css transform |
| 206 | // the 'to' element so that it matches the position and size |
| 207 | // of the 'from' element |
| 208 | if( elementOptions.translate !== false || elementOptions.scale !== false ) { |
| 209 | |
| 210 | let presentationScale = this.Reveal.getScale(); |
| 211 | |
| 212 | let delta = { |
| 213 | x: ( fromProps.x - toProps.x ) / presentationScale, |
| 214 | y: ( fromProps.y - toProps.y ) / presentationScale, |
| 215 | scaleX: fromProps.width / toProps.width, |
| 216 | scaleY: fromProps.height / toProps.height |
| 217 | }; |
| 218 | |
| 219 | // Limit decimal points to avoid 0.0001px blur and stutter |
| 220 | delta.x = Math.round( delta.x * 1000 ) / 1000; |
| 221 | delta.y = Math.round( delta.y * 1000 ) / 1000; |
| 222 | delta.scaleX = Math.round( delta.scaleX * 1000 ) / 1000; |
| 223 | delta.scaleX = Math.round( delta.scaleX * 1000 ) / 1000; |
| 224 | |
| 225 | let translate = elementOptions.translate !== false && ( delta.x !== 0 || delta.y !== 0 ), |
| 226 | scale = elementOptions.scale !== false && ( delta.scaleX !== 0 || delta.scaleY !== 0 ); |
| 227 | |
| 228 | // No need to transform if nothing's changed |
| 229 | if( translate || scale ) { |
| 230 | |
| 231 | let transform = []; |
| 232 | |
| 233 | if( translate ) transform.push( `translate(${delta.x}px, ${delta.y}px)` ); |
| 234 | if( scale ) transform.push( `scale(${delta.scaleX}, ${delta.scaleY})` ); |
| 235 | |
| 236 | fromProps.styles['transform'] = transform.join( ' ' ); |
| 237 | fromProps.styles['transform-origin'] = 'top left'; |
| 238 | |
| 239 | toProps.styles['transform'] = 'none'; |
| 240 | |
| 241 | } |
| 242 | |
| 243 | } |
| 244 | |
| 245 | // Delete all unchanged 'to' styles |
| 246 | for( let propertyName in toProps.styles ) { |
| 247 | const toValue = toProps.styles[propertyName]; |
| 248 | const fromValue = fromProps.styles[propertyName]; |
| 249 | |
| 250 | if( toValue === fromValue ) { |
| 251 | delete toProps.styles[propertyName]; |
| 252 | } |
| 253 | else { |
| 254 | // If these property values were set via a custom matcher providing |
| 255 | // an explicit 'from' and/or 'to' value, we always inject those values. |
| 256 | if( toValue.explicitValue === true ) { |
| 257 | toProps.styles[propertyName] = toValue.value; |
| 258 | } |
| 259 | |
| 260 | if( fromValue.explicitValue === true ) { |
| 261 | fromProps.styles[propertyName] = fromValue.value; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | let css = ''; |
| 267 | |
| 268 | let toStyleProperties = Object.keys( toProps.styles ); |
| 269 | |
| 270 | // Only create animate this element IF at least one style |
| 271 | // property has changed |
| 272 | if( toStyleProperties.length > 0 ) { |
| 273 | |
| 274 | // Instantly move to the 'from' state |
| 275 | fromProps.styles['transition'] = 'none'; |
| 276 | |
| 277 | // Animate towards the 'to' state |
| 278 | toProps.styles['transition'] = `all ${options.duration}s ${options.easing} ${options.delay}s`; |
| 279 | toProps.styles['transition-property'] = toStyleProperties.join( ', ' ); |
| 280 | toProps.styles['will-change'] = toStyleProperties.join( ', ' ); |
| 281 | |
| 282 | // Build up our custom CSS. We need to override inline styles |
| 283 | // so we need to make our styles vErY IMPORTANT!1!! |
| 284 | let fromCSS = Object.keys( fromProps.styles ).map( propertyName => { |
| 285 | return propertyName + ': ' + fromProps.styles[propertyName] + ' !important;'; |
| 286 | } ).join( '' ); |
| 287 | |
| 288 | let toCSS = Object.keys( toProps.styles ).map( propertyName => { |
| 289 | return propertyName + ': ' + toProps.styles[propertyName] + ' !important;'; |
| 290 | } ).join( '' ); |
| 291 | |
| 292 | css = '[data-auto-animate-target="'+ id +'"] {'+ fromCSS +'}' + |
| 293 | '[data-auto-animate="running"] [data-auto-animate-target="'+ id +'"] {'+ toCSS +'}'; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | return css; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns the auto-animate options for the given element. |
| 303 | * |
| 304 | * @param {HTMLElement} element Element to pick up options |
| 305 | * from, either a slide or an animation target |
| 306 | * @param {Object} [inheritedOptions] Optional set of existing |
| 307 | * options |
| 308 | */ |
| 309 | getAutoAnimateOptions( element, inheritedOptions ) { |
| 310 | |
| 311 | let options = { |
| 312 | easing: this.Reveal.getConfig().autoAnimateEasing, |
| 313 | duration: this.Reveal.getConfig().autoAnimateDuration, |
| 314 | delay: 0 |
| 315 | }; |
| 316 | |
| 317 | options = extend( options, inheritedOptions ); |
| 318 | |
| 319 | // Inherit options from parent elements |
| 320 | if( element.parentNode ) { |
| 321 | let autoAnimatedParent = closest( element.parentNode, '[data-auto-animate-target]' ); |
| 322 | if( autoAnimatedParent ) { |
| 323 | options = this.getAutoAnimateOptions( autoAnimatedParent, options ); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if( element.dataset.autoAnimateEasing ) { |
| 328 | options.easing = element.dataset.autoAnimateEasing; |
| 329 | } |
| 330 | |
| 331 | if( element.dataset.autoAnimateDuration ) { |
| 332 | options.duration = parseFloat( element.dataset.autoAnimateDuration ); |
| 333 | } |
| 334 | |
| 335 | if( element.dataset.autoAnimateDelay ) { |
| 336 | options.delay = parseFloat( element.dataset.autoAnimateDelay ); |
| 337 | } |
| 338 | |
| 339 | return options; |
| 340 | |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Returns an object containing all of the properties |
| 345 | * that can be auto-animated for the given element and |
| 346 | * their current computed values. |
| 347 | * |
| 348 | * @param {String} direction 'from' or 'to' |
| 349 | */ |
| 350 | getAutoAnimatableProperties( direction, element, elementOptions ) { |
| 351 | |
| 352 | let config = this.Reveal.getConfig(); |
| 353 | |
| 354 | let properties = { styles: [] }; |
| 355 | |
| 356 | // Position and size |
| 357 | if( elementOptions.translate !== false || elementOptions.scale !== false ) { |
| 358 | let bounds; |
| 359 | |
| 360 | // Custom auto-animate may optionally return a custom tailored |
| 361 | // measurement function |
| 362 | if( typeof elementOptions.measure === 'function' ) { |
| 363 | bounds = elementOptions.measure( element ); |
| 364 | } |
| 365 | else { |
| 366 | if( config.center ) { |
| 367 | // More precise, but breaks when used in combination |
| 368 | // with zoom for scaling the deck ¯\_(ツ)_/¯ |
| 369 | bounds = element.getBoundingClientRect(); |
| 370 | } |
| 371 | else { |
| 372 | let scale = this.Reveal.getScale(); |
| 373 | bounds = { |
| 374 | x: element.offsetLeft * scale, |
| 375 | y: element.offsetTop * scale, |
| 376 | width: element.offsetWidth * scale, |
| 377 | height: element.offsetHeight * scale |
| 378 | }; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | properties.x = bounds.x; |
| 383 | properties.y = bounds.y; |
| 384 | properties.width = bounds.width; |
| 385 | properties.height = bounds.height; |
| 386 | } |
| 387 | |
| 388 | const computedStyles = getComputedStyle( element ); |
| 389 | |
| 390 | // CSS styles |
| 391 | ( elementOptions.styles || config.autoAnimateStyles ).forEach( style => { |
| 392 | let value; |
| 393 | |
| 394 | // `style` is either the property name directly, or an object |
| 395 | // definition of a style property |
| 396 | if( typeof style === 'string' ) style = { property: style }; |
| 397 | |
| 398 | if( typeof style.from !== 'undefined' && direction === 'from' ) { |
| 399 | value = { value: style.from, explicitValue: true }; |
| 400 | } |
| 401 | else if( typeof style.to !== 'undefined' && direction === 'to' ) { |
| 402 | value = { value: style.to, explicitValue: true }; |
| 403 | } |
| 404 | else { |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 405 | // Use a unitless value for line-height so that it inherits properly |
| 406 | if( style.property === 'line-height' ) { |
| 407 | value = parseFloat( computedStyles['line-height'] ) / parseFloat( computedStyles['font-size'] ); |
| 408 | } |
| 409 | |
| 410 | if( isNaN(value) ) { |
| 411 | value = computedStyles[style.property]; |
| 412 | } |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if( value !== '' ) { |
| 416 | properties.styles[style.property] = value; |
| 417 | } |
| 418 | } ); |
| 419 | |
| 420 | return properties; |
| 421 | |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Get a list of all element pairs that we can animate |
| 426 | * between the given slides. |
| 427 | * |
| 428 | * @param {HTMLElement} fromSlide |
| 429 | * @param {HTMLElement} toSlide |
| 430 | * |
| 431 | * @return {Array} Each value is an array where [0] is |
| 432 | * the element we're animating from and [1] is the |
| 433 | * element we're animating to |
| 434 | */ |
| 435 | getAutoAnimatableElements( fromSlide, toSlide ) { |
| 436 | |
| 437 | let matcher = typeof this.Reveal.getConfig().autoAnimateMatcher === 'function' ? this.Reveal.getConfig().autoAnimateMatcher : this.getAutoAnimatePairs; |
| 438 | |
| 439 | let pairs = matcher.call( this, fromSlide, toSlide ); |
| 440 | |
| 441 | let reserved = []; |
| 442 | |
| 443 | // Remove duplicate pairs |
| 444 | return pairs.filter( ( pair, index ) => { |
| 445 | if( reserved.indexOf( pair.to ) === -1 ) { |
| 446 | reserved.push( pair.to ); |
| 447 | return true; |
| 448 | } |
| 449 | } ); |
| 450 | |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Identifies matching elements between slides. |
| 455 | * |
| 456 | * You can specify a custom matcher function by using |
| 457 | * the `autoAnimateMatcher` config option. |
| 458 | */ |
| 459 | getAutoAnimatePairs( fromSlide, toSlide ) { |
| 460 | |
| 461 | let pairs = []; |
| 462 | |
| 463 | const codeNodes = 'pre'; |
| 464 | const textNodes = 'h1, h2, h3, h4, h5, h6, p, li'; |
| 465 | const mediaNodes = 'img, video, iframe'; |
| 466 | |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 467 | // Explicit matches via data-id |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 468 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, '[data-id]', node => { |
| 469 | return node.nodeName + ':::' + node.getAttribute( 'data-id' ); |
| 470 | } ); |
| 471 | |
| 472 | // Text |
| 473 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, textNodes, node => { |
| 474 | return node.nodeName + ':::' + node.innerText; |
| 475 | } ); |
| 476 | |
| 477 | // Media |
| 478 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, mediaNodes, node => { |
| 479 | return node.nodeName + ':::' + ( node.getAttribute( 'src' ) || node.getAttribute( 'data-src' ) ); |
| 480 | } ); |
| 481 | |
| 482 | // Code |
| 483 | this.findAutoAnimateMatches( pairs, fromSlide, toSlide, codeNodes, node => { |
| 484 | return node.nodeName + ':::' + node.innerText; |
| 485 | } ); |
| 486 | |
| 487 | pairs.forEach( pair => { |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 488 | // Disable scale transformations on text nodes, we transition |
| 489 | // each individual text property instead |
| 490 | if( matches( pair.from, textNodes ) ) { |
| 491 | pair.options = { scale: false }; |
| 492 | } |
| 493 | // Animate individual lines of code |
| 494 | else if( matches( pair.from, codeNodes ) ) { |
| 495 | |
| 496 | // Transition the code block's width and height instead of scaling |
| 497 | // to prevent its content from being squished |
| 498 | pair.options = { scale: false, styles: [ 'width', 'height' ] }; |
| 499 | |
| 500 | // Lines of code |
| 501 | this.findAutoAnimateMatches( pairs, pair.from, pair.to, '.hljs .hljs-ln-code', node => { |
| 502 | return node.textContent; |
| 503 | }, { |
| 504 | scale: false, |
| 505 | styles: [], |
| 506 | measure: this.getLocalBoundingBox.bind( this ) |
| 507 | } ); |
| 508 | |
| 509 | // Line numbers |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 510 | this.findAutoAnimateMatches( pairs, pair.from, pair.to, '.hljs .hljs-ln-numbers[data-line-number]', node => { |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 511 | return node.getAttribute( 'data-line-number' ); |
| 512 | }, { |
| 513 | scale: false, |
| 514 | styles: [ 'width' ], |
| 515 | measure: this.getLocalBoundingBox.bind( this ) |
| 516 | } ); |
| 517 | |
| 518 | } |
| 519 | |
| 520 | }, this ); |
| 521 | |
| 522 | return pairs; |
| 523 | |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Helper method which returns a bounding box based on |
| 528 | * the given elements offset coordinates. |
| 529 | * |
| 530 | * @param {HTMLElement} element |
| 531 | * @return {Object} x, y, width, height |
| 532 | */ |
| 533 | getLocalBoundingBox( element ) { |
| 534 | |
| 535 | const presentationScale = this.Reveal.getScale(); |
| 536 | |
| 537 | return { |
| 538 | x: Math.round( ( element.offsetLeft * presentationScale ) * 100 ) / 100, |
| 539 | y: Math.round( ( element.offsetTop * presentationScale ) * 100 ) / 100, |
| 540 | width: Math.round( ( element.offsetWidth * presentationScale ) * 100 ) / 100, |
| 541 | height: Math.round( ( element.offsetHeight * presentationScale ) * 100 ) / 100 |
| 542 | }; |
| 543 | |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Finds matching elements between two slides. |
| 548 | * |
| 549 | * @param {Array} pairs List of pairs to push matches to |
| 550 | * @param {HTMLElement} fromScope Scope within the from element exists |
| 551 | * @param {HTMLElement} toScope Scope within the to element exists |
| 552 | * @param {String} selector CSS selector of the element to match |
| 553 | * @param {Function} serializer A function that accepts an element and returns |
| 554 | * a stringified ID based on its contents |
| 555 | * @param {Object} animationOptions Optional config options for this pair |
| 556 | */ |
| 557 | findAutoAnimateMatches( pairs, fromScope, toScope, selector, serializer, animationOptions ) { |
| 558 | |
| 559 | let fromMatches = {}; |
| 560 | let toMatches = {}; |
| 561 | |
| 562 | [].slice.call( fromScope.querySelectorAll( selector ) ).forEach( ( element, i ) => { |
| 563 | const key = serializer( element ); |
| 564 | if( typeof key === 'string' && key.length ) { |
| 565 | fromMatches[key] = fromMatches[key] || []; |
| 566 | fromMatches[key].push( element ); |
| 567 | } |
| 568 | } ); |
| 569 | |
| 570 | [].slice.call( toScope.querySelectorAll( selector ) ).forEach( ( element, i ) => { |
| 571 | const key = serializer( element ); |
| 572 | toMatches[key] = toMatches[key] || []; |
| 573 | toMatches[key].push( element ); |
| 574 | |
| 575 | let fromElement; |
| 576 | |
| 577 | // Retrieve the 'from' element |
| 578 | if( fromMatches[key] ) { |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 579 | const primaryIndex = toMatches[key].length - 1; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 580 | const secondaryIndex = fromMatches[key].length - 1; |
| 581 | |
| 582 | // If there are multiple identical from elements, retrieve |
| 583 | // the one at the same index as our to-element. |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 584 | if( fromMatches[key][ primaryIndex ] ) { |
| 585 | fromElement = fromMatches[key][ primaryIndex ]; |
| 586 | fromMatches[key][ primaryIndex ] = null; |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 587 | } |
| 588 | // If there are no matching from-elements at the same index, |
| 589 | // use the last one. |
| 590 | else if( fromMatches[key][ secondaryIndex ] ) { |
| 591 | fromElement = fromMatches[key][ secondaryIndex ]; |
| 592 | fromMatches[key][ secondaryIndex ] = null; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // If we've got a matching pair, push it to the list of pairs |
| 597 | if( fromElement ) { |
| 598 | pairs.push({ |
| 599 | from: fromElement, |
| 600 | to: element, |
| 601 | options: animationOptions |
| 602 | }); |
| 603 | } |
| 604 | } ); |
| 605 | |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Returns a all elements within the given scope that should |
| 610 | * be considered unmatched in an auto-animate transition. If |
| 611 | * fading of unmatched elements is turned on, these elements |
| 612 | * will fade when going between auto-animate slides. |
| 613 | * |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 614 | * Note that parents of auto-animate targets are NOT considered |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 615 | * unmatched since fading them would break the auto-animation. |
| 616 | * |
| 617 | * @param {HTMLElement} rootElement |
| 618 | * @return {Array} |
| 619 | */ |
| 620 | getUnmatchedAutoAnimateElements( rootElement ) { |
| 621 | |
| 622 | return [].slice.call( rootElement.children ).reduce( ( result, element ) => { |
| 623 | |
| 624 | const containsAnimatedElements = element.querySelector( '[data-auto-animate-target]' ); |
| 625 | |
| 626 | // The element is unmatched if |
| 627 | // - It is not an auto-animate target |
| 628 | // - It does not contain any auto-animate targets |
| 629 | if( !element.hasAttribute( 'data-auto-animate-target' ) && !containsAnimatedElements ) { |
| 630 | result.push( element ); |
| 631 | } |
| 632 | |
| 633 | if( element.querySelector( '[data-auto-animate-target]' ) ) { |
| 634 | result = result.concat( this.getUnmatchedAutoAnimateElements( element ) ); |
| 635 | } |
| 636 | |
| 637 | return result; |
| 638 | |
| 639 | }, [] ); |
| 640 | |
| 641 | } |
| 642 | |
| 643 | } |