/
RenderStory.ts
165 lines (138 loc) · 3.41 KB
/
RenderStory.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* eslint-disable vue/one-component-per-file */
import {
defineComponent as _defineComponent,
onBeforeUnmount as _onBeforeUnmount,
onMounted as _onMounted,
PropType as _PropType,
ref as _ref,
watch as _watch,
h as _h,
} from '@histoire/vendors/vue'
import type { Story, Variant } from '@histoire/shared'
// @ts-expect-error virtual module id
import * as setup from 'virtual:$histoire-setup'
// @ts-expect-error virtual module id
import * as generatedSetup from 'virtual:$histoire-generated-global-setup'
import type { App, MountApi, VanillaApi } from './types'
export default _defineComponent({
name: 'RenderStory',
props: {
variant: {
type: Object as _PropType<Variant>,
required: true,
},
story: {
type: Object as _PropType<Story>,
required: true,
},
slotName: {
type: String,
default: 'default',
},
},
emits: {
ready: () => true,
},
setup (props, { emit }) {
const sandbox = _ref<HTMLDivElement>()
let mounting = false
let app: App
let appHooks: Record<'onUpdate' | 'onUnmount', (() => unknown)[]>
async function unmountVariant () {
if (app) {
await app.onUnmount?.()
if (appHooks) {
for (const hook of appHooks.onUnmount) {
await hook()
}
}
app.el.parentNode.removeChild(app.el)
app = null
}
}
async function mountVariant () {
if (mounting) return
mounting = true
await unmountVariant()
app = {
el: document.createElement('div'),
}
if (typeof generatedSetup?.setupVanilla === 'function') {
await generatedSetup.setupVanilla({
app,
story: props.story,
variant: props.variant,
})
}
if (typeof setup?.setupVanilla === 'function') {
await setup.setupVanilla({
app,
story: props.story,
variant: props.variant,
})
}
if (typeof props.variant.setupApp === 'function') {
await props.variant.setupApp({
app,
story: props.story,
variant: props.variant,
})
}
await app.onMount?.()
appHooks = {
onUpdate: [],
onUnmount: [],
}
const api: MountApi = {
el: app.el,
state: props.variant.state,
onUpdate: (cb) => {
appHooks.onUpdate.push(cb)
},
onUnmount: (cb) => {
appHooks.onUnmount.push(cb)
},
}
const onMount = props.variant.slots()[props.slotName] as VanillaApi['onMount'] | VanillaApi['onMountControls']
await onMount(api)
sandbox.value.appendChild(app.el)
emit('ready')
}
_onMounted(async () => {
if (props.variant.configReady) {
await mountVariant()
}
})
_watch(() => props.variant, async value => {
if (value.configReady && !mounting) {
if (!app) {
await mountVariant()
} else {
// @TODO check if need to refresh here
}
}
}, {
deep: true,
})
_watch(() => props.variant.state, async () => {
if (appHooks) {
for (const hook of appHooks.onUpdate) {
await hook()
}
}
}, {
deep: true,
})
_onBeforeUnmount(() => {
unmountVariant()
})
return {
sandbox,
}
},
render () {
return _h('div', {
ref: 'sandbox',
})
},
})