File tree Expand file tree Collapse file tree 6 files changed +49
-15
lines changed Expand file tree Collapse file tree 6 files changed +49
-15
lines changed Original file line number Diff line number Diff line change 11import { isMobile } from '../util/env'
22import { body , on } from '../util/dom'
33import * as sidebar from './sidebar'
4- import { scrollIntoView } from './scroll'
4+ import { scrollIntoView , scroll2Top } from './scroll'
55
66export 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+ } ) ( ) ;
924
1025 if ( this . config . loadNavbar ) {
1126 sidebar . getAndActive ( this . router , 'nav' )
Original file line number Diff line number Diff line change @@ -145,7 +145,7 @@ export function fetchMixin(proto) {
145145 }
146146 }
147147
148- proto . $fetch = function ( cb = noop ) {
148+ proto . $fetch = function ( cb = noop , $resetEvents = this . $resetEvents . bind ( this ) ) {
149149 const done = ( ) => {
150150 callHook ( this , 'doneEach' )
151151 cb ( )
@@ -157,7 +157,7 @@ export function fetchMixin(proto) {
157157 done ( )
158158 } else {
159159 this . _fetch ( ( ) => {
160- this . $resetEvents ( )
160+ $resetEvents ( ) ;
161161 done ( )
162162 } )
163163 }
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ import {getAndActive, sticky} from '../event/sidebar'
88import { getPath , isAbsolutePath } from '../router/util'
99import { isMobile , inBrowser } from '../util/env'
1010import { isPrimitive } from '../util/core'
11- import { scrollActiveSidebar , scroll2Top } from '../event/scroll'
11+ import { scrollActiveSidebar } from '../event/scroll'
1212import { prerenderEmbed } from './embed'
1313
1414function executeScript ( ) {
@@ -109,7 +109,7 @@ export function renderMixin(proto) {
109109 }
110110
111111 proto . _bindEventOnRendered = function ( activeEl ) {
112- const { autoHeader, auto2top } = this . config
112+ const { autoHeader} = this . config
113113
114114 scrollActiveSidebar ( this . router )
115115
@@ -122,8 +122,6 @@ export function renderMixin(proto) {
122122 dom . before ( main , h1 )
123123 }
124124 }
125-
126- auto2top && scroll2Top ( auto2top )
127125 }
128126
129127 proto . _renderNav = function ( text ) {
Original file line number Diff line number Diff line change @@ -30,7 +30,25 @@ export class HashHistory extends History {
3030 }
3131
3232 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+ } )
3452 }
3553
3654 normalize ( ) {
Original file line number Diff line number Diff line change @@ -28,11 +28,13 @@ export class HTML5History extends History {
2828 e . preventDefault ( )
2929 const url = el . href
3030 window . history . pushState ( { key : url } , '' , url )
31- cb ( )
31+ cb ( { event : e , source : 'navigate' } )
3232 }
3333 } )
3434
35- on ( 'popstate' , cb )
35+ on ( 'popstate' , e => {
36+ cb ( { event : e , source : 'history' } )
37+ } )
3638 }
3739
3840 /**
Original file line number Diff line number Diff line change 11import { HashHistory } from './history/hash'
22import { HTML5History } from './history/html5'
33import { supportsPushState } from '../util/env'
4+ import { noop } from '../util/core'
45import * as dom from '../util/dom'
56
67export function routerMixin ( proto ) {
@@ -30,16 +31,16 @@ export function initRouter(vm) {
3031 updateRender ( vm )
3132 lastRoute = vm . route
3233
33- router . onchange ( _ => {
34+ router . onchange ( params => {
3435 updateRender ( vm )
3536 vm . _updateRender ( )
3637
3738 if ( lastRoute . path === vm . route . path ) {
38- vm . $resetEvents ( )
39+ vm . $resetEvents ( params . source )
3940 return
4041 }
4142
42- vm . $fetch ( )
43+ vm . $fetch ( noop , vm . $resetEvents . bind ( vm , params . source ) )
4344 lastRoute = vm . route
4445 } )
4546}
You can’t perform that action at this time.
0 commit comments