@@ -18,24 +18,20 @@ import mdurl from 'mdurl'
18
18
import exportNote from 'browser/main/lib/dataApi/exportNote'
19
19
import { escapeHtmlCharacters } from 'browser/lib/utils'
20
20
import yaml from 'js-yaml'
21
- import context from 'browser/lib/context'
22
- import i18n from 'browser/lib/i18n'
23
- import fs from 'fs'
24
21
import { render } from 'react-dom'
25
22
import Carousel from 'react-image-carousel'
26
23
import ConfigManager from '../main/lib/ConfigManager'
27
24
28
25
const { remote, shell } = require ( 'electron' )
29
26
const attachmentManagement = require ( '../main/lib/dataApi/attachmentManagement' )
27
+ const buildMarkdownPreviewContextMenu = require ( 'browser/lib/contextMenuBuilder' ) . buildMarkdownPreviewContextMenu
30
28
31
29
const { app } = remote
32
30
const path = require ( 'path' )
33
31
const fileUrl = require ( 'file-url' )
34
32
35
33
const dialog = remote . dialog
36
34
37
- const uri2path = require ( 'file-uri-to-path' )
38
-
39
35
const markdownStyle = require ( '!!css!stylus?sourceMap!./markdown.styl' ) [ 0 ] [ 1 ]
40
36
const appPath = fileUrl (
41
37
process . env . NODE_ENV === 'production' ? app . getAppPath ( ) : path . resolve ( )
@@ -45,7 +41,6 @@ const CSS_FILES = [
45
41
`${ appPath } /node_modules/codemirror/lib/codemirror.css` ,
46
42
`${ appPath } /node_modules/react-image-carousel/lib/css/main.min.css`
47
43
]
48
- const win = global . process . platform === 'win32'
49
44
50
45
/**
51
46
* @param {Object } opts
@@ -267,30 +262,12 @@ export default class MarkdownPreview extends React.Component {
267
262
}
268
263
269
264
handleContextMenu ( event ) {
270
- // If a contextMenu handler was passed to us, use it instead of the self-defined one -> return
271
- if ( _ . isFunction ( this . props . onContextMenu ) ) {
265
+ const menu = buildMarkdownPreviewContextMenu ( this , event )
266
+ const switchPreview = ConfigManager . get ( ) . editor . switchPreview
267
+ if ( menu != null && switchPreview !== 'RIGHTCLICK' ) {
268
+ menu . popup ( remote . getCurrentWindow ( ) )
269
+ } else if ( _ . isFunction ( this . props . onContextMenu ) ) {
272
270
this . props . onContextMenu ( event )
273
- return
274
- }
275
- // No contextMenu was passed to us -> execute our own link-opener
276
- if ( event . target . tagName . toLowerCase ( ) === 'a' && event . target . getAttribute ( 'href' ) ) {
277
- const href = event . target . href
278
- const isLocalFile = href . startsWith ( 'file:' )
279
- if ( isLocalFile ) {
280
- const absPath = uri2path ( href )
281
- try {
282
- if ( fs . lstatSync ( absPath ) . isFile ( ) ) {
283
- context . popup ( [
284
- {
285
- label : i18n . __ ( 'Show in explorer' ) ,
286
- click : ( e ) => shell . showItemInFolder ( absPath )
287
- }
288
- ] )
289
- }
290
- } catch ( e ) {
291
- console . log ( 'Error while evaluating if the file is locally available' , e )
292
- }
293
- }
294
271
}
295
272
}
296
273
@@ -367,7 +344,7 @@ export default class MarkdownPreview extends React.Component {
367
344
body ,
368
345
this . props . storagePath
369
346
)
370
- const files = [ this . GetCodeThemeLink ( codeBlockTheme ) , ...CSS_FILES ]
347
+ const files = [ this . getCodeThemeLink ( codeBlockTheme ) , ...CSS_FILES ]
371
348
files . forEach ( file => {
372
349
if ( global . process . platform === 'win32' ) {
373
350
file = file . replace ( 'file:///' , '' )
@@ -403,7 +380,7 @@ export default class MarkdownPreview extends React.Component {
403
380
404
381
handleSaveAsPdf ( ) {
405
382
this . exportAsDocument ( 'pdf' , ( noteContent , exportTasks , targetDir ) => {
406
- const printout = new remote . BrowserWindow ( { show : false , webPreferences : { webSecurity : false } } )
383
+ const printout = new remote . BrowserWindow ( { show : false , webPreferences : { webSecurity : false , javascript : false } } )
407
384
printout . loadURL ( 'data:text/html;charset=UTF-8,' + this . htmlContentFormatter ( noteContent , exportTasks , targetDir ) )
408
385
return new Promise ( ( resolve , reject ) => {
409
386
printout . webContents . on ( 'did-finish-load' , ( ) => {
@@ -598,16 +575,19 @@ export default class MarkdownPreview extends React.Component {
598
575
}
599
576
600
577
componentDidUpdate ( prevProps ) {
601
- if ( prevProps . value !== this . props . value ) this . rewriteIframe ( )
578
+ // actual rewriteIframe function should be called only once
579
+ let needsRewriteIframe = false
580
+ if ( prevProps . value !== this . props . value ) needsRewriteIframe = true
602
581
if (
603
582
prevProps . smartQuotes !== this . props . smartQuotes ||
604
583
prevProps . sanitize !== this . props . sanitize ||
584
+ prevProps . mermaidHTMLLabel !== this . props . mermaidHTMLLabel ||
605
585
prevProps . smartArrows !== this . props . smartArrows ||
606
586
prevProps . breaks !== this . props . breaks ||
607
587
prevProps . lineThroughCheckbox !== this . props . lineThroughCheckbox
608
588
) {
609
589
this . initMarkdown ( )
610
- this . rewriteIframe ( )
590
+ needsRewriteIframe = true
611
591
}
612
592
if (
613
593
prevProps . fontFamily !== this . props . fontFamily ||
@@ -622,8 +602,17 @@ export default class MarkdownPreview extends React.Component {
622
602
prevProps . customCSS !== this . props . customCSS
623
603
) {
624
604
this . applyStyle ( )
605
+ needsRewriteIframe = true
606
+ }
607
+
608
+ if ( needsRewriteIframe ) {
625
609
this . rewriteIframe ( )
626
610
}
611
+
612
+ // Should scroll to top after selecting another note
613
+ if ( prevProps . noteKey !== this . props . noteKey ) {
614
+ this . getWindow ( ) . scrollTo ( 0 , 0 )
615
+ }
627
616
}
628
617
629
618
getStyleParams ( ) {
@@ -679,8 +668,7 @@ export default class MarkdownPreview extends React.Component {
679
668
680
669
this . getWindow ( ) . document . getElementById (
681
670
'codeTheme'
682
- ) . href = this . GetCodeThemeLink ( codeBlockTheme )
683
-
671
+ ) . href = this . getCodeThemeLink ( codeBlockTheme )
684
672
this . getWindow ( ) . document . getElementById ( 'style' ) . innerHTML = buildStyle ( {
685
673
fontFamily,
686
674
fontSize,
@@ -695,11 +683,11 @@ export default class MarkdownPreview extends React.Component {
695
683
this . getWindow ( ) . document . documentElement . style . overflowY = 'hidden'
696
684
}
697
685
698
- GetCodeThemeLink ( name ) {
686
+ getCodeThemeLink ( name ) {
699
687
const theme = consts . THEMES . find ( theme => theme . name === name )
700
688
701
- return theme
702
- ? ( win ? theme . path : ` ${ appPath } / ${ theme . path } ` )
689
+ return theme != null
690
+ ? theme . path
703
691
: `${ appPath } /node_modules/codemirror/theme/elegant.css`
704
692
}
705
693
@@ -726,7 +714,8 @@ export default class MarkdownPreview extends React.Component {
726
714
showCopyNotification,
727
715
storagePath,
728
716
noteKey,
729
- sanitize
717
+ sanitize,
718
+ mermaidHTMLLabel
730
719
} = this . props
731
720
let { value, codeBlockTheme } = this . props
732
721
@@ -858,6 +847,7 @@ export default class MarkdownPreview extends React.Component {
858
847
canvas . height = height . value + 'vh'
859
848
}
860
849
850
+ // eslint-disable-next-line no-unused-vars
861
851
const chart = new Chart ( canvas , chartConfig )
862
852
} catch ( e ) {
863
853
el . className = 'chart-error'
@@ -868,7 +858,7 @@ export default class MarkdownPreview extends React.Component {
868
858
_ . forEach (
869
859
this . refs . root . contentWindow . document . querySelectorAll ( '.mermaid' ) ,
870
860
el => {
871
- mermaidRender ( el , htmlTextHelper . decodeEntities ( el . innerHTML ) , theme )
861
+ mermaidRender ( el , htmlTextHelper . decodeEntities ( el . innerHTML ) , theme , mermaidHTMLLabel )
872
862
}
873
863
)
874
864
@@ -996,8 +986,6 @@ export default class MarkdownPreview extends React.Component {
996
986
overlay . appendChild ( zoomImg )
997
987
document . body . appendChild ( overlay )
998
988
}
999
-
1000
- this . getWindow ( ) . scrollTo ( 0 , 0 )
1001
989
}
1002
990
1003
991
focus ( ) {
0 commit comments