File tree 6 files changed +52
-15
lines changed
6 files changed +52
-15
lines changed Original file line number Diff line number Diff line change 1
1
import { isMobile } from '../util/env' ;
2
2
import { body , on } from '../util/dom' ;
3
3
import * as sidebar from './sidebar' ;
4
- import { scrollIntoView } from './scroll' ;
4
+ import { scrollIntoView , scroll2Top } from './scroll' ;
5
5
6
6
export function eventMixin ( proto ) {
7
- proto . $resetEvents = function ( ) {
8
- scrollIntoView ( this . route . path , this . route . query . id ) ;
7
+ proto . $resetEvents = function ( source ) {
8
+ const { auto2top } = this . config ;
9
+
10
+ ( ( ) => {
11
+ // Rely on the browser's scroll auto-restoration when going back or forward
12
+ if ( source === 'history' ) {
13
+ return ;
14
+ }
15
+ // Scroll to ID if specified
16
+ if ( this . route . query . id ) {
17
+ scrollIntoView ( this . route . path , this . route . query . id ) ;
18
+ }
19
+ // Scroll to top if a link was clicked and auto2top is enabled
20
+ if ( source === 'navigate' ) {
21
+ auto2top && scroll2Top ( auto2top ) ;
22
+ }
23
+ } ) ( ) ;
9
24
10
25
if ( this . config . loadNavbar ) {
11
26
sidebar . getAndActive ( this . router , 'nav' ) ;
Original file line number Diff line number Diff line change @@ -148,7 +148,10 @@ export function fetchMixin(proto) {
148
148
}
149
149
} ;
150
150
151
- proto . $fetch = function ( cb = noop ) {
151
+ proto . $fetch = function (
152
+ cb = noop ,
153
+ $resetEvents = this . $resetEvents . bind ( this )
154
+ ) {
152
155
const done = ( ) => {
153
156
callHook ( this , 'doneEach' ) ;
154
157
cb ( ) ;
@@ -160,7 +163,7 @@ export function fetchMixin(proto) {
160
163
done ( ) ;
161
164
} else {
162
165
this . _fetch ( ( ) => {
163
- this . $resetEvents ( ) ;
166
+ $resetEvents ( ) ;
164
167
done ( ) ;
165
168
} ) ;
166
169
}
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import { getAndActive, sticky } from '../event/sidebar';
6
6
import { getPath , isAbsolutePath } from '../router/util' ;
7
7
import { isMobile , inBrowser } from '../util/env' ;
8
8
import { isPrimitive } from '../util/core' ;
9
- import { scrollActiveSidebar , scroll2Top } from '../event/scroll' ;
9
+ import { scrollActiveSidebar } from '../event/scroll' ;
10
10
import { Compiler } from './compiler' ;
11
11
import * as tpl from './tpl' ;
12
12
import { prerenderEmbed } from './embed' ;
@@ -123,7 +123,7 @@ export function renderMixin(proto) {
123
123
} ;
124
124
125
125
proto . _bindEventOnRendered = function ( activeEl ) {
126
- const { autoHeader, auto2top } = this . config ;
126
+ const { autoHeader } = this . config ;
127
127
128
128
scrollActiveSidebar ( this . router ) ;
129
129
@@ -136,8 +136,6 @@ export function renderMixin(proto) {
136
136
dom . before ( main , wrapper . children [ 0 ] ) ;
137
137
}
138
138
}
139
-
140
- auto2top && scroll2Top ( auto2top ) ;
141
139
} ;
142
140
143
141
proto . _renderNav = function ( text ) {
Original file line number Diff line number Diff line change @@ -30,7 +30,25 @@ export class HashHistory extends History {
30
30
}
31
31
32
32
onchange ( cb = noop ) {
33
- on ( 'hashchange' , cb ) ;
33
+ // The hashchange event does not tell us if it originated from
34
+ // a clicked link or by moving back/forward in the history;
35
+ // therefore we set a `navigating` flag when a link is clicked
36
+ // to be able to tell these two scenarios apart
37
+ let navigating = false ;
38
+
39
+ on ( 'click' , e => {
40
+ const el = e . target . tagName === 'A' ? e . target : e . target . parentNode ;
41
+
42
+ if ( el . tagName === 'A' && ! / _ b l a n k / . test ( el . target ) ) {
43
+ navigating = true ;
44
+ }
45
+ } ) ;
46
+
47
+ on ( 'hashchange' , e => {
48
+ const source = navigating ? 'navigate' : 'history' ;
49
+ navigating = false ;
50
+ cb ( { event : e , source } ) ;
51
+ } ) ;
34
52
}
35
53
36
54
normalize ( ) {
Original file line number Diff line number Diff line change @@ -28,11 +28,13 @@ export class HTML5History extends History {
28
28
e . preventDefault ( ) ;
29
29
const url = el . href ;
30
30
window . history . pushState ( { key : url } , '' , url ) ;
31
- cb ( ) ;
31
+ cb ( { event : e , source : 'navigate' } ) ;
32
32
}
33
33
} ) ;
34
34
35
- on ( 'popstate' , cb ) ;
35
+ on ( 'popstate' , e => {
36
+ cb ( { event : e , source : 'history' } ) ;
37
+ } ) ;
36
38
}
37
39
38
40
/**
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { supportsPushState } from '../util/env';
2
2
import * as dom from '../util/dom' ;
3
3
import { HashHistory } from './history/hash' ;
4
4
import { HTML5History } from './history/html5' ;
5
+ import { noop } from '../util/core' ;
5
6
6
7
export function routerMixin ( proto ) {
7
8
proto . route = { } ;
@@ -31,16 +32,16 @@ export function initRouter(vm) {
31
32
lastRoute = vm . route ;
32
33
33
34
// eslint-disable-next-line no-unused-vars
34
- router . onchange ( _ => {
35
+ router . onchange ( params => {
35
36
updateRender ( vm ) ;
36
37
vm . _updateRender ( ) ;
37
38
38
39
if ( lastRoute . path === vm . route . path ) {
39
- vm . $resetEvents ( ) ;
40
+ vm . $resetEvents ( params . source ) ;
40
41
return ;
41
42
}
42
43
43
- vm . $fetch ( ) ;
44
+ vm . $fetch ( noop , vm . $resetEvents . bind ( vm , params . source ) ) ;
44
45
lastRoute = vm . route ;
45
46
} ) ;
46
47
}
You can’t perform that action at this time.
0 commit comments