Skip to content

Commit 90f8f1e

Browse files
committed
Use Obsidian's registerDomEvent to inject the message handler function.
1 parent 2b895e0 commit 90f8f1e

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

src/gist_processor.ts

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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>https?:\/\/)?(?<host>gist\.github\.com\/)?((?<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 = `
164156
Failed 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 }

src/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Plugin } from 'obsidian';
2-
import { nanoid } from 'nanoid';
32
import { GistProcessor } from './gist_processor';
43

54
export default class GistPlugin extends Plugin {
65
async onload() {
76
const gistProcessor = new GistProcessor()
87

9-
gistProcessor.injectContainerHeightAdjustmentScript()
8+
this.registerDomEvent(window, "message", gistProcessor.messageEventHandler)
109

1110
this.registerMarkdownCodeBlockProcessor("gist", gistProcessor.processor)
1211
}

0 commit comments

Comments
 (0)