refactor: 拆分webview缓存与非缓存

并迁移部分插件iframe到Webview组件
pull/64/head
moonrailgun 2 years ago
parent 1326b9dd05
commit 62c7edfd6b

@ -1,4 +1,5 @@
import { regCustomPanel } from '@capital/common'; import { regCustomPanel } from '@capital/common';
import { WebviewKeepAlive } from '@capital/component';
import React from 'react'; import React from 'react';
import { Translate } from './translate'; import { Translate } from './translate';
@ -10,6 +11,9 @@ regCustomPanel({
label: Translate.panelName, label: Translate.panelName,
icon: 'mdi:radio-tower', icon: 'mdi:radio-tower',
render: () => ( render: () => (
<iframe className="w-full h-full bg-white" src="https://snapdrop.net/" /> <WebviewKeepAlive
className="w-full h-full bg-white"
url="https://snapdrop.net/"
/>
), ),
}); });

@ -3,6 +3,7 @@ import { encode } from 'js-base64';
import { isValidStr } from '@capital/common'; import { isValidStr } from '@capital/common';
import { Translate } from '../translate'; import { Translate } from '../translate';
import { sanitize } from 'script_sanitize'; import { sanitize } from 'script_sanitize';
import { WebviewKeepAlive } from '@capital/component';
function getInjectedStyle() { function getInjectedStyle() {
try { try {
@ -25,7 +26,7 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
} }
const html = panelInfo?.meta?.html; const html = panelInfo?.meta?.html;
const src = useMemo(() => { const url = useMemo(() => {
if (isValidStr(html)) { if (isValidStr(html)) {
const appendHtml = getInjectedStyle(); // 额外追加的样式 const appendHtml = getInjectedStyle(); // 额外追加的样式
try { try {
@ -40,7 +41,7 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
} }
}, [html]); }, [html]);
return <iframe className="w-full h-full" src={src} />; return <WebviewKeepAlive className="w-full h-full" url={url} />;
}; };
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender'; GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { Translate } from '../translate'; import { Translate } from '../translate';
import { Webview } from '@capital/component'; import { WebviewKeepAlive } from '@capital/component';
const GroupWebPanelRender: React.FC<{ panelInfo: any }> = (props) => { const GroupWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
const panelInfo = props.panelInfo; const panelInfo = props.panelInfo;
@ -11,7 +11,9 @@ const GroupWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
const url = panelInfo?.meta?.url; const url = panelInfo?.meta?.url;
return <Webview key={String(url)} className="w-full h-full" url={url} />; return (
<WebviewKeepAlive key={String(url)} className="w-full h-full" url={url} />
);
}; };
GroupWebPanelRender.displayName = 'GroupWebPanelRender'; GroupWebPanelRender.displayName = 'GroupWebPanelRender';

@ -10,11 +10,17 @@ interface WebviewProps {
/** /**
* *
*/ */
export const Webview: React.FC<WebviewProps> = export const Webview: React.FC<WebviewProps> = (props) => {
withKeepAliveOverlay<WebviewProps>( return <iframe className="w-full h-full" src={props.url} />;
(props) => { };
return <iframe className="w-full h-full bg-white" src={props.url} />;
},
{ cacheId: (props) => props.url }
);
Webview.displayName = 'Webview'; Webview.displayName = 'Webview';
/**
*
* case
*/
export const WebviewKeepAlive: React.FC<WebviewProps> =
withKeepAliveOverlay<WebviewProps>(Webview, {
cacheId: (props) => props.url,
});
WebviewKeepAlive.displayName = 'WebviewKeepAlive';

@ -56,4 +56,4 @@ export { ErrorBoundary } from '@/components/ErrorBoundary';
export { UserAvatar } from '@/components/UserAvatar'; export { UserAvatar } from '@/components/UserAvatar';
export { UserName } from '@/components/UserName'; export { UserName } from '@/components/UserName';
export { Markdown } from '@/components/Markdown'; export { Markdown } from '@/components/Markdown';
export { Webview } from '@/components/Webview'; export { Webview, WebviewKeepAlive } from '@/components/Webview';

Loading…
Cancel
Save