|
1 | 1 | /* eslint-disable react-hooks/exhaustive-deps */ |
2 | | -import './HtmlRunner.css'; |
3 | | -import React, { useRef, useEffect } from 'react'; |
4 | | -import { useSelector } from 'react-redux' |
| 2 | +import "./HtmlRunner.scss"; |
| 3 | +import React, { useRef, useEffect } from "react"; |
| 4 | +import { useSelector } from "react-redux"; |
5 | 5 |
|
6 | 6 | function HtmlRunner() { |
7 | 7 | const projectCode = useSelector((state) => state.editor.project.components); |
8 | 8 | const output = useRef(); |
9 | 9 |
|
10 | 10 | const getBlobURL = (code, type) => { |
11 | | - const blob = new Blob([code], { type }) |
12 | | - return URL.createObjectURL(blob) |
13 | | - } |
| 11 | + const blob = new Blob([code], { type }); |
| 12 | + return URL.createObjectURL(blob); |
| 13 | + }; |
14 | 14 |
|
15 | 15 | useEffect(() => { |
16 | 16 | runCode(); |
17 | 17 | }, [projectCode]); |
18 | 18 |
|
19 | 19 | const runCode = () => { |
20 | 20 | // TODO: get html files and handle urls for non index pages |
21 | | - var indexPage = projectCode[0].content; |
22 | | - |
23 | | - var cssFiles = projectCode.filter(component => component.extension === 'css'); |
24 | | - cssFiles.forEach(cssFile => { |
25 | | - var cssFileBlob = getBlobURL(cssFile.content, 'text/css'); |
26 | | - indexPage = indexPage.replace(`href="${cssFile.name}.css"`, `href="${cssFileBlob}"`) |
| 21 | + let indexPage = projectCode[0].content; |
| 22 | + |
| 23 | + const cssFiles = projectCode.filter( |
| 24 | + (component) => component.extension === "css" |
| 25 | + ); |
| 26 | + cssFiles.forEach((cssFile) => { |
| 27 | + const cssFileBlob = getBlobURL(cssFile.content, "text/css"); |
| 28 | + indexPage = indexPage.replace( |
| 29 | + `href="${cssFile.name}.css"`, |
| 30 | + `href="${cssFileBlob}"` |
| 31 | + ); |
27 | 32 | }); |
28 | 33 |
|
29 | | - var blob = getBlobURL(indexPage, 'text/html'); |
| 34 | + const blob = getBlobURL(indexPage, "text/html"); |
30 | 35 | output.current.src = blob; |
31 | | - } |
| 36 | + }; |
32 | 37 |
|
33 | 38 | return ( |
34 | 39 | <div className="htmlrunner-container"> |
35 | | - <iframe className="htmlrunner-iframe" id="output-frame" title="html-output-frame" ref={output} /> |
| 40 | + <iframe |
| 41 | + className="htmlrunner-iframe" |
| 42 | + id="output-frame" |
| 43 | + title="html-output-frame" |
| 44 | + ref={output} |
| 45 | + /> |
36 | 46 | </div> |
37 | 47 | ); |
38 | 48 | } |
|
0 commit comments