1
- import { patchDomFromHtmlString } from '../services/vdom.ts' ;
1
+ import { patchDomFromHtmlString } from '../services/vdom' ;
2
+ import { MarkdownEditor } from "./index.mjs" ;
2
3
3
4
export class Display {
5
+ protected editor : MarkdownEditor ;
6
+ protected container : HTMLIFrameElement ;
7
+ protected doc : Document | null = null ;
8
+ protected lastDisplayClick : number = 0 ;
4
9
5
- /**
6
- * @param {MarkdownEditor } editor
7
- */
8
- constructor ( editor ) {
10
+ constructor ( editor : MarkdownEditor ) {
9
11
this . editor = editor ;
10
12
this . container = editor . config . displayEl ;
11
13
12
- this . doc = null ;
13
- this . lastDisplayClick = 0 ;
14
-
15
- if ( this . container . contentDocument . readyState === 'complete' ) {
14
+ if ( this . container . contentDocument ?. readyState === 'complete' ) {
16
15
this . onLoad ( ) ;
17
16
} else {
18
17
this . container . addEventListener ( 'load' , this . onLoad . bind ( this ) ) ;
19
18
}
20
19
21
- this . updateVisibility ( editor . settings . get ( 'showPreview' ) ) ;
22
- editor . settings . onChange ( 'showPreview' , show => this . updateVisibility ( show ) ) ;
20
+ this . updateVisibility ( Boolean ( editor . settings . get ( 'showPreview' ) ) ) ;
21
+ editor . settings . onChange ( 'showPreview' , ( show ) => this . updateVisibility ( Boolean ( show ) ) ) ;
23
22
}
24
23
25
- updateVisibility ( show ) {
26
- const wrap = this . container . closest ( '.markdown-editor-wrap' ) ;
27
- wrap . style . display = show ? null : 'none' ;
24
+ protected updateVisibility ( show : boolean ) : void {
25
+ const wrap = this . container . closest ( '.markdown-editor-wrap' ) as HTMLElement ;
26
+ wrap . style . display = show ? '' : 'none' ;
28
27
}
29
28
30
- onLoad ( ) {
29
+ protected onLoad ( ) : void {
31
30
this . doc = this . container . contentDocument ;
32
31
32
+ if ( ! this . doc ) return ;
33
+
33
34
this . loadStylesIntoDisplay ( ) ;
34
35
this . doc . body . className = 'page-content' ;
35
36
36
37
// Prevent markdown display link click redirect
37
38
this . doc . addEventListener ( 'click' , this . onDisplayClick . bind ( this ) ) ;
38
39
}
39
40
40
- /**
41
- * @param {MouseEvent } event
42
- */
43
- onDisplayClick ( event ) {
41
+ protected onDisplayClick ( event : MouseEvent ) : void {
44
42
const isDblClick = Date . now ( ) - this . lastDisplayClick < 300 ;
45
43
46
- const link = event . target . closest ( 'a' ) ;
44
+ const link = ( event . target as Element ) . closest ( 'a' ) ;
47
45
if ( link !== null ) {
48
46
event . preventDefault ( ) ;
49
- window . open ( link . getAttribute ( 'href' ) ) ;
47
+ const href = link . getAttribute ( 'href' ) ;
48
+ if ( href ) {
49
+ window . open ( href ) ;
50
+ }
50
51
return ;
51
52
}
52
53
53
- const drawing = event . target . closest ( '[drawio-diagram]' ) ;
54
+ const drawing = ( event . target as Element ) . closest ( '[drawio-diagram]' ) as HTMLElement ;
54
55
if ( drawing !== null && isDblClick ) {
55
56
this . editor . actions . editDrawing ( drawing ) ;
56
57
return ;
@@ -59,10 +60,12 @@ export class Display {
59
60
this . lastDisplayClick = Date . now ( ) ;
60
61
}
61
62
62
- loadStylesIntoDisplay ( ) {
63
+ protected loadStylesIntoDisplay ( ) : void {
64
+ if ( ! this . doc ) return ;
65
+
63
66
this . doc . documentElement . classList . add ( 'markdown-editor-display' ) ;
64
67
65
- // Set display to be dark mode if parent is
68
+ // Set display to be dark mode if the parent is
66
69
if ( document . documentElement . classList . contains ( 'dark-mode' ) ) {
67
70
this . doc . documentElement . style . backgroundColor = '#222' ;
68
71
this . doc . documentElement . classList . add ( 'dark-mode' ) ;
@@ -71,39 +74,42 @@ export class Display {
71
74
this . doc . head . innerHTML = '' ;
72
75
const styles = document . head . querySelectorAll ( 'style,link[rel=stylesheet]' ) ;
73
76
for ( const style of styles ) {
74
- const copy = style . cloneNode ( true ) ;
77
+ const copy = style . cloneNode ( true ) as HTMLElement ;
75
78
this . doc . head . appendChild ( copy ) ;
76
79
}
77
80
}
78
81
79
82
/**
80
83
* Patch the display DOM with the given HTML content.
81
- * @param {String } html
82
84
*/
83
- patchWithHtml ( html ) {
84
- const { body} = this . doc ;
85
+ public patchWithHtml ( html : string ) : void {
86
+ if ( ! this . doc ) return ;
87
+
88
+ const { body } = this . doc ;
85
89
86
90
if ( body . children . length === 0 ) {
87
91
const wrap = document . createElement ( 'div' ) ;
88
92
this . doc . body . append ( wrap ) ;
89
93
}
90
94
91
- const target = body . children [ 0 ] ;
95
+ const target = body . children [ 0 ] as HTMLElement ;
92
96
93
97
patchDomFromHtmlString ( target , html ) ;
94
98
}
95
99
96
100
/**
97
101
* Scroll to the given block index within the display content.
98
102
* Will scroll to the end if the index is -1.
99
- * @param {Number } index
100
103
*/
101
- scrollToIndex ( index ) {
102
- const elems = this . doc . body ?. children [ 0 ] ?. children ;
103
- if ( elems && elems . length <= index ) return ;
104
+ public scrollToIndex ( index : number ) : void {
105
+ const elems = this . doc ? .body ?. children [ 0 ] ?. children ;
106
+ if ( ! elems || elems . length <= index ) return ;
104
107
105
108
const topElem = ( index === - 1 ) ? elems [ elems . length - 1 ] : elems [ index ] ;
106
- topElem . scrollIntoView ( { block : 'start' , inline : 'nearest' , behavior : 'smooth' } ) ;
109
+ ( topElem as Element ) . scrollIntoView ( {
110
+ block : 'start' ,
111
+ inline : 'nearest' ,
112
+ behavior : 'smooth'
113
+ } ) ;
107
114
}
108
-
109
- }
115
+ }
0 commit comments