diff --git a/src/components/WorkspaceExport.js b/src/components/WorkspaceExport.js index 1bc968fe519b937640fd0085501f633bbb178768..48559d65f0a6136fab6a38d5264eb5d542b0f218 100644 --- a/src/components/WorkspaceExport.js +++ b/src/components/WorkspaceExport.js @@ -13,10 +13,11 @@ export class WorkspaceExport extends Component { */ exportableState() { const { state } = this.props; - const { config, windows } = state; + const { config, viewers, windows } = state; return JSON.stringify({ config, + viewers, windows, }, null, 2); } diff --git a/src/containers/App.js b/src/containers/App.js index afd53d574b32e3c23ae3ffd94e0d26397692c83e..441db5210b216b6b30597713fdcbb55e9572466a 100644 --- a/src/containers/App.js +++ b/src/containers/App.js @@ -12,7 +12,6 @@ import { App } from '../components/App'; */ const mapStateToProps = state => ( { - errors: state.errors, isFullscreenEnabled: state.workspace.isFullscreenEnabled, language: state.config.language, theme: state.config.theme, @@ -26,7 +25,6 @@ const mapStateToProps = state => ( * @private */ const mapDispatchToProps = { - removeError: actions.removeError, setWorkspaceFullscreen: actions.setWorkspaceFullscreen, }; diff --git a/src/containers/WorkspaceImport.js b/src/containers/WorkspaceImport.js index 0427172995ed7dc84e20fa18b672f6e3660f0cf1..6a9552a478d36cfe04f8eecdf759a9b5cd1b6f4f 100644 --- a/src/containers/WorkspaceImport.js +++ b/src/containers/WorkspaceImport.js @@ -5,13 +5,6 @@ import { withPlugins } from '../extend'; import WorkspaceImport from '../components/WorkspaceImport'; import * as actions from '../state/actions'; -/** - * mapStateToProps - to hook up connect - * @memberof WorkspaceImport - * @private - */ -const mapStateToProps = state => ({ state }); - /** * mapDispatchToProps - used to hook up connect to action creators * @memberof App @@ -23,7 +16,7 @@ const mapDispatchToProps = { }; const enhance = compose( - connect(mapStateToProps, mapDispatchToProps), + connect(null, mapDispatchToProps), withTranslation(), withPlugins('WorkspaceImport'), ); diff --git a/src/state/actions/workspace.js b/src/state/actions/workspace.js index c079edc7607644201ef6c6f5b1a45d703e636714..1ff7a5ae814736024713a8239645b05fb98c7396 100644 --- a/src/state/actions/workspace.js +++ b/src/state/actions/workspace.js @@ -1,4 +1,4 @@ -import _ from 'lodash'; +import { difference, values } from 'lodash'; import ActionTypes from './action-types'; import { importConfig } from './config'; import { removeWindow, addWindow, updateWindow } from './window'; @@ -97,33 +97,33 @@ export function toggleWorkspaceExposeMode() { export function importWorkspace(stateExport) { return (dispatch, getState) => { dispatch(importConfig(stateExport.config)); - const { viewers } = stateExport; - const newWindows = stateExport.windows; - const newWindowsKeys = _.keys(newWindows); - const newWindowsCnt = newWindowsKeys.length; + const { viewers } = stateExport || {}; + const imWins = values(stateExport.windows); - const existingWindows = getState().windows; - const existingWindowsKeys = _.keys(existingWindows); - const existingWindowsCnt = existingWindowsKeys.length; + /* re-use existing windows */ + const exIds = values(getState().windows).map((exWin) => { + const imWin = imWins.shift(); + const viewer = viewers[imWin.id]; + delete imWin.id; - let currentKey = ''; - // re-use existing windows - for (let i = 0; i < newWindowsCnt; i++) { // eslint-disable-line no-plusplus - dispatch(fetchManifest(newWindows[newWindowsKeys[i]].manifestId)); - if (i < existingWindowsCnt) { - currentKey = existingWindowsKeys[i]; - delete newWindows[newWindowsKeys[i]].id; - dispatch(updateWindow(existingWindowsKeys[i], newWindows[newWindowsKeys[i]])); - } else { - dispatch(addWindow(newWindows[newWindowsKeys[i]])); - currentKey = newWindowsKeys[i]; - } - dispatch(updateViewport(currentKey, viewers[newWindowsKeys[i]])); - } + dispatch(fetchManifest(imWin.manifestId)); + dispatch(updateWindow(exWin.id, imWin)); + dispatch(updateViewport(exWin.id, viewer)); - // clean up surplus windows if there are any - if (existingWindowsCnt > newWindowsCnt) { - Object.keys(existingWindows).slice(newWindowsCnt).map(windowId => dispatch(removeWindow(windowId))); // eslint-disable-line max-len - } + return exWin.id; + }); + + /* create new windows for additionally imported ones */ + const imIds = imWins.map((imWin) => { + dispatch(fetchManifest(imWin.manifestId)); + dispatch(addWindow(imWin)); + dispatch(updateViewport(imWin.id, viewers[imWin.id])); + + return imWin.id; + }); + + /* close surplus windows */ + difference(getState().windows, exIds.concat(imIds)) + .map(winId => dispatch(removeWindow(winId))); }; }