animations.js (2917B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const { 8 UPDATE_ANIMATIONS, 9 UPDATE_DETAIL_VISIBILITY, 10 UPDATE_ELEMENT_PICKER_ENABLED, 11 UPDATE_HIGHLIGHTED_NODE, 12 UPDATE_PLAYBACK_RATES, 13 UPDATE_SELECTED_ANIMATION, 14 UPDATE_SIDEBAR_SIZE, 15 } = require("resource://devtools/client/inspector/animation/actions/index.js"); 16 17 loader.lazyRequireGetter( 18 this, 19 "TimeScale", 20 "resource://devtools/client/inspector/animation/utils/timescale.js" 21 ); 22 23 const INITIAL_STATE = { 24 animations: [], 25 detailVisibility: false, 26 elementPickerEnabled: false, 27 highlightedNode: null, 28 playbackRates: [], 29 selectedAnimation: null, 30 sidebarSize: { 31 height: 0, 32 width: 0, 33 }, 34 timeScale: null, 35 }; 36 37 const reducers = { 38 [UPDATE_ANIMATIONS](state, { animations }) { 39 let detailVisibility = state.detailVisibility; 40 let selectedAnimation = state.selectedAnimation; 41 42 if ( 43 !state.selectedAnimation || 44 !animations.find( 45 animation => animation.actorID === selectedAnimation.actorID 46 ) 47 ) { 48 selectedAnimation = animations.length === 1 ? animations[0] : null; 49 detailVisibility = !!selectedAnimation; 50 } 51 52 const playbackRates = getPlaybackRates(state.playbackRates, animations); 53 54 return Object.assign({}, state, { 55 animations, 56 detailVisibility, 57 playbackRates, 58 selectedAnimation, 59 timeScale: new TimeScale(animations), 60 }); 61 }, 62 63 [UPDATE_DETAIL_VISIBILITY](state, { detailVisibility }) { 64 const selectedAnimation = detailVisibility ? state.selectedAnimation : null; 65 66 return Object.assign({}, state, { 67 detailVisibility, 68 selectedAnimation, 69 }); 70 }, 71 72 [UPDATE_ELEMENT_PICKER_ENABLED](state, { elementPickerEnabled }) { 73 return Object.assign({}, state, { 74 elementPickerEnabled, 75 }); 76 }, 77 78 [UPDATE_HIGHLIGHTED_NODE](state, { highlightedNode }) { 79 return Object.assign({}, state, { 80 highlightedNode, 81 }); 82 }, 83 84 [UPDATE_PLAYBACK_RATES](state) { 85 return Object.assign({}, state, { 86 playbackRates: getPlaybackRates([], state.animations), 87 }); 88 }, 89 90 [UPDATE_SELECTED_ANIMATION](state, { selectedAnimation }) { 91 const detailVisibility = !!selectedAnimation; 92 93 return Object.assign({}, state, { 94 detailVisibility, 95 selectedAnimation, 96 }); 97 }, 98 99 [UPDATE_SIDEBAR_SIZE](state, { sidebarSize }) { 100 return Object.assign({}, state, { 101 sidebarSize, 102 }); 103 }, 104 }; 105 106 function getPlaybackRates(basePlaybackRate, animations) { 107 return [ 108 ...new Set( 109 animations.map(a => a.state.playbackRate).concat(basePlaybackRate) 110 ), 111 ]; 112 } 113 114 module.exports = function (state = INITIAL_STATE, action) { 115 const reducer = reducers[action.type]; 116 return reducer ? reducer(state, action) : state; 117 };