Skip to content

Commit 9debc07

Browse files
committed
optimize code
1 parent 68d0cac commit 9debc07

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-flow-render",
3-
"version": "0.1.39",
3+
"version": "1.0.0",
44
"main": "dist/vue-flow-render.umd.min.js",
55
"files": [
66
"dist"
@@ -29,10 +29,10 @@
2929
"vue": "^2.6.10"
3030
},
3131
"devDependencies": {
32-
"@vue/cli-plugin-babel": "^3.9.2",
33-
"@vue/cli-plugin-eslint": "^3.9.2",
34-
"@vue/cli-plugin-unit-jest": "^3.9.0",
35-
"@vue/cli-service": "^3.9.3",
32+
"@vue/cli-plugin-babel": "^3.10.0",
33+
"@vue/cli-plugin-eslint": "^3.10.0",
34+
"@vue/cli-plugin-unit-jest": "^3.10.0",
35+
"@vue/cli-service": "^3.10.0",
3636
"@vue/eslint-config-prettier": "^5.0.0",
3737
"@vue/test-utils": "1.0.0-beta.29",
3838
"babel-core": "7.0.0-bridge.0",

src/render.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default {
9999
scroll (offset, up) {
100100
const isUp = up === undefined ? offset < this.lastScrollTop : up
101101
this.lastScrollTop = offset
102-
const { start, remain, cache, offsetTop, total, wrapHeight } = this
102+
const { start, remain, cache, total } = this
103103
/**
104104
* 元素比较少,还不需要懒加载
105105
*/
@@ -109,7 +109,7 @@ export default {
109109
/**
110110
* 实际的滚动的高度要减去 offset
111111
*/
112-
const scrollTop = offset - offsetTop
112+
const scrollTop = offset - this.offsetTop
113113
/**
114114
* 位移修正(iOS offset 可能为负值)
115115
*/
@@ -135,7 +135,7 @@ export default {
135135
* 2. 当前列表的第一个元素的顶部已经进入视口
136136
*/
137137
if (
138-
cache[start + remain - 1].top > scrollTop + wrapHeight ||
138+
cache[start + remain - 1].top > scrollTop + this.wrapHeight ||
139139
cache[start].top > scrollTop
140140
) {
141141
this.style.paddingTop -= cache[start - 1].height
@@ -156,7 +156,7 @@ export default {
156156
*/
157157
if (
158158
cache[start].bottom < scrollTop ||
159-
cache[start + remain - 1].bottom < scrollTop + wrapHeight
159+
cache[start + remain - 1].bottom < scrollTop + this.wrapHeight
160160
) {
161161
this.style.paddingTop += cache[start].height
162162
this.start++
@@ -168,11 +168,11 @@ export default {
168168
height: 0,
169169
paddingTop: 0
170170
}
171-
this.cache = {}
172171
this.start = 0
172+
this.cache = {}
173173
},
174174
_adjustStart () {
175-
const { lastScrollTop, cache, start, isSameHeight, height, remain, column, offsetTop, total, wrapHeight } = this
175+
const { cache, start, remain, total } = this
176176
/**
177177
* 元素比较少,还不需要懒加载
178178
*/
@@ -182,7 +182,7 @@ export default {
182182
/**
183183
* 如果在顶部,则直接修正
184184
*/
185-
const scrollTop = lastScrollTop - offsetTop
185+
const scrollTop = this.lastScrollTop - this.offsetTop
186186
if (scrollTop <= 0) {
187187
this.style.paddingTop = 0
188188
this.start = 0
@@ -191,12 +191,14 @@ export default {
191191
/**
192192
* 如果触底了,则直接修正
193193
*/
194-
const scrollBottom = scrollTop + wrapHeight
194+
const scrollBottom = scrollTop + this.wrapHeight
195195
if (scrollBottom >= cache[total - 1].bottom) {
196196
this.start = total - remain
197197
this.style.paddingTop = cache[total - remain].top
198198
return
199199
}
200+
201+
const { isSameHeight, column, height } = this
200202
/**
201203
* 向上修正
202204
*/
@@ -219,7 +221,7 @@ export default {
219221
} else {
220222
/**
221223
* 如果元素不等高
222-
* 从当前列表的上一个元素开始,到第 0 个元素结束开始循环
224+
* 从当前列表的上一个元素开始,到第 0 个元素结束
223225
* 寻找第一个顶部在视口边界的元素
224226
*/
225227
for (let i = start - 1; i >= 0; i--) {
@@ -254,7 +256,7 @@ export default {
254256
} else {
255257
/**
256258
* 如果元素不等高
257-
* 从最后一个元素的下一个元素开始,到最后一个元素开始循环
259+
* 从当前列表的最后一个元素的下一个元素开始,到最后一个元素结束
258260
* 寻找第一个底部在视口边界的元素
259261
*/
260262
for (let i = start + remain; i < total; i++) {
@@ -275,11 +277,12 @@ export default {
275277
adjustDown()
276278
},
277279
_computeRenderHeight (items, offset) {
278-
const { height, isSameHeight, total, column, cache, isSingleColumn } = this
280+
const { total, column, cache } = this
279281
if (!total) {
280282
return
281283
}
282-
if (isSameHeight) {
284+
if (this.isSameHeight) {
285+
const height = this.height
283286
const end = items ? items.length : total - offset
284287
for (let i = 0; i < end; i++) {
285288
const top = height * Math.floor((i + offset) / column)
@@ -291,10 +294,10 @@ export default {
291294
}
292295
this.style.height = height * total / column
293296
} else {
294-
if (isSingleColumn) {
297+
if (this.isSingleColumn) {
295298
let beforeHeight = offset ? cache[offset - 1].bottom : 0
296299
items.forEach((item, index) => {
297-
const hgt = +item.data.style.height.replace('px', '')
300+
const hgt = parseInt(item.data.style.height, 10)
298301
cache[index + offset] = {
299302
height: hgt,
300303
top: beforeHeight,
@@ -316,7 +319,7 @@ export default {
316319
items.forEach((item, index) => {
317320
const realIndex = index + offset
318321
const beforeHeight = Math.min(...offsets)
319-
const hgt = +item.data.style.height.replace('px', '')
322+
const hgt = parseInt(item.data.style.height, 10)
320323
cache[realIndex] = {
321324
height: hgt,
322325
top: beforeHeight,
@@ -329,13 +332,13 @@ export default {
329332
}
330333
},
331334
_filter (h) {
332-
const { remain, total, start, item, getter } = this
335+
const { remain, total, start, item } = this
333336
const end = remain >= total ? total : start + remain
334337

335338
if (item) {
336339
const result = []
337340
for (let i = start; i < end; i++) {
338-
result.push(h(item, getter(i)))
341+
result.push(h(item, this.getter(i)))
339342
}
340343
return result
341344
}
@@ -347,17 +350,14 @@ export default {
347350
}
348351
},
349352
render: function (h) {
350-
const { paddingTop, height } = this.style
351-
const list = this._filter(h)
352-
353353
return h('div', {
354354
'style': {
355355
boxSizing: 'border-box',
356-
height: `${height}px`,
357-
paddingTop: `${paddingTop}px`,
358-
willChange: 'padding-top'
356+
willChange: 'padding-top',
357+
paddingTop: `${this.style.paddingTop}px`,
358+
height: `${this.style.height}px`
359359
},
360360
'class': 'vue-flow-render'
361-
}, list)
361+
}, this._filter(h))
362362
}
363363
}

yarn.lock

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -928,10 +928,10 @@
928928
lodash.kebabcase "^4.1.1"
929929
svg-tags "^1.0.0"
930930

931-
"@vue/babel-preset-app@^3.9.2":
932-
version "3.9.2"
933-
resolved "https://registry.npm.taobao.org/@vue/babel-preset-app/download/@vue/babel-preset-app-3.9.2.tgz#b72a9b06abbe3f8f272783be13951271277be338"
934-
integrity sha1-tyqbBqu+P48nJ4O+E5UScSd74zg=
931+
"@vue/babel-preset-app@^3.10.0":
932+
version "3.10.0"
933+
resolved "https://registry.yarnpkg.com/@vue/babel-preset-app/-/babel-preset-app-3.10.0.tgz#3f89d631dd0f174c8a72e769b55f081c533c4677"
934+
integrity sha512-NzJLI4Qe0SYm9gVHQC9RXyP0YcPjI28TmZ0ds2RJa9NO96LXHLES2U1HqiMDN4+CVjOQFrWUNd7wWeaETRPXbg==
935935
dependencies:
936936
"@babel/helper-module-imports" "^7.0.0"
937937
"@babel/plugin-proposal-class-properties" "^7.0.0"
@@ -994,28 +994,28 @@
994994
"@vue/babel-plugin-transform-vue-jsx" "^1.0.0"
995995
camelcase "^5.0.0"
996996

997-
"@vue/cli-overlay@^3.9.0":
998-
version "3.9.0"
999-
resolved "https://registry.npm.taobao.org/@vue/cli-overlay/download/@vue/cli-overlay-3.9.0.tgz#11f513d1fa11b0135fb8ba8b88d228df0dc542e0"
1000-
integrity sha1-EfUT0foRsBNfuLqLiNIo3w3FQuA=
997+
"@vue/cli-overlay@^3.10.0":
998+
version "3.10.0"
999+
resolved "https://registry.yarnpkg.com/@vue/cli-overlay/-/cli-overlay-3.10.0.tgz#9266c96fb2bffd35ca96edd8e1e1284ca8c00c94"
1000+
integrity sha512-DQCY6WIl1UN1nOuPirW63CcYWSBdIn6s4zdGfFodCfV+0PAEXGcrfNStygG+IKUsydQaJGTneV7SFxcS+9gyzA==
10011001

1002-
"@vue/cli-plugin-babel@^3.9.2":
1003-
version "3.9.2"
1004-
resolved "https://registry.npm.taobao.org/@vue/cli-plugin-babel/download/@vue/cli-plugin-babel-3.9.2.tgz#8ff962a383aaeafd2b280998428a57ea23e9539c"
1005-
integrity sha1-j/lio4Oq6v0rKAmYQopX6iPpU5w=
1002+
"@vue/cli-plugin-babel@^3.10.0":
1003+
version "3.10.0"
1004+
resolved "https://registry.yarnpkg.com/@vue/cli-plugin-babel/-/cli-plugin-babel-3.10.0.tgz#3c5300bd6daf30b53292a6c3982e3a681cba2c18"
1005+
integrity sha512-NHrg6ZYN2fh5ZiMMzCNRuDlH9mcTOu+GIti1Va/zPnG3qMkX2iZ0zZGFaOCltIFoVSXdyOfa0sMtJGDoP9Q7ZA==
10061006
dependencies:
10071007
"@babel/core" "^7.0.0"
1008-
"@vue/babel-preset-app" "^3.9.2"
1009-
"@vue/cli-shared-utils" "^3.9.0"
1008+
"@vue/babel-preset-app" "^3.10.0"
1009+
"@vue/cli-shared-utils" "^3.10.0"
10101010
babel-loader "^8.0.5"
10111011
webpack ">=4 < 4.29"
10121012

1013-
"@vue/cli-plugin-eslint@^3.9.2":
1014-
version "3.9.2"
1015-
resolved "https://registry.npm.taobao.org/@vue/cli-plugin-eslint/download/@vue/cli-plugin-eslint-3.9.2.tgz#747c616b13a11f34ac80554eee899cbfcd1977b8"
1016-
integrity sha1-dHxhaxOhHzSsgFVO7omcv80Zd7g=
1013+
"@vue/cli-plugin-eslint@^3.10.0":
1014+
version "3.10.0"
1015+
resolved "https://registry.yarnpkg.com/@vue/cli-plugin-eslint/-/cli-plugin-eslint-3.10.0.tgz#44a2abcecadc5b4854ee8fc4a7c5930f660293b0"
1016+
integrity sha512-grM3Z4je8XlPomhYqTC4ILB26rTrJKZhEkCwbXdduMrWtpn2Ggotl2nYayplOzDgoZ4Cx3ykJMkRulla2Zi47g==
10171017
dependencies:
1018-
"@vue/cli-shared-utils" "^3.9.0"
1018+
"@vue/cli-shared-utils" "^3.10.0"
10191019
babel-eslint "^10.0.1"
10201020
eslint-loader "^2.1.2"
10211021
globby "^9.2.0"
@@ -1025,12 +1025,12 @@
10251025
eslint "^4.19.1"
10261026
eslint-plugin-vue "^4.7.1"
10271027

1028-
"@vue/cli-plugin-unit-jest@^3.9.0":
1029-
version "3.9.0"
1030-
resolved "https://registry.npm.taobao.org/@vue/cli-plugin-unit-jest/download/@vue/cli-plugin-unit-jest-3.9.0.tgz#0662adee9467006381b9cddc7fb3b0a21ccbbd07"
1031-
integrity sha1-BmKt7pRnAGOBuc3cf7OwohzLvQc=
1028+
"@vue/cli-plugin-unit-jest@^3.10.0":
1029+
version "3.10.0"
1030+
resolved "https://registry.yarnpkg.com/@vue/cli-plugin-unit-jest/-/cli-plugin-unit-jest-3.10.0.tgz#6fe6d712d9a089239ae4a9f5c18888fe720f285c"
1031+
integrity sha512-5+BLqwkjpU+LMxilzSMIy6bo20ygWGULJ9a4/za+u72ebMic0F1mFwS28niW5mEZheUQU3vzSTyVBfJ4Wj8dow==
10321032
dependencies:
1033-
"@vue/cli-shared-utils" "^3.9.0"
1033+
"@vue/cli-shared-utils" "^3.10.0"
10341034
babel-jest "^23.6.0"
10351035
babel-plugin-transform-es2015-modules-commonjs "^6.26.2"
10361036
jest "^23.6.0"
@@ -1039,15 +1039,15 @@
10391039
jest-watch-typeahead "0.2.1"
10401040
vue-jest "^3.0.4"
10411041

1042-
"@vue/cli-service@^3.9.3":
1043-
version "3.9.3"
1044-
resolved "https://registry.npm.taobao.org/@vue/cli-service/download/@vue/cli-service-3.9.3.tgz#4bd5786465afb8b608c5c7fc9661390e747ea756"
1045-
integrity sha1-S9V4ZGWvuLYIxcf8lmE5DnR+p1Y=
1042+
"@vue/cli-service@^3.10.0":
1043+
version "3.10.0"
1044+
resolved "https://registry.yarnpkg.com/@vue/cli-service/-/cli-service-3.10.0.tgz#1e61afac9eeed5d902c124715c29719abeb743c5"
1045+
integrity sha512-aQaAjtkpSl4XFBM7Di9doh4GvHp1H8/H1QJKRK8bPJ5MVbt8Lt4tBo+YgK9Qs/9x0mrxCAa5UrR9+8ZWIQs2Zw==
10461046
dependencies:
10471047
"@intervolga/optimize-cssnano-plugin" "^1.0.5"
10481048
"@soda/friendly-errors-webpack-plugin" "^1.7.1"
1049-
"@vue/cli-overlay" "^3.9.0"
1050-
"@vue/cli-shared-utils" "^3.9.0"
1049+
"@vue/cli-overlay" "^3.10.0"
1050+
"@vue/cli-shared-utils" "^3.10.0"
10511051
"@vue/component-compiler-utils" "^2.6.0"
10521052
"@vue/preload-webpack-plugin" "^1.1.0"
10531053
"@vue/web-component-wrapper" "^1.2.0"
@@ -1101,10 +1101,10 @@
11011101
webpack-dev-server "^3.4.1"
11021102
webpack-merge "^4.2.1"
11031103

1104-
"@vue/cli-shared-utils@^3.9.0":
1105-
version "3.9.0"
1106-
resolved "https://registry.npm.taobao.org/@vue/cli-shared-utils/download/@vue/cli-shared-utils-3.9.0.tgz#cb56a443bf763a873849a11d07e9e7638aa16cc2"
1107-
integrity sha1-y1akQ792Ooc4SaEdB+nnY4qhbMI=
1104+
"@vue/cli-shared-utils@^3.10.0":
1105+
version "3.10.0"
1106+
resolved "https://registry.yarnpkg.com/@vue/cli-shared-utils/-/cli-shared-utils-3.10.0.tgz#9d156f3c0ef675a939319062489e98c8d3d80f7e"
1107+
integrity sha512-i96XBUtLdWeKFCC/ot12ngqnVikN/dXpelGdyxvNZczCkX7Je0FUdrZkiw0+uTYTu1RmuYWpLs+vb/YQerjiWg==
11081108
dependencies:
11091109
"@hapi/joi" "^15.0.1"
11101110
chalk "^2.4.1"

0 commit comments

Comments
 (0)