allow renaming of layouts

This commit is contained in:
Martin McKeaveney 2020-12-05 09:43:00 +00:00
parent 64c4f0d300
commit 40f6093198
3 changed files with 43 additions and 11 deletions

View File

@ -238,15 +238,21 @@ export const getFrontendStore = () => {
if (!json.ok) throw new Error("Error updating layout") if (!json.ok) throw new Error("Error updating layout")
store.update(state => { store.update(state => {
const layoutToUpdate = state.layouts.find( layoutToSave._rev = json.rev
stateLayout => stateLayout._id === layout._id layoutToSave._id = json.id
const layoutIdx = state.layouts.findIndex(
stateLayout => stateLayout._id === layoutToSave._id
) )
if (layoutToUpdate) {
layoutToUpdate._rev = json.rev if (layoutIdx >= 0) {
// update existing layout
state.layouts.splice(layoutIdx, 1, layoutToSave)
} else { } else {
// TODO: when a new layout is created // save new layout
state.layouts.push({}) state.layouts.push(layoutToSave)
} }
return state return state
}) })
}, },

View File

@ -3,16 +3,17 @@
import { store } from "builderStore" import { store } from "builderStore"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { DropdownMenu, Modal, ModalContent } from "@budibase/bbui" import { DropdownMenu, Modal, ModalContent, Input } from "@budibase/bbui"
import { DropdownContainer, DropdownItem } from "components/common/Dropdowns" import { DropdownContainer, DropdownItem } from "components/common/Dropdowns"
import { cloneDeep } from "lodash/fp"
export let layoutId export let layout
let confirmDeleteDialog let confirmDeleteDialog
let editLayoutNameModal
let dropdown let dropdown
let anchor let anchor
let name = layout.name
$: layout = $store.layouts.find(layout => layout._id === layoutId)
const deleteLayout = async () => { const deleteLayout = async () => {
try { try {
@ -22,6 +23,17 @@
notifier.danger(`Error deleting layout: ${err.message}`) notifier.danger(`Error deleting layout: ${err.message}`)
} }
} }
const saveLayout = async () => {
try {
const layoutToSave = cloneDeep(layout)
layoutToSave.name = name
await store.actions.layouts.save(layoutToSave)
notifier.success(`Layout saved successfully.`)
} catch (err) {
notifier.danger(`Error saving layout: ${err.message}`)
}
}
</script> </script>
<div bind:this={anchor} on:click|stopPropagation> <div bind:this={anchor} on:click|stopPropagation>
@ -34,6 +46,10 @@
icon="ri-delete-bin-line" icon="ri-delete-bin-line"
title="Delete" title="Delete"
on:click={() => confirmDeleteDialog.show()} /> on:click={() => confirmDeleteDialog.show()} />
<DropdownItem
icon="ri-pencil-line"
title="Edit"
on:click={() => editLayoutNameModal.show()} />
</DropdownContainer> </DropdownContainer>
</DropdownMenu> </DropdownMenu>
</div> </div>
@ -44,6 +60,16 @@
okText="Delete Layout" okText="Delete Layout"
onOk={deleteLayout} /> onOk={deleteLayout} />
<Modal bind:this={editLayoutNameModal}>
<ModalContent
title="Edit Layout Name"
confirmText="Save"
onConfirm={saveLayout}
disabled={!name}>
<Input thin type="text" label="Name" bind:value={name} />
</ModalContent>
</Modal>
<style> <style>
.icon i { .icon i {
font-size: 16px; font-size: 16px;

View File

@ -30,7 +30,7 @@
selected={$store.currentComponentInfo?._id === layout.props._id} selected={$store.currentComponentInfo?._id === layout.props._id}
opened={$store.currentAssetId === layout._id} opened={$store.currentAssetId === layout._id}
on:click={selectLayout}> on:click={selectLayout}>
<LayoutDropdownMenu layoutId={layout._id} /> <LayoutDropdownMenu {layout} />
</NavItem> </NavItem>
{#if $store.currentAssetId === layout._id && layout.props._children} {#if $store.currentAssetId === layout._id && layout.props._children}