Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Commit

Permalink
Store layout (full screen mode and panel visibility) into permalink s…
Browse files Browse the repository at this point in the history
…o it can be persisted across refresh and also shared to others.

closes #180 #200
  • Loading branch information
Tim Kindberg committed Jun 20, 2016
1 parent a449f76 commit d0ed51a
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import renderStorybookUI, { Provider } from '../';
import renderStorybookUI from '../';
import { expect } from 'chai';
import sinon from 'sinon';
const { describe, it } = global;

describe('Main API', () => {
describe('default export', (done) => {
describe('default export', () => {
it('should fail if provider is not extended from the base Provider', () => {
const run = () => {
const fakeProvider = {};
Expand All @@ -13,5 +12,5 @@ describe('Main API', () => {

expect(run).to.throw(/base Provider/);
});
})
});
});
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import uiModule from './modules/ui';
import providerModule from './modules/provider';

export class Provider {
renderPreview(selectedKind, selectedStory) {
renderPreview(selectedKind, selectedStory) { // eslint-disable-line no-unused-vars
throw new Error('Provider.enderPreview() is not implemented!');
}

handleAPI(api) {
handleAPI(api) { // eslint-disable-line no-unused-vars
throw new Error('Provider.handleAPI() is not implemented!');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/provider/configs/init_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (provider, reduxStore, actions) {
const { api } = reduxStore.getState();
if (!api) return;
if (!providerApi._onStoryCallback) return;

providerApi._onStoryCallback(api.selectedKind, api.selectedStory);
});
}
1 change: 1 addition & 0 deletions src/modules/shortcuts/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import shortcuts from './shortcuts';

export const types = {
HANDLE_EVENT: 'SHORTCUTS_HANDLE_EVENT',
SET_LAYOUT: 'SHORTCUTS_SET_LAYOUT',
};

export default {
Expand Down
8 changes: 8 additions & 0 deletions src/modules/shortcuts/actions/shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ export default {
});
}
},

setLayout(context, layout) {
const { reduxStore } = context;
reduxStore.dispatch({
type: types.SET_LAYOUT,
layout,
});
},
};
7 changes: 7 additions & 0 deletions src/modules/shortcuts/configs/reducers/shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ export default function (state = defaultState, action) {
};
}

case types.SET_LAYOUT: {
return {
...state,
...action.layout,
};
}

default:
return state;
}
Expand Down
28 changes: 25 additions & 3 deletions src/modules/ui/configs/__tests__/handle_routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@ describe('manager.ui.config.handle_routing', () => {
changeUrl(null);
config.insidePopState = false;
});

it('should put the correct URL and state to pushState', (done) => {
const reduxState = {
api: {
selectedKind: 'kk',
selectedStory: 'ss',
},
shortcuts: {
goFullScreen: false,
showDownPanel: true,
showLeftPanel: true,
},
};

const reduxStore = {
getState: () => reduxState,
};

const pushState = {
url: '?selectedKind=kk&selectedStory=ss',
url: '?selectedKind=kk&selectedStory=ss&full=0&down=1&left=1',
selectedKind: 'kk',
selectedStory: 'ss',
full: false,
down: true,
left: true,
};

const originalPushState = window.history.pushState;
Expand All @@ -46,15 +55,28 @@ describe('manager.ui.config.handle_routing', () => {
api: {
selectStory: sinon.mock(),
},
shortcuts: {
setLayout: sinon.mock(),
},
};

const url = '?selectedKind=kk&selectedStory=ss&full=1&down=0&left=0';

const location = {
search: '?selectedKind=kk&selectedStory=ss',
search: url,
};
window.location.search = '?selectedKind=kk&selectedStory=ss';
window.location.search = url;
handleInitialUrl(actions, location);

expect(actions.api.selectStory.callCount).to.be.equal(1);
expect(actions.shortcuts.setLayout.callCount).to.be.equal(1);
/* eslint-disable no-unused-expressions */
expect(actions.shortcuts.setLayout.calledWith({
goFullScreen: true,
showDownPanel: false,
showLeftPanel: false,
})).to.be.true;
/* eslint-enable no-unused-expressions */
});
});
});
25 changes: 22 additions & 3 deletions src/modules/ui/configs/handle_routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,48 @@ export function changeUrl(reduxStore) {
// Do not change the URL if we are inside a popState event.
if (config.insidePopState) return;

const { api } = reduxStore.getState();
const { api, shortcuts } = reduxStore.getState();
if (!api) return;

const { selectedKind, selectedStory } = api;
const queryString = qs.stringify({ selectedKind, selectedStory });

if (queryString === '') return;

const url = `?${queryString}`;
const { goFullScreen: full, showDownPanel: down, showLeftPanel: left } = shortcuts;
const layoutQuery = qs.stringify({ full: Number(full), down: Number(down), left: Number(left) });

const url = `?${queryString}&${layoutQuery}`;
const state = {
url,
selectedKind,
selectedStory,
full,
down,
left,
};

window.history.pushState(state, '', url);
}

export function updateStore(queryParams, actions) {
const { selectedKind, selectedStory } = queryParams;
const {
selectedKind,
selectedStory,
full,
down,
left,
} = queryParams;

if (selectedKind && selectedStory) {
actions.api.selectStory(selectedKind, selectedStory);
}

actions.shortcuts.setLayout({
goFullScreen: Boolean(Number(full)),
showDownPanel: Boolean(Number(down)),
showLeftPanel: Boolean(Number(left)),
});
}

export function handleInitialUrl(actions, location) {
Expand Down

0 comments on commit d0ed51a

Please sign in to comment.