2020-06-01 23:16:55 +02:00
|
|
|
<script>
|
2020-12-07 16:27:46 +01:00
|
|
|
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"
|
2021-01-12 21:00:35 +01:00
|
|
|
import { findComponentParent } from "builderStore/storeUtils"
|
2021-10-04 17:50:52 +02:00
|
|
|
import { ActionMenu, MenuItem, Icon, notifications } from "@budibase/bbui"
|
2020-06-01 13:15:44 +02:00
|
|
|
|
|
|
|
export let component
|
|
|
|
|
|
|
|
let confirmDeleteDialog
|
2020-09-06 23:58:47 +02:00
|
|
|
|
2021-02-09 11:49:22 +01:00
|
|
|
$: definition = store.actions.components.getDefinition(component?._component)
|
|
|
|
$: noChildrenAllowed = !component || !definition?.hasChildren
|
2020-06-02 12:11:53 +02:00
|
|
|
$: noPaste = !$store.componentToPaste
|
2020-06-01 13:15:44 +02:00
|
|
|
|
|
|
|
const moveUpComponent = () => {
|
2021-01-14 10:09:23 +01:00
|
|
|
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)
|
2021-01-14 10:09:23 +01:00
|
|
|
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:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
const moveDownComponent = () => {
|
2021-01-14 10:09:23 +01:00
|
|
|
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)
|
2021-01-14 10:09:23 +01:00
|
|
|
newChildren.splice(currentIndex + 1, 0, component)
|
|
|
|
parent._children = newChildren
|
|
|
|
store.actions.preview.saveSelected()
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:44:18 +01:00
|
|
|
const duplicateComponent = () => {
|
2020-10-07 23:30:00 +02:00
|
|
|
storeComponentForCopy(false)
|
|
|
|
pasteComponent("below")
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2021-02-23 17:29:50 +01:00
|
|
|
const deleteComponent = async () => {
|
2021-10-04 17:50:52 +02:00
|
|
|
try {
|
|
|
|
await store.actions.components.delete(component)
|
|
|
|
} catch (error) {
|
|
|
|
notifications.error(error)
|
|
|
|
}
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-01 13:12:25 +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
|
|
|
}
|
2020-06-01 13:12:25 +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>
|
|
|
|
|
2021-07-07 18:07:16 +02:00
|
|
|
{#if definition?.editable !== false}
|
2021-06-30 10:05:03 +02:00
|
|
|
<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}
|