Merge pull request #8232 from storybookjs/core/setConfig-method

ADD method to set config for manager
This commit is contained in:
Norbert de Langen 2019-10-03 12:57:18 +02:00 committed by GitHub
commit d7495fbaad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 4 deletions

View File

@ -46,6 +46,10 @@ export default class ReactProvider extends Provider {
return addons.getElements(type);
}
getConfig() {
return this.addons.getConfig();
}
renderPreview() {
return (
<Consumer filter={mapper} pure>

View File

@ -1,2 +1,9 @@
import '@storybook/addon-roundtrip/register';
import '@storybook/addon-parameter/register';
import { addons } from '@storybook/addons';
import { themes } from '@storybook/theming';
addons.setConfig({
theme: themes.dark,
});

View File

@ -39,6 +39,10 @@ interface Elements {
[key: string]: Collection;
}
interface Config {
[key: string]: any;
}
export class AddonStore {
constructor() {
this.promise = new Promise(res => {
@ -50,6 +54,8 @@ export class AddonStore {
private elements: Elements = {};
private config: Config = {};
private channel: Channel | undefined;
private promise: any;
@ -96,6 +102,12 @@ export class AddonStore {
collection[name] = { id: name, ...addon };
};
setConfig = (value: Config) => {
Object.assign(this.config, value);
};
getConfig = () => this.config;
register = (name: string, registerCallback: (api: API) => void): void => {
if (this.loaders[name]) {
logger.warn(`${name} was loaded twice, this could have bad side-effects`);

View File

@ -8,6 +8,7 @@ import { themes, ThemeVars } from '@storybook/theming';
import merge from '../lib/merge';
import { State } from '../index';
import Store from '../store';
import { Provider } from '../init-provider-api';
export type PanelPositions = 'bottom' | 'right';
@ -153,7 +154,7 @@ export const focusableUIElements = {
};
let hasSetOptions = false;
export default function({ store }: { store: Store }) {
export default function({ store, provider }: { store: Store; provider: Provider }) {
const api = {
toggleFullscreen(toggled?: boolean) {
return store.setState((state: State) => {
@ -248,6 +249,15 @@ export default function({ store }: { store: Store }) {
}
},
getInitialOptions() {
const { theme } = provider.getConfig();
return {
...initial,
theme: theme || initial.theme,
};
},
setOptions: (options: any) => {
// The very first time the user sets their options, we don't consider what is in the store.
// At this point in time, what is in the store is what we *persisted*. We did that in order
@ -255,7 +265,9 @@ export default function({ store }: { store: Store }) {
// However, we don't want to have a memory about these things, otherwise we see bugs like the
// user setting a name for their storybook, persisting it, then never being able to unset it
// without clearing localstorage. See https://github.com/storybookjs/storybook/issues/5857
const { layout, ui, selectedPanel, theme } = hasSetOptions ? store.getState() : initial;
const { layout, ui, selectedPanel, theme } = hasSetOptions
? store.getState()
: api.getInitialOptions();
if (options) {
const updatedLayout = {
@ -301,5 +313,5 @@ export default function({ store }: { store: Store }) {
const persisted = pick(store.getState(), 'layout', 'ui', 'selectedPanel', 'theme');
return { api, state: merge(initial, persisted) };
return { api, state: merge(api.getInitialOptions(), persisted) };
}

View File

@ -33,7 +33,7 @@ describe('layout API', () => {
getState: () => currentState,
setState: jest.fn(),
};
layoutApi = initLayout({ store }).api;
layoutApi = initLayout({ store, provider: { getConfig: jest.fn(() => ({})) } }).api;
});
it('should not change selectedPanel if it is undefined in the options', () => {

View File

@ -20,6 +20,10 @@ export default class ReactProvider extends Provider {
return this.addons.getElements(type);
}
getConfig() {
return this.addons.getConfig();
}
handleAPI(api) {
this.addons.loadAddons(api);
}

View File

@ -28,6 +28,10 @@ class FakeProvider extends Provider {
handleAPI(api) {
addons.loadAddons(api);
}
getConfig() {
return {};
}
}
storiesOf('UI|Layout/App', module)

View File

@ -6,4 +6,8 @@ export default class Provider {
handleAPI() {
throw new Error('Provider.handleAPI() is not implemented!');
}
getConfig() {
throw new Error('Provider.getConfig() is not implemented!');
}
}