budibase/packages/builder/src/components/design/NavigationPanel/ComponentDropdownMenu.svelte

121 lines
3.4 KiB
Svelte
Raw Normal View History

2020-06-01 23:16:55 +02:00
<script>
import { get } from "svelte/store"
import { store, currentAsset } from "builderStore"
2020-06-01 13:15:44 +02:00
import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { findComponentParent } from "builderStore/storeUtils"
2021-04-26 14:42:45 +02:00
import { ActionMenu, MenuItem, Icon } from "@budibase/bbui"
2020-06-01 13:15:44 +02:00
export let component
let confirmDeleteDialog
2020-09-07 00:50:11 +02:00
let anchor
$: definition = store.actions.components.getDefinition(component?._component)
$: noChildrenAllowed = !component || !definition?.hasChildren
$: noPaste = !$store.componentToPaste
2020-06-01 13:15:44 +02:00
const moveUpComponent = () => {
const asset = get(currentAsset)
const parent = findComponentParent(asset.props, component._id)
if (!parent) {
return
}
const currentIndex = parent._children.indexOf(component)
if (currentIndex === 0) {
return
}
2021-04-23 12:39:48 +02:00
const newChildren = parent._children.filter((c) => c !== component)
newChildren.splice(currentIndex - 1, 0, component)
parent._children = newChildren
store.actions.preview.saveSelected()
2020-06-01 13:15:44 +02:00
}
2020-06-01 13:15:44 +02:00
const moveDownComponent = () => {
const asset = get(currentAsset)
const parent = findComponentParent(asset.props, component._id)
if (!parent) {
return
}
const currentIndex = parent._children.indexOf(component)
if (currentIndex === parent._children.length - 1) {
return
}
2021-04-23 12:39:48 +02:00
const newChildren = parent._children.filter((c) => c !== component)
newChildren.splice(currentIndex + 1, 0, component)
parent._children = newChildren
store.actions.preview.saveSelected()
2020-06-01 13:15:44 +02:00
}
const duplicateComponent = () => {
2020-10-07 23:30:00 +02:00
storeComponentForCopy(false)
pasteComponent("below")
2020-06-01 13:15:44 +02:00
}
const deleteComponent = async () => {
await store.actions.components.delete(component)
2020-06-01 13:15:44 +02:00
}
2020-06-01 13:15:44 +02:00
const storeComponentForCopy = (cut = false) => {
2020-08-12 17:28:19 +02:00
// lives in store - also used by drag drop
2020-11-04 18:09:45 +01:00
store.actions.components.copy(component, cut)
2020-06-01 13:15:44 +02:00
}
2021-04-23 12:39:48 +02:00
const pasteComponent = (mode) => {
2020-08-12 17:28:19 +02:00
// lives in store - also used by drag drop
2020-11-04 18:09:45 +01:00
store.actions.components.paste(component, mode)
2020-06-01 13:15:44 +02:00
}
2020-06-01 11:18:45 +02:00
</script>
<ActionMenu>
<div slot="control" class="icon">
2021-04-27 16:30:13 +02:00
<Icon size="S" hoverable name="MoreSmallList" />
2021-04-26 12:29:45 +02:00
</div>
<MenuItem icon="Delete" on:click={confirmDeleteDialog.show}>Delete</MenuItem>
<MenuItem noClose icon="ChevronUp" on:click={moveUpComponent}
>Move up</MenuItem
2021-04-26 14:42:45 +02:00
>
<MenuItem noClose icon="ChevronDown" on:click={moveDownComponent}
>Move down</MenuItem
>
<MenuItem noClose icon="Duplicate" on:click={duplicateComponent}
>Duplicate</MenuItem
>
<MenuItem icon="Cut" on:click={() => storeComponentForCopy(true)}
>Cut</MenuItem
2021-04-26 14:42:45 +02:00
>
<MenuItem icon="Copy" on:click={() => storeComponentForCopy(false)}
>Copy</MenuItem
>
<MenuItem
icon="LayersBringToFront"
on:click={() => pasteComponent("above")}
2021-04-26 14:42:45 +02:00
disabled={noPaste}>Paste above</MenuItem
>
<MenuItem
icon="LayersSendToBack"
on:click={() => pasteComponent("below")}
2021-04-26 14:42:45 +02:00
disabled={noPaste}>Paste below</MenuItem
>
<MenuItem
icon="ShowOneLayer"
on:click={() => pasteComponent("inside")}
2021-04-26 14:42:45 +02:00
disabled={noPaste || noChildrenAllowed}>Paste inside</MenuItem
>
2021-04-26 12:29:45 +02:00
</ActionMenu>
2020-06-01 11:18:45 +02:00
<ConfirmDialog
bind:this={confirmDeleteDialog}
title="Confirm Deletion"
body={`Are you sure you wish to delete this '${definition?.name}' component?`}
2020-06-01 11:18:45 +02:00
okText="Delete Component"
2021-04-23 12:39:48 +02:00
onOk={deleteComponent}
/>
2020-06-01 11:18:45 +02:00
<style>
hr {
2020-06-01 16:31:55 +02:00
margin: 8px 0;
background-color: var(--grey-4);
height: 1px;
border: none;
2020-06-01 16:31:55 +02:00
}
2020-06-01 13:15:44 +02:00
</style>