From f88ef064b1ebb4a4be09c01a667411ccce7a2528 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 16 Sep 2021 16:27:19 +0100 Subject: [PATCH] Fix intermittent crash due to invalid iframe reference when hot reloading --- .../AppPreview/CurrentItemPreview.svelte | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte b/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte index 33ef0c6ea4..9b44c138a4 100644 --- a/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte +++ b/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte @@ -104,8 +104,10 @@ // remove all iframe event listeners on component destroy onDestroy(() => { - iframe.contentWindow.removeEventListener("bb-event", handleBudibaseEvent) - iframe.contentWindow.removeEventListener("keydown", handleKeydownEvent) + if (iframe.contentWindow) { + iframe.contentWindow.removeEventListener("bb-event", handleBudibaseEvent) + iframe.contentWindow.removeEventListener("keydown", handleKeydownEvent) + } }) const handleBudibaseEvent = event => { @@ -123,17 +125,21 @@ } else { console.warning(`Client sent unknown event type: ${type}`) } - }; + } const handleKeydownEvent = event => { - if ((event.key === "Delete" || event.key === "Backspace") && + if ( + (event.key === "Delete" || event.key === "Backspace") && selectedComponentId && - ['input', 'textarea'].indexOf(iframe.contentWindow.document.activeElement?.tagName.toLowerCase()) === -1) { - confirmDeleteComponent(selectedComponentId); + ["input", "textarea"].indexOf( + iframe.contentWindow.document.activeElement?.tagName.toLowerCase() + ) === -1 + ) { + confirmDeleteComponent(selectedComponentId) } } - const confirmDeleteComponent = (componentId) => { + const confirmDeleteComponent = componentId => { idToDelete = componentId confirmDeleteDialog.show() }