2020-06-01 23:16:55 +02:00
|
|
|
<script>
|
2020-10-13 10:06:56 +02:00
|
|
|
import { goto } from "@sveltech/routify"
|
2020-06-01 13:15:44 +02:00
|
|
|
import { store } from "builderStore"
|
2020-08-13 15:02:15 +02:00
|
|
|
import { getComponentDefinition } from "builderStore/storeUtils"
|
2020-06-01 13:15:44 +02:00
|
|
|
import ConfirmDialog from "components/common/ConfirmDialog.svelte"
|
2020-10-26 15:23:08 +01:00
|
|
|
import { last } from "lodash/fp"
|
2020-10-13 10:06:56 +02:00
|
|
|
import { getParent, saveCurrentPreviewItem } from "builderStore/storeUtils"
|
2020-09-07 00:50:11 +02:00
|
|
|
import { DropdownMenu } from "@budibase/bbui"
|
2020-10-26 15:23:08 +01:00
|
|
|
import { DropdownContainer, DropdownItem } from "components/common/Dropdowns"
|
2020-06-01 13:15:44 +02:00
|
|
|
|
|
|
|
export let component
|
|
|
|
|
|
|
|
let confirmDeleteDialog
|
2020-09-06 23:58:47 +02:00
|
|
|
let dropdown
|
2020-09-07 00:50:11 +02:00
|
|
|
let anchor
|
2020-09-06 23:58:47 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
$: noChildrenAllowed =
|
2020-08-12 17:28:19 +02:00
|
|
|
!component || !getComponentDefinition($store, component._component).children
|
2020-06-02 12:11:53 +02:00
|
|
|
$: noPaste = !$store.componentToPaste
|
2020-06-01 13:15:44 +02:00
|
|
|
|
2020-10-27 16:28:13 +01:00
|
|
|
const lastPartOfName = c => (c ? last(c._component.split("/")) : "")
|
2020-06-01 13:15:44 +02:00
|
|
|
|
|
|
|
const hideDropdown = () => {
|
|
|
|
dropdown.hide()
|
|
|
|
}
|
|
|
|
|
2020-10-27 16:28:13 +01:00
|
|
|
const selectComponent = component => {
|
2020-10-13 10:06:56 +02:00
|
|
|
store.selectComponent(component)
|
|
|
|
const path = store.getPathToComponent(component)
|
|
|
|
$goto(`./:page/:screen/${path}`)
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
const moveUpComponent = () => {
|
2020-10-27 16:28:13 +01:00
|
|
|
store.update(s => {
|
2020-06-01 13:15:44 +02:00
|
|
|
const parent = getParent(s.currentPreviewItem.props, component)
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
const currentIndex = parent._children.indexOf(component)
|
|
|
|
if (currentIndex === 0) return s
|
|
|
|
|
2020-10-27 16:28:13 +01:00
|
|
|
const newChildren = parent._children.filter(c => c !== component)
|
2020-06-01 13:15:44 +02:00
|
|
|
newChildren.splice(currentIndex - 1, 0, component)
|
|
|
|
parent._children = newChildren
|
|
|
|
}
|
|
|
|
s.currentComponentInfo = component
|
|
|
|
saveCurrentPreviewItem(s)
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
return s
|
|
|
|
})
|
|
|
|
}
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
const moveDownComponent = () => {
|
2020-10-27 16:28:13 +01:00
|
|
|
store.update(s => {
|
2020-06-01 13:15:44 +02:00
|
|
|
const parent = getParent(s.currentPreviewItem.props, component)
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
if (parent) {
|
|
|
|
const currentIndex = parent._children.indexOf(component)
|
|
|
|
if (currentIndex === parent._children.length - 1) return s
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-10-27 16:28:13 +01:00
|
|
|
const newChildren = parent._children.filter(c => c !== component)
|
2020-06-01 13:15:44 +02:00
|
|
|
newChildren.splice(currentIndex + 1, 0, component)
|
|
|
|
parent._children = newChildren
|
|
|
|
}
|
|
|
|
s.currentComponentInfo = component
|
|
|
|
saveCurrentPreviewItem(s)
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
return s
|
2020-06-01 13:12:25 +02:00
|
|
|
})
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const copyComponent = () => {
|
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
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
const deleteComponent = () => {
|
2020-10-27 16:28:13 +01:00
|
|
|
store.update(state => {
|
2020-06-01 13:15:44 +02:00
|
|
|
const parent = getParent(state.currentPreviewItem.props, component)
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
if (parent) {
|
2020-10-27 16:28:13 +01:00
|
|
|
parent._children = parent._children.filter(c => c !== component)
|
2020-10-13 10:06:56 +02:00
|
|
|
selectComponent(parent)
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-06-01 13:15:44 +02:00
|
|
|
saveCurrentPreviewItem(state)
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
store.storeComponentForCopy(component, cut)
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-01 13:12:25 +02:00
|
|
|
|
2020-10-27 16:28:13 +01:00
|
|
|
const pasteComponent = mode => {
|
2020-08-12 17:28:19 +02:00
|
|
|
// lives in store - also used by drag drop
|
|
|
|
store.pasteComponent(component, mode)
|
2020-06-01 13:15:44 +02:00
|
|
|
}
|
2020-06-01 11:18:45 +02:00
|
|
|
</script>
|
|
|
|
|
2020-10-26 16:17:18 +01:00
|
|
|
<div bind:this={anchor} on:click|stopPropagation>
|
2020-10-14 14:21:43 +02:00
|
|
|
<div class="icon" on:click={dropdown.show}><i class="ri-more-line" /></div>
|
2020-10-26 15:23:08 +01:00
|
|
|
<DropdownMenu bind:this={dropdown} width="170px" {anchor} align="left">
|
|
|
|
<DropdownContainer on:click={hideDropdown}>
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-delete-bin-line"
|
|
|
|
title="Delete"
|
|
|
|
on:click={() => confirmDeleteDialog.show()} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-arrow-up-line"
|
|
|
|
title="Move up"
|
|
|
|
on:click={moveUpComponent} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-arrow-down-line"
|
|
|
|
title="Move down"
|
|
|
|
on:click={moveDownComponent} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-repeat-one-line"
|
|
|
|
title="Duplicate"
|
|
|
|
on:click={copyComponent} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-scissors-cut-line"
|
|
|
|
title="Cut"
|
|
|
|
on:click={() => storeComponentForCopy(true)} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-file-copy-line"
|
|
|
|
title="Copy"
|
|
|
|
on:click={() => storeComponentForCopy(false)} />
|
|
|
|
<hr class="hr-style" />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-insert-row-top"
|
|
|
|
title="Paste above"
|
|
|
|
disabled={noPaste}
|
|
|
|
on:click={() => pasteComponent('above')} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-insert-row-bottom"
|
|
|
|
title="Paste below"
|
|
|
|
disabled={noPaste}
|
|
|
|
on:click={() => pasteComponent('below')} />
|
|
|
|
<DropdownItem
|
|
|
|
icon="ri-insert-column-right"
|
|
|
|
title="Paste inside"
|
|
|
|
disabled={noPaste || noChildrenAllowed}
|
|
|
|
on:click={() => pasteComponent('inside')} />
|
|
|
|
</DropdownContainer>
|
|
|
|
</DropdownMenu>
|
2020-09-07 00:50:11 +02:00
|
|
|
</div>
|
2020-06-01 11:18:45 +02:00
|
|
|
<ConfirmDialog
|
|
|
|
bind:this={confirmDeleteDialog}
|
|
|
|
title="Confirm Delete"
|
|
|
|
body={`Are you sure you wish to delete this '${lastPartOfName(component)}' component?`}
|
|
|
|
okText="Delete Component"
|
2020-06-01 13:12:25 +02:00
|
|
|
onOk={deleteComponent} />
|
2020-06-01 11:18:45 +02:00
|
|
|
|
|
|
|
<style>
|
2020-10-26 15:23:08 +01:00
|
|
|
hr {
|
2020-06-01 16:31:55 +02:00
|
|
|
margin: 8px 0;
|
2020-10-26 15:23:08 +01:00
|
|
|
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>
|