@@ -5,11 +5,6 @@ import hljsTheme from 'browser/lib/hljsThemes'
55
66const markdownStyle = require ( '!!css!stylus?sourceMap!./markdown.styl' ) [ 0 ] [ 1 ]
77const { shell } = require ( 'electron' )
8- const goExternal = function ( e ) {
9- e . preventDefault ( )
10- e . stopPropagation ( )
11- shell . openExternal ( e . target . href )
12- }
138
149const OSX = global . process . platform === 'darwin'
1510
@@ -27,17 +22,39 @@ export default class MarkdownPreview extends React.Component {
2722 this . contextMenuHandler = ( e ) => this . handleContextMenu ( e )
2823 this . mouseDownHandler = ( e ) => this . handleMouseDown ( e )
2924 this . mouseUpHandler = ( e ) => this . handleMouseUp ( e )
25+ this . goExternalHandler = ( e ) => this . handlePreviewAnchorClick ( e )
26+ }
27+
28+ handlePreviewAnchorClick ( e ) {
29+ e . preventDefault ( )
30+ e . stopPropagation ( )
31+
32+ let href = e . target . getAttribute ( 'href' )
33+ if ( _ . isString ( href ) && href . match ( / ^ # / ) ) {
34+ let targetElement = this . refs . root . contentWindow . document . getElementById ( href . substring ( 1 , href . length ) )
35+ if ( targetElement != null ) {
36+ this . getWindow ( ) . scrollTo ( 0 , targetElement . offsetTop )
37+ }
38+ } else {
39+ shell . openExternal ( e . target . href )
40+ }
3041 }
3142
3243 handleContextMenu ( e ) {
3344 this . props . onContextMenu ( e )
3445 }
3546
3647 handleMouseDown ( e ) {
48+ if ( e . target != null && e . target . tagName === 'A' ) {
49+ return null
50+ }
3751 if ( this . props . onMouseDown != null ) this . props . onMouseDown ( e )
3852 }
3953
4054 handleMouseUp ( e ) {
55+ if ( e . target != null && e . target . tagName === 'A' ) {
56+ return null
57+ }
4158 if ( this . props . onMouseUp != null ) this . props . onMouseUp ( e )
4259 }
4360
@@ -68,7 +85,7 @@ export default class MarkdownPreview extends React.Component {
6885
6986 rewriteIframe ( ) {
7087 Array . prototype . forEach . call ( this . refs . root . contentWindow . document . querySelectorAll ( 'a' ) , ( el ) => {
71- el . removeEventListener ( 'click' , goExternal )
88+ el . removeEventListener ( 'click' , this . goExternalHandler )
7289 } )
7390
7491 let { value, fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme } = this . props
@@ -111,7 +128,7 @@ export default class MarkdownPreview extends React.Component {
111128 this . refs . root . contentWindow . document . body . innerHTML = markdown ( value )
112129
113130 Array . prototype . forEach . call ( this . refs . root . contentWindow . document . querySelectorAll ( 'a' ) , ( el ) => {
114- el . addEventListener ( 'mousedown ' , goExternal )
131+ el . addEventListener ( 'click ' , this . goExternalHandler )
115132 } )
116133 }
117134
@@ -147,6 +164,7 @@ export default class MarkdownPreview extends React.Component {
147164 style = { style }
148165 tabIndex = { tabIndex }
149166 ref = 'root'
167+ onClick = { ( e ) => this . handleClick ( e ) }
150168 />
151169 )
152170 }
0 commit comments