다음과 같은 코드가 있습니다:
let iFrame = document.getElementById('oc-editor');
iFrame.addEventListener('load', function() {
iFrame.contentWindow.postMessage({
eventType: 'populateCode',
language: "{{this.codeLang}}",
files: [
{
"name": "file.{{this.codeLang}}",
"content": "{{this.codeLang}}"
}
]
}, "*");
});
이 코드는 DModal 내부의 iframe을 변경하여 코드의 언어와 내용을 포함하도록 합니다. 하지만 iFrame이 null이라고 알려주며 addEventListener를 추가하지 못합니다.
다음과 같이 시도해 보았습니다:
<template>
<DButton
@translatedLabel="Show Modal"
@action={{this.showModal}}
/>
{{#if this.modalIsVisible}}
<DModal @title="Code Compiler" @closeModal={{this.hideModal}}>
<p>Code: {{this.getCode}}</p>
<iframe
frameBorder="0"
height="450px"
src="https://onecompiler.com/embed/{{this.codeLang}}"
width="100%"
id="oc-editor"
title="OneCompiler Code Editor"
listenToEvents="true">
</iframe>
<script>
let iFrame = document.getElementById('oc-editor');
iFrame.addEventListener('load', function() {
iFrame.contentWindow.postMessage({
eventType: 'populateCode',
language: "{{this.codeLang}}",
files: [
{
"name": "file.{{this.codeLang}}",
"content": "{{this.codeLang}}"
}
]
}, "*");
});
</script>
</DModal>
{{/if}}
</template>
하지만 이것도 작동하지 않았습니다. 이를 수행할 수 있는 방법이 있을까요?
리포지토리 링크:
https://github.com/NateDhaliwal/Discourse-OneCompiler-Code-Runner