From 28bbf9fa2eaed43dfb6530e0fa0cba3e1ef46dfb Mon Sep 17 00:00:00 2001 From: forrest Date: Mon, 17 Jan 2022 13:41:19 -0500 Subject: [PATCH] fix: properly remap views separately on state load --- src/store/index.js | 28 ++++++++++++++++++++++++---- src/store/views.js | 8 +++----- src/store/widgets.js | 2 +- src/utils.js | 15 +++++++++++++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index ec617b73..df8fbe25 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -308,7 +308,10 @@ function createStore(injected) { // make sure store modules have a chance to rewrite their saved mappings // before we re-populate proxy manager state - dispatch('rewriteProxyIds', $oldToNewIdMapping).then(() => { + dispatch('rewriteProxyIds', { + appState, + mapping: $oldToNewIdMapping, + }).then(() => { // Force update proxyManager.modified(); @@ -499,9 +502,26 @@ function createStore(injected) { dispatch('changeCameraViewPoint', keys[ind]); }, - rewriteProxyIds({ dispatch }, oldToNewMapping) { - dispatch('widgets/rewriteProxyIds', oldToNewMapping); - dispatch('views/rewriteProxyIds', oldToNewMapping); + rewriteProxyIds({ dispatch }, { appState, mapping }) { + // split out the mappings + const extractMapping = (objs) => + objs.reduce( + (map, obj) => ({ ...map, [obj.id]: mapping[obj.id] }), + {} + ); + + const sourcesMapping = extractMapping(appState.sources); + const viewsMapping = extractMapping(appState.views); + const repsMapping = extractMapping(appState.representations); + const maps = { + sources: sourcesMapping, + views: viewsMapping, + reps: repsMapping, + all: mapping, + }; + + dispatch('widgets/rewriteProxyIds', maps); + dispatch('views/rewriteProxyIds', maps); }, }, }); diff --git a/src/store/views.js b/src/store/views.js index fe058a56..7791a001 100644 --- a/src/store/views.js +++ b/src/store/views.js @@ -9,7 +9,7 @@ import { } from 'paraview-glance/src/components/core/VtkView/constants'; import { DEFAULT_BACKGROUND } from 'paraview-glance/src/components/core/VtkView/palette'; -import { wrapMutationAsAction } from 'paraview-glance/src/utils'; +import { remapIdValues, wrapMutationAsAction } from 'paraview-glance/src/utils'; const { CaptureOn } = WidgetManagerConstants; @@ -82,10 +82,8 @@ export default ({ proxyManager }) => ({ Vue.set(state.viewOrder, index, viewType); Vue.set(state.viewOrder, dstIndex, srcViewType); }, - rewriteProxyIds(state, idMapping) { - Object.entries(state.viewTypeToId).forEach(([type, id]) => { - state.viewTypeToId[type] = idMapping[id]; - }); + rewriteProxyIds(state, { views: idMapping }) { + state.viewTypeToId = remapIdValues(state.viewTypeToId, idMapping); }, }, actions: { diff --git a/src/store/widgets.js b/src/store/widgets.js index 23f918e5..82a76fe0 100644 --- a/src/store/widgets.js +++ b/src/store/widgets.js @@ -60,7 +60,7 @@ export default ({ proxyManager }) => ({ setCroppingState(state, { datasetId, planes }) { state.croppingStates[datasetId] = Array.from(planes); }, - rewriteProxyIds(state, idMapping) { + rewriteProxyIds(state, { sources: idMapping }) { state.measurements = remapIdKeys(state.measurements, idMapping); state.imageToLabelmaps = remapIdKeys(state.imageToLabelmaps, idMapping); state.labelmapStates = remapIdKeys(state.labelmapStates, idMapping); diff --git a/src/utils.js b/src/utils.js index 4be619ec..139d6ba4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -44,6 +44,21 @@ export function remapIdKeys(obj, mapping) { return newObj; } +/** + * Renames values in an object according to a lookup. + */ +export function remapIdValues(obj, mapping) { + const newObj = {}; + Object.entries(obj).forEach(([key, id]) => { + let newId = id; + if (id in mapping) { + newId = mapping[id]; + } + newObj[key] = newId; + }); + return newObj; +} + /** * Replaces elements in a list according to a mapping. */