-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: plugin default use iframe render (#141)
* 🌐 style: update i18n * 🐛 fix: 修正 functions 可能会重复的问题 * ✨ feat: 支持 iframe 模式的插件加载 * 🐛 fix: 优化默认规则
- Loading branch information
Showing
10 changed files
with
153 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/pages/chat/features/Conversation/ChatList/Plugins/IFrameRender/hooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { onPluginFetchMessage, onPluginReady } from './utils'; | ||
|
||
export const useOnPluginReady = (onReady: () => void) => { | ||
useEffect(() => { | ||
const fn = (e: MessageEvent) => { | ||
onPluginReady(e, onReady); | ||
}; | ||
|
||
window.addEventListener('message', fn); | ||
return () => { | ||
window.removeEventListener('message', fn); | ||
}; | ||
}, []); | ||
}; | ||
|
||
export const useOnPluginFetchMessage = (onRequest: (data: any) => void, deps: any[] = []) => { | ||
useEffect(() => { | ||
const fn = (e: MessageEvent) => { | ||
onPluginFetchMessage(e, onRequest); | ||
}; | ||
|
||
window.addEventListener('message', fn); | ||
return () => { | ||
window.removeEventListener('message', fn); | ||
}; | ||
}, deps); | ||
}; |
62 changes: 62 additions & 0 deletions
62
src/pages/chat/features/Conversation/ChatList/Plugins/IFrameRender/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { PluginRenderProps } from '@lobehub/chat-plugin-sdk'; | ||
import { Skeleton } from 'antd'; | ||
import { memo, useEffect, useRef, useState } from 'react'; | ||
|
||
import { useOnPluginFetchMessage, useOnPluginReady } from './hooks'; | ||
import { sendMessageToPlugin } from './utils'; | ||
|
||
interface IFrameRenderProps extends PluginRenderProps { | ||
height?: number; | ||
url: string; | ||
width?: number; | ||
} | ||
|
||
const IFrameRender = memo<IFrameRenderProps>(({ url, width = 800, height = 300, ...props }) => { | ||
const [loading, setLoading] = useState(true); | ||
const [readyForRender, setReady] = useState(false); | ||
const iframeRef = useRef<HTMLIFrameElement>(null); | ||
|
||
useOnPluginReady(() => setReady(true)); | ||
|
||
// 当 props 发生变化时,主动向 iframe 发送数据 | ||
useEffect(() => { | ||
const iframeWin = iframeRef.current?.contentWindow; | ||
|
||
if (iframeWin && readyForRender) { | ||
sendMessageToPlugin(iframeWin, props); | ||
} | ||
}, [readyForRender, props]); | ||
|
||
// 当接收到来自 iframe 的请求时,触发发送数据 | ||
useOnPluginFetchMessage(() => { | ||
const iframeWin = iframeRef.current?.contentWindow; | ||
if (iframeWin) { | ||
sendMessageToPlugin(iframeWin, props); | ||
} | ||
}, [props]); | ||
|
||
return ( | ||
<> | ||
{loading && <Skeleton active style={{ width }} />} | ||
<iframe | ||
// @ts-ignore | ||
allowtransparency="true" | ||
height={height} | ||
hidden={loading} | ||
onLoad={() => { | ||
setLoading(false); | ||
}} | ||
ref={iframeRef} | ||
src={url} | ||
style={{ | ||
border: 0, | ||
// iframe 在 color-scheme:dark 模式下无法透明 | ||
// refs: https://www.jianshu.com/p/bc5a37bb6a7b | ||
colorScheme: 'light', | ||
}} | ||
width={width} | ||
/> | ||
</> | ||
); | ||
}); | ||
export default IFrameRender; |
17 changes: 17 additions & 0 deletions
17
src/pages/chat/features/Conversation/ChatList/Plugins/IFrameRender/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { PluginChannel } from '@lobehub/chat-plugin-sdk'; | ||
|
||
export const onPluginReady = (e: MessageEvent, onReady: () => void) => { | ||
if (e.data.type === PluginChannel.pluginReadyForRender) { | ||
onReady(); | ||
} | ||
}; | ||
|
||
export const onPluginFetchMessage = (e: MessageEvent, onRequest: (data: any) => void) => { | ||
if (e.data.type === PluginChannel.fetchPluginMessage) { | ||
onRequest(e.data); | ||
} | ||
}; | ||
|
||
export const sendMessageToPlugin = (window: Window, props: any) => { | ||
window.postMessage({ props, type: PluginChannel.renderPlugin }, '*'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters