From e625d2e4a321a20ff62c1904c9e65ce4ec0df636 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 16 Sep 2021 17:39:39 +0100 Subject: [PATCH] Reduce duplication in move componment handler from dnd callback --- .../AppPreview/CurrentItemPreview.svelte | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte b/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte index 3901252ee9..4e51850fe1 100644 --- a/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte +++ b/packages/builder/src/components/design/AppPreview/CurrentItemPreview.svelte @@ -125,29 +125,24 @@ // loading is supported loading = false } else if (type === "move-component") { - // Copy - const sourceComponent = findComponent( - get(currentAsset).props, - data.componentId - ) - const destinationComponent = findComponent( - get(currentAsset).props, - data.destinationComponentId - ) + const { componentId, destinationComponentId } = data + const rootComponent = get(currentAsset).props + + // Get source and destination components + const source = findComponent(rootComponent, componentId) + const destination = findComponent(rootComponent, destinationComponentId) // Stop if the target is a child of source - const path = findComponentPath( - sourceComponent, - data.destinationComponentId - ) + const path = findComponentPath(source, destinationComponentId) const ids = path.map(component => component._id) if (ids.includes(data.destinationComponentId)) { return } - if (sourceComponent && destinationComponent) { - store.actions.components.copy(sourceComponent, true) - store.actions.components.paste(destinationComponent, data.mode) + // Cut and paste the component to the new destination + if (source && destination) { + store.actions.components.copy(source, true) + store.actions.components.paste(destination, data.mode) } } else { console.warning(`Client sent unknown event type: ${type}`)