-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
render.js
111 lines (91 loc) · 2.94 KB
/
render.js
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
import { stripIndents } from 'common-tags';
import Vue from 'vue';
import ErrorDisplay from './ErrorDisplay.vue';
import NoPreview from './NoPreview.vue';
const logger = console;
let previousKind = '';
let previousStory = '';
let app = null;
let err = null;
function renderErrorDisplay(error) {
if (err) err.$destroy();
err = new Vue({
el: '#error-display',
render(h) {
return h(
'div',
{ attrs: { id: 'error-display' } },
error ? [h(ErrorDisplay, { props: { message: error.message, stack: error.stack } })] : []
);
},
});
}
export function renderError(error) {
renderErrorDisplay(error);
}
export function renderException(error) {
// We always need to render redbox in the mainPage if we get an error.
// Since this is an error, this affects to the main page as well.
renderErrorDisplay(error);
// Log the stack to the console. So, user could check the source code.
logger.error(error.stack);
}
function renderRoot(options) {
if (err) {
renderErrorDisplay(null); // clear
err = null;
}
if (app) app.$destroy();
app = new Vue(options);
}
export function renderMain(data, storyStore) {
if (storyStore.size() === 0) return;
const { selectedKind, selectedStory } = data;
const story = storyStore.getStory(selectedKind, selectedStory);
// Unmount the previous story only if selectedKind or selectedStory has changed.
// renderMain() gets executed after each action. Actions will cause the whole
// story to re-render without this check.
// https://github.com/storybooks/react-storybook/issues/116
if (selectedKind !== previousKind || previousStory !== selectedStory) {
// We need to unmount the existing set of components in the DOM node.
// Otherwise, React may not recrease instances for every story run.
// This could leads to issues like below:
// https://github.com/storybooks/react-storybook/issues/81
previousKind = selectedKind;
previousStory = selectedStory;
} else {
return;
}
const context = {
kind: selectedKind,
story: selectedStory,
};
const component = story ? story(context) : NoPreview;
if (!component) {
const error = {
message: `Expecting a Vue component from the story: "${selectedStory}" of "${selectedKind}".`,
stack: stripIndents`
Did you forget to return the Vue component from the story?
Use "() => ({ template: '<my-comp></my-comp>' })" or "() => ({ components: MyComp, template: '<my-comp></my-comp>' })" when defining the story.
`,
};
renderError(error);
}
renderRoot({
el: '#root',
render(h) {
return h('div', { attrs: { id: 'root' } }, [h(component)]);
},
});
}
export default function renderPreview({ reduxStore, storyStore }) {
const state = reduxStore.getState();
if (state.error) {
return renderException(state.error);
}
try {
return renderMain(state, storyStore);
} catch (ex) {
return renderException(ex);
}
}