Improve component selection after deleting a component

This commit is contained in:
Andrew Kingston 2022-07-29 13:05:50 +01:00
parent 5737e23dd6
commit 65e43b0f70
1 changed files with 57 additions and 51 deletions

View File

@ -534,7 +534,16 @@ export const getFrontendStore = () => {
if (!component) { if (!component) {
return return
} }
let parentId
// Determine the next component to select after deletion
const state = get(store)
let nextSelectedComponentId
if (state.selectedComponentId === component._id) {
nextSelectedComponentId = store.actions.components.getNext()
if (!nextSelectedComponentId) {
nextSelectedComponentId = store.actions.components.getPrevious()
}
}
// Patch screen // Patch screen
await store.actions.screens.patch(screen => { await store.actions.screens.patch(screen => {
@ -549,17 +558,18 @@ export const getFrontendStore = () => {
if (!parent) { if (!parent) {
return false return false
} }
parentId = parent._id
parent._children = parent._children.filter( parent._children = parent._children.filter(
child => child._id !== component._id child => child._id !== component._id
) )
}) })
// Select the deleted component's parent // Update selected component if required
store.update(state => { if (nextSelectedComponentId) {
state.selectedComponentId = parentId store.update(state => {
return state state.selectedComponentId = nextSelectedComponentId
}) return state
})
}
}, },
copy: (component, cut = false, selectParent = true) => { copy: (component, cut = false, selectParent = true) => {
// Update store with copied component // Update store with copied component
@ -664,17 +674,16 @@ export const getFrontendStore = () => {
return state return state
}) })
}, },
selectPrevious: () => { getPrevious: () => {
const state = get(store) const state = get(store)
const componentId = state.selectedComponentId const componentId = state.selectedComponentId
const screen = get(selectedScreen) const screen = get(selectedScreen)
const parent = findComponentParent(screen.props, componentId) const parent = findComponentParent(screen.props, componentId)
let newComponentId = componentId
// Check we aren't right at the top of the tree // Check we aren't right at the top of the tree
const index = parent?._children.findIndex(x => x._id === componentId) const index = parent?._children.findIndex(x => x._id === componentId)
if (!parent || componentId === screen.props._id) { if (!parent || componentId === screen.props._id) {
return return null
} }
// If we have siblings above us, choose the sibling or a descendant // If we have siblings above us, choose the sibling or a descendant
@ -686,75 +695,72 @@ export const getFrontendStore = () => {
while (target._children?.length) { while (target._children?.length) {
target = target._children[target._children.length - 1] target = target._children[target._children.length - 1]
} }
newComponentId = target._id return target._id
} }
// Otherwise just select sibling // Otherwise just select sibling
else { return previousSibling._id
newComponentId = previousSibling._id
}
} }
// If no siblings above us, select the parent // If no siblings above us, select the parent
else { return parent._id
newComponentId = parent._id
}
// Only update state if component changed
if (newComponentId !== componentId) {
store.update(state => {
state.selectedComponentId = newComponentId
return state
})
}
}, },
selectNext: () => { getNext: () => {
const component = get(selectedComponent) const component = get(selectedComponent)
const componentId = component?._id const componentId = component?._id
const screen = get(selectedScreen) const screen = get(selectedScreen)
const parent = findComponentParent(screen.props, componentId) const parent = findComponentParent(screen.props, componentId)
const index = parent?._children.findIndex(x => x._id === componentId) const index = parent?._children.findIndex(x => x._id === componentId)
let newComponentId = componentId
// If we have children, select first child // If we have children, select first child
if (component._children?.length) { if (component._children?.length) {
newComponentId = component._children[0]._id return component._children[0]._id
} else if (!parent) { } else if (!parent) {
return null return null
} }
// Otherwise select the next sibling if we have one // Otherwise select the next sibling if we have one
else if (index < parent._children.length - 1) { if (index < parent._children.length - 1) {
const nextSibling = parent._children[index + 1] const nextSibling = parent._children[index + 1]
newComponentId = nextSibling._id return nextSibling._id
} }
// Last child, select our parents next sibling // Last child, select our parents next sibling
else { let target = parent
let target = parent let targetParent = findComponentParent(screen.props, target._id)
let targetParent = findComponentParent(screen.props, target._id) let targetIndex = targetParent?._children.findIndex(
let targetIndex = targetParent?._children.findIndex( child => child._id === target._id
)
while (
targetParent != null &&
targetIndex === targetParent._children?.length - 1
) {
target = targetParent
targetParent = findComponentParent(screen.props, target._id)
targetIndex = targetParent?._children.findIndex(
child => child._id === target._id child => child._id === target._id
) )
while (
targetParent != null &&
targetIndex === targetParent._children?.length - 1
) {
target = targetParent
targetParent = findComponentParent(screen.props, target._id)
targetIndex = targetParent?._children.findIndex(
child => child._id === target._id
)
}
if (targetParent) {
newComponentId = targetParent._children[targetIndex + 1]._id
}
} }
if (targetParent) {
// Only update state if component ID is different return targetParent._children[targetIndex + 1]._id
if (newComponentId !== componentId) { } else {
return null
}
},
selectPrevious: () => {
const previousId = store.actions.components.getPrevious()
if (previousId) {
store.update(state => { store.update(state => {
state.selectedComponentId = newComponentId state.selectedComponentId = previousId
return state
})
}
},
selectNext: () => {
const nextId = store.actions.components.getNext()
if (nextId) {
store.update(state => {
state.selectedComponentId = nextId
return state return state
}) })
} }