Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor all startup code into @storybook/core also #3259

Merged
merged 2 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"react": "^16.0.0",
"react-dev-utils": "^5.0.0",
"react-dom": "^16.0.0",
"redux": "^3.6.0",
"sass-loader": "^6.0.7",
"ts-loader": "^3.5.0",
"uglifyjs-webpack-plugin": "^1.2.4",
Expand Down
63 changes: 4 additions & 59 deletions app/angular/src/client/preview/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
import { window, navigator } from 'global';
import { createStore } from 'redux';
import addons from '@storybook/addons';
import createChannel from '@storybook/channel-postmessage';
import { handleKeyboardShortcuts } from '@storybook/ui/dist/libs/key_events';
import {
StoryStore,
ClientApi,
ConfigApi,
Actions,
reducer,
syncUrlWithStore,
} from '@storybook/core/client';
import { start } from '@storybook/core/client';

import render from './render';

// check whether we're running on node/browser
const isBrowser =
navigator &&
navigator.userAgent &&
navigator.userAgent !== 'storyshots' &&
!(navigator.userAgent.indexOf('Node.js') > -1) &&
!(navigator.userAgent.indexOf('jsdom') > -1);
const { clientApi, configApi, forceReRender } = start(render);

const storyStore = new StoryStore();
const reduxStore = createStore(reducer);
const context = { storyStore, reduxStore };
const clientApi = new ClientApi(context);
export const {
storiesOf,
setAddon,
Expand All @@ -34,39 +13,5 @@ export const {
getStorybook,
} = clientApi;

let channel;
if (isBrowser) {
// create preview channel
channel = createChannel({ page: 'preview' });
channel.on('setCurrentStory', data => {
reduxStore.dispatch(Actions.selectStory(data.kind, data.story));
});
addons.setChannel(channel);
Object.assign(context, { channel });

syncUrlWithStore(reduxStore);

// Handle keyboard shortcuts
window.onkeydown = handleKeyboardShortcuts(channel);
}

// Provide access to external scripts if `window` is defined.
// NOTE this is different to isBrowser, primarily for the JSDOM use case
if (typeof window !== 'undefined') {
window.__STORYBOOK_CLIENT_API__ = clientApi;
window.__STORYBOOK_ADDONS_CHANNEL__ = channel; // may not be defined
}

const configApi = new ConfigApi({ ...context, clearDecorators });
export const { configure } = configApi;

// initialize the UI
const renderUI = () => {
if (isBrowser) {
render(context);
}
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
export { forceReRender };
1 change: 0 additions & 1 deletion app/polymer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"react": "^16.0.0",
"react-dev-utils": "^5.0.0",
"react-dom": "^16.0.0",
"redux": "^3.7.2",
"uglifyjs-webpack-plugin": "^1.2.4",
"util-deprecate": "^1.0.2",
"webpack": "^3.11.0",
Expand Down
63 changes: 3 additions & 60 deletions app/polymer/src/client/preview/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { createStore } from 'redux';
import addons from '@storybook/addons';
import { navigator, window } from 'global';
import createChannel from '@storybook/channel-postmessage';
import { handleKeyboardShortcuts } from '@storybook/ui/dist/libs/key_events';
import {
StoryStore,
ClientApi,
ConfigApi,
Actions,
reducer,
syncUrlWithStore,
} from '@storybook/core/client';
import { start } from '@storybook/core/client';

import render from './render';

// check whether we're running on node/browser
const isBrowser =
navigator &&
navigator.userAgent &&
navigator.userAgent !== 'storyshots' &&
!(navigator.userAgent.indexOf('Node.js') > -1) &&
!(navigator.userAgent.indexOf('jsdom') > -1);
const { clientApi, configApi, forceReRender } = start(render);

const storyStore = new StoryStore();
const reduxStore = createStore(reducer);
const context = { storyStore, reduxStore };

const clientApi = new ClientApi(context);
export const {
storiesOf,
setAddon,
Expand All @@ -36,39 +13,5 @@ export const {
getStorybook,
} = clientApi;

let channel;
if (isBrowser) {
// setup preview channel
channel = createChannel({ page: 'preview' });
channel.on('setCurrentStory', data => {
reduxStore.dispatch(Actions.selectStory(data.kind, data.story));
});
addons.setChannel(channel);
Object.assign(context, { channel });

syncUrlWithStore(reduxStore);

// Handle keyboard shortcuts
window.onkeydown = handleKeyboardShortcuts(channel);
}

// Provide access to external scripts if `window` is defined.
// NOTE this is different to isBrowser, primarily for the JSDOM use case
if (typeof window !== 'undefined') {
window.__STORYBOOK_CLIENT_API__ = clientApi;
window.__STORYBOOK_ADDONS_CHANNEL__ = channel; // may not be defined
}

const configApi = new ConfigApi({ clearDecorators, ...context });
export const { configure } = configApi;

// initialize the UI
const renderUI = () => {
if (isBrowser) {
render(context);
}
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
export { forceReRender };
1 change: 0 additions & 1 deletion app/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"markdown-loader": "^2.0.2",
"prop-types": "^15.6.1",
"react-dev-utils": "^5.0.0",
"redux": "^3.7.2",
"uglifyjs-webpack-plugin": "^1.2.4",
"util-deprecate": "^1.0.2",
"webpack": "^3.11.0",
Expand Down
63 changes: 3 additions & 60 deletions app/react/src/client/preview/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { createStore } from 'redux';
import addons from '@storybook/addons';
import { navigator, window } from 'global';
import createChannel from '@storybook/channel-postmessage';
import { handleKeyboardShortcuts } from '@storybook/ui/dist/libs/key_events';
import {
StoryStore,
ClientApi,
ConfigApi,
Actions,
reducer,
syncUrlWithStore,
} from '@storybook/core/client';
import { start } from '@storybook/core/client';

import render from './render';

// check whether we're running on node/browser
const isBrowser =
navigator &&
navigator.userAgent &&
navigator.userAgent !== 'storyshots' &&
!(navigator.userAgent.indexOf('Node.js') > -1) &&
!(navigator.userAgent.indexOf('jsdom') > -1);
const { clientApi, configApi, forceReRender } = start(render);

const storyStore = new StoryStore();
const reduxStore = createStore(reducer);
const context = { storyStore, reduxStore };

const clientApi = new ClientApi(context);
export const {
storiesOf,
setAddon,
Expand All @@ -36,39 +13,5 @@ export const {
getStorybook,
} = clientApi;

let channel;
if (isBrowser) {
// setup preview channel
channel = createChannel({ page: 'preview' });
channel.on('setCurrentStory', data => {
reduxStore.dispatch(Actions.selectStory(data.kind, data.story));
});
addons.setChannel(channel);
Object.assign(context, { channel });

syncUrlWithStore(reduxStore);

// Handle keyboard shortcuts
window.onkeydown = handleKeyboardShortcuts(channel);
}

// Provide access to external scripts if `window` is defined.
// NOTE this is different to isBrowser, primarily for the JSDOM use case
if (typeof window !== 'undefined') {
window.__STORYBOOK_CLIENT_API__ = clientApi;
window.__STORYBOOK_ADDONS_CHANNEL__ = channel; // may not be defined
}

const configApi = new ConfigApi({ clearDecorators, ...context });
export const { configure } = configApi;

// initialize the UI
const renderUI = () => {
if (isBrowser) {
render(context);
}
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
export { forceReRender };
1 change: 0 additions & 1 deletion app/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"react": "^16.2.0",
"react-dev-utils": "^5.0.0",
"react-dom": "^16.2.0",
"redux": "^3.7.2",
"uglifyjs-webpack-plugin": "^1.2.4",
"util-deprecate": "^1.0.2",
"webpack": "^3.11.0",
Expand Down
66 changes: 5 additions & 61 deletions app/vue/src/client/preview/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
import { createStore } from 'redux';
import addons from '@storybook/addons';
import createChannel from '@storybook/channel-postmessage';
import { navigator, window } from 'global';
import { handleKeyboardShortcuts } from '@storybook/ui/dist/libs/key_events';
import {
StoryStore,
ClientApi,
ConfigApi,
Actions,
reducer,
syncUrlWithStore,
} from '@storybook/core/client';
import { start } from '@storybook/core/client';

import render from './render';

// check whether we're running on node/browser
const isBrowser =
navigator &&
navigator.userAgent &&
navigator.userAgent !== 'storyshots' &&
!(navigator.userAgent.indexOf('Node.js') > -1) &&
!(navigator.userAgent.indexOf('jsdom') > -1);

const storyStore = new StoryStore();
const reduxStore = createStore(reducer);

const createWrapperComponent = Target => ({
functional: true,
render(h, c) {
Expand All @@ -42,8 +19,9 @@ const decorateStory = (getStory, decorators) =>
},
getStory
);
const context = { storyStore, reduxStore, decorateStory };
const clientApi = new ClientApi(context);

const { clientApi, configApi, forceReRender } = start(render, { decorateStory });

export const {
storiesOf,
setAddon,
Expand All @@ -53,39 +31,5 @@ export const {
getStorybook,
} = clientApi;

let channel;
if (isBrowser) {
// create preview channel
channel = createChannel({ page: 'preview' });
channel.on('setCurrentStory', data => {
reduxStore.dispatch(Actions.selectStory(data.kind, data.story));
});
addons.setChannel(channel);
Object.assign(context, { channel });

syncUrlWithStore(reduxStore);

// Handle keyboard shortcuts
window.onkeydown = handleKeyboardShortcuts(channel);
}

// Provide access to external scripts if `window` is defined.
// NOTE this is different to isBrowser, primarily for the JSDOM use case
if (typeof window !== 'undefined') {
window.__STORYBOOK_CLIENT_API__ = clientApi;
window.__STORYBOOK_ADDONS_CHANNEL__ = channel; // may not be defined
}

const configApi = new ConfigApi({ ...context, clearDecorators });
export const { configure } = configApi;

// initialize the UI
const renderUI = () => {
if (isBrowser) {
render(context);
}
};

reduxStore.subscribe(renderUI);

export const forceReRender = () => render(context, true);
export { forceReRender };
1 change: 1 addition & 0 deletions lib/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"qs": "^6.5.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"redux": "^3.7.2",
"serve-favicon": "^2.4.5",
"shelljs": "^0.8.1",
"style-loader": "^0.20.3",
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/client/preview/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import start from './start';
import * as Actions from './actions';
import ClientApi from './client_api';
import ConfigApi from './config_api';
import StoryStore from './story_store';
import reducer from './reducer';
import syncUrlWithStore from './syncUrlWithStore';

export default { Actions, ClientApi, ConfigApi, StoryStore, reducer, syncUrlWithStore };
export default { start, Actions, ClientApi, ConfigApi, StoryStore, reducer, syncUrlWithStore };
Loading