Skip to content

Commit 62c7edf

Browse files
committed
refactor: 拆分webview缓存与非缓存
并迁移部分插件iframe到Webview组件
1 parent 1326b9d commit 62c7edf

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

client/web/plugins/com.msgbyte.snapdrop/src/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { regCustomPanel } from '@capital/common';
2+
import { WebviewKeepAlive } from '@capital/component';
23
import React from 'react';
34
import { Translate } from './translate';
45

@@ -10,6 +11,9 @@ regCustomPanel({
1011
label: Translate.panelName,
1112
icon: 'mdi:radio-tower',
1213
render: () => (
13-
<iframe className="w-full h-full bg-white" src="https://snapdrop.net/" />
14+
<WebviewKeepAlive
15+
className="w-full h-full bg-white"
16+
url="https://snapdrop.net/"
17+
/>
1418
),
1519
});

client/web/plugins/com.msgbyte.webview/src/group/GroupCustomWebPanelRender.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { encode } from 'js-base64';
33
import { isValidStr } from '@capital/common';
44
import { Translate } from '../translate';
55
import { sanitize } from 'script_sanitize';
6+
import { WebviewKeepAlive } from '@capital/component';
67

78
function getInjectedStyle() {
89
try {
@@ -25,7 +26,7 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
2526
}
2627

2728
const html = panelInfo?.meta?.html;
28-
const src = useMemo(() => {
29+
const url = useMemo(() => {
2930
if (isValidStr(html)) {
3031
const appendHtml = getInjectedStyle(); // 额外追加的样式
3132
try {
@@ -40,7 +41,7 @@ const GroupCustomWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
4041
}
4142
}, [html]);
4243

43-
return <iframe className="w-full h-full" src={src} />;
44+
return <WebviewKeepAlive className="w-full h-full" url={url} />;
4445
};
4546
GroupCustomWebPanelRender.displayName = 'GroupCustomWebPanelRender';
4647

client/web/plugins/com.msgbyte.webview/src/group/GroupWebPanelRender.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { Translate } from '../translate';
3-
import { Webview } from '@capital/component';
3+
import { WebviewKeepAlive } from '@capital/component';
44

55
const GroupWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
66
const panelInfo = props.panelInfo;
@@ -11,7 +11,9 @@ const GroupWebPanelRender: React.FC<{ panelInfo: any }> = (props) => {
1111

1212
const url = panelInfo?.meta?.url;
1313

14-
return <Webview key={String(url)} className="w-full h-full" url={url} />;
14+
return (
15+
<WebviewKeepAlive key={String(url)} className="w-full h-full" url={url} />
16+
);
1517
};
1618
GroupWebPanelRender.displayName = 'GroupWebPanelRender';
1719

client/web/src/components/Webview.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ interface WebviewProps {
1010
/**
1111
* 网页渲染容器
1212
*/
13-
export const Webview: React.FC<WebviewProps> =
14-
withKeepAliveOverlay<WebviewProps>(
15-
(props) => {
16-
return <iframe className="w-full h-full bg-white" src={props.url} />;
17-
},
18-
{ cacheId: (props) => props.url }
19-
);
13+
export const Webview: React.FC<WebviewProps> = (props) => {
14+
return <iframe className="w-full h-full" src={props.url} />;
15+
};
2016
Webview.displayName = 'Webview';
17+
18+
/**
19+
* 带缓存的网页渲染容器
20+
* 用于需要在切换时依旧保持加载的case
21+
*/
22+
export const WebviewKeepAlive: React.FC<WebviewProps> =
23+
withKeepAliveOverlay<WebviewProps>(Webview, {
24+
cacheId: (props) => props.url,
25+
});
26+
WebviewKeepAlive.displayName = 'WebviewKeepAlive';

client/web/src/plugin/component/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ export { ErrorBoundary } from '@/components/ErrorBoundary';
5656
export { UserAvatar } from '@/components/UserAvatar';
5757
export { UserName } from '@/components/UserName';
5858
export { Markdown } from '@/components/Markdown';
59-
export { Webview } from '@/components/Webview';
59+
export { Webview, WebviewKeepAlive } from '@/components/Webview';

0 commit comments

Comments
 (0)