-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ts migration/vue
- Loading branch information
Showing
19 changed files
with
251 additions
and
147 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
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,133 @@ | ||
/* eslint-disable prefer-destructuring */ | ||
import Vue, { VueConstructor, ComponentOptions } from 'vue'; | ||
import { start } from '@storybook/core/client'; | ||
import { | ||
ClientStoryApi, | ||
StoryFn, | ||
DecoratorFunction, | ||
StoryContext, | ||
Loadable, | ||
} from '@storybook/addons'; | ||
|
||
import './globals'; | ||
import { IStorybookSection, StoryFnVueReturnType } from './types'; | ||
|
||
import render, { VALUES } from './render'; | ||
import { extractProps } from './util'; | ||
|
||
export const WRAPS = 'STORYBOOK_WRAPS'; | ||
|
||
function prepare(rawStory: StoryFnVueReturnType, innerStory?: VueConstructor): VueConstructor { | ||
let story: ComponentOptions<Vue> | VueConstructor; | ||
|
||
if (typeof rawStory === 'string') { | ||
story = { template: rawStory }; | ||
} else { | ||
story = rawStory as ComponentOptions<Vue>; | ||
} | ||
|
||
// @ts-ignore | ||
// eslint-disable-next-line no-underscore-dangle | ||
if (!story._isVue) { | ||
if (innerStory) { | ||
story.components = { ...(story.components || {}), story: innerStory }; | ||
} | ||
story = Vue.extend(story); | ||
// @ts-ignore // https://github.com/storybookjs/storybook/pull/7578#discussion_r307984824 | ||
} else if (story.options[WRAPS]) { | ||
return story as VueConstructor; | ||
} | ||
|
||
return Vue.extend({ | ||
// @ts-ignore // https://github.com/storybookjs/storybook/pull/7578#discussion_r307985279 | ||
[WRAPS]: story, | ||
// @ts-ignore // https://github.com/storybookjs/storybook/pull/7578#discussion_r307984824 | ||
[VALUES]: { ...(innerStory ? innerStory.options[VALUES] : {}), ...extractProps(story) }, | ||
functional: true, | ||
render(h, { data, parent, children }) { | ||
return h( | ||
story, | ||
{ | ||
...data, | ||
// @ts-ignore // https://github.com/storybookjs/storybook/pull/7578#discussion_r307986196 | ||
props: { ...(data.props || {}), ...parent.$root[VALUES] }, | ||
}, | ||
children | ||
); | ||
}, | ||
}); | ||
} | ||
|
||
const defaultContext: StoryContext = { | ||
id: 'unspecified', | ||
name: 'unspecified', | ||
kind: 'unspecified', | ||
parameters: {}, | ||
}; | ||
|
||
function decorateStory( | ||
storyFn: StoryFn<StoryFnVueReturnType>, | ||
decorators: DecoratorFunction<VueConstructor>[] | ||
): StoryFn<VueConstructor> { | ||
return decorators.reduce( | ||
(decorated: StoryFn<VueConstructor>, decorator) => (context: StoryContext = defaultContext) => { | ||
let story; | ||
|
||
const decoratedStory = decorator(p => { | ||
story = decorated( | ||
p | ||
? { | ||
...context, | ||
...p, | ||
parameters: { | ||
...context.parameters, | ||
...p.parameters, | ||
}, | ||
} | ||
: context | ||
); | ||
|
||
return story; | ||
}, context); | ||
|
||
if (!story) { | ||
story = decorated(context); | ||
} | ||
|
||
if (decoratedStory === story) { | ||
return story; | ||
} | ||
|
||
return prepare(decoratedStory, story); | ||
}, | ||
context => prepare(storyFn(context)) | ||
); | ||
} | ||
const framework = 'vue'; | ||
|
||
interface ClientApi extends ClientStoryApi<StoryFnVueReturnType> { | ||
setAddon(addon: any): void; | ||
configure(loader: Loadable, module: NodeModule, framework: string): void; | ||
getStorybook(): IStorybookSection[]; | ||
clearDecorators(): void; | ||
forceReRender(): void; | ||
raw: () => any; // todo add type | ||
load: (...args: any[]) => void; | ||
} | ||
|
||
const api = start(render, { decorateStory }); | ||
|
||
export const storiesOf: ClientApi['storiesOf'] = (kind, m) => { | ||
return (api.clientApi.storiesOf(kind, m) as ReturnType<ClientApi['storiesOf']>).addParameters({ | ||
framework, | ||
}); | ||
}; | ||
|
||
export const configure: ClientApi['configure'] = (...args) => api.configure(...args, framework); | ||
export const addDecorator: ClientApi['addDecorator'] = api.clientApi.addDecorator; | ||
export const addParameters: ClientApi['addParameters'] = api.clientApi.addParameters; | ||
export const clearDecorators: ClientApi['clearDecorators'] = api.clientApi.clearDecorators; | ||
export const setAddon: ClientApi['setAddon'] = api.clientApi.setAddon; | ||
export const forceReRender: ClientApi['forceReRender'] = api.forceReRender; | ||
export const getStorybook: ClientApi['getStorybook'] = api.clientApi.getStorybook; | ||
export const raw: ClientApi['raw'] = api.clientApi.raw; |
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,31 @@ | ||
import { Component, VueConstructor } from 'vue'; | ||
import { StoryFn } from '@storybook/addons'; | ||
// TODO, 'any' should be what is actually expected from a storyFn | ||
|
||
export interface ShowErrorArgs { | ||
title: string; | ||
description: string; | ||
} | ||
|
||
export interface RenderMainArgs { | ||
storyFn: StoryFn<VueConstructor>; | ||
selectedKind: string; | ||
selectedStory: string; | ||
showMain: () => void; | ||
showError: (args: ShowErrorArgs) => void; | ||
showException: (...args: any[]) => void; | ||
forceRender: boolean; | ||
} | ||
|
||
// TODO: some vue expert needs to look at this | ||
export type StoryFnVueReturnType = string | Component; | ||
|
||
export interface IStorybookStory { | ||
name: string; | ||
render: () => any; | ||
} | ||
|
||
export interface IStorybookSection { | ||
kind: string; | ||
stories: IStorybookStory[]; | ||
} |
11 changes: 7 additions & 4 deletions
11
app/vue/src/client/preview/util.js → app/vue/src/client/preview/util.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
File renamed without changes.
Oops, something went wrong.
d1d22d7
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.
Successfully aliased the URL https://monorepo-6pyg0wdrx.now.sh to the following aliases: