Skip to content

Commit

Permalink
feat: postmessage
Browse files Browse the repository at this point in the history
  • Loading branch information
wkylin committed Jul 21, 2024
1 parent d9d3007 commit f38cf5a
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ APP_BASE_URL=
DEPLOYED_ENV=Dev
VITE_GREETINGS=Dev
AUTH_USER=wkylin.w
AUTH_PASSWORD=wkylin.w
AUTH_PASSWORD=wkylin.w
IFRAME_ORIGIN=http://localhost:8080
3 changes: 2 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ DEPLOYED_ENV=Local
VITE_GREETINGS=Development
AUTH_USER=wkylin.w
AUTH_PASSWORD=wkylin.w
PUBLIC_URL=''
PUBLIC_URL=''
IFRAME_ORIGIN=http://localhost:8080
3 changes: 2 additions & 1 deletion .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ AUTH_USER=wkylin.w
AUTH_PASSWORD=wkylin.w
# github pages
# PUBLIC_URL='/pro-react-admin'
PUBLIC_URL=''
PUBLIC_URL=''
IFRAME_ORIGIN=https://wkylin.github.io/pro-react-admin
2 changes: 1 addition & 1 deletion src/pages/bigScreen/chinaMap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react'
import EChartsCommon from '@stateless/EChartsCommon'
import { mapOptions } from './options'
import mapOptions from './options'

const ChinaMap = () => {
const [mapData] = useState({
Expand Down
10 changes: 6 additions & 4 deletions src/pages/bigScreen/chinaMap/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ const mapData = {
],
}

export const mapOptions = (params: any) => ({
const mapOptions = (params: any) => ({
title: {
show: false,
text: '全国物流输送图',
Expand All @@ -1651,9 +1651,9 @@ export const mapOptions = (params: any) => ({
zoom: 1.2,
itemStyle: {
normal: {
borderColor: 'rgba(255,209,163, .5)', //区域边框颜色
areaColor: 'rgba(73,86,166,.1)', //区域颜色
borderWidth: 0.5, //区域边框宽度
borderColor: 'rgba(255,209,163, .5)', // 区域边框颜色
areaColor: 'rgba(73,86,166,.1)', // 区域颜色
borderWidth: 0.5, // 区域边框宽度
shadowBlur: 5,
shadowColor: 'rgba(107,91,237,.7)',
},
Expand Down Expand Up @@ -1733,3 +1733,5 @@ export const mapOptions = (params: any) => ({
},
],
})

export default mapOptions
12 changes: 6 additions & 6 deletions src/pages/dashboard/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Dashboard = () => {
const navigate = useNavigate()
// const navigateType = useNavigationType()
const {
token: { colorBgContainer }
token: { colorBgContainer },
} = theme.useToken()

return (
Expand All @@ -20,16 +20,16 @@ const Dashboard = () => {
<Content style={{ height: '100%', background: colorBgContainer }}>
<Routes>
<Route
path='/'
path="/"
element={
<>
{/* <h2>Look, more routes!</h2> */}
{/* <h2>Navigate type: {navigateType}</h2> */}
<Space>
<Button type='primary' onClick={() => navigate('/')}>
<Button type="primary" onClick={() => navigate('/')}>
Navigate /
</Button>
<Button type='primary' onClick={() => navigate('invoices')}>
<Button type="primary" onClick={() => navigate('invoices')}>
navigate to invoices
</Button>
</Space>
Expand All @@ -38,10 +38,10 @@ const Dashboard = () => {
}
/>
<Route
path='invoices'
path="invoices"
element={
<>
<Button type='primary' onClick={() => navigate(-1)}>
<Button type="primary" onClick={() => navigate(-1)}>
navigate to dashborad
</Button>
</>
Expand Down
1 change: 1 addition & 0 deletions src/pages/layout/proSecNav/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const ProSecNav = () => {
{ label: 'Dynamic', key: '/dynamic', icon: <QrcodeOutlined /> },
{ label: 'BigScreen', key: '/big-screen', icon: <FireOutlined /> },
{ label: 'ReactAmap', key: '/react-amap', icon: <QrcodeOutlined /> },
{ label: 'PostMessage', key: '/postmessage', icon: <FireOutlined /> },
{
label: '技术栈',
key: '/sub-act',
Expand Down
50 changes: 50 additions & 0 deletions src/pages/postmessage/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useRef, useEffect } from 'react'

const PostMessage = () => {
const origin = process.env.IFRAME_ORIGIN
const childOrigin = `${process.env.IFRAME_ORIGIN}/#/my-iframe`
const ref = useRef()

const onReceivedMessage = (event) => {
if (event.origin !== childOrigin) {
// do something with the received messages
}

// do something with the received messages
}

const sendMessage = () => {
if (!ref.current) {
return
}
ref.current.contentWindow.postMessage(`Hello iframe!${Math.random()}`, origin)
}

useEffect(() => {
window.addEventListener('message', onReceivedMessage)

return () => {
window.removeEventListener('message', onReceivedMessage)
}
})

return (
<>
<button onClick={sendMessage}>send a message</button>

<iframe
style={{
display: 'block',
border: 'none',
width: '100%',
}}
src={childOrigin}
ref={ref}
width="800px"
height="600px"
/>
</>
)
}

export default PostMessage
32 changes: 32 additions & 0 deletions src/pages/postmessage/myIframe/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useState, useEffect } from 'react'

const MyIFrame = () => {
const parentOrigin = 'http://localhost:8080'
const [message, setMessage] = useState() // string | undefined

const onReceivedMessage = (event) => {
// check the message source origin as a security measure
if (event.origin !== parentOrigin) {
return
}

// see notes on checking the data type
setMessage(event.data)
}

useEffect(() => {
window.addEventListener('message', onReceivedMessage)

return () => {
window.removeEventListener('message', onReceivedMessage)
}
})

if (!message) {
return <p>no message has been received</p>
}

return <p>message received: {message}</p>
}

export default MyIFrame
18 changes: 18 additions & 0 deletions src/routers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const PrismRender = lazy(() => import('@pages/prism'))
// const DynamicModal = lazy(() => import('@pages/dynamicModal'))
const BigScreen = lazy(() => import('@pages/bigScreen'))
const ReactAmap = lazy(() => import('@pages/reactAmap'))
const PostMessage = lazy(() => import('@pages/postmessage'))
const MyIframe = lazy(() => import('@pages/postmessage/myIframe'))
const Exception403 = lazy(() => import('@stateless/Exception/exception403'))
const NoMatch = lazy(() => import('@stateless/NoMatch'))

Expand Down Expand Up @@ -178,6 +180,22 @@ const rootRouter = [
auth: false,
element: lazyLoad(ReactAmap),
},
{
index: false,
path: 'postmessage',
name: 'postmessage',
key: '/postmessage',
auth: false,
element: lazyLoad(PostMessage),
},
{
index: false,
path: 'my-iframe',
name: 'my-iframe',
key: '/my-iframe',
auth: false,
element: lazyLoad(MyIframe),
},
{
index: false,
path: 'coupons',
Expand Down
4 changes: 2 additions & 2 deletions src/utils/publicFn/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const getKeyName = (pathName = '/404') => {
title: 'Not Found',
tabKey: '/404',
element: <Exception404 />,
i18nKey: 'notFound'
i18nKey: 'notFound',
}
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export const formatTime = (time, fmt) => {
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
S: date.getMilliseconds()
S: date.getMilliseconds(),
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
Expand Down

0 comments on commit f38cf5a

Please sign in to comment.