From 8b352fe40ab546ab219ceb4fa146397ade199aee Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 14 Dec 2020 11:14:16 +0000 Subject: [PATCH] Allow independent selection of screen IDs and layout IDs so that selections persist between toggling tabs. Update logic around role selection and screen filtering --- packages/builder/src/builderStore/index.js | 30 ++++++------- .../src/builderStore/store/frontend.js | 26 +++++++---- .../ComponentTree.svelte | 4 +- .../ComponentNavigationTree/PathTree.svelte | 5 +-- .../ComponentNavigationTree/index.svelte | 17 ++++--- .../ComponentSelectionList.svelte | 11 +++-- .../userInterface/FrontendNavigatePane.svelte | 45 ++++++++++++++----- .../components/userInterface/Layout.svelte | 6 +-- .../design/[assetType]/[asset]/_layout.svelte | 11 ++--- .../design/[assetType]/_layout.svelte | 5 +-- .../design/[assetType]/index.svelte | 24 +++++++++- 11 files changed, 124 insertions(+), 60 deletions(-) diff --git a/packages/builder/src/builderStore/index.js b/packages/builder/src/builderStore/index.js index f1914efab2..cf668670bb 100644 --- a/packages/builder/src/builderStore/index.js +++ b/packages/builder/src/builderStore/index.js @@ -4,7 +4,7 @@ import { getAutomationStore } from "./store/automation/" import { getThemeStore } from "./store/theme" import { derived, writable } from "svelte/store" import analytics from "analytics" -import { LAYOUT_NAMES } from "../constants" +import { FrontendTypes, LAYOUT_NAMES } from "../constants" import { makePropsSafe } from "components/userInterface/assetParsing/createProps" export const store = getFrontendStore() @@ -13,18 +13,12 @@ export const automationStore = getAutomationStore() export const themeStore = getThemeStore() export const currentAsset = derived(store, $store => { - const layout = $store.layouts - ? $store.layouts.find(layout => layout._id === $store.currentAssetId) - : null - - if (layout) return layout - - const screen = $store.screens - ? $store.screens.find(screen => screen._id === $store.currentAssetId) - : null - - if (screen) return screen - + const type = $store.currentFrontEndType + if (type === FrontendTypes.SCREEN) { + return $store.screens.find(screen => screen._id === $store.selectedScreenId) + } else if (type === FrontendTypes.LAYOUT) { + return $store.layouts.find(layout => layout._id === $store.selectedLayoutId) + } return null }) @@ -59,8 +53,14 @@ export const selectedComponent = derived( } ) -export const currentAssetName = derived(store, () => { - return currentAsset.name +export const currentAssetId = derived(store, $store => { + return $store.currentFrontEndType === FrontendTypes.SCREEN + ? $store.selectedScreenId + : $store.selectedLayoutId +}) + +export const currentAssetName = derived(currentAsset, $currentAsset => { + return $currentAsset?.name }) // leave this as before for consistency diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index df04bcf8f9..263041aadd 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -32,7 +32,8 @@ const INITIAL_FRONTEND_STATE = { screens: [], components: [], currentFrontEndType: "none", - currentAssetId: "", + selectedScreenId: "", + selectedLayoutId: "", selectedComponentId: "", errors: [], hasAppPackage: false, @@ -89,8 +90,13 @@ export const getFrontendStore = () => { let screen = screens.find(screen => screen._id === screenId) || screens[0] if (!screen) return state + + // Update role to the screen's role setting so that it will always + // be visible + selectedAccessRole.set(screen.routing.roleId) + state.currentFrontEndType = FrontendTypes.SCREEN - state.currentAssetId = screen._id + state.selectedScreenId = screen._id state.currentView = "detail" state.selectedComponentId = screen.props?._id return state @@ -99,7 +105,7 @@ export const getFrontendStore = () => { create: async screen => { screen = await store.actions.screens.save(screen) store.update(state => { - state.currentAssetId = screen._id + state.selectedScreenId = screen._id state.selectedComponentId = screen.props._id state.currentFrontEndType = FrontendTypes.SCREEN selectedAccessRole.set(screen.routing.roleId) @@ -144,8 +150,8 @@ export const getFrontendStore = () => { `/api/screens/${screenToDelete._id}/${screenToDelete._rev}` ) ) - if (screenToDelete._id === state.currentAssetId) { - state.currentAssetId = "" + if (screenToDelete._id === state.selectedScreenId) { + state.selectedScreenId = null } } return state @@ -168,11 +174,12 @@ export const getFrontendStore = () => { layouts: { select: layoutId => { store.update(state => { - const layout = store.actions.layouts.find(layoutId) + const layout = + store.actions.layouts.find(layoutId) || get(store).layouts[0] if (!layout) return state.currentFrontEndType = FrontendTypes.LAYOUT state.currentView = "detail" - state.currentAssetId = layout._id + state.selectedLayoutId = layout._id state.selectedComponentId = layout.props?._id return state }) @@ -215,16 +222,17 @@ export const getFrontendStore = () => { const response = await api.delete( `/api/layouts/${layoutToDelete._id}/${layoutToDelete._rev}` ) - if (response.status !== 200) { const json = await response.json() throw new Error(json.message) } - store.update(state => { state.layouts = state.layouts.filter( layout => layout._id !== layoutToDelete._id ) + if (layoutToDelete._id === state.selectedLayoutId) { + state.selectedLayoutId = get(mainLayout)._id + } return state }) }, diff --git a/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte b/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte index 485af84e74..320a81f00d 100644 --- a/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte +++ b/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte @@ -1,6 +1,6 @@ diff --git a/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte b/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte index 25f939d2f1..698476dcd8 100644 --- a/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte +++ b/packages/builder/src/components/userInterface/FrontendNavigatePane.svelte @@ -3,6 +3,7 @@ import { goto, params, url } from "@sveltech/routify" import { store, + allScreens, currentAsset, backendUiStore, selectedAccessRole, @@ -29,11 +30,38 @@ let routes = {} let tab = $params.assetType - function navigate({ detail }) { - if (!detail) return + const navigate = ({ detail }) => { + if (!detail) { + return + } $goto(`../${detail.heading.key}`) } + const updateAccessRole = event => { + const role = event.target.value + + // Select a valid screen with this new role - otherwise we'll not be + // able to change role at all because ComponentNavigationTree will kick us + // back the current role again because the same screen ID is still selected + const firstValidScreenId = $allScreens.find( + screen => screen.routing.roleId === role + )?._id + if (firstValidScreenId) { + store.actions.screens.select(firstValidScreenId) + } + + // Otherwise clear the selected screen ID so that the first new valid screen + // can be selected by ComponentNavigationTree + else { + store.update(state => { + state.selectedScreenId = null + return state + }) + } + + selectedAccessRole.set(role) + } + onMount(() => { store.actions.routing.fetch() }) @@ -46,24 +74,21 @@ on:click={modal.show} data-cy="new-screen" class="ri-add-circle-fill" /> -
- - {#if $currentAsset} - - {/if} + diff --git a/packages/builder/src/components/userInterface/Layout.svelte b/packages/builder/src/components/userInterface/Layout.svelte index 37593dedf7..77bab330c5 100644 --- a/packages/builder/src/components/userInterface/Layout.svelte +++ b/packages/builder/src/components/userInterface/Layout.svelte @@ -28,13 +28,13 @@ icon="ri-layout-3-line" text={layout.name} withArrow - selected={$store.currentAssetId === layout._id} - opened={$store.currentAssetId === layout._id} + selected={$store.selectedLayoutId === layout._id} + opened={$store.selectedLayoutId === layout._id} on:click={selectLayout}> -{#if $store.currentAssetId === layout._id && layout.props?._children} +{#if $store.selectedLayoutId === layout._id && layout.props?._children} - asset._id === $params.asset || - asset._id === decodeURIComponent($params.asset) - ).props._children + const assetChildren = + assetList.find( + asset => + asset._id === $params.asset || + asset._id === decodeURIComponent($params.asset) + )?.props._children ?? [] findComponent(componentIds, assetChildren) } // } diff --git a/packages/builder/src/pages/[application]/design/[assetType]/_layout.svelte b/packages/builder/src/pages/[application]/design/[assetType]/_layout.svelte index f95760ec53..b797a18dc4 100644 --- a/packages/builder/src/pages/[application]/design/[assetType]/_layout.svelte +++ b/packages/builder/src/pages/[application]/design/[assetType]/_layout.svelte @@ -1,11 +1,10 @@