1
0
Fork 0
mirror of synced 2024-10-02 01:56:57 +13:00

Fully remove source component from client tree while dragging

This commit is contained in:
Andrew Kingston 2022-10-10 09:51:13 +01:00
parent c5b36863d2
commit 90172ec2a1
3 changed files with 34 additions and 8 deletions

View file

@ -210,8 +210,10 @@
return return
} }
// Filter out source component from consideration // Filter out source component and placeholder from consideration
const children = parent._children?.filter(x => x._id !== sourceId) const children = parent._children?.filter(
x => x._id !== "placeholder" && x._id !== sourceId
)
// Use inside if no existing children // Use inside if no existing children
if (!children?.length) { if (!children?.length) {

View file

@ -3,10 +3,7 @@ import { routeStore } from "./routes"
import { builderStore } from "./builder" import { builderStore } from "./builder"
import { appStore } from "./app" import { appStore } from "./app"
import { RoleUtils } from "@budibase/frontend-core" import { RoleUtils } from "@budibase/frontend-core"
import { import { findComponentById, findComponentParent } from "../utils/components.js"
findComponentById,
findComponentPathById,
} from "../utils/components.js"
import { Helpers } from "@budibase/bbui" import { Helpers } from "@budibase/bbui"
const createScreenStore = () => { const createScreenStore = () => {
@ -51,11 +48,16 @@ const createScreenStore = () => {
const { dndParent, dndIndex, selectedComponentId } = $builderStore const { dndParent, dndIndex, selectedComponentId } = $builderStore
const insert = true const insert = true
if (insert && activeScreen && dndParent && dndIndex != null) { if (insert && activeScreen && dndParent && dndIndex != null) {
let selectedComponent = findComponentById( // Remove selected component from tree
let selectedParent = findComponentParent(
activeScreen.props, activeScreen.props,
selectedComponentId selectedComponentId
) )
delete selectedComponent._component selectedParent._children = selectedParent._children?.filter(
x => x._id !== selectedComponentId
)
// Insert placeholder
const placeholder = { const placeholder = {
_component: "placeholder", _component: "placeholder",
_id: "placeholder", _id: "placeholder",

View file

@ -60,3 +60,25 @@ export const findChildrenByType = (component, type, children = []) => {
findChildrenByType(child, type, children) findChildrenByType(child, type, children)
}) })
} }
/**
* Recursively searches for the parent component of a specific component ID
*/
export const findComponentParent = (rootComponent, id, parentComponent) => {
if (!rootComponent || !id) {
return null
}
if (rootComponent._id === id) {
return parentComponent
}
if (!rootComponent._children) {
return null
}
for (const child of rootComponent._children) {
const childResult = findComponentParent(child, id, rootComponent)
if (childResult) {
return childResult
}
}
return null
}