-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
329 additions
and
320 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
AnyFramework, | ||
ComponentTitle, | ||
StoryContextForLoaders, | ||
StoryId, | ||
StoryName, | ||
} from '@storybook/csf'; | ||
import { CSFFile, ModuleExport, Story, StoryStore } from '@storybook/store'; | ||
import { PreviewWeb } from '../PreviewWeb'; | ||
|
||
import { DocsContextProps } from './DocsContextProps'; | ||
|
||
export class DocsContext<TFramework extends AnyFramework> implements DocsContextProps<TFramework> { | ||
private componentStoriesValue: Story<TFramework>[]; | ||
|
||
private storyIdToCSFFile: Map<StoryId, CSFFile<TFramework>>; | ||
|
||
private exportToStoryId: Map<ModuleExport, StoryId>; | ||
|
||
private nameToStoryId: Map<StoryName, StoryId>; | ||
|
||
constructor( | ||
public readonly id: StoryId, | ||
public readonly title: ComponentTitle, | ||
public readonly name: StoryName, | ||
protected store: StoryStore<TFramework>, | ||
/** The CSF files known (via the index) to be refererenced by this docs file */ | ||
public renderStoryToElement: PreviewWeb<TFramework>['renderStoryToElement'], | ||
csfFiles: CSFFile<TFramework>[], | ||
componentStoriesFromAllCsfFiles = true | ||
) { | ||
this.storyIdToCSFFile = new Map(); | ||
this.exportToStoryId = new Map(); | ||
this.nameToStoryId = new Map(); | ||
this.componentStoriesValue = []; | ||
|
||
csfFiles.forEach((csfFile, index) => { | ||
Object.values(csfFile.stories).forEach((annotation) => { | ||
this.storyIdToCSFFile.set(annotation.id, csfFile); | ||
this.exportToStoryId.set(annotation.moduleExport, annotation.id); | ||
this.nameToStoryId.set(annotation.name, annotation.id); | ||
|
||
if (componentStoriesFromAllCsfFiles || index === 0) | ||
this.componentStoriesValue.push(this.storyById(annotation.id)); | ||
}); | ||
}); | ||
} | ||
|
||
setMeta() { | ||
// Do nothing | ||
} | ||
|
||
storyIdByModuleExport = (storyExport: ModuleExport) => { | ||
const storyId = this.exportToStoryId.get(storyExport); | ||
if (storyId) return storyId; | ||
|
||
throw new Error(`No story found with that export: ${storyExport}`); | ||
}; | ||
|
||
storyIdByName = (storyName: StoryName) => { | ||
const storyId = this.nameToStoryId.get(storyName); | ||
if (storyId) return storyId; | ||
|
||
throw new Error(`No story found with that name: ${storyName}`); | ||
}; | ||
|
||
componentStories = () => { | ||
return this.componentStoriesValue; | ||
}; | ||
|
||
storyById = (inputStoryId?: StoryId) => { | ||
const storyId = inputStoryId || this.id; | ||
const csfFile = this.storyIdToCSFFile.get(storyId); | ||
if (!csfFile) | ||
throw new Error(`Called \`storyById\` for story that was never loaded: ${storyId}`); | ||
return this.store.storyFromCSFFile({ storyId, csfFile }); | ||
}; | ||
|
||
getStoryContext = (story: Story<TFramework>) => { | ||
return { | ||
...this.store.getStoryContext(story), | ||
viewMode: 'docs', | ||
} as StoryContextForLoaders<TFramework>; | ||
}; | ||
|
||
loadStory = (id: StoryId) => { | ||
return this.store.loadStory({ storyId: id }); | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { | ||
StoryId, | ||
StoryName, | ||
AnyFramework, | ||
StoryContextForLoaders, | ||
ComponentTitle, | ||
} from '@storybook/csf'; | ||
import type { ModuleExport, ModuleExports, Story } from '@storybook/store'; | ||
|
||
export interface DocsContextProps<TFramework extends AnyFramework = AnyFramework> { | ||
/** | ||
* These fields represent the docs entry that is being rendered to the screen | ||
*/ | ||
id: StoryId; | ||
title: ComponentTitle; | ||
name: StoryName; | ||
|
||
/** | ||
* Register the CSF file that this docs entry represents. | ||
* Used by the `<Meta of={} />` block. | ||
*/ | ||
setMeta: (metaExports: ModuleExports) => void; | ||
|
||
/** | ||
* Find a story's id from the direct export from the CSF file. | ||
* This is primarily used by the `<Story of={} /> block. | ||
*/ | ||
storyIdByModuleExport: (storyExport: ModuleExport, metaExports?: ModuleExports) => StoryId; | ||
/** | ||
* Find a story's id from the name of the story. | ||
* This is primarily used by the `<Story name={} /> block. | ||
* Note that the story must be part of the primary CSF file of the docs entry. | ||
*/ | ||
storyIdByName: (storyName: StoryName) => StoryId; | ||
/** | ||
* Syncronously find a story by id (if the id is not provided, this will look up the primary | ||
* story in the CSF file, if such a file exists). | ||
*/ | ||
storyById: (id?: StoryId) => Story<TFramework>; | ||
/** | ||
* Syncronously find all stories of the component referenced by the CSF file. | ||
*/ | ||
componentStories: () => Story<TFramework>[]; | ||
|
||
/** | ||
* Get the story context of the referenced story. | ||
*/ | ||
getStoryContext: (story: Story<TFramework>) => StoryContextForLoaders<TFramework>; | ||
/** | ||
* Asyncronously load an arbitrary story by id. | ||
*/ | ||
loadStory: (id: StoryId) => Promise<Story<TFramework>>; | ||
|
||
/** | ||
* Render a story to a given HTML element and keep it up to date across context changes | ||
*/ | ||
renderStoryToElement: (story: Story<TFramework>, element: HTMLElement) => () => Promise<void>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { AnyFramework, Parameters } from '@storybook/csf'; | ||
import { DocsContextProps } from './DocsContextProps'; | ||
|
||
export type DocsRenderFunction<TFramework extends AnyFramework> = ( | ||
docsContext: DocsContextProps<TFramework>, | ||
docsParameters: Parameters, | ||
element: HTMLElement, | ||
callback: () => void | ||
) => void; |
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.