From d9ceee49a1d212e8217869a7aa9e3bd22976fb1f Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Wed, 19 Feb 2020 10:17:31 +0000 Subject: [PATCH] reording and duplication of components --- packages/builder/src/builderStore/store.js | 82 +++++++++++++++++-- .../src/common/Icons/ChevronDown.svelte | 8 ++ .../builder/src/common/Icons/ChevronUp.svelte | 8 ++ packages/builder/src/common/Icons/Copy.svelte | 8 ++ packages/builder/src/common/Icons/index.js | 3 + .../userInterface/ComponentsHierarchy.svelte | 5 +- .../ComponentsHierarchyChildren.svelte | 73 ++++++++++++++--- .../userInterface/UserInterfaceRoot.svelte | 5 +- 8 files changed, 170 insertions(+), 22 deletions(-) create mode 100644 packages/builder/src/common/Icons/ChevronDown.svelte create mode 100644 packages/builder/src/common/Icons/ChevronUp.svelte create mode 100644 packages/builder/src/common/Icons/Copy.svelte diff --git a/packages/builder/src/builderStore/store.js b/packages/builder/src/builderStore/store.js index 8d2456abb0..70207314ac 100644 --- a/packages/builder/src/builderStore/store.js +++ b/packages/builder/src/builderStore/store.js @@ -33,6 +33,7 @@ import { loadLibs, loadLibUrls } from "./loadComponentLibraries" import { buildCodeForScreens } from "./buildCodeForScreens" import { generate_screen_css } from "./generate_css" import { insertCodeMetadata } from "./insertCodeMetadata" +import { uuid } from "./uuid" let appname = "" @@ -107,6 +108,9 @@ export const getStore = () => { store.setComponentCode = setComponentCode(store) store.setScreenType = setScreenType(store) store.deleteComponent = deleteComponent(store) + store.moveUpComponent = moveUpComponent(store) + store.moveDownComponent = moveDownComponent(store) + store.copyComponent = copyComponent(store) return store } @@ -815,13 +819,7 @@ const setScreenType = store => type => { const deleteComponent = store => component => { store.update(s => { - let parent - walkProps(s.currentPreviewItem.props, (p, breakWalk) => { - if (p._children.includes(component)) { - parent = p - breakWalk() - } - }) + const parent = getParent(s.currentPreviewItem.props, component) if (parent) { parent._children = parent._children.filter(c => c !== component) @@ -835,6 +833,76 @@ const deleteComponent = store => component => { }) } +const moveUpComponent = store => component => { + store.update(s => { + const parent = getParent(s.currentPreviewItem.props, component) + + if (parent) { + const currentIndex = parent._children.indexOf(component) + if (currentIndex === 0) return s + + const newChildren = parent._children.filter(c => c !== component) + newChildren.splice(currentIndex - 1, 0, component) + parent._children = newChildren + } + s.currentComponentInfo = component + s.currentFrontEndType === "page" + ? _savePage(s) + : _saveScreenApi(s.currentPreviewItem, s) + + return s + }) +} + +const moveDownComponent = store => component => { + store.update(s => { + const parent = getParent(s.currentPreviewItem.props, component) + + if (parent) { + const currentIndex = parent._children.indexOf(component) + if (currentIndex === parent._children.length - 1) return s + + const newChildren = parent._children.filter(c => c !== component) + newChildren.splice(currentIndex + 1, 0, component) + parent._children = newChildren + } + s.currentComponentInfo = component + s.currentFrontEndType === "page" + ? _savePage(s) + : _saveScreenApi(s.currentPreviewItem, s) + + return s + }) +} + +const copyComponent = store => component => { + store.update(s => { + const parent = getParent(s.currentPreviewItem.props, component) + const copiedComponent = cloneDeep(component) + walkProps(copiedComponent, p => { + p._id = uuid() + }) + parent._children = [...parent._children, copiedComponent] + s.curren + s.currentFrontEndType === "page" + ? _savePage(s) + : _saveScreenApi(s.currentPreviewItem, s) + s.currentComponentInfo = copiedComponent + return s + }) +} + +const getParent = (rootProps, child) => { + let parent + walkProps(rootProps, (p, breakWalk) => { + if (p._children.includes(child)) { + parent = p + breakWalk() + } + }) + return parent +} + const walkProps = (props, action, cancelToken = null) => { cancelToken = cancelToken || { cancelled: false } action(props, () => { diff --git a/packages/builder/src/common/Icons/ChevronDown.svelte b/packages/builder/src/common/Icons/ChevronDown.svelte new file mode 100644 index 0000000000..4e678136ac --- /dev/null +++ b/packages/builder/src/common/Icons/ChevronDown.svelte @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/packages/builder/src/common/Icons/ChevronUp.svelte b/packages/builder/src/common/Icons/ChevronUp.svelte new file mode 100644 index 0000000000..8a0e3246a5 --- /dev/null +++ b/packages/builder/src/common/Icons/ChevronUp.svelte @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/packages/builder/src/common/Icons/Copy.svelte b/packages/builder/src/common/Icons/Copy.svelte new file mode 100644 index 0000000000..f8fdc25663 --- /dev/null +++ b/packages/builder/src/common/Icons/Copy.svelte @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/packages/builder/src/common/Icons/index.js b/packages/builder/src/common/Icons/index.js index 7258d4c90c..353985a59e 100644 --- a/packages/builder/src/common/Icons/index.js +++ b/packages/builder/src/common/Icons/index.js @@ -8,3 +8,6 @@ export { default as CircleIndicator } from "./CircleIndicator.svelte" export { default as PencilIcon } from "./Pencil.svelte" export { default as EventsIcon } from "./Events.svelte" export { default as XCircleIcon } from "./XCircle.svelte" +export { default as ChevronUpIcon } from "./ChevronUp.svelte" +export { default as ChevronDownIcon } from "./ChevronDown.svelte" +export { default as CopyIcon } from "./Copy.svelte" diff --git a/packages/builder/src/userInterface/ComponentsHierarchy.svelte b/packages/builder/src/userInterface/ComponentsHierarchy.svelte index 274615c9cc..4df058e455 100644 --- a/packages/builder/src/userInterface/ComponentsHierarchy.svelte +++ b/packages/builder/src/userInterface/ComponentsHierarchy.svelte @@ -74,7 +74,10 @@ components={screen.component.props._children} currentComponent={$store.currentComponentInfo} onSelect={store.selectComponent} - onDeleteComponent={confirmDeleteComponent}/> + onDeleteComponent={confirmDeleteComponent} + onMoveUpComponent={store.moveUpComponent} + onMoveDownComponent={store.moveDownComponent} + onCopyComponent={store.copyComponent}/> {/if} {/each} diff --git a/packages/builder/src/userInterface/ComponentsHierarchyChildren.svelte b/packages/builder/src/userInterface/ComponentsHierarchyChildren.svelte index 7b0b326a94..6e1de3db6f 100644 --- a/packages/builder/src/userInterface/ComponentsHierarchyChildren.svelte +++ b/packages/builder/src/userInterface/ComponentsHierarchyChildren.svelte @@ -1,13 +1,16 @@