From f6b86d2d3370e05241f51cfe535b2a38c9fc0641 Mon Sep 17 00:00:00 2001 From: Josh Ponelat Date: Wed, 18 Oct 2017 13:07:38 +0200 Subject: [PATCH] Refactor layout .show/.isShown to handle branch nodes --- src/core/plugins/layout/actions.js | 14 -------------- src/core/plugins/layout/reducers.js | 13 +++++++++---- src/core/plugins/layout/selectors.js | 4 ++-- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/core/plugins/layout/actions.js b/src/core/plugins/layout/actions.js index d65d34e3..987395b0 100644 --- a/src/core/plugins/layout/actions.js +++ b/src/core/plugins/layout/actions.js @@ -37,17 +37,3 @@ export function changeMode(thing, mode="") { payload: {thing, mode} } } - - -// export function onlyShow(thing, shown=true) { -// thing = normalizeArray(thing) -// if(thing.length < 2) -// throw new Error("layoutActions.onlyShow only works, when `thing` is an array with length > 1") -// return { -// type: ONLY_SHOW, -// payload: {thing, shown} -// } -// } - - - diff --git a/src/core/plugins/layout/reducers.js b/src/core/plugins/layout/reducers.js index 4b561015..aa067abd 100644 --- a/src/core/plugins/layout/reducers.js +++ b/src/core/plugins/layout/reducers.js @@ -1,3 +1,4 @@ +import { fromJS } from "immutable" import { UPDATE_LAYOUT, UPDATE_FILTER, @@ -12,9 +13,14 @@ export default { [UPDATE_FILTER]: (state, action) => state.set("filter", action.payload), [SHOW]: (state, action) => { - let thing = action.payload.thing - let shown = action.payload.shown - return state.setIn(["shown"].concat(thing), shown) + const isShown = action.payload.shown + // This is one way to serialize an array, another (preferred) is to convert to json-pointer + // TODO: use json-pointer serilization instead of fromJS(...), for performance + const thingToShow = fromJS(action.payload.thing) + // This is a map of paths to bools + // eg: [one, two] => true + // eg: [one] => false + return state.update("shown", fromJS({}), a => a.set(thingToShow, isShown)) }, [UPDATE_MODE]: (state, action) => { @@ -24,4 +30,3 @@ export default { } } - diff --git a/src/core/plugins/layout/selectors.js b/src/core/plugins/layout/selectors.js index 42a3bcbb..7d75d37d 100644 --- a/src/core/plugins/layout/selectors.js +++ b/src/core/plugins/layout/selectors.js @@ -1,5 +1,6 @@ import { createSelector } from "reselect" import { normalizeArray } from "core/utils" +import { fromJS } from "immutable" const state = state => state @@ -9,7 +10,7 @@ export const currentFilter = state => state.get("filter") export const isShown = (state, thing, def) => { thing = normalizeArray(thing) - return Boolean(state.getIn(["shown", ...thing], def)) + return state.get("shown", fromJS({})).get(fromJS(thing), def) } export const whatMode = (state, thing, def="") => { @@ -21,4 +22,3 @@ export const showSummary = createSelector( state, state => !isShown(state, "editor") ) -