-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathWebview.tsx
More file actions
54 lines (48 loc) · 1.39 KB
/
Webview.tsx
File metadata and controls
54 lines (48 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { t , useDragstatus } from 'tailchat-shared';
import { withKeepAliveOverlay } from './KeepAliveOverlay';
import { Loading } from './Loading';
interface WebviewProps {
className?: string;
style?: React.CSSProperties;
url: string;
}
/**
* 网页渲染容器
*/
export const Webview: React.FC<WebviewProps> = (props) => {
const ref = useRef<HTMLIFrameElement>(null);
const [spinning, setSpinning] = useState(true);
const status = useDragstatus()
useEffect(() => {
const callback = () => {
setSpinning(false);
};
ref.current?.addEventListener('load', callback);
return () => {
ref.current?.removeEventListener('load', callback);
};
}, []);
const pointerEvents =useMemo(()=>{
return status ? 'none' : 'auto'
},[status])
return (
<Loading
spinning={spinning}
className="w-full h-full"
tip={t('加载网页中...')}
>
<iframe ref={ref} style={{pointerEvents}} className="w-full h-full" src={props.url} />
</Loading>
);
};
Webview.displayName = 'Webview';
/**
* 带缓存的网页渲染容器
* 用于需要在切换时依旧保持加载的case
*/
export const WebviewKeepAlive: React.FC<WebviewProps> =
withKeepAliveOverlay<WebviewProps>(Webview, {
cacheId: (props) => props.url,
});
WebviewKeepAlive.displayName = 'WebviewKeepAlive';