From 98cd12db5fc6d96826dc1a3b4480c66ae146d058 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 14 Dec 2021 14:04:10 +0000 Subject: [PATCH] Proxy state updates back from peek modals --- .../overlay/PeekScreenDisplay.svelte | 22 ++++++++++++++++--- packages/client/src/utils/buttonActions.js | 9 ++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/client/src/components/overlay/PeekScreenDisplay.svelte b/packages/client/src/components/overlay/PeekScreenDisplay.svelte index 51ff4412dc..7d3531d236 100644 --- a/packages/client/src/components/overlay/PeekScreenDisplay.svelte +++ b/packages/client/src/components/overlay/PeekScreenDisplay.svelte @@ -4,6 +4,7 @@ dataSourceStore, notificationStore, routeStore, + stateStore, } from "stores" import { Modal, ModalContent, ActionButton } from "@budibase/bbui" import { onDestroy } from "svelte" @@ -12,12 +13,13 @@ NOTIFICATION: "notification", CLOSE_SCREEN_MODAL: "close-screen-modal", INVALIDATE_DATASOURCE: "invalidate-datasource", + UPDATE_STATE: "update-state", } let iframe let listenersAttached = false - const invalidateDataSource = event => { + const proxyInvalidation = event => { const { dataSourceId } = event.detail dataSourceStore.actions.invalidateDataSource(dataSourceId) } @@ -27,14 +29,28 @@ notificationStore.actions.send(message, type, icon) } + const proxyStateUpdate = event => { + const { type, key, value, persist } = event.detail + if (type === "set") { + stateStore.actions.setValue(key, value, persist) + } else if (type === "delete") { + stateStore.actions.deleteValue(key) + } + } + function receiveMessage(message) { const handlers = { [MessageTypes.NOTIFICATION]: () => { proxyNotification(message.data) }, - [MessageTypes.CLOSE_SCREEN_MODAL]: peekStore.actions.hidePeek, + [MessageTypes.CLOSE_SCREEN_MODAL]: () => { + peekStore.actions.hidePeek() + }, [MessageTypes.INVALIDATE_DATASOURCE]: () => { - invalidateDataSource(message.data) + proxyInvalidation(message.data) + }, + [MessageTypes.UPDATE_STATE]: () => { + proxyStateUpdate(message.data) }, } diff --git a/packages/client/src/utils/buttonActions.js b/packages/client/src/utils/buttonActions.js index 5ee571ae5d..9c6ae73c1f 100644 --- a/packages/client/src/utils/buttonActions.js +++ b/packages/client/src/utils/buttonActions.js @@ -116,6 +116,15 @@ const updateStateHandler = action => { } else if (type === "delete") { stateStore.actions.deleteValue(key) } + + // Emit this as an event so that parent windows which are iframing us in + // can also update their state + if (get(routeStore).queryParams?.peek) { + window.parent.postMessage({ + type: "update-state", + detail: { type, key, value, persist }, + }) + } } const handlerMap = {