@@ -21,6 +21,7 @@ import yaml from 'js-yaml'
21
21
import { render } from 'react-dom'
22
22
import Carousel from 'react-image-carousel'
23
23
import ConfigManager from '../main/lib/ConfigManager'
24
+ import i18n from 'browser/lib/i18n'
24
25
25
26
const { remote, shell } = require ( 'electron' )
26
27
const attachmentManagement = require ( '../main/lib/dataApi/attachmentManagement' )
@@ -50,7 +51,6 @@ const CSS_FILES = [
50
51
* @param {String } opts.theme
51
52
* @param {Boolean } [opts.lineNumber] Should show line number
52
53
* @param {Boolean } [opts.scrollPastEnd]
53
- * @param {Boolean } [opts.optimizeOverflowScroll] Should tweak body style to optimize overflow scrollbar display
54
54
* @param {Boolean } [opts.allowCustomCSS] Should add custom css
55
55
* @param {String } [opts.customCSS] Will be added to bottom, only if `opts.allowCustomCSS` is truthy
56
56
* @returns {String }
@@ -62,7 +62,6 @@ function buildStyle (opts) {
62
62
codeBlockFontFamily,
63
63
lineNumber,
64
64
scrollPastEnd,
65
- optimizeOverflowScroll,
66
65
theme,
67
66
allowCustomCSS,
68
67
customCSS,
@@ -103,7 +102,12 @@ ${markdownStyle}
103
102
body {
104
103
font-family: '${ fontFamily . join ( "','" ) } ';
105
104
font-size: ${ fontSize } px;
106
- ${ scrollPastEnd ? 'padding-bottom: 90vh;' : '' }
105
+
106
+ ${ scrollPastEnd ? `
107
+ padding-bottom: 90vh;
108
+ box-sizing: border-box;
109
+ `
110
+ : '' }
107
111
${ optimizeOverflowScroll ? 'height: 100%;' : '' }
108
112
${ RTL ? 'direction: rtl;' : '' }
109
113
${ RTL ? 'text-align: right;' : '' }
@@ -184,6 +188,10 @@ const scrollBarStyle = `
184
188
::-webkit-scrollbar-thumb {
185
189
background-color: rgba(0, 0, 0, 0.15);
186
190
}
191
+
192
+ ::-webkit-scrollbar-track-piece {
193
+ background-color: inherit;
194
+ }
187
195
`
188
196
const scrollBarDarkStyle = `
189
197
::-webkit-scrollbar {
@@ -193,6 +201,10 @@ const scrollBarDarkStyle = `
193
201
::-webkit-scrollbar-thumb {
194
202
background-color: rgba(0, 0, 0, 0.3);
195
203
}
204
+
205
+ ::-webkit-scrollbar-track-piece {
206
+ background-color: inherit;
207
+ }
196
208
`
197
209
198
210
const OSX = global . process . platform === 'darwin'
@@ -241,6 +253,7 @@ export default class MarkdownPreview extends React.Component {
241
253
this . saveAsHtmlHandler = ( ) => this . handleSaveAsHtml ( )
242
254
this . saveAsPdfHandler = ( ) => this . handleSaveAsPdf ( )
243
255
this . printHandler = ( ) => this . handlePrint ( )
256
+ this . resizeHandler = _ . throttle ( this . handleResize . bind ( this ) , 100 )
244
257
245
258
this . linkClickHandler = this . handleLinkClick . bind ( this )
246
259
this . initMarkdown = this . initMarkdown . bind ( this )
@@ -346,7 +359,7 @@ export default class MarkdownPreview extends React.Component {
346
359
customCSS,
347
360
RTL
348
361
} )
349
- let body = this . markdown . render ( noteContent )
362
+ let body = this . refs . root . contentWindow . document . body . innerHTML
350
363
body = attachmentManagement . fixLocalURLS (
351
364
body ,
352
365
this . props . storagePath
@@ -366,7 +379,7 @@ export default class MarkdownPreview extends React.Component {
366
379
367
380
let styles = ''
368
381
files . forEach ( file => {
369
- styles += `<link rel="stylesheet" href="css/${ path . basename ( file ) } ">`
382
+ styles += `<link rel="stylesheet" href="../ css/${ path . basename ( file ) } ">`
370
383
} )
371
384
372
385
return `<html>
@@ -421,7 +434,8 @@ export default class MarkdownPreview extends React.Component {
421
434
. then ( res => {
422
435
dialog . showMessageBox ( remote . getCurrentWindow ( ) , {
423
436
type : 'info' ,
424
- message : `Exported to ${ filename } `
437
+ message : `Exported to ${ filename } ` ,
438
+ buttons : [ i18n . __ ( 'Ok' ) ]
425
439
} )
426
440
} )
427
441
. catch ( err => {
@@ -536,6 +550,10 @@ export default class MarkdownPreview extends React.Component {
536
550
'scroll' ,
537
551
this . scrollHandler
538
552
)
553
+ this . refs . root . contentWindow . addEventListener (
554
+ 'resize' ,
555
+ this . resizeHandler
556
+ )
539
557
eventEmitter . on ( 'export:save-text' , this . saveAsTextHandler )
540
558
eventEmitter . on ( 'export:save-md' , this . saveAsMdHandler )
541
559
eventEmitter . on ( 'export:save-html' , this . saveAsHtmlHandler )
@@ -574,6 +592,10 @@ export default class MarkdownPreview extends React.Component {
574
592
'scroll' ,
575
593
this . scrollHandler
576
594
)
595
+ this . refs . root . contentWindow . removeEventListener (
596
+ 'resize' ,
597
+ this . resizeHandler
598
+ )
577
599
eventEmitter . off ( 'export:save-text' , this . saveAsTextHandler )
578
600
eventEmitter . off ( 'export:save-md' , this . saveAsMdHandler )
579
601
eventEmitter . off ( 'export:save-html' , this . saveAsHtmlHandler )
@@ -619,7 +641,7 @@ export default class MarkdownPreview extends React.Component {
619
641
620
642
// Should scroll to top after selecting another note
621
643
if ( prevProps . noteKey !== this . props . noteKey ) {
622
- this . getWindow ( ) . scrollTo ( 0 , 0 )
644
+ this . scrollTo ( 0 , 0 )
623
645
}
624
646
}
625
647
@@ -686,13 +708,11 @@ export default class MarkdownPreview extends React.Component {
686
708
codeBlockFontFamily,
687
709
lineNumber,
688
710
scrollPastEnd,
689
- optimizeOverflowScroll : true ,
690
711
theme,
691
712
allowCustomCSS,
692
713
customCSS,
693
714
RTL
694
715
} )
695
- this . getWindow ( ) . document . documentElement . style . overflowY = 'hidden'
696
716
}
697
717
698
718
getCodeThemeLink ( name ) {
@@ -1000,6 +1020,15 @@ export default class MarkdownPreview extends React.Component {
1000
1020
}
1001
1021
}
1002
1022
1023
+ handleResize ( ) {
1024
+ _ . forEach (
1025
+ this . refs . root . contentWindow . document . querySelectorAll ( 'svg[ratio]' ) ,
1026
+ el => {
1027
+ el . setAttribute ( 'height' , el . clientWidth / el . getAttribute ( 'ratio' ) )
1028
+ }
1029
+ )
1030
+ }
1031
+
1003
1032
focus ( ) {
1004
1033
this . refs . root . focus ( )
1005
1034
}
@@ -1008,7 +1037,11 @@ export default class MarkdownPreview extends React.Component {
1008
1037
return this . refs . root . contentWindow
1009
1038
}
1010
1039
1011
- scrollTo ( targetRow ) {
1040
+ /**
1041
+ * @public
1042
+ * @param {Number } targetRow
1043
+ */
1044
+ scrollToRow ( targetRow ) {
1012
1045
const blocks = this . getWindow ( ) . document . querySelectorAll (
1013
1046
'body>[data-line]'
1014
1047
)
@@ -1018,12 +1051,21 @@ export default class MarkdownPreview extends React.Component {
1018
1051
const row = parseInt ( block . getAttribute ( 'data-line' ) )
1019
1052
if ( row > targetRow || index === blocks . length - 1 ) {
1020
1053
block = blocks [ index - 1 ]
1021
- block != null && this . getWindow ( ) . scrollTo ( 0 , block . offsetTop )
1054
+ block != null && this . scrollTo ( 0 , block . offsetTop )
1022
1055
break
1023
1056
}
1024
1057
}
1025
1058
}
1026
1059
1060
+ /**
1061
+ * `document.body.scrollTo`
1062
+ * @param {Number } x
1063
+ * @param {Number } y
1064
+ */
1065
+ scrollTo ( x , y ) {
1066
+ this . getWindow ( ) . document . body . scrollTo ( x , y )
1067
+ }
1068
+
1027
1069
preventImageDroppedHandler ( e ) {
1028
1070
e . preventDefault ( )
1029
1071
e . stopPropagation ( )
@@ -1061,12 +1103,12 @@ export default class MarkdownPreview extends React.Component {
1061
1103
if ( posOfHash > - 1 ) {
1062
1104
const extractedId = linkHash . slice ( posOfHash + 1 )
1063
1105
const targetId = mdurl . encode ( extractedId )
1064
- const targetElement = this . refs . root . contentWindow . document . getElementById (
1106
+ const targetElement = this . getWindow ( ) . document . getElementById (
1065
1107
targetId
1066
1108
)
1067
1109
1068
1110
if ( targetElement != null ) {
1069
- this . getWindow ( ) . scrollTo ( 0 , targetElement . offsetTop )
1111
+ this . scrollTo ( 0 , targetElement . offsetTop )
1070
1112
}
1071
1113
return
1072
1114
}
0 commit comments