sbkwgh 8 anos atrás
pai
commit
4bdc14a239
2 arquivos alterados com 24 adições e 13 exclusões
  1. 23 11
      src/components/routes/Thread.vue
  2. 1 2
      src/store/modules/thread.js

+ 23 - 11
src/components/routes/Thread.vue

@@ -150,7 +150,8 @@
 			},	
 			goToPost (number, getPostNumber) {
 				let pushRoute = postNumber => {
-					if(this.$route.params.post_number === postNumber) {
+					//If postNumber is a post in `this.posts`
+					if(this.posts.find(post => post.postNumber === postNumber)) {
 						this.highlightPost(postNumber)
 					} else {
 						this.$router.push({ name: 'thread-post', params: { post_number: postNumber } })
@@ -158,6 +159,8 @@
 					}
 				}
 
+				//If `number` is actualy the postId
+				//Get the postNumber via api request
 				if(getPostNumber) {
 					this.axios
 						.get('/api/v1/post/' + number)
@@ -167,18 +170,26 @@
 				}
 			},
 			scrollTo (postNumber, cb) {
-				for(var i = 0; i < this.posts.length; i++) {
-					let post = this.posts[i]
+				let getScrollTopPosition = i => {
+					let postTop = this.$refs.posts[i].$el.getBoundingClientRect().top
+					let header = this.$refs.title.getBoundingClientRect().height
+					
+					return window.pageYOffset + postTop - header - 32
+				}
 
-					if(post.postNumber === postNumber) {
-						this.$nextTick(() => {
-							let postTop = this.$refs.posts[i].$el.getBoundingClientRect().top
-							let header = this.$refs.title.getBoundingClientRect().height
-							debugger
-							window.scrollTo(0, postTop - header - 32)
+				let scroll = (i) => {
+					let post = this.posts[i]
+					window.scrollTo(0, getScrollTopPosition(i))
+					if(cb) cb(i, post)
+				}
 
-							if(cb) cb(i, post)
-						})
+				for(var i = 0; i < this.posts.length; i++) {
+					if(this.posts[i].postNumber === postNumber) {
+						if(this.$refs.posts) {
+							scroll(i)
+						} else {
+							this.$nextTick(_ => scroll(i))
+						}
 
 						break;
 					}
@@ -187,6 +198,7 @@
 			highlightPost (postNumber) {
 				this.scrollTo(postNumber, (i) => {
 					this.highlightedPostIndex = i
+					this.$router.push({ name: 'thread-post', params: { post_number: postNumber } })
 					
 					if(this.highlightedPostIndex === i) {
 						setTimeout(() => this.highlightedPostIndex = null, 3000)

+ 1 - 2
src/store/modules/thread.js

@@ -84,9 +84,8 @@ const actions = {
 				commit('setPosts', res.data.Posts)
 
 				if(postNumber !== undefined) {
-					//vue.highlightPost(+postNumber)
 					vue.$router.push({ name: 'thread-post', params: { post_number: postNumber } })
-					vue.scrollTo(postNumber)
+					vue.highlightPost(+postNumber)
 				}
 			}).catch(AjaxErrorHandler(vue.$store))
 	},