From a00bf70d2935cc5588415213581f6b1e213857ed Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 19 Nov 2020 11:15:29 +0000 Subject: [PATCH] further store tidy up, derive selected page --- packages/builder/src/builderStore/index.js | 6 ++ .../src/builderStore/store/frontend.js | 57 ++++++++----------- .../ComponentTree.svelte | 7 +-- .../ComponentNavigationTree/PathTree.svelte | 11 ++-- .../ComponentNavigationTree/Screen.svelte | 0 .../ScreenDropdownMenu.svelte | 53 +++++++++++++++++ .../ComponentNavigationTree/dragDropStore.js | 2 +- .../ComponentNavigationTree/index.svelte | 4 -- .../ComponentsHierarchyChildren.svelte | 2 +- packages/client/src/render/screenRouter.js | 2 +- 10 files changed, 95 insertions(+), 49 deletions(-) delete mode 100644 packages/builder/src/components/userInterface/ComponentNavigationTree/Screen.svelte create mode 100644 packages/builder/src/components/userInterface/ComponentNavigationTree/ScreenDropdownMenu.svelte diff --git a/packages/builder/src/builderStore/index.js b/packages/builder/src/builderStore/index.js index 1bd86480a4..ae77889404 100644 --- a/packages/builder/src/builderStore/index.js +++ b/packages/builder/src/builderStore/index.js @@ -31,6 +31,12 @@ export const currentScreens = derived(store, $store => { : Object.values(currentScreens) }) +export const selectedPage = derived(store, $store => { + if (!$store.pages) return null + + return $store.pages[$store.currentPageName || "main"] +}) + export const initialise = async () => { try { await analytics.activate() diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 7b34013114..9c68bd814a 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -5,8 +5,7 @@ import { getBuiltin, makePropsSafe, } from "components/userInterface/pagesParsing/createProps" -import { getExactComponent } from "components/userInterface/pagesParsing/searchComponents" -import { allScreens, backendUiStore } from "builderStore" +import { allScreens, backendUiStore, selectedPage } from "builderStore" import { generate_screen_css } from "../generate_css" import { fetchComponentLibDefinitions } from "../loadComponentLibraries" import api from "../api" @@ -112,10 +111,9 @@ export const getFrontendStore = () => { store.update(state => { state.currentFrontEndType = type - const pageOrScreen = - type === "page" - ? state.pages[state.currentPageName] - : state.pages[state.currentPageName]._screens[0] + const page = get(selectedPage) + + const pageOrScreen = type === "page" ? page : page._screens[0] state.currentComponentInfo = pageOrScreen ? pageOrScreen.props : null state.currentPreviewItem = pageOrScreen @@ -159,32 +157,25 @@ export const getFrontendStore = () => { await savePromise }, save: async screen => { - const storeContents = get(store) - const pageName = storeContents.currentPageName || "main" - const currentPage = storeContents.pages[pageName] - const currentPageScreens = currentPage._screens + const page = get(selectedPage) + const currentPageScreens = page._screens const creatingNewScreen = screen._id === undefined let savePromise - const response = await api.post( - `/api/screens/${currentPage._id}`, - screen - ) + const response = await api.post(`/api/screens/${page._id}`, screen) const json = await response.json() screen._rev = json.rev screen._id = json.id - const foundScreen = currentPageScreens.findIndex( - el => el._id === screen._id - ) + const foundScreen = page._screens.findIndex(el => el._id === screen._id) if (foundScreen !== -1) { - currentPageScreens.splice(foundScreen, 1) + page._screens.splice(foundScreen, 1) } - currentPageScreens.push(screen) + page._screens.push(screen) // TODO: should carry out all server updates to screen in a single call store.update(state => { - state.pages[pageName]._screens = currentPageScreens + page._screens = currentPageScreens if (creatingNewScreen) { state.currentPreviewItem = screen @@ -210,21 +201,20 @@ export const getFrontendStore = () => { store.actions.screens.regenerateCss(currentPreviewItem) } }, - delete: async (screensToDelete, pageName) => { + delete: async screens => { let deletePromise + const screensToDelete = Array.isArray(screens) ? screens : [screens] + store.update(state => { - if (pageName == null) { - pageName = state.pages.main.name - } - for (let screenToDelete of Array.isArray(screensToDelete) - ? screensToDelete - : [screensToDelete]) { + const currentPage = get(selectedPage) + + for (let screenToDelete of screensToDelete) { // Remove screen from current page as well // TODO: Should be done server side - state.pages[pageName]._screens = state.pages[ - pageName - ]._screens.filter(scr => scr._id !== screenToDelete._id) + currentPage._screens = currentPage._screens.filter( + scr => scr._id !== screenToDelete._id + ) deletePromise = api.delete( `/api/screens/${screenToDelete._id}/${screenToDelete._rev}` ) @@ -310,14 +300,13 @@ export const getFrontendStore = () => { create: (componentToAdd, presetProps) => { store.update(state => { function findSlot(component_array) { - for (let i = 0; i < component_array.length; i += 1) { - if (component_array[i]._component === "##builtin/screenslot") { + for (let component of component_array) { + if (component._component === "##builtin/screenslot") { return true } - if (component_array[i]._children) findSlot(component_array[i]) + if (component._children) findSlot(component) } - return false } diff --git a/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte b/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte index e856908dfd..ee162f0eba 100644 --- a/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte +++ b/packages/builder/src/components/userInterface/ComponentNavigationTree/ComponentTree.svelte @@ -1,11 +1,10 @@ + +
+
dropdown.show()}> + +
+ + + confirmDeleteDialog.show()} /> + + +
+ + + diff --git a/packages/builder/src/components/userInterface/ComponentNavigationTree/dragDropStore.js b/packages/builder/src/components/userInterface/ComponentNavigationTree/dragDropStore.js index c5e7fc8736..1aa180e938 100644 --- a/packages/builder/src/components/userInterface/ComponentNavigationTree/dragDropStore.js +++ b/packages/builder/src/components/userInterface/ComponentNavigationTree/dragDropStore.js @@ -46,7 +46,7 @@ export default function() { // hovered in center of target if (mousePosition > 0.4 && mousePosition < 0.8) { - state.dropPosition = DropPosition.BELOW + state.dropPosition = DropPosition.INSIDE } return } diff --git a/packages/builder/src/components/userInterface/ComponentNavigationTree/index.svelte b/packages/builder/src/components/userInterface/ComponentNavigationTree/index.svelte index a80409dac0..0063502dd4 100644 --- a/packages/builder/src/components/userInterface/ComponentNavigationTree/index.svelte +++ b/packages/builder/src/components/userInterface/ComponentNavigationTree/index.svelte @@ -1,10 +1,6 @@