@dobot-plus/template 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dobot/.sftprc +3 -0
- package/.dobot/components/DobotPlusApp.tsx +78 -0
- package/.dobot/http/api.json +11 -0
- package/.dobot/http/api.ts +26 -0
- package/.dobot/http/api.ts.txt +26 -0
- package/.dobot/http/axios.ts +52 -0
- package/.dobot/http/axios.ts.txt +52 -0
- package/.dobot/http/http.ts +22 -0
- package/.dobot/http/http.ts.txt +22 -0
- package/.dobot/index.ts +2 -0
- package/.dobot/index.txt +2 -0
- package/.dobot/protocol/keyBoard.ts +18 -0
- package/.dobot/protocol/keyBoard.ts.txt +18 -0
- package/.dobot/protocol/methodsHandler.ts +15 -0
- package/.dobot/protocol/methodsHandler.ts.txt +15 -0
- package/.dobot/protocol/postMessageCenter.ts +50 -0
- package/.dobot/protocol/postMessageCenter.ts.txt +38 -0
- package/.dobot/protocol/postMessageHandler.ts +289 -0
- package/.dobot/protocol/postMessageHandler.ts.txt +289 -0
- package/.dobot/protocol/websocketHandler.ts +234 -0
- package/.dobot/protocol/websocketHandler.ts.txt +234 -0
- package/.dobot/shim.d.ts +3 -0
- package/.dobot/shim.d.ts.txt +3 -0
- package/.dobot/store/actions/toolActions.ts +69 -0
- package/.dobot/store/actions/toolActions.ts.txt +69 -0
- package/.dobot/store/actions/userMagamentActions.ts +25 -0
- package/.dobot/store/actions/userMagamentActions.ts.txt +25 -0
- package/.dobot/store/index.ts +7 -0
- package/.dobot/store/index.ts.txt +7 -0
- package/.dobot/store/reducers/index.ts +8 -0
- package/.dobot/store/reducers/index.ts.txt +8 -0
- package/.dobot/store/reducers/toolReducer.ts +152 -0
- package/.dobot/store/reducers/toolReducer.ts.txt +149 -0
- package/.dobot/store/reducers/userMagamentReducer.ts +38 -0
- package/.dobot/store/reducers/userMagamentReducer.ts.txt +35 -0
- package/.dobot/store/types.ts +240 -0
- package/.dobot/store/types.ts.txt +237 -0
- package/.dobot/template/default.tsx.mustache +20 -0
- package/.dobot/template/entry.css +26 -0
- package/.dobot/template/entry.tsx +53 -0
- package/.dobot/template/index.html +23 -0
- package/.dobot/template/index.scss +5 -0
- package/.dobot/utils/i18n.ts +31 -0
- package/.dobot/utils/i18n.ts.txt +31 -0
- package/.dobot/utils/mqtt.ts +61 -0
- package/.dobot/utils/mqtt.ts.txt +61 -0
- package/.dobot/utils/rem.js +21 -0
- package/.dobot/utils/rem.js.txt +21 -0
- package/.dobot/utils/tool.ts +15 -0
- package/.dobot/utils/tool.ts.txt +15 -0
- package/.eslintrc.cjs +18 -0
- package/.luarc.json +17 -0
- package/.vscode/Api.schema.json +103 -0
- package/.vscode/Blocks.schema.json +94 -0
- package/.vscode/Main.schema.json +42 -0
- package/.vscode/Script.schema.json +22 -0
- package/.vscode/Toolbar.schema.json +19 -0
- package/.vscode/extensions.json +8 -0
- package/.vscode/settings.json +52 -0
- package/Resources/document/config.json +0 -0
- package/Resources/i18n/client/de.json +11 -0
- package/Resources/i18n/client/en.json +11 -0
- package/Resources/i18n/client/es.json +11 -0
- package/Resources/i18n/client/hk.json +11 -0
- package/Resources/i18n/client/ja.json +11 -0
- package/Resources/i18n/client/ko.json +11 -0
- package/Resources/i18n/client/ru.json +11 -0
- package/Resources/i18n/client/zh.json +11 -0
- package/Resources/i18n/plugin/de.json +3 -0
- package/Resources/i18n/plugin/en.json +3 -0
- package/Resources/i18n/plugin/es.json +3 -0
- package/Resources/i18n/plugin/hk.json +3 -0
- package/Resources/i18n/plugin/ja.json +3 -0
- package/Resources/i18n/plugin/ko.json +3 -0
- package/Resources/i18n/plugin/ru.json +3 -0
- package/Resources/i18n/plugin/zh.json +3 -0
- package/Resources/images/pallet.svg +1 -0
- package/configs/Blocks.json +21 -0
- package/configs/Main.json +5 -0
- package/configs/Scripts.json +7 -0
- package/configs/Toolbar.json +5 -0
- package/dpt.json +4 -0
- package/lua/control.lua +19 -0
- package/lua/daemon.lua +22 -0
- package/lua/httpAPI.lua +65 -0
- package/lua/userAPI.lua +51 -0
- package/lua/utils/await485.lua +55 -0
- package/lua/utils/mqtt.lua +17 -0
- package/lua/utils/num_convert.lua +41 -0
- package/lua/utils/tcp.lua +31 -0
- package/lua/utils/util.lua +31 -0
- package/lua/utils/variables.lua +0 -0
- package/package.json +51 -0
- package/project.json +3 -0
- package/tsconfig.json +27 -0
- package/ui/Blocks.tsx +5 -0
- package/ui/Main.tsx +31 -0
- package/ui/Toolbar.tsx +5 -0
package/.dobot/.sftprc
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ReactNode, useEffect } from 'react'
|
|
2
|
+
import { MqttWebSocketClient } from '../utils/mqtt'
|
|
3
|
+
import dptConfig from '../../dpt.json'
|
|
4
|
+
import MainConfig from '../../configs/Main.json'
|
|
5
|
+
import { getPluginPort } from '@dobot/http/axios'
|
|
6
|
+
import '@dobot/protocol/methodsHandler'
|
|
7
|
+
import { toolActions } from '@dobot/store/actions/toolActions'
|
|
8
|
+
import store from '@dobot/store'
|
|
9
|
+
import { useSelector } from 'react-redux'
|
|
10
|
+
import { IStoreState } from '@dobot/store/types'
|
|
11
|
+
|
|
12
|
+
type BasicProps = {
|
|
13
|
+
children: ReactNode
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type DobotPlusAppProps =
|
|
17
|
+
| (BasicProps & {
|
|
18
|
+
useMqtt: true
|
|
19
|
+
topic?: string
|
|
20
|
+
onMessage: (data: object | string) => void
|
|
21
|
+
port?: number
|
|
22
|
+
})
|
|
23
|
+
| (BasicProps & {
|
|
24
|
+
useMqtt?: false
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
export function DobotPlusApp(props: DobotPlusAppProps) {
|
|
28
|
+
const { children, useMqtt } = props
|
|
29
|
+
const { portData } = useSelector((state: IStoreState) => state.toolReducer)
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const pluginName = MainConfig.name
|
|
32
|
+
const pluginVersion = MainConfig.version
|
|
33
|
+
|
|
34
|
+
if (useMqtt) {
|
|
35
|
+
const { topic, onMessage, port } = props
|
|
36
|
+
|
|
37
|
+
const ip = process.env.NODE_ENV === 'production' ? portData.ip : dptConfig.ip
|
|
38
|
+
|
|
39
|
+
if (ip && onMessage) {
|
|
40
|
+
const serverUrl = `ws://${ip}:${port || 8083}/mqtt`
|
|
41
|
+
const clientId = `${pluginName}_clientId`
|
|
42
|
+
const mqttClient = new MqttWebSocketClient(serverUrl, clientId)
|
|
43
|
+
mqttClient.connect()
|
|
44
|
+
mqttClient.subscribe(topic || `${pluginName}Status`, (topic, buf) => {
|
|
45
|
+
let data = {}
|
|
46
|
+
try {
|
|
47
|
+
data = JSON.parse(buf.toString())
|
|
48
|
+
} catch {
|
|
49
|
+
data = buf.toString()
|
|
50
|
+
}
|
|
51
|
+
onMessage(data)
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getPluginPort()
|
|
57
|
+
.then((res) => {
|
|
58
|
+
const { data } = res
|
|
59
|
+
const pluginKey = data[`${pluginName}_v${pluginVersion}`]
|
|
60
|
+
if (data) {
|
|
61
|
+
console.log('pluginPorts', data)
|
|
62
|
+
const pluginPort = data[pluginKey]
|
|
63
|
+
if (!pluginPort) {
|
|
64
|
+
setTimeout(() => {
|
|
65
|
+
getPluginPort()
|
|
66
|
+
}, 500)
|
|
67
|
+
} else {
|
|
68
|
+
store.dispatch(toolActions.setPluginPort(pluginPort))
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.catch((err) => {
|
|
73
|
+
console.log(err)
|
|
74
|
+
})
|
|
75
|
+
}, [portData.isSync])
|
|
76
|
+
|
|
77
|
+
return <>{children}</>
|
|
78
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios'
|
|
2
|
+
import apiConfig from './api.json'
|
|
3
|
+
import { request } from './axios'
|
|
4
|
+
|
|
5
|
+
type MethodName = keyof typeof apiConfig
|
|
6
|
+
export const http = {} as {
|
|
7
|
+
[key in MethodName]: (data?: object) => Promise<AxiosResponse<any, any>>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Object.entries(apiConfig).forEach(([key, value]) => {
|
|
11
|
+
const { url } = value
|
|
12
|
+
if (url) {
|
|
13
|
+
http[key as MethodName] = (data) => {
|
|
14
|
+
return request({
|
|
15
|
+
...value,
|
|
16
|
+
data
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
http[key as MethodName] = (data) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
console.warn('Please check your api config: ', key, data)
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios'
|
|
2
|
+
import apiConfig from './api.json'
|
|
3
|
+
import { request } from './axios'
|
|
4
|
+
|
|
5
|
+
type MethodName = keyof typeof apiConfig
|
|
6
|
+
export const http = {} as {
|
|
7
|
+
[key in MethodName]: (data?: object) => Promise<AxiosResponse<any, any>>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
Object.entries(apiConfig).forEach(([key, value]) => {
|
|
11
|
+
const { url } = value
|
|
12
|
+
if (url) {
|
|
13
|
+
http[key as MethodName] = (data) => {
|
|
14
|
+
return request({
|
|
15
|
+
...value,
|
|
16
|
+
data
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
http[key as MethodName] = (data) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
console.warn('Please check your api config: ', key, data)
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
})
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
2
|
+
import dptConfig from '../../dpt.json'
|
|
3
|
+
import pluginConfig from '../../configs/Main.json'
|
|
4
|
+
import store from '@dobot/store'
|
|
5
|
+
|
|
6
|
+
const instance = axios.create()
|
|
7
|
+
|
|
8
|
+
instance.interceptors.request.use(
|
|
9
|
+
(config: InternalAxiosRequestConfig) => {
|
|
10
|
+
config.headers.AuthorizationToken = localStorage.getItem('AuthorizationToken') || ''
|
|
11
|
+
return config
|
|
12
|
+
},
|
|
13
|
+
(err: AxiosError) => Promise.reject(err)
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
instance.interceptors.response.use(
|
|
17
|
+
(res: AxiosResponse) => {
|
|
18
|
+
return res
|
|
19
|
+
},
|
|
20
|
+
(err: AxiosError) => {
|
|
21
|
+
return Promise.reject(err)
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
export function request(config: AxiosRequestConfig, port?: number) {
|
|
26
|
+
const { portData } = store.getState().toolReducer
|
|
27
|
+
|
|
28
|
+
const options: AxiosRequestConfig = {
|
|
29
|
+
...config,
|
|
30
|
+
withCredentials: true,
|
|
31
|
+
baseURL: `http://${portData.ip}:${port}/dobotPlus/${pluginConfig.name}_v${pluginConfig.version}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
+
options.baseURL = `/dobotPlus/${pluginConfig.name}_v${pluginConfig.version}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return instance.request({
|
|
39
|
+
...options,
|
|
40
|
+
method: 'post'
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getPluginPort() {
|
|
45
|
+
return instance.request({
|
|
46
|
+
baseURL: process.env.NODE_ENV !== 'production' ? `` : `http://${dptConfig.ip}:22001`,
|
|
47
|
+
url: '/dobotPlus/getPorts',
|
|
48
|
+
method: 'get'
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default instance
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
2
|
+
import dptConfig from '../../dpt.json'
|
|
3
|
+
import pluginConfig from '../../configs/Main.json'
|
|
4
|
+
import store from '@dobot/store'
|
|
5
|
+
|
|
6
|
+
const instance = axios.create()
|
|
7
|
+
|
|
8
|
+
instance.interceptors.request.use(
|
|
9
|
+
(config: InternalAxiosRequestConfig) => {
|
|
10
|
+
config.headers.AuthorizationToken = localStorage.getItem('AuthorizationToken') || ''
|
|
11
|
+
return config
|
|
12
|
+
},
|
|
13
|
+
(err: AxiosError) => Promise.reject(err)
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
instance.interceptors.response.use(
|
|
17
|
+
(res: AxiosResponse) => {
|
|
18
|
+
return res
|
|
19
|
+
},
|
|
20
|
+
(err: AxiosError) => {
|
|
21
|
+
return Promise.reject(err)
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
export function request(config: AxiosRequestConfig, port?: number) {
|
|
26
|
+
const { portData } = store.getState().toolReducer
|
|
27
|
+
|
|
28
|
+
const options: AxiosRequestConfig = {
|
|
29
|
+
...config,
|
|
30
|
+
withCredentials: true,
|
|
31
|
+
baseURL: `http://${portData.ip}:${port}/dobotPlus/${pluginConfig.name}_v${pluginConfig.version}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
+
options.baseURL = `/dobotPlus/${pluginConfig.name}_v${pluginConfig.version}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return instance.request({
|
|
39
|
+
...options,
|
|
40
|
+
method: 'post'
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getPluginPort() {
|
|
45
|
+
return instance.request({
|
|
46
|
+
baseURL: process.env.NODE_ENV !== 'production' ? `` : `http://${dptConfig.ip}:22001`,
|
|
47
|
+
url: '/dobotPlus/getPorts',
|
|
48
|
+
method: 'get'
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default instance
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { request } from './axios'
|
|
2
|
+
|
|
3
|
+
export const demoMethod1 = (data: any) => {
|
|
4
|
+
return request({
|
|
5
|
+
url: 'demoMethod1',
|
|
6
|
+
data
|
|
7
|
+
})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const demoMethod2 = (data: any) => {
|
|
11
|
+
return request({
|
|
12
|
+
url: 'demoMethod2',
|
|
13
|
+
data
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const demoMethod3 = (data: any) => {
|
|
18
|
+
return request({
|
|
19
|
+
url: 'demoMethod3',
|
|
20
|
+
data
|
|
21
|
+
})
|
|
22
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { request } from './axios'
|
|
2
|
+
|
|
3
|
+
export const demoMethod1 = (data: any) => {
|
|
4
|
+
return request({
|
|
5
|
+
url: 'demoMethod1',
|
|
6
|
+
data
|
|
7
|
+
})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const demoMethod2 = (data: any) => {
|
|
11
|
+
return request({
|
|
12
|
+
url: 'demoMethod2',
|
|
13
|
+
data
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const demoMethod3 = (data: any) => {
|
|
18
|
+
return request({
|
|
19
|
+
url: 'demoMethod3',
|
|
20
|
+
data
|
|
21
|
+
})
|
|
22
|
+
}
|
package/.dobot/index.ts
ADDED
package/.dobot/index.txt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isInIframe } from '@dobot/utils/tool'
|
|
2
|
+
import keyboardLink from 'keyboard-link'
|
|
3
|
+
if (!isInIframe) {
|
|
4
|
+
const ws = new WebSocket('ws:127.0.0.1:10086')
|
|
5
|
+
ws.onopen = () => {
|
|
6
|
+
console.log('链接成功')
|
|
7
|
+
ws.close()
|
|
8
|
+
|
|
9
|
+
new keyboardLink({
|
|
10
|
+
host: 'ws:127.0.0.1',
|
|
11
|
+
port: 10086
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
ws.onerror = () => {
|
|
16
|
+
console.log('链接失败')
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isInIframe } from '@dobot/utils/tool'
|
|
2
|
+
import keyboardLink from 'keyboard-link'
|
|
3
|
+
if (!isInIframe) {
|
|
4
|
+
const ws = new WebSocket('ws:127.0.0.1:10086')
|
|
5
|
+
ws.onopen = () => {
|
|
6
|
+
console.log('链接成功')
|
|
7
|
+
ws.close()
|
|
8
|
+
|
|
9
|
+
new keyboardLink({
|
|
10
|
+
host: 'ws:127.0.0.1',
|
|
11
|
+
port: 10086
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
ws.onerror = () => {
|
|
16
|
+
console.log('链接失败')
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PostMessageHandler } from './postMessageHandler'
|
|
2
|
+
import { WebSocketHandler } from './websocketHandler'
|
|
3
|
+
import './keyBoard'
|
|
4
|
+
import { isInIframe } from '@dobot/utils/tool'
|
|
5
|
+
|
|
6
|
+
let methodsHandler: any = undefined
|
|
7
|
+
const postMessageHandler = new PostMessageHandler()
|
|
8
|
+
// 根据环境判断
|
|
9
|
+
if (isInIframe) {
|
|
10
|
+
methodsHandler = postMessageHandler
|
|
11
|
+
} else {
|
|
12
|
+
methodsHandler = new WebSocketHandler()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { methodsHandler, postMessageHandler }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PostMessageHandler } from './postMessageHandler'
|
|
2
|
+
import { WebSocketHandler } from './websocketHandler'
|
|
3
|
+
import './keyBoard'
|
|
4
|
+
import { isInIframe } from '@dobot/utils/tool'
|
|
5
|
+
|
|
6
|
+
let methodsHandler: any = undefined
|
|
7
|
+
const postMessageHandler = new PostMessageHandler()
|
|
8
|
+
// 根据环境判断
|
|
9
|
+
if (isInIframe) {
|
|
10
|
+
methodsHandler = postMessageHandler
|
|
11
|
+
} else {
|
|
12
|
+
methodsHandler = new WebSocketHandler()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { methodsHandler, postMessageHandler }
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { jsonSafeParse } from "@dobot/utils/tool"
|
|
2
|
+
|
|
3
|
+
export class PostMessageCenter {
|
|
4
|
+
dest: { [iframeName: string]: any }
|
|
5
|
+
constructor() {
|
|
6
|
+
this.dest = {}
|
|
7
|
+
}
|
|
8
|
+
send(message: any) {
|
|
9
|
+
if (!message.iframeName) return
|
|
10
|
+
const iframe = this.dest[message.to || message.iframeName]
|
|
11
|
+
const origin = this.dest[`${message.to || message.iframeName}Origin`]
|
|
12
|
+
if (!iframe || !origin) {
|
|
13
|
+
throw 'please register source and origin firstly'
|
|
14
|
+
}
|
|
15
|
+
iframe.postMessage(message, origin)
|
|
16
|
+
}
|
|
17
|
+
listen(cb: Function) {
|
|
18
|
+
window.addEventListener('message', event => {
|
|
19
|
+
|
|
20
|
+
const data = jsonSafeParse(event.data)
|
|
21
|
+
|
|
22
|
+
const iframeName = data.from || data.iframeName
|
|
23
|
+
if (
|
|
24
|
+
iframeName &&
|
|
25
|
+
[
|
|
26
|
+
'blockly',
|
|
27
|
+
'DobotStudio2020',
|
|
28
|
+
'dobotPlus',
|
|
29
|
+
'dobotplusApp',
|
|
30
|
+
'palleting3D'
|
|
31
|
+
].includes(iframeName)
|
|
32
|
+
) {
|
|
33
|
+
cb(event)
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
public addDest = (
|
|
38
|
+
iframeName: string,
|
|
39
|
+
source: HTMLIFrameElement,
|
|
40
|
+
iframeOrigin: string,
|
|
41
|
+
origin: string
|
|
42
|
+
) => {
|
|
43
|
+
this.dest[iframeName] = source
|
|
44
|
+
this.dest[iframeOrigin] = origin
|
|
45
|
+
}
|
|
46
|
+
registerSource(iframeName: string, source: any, origin: string) {
|
|
47
|
+
this.addDest(iframeName, source, `${iframeName}Origin`, origin)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export const communicator = new PostMessageCenter()
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { jsonSafeParse } from '@dobot/utils/tool'
|
|
2
|
+
|
|
3
|
+
export class PostMessageCenter {
|
|
4
|
+
dest: { [iframeName: string]: any }
|
|
5
|
+
constructor() {
|
|
6
|
+
this.dest = {}
|
|
7
|
+
}
|
|
8
|
+
send(message: any) {
|
|
9
|
+
if (!message.iframeName) return
|
|
10
|
+
const iframe = this.dest[message.to || message.iframeName]
|
|
11
|
+
const origin = this.dest[`${message.to || message.iframeName}Origin`]
|
|
12
|
+
if (!iframe || !origin) {
|
|
13
|
+
throw 'please register source and origin firstly'
|
|
14
|
+
}
|
|
15
|
+
iframe.postMessage(message, origin)
|
|
16
|
+
}
|
|
17
|
+
listen(cb: Function) {
|
|
18
|
+
window.addEventListener('message', (event) => {
|
|
19
|
+
const data = jsonSafeParse(event.data)
|
|
20
|
+
|
|
21
|
+
const iframeName = data.from || data.iframeName
|
|
22
|
+
if (
|
|
23
|
+
iframeName &&
|
|
24
|
+
['blockly', 'DobotStudio2020', 'dobotPlus', 'dobotplusApp', 'palleting3D'].includes(iframeName)
|
|
25
|
+
) {
|
|
26
|
+
cb(event)
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
public addDest = (iframeName: string, source: HTMLIFrameElement, iframeOrigin: string, origin: string) => {
|
|
31
|
+
this.dest[iframeName] = source
|
|
32
|
+
this.dest[iframeOrigin] = origin
|
|
33
|
+
}
|
|
34
|
+
registerSource(iframeName: string, source: any, origin: string) {
|
|
35
|
+
this.addDest(iframeName, source, `${iframeName}Origin`, origin)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export const communicator = new PostMessageCenter()
|