-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: json renderer and document renderFn
- Loading branch information
1 parent
2c8ee72
commit 5f4f2ce
Showing
27 changed files
with
134 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './dist/json.d'; |
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 @@ | ||
module.exports = require('./dist/json'); |
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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
"dist/", | ||
"package.json", | ||
"README.md", | ||
"json.js", | ||
"json.d.ts", | ||
"react.js", | ||
"react.d.ts" | ||
], | ||
|
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,5 +1,5 @@ | ||
import { config } from '../../rollup-config'; | ||
|
||
export default config({ | ||
input: ['./src/index.ts', './src/react.tsx'], | ||
input: ['./src/index.ts', './src/react.tsx', './src/json.tsx'], | ||
}); |
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,55 @@ | ||
import { | ||
Story, | ||
Document, | ||
StoryRenderFn, | ||
getControlValues, | ||
deepMerge, | ||
FrameworkRenderFn, | ||
ComponentControls, | ||
} from '@component-controls/core'; | ||
|
||
type ComposeFn = ( | ||
props: Parameters<FrameworkRenderFn>[0], | ||
) => { | ||
renderFn: StoryRenderFn | undefined; | ||
values: any; | ||
context: { | ||
story: Story; | ||
doc: Document; | ||
controls: ComponentControls; | ||
} & Record<string, any>; | ||
}; | ||
export const composeFn: ComposeFn = ({ story, doc, options }) => { | ||
if (!story) { | ||
throw new Error(`Invalid story`); | ||
} | ||
const controls = story.controls; | ||
const values = getControlValues(controls); | ||
//parameters added to avoid bug in SB6 rc that assumes parameters exist | ||
const context = { | ||
story, | ||
doc, | ||
controls, | ||
...options, | ||
}; | ||
const { decorators: globalDecorators = [] } = options || {}; | ||
const { decorators: storyDecorators = [] } = story; | ||
const decorators = deepMerge<StoryRenderFn[]>( | ||
globalDecorators, | ||
storyDecorators, | ||
); | ||
const sortedDecorators = decorators.reverse(); | ||
let renderFn = story.storyFn; | ||
for (let i = 0; i < sortedDecorators.length; i += 1) { | ||
const decorator = sortedDecorators[i]; | ||
const childFn = renderFn; | ||
const nextRenderFn = (_: any, nextContext: any) => | ||
(childFn as StoryRenderFn)(values, { ...context, ...nextContext }); | ||
renderFn = () => | ||
decorator(nextRenderFn, { | ||
...context, | ||
renderFn: nextRenderFn, | ||
}); | ||
} | ||
return { renderFn, values, context }; | ||
}; |
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,7 +1,9 @@ | ||
import { FrameworkRenderFn } from '@component-controls/core'; | ||
import { render as renderReact } from './react'; | ||
import { render as renderJSON } from './json'; | ||
|
||
//noop | ||
export const renderers: Record<string, FrameworkRenderFn> = { | ||
react: renderReact, | ||
json: renderJSON, | ||
}; |
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,11 @@ | ||
import { StoryRenderFn, FrameworkRenderFn } from '@component-controls/core'; | ||
import { composeFn } from './compose'; | ||
|
||
export const render: FrameworkRenderFn = props => { | ||
const { renderFn, values, context } = composeFn(props); | ||
const result = | ||
typeof renderFn === 'function' | ||
? (renderFn as StoryRenderFn)(values, context) | ||
: 'invalid function'; | ||
return JSON.stringify(result, null, 2); | ||
}; |
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
Oops, something went wrong.