|
|
@ -1,8 +1,5 @@
|
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
import { isValidStr } from '@capital/common';
|
|
|
|
|
|
|
|
import { Translate } from '../translate';
|
|
|
|
import { Translate } from '../translate';
|
|
|
|
import { sanitize } from 'script_sanitize';
|
|
|
|
|
|
|
|
import { WebviewKeepAlive } from '@capital/component';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getInjectedStyle() {
|
|
|
|
function getInjectedStyle() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -17,51 +14,27 @@ function getInjectedStyle() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const cacheMap = new Map<string, string>();
|
|
|
|
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
|
|
|
|
|
|
|
|
const panelInfo = props.panelInfo;
|
|
|
|
function createInlineUrl(html: string): string | undefined {
|
|
|
|
const ref = useRef<HTMLIFrameElement>(null);
|
|
|
|
if (cacheMap.has(html)) {
|
|
|
|
const html = panelInfo?.meta?.html;
|
|
|
|
return cacheMap.get(html);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isValidStr(html)) {
|
|
|
|
|
|
|
|
const appendHtml = getInjectedStyle(); // 额外追加的样式
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const blob = new Blob(
|
|
|
|
|
|
|
|
[
|
|
|
|
|
|
|
|
sanitize(html + appendHtml, {
|
|
|
|
|
|
|
|
replacementText: 'No allowed script',
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
type: 'text/html',
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
useEffect(() => {
|
|
|
|
cacheMap.set(html, url);
|
|
|
|
if (!ref.current || !html) {
|
|
|
|
return url;
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
|
|
|
|
const doc = ref.current.contentWindow.document;
|
|
|
|
const panelInfo = props.panelInfo;
|
|
|
|
doc.open();
|
|
|
|
|
|
|
|
doc.writeln(getInjectedStyle(), html);
|
|
|
|
|
|
|
|
doc.close();
|
|
|
|
|
|
|
|
}, [html]);
|
|
|
|
|
|
|
|
|
|
|
|
if (!panelInfo) {
|
|
|
|
if (!panelInfo) {
|
|
|
|
return <div>{Translate.notfound}</div>;
|
|
|
|
return <div>{Translate.notfound}</div>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const html = panelInfo?.meta?.html;
|
|
|
|
return <iframe ref={ref} />;
|
|
|
|
const url = useMemo(() => {
|
|
|
|
|
|
|
|
return createInlineUrl(html);
|
|
|
|
|
|
|
|
}, [html]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return <WebviewKeepAlive className="w-full h-full" url={url} />;
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';
|
|
|
|
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';
|
|
|
|
|
|
|
|
|
|
|
|