Dedupe component actions by dispatching keyboard events when clicking component dropdown menu

This commit is contained in:
Andrew Kingston 2022-08-01 10:06:01 +01:00
parent 808de3bbfb
commit f07fd31e68
1 changed files with 58 additions and 116 deletions

View File

@ -1,127 +1,69 @@
<script> <script>
import { store } from "builderStore" import { store } from "builderStore"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import { ActionMenu, MenuItem, Icon } from "@budibase/bbui"
import { ActionMenu, MenuItem, Icon, notifications } from "@budibase/bbui"
export let component
let confirmDeleteDialog
$: definition = store.actions.components.getDefinition(component?._component)
$: noChildrenAllowed = !component || !definition?.hasChildren
$: noPaste = !$store.componentToPaste $: noPaste = !$store.componentToPaste
// "editable" has been repurposed for inline text editing. const keyboardEvent = (key, ctrlKey = false) => {
// It remains here for legacy compatibility. document.dispatchEvent(new KeyboardEvent("keydown", { key, ctrlKey }))
// Future components should define "static": true for indicate they should
// not show a context menu.
$: showMenu = definition?.editable !== false && definition?.static !== true
const moveUpComponent = async () => {
try {
await store.actions.components.moveUp(component)
} catch (error) {
notifications.error("Error moving component up")
}
}
const moveDownComponent = async () => {
try {
await store.actions.components.moveDown(component)
} catch (error) {
notifications.error("Error moving component down")
}
}
const duplicateComponent = () => {
storeComponentForCopy(false)
pasteComponent("below")
}
const deleteComponent = async () => {
try {
await store.actions.components.delete(component)
} catch (error) {
notifications.error("Error deleting component")
}
}
const storeComponentForCopy = (cut = false) => {
store.actions.components.copy(component, cut)
}
const pasteComponent = mode => {
try {
store.actions.components.paste(component, mode)
} catch (error) {
notifications.error("Error saving component")
}
} }
</script> </script>
{#if showMenu}
<ActionMenu> <ActionMenu>
<div slot="control" class="icon"> <div slot="control" class="icon">
<Icon size="S" hoverable name="MoreSmallList" /> <Icon size="S" hoverable name="MoreSmallList" />
</div> </div>
<MenuItem icon="Delete" keyBind="Del" on:click={confirmDeleteDialog.show}> <MenuItem
icon="Delete"
keyBind="Del"
on:click={() => keyboardEvent("Delete")}
>
Delete Delete
</MenuItem> </MenuItem>
<MenuItem <MenuItem
noClose
icon="ChevronUp" icon="ChevronUp"
keyBind="Ctrl+↑" keyBind="Ctrl+↑"
on:click={moveUpComponent} on:click={() => keyboardEvent("ArrowUp", true)}
> >
Move up Move up
</MenuItem> </MenuItem>
<MenuItem <MenuItem
noClose
icon="ChevronDown" icon="ChevronDown"
keyBind="Ctrl+↓" keyBind="Ctrl+↓"
on:click={moveDownComponent} on:click={() => keyboardEvent("ArrowDown", true)}
> >
Move down Move down
</MenuItem> </MenuItem>
<MenuItem <MenuItem
noClose
icon="Duplicate" icon="Duplicate"
keyBind="Ctrl+D" keyBind="Ctrl+D"
on:click={duplicateComponent} on:click={() => keyboardEvent("d", true)}
> >
Duplicate Duplicate
</MenuItem> </MenuItem>
<MenuItem <MenuItem
icon="Cut" icon="Cut"
keyBind="Ctrl+X" keyBind="Ctrl+X"
on:click={() => storeComponentForCopy(true)} on:click={() => keyboardEvent("x", true)}
> >
Cut Cut
</MenuItem> </MenuItem>
<MenuItem <MenuItem
icon="Copy" icon="Copy"
keyBind="Ctrl+C" keyBind="Ctrl+C"
on:click={() => storeComponentForCopy(false)} on:click={() => keyboardEvent("c", true)}
> >
Copy Copy
</MenuItem> </MenuItem>
<MenuItem <MenuItem
icon="LayersSendToBack" icon="LayersSendToBack"
keyBind="Ctrl+V" keyBind="Ctrl+V"
on:click={() => pasteComponent("inside")} on:click={() => keyboardEvent("v", true)}
disabled={noPaste} disabled={noPaste}
> >
Paste Paste
</MenuItem> </MenuItem>
</ActionMenu> </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}
<style> <style>
.icon { .icon {