Skip to content

Commit

Permalink
withStorySource deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Jan 20, 2020
1 parent d77bbcf commit 95c4c7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
5 changes: 4 additions & 1 deletion addons/docs/src/blocks/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ const extract = (targetId: string, { source, locationsMap }: StorySource) => {
if (!location) return null;
const { startBody: start, endBody: end } = location;
const lines = source.split('\n');
if (start.line === end.line) {
if (start.line === end.line && lines[start.line - 1] !== undefined) {
return lines[start.line - 1].substring(start.col, end.col);
}
// NOTE: storysource locations are 1-based not 0-based!
const startLine = lines[start.line - 1];
const endLine = lines[end.line - 1];
if (startLine === undefined || endLine === undefined) {
return source;
}
return [
startLine.substring(start.col),
...lines.slice(start.line, end.line - 1),
Expand Down
3 changes: 2 additions & 1 deletion addons/storysource/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ADDON_ID, PANEL_ID } from './events';
import { withStorySource } from './preview';

export { ADDON_ID, PANEL_ID };
export { ADDON_ID, PANEL_ID, withStorySource };

if (module && module.hot && module.hot.decline) {
module.hot.decline();
Expand Down
10 changes: 10 additions & 0 deletions addons/storysource/src/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function withStorySource(source: string, locationsMap = {}) {
// eslint-disable-next-line no-console
console.error(
'@storybook/addon-storysource/withStorySource is deprecated, please use paramaters instead.'
);

return (storyFn: (context: any) => React.ReactNode, context: any) => {
return storyFn(context);
};
}
1 change: 1 addition & 0 deletions examples/html-kitchen-sink/stories/button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button>Hello world</button>
24 changes: 24 additions & 0 deletions examples/html-kitchen-sink/stories/source-loader.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import button from './button.html';

export default {
title: 'Addons/Source loader',
};

export const Button = () => button;
Button.story = {
parameters: {
storySource: {
source: button,
},
},
};

export const SimpleStory = () =>
`<p>
<strong>
This is a fragment of HTML
</strong>
</p>`;
SimpleStory.story = {
name: 'Very simple story',
};

0 comments on commit 95c4c7d

Please sign in to comment.