Skip to content

Commit 3dd93d0

Browse files
committed
Restore class name from onCancel event and remove previousAnimation from Animation manager.
1 parent d3ac2cb commit 3dd93d0

File tree

7 files changed

+71
-32
lines changed

7 files changed

+71
-32
lines changed

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
./src
22
./img
33
./www
4-
./react
4+
./react
5+
6+
.github
7+
.stencil

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.1.3] - 2020-04-21
10+
### Fixed
11+
- Restore class name from `onCancel` event and remove `previousAnimation` from Animation manager.
12+
913
## [1.1.2] - 2020-04-21
1014
### Fixed
1115
- Fixed issue from `destroy` method by using `cancel` instead of `finish`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@proyecto26/animatable-component",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"private": false,
55
"description": "Animate once, use Everywhere! 💫",
66
"author": "Proyecto 26",

src/components/animatable-component/animatable-component.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,20 @@ export class AnimatableComponent implements IAnimatableComponent {
322322
* Initialize manager
323323
*/
324324
componentWillLoad() {
325-
this.manager = new AnimationManager();
325+
this.manager = new AnimationManager(this);
326+
this.manager.setState(this.element, this);
326327
}
327328

328329
componentDidLoad() {
329-
this.manager.setState(this.element, this);
330+
this.manager.update();
330331
}
331332

332-
/**
333-
* Clear current animation
334-
*/
335-
async componentWillUpdate() {
336-
await this.clear();
333+
componentWillUpdate() {
334+
this.manager.setState(this.element, this);
337335
}
338336

339337
componentDidUpdate() {
340-
this.manager.setState(this.element, this);
338+
this.manager.update();
341339
}
342340

343341
componentDidUnload() {

src/components/animatable-cube/animatable-cube.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,20 @@ export class Cube implements IAnimatableComponent {
332332
* Initialize manager
333333
*/
334334
componentWillLoad() {
335-
this.manager = new AnimationManager();
335+
this.manager = new AnimationManager(this);
336336
}
337337

338338
componentDidLoad() {
339339
this.manager.setState(this.element, this);
340+
this.manager.update();
340341
}
341342

342-
/**
343-
* Clear current animation
344-
*/
345-
async componentWillUpdate() {
346-
await this.clear();
343+
componentWillUpdate() {
344+
this.manager.setState(this.element, this);
347345
}
348346

349347
componentDidUpdate() {
350-
this.manager.setState(this.element, this);
348+
this.manager.update();
351349
}
352350

353351
componentDidUnload() {

src/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
#animatable-cube {
180180
margin: 0 auto;
181181
margin-bottom: 50px;
182+
display: block;
183+
cursor: pointer;
182184
}
183185

184186
body.reverse #direction {
@@ -1070,6 +1072,16 @@ <h3>
10701072
* Animatable Components
10711073
**/
10721074
const animatableCube = document.querySelector('#animatable-cube');
1075+
1076+
animatableCube.addEventListener('click', function () {
1077+
animatableCube.getPlayState().then(function(playState) {
1078+
if (playState === 'running') {
1079+
animatableCube.pause();
1080+
} else {
1081+
animatableCube.play();
1082+
}
1083+
})
1084+
})
10731085

10741086
let currentAnimatable = animatable;
10751087

@@ -1186,6 +1198,7 @@ <h3>
11861198
console.log('ANIMATION FINISH', event);
11871199

11881200
//Remove listener and create my own infinity :)
1201+
animatable.destroy();
11891202
animatable.autoPlay = false;
11901203
animatable.removeEventListener("finish", startSecondAnimation);
11911204
animatable.options = {

src/utils/waapi.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ export class AnimationManager {
8585
private element: HTMLElement
8686
private state: IAnimatable
8787
private animation: Animation = null
88-
private previousAnimation: Animation = null
88+
private className: string
89+
90+
constructor(initState: IAnimatable) {
91+
this.state = initState;
92+
}
8993

9094
get currentAnimation(): Animation {
9195
return this.animation || this.loadAnimation();
@@ -116,12 +120,10 @@ export class AnimationManager {
116120
}
117121

118122
destroyAnimation() {
119-
const currentAnimation = this.previousAnimation || this.animation;
120-
if (currentAnimation !== null) {
121-
currentAnimation.removeEventListener('finish', this.onFinishAnimation);
122-
currentAnimation.removeEventListener('cancel', this.onCancelAnimation);
123-
currentAnimation.cancel();
124-
}
123+
if (this.animation === null) return;
124+
const currentAnimation = this.animation;
125+
this.clearAnimation();
126+
currentAnimation.cancel();
125127
}
126128

127129
playAnimation() {
@@ -136,29 +138,50 @@ export class AnimationManager {
136138
setState(element: HTMLElement, newState: IAnimatable) {
137139
this.element = element;
138140
this.state = newState;
141+
}
139142

140-
this.previousAnimation = this.currentAnimation;
143+
update() {
141144
/**
142-
* Check if `autoPlay` is enabled to emit the event and play the animation
145+
* Check if `autoPlay` is enabled to play a new animation and emit the event.
143146
*/
144-
if (this.state.autoPlay) this.playAnimation();
147+
if (this.state.autoPlay) {
148+
this.destroyAnimation();
149+
this.playAnimation();
150+
}
145151
}
146152

153+
/**
154+
* Emit `onStart` event and update class name with `fromClassName`.
155+
*/
147156
onStartAnimation = () => {
148-
const { element, state } = this;
149-
state.onStart.emit(element);
150-
if (state.fromClassName !== undefined)
151-
element.className = state.fromClassName;
157+
this.state.onStart.emit(this.element);
158+
if (this.state.fromClassName !== undefined) {
159+
this.className = this.element.className;
160+
this.element.className = this.state.fromClassName;
161+
}
152162
}
153163

164+
/**
165+
* Emit `onCancel` event and restore class name.
166+
*/
154167
onCancelAnimation = () => {
155168
this.state.onCancel.emit(this.element);
169+
if (
170+
this.state.fromClassName !== undefined &&
171+
this.className !== undefined
172+
) {
173+
this.element.className = this.className;
174+
}
156175
}
157176

177+
/**
178+
* Emit `onFinish` event and update class name with `toClassName`.
179+
*/
158180
onFinishAnimation = () => {
159181
const { element, state } = this;
160182
state.onFinish.emit(element);
161-
if (state.toClassName !== undefined)
183+
if (state.toClassName !== undefined) {
162184
element.className = state.toClassName;
185+
}
163186
}
164187
}

0 commit comments

Comments
 (0)