1- import { patchDomFromHtmlString } from '../services/vdom.ts' ;
1+ import { patchDomFromHtmlString } from '../services/vdom' ;
2+ import { MarkdownEditor } from "./index.mjs" ;
23
34export class Display {
5+ protected editor : MarkdownEditor ;
6+ protected container : HTMLIFrameElement ;
7+ protected doc : Document | null = null ;
8+ protected lastDisplayClick : number = 0 ;
49
5- /**
6- * @param {MarkdownEditor } editor
7- */
8- constructor ( editor ) {
10+ constructor ( editor : MarkdownEditor ) {
911 this . editor = editor ;
1012 this . container = editor . config . displayEl ;
1113
12- this . doc = null ;
13- this . lastDisplayClick = 0 ;
14-
15- if ( this . container . contentDocument . readyState === 'complete' ) {
14+ if ( this . container . contentDocument ?. readyState === 'complete' ) {
1615 this . onLoad ( ) ;
1716 } else {
1817 this . container . addEventListener ( 'load' , this . onLoad . bind ( this ) ) ;
1918 }
2019
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 ) ) ) ;
2322 }
2423
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' ;
2827 }
2928
30- onLoad ( ) {
29+ protected onLoad ( ) : void {
3130 this . doc = this . container . contentDocument ;
3231
32+ if ( ! this . doc ) return ;
33+
3334 this . loadStylesIntoDisplay ( ) ;
3435 this . doc . body . className = 'page-content' ;
3536
3637 // Prevent markdown display link click redirect
3738 this . doc . addEventListener ( 'click' , this . onDisplayClick . bind ( this ) ) ;
3839 }
3940
40- /**
41- * @param {MouseEvent } event
42- */
43- onDisplayClick ( event ) {
41+ protected onDisplayClick ( event : MouseEvent ) : void {
4442 const isDblClick = Date . now ( ) - this . lastDisplayClick < 300 ;
4543
46- const link = event . target . closest ( 'a' ) ;
44+ const link = ( event . target as Element ) . closest ( 'a' ) ;
4745 if ( link !== null ) {
4846 event . preventDefault ( ) ;
49- window . open ( link . getAttribute ( 'href' ) ) ;
47+ const href = link . getAttribute ( 'href' ) ;
48+ if ( href ) {
49+ window . open ( href ) ;
50+ }
5051 return ;
5152 }
5253
53- const drawing = event . target . closest ( '[drawio-diagram]' ) ;
54+ const drawing = ( event . target as Element ) . closest ( '[drawio-diagram]' ) as HTMLElement ;
5455 if ( drawing !== null && isDblClick ) {
5556 this . editor . actions . editDrawing ( drawing ) ;
5657 return ;
@@ -59,10 +60,12 @@ export class Display {
5960 this . lastDisplayClick = Date . now ( ) ;
6061 }
6162
62- loadStylesIntoDisplay ( ) {
63+ protected loadStylesIntoDisplay ( ) : void {
64+ if ( ! this . doc ) return ;
65+
6366 this . doc . documentElement . classList . add ( 'markdown-editor-display' ) ;
6467
65- // Set display to be dark mode if parent is
68+ // Set display to be dark mode if the parent is
6669 if ( document . documentElement . classList . contains ( 'dark-mode' ) ) {
6770 this . doc . documentElement . style . backgroundColor = '#222' ;
6871 this . doc . documentElement . classList . add ( 'dark-mode' ) ;
@@ -71,39 +74,42 @@ export class Display {
7174 this . doc . head . innerHTML = '' ;
7275 const styles = document . head . querySelectorAll ( 'style,link[rel=stylesheet]' ) ;
7376 for ( const style of styles ) {
74- const copy = style . cloneNode ( true ) ;
77+ const copy = style . cloneNode ( true ) as HTMLElement ;
7578 this . doc . head . appendChild ( copy ) ;
7679 }
7780 }
7881
7982 /**
8083 * Patch the display DOM with the given HTML content.
81- * @param {String } html
8284 */
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 ;
8589
8690 if ( body . children . length === 0 ) {
8791 const wrap = document . createElement ( 'div' ) ;
8892 this . doc . body . append ( wrap ) ;
8993 }
9094
91- const target = body . children [ 0 ] ;
95+ const target = body . children [ 0 ] as HTMLElement ;
9296
9397 patchDomFromHtmlString ( target , html ) ;
9498 }
9599
96100 /**
97101 * Scroll to the given block index within the display content.
98102 * Will scroll to the end if the index is -1.
99- * @param {Number } index
100103 */
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 ;
104107
105108 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+ } ) ;
107114 }
108-
109- }
115+ }
0 commit comments