Skip to content

Commit bd20bfe

Browse files
committed
add setDefaultStyle
1 parent e03b785 commit bd20bfe

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@
6767
},
6868
"devDependencies": {
6969
"@types/react": "^16.0.0",
70+
"antd": "^3.12.1",
7071
"core-js": "^2.5.7",
7172
"expect.js": "0.3.x",
7273
"precommit-hook": "^3.0.0",
74+
"rc-queue-anim": "^1.6.1",
75+
"rc-scroll-anim": "2.x",
7376
"rc-test": "6.x",
7477
"rc-tools": "8.x",
7578
"react": "^16.4.0",
7679
"react-dom": "^16.4.0",
77-
"rc-queue-anim": "^1.6.1",
78-
"rc-scroll-anim": "2.x",
7980
"tslint-config-prettier": "^1.17.0",
8081
"tslint-react": "^3.6.0",
8182
"typescript": "3.x"
@@ -88,7 +89,7 @@
8889
"prop-types": "^15.6.1",
8990
"babel-runtime": "6.x",
9091
"raf": "~3.4.0",
91-
"style-utils": "~0.1.13",
92+
"style-utils": "~0.2.0",
9293
"tween-functions": "~1.2.0"
9394
},
9495
"types": "typings/index.d.ts"

src/Tween.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import {
88
toFixed,
99
stylesToCss,
1010
createMatrix,
11+
getGsapType,
12+
isTransform,
1113
checkStyleName,
14+
toCssLowerCase,
1215
} from 'style-utils';
1316

1417
import easingTypes from './easing';
@@ -88,7 +91,7 @@ p.setAttrIsStyle = function () {
8891
}
8992
});
9093
data[i].style = _d;
91-
this.startDefaultData.style = this.target.getAttribute('style');
94+
this.startDefaultData.style = this.target.getAttribute('style') || '';
9295
} else if (this.attr === 'attr') {
9396
Object.keys(_d).forEach(key => {
9497
if (key === 'style' && Array.isArray(d[key])) {
@@ -97,7 +100,7 @@ p.setAttrIsStyle = function () {
97100
if (key === 'bezier') {
98101
_d.style = { ..._d.style, bezier: _d[key] };
99102
delete _d[key];
100-
this.startDefaultData.style = this.target.getAttribute('style');
103+
this.startDefaultData.style = this.target.getAttribute('style') || '';
101104
} else {
102105
this.startDefaultData[key] = this.target.getAttribute(key);
103106
}
@@ -389,15 +392,50 @@ p.resetAnimData = function () {
389392
this.start = {};
390393
};
391394

395+
p.getDefaultStyle = function (domStyle) {
396+
const $data = defaultData({}, 0);
397+
const getStyleToArray = (styleString) => (
398+
styleString.split(';').filter(c => c).map(str =>
399+
str.split(':').map(s => s.trim())
400+
)
401+
);
402+
const styleToArray = getStyleToArray(this.startDefaultData.style);
403+
let domStyleToArray = getStyleToArray(domStyle);
404+
this.data.forEach(value => {
405+
Object.keys(value).forEach(name => {
406+
if (!(name in $data)) {
407+
const styleName = toCssLowerCase(isTransform(getGsapType(name)));
408+
domStyleToArray = domStyleToArray.filter(item => item[0] !== styleName);
409+
}
410+
})
411+
});
412+
styleToArray.forEach(item => {
413+
domStyleToArray = domStyleToArray.map($item => {
414+
if ($item[0] === item[0]) {
415+
return item;
416+
}
417+
return $item;
418+
});
419+
})
420+
return domStyleToArray.map(item => item.join(':')).join(';');
421+
}
422+
392423
p.resetDefaultStyle = function () {
393424
this.tween = {};
394425
this.defaultData = this.defaultData.map(item => {
395426
item.reset = true;
396427
delete item.mode;
397428
return item;
398429
});
430+
const data = defaultData({}, 0);
399431
Object.keys(this.startDefaultData).forEach(key => {
400-
if (!(key in defaultData({}, 0))) {
432+
if (!(key in data)) {
433+
if (key === 'style') {
434+
const value = this.getDefaultStyle(this.target.style.cssText);
435+
this.target.setAttribute(key, value);
436+
} else {
437+
this.target.setAttribute(key, this.startDefaultData[key]);
438+
}
401439
this.target.setAttribute(key, this.startDefaultData[key]);
402440
this.computedStyle = null;
403441
}
@@ -406,7 +444,7 @@ p.resetDefaultStyle = function () {
406444

407445
p.reStart = function (style) {
408446
this.start = {};
409-
this.target.style.cssText = '';
447+
this.target.style.cssText = this.getDefaultStyle(this.target.style.cssText);
410448
Object.keys(style || {}).forEach(key => {
411449
this.target.style[key] = stylesToCss(key, style[key]);
412450
});

src/plugin/StylePlugin.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable func-names, no-console */
2-
import cssList, {
2+
import {
3+
cssList,
34
checkStyleName,
45
getGsapType,
56
parseShadow,

0 commit comments

Comments
 (0)