Skip to content

Commit 2777294

Browse files
frank10000PanJiaChen
authored andcommitted
perf[tagsView]: refactor the moveToTarget function (#1195)
* fix[tagsView]:fixed visited view move to currentTag * edit the scroll regular friendly * tweak
1 parent 4cdcbee commit 2777294

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

src/components/ScrollPane/index.vue

+45-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</template>
66

77
<script>
8-
const padding = 15 // tag's padding
8+
const tagAndTagSpacing = 4 // tagAndTagSpacing
99
1010
export default {
1111
name: 'ScrollPane',
@@ -20,18 +20,54 @@ export default {
2020
const $scrollWrapper = this.$refs.scrollContainer.$refs.wrap
2121
$scrollWrapper.scrollLeft = $scrollWrapper.scrollLeft + eventDelta / 4
2222
},
23-
moveToTarget($target) {
23+
moveToTarget(currentTag) {
2424
const $container = this.$refs.scrollContainer.$el
2525
const $containerWidth = $container.offsetWidth
2626
const $scrollWrapper = this.$refs.scrollContainer.$refs.wrap
27-
const $targetLeft = $target.offsetLeft
28-
const $targetWidth = $target.offsetWidth
29-
if ($targetLeft > $containerWidth) {
30-
// tag in the right
31-
$scrollWrapper.scrollLeft = $targetLeft - $containerWidth + $targetWidth + padding
27+
const tagList = this.$parent.$refs.tag
28+
29+
let firstTag = null
30+
let lastTag = null
31+
let prevTag = null
32+
let nextTag = null
33+
34+
// find first tag and last tag
35+
if (tagList.length > 0) {
36+
firstTag = tagList[0]
37+
lastTag = tagList[tagList.length - 1]
38+
}
39+
40+
// find preTag and nextTag
41+
for (let i = 0; i < tagList.length; i++) {
42+
if (tagList[i] === currentTag) {
43+
if (i === 0) {
44+
nextTag = tagList[i].length > 1 && tagList[i + 1]
45+
} else if (i === tagList.length - 1) {
46+
prevTag = tagList[i].length > 1 && tagList[i - 1]
47+
} else {
48+
prevTag = tagList[i - 1]
49+
nextTag = tagList[i + 1]
50+
}
51+
break
52+
}
53+
}
54+
55+
if (firstTag === currentTag) {
56+
$scrollWrapper.scrollLeft = 0
57+
} else if (lastTag === currentTag) {
58+
$scrollWrapper.scrollLeft = $scrollWrapper.scrollWidth - $containerWidth
3259
} else {
33-
// tag in the left
34-
$scrollWrapper.scrollLeft = $targetLeft - padding
60+
// the tag's offsetLeft after of nextTag
61+
const afterNextTagOffsetLeft = nextTag.$el.offsetLeft + nextTag.$el.offsetWidth + tagAndTagSpacing
62+
63+
// the tag's offsetLeft before of prevTag
64+
const beforePrevTagOffsetLeft = prevTag.$el.offsetLeft - tagAndTagSpacing
65+
66+
if (afterNextTagOffsetLeft > $scrollWrapper.scrollLeft + $containerWidth) {
67+
$scrollWrapper.scrollLeft = afterNextTagOffsetLeft - $containerWidth
68+
} else if (beforePrevTagOffsetLeft < $scrollWrapper.scrollLeft) {
69+
$scrollWrapper.scrollLeft = beforePrevTagOffsetLeft
70+
}
3571
}
3672
}
3773
}

src/views/layout/components/TagsView.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ export default {
7575
const tags = this.$refs.tag
7676
this.$nextTick(() => {
7777
for (const tag of tags) {
78-
if (tag.to.path === this.$route.path) {
79-
this.$refs.scrollPane.moveToTarget(tag.$el)
78+
if (tag.to === this.$route.fullPath) {
79+
this.$refs.scrollPane.moveToTarget(tag)
8080
8181
// when query is different then update
8282
if (tag.to.fullPath !== this.$route.fullPath) {

0 commit comments

Comments
 (0)