-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Use per-story parameters in Notes addon #3373
Changes from 2 commits
fdafcbf
b490fa4
88212b2
194d6cd
c2fa6c3
3cb0823
83cc95b
e2f2c4b
bd0eb7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,25 +3,49 @@ import addons from '@storybook/addons'; | |
import marked from 'marked'; | ||
import { WithNotes as ReactWithNotes } from './react'; | ||
|
||
export const withMarkdownNotes = (text, options) => { | ||
function renderMarkdown(text, options) { | ||
marked.setOptions({ ...marked.defaults, options }); | ||
return marked(text); | ||
} | ||
|
||
const decorator = 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) => { | ||
const { parameters: { notes } } = context; | ||
console.log({ options, getStory, context, notes }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Left by mistake? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😬 |
||
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); | ||
}; | ||
}; | ||
|
||
export const withNotes = textOrOptions => { | ||
const channel = addons.getChannel(); | ||
const options = typeof textOrOptions === 'string' ? { text: textOrOptions } : textOrOptions; | ||
const hoc = options => story => context => decorator(options)(story, context); | ||
|
||
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); | ||
}; | ||
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]); | ||
}; | ||
|
||
Object.defineProperty(exports, 'WithNotes', { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,11 @@ import { action } from '@storybook/addon-actions'; | |
import BaseButton from '../components/BaseButton'; | ||
import markdownNotes from './notes/notes.md'; | ||
|
||
storiesOf('Addons|Notes', module) | ||
.add( | ||
'withNotes', | ||
withNotes( | ||
'This is the notes for a button. This is helpful for adding details about a story in a separate panel.' | ||
)(() => <BaseButton label="Button with notes - check the notes panel for details" />) | ||
) | ||
.add( | ||
'withNotes rendering imported markdown', | ||
withNotes(markdownNotes)(() => ( | ||
<BaseButton label="Button with notes - check the notes panel for details" /> | ||
)) | ||
) | ||
.add( | ||
'withNotes rendering inline, github-flavored markdown', | ||
withMarkdownNotes(` | ||
const baseStory = () => ( | ||
<BaseButton label="Button with notes - check the notes panel for details" /> | ||
); | ||
|
||
const markdownString = ` | ||
# Documentation | ||
|
||
This is inline github-flavored markdown! | ||
|
@@ -37,10 +26,20 @@ storiesOf('Addons|Notes', module) | |
)) | ||
) | ||
~~~ | ||
`)(() => ( | ||
<BaseButton label="Button with notes from inline github-flavored markdown - check the notes panel for details" /> | ||
)) | ||
) | ||
`; | ||
|
||
storiesOf('Addons|Notes', module) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add these example to the other frameworks too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! |
||
.addDecorator(withNotes) | ||
.add('withNotes', baseStory, { | ||
notes: | ||
'This is the notes for a button. This is helpful for adding details about a story in a separate panel.', | ||
}) | ||
.add('withNotes rendering imported markdown', baseStory, { notes: markdownNotes }) | ||
.add('withNotes rendering inline, github-flavored markdown', baseStory, { | ||
notes: { markdown: markdownString }, | ||
}) | ||
.add('using decorator arguments, withNotes', withNotes('Notes into withNotes')(baseStory)) | ||
.add('using decorator arguments, withMarkdownNotes', withMarkdownNotes(markdownString)(baseStory)) | ||
.add('using deprecated API', () => ( | ||
<WithNotes notes="Hello"> | ||
<BaseButton onClick={action('clicked')} label="😀 😎 👍 💯" /> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "version" word is a bit confusing here =)