-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.ts
133 lines (130 loc) · 3.72 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type { GetPropsFn, RendererLoader } from "@previewjs/iframe";
import type { App } from "vue";
import { createApp } from "vue";
const root = document.getElementById("root")!;
let app: App | null = null;
export const loadRenderer: RendererLoader = async ({
wrapperModule,
wrapperName,
previewableModule,
previewableName,
shouldAbortRender,
}) => {
const isStoryModule = !!previewableModule.default?.component;
const Wrapper =
(wrapperModule && wrapperModule[wrapperName || "default"]) || null;
let Previewable: any;
if (
(
previewableModule.default?.__file ||
previewableModule.default?.options?.__file
)?.endsWith(".vue")
) {
Previewable = previewableModule.default;
if (!Previewable) {
throw new Error(`No default component could be found`);
}
} else {
Previewable = previewableModule[`__previewjs__${previewableName}`];
if (!Previewable) {
throw new Error(`No component or story named '${previewableName}'`);
}
}
let storyDecorators = Previewable.decorators || [];
let RenderComponent = Previewable;
if (Previewable.render && !isStoryModule) {
// Vue component. Nothing to do.
} else {
// JSX or Storybook story, either CSF2 or CSF3.
if (typeof Previewable === "function") {
RenderComponent = (props: any) => {
const storyReturnValue = Previewable(props);
if (storyReturnValue.template) {
// CSF2 story.
// @ts-ignore
return h(storyReturnValue, props);
} else {
// JSX
return storyReturnValue;
}
};
} else {
// CSF3 story.
const csf3Story = Previewable;
RenderComponent =
csf3Story.component || previewableModule.default?.component;
if (!RenderComponent) {
throw new Error("Encountered a story with no component");
}
}
}
const decorators = [
...storyDecorators,
...(previewableModule.default?.decorators || []),
];
const Decorated = decorators.reduce((component, decorator) => {
const decorated = decorator();
return {
...decorated,
components: { ...decorated.components, story: component },
};
}, RenderComponent);
return {
render: async (getProps: GetPropsFn) => {
if (shouldAbortRender()) {
return;
}
if (app) {
app.unmount();
app = null;
}
const props = getProps({
presetGlobalProps: previewableModule.default?.args || {},
presetProps: Previewable.args || {},
});
app = createApp(() => {
// @ts-ignore
const decoratedNode = slotTransformingH(Decorated, props);
return Wrapper
? // @ts-ignore
h(Wrapper, null, () => decoratedNode)
: decoratedNode;
}, {});
app.mount(root);
if (Previewable.play) {
try {
await Previewable.play({ canvasElement: root });
} catch (e: any) {
// For some reason, Storybook expects to throw exceptions that should be ignored.
if (!e.message?.startsWith("ignoredException")) {
throw e;
}
}
}
},
// @ts-ignore
jsxFactory: slotTransformingH,
};
};
function slotTransformingH(component: any, props: any, children: any) {
props ||= {};
// @ts-ignore
return h(
component,
Object.fromEntries(
Object.entries(props).filter(
([propName]) => !propName.startsWith("slot:")
)
),
children !== undefined
? children
: Object.fromEntries(
Object.entries(props)
.filter(([propName]) => propName.startsWith("slot:"))
.map(([propName, propValue]) => [
propName.substring(5),
() => propValue,
])
)
);
}