@@ -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,48 @@ 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 . anchorClickHandler = ( e ) => this . handlePreviewAnchorClick ( e )
26+ this . checkboxClickHandler = ( e ) => this . handleCheckboxClick ( e )
27+ }
28+
29+ handlePreviewAnchorClick ( e ) {
30+ e . preventDefault ( )
31+ e . stopPropagation ( )
32+
33+ let href = e . target . getAttribute ( 'href' )
34+ if ( _ . isString ( href ) && href . match ( / ^ # / ) ) {
35+ let targetElement = this . refs . root . contentWindow . document . getElementById ( href . substring ( 1 , href . length ) )
36+ if ( targetElement != null ) {
37+ this . getWindow ( ) . scrollTo ( 0 , targetElement . offsetTop )
38+ }
39+ } else {
40+ shell . openExternal ( e . target . href )
41+ }
42+ }
43+
44+ handleCheckboxClick ( e ) {
45+ this . props . onCheckboxClick ( e )
3046 }
3147
3248 handleContextMenu ( e ) {
3349 this . props . onContextMenu ( e )
3450 }
3551
3652 handleMouseDown ( e ) {
53+ if ( e . target != null ) {
54+ switch ( e . target . tagName ) {
55+ case 'A' :
56+ case 'INPUT' :
57+ return null
58+ }
59+ }
3760 if ( this . props . onMouseDown != null ) this . props . onMouseDown ( e )
3861 }
3962
4063 handleMouseUp ( e ) {
64+ if ( e . target != null && e . target . tagName === 'A' ) {
65+ return null
66+ }
4167 if ( this . props . onMouseUp != null ) this . props . onMouseUp ( e )
4268 }
4369
@@ -68,7 +94,10 @@ export default class MarkdownPreview extends React.Component {
6894
6995 rewriteIframe ( ) {
7096 Array . prototype . forEach . call ( this . refs . root . contentWindow . document . querySelectorAll ( 'a' ) , ( el ) => {
71- el . removeEventListener ( 'click' , goExternal )
97+ el . removeEventListener ( 'click' , this . anchorClickHandler )
98+ } )
99+ Array . prototype . forEach . call ( this . refs . root . contentWindow . document . querySelectorAll ( 'input[type="checkbox"]' ) , ( el ) => {
100+ el . removeEventListener ( 'click' , this . checkboxClickHandler )
72101 } )
73102
74103 let { value, fontFamily, fontSize, codeBlockFontFamily, lineNumber, codeBlockTheme } = this . props
@@ -111,7 +140,10 @@ export default class MarkdownPreview extends React.Component {
111140 this . refs . root . contentWindow . document . body . innerHTML = markdown ( value )
112141
113142 Array . prototype . forEach . call ( this . refs . root . contentWindow . document . querySelectorAll ( 'a' ) , ( el ) => {
114- el . addEventListener ( 'mousedown' , goExternal )
143+ el . addEventListener ( 'click' , this . anchorClickHandler )
144+ } )
145+ Array . prototype . forEach . call ( this . refs . root . contentWindow . document . querySelectorAll ( 'input[type="checkbox"]' ) , ( el ) => {
146+ el . addEventListener ( 'click' , this . checkboxClickHandler )
115147 } )
116148 }
117149
@@ -124,14 +156,14 @@ export default class MarkdownPreview extends React.Component {
124156 }
125157
126158 scrollTo ( targetRow ) {
127- let lineAnchors = this . getWindow ( ) . document . querySelectorAll ( 'a.lineAnchor ' )
159+ let blocks = this . getWindow ( ) . document . querySelectorAll ( 'body>[data-line] ' )
128160
129- for ( let index = 0 ; index < lineAnchors . length ; index ++ ) {
130- let lineAnchor = lineAnchors [ index ]
131- let row = parseInt ( lineAnchor . getAttribute ( 'data-key ' ) )
161+ for ( let index = 0 ; index < blocks . length ; index ++ ) {
162+ let block = blocks [ index ]
163+ let row = parseInt ( block . getAttribute ( 'data-line ' ) )
132164 if ( row > targetRow ) {
133- let targetAnchor = lineAnchors [ index - 1 ]
134- this . getWindow ( ) . scrollTo ( 0 , targetAnchor . offsetTop )
165+ let targetAnchor = blocks [ index - 1 ]
166+ targetAnchor != null && this . getWindow ( ) . scrollTo ( 0 , targetAnchor . offsetTop )
135167 break
136168 }
137169 }
@@ -147,6 +179,7 @@ export default class MarkdownPreview extends React.Component {
147179 style = { style }
148180 tabIndex = { tabIndex }
149181 ref = 'root'
182+ onClick = { ( e ) => this . handleClick ( e ) }
150183 />
151184 )
152185 }
0 commit comments