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

147 lines
4.1 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-23 12:39:48 +02:00
import { Icon, Popover } from "@budibase/bbui"
import { DropdownContainer, DropdownItem } from "components/common/Dropdowns"
2020-06-01 13:15:44 +02:00
export let component
let confirmDeleteDialog
let dropdown
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 hideDropdown = () => {
dropdown.hide()
}
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>
<div bind:this={anchor} on:click|stopPropagation>
2021-04-23 12:39:48 +02:00
<div class="icon" on:click={dropdown.show}><Icon hoverable name="MoreSmallList" /></div>
<Popover bind:this={dropdown} width="170px" {anchor} align="left">
<DropdownContainer on:click={hideDropdown}>
<DropdownItem
icon="ri-delete-bin-line"
title="Delete"
2021-04-23 12:39:48 +02:00
on:click={() => confirmDeleteDialog.show()}
/>
<DropdownItem
icon="ri-arrow-up-line"
title="Move up"
2021-04-23 12:39:48 +02:00
on:click={moveUpComponent}
/>
<DropdownItem
icon="ri-arrow-down-line"
title="Move down"
2021-04-23 12:39:48 +02:00
on:click={moveDownComponent}
/>
<DropdownItem
icon="ri-repeat-one-line"
title="Duplicate"
2021-04-23 12:39:48 +02:00
on:click={duplicateComponent}
/>
<DropdownItem
icon="ri-scissors-cut-line"
title="Cut"
2021-04-23 12:39:48 +02:00
on:click={() => storeComponentForCopy(true)}
/>
<DropdownItem
icon="ri-file-copy-line"
title="Copy"
2021-04-23 12:39:48 +02:00
on:click={() => storeComponentForCopy(false)}
/>
<hr class="hr-style" />
<DropdownItem
icon="ri-insert-row-top"
title="Paste above"
disabled={noPaste}
2021-04-23 12:39:48 +02:00
on:click={() => pasteComponent("above")}
/>
<DropdownItem
icon="ri-insert-row-bottom"
title="Paste below"
disabled={noPaste}
2021-04-23 12:39:48 +02:00
on:click={() => pasteComponent("below")}
/>
<DropdownItem
icon="ri-insert-column-right"
title="Paste inside"
disabled={noPaste || noChildrenAllowed}
2021-04-23 12:39:48 +02:00
on:click={() => pasteComponent("inside")}
/>
</DropdownContainer>
</Popover>
2020-09-07 00:50:11 +02:00
</div>
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>