-
-
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.
Merge pull request #3373 from storybooks/tmeasday/use-parameters-in-a…
…ddons Use per-story parameters in Notes addon
- Loading branch information
Showing
11 changed files
with
181 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,46 @@ | ||
import deprecate from 'util-deprecate'; | ||
import addons from '@storybook/addons'; | ||
import marked from 'marked'; | ||
import { WithNotes as ReactWithNotes } from './react'; | ||
|
||
export const withMarkdownNotes = (text, options) => { | ||
const channel = addons.getChannel(); | ||
return getStory => context => { | ||
marked.setOptions({ ...marked.defaults, options }); | ||
// send the notes to the channel before the story is rendered | ||
channel.emit('storybook/notes/add_notes', marked(text)); | ||
return getStory(context); | ||
}; | ||
}; | ||
function renderMarkdown(text, options) { | ||
marked.setOptions({ ...marked.defaults, options }); | ||
return marked(text); | ||
} | ||
|
||
export const withNotes = textOrOptions => { | ||
const decorator = options => { | ||
const channel = addons.getChannel(); | ||
const options = typeof textOrOptions === 'string' ? { text: textOrOptions } : textOrOptions; | ||
return (getStory, context) => { | ||
const { parameters: { notes } } = context; | ||
const storyOptions = notes || options; | ||
|
||
if (storyOptions) { | ||
const { text, markdown, markdownOptions } = | ||
typeof storyOptions === 'string' ? { text: storyOptions } : storyOptions; | ||
|
||
if (!text && !markdown) { | ||
throw new Error('You must set of one of `text` or `markdown` on the `notes` parameter'); | ||
} | ||
|
||
channel.emit('storybook/notes/add_notes', text || renderMarkdown(markdown, markdownOptions)); | ||
} | ||
|
||
return getStory => context => { | ||
// send the notes to the channel before the story is rendered | ||
channel.emit('storybook/notes/add_notes', options.text); | ||
return getStory(context); | ||
}; | ||
}; | ||
|
||
Object.defineProperty(exports, 'WithNotes', { | ||
configurable: true, | ||
enumerable: true, | ||
get: deprecate( | ||
() => ReactWithNotes, | ||
'@storybook/addon-notes WithNotes Component is deprecated, use withNotes() instead. See https://github.com/storybooks/storybook/tree/master/addons/notes' | ||
), | ||
}); | ||
const hoc = options => story => context => decorator(options)(story, context); | ||
|
||
export const withMarkdownNotes = (text, options) => | ||
hoc({ | ||
markdown: text, | ||
markdownOptions: options, | ||
}); | ||
|
||
export const withNotes = (...args) => { | ||
// Used without options as .addDecorator(withNotes) | ||
if (typeof args[0] === 'function') { | ||
return decorator()(...args); | ||
} | ||
|
||
// Input are options, ala .add('name', withNotes('note')(() => <Story/>)) | ||
return hoc(args[0]); | ||
}; |
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
Oops, something went wrong.