Skip to content

Commit f83957a

Browse files
authored
744 reinvestigate iframe (#745)
* rewrite iframe to html * unpack html and append to the div; this way also enable scripts
1 parent 88dc168 commit f83957a

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, {useEffect, useRef, useState} from "react";
2+
import {downloadVisData, fileDownloaded,} from "../../../utils/visualization";
3+
import {readTextFromFile} from "../../../utils/common";
4+
5+
type htmlProps = {
6+
fileId?: string;
7+
visualizationId?: string;
8+
};
9+
10+
export default function Html(props: htmlProps) {
11+
const {fileId, visualizationId} = props;
12+
const divRef = useRef(null);
13+
const isFirstRender = useRef(true);
14+
15+
const [html, setHtml] = useState();
16+
17+
useEffect(() => {
18+
const processBlob = async () => {
19+
try {
20+
let blob;
21+
if (visualizationId) {
22+
blob = await downloadVisData(visualizationId);
23+
} else {
24+
blob = await fileDownloaded(fileId, 0);
25+
}
26+
const file = new File([blob], "text.tmp");
27+
const text = await readTextFromFile(file);
28+
setHtml(text);
29+
} catch (error) {
30+
console.error("Error fetching data:", error);
31+
}
32+
};
33+
34+
processBlob();
35+
}, [visualizationId, fileId]);
36+
37+
useEffect(() => {
38+
if (html && divRef.current) {
39+
const slotHtml = document.createRange().createContextualFragment(html);
40+
divRef.current.innerHTML = ""; // Clear the container
41+
divRef.current.appendChild(slotHtml); // Append the new content
42+
}
43+
44+
if (!isFirstRender.current) return;
45+
isFirstRender.current = false;
46+
}, [html, divRef])
47+
48+
return (<div ref={divRef} style={{width: "auto", maxHeight: "100vh", overflow: "auto"}}/>);
49+
}

frontend/src/components/visualizations/Iframe/manifest.json renamed to frontend/src/components/visualizations/Html/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"name": "basic-iframe-component",
33
"version": "1.0.0",
44
"description": "A React component for html",
5-
"main": "Iframe.tsx",
5+
"main": "Html.tsx",
66
"dependencies": {
77
"clowder2-core": "1.0.0",
88
"react": "^17.0.2",
99
"react-dom": "^17.0.2"
1010
},
1111
"visConfig": {
12-
"name": "Iframe",
12+
"name": "Html",
1313
"mainType": "text",
1414
"mimeTypes": [
1515
"text/html"

frontend/src/components/visualizations/Iframe/Iframe.tsx

Lines changed: 0 additions & 40 deletions
This file was deleted.

frontend/src/visualization.config.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { lazy } from "react";
1+
import React, {lazy} from "react";
22

33
export interface VisComponentDefinitions {
44
name: string;
@@ -9,14 +9,14 @@ export interface VisComponentDefinitions {
99

1010
const visComponentDefinitions: VisComponentDefinitions[] = <
1111
VisComponentDefinitions[]
12-
>[];
12+
>[];
1313

1414
function registerComponent(config) {
1515
return lazy(
1616
() =>
1717
import(
1818
/* webpackChunkName: "[request]" */ `./components/visualizations/${config.visConfig.name}/${config.main}`
19-
)
19+
)
2020
);
2121
}
2222

@@ -36,12 +36,12 @@ visComponentDefinitions.push({
3636
// component: React.createElement(registerComponent(configDemo)),
3737
// });
3838

39-
const configIframe = require("./components/visualizations/Iframe/manifest.json");
39+
const configHtml = require("./components/visualizations/Html/manifest.json");
4040
visComponentDefinitions.push({
41-
name: configIframe.name,
42-
mainType: configIframe.visConfig.mainType,
43-
mimeTypes: configIframe.visConfig.mimeTypes,
44-
component: React.createElement(registerComponent(configIframe)),
41+
name: configHtml.name,
42+
mainType: configHtml.visConfig.mainType,
43+
mimeTypes: configHtml.visConfig.mimeTypes,
44+
component: React.createElement(registerComponent(configHtml)),
4545
});
4646

4747
const configImage = require("./components/visualizations/Image/manifest.json");
@@ -84,4 +84,4 @@ visComponentDefinitions.push({
8484
component: React.createElement(registerComponent(configVega)),
8585
});
8686

87-
export { visComponentDefinitions };
87+
export {visComponentDefinitions};

0 commit comments

Comments
 (0)