@@ -17,30 +17,22 @@ class GistProcessor {
1717 constructor ( ) {
1818 }
1919
20- injectContainerHeightAdjustmentScript = ( ) => {
21- const containerHeightAdjustmentScript = document . createElement ( 'script' )
22- containerHeightAdjustmentScript . id = `${ pluginName } -container-height-adjustment`
23- containerHeightAdjustmentScript . textContent = `
24- window.addEventListener("message", (messageEvent) => {
25- const sender = messageEvent.data.sender
26-
27- if (messageEvent.origin !== 'null') {
28- // a message received from the iFrame with \`srcdoc\` attribute, the \`origin\` will be \`null\`.
29- return;
30- }
20+ messageEventHandler = ( messageEvent : MessageEvent ) => {
21+ if ( messageEvent . origin !== 'null' ) {
22+ // a message received from the iFrame with `srcdoc` attribute, the `origin` will be `null`.
23+ return ;
24+ }
3125
32- // only process message coming from this plugin
33- if (sender === '${ pluginName } ') {
34- const gistUUID = messageEvent.data.gistUUID
35- const contentHeight = messageEvent.data.contentHeight
26+ const sender = messageEvent . data . sender
3627
37- const gistContainer = document.querySelector('iframe#' + gistUUID)
38- gistContainer.height = contentHeight
39- }
40- }, false)
41- `
28+ // only process message coming from this plugin
29+ if ( sender === pluginName ) {
30+ const gistUUID = messageEvent . data . gistUUID
31+ const contentHeight = messageEvent . data . contentHeight
4232
43- document . head . appendChild ( containerHeightAdjustmentScript )
33+ const gistContainer : HTMLElement = document . querySelector ( 'iframe#' + gistUUID )
34+ gistContainer . setAttribute ( 'height' , contentHeight )
35+ }
4436 }
4537
4638 processor = async ( sourceString : string , el : HTMLElement ) => {
@@ -55,7 +47,7 @@ class GistProcessor {
5547
5648 // private
5749
58- async _processGist ( el : HTMLElement , gistString : string ) {
50+ private async _processGist ( el : HTMLElement , gistString : string ) {
5951 const pattern = / (?< protocol > h t t p s ? : \/ \/ ) ? (?< host > g i s t \. g i t h u b \. c o m \/ ) ? ( (?< username > \w + ) \/ ) ? (?< gistID > \w + ) ( \# (?< filename > .+ ) ) ? /
6052
6153 const matchResult = gistString . match ( pattern ) . groups
@@ -86,7 +78,7 @@ class GistProcessor {
8678 }
8779 }
8880
89- async _insertGistElement ( el : HTMLElement , gistID : string , gistJSON : GistJSON ) {
81+ private async _insertGistElement ( el : HTMLElement , gistID : string , gistJSON : GistJSON ) {
9082 // generate an uuid for each gist element
9183 const gistUUID = `${ pluginName } -${ gistID } -${ nanoid ( ) } `
9284
@@ -159,7 +151,7 @@ class GistProcessor {
159151 el . appendChild ( container )
160152 }
161153
162- async _showError ( el : HTMLElement , gistIDAndFilename : String , errorMessage : String = '' ) {
154+ private async _showError ( el : HTMLElement , gistIDAndFilename : String , errorMessage : String = '' ) {
163155 const errorText = `
164156Failed to load the Gist (${ gistIDAndFilename } ).
165157
@@ -170,8 +162,6 @@ Error:
170162
171163 el . createEl ( 'pre' , { text : errorText } )
172164 }
173-
174-
175165}
176166
177- export { GistProcessor }
167+ export { GistProcessor }
0 commit comments