33
44const ReactDOMServer = require ( 'react-dom/server' ) ;
55const etch = require ( 'etch' ) ;
6- const marked = require ( 'marked' ) ;
76const createDOMPurify = require ( 'dompurify' ) ;
87/**
98 * [domPurify description]
109 * @type {DOMPurify }
1110 */
1211const domPurify = createDOMPurify ( ) ;
1312
14- class SnippetView {
15- constructor ( { snippet, grammar } ) {
16- this . rootElement = document . createElement ( "div" ) ;
17- this . rootElement . className = "datatip-container" ;
18- if ( snippet ) {
19- this . refreshRootElement ( snippet , grammar ) ;
20- }
21- }
22-
23- update ( { snippet, grammar } ) {
24- this . refreshRootElement ( snippet , grammar ) ;
25- }
26-
27- refreshRootElement ( snippet , grammar ) {
28- const wrapper = document . createElement ( "div" ) ;
29- wrapper . className = "datatip-marked-text-editor" ;
30- if ( snippet ) {
31- const regExpLSPPrefix = / ^ \( ( m e t h o d | p r o p e r t y | p a r a m e t e r ) \) \W / ;
32- snippet = snippet . replace ( regExpLSPPrefix , '' ) ;
33- if ( grammar ) {
34- const atomElem = atom . workspace . buildTextEditor ( {
35- lineNumberGutterVisible : false ,
36- readonly : true ,
37- keyboardInputEnabled : false ,
38- showInvisibles : false ,
39- tabLength : atom . config . get ( "editor.tabLength" )
40- } ) ;
41- atomElem . setGrammar ( grammar ) ;
42- atomElem . setText ( snippet ) ;
43- const atomView = atom . views . getView ( atomElem ) ;
44- atomView . setUpdatedSynchronously ( true ) ;
45- atomView . style . pointerEvents = "none"
46- atomView . style . position = "absolute" ;
47- atomView . style . width = "0px" ;
48- atomView . style . height = "1px" ;
49- atom . views . getView ( atom . workspace ) . appendChild ( atomView ) ;
50- this . editorTokenized ( atomElem ) . then ( ( ) => {
51- const html = Array . from ( atomView . querySelectorAll ( ".line:not(.dummy)" ) ) ;
52- wrapper . innerHTML = domPurify . sanitize ( html . map ( x => x . innerHTML ) . join ( "\n" ) , { breaks : true } ) ;
53- atomView . remove ( ) ;
54- } ) . catch ( ( ) => {
55- atomView . remove ( ) ;
56- } )
57- }
58- }
59- this . rootElement . appendChild ( wrapper ) ;
60- }
61-
62- get element ( ) {
63- return this . rootElement ;
64- }
65-
66- /**
67- * [editorTokenized description]
68- * @param {TextEditor } editor [description]
69- * @return {Promise } [description]
70- */
71- editorTokenized ( editor ) {
72- return new Promise ( resolve => {
73- if ( editor . getBuffer ( ) . getLanguageMode ( ) . fullyTokenized ) {
74- resolve ( )
75- } else {
76- const disp = editor . onDidTokenize ( ( ) => {
77- disp . dispose ( )
78- resolve ( )
79- } )
80- }
81- } )
82- }
83- }
84-
85- class MarkdownView {
86- constructor ( { markedString } ) {
87- this . markedString = markedString ;
13+ class HtmlView {
14+ constructor ( { html } ) {
8815 this . rootElement = document . createElement ( 'div' ) ;
8916 this . rootElement . className = "datatip-marked-container" ;
9017 this . rootElement . addEventListener ( "wheel" , this . onMouseWheel , { passive : true } ) ;
91-
92- if ( this . markedString ) {
93- this . rootElement . innerHTML = domPurify . sanitize ( marked ( this . markedString , { breaks : true } ) ) ;
94- }
95- }
96- update ( { markedString } ) {
97- this . markedString = markedString ;
98- if ( this . markedString ) {
99- this . rootElement . innerHTML = domPurify . sanitize ( marked ( this . markedString , { breaks : true } ) ) ;
18+ if ( html ) {
19+ this . rootElement . innerHTML = domPurify . sanitize ( html ) ;
10020 }
10121 }
10222
@@ -113,7 +33,7 @@ class MarkdownView {
11333 }
11434}
11535
116- class ReactComponentView {
36+ class ReactView {
11737 constructor ( { component } ) {
11838 this . rootElement = document . createElement ( 'span' )
11939 if ( component ) {
@@ -134,24 +54,24 @@ module.exports = class DataTipView {
13454 }
13555
13656 render ( ) {
137- if ( this . properties . component ) {
57+ if ( this . properties . reactView ) {
58+ return (
59+ < div className = "datatip-element" >
60+ < ReactView component = { this . properties . reactView } />
61+ </ div >
62+ ) ;
63+ }
64+ else if ( this . properties . htmlView ) {
13865 return (
13966 < div className = "datatip-element" >
140- < ReactComponentView component = { this . properties . component } />
67+ < HtmlView html = { this . properties . htmlView } />
14168 </ div >
14269 ) ;
14370 }
14471 else {
14572 return (
14673 < div className = "datatip-element" >
147- { this . properties . markedStrings . map ( ( m , i ) => {
148- switch ( m . type ) {
149- case "snippet" :
150- return < SnippetView key = { i } snippet = { m . value } grammar = { m . grammar } /> ;
151- case "markdown" :
152- return < MarkdownView key = { i } markedString = { m . value } /> ;
153- }
154- } ) }
74+ { this . children }
15575 </ div >
15676 ) ;
15777 }
0 commit comments