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

125 lines
3.5 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"
import { ActionMenu, MenuItem, Icon, notifications } from "@budibase/bbui"
2020-06-01 13:15:44 +02:00
export let component
let confirmDeleteDialog
$: 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-05-04 12:32:22 +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-05-04 12:32:22 +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 () => {
try {
await store.actions.components.delete(component)
} catch (error) {
notifications.error(error)
}
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-05-04 12:32:22 +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>
{#if definition?.editable !== false}
<ActionMenu>
<div slot="control" class="icon">
<Icon size="S" hoverable name="MoreSmallList" />
</div>
<MenuItem icon="Delete" on:click={confirmDeleteDialog.show}>
Delete
</MenuItem>
<MenuItem noClose icon="ChevronUp" on:click={moveUpComponent}>
Move up
</MenuItem>
<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>
<MenuItem icon="Copy" on:click={() => storeComponentForCopy(false)}>
Copy
</MenuItem>
<MenuItem
icon="LayersBringToFront"
on:click={() => pasteComponent("above")}
disabled={noPaste}
>
Paste above
</MenuItem>
<MenuItem
icon="LayersSendToBack"
on:click={() => pasteComponent("below")}
disabled={noPaste}
>
Paste below
</MenuItem>
<MenuItem
icon="ShowOneLayer"
on:click={() => pasteComponent("inside")}
disabled={noPaste || noChildrenAllowed}
>
Paste inside
</MenuItem>
</ActionMenu>
<ConfirmDialog
bind:this={confirmDeleteDialog}
title="Confirm Deletion"
body={`Are you sure you wish to delete this '${definition?.name}' component?`}
okText="Delete Component"
onOk={deleteComponent}
/>
{/if}