Skip to content

Commit adcf253

Browse files
author
Artyom Podymov
committed
refactor events communication between app and plugins
1 parent e0e5cdc commit adcf253

File tree

6 files changed

+63
-18
lines changed

6 files changed

+63
-18
lines changed

redisinsight/ui/src/components/query-card/QueryCardCliPlugin/QueryCardCliPlugin.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,31 @@ const QueryCardCliPlugin = (props: Props) => {
4848
const dispatch = useDispatch()
4949

5050
const executeCommand = () => {
51-
pluginIframeRef?.current?.contentWindow?.postMessage({
51+
const event:any = document.createEvent('Event')
52+
event.initEvent('message', true, true)
53+
event.data = {
5254
event: 'executeCommand',
5355
method: currentView.activationMethod,
5456
data: { command: query, data: result, status }
55-
}, '*')
57+
}
58+
event.origin = '*'
59+
pluginIframeRef?.current?.contentWindow?.dispatchEvent(event)
5660
}
5761

5862
const sendRedisCommand = (command: string, requestId: string) => {
5963
dispatch(
6064
sendPluginCommandAction({
6165
command,
6266
onSuccessAction: (response) => {
63-
pluginIframeRef?.current?.contentWindow?.postMessage({
67+
const event:any = document.createEvent('Event')
68+
event.initEvent('message', true, true)
69+
event.data = {
6470
event: 'executeRedisCommand',
6571
requestId,
6672
data: response
67-
}, '*')
73+
}
74+
event.origin = '*'
75+
pluginIframeRef?.current?.contentWindow?.dispatchEvent(event)
6876
}
6977
})
7078
)
@@ -107,8 +115,9 @@ const QueryCardCliPlugin = (props: Props) => {
107115
bodyClass: theme === Theme.Dark ? 'theme_DARK' : 'theme_LIGHT',
108116
modules
109117
})
118+
//pluginIframeRef.current.src = `data:text/html;charset=utf-8,${encodeURI(html)}`
110119
// @ts-ignore
111-
pluginIframeRef.current.src = `data:text/html;charset=utf-8,${encodeURI(html)}`
120+
pluginIframeRef.current.srcdoc = html
112121
}
113122

