-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move code from lib/addons to manager-api, reference manager-api or pr…
…eview-api instead of lib/addons
- Loading branch information
Showing
50 changed files
with
223 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
# Storybook Addons | ||
The contents of this package have moved to `@storybook/preview-api` and `@storybook/manager-api`. Please update your import depending on where are are using this API. | ||
|
||
Storybook Addons is a node module which is used to load custom addons to storybook. | ||
|
||
It stores addon loaders, communication channel and other resources which can be used by storybook implementations where required. | ||
|
||
--- | ||
|
||
For more information visit: [storybook.js.org](https://storybook.js.org) | ||
This package will no longer be released as part of the 8.0 release of storybook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1 @@ | ||
import global from 'global'; | ||
|
||
import type { Channel } from '@storybook/channels'; | ||
import { SET_CONFIG } from '@storybook/core-events'; | ||
import type { API } from '@storybook/manager-api'; | ||
import type { | ||
Addon_Collection, | ||
Addon_Config, | ||
Addon_Elements, | ||
Addon_Loaders, | ||
Addon_Type, | ||
Addon_Types, | ||
} from '@storybook/types'; | ||
import { Addon_TypesEnum } from '@storybook/types'; | ||
import { logger } from '@storybook/client-logger'; | ||
import { mockChannel } from './storybook-channel-mock'; | ||
|
||
export { Addon_Type as Addon, Addon_TypesEnum as types }; | ||
|
||
export function isSupportedType(type: Addon_Types): boolean { | ||
return !!Object.values(Addon_TypesEnum).find((typeVal) => typeVal === type); | ||
} | ||
|
||
export class AddonStore { | ||
constructor() { | ||
this.promise = new Promise((res) => { | ||
this.resolve = () => res(this.getChannel()); | ||
}) as Promise<Channel>; | ||
} | ||
|
||
private loaders: Addon_Loaders<API> = {}; | ||
|
||
private elements: Addon_Elements = {}; | ||
|
||
private config: Addon_Config = {}; | ||
|
||
private channel: Channel | undefined; | ||
|
||
private serverChannel: Channel | undefined; | ||
|
||
private promise: any; | ||
|
||
private resolve: any; | ||
|
||
getChannel = (): Channel => { | ||
// this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), set a mock instead. | ||
if (!this.channel) { | ||
this.setChannel(mockChannel()); | ||
} | ||
|
||
return this.channel; | ||
}; | ||
|
||
getServerChannel = (): Channel => { | ||
if (!this.serverChannel) { | ||
throw new Error('Accessing non-existent serverChannel'); | ||
} | ||
|
||
return this.serverChannel; | ||
}; | ||
|
||
ready = (): Promise<Channel> => this.promise; | ||
|
||
hasChannel = (): boolean => !!this.channel; | ||
|
||
hasServerChannel = (): boolean => !!this.serverChannel; | ||
|
||
setChannel = (channel: Channel): void => { | ||
this.channel = channel; | ||
this.resolve(); | ||
}; | ||
|
||
setServerChannel = (channel: Channel): void => { | ||
this.serverChannel = channel; | ||
}; | ||
|
||
getElements = (type: Addon_Types): Addon_Collection => { | ||
if (!this.elements[type]) { | ||
this.elements[type] = {}; | ||
} | ||
return this.elements[type]; | ||
}; | ||
|
||
addPanel = (name: string, options: Addon_Type): void => { | ||
this.add(name, { | ||
type: Addon_TypesEnum.PANEL, | ||
...options, | ||
}); | ||
}; | ||
|
||
add = (name: string, addon: Addon_Type) => { | ||
const { type } = addon; | ||
const collection = this.getElements(type); | ||
collection[name] = { id: name, ...addon }; | ||
}; | ||
|
||
setConfig = (value: Addon_Config) => { | ||
Object.assign(this.config, value); | ||
if (this.hasChannel()) { | ||
this.getChannel().emit(SET_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`); | ||
} | ||
this.loaders[name] = registerCallback; | ||
}; | ||
|
||
loadAddons = (api: any) => { | ||
Object.values(this.loaders).forEach((value) => value(api)); | ||
}; | ||
} | ||
|
||
// Enforce addons store to be a singleton | ||
const KEY = '__STORYBOOK_ADDONS'; | ||
|
||
function getAddonsStore(): AddonStore { | ||
if (!global[KEY]) { | ||
global[KEY] = new AddonStore(); | ||
} | ||
return global[KEY]; | ||
} | ||
|
||
// Exporting this twice in order to to be able to import it like { addons } instead of 'addons' | ||
// prefer import { addons } from '@storybook/addons' over import addons from '@storybook/addons' | ||
// | ||
// See public_api.ts | ||
|
||
export const addons = getAddonsStore(); | ||
export { addons, type AddonStore } from '@storybook/manager-api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1 @@ | ||
import { Channel } from '@storybook/channels'; | ||
|
||
export function mockChannel() { | ||
const transport = { | ||
setHandler: () => {}, | ||
send: () => {}, | ||
}; | ||
|
||
return new Channel({ transport }); | ||
} | ||
export { mockChannel } from '@storybook/manager-api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.