114123
const getGlobalStylesSrc = (): string =>
@@ -155,7 +164,7 @@ const QueryCardCliPlugin = (props: Props) => {
155164
title={id}
156165
ref={pluginIframeRef}
157166
referrerPolicy="no-referrer"
158-
sandbox="allow-same-origin allow-scripts"
167+
sandbox="allow-same-origin allow-scripts allow-modals"
159168
/>
160169
{!!error && (
161170
<div className={styles.container}>

redisinsight/ui/src/packages/redis-app-plugin-api/helpers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
* @param data
55
*/
66
export const sendMessageToMain = (data = {}) => {
7-
window.top.postMessage(data, '*')
7+
const event = document.createEvent('Event')
8+
event.initEvent('message', true, true)
9+
event.data = data
10+
event.origin = '*'
11+
// eslint-disable-next-line no-restricted-globals
12+
parent.dispatchEvent(event)
813
}
14+
// export const sendMessageToMain = (data = {}) => {
15+
// window.top.postMessage(data, '*')
16+
// }

redisinsight/ui/src/packages/redisearch/src/main.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface Props {
1414

1515
const renderRediSearch = (props:Props) => {
1616
const { command = '', status = '', data: response = {} } = props
17+
alert('renderRediSearch')
1718
render(<App command={command} response={response} status={status} />,
1819
document.getElementById('app'))
1920
}

redisinsight/ui/src/plugins/pluginEvents.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export enum PluginEvents {
2020

2121
export const listenPluginsEvents = () => {
2222
globalThis.onmessage = (e: MessageEvent) => {
23+
console.log(22222, e.data)
2324
switch (e.data?.event) {
2425
case 'loaded': {
2526
pluginApi.sendEvent(e.data.iframeId, 'loaded')

redisinsight/ui/src/plugins/pluginImport.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export const importPluginScript = () => (config) => {
2020
const { callbacks } = globalThis.state
2121

2222
const sendMessageToMain = (data = {}) => {
23-
globalThis.top.postMessage(data, '*')
23+
const event = document.createEvent('Event')
24+
event.initEvent('message', true, true)
25+
event.data = data
26+
event.origin = '*'
27+
// eslint-disable-next-line no-restricted-globals
28+
parent.dispatchEvent(event)
2429
}
2530

2631
const providePluginSDK = () => {
@@ -36,9 +41,22 @@ export const importPluginScript = () => (config) => {
3641
}
3742

3843
const listenEvents = () => {
44+
3945
globalThis.onmessage = (e) => {
4046
if (e.data.event === events.EXECUTE_COMMAND) {
41-
plugin[e.data.method] && plugin[e.data.method](e.data.data)
47+
const event = document.createEvent('Event')
48+
event.initEvent('message', true, true)
49+
event.data = {
50+
event: events.EXECUTE_COMMAND,
51+
method: e.data.method,
52+
data: e.data.data,
53+
plugin: globalThis.plugin,
54+
iframeId: `${iframeId}`
55+
}
56+
event.origin = '*'
57+
// eslint-disable-next-line no-restricted-globals
58+
parent.dispatchEvent(event)
59+
globalThis.plugin[e.data.method] && globalThis.plugin[e.data.method](e.data.data)
4260
}
4361

4462
if (e.data.event === events.EXECUTE_REDIS_COMMAND) {
@@ -84,22 +102,29 @@ export const prepareIframeHtml = (config) => {
84102
<body class="${bodyClass}" style="height: fit-content">
85103
<div id="app"></div>
86104
<script>
87-
let plugin = {}
88105
;(${importPluginScriptInner})(\`${configString}\`);
89106
import(\`${scriptSrc}\`)
90107
.then((module) => {
91-
plugin = { ...module.default };
92-
globalThis.top.postMessage({
108+
globalThis.plugin = { ...module.default };
109+
var event = document.createEvent('Event')
110+
event.initEvent("message", true, true);
111+
event.data = {
93112
event: 'loaded',
94113
iframeId: \`${iframeId}\`
95-
}, '*')
114+
}
115+
event.origin = '*'
116+
parent.dispatchEvent(event);
96117
})
97-
.catch(() => {
98-
globalThis.top.postMessage({
118+
.catch((e) => {
119+
var event = document.createEvent('Event')
120+
event.initEvent("message", true, true);
121+
event.data = {
99122
event: 'error',
100123
iframeId: \`${iframeId}\`,
101124
error: \`${scriptPath} not found. Check if it has been renamed or deleted and try again.\`
102-
}, '*')
125+
}
126+
event.origin = '*'
127+
parent.dispatchEvent(event);
103128
})
104129
</script>
105130
<script src="${scriptSrc}" type="module"></script>

tests/e2e/tests/critical-path/workbench/scripting-area.e2e.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ test.skip('Verify that user when he have more than 10 results can request to vie
8888
await workbenchPage.sendCommandInWorkbench('FT.DROPINDEX products DD');
8989
});
9090
//skipped due the inaccessibility of the iframe
91-
test.skip
92-
.after(async t => {
91+
test.only.after(async t => {
9392
//Drop index and documents
9493
await workbenchPage.sendCommandInWorkbench('FT.DROPINDEX products DD');
9594
})
@@ -105,6 +104,8 @@ test.skip
105104
//Send search command
106105
await workbenchPage.sendCommandInWorkbench(searchCommand);
107106
//Check that result is displayed in Table view
107+
t.wait(10000)
108+
t.debug()
108109
await t.expect(workbenchPage.queryTableResult.exists).ok('The result is displayed in Table view');
109110
//Select Text view type
110111
await workbenchPage.selectViewTypeText();

0 commit comments

Comments
 (0)