histoire 0.4.6 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client.d.ts +2 -0
- package/dist/bundled/HomeView.js +1 -0
- package/dist/bundled/MobileOverlay.js +3 -1
- package/dist/bundled/SandboxVue3.js +649 -0
- package/dist/bundled/SearchPane.js +2 -0
- package/dist/bundled/StoryView.js +307 -618
- package/dist/bundled/global-components.js +5 -5
- package/dist/bundled/index2.js +111 -43
- package/dist/bundled/sandbox.js +2 -2
- package/dist/bundled/state.js +16 -83
- package/dist/client/app/App.vue +18 -0
- package/dist/client/app/codegen/serialize-js.js +1 -1
- package/dist/client/app/codegen/vue3.d.ts +2 -0
- package/dist/client/app/codegen/vue3.js +37 -12
- package/dist/client/app/components/app/InitialLoading.vue +46 -0
- package/dist/client/app/components/app/MobileOverlay.vue +1 -1
- package/dist/client/app/components/exposed/Story.vue +2 -1
- package/dist/client/app/components/exposed/Variant.vue +4 -4
- package/dist/client/app/components/panel/ControlsComponentPropItem.vue +96 -0
- package/dist/client/app/components/panel/ControlsComponentProps.vue +33 -0
- package/dist/client/app/components/panel/StatePresets.vue +8 -6
- package/dist/client/app/components/panel/StoryControls.vue +18 -3
- package/dist/client/app/components/sandbox/SandboxVue3.vue +70 -2
- package/dist/client/app/style/transitions.pcss +12 -0
- package/dist/client/app/types.d.ts +19 -7
- package/dist/client/app/util/state.d.ts +1 -0
- package/dist/client/app/util/state.js +11 -2
- package/dist/node/build.js +1 -3
- package/dist/node/collect/index.js +6 -0
- package/dist/node/config.d.ts +23 -23
- package/dist/node/plugin.d.ts +2 -2
- package/dist/node/server.js +16 -5
- package/dist/style.css +92 -13
- package/index.d.ts +3 -0
- package/package.json +3 -3
- package/src/client/app/App.vue +18 -0
- package/src/client/app/codegen/serialize-js.ts +1 -1
- package/src/client/app/codegen/vue3.ts +40 -12
- package/src/client/app/components/app/InitialLoading.vue +46 -0
- package/src/client/app/components/app/MobileOverlay.vue +1 -1
- package/src/client/app/components/exposed/Story.vue +2 -1
- package/src/client/app/components/exposed/Variant.vue +4 -4
- package/src/client/app/components/panel/ControlsComponentPropItem.vue +96 -0
- package/src/client/app/components/panel/ControlsComponentProps.vue +33 -0
- package/src/client/app/components/panel/StatePresets.vue +8 -6
- package/src/client/app/components/panel/StoryControls.vue +18 -3
- package/src/client/app/components/sandbox/SandboxVue3.vue +70 -2
- package/src/client/app/style/transitions.pcss +12 -0
- package/src/client/app/types.ts +22 -7
- package/src/client/app/util/state.ts +12 -2
- package/src/components.d.ts +143 -0
- package/src/node/build.ts +1 -3
- package/src/node/collect/index.ts +7 -0
- package/src/node/server.ts +20 -5
- package/dist/bundled/dark.js +0 -4
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
// @ts-
|
|
2
|
-
// @TODO remove no-check and fix vnode type errors
|
|
1
|
+
// @TODO remove @ts-ignore
|
|
3
2
|
|
|
4
3
|
import { VNode, vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic, Text } from 'vue'
|
|
5
4
|
import { pascal } from 'case'
|
|
@@ -12,15 +11,17 @@ export async function generateSourceCode (variant: Variant) {
|
|
|
12
11
|
const vnode = variant.slots().default?.({ state: variant.state ?? {} }) ?? []
|
|
13
12
|
const list = Array.isArray(vnode) ? vnode : [vnode]
|
|
14
13
|
const lines: string[] = []
|
|
15
|
-
for (const
|
|
16
|
-
|
|
14
|
+
for (const n in list) {
|
|
15
|
+
const vnode = list[n]
|
|
16
|
+
lines.push(...(await printVNode(vnode, variant.state?._hPropState?.[n])).lines)
|
|
17
17
|
}
|
|
18
18
|
return lines.join('\n')
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: boolean }> {
|
|
21
|
+
async function printVNode (vnode: VNode, propsOverrides: Record<string, any> = null): Promise<{ lines: string[], isText?: boolean }> {
|
|
22
22
|
if (vnode.type === Text) {
|
|
23
23
|
return {
|
|
24
|
+
// @ts-ignore
|
|
24
25
|
lines: [vnode.children],
|
|
25
26
|
isText: true,
|
|
26
27
|
}
|
|
@@ -30,7 +31,9 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
30
31
|
|
|
31
32
|
if (typeof vnode.type === 'object' || typeof vnode.type === 'string') {
|
|
32
33
|
// Wait for async component
|
|
34
|
+
// @ts-ignore
|
|
33
35
|
if (vnode.type?.__asyncLoader && !vnode.type.__asyncResolved) {
|
|
36
|
+
// @ts-ignore
|
|
34
37
|
await vnode.type.__asyncLoader()
|
|
35
38
|
}
|
|
36
39
|
|
|
@@ -85,7 +88,9 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
85
88
|
}
|
|
86
89
|
}
|
|
87
90
|
genDirective('model', dir, valueCode)
|
|
91
|
+
// @ts-ignore
|
|
88
92
|
} else if (dir.instance._ || dir.instance.$) {
|
|
93
|
+
// @ts-ignore
|
|
89
94
|
const target = dir.instance.$ ?? dir.instance._
|
|
90
95
|
let dirName: string
|
|
91
96
|
for (const directives of [target.directives, target.appContext.directives]) {
|
|
@@ -105,11 +110,8 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
// Attributes
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
continue
|
|
111
|
-
}
|
|
112
|
-
const value = vnode.props[prop]
|
|
113
|
+
function addAttr (prop: string, value: any) {
|
|
114
|
+
// @ts-ignore
|
|
113
115
|
if (typeof value !== 'string' || vnode.dynamicProps?.includes(prop)) {
|
|
114
116
|
let directive = ':'
|
|
115
117
|
if (prop.startsWith('on')) {
|
|
@@ -119,6 +121,7 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
119
121
|
|
|
120
122
|
// v-model on component
|
|
121
123
|
const vmodelListener = `onUpdate:${prop}`
|
|
124
|
+
// @ts-ignore
|
|
122
125
|
if (directive === ':' && vnode.dynamicProps?.includes(vmodelListener)) {
|
|
123
126
|
// Listener
|
|
124
127
|
skipProps.push(vmodelListener)
|
|
@@ -141,7 +144,7 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
141
144
|
modifiers,
|
|
142
145
|
value,
|
|
143
146
|
}, valueCode)
|
|
144
|
-
|
|
147
|
+
return
|
|
145
148
|
}
|
|
146
149
|
|
|
147
150
|
let serialized: string[]
|
|
@@ -177,6 +180,21 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
177
180
|
attrs.push([`${prop}="${value}"`])
|
|
178
181
|
}
|
|
179
182
|
}
|
|
183
|
+
|
|
184
|
+
for (const prop in vnode.props) {
|
|
185
|
+
if (skipProps.includes(prop) || (propsOverrides && prop in propsOverrides)) {
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
188
|
+
const value = vnode.props[prop]
|
|
189
|
+
addAttr(prop, value)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (propsOverrides) {
|
|
193
|
+
for (const prop in propsOverrides) {
|
|
194
|
+
addAttr(prop, propsOverrides[prop])
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
180
198
|
if (attrs.length > 1) {
|
|
181
199
|
multilineAttrs = true
|
|
182
200
|
}
|
|
@@ -197,6 +215,7 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
197
215
|
} else if (Array.isArray(vnode.children)) {
|
|
198
216
|
let isAllChildText
|
|
199
217
|
for (const child of vnode.children) {
|
|
218
|
+
// @ts-ignore
|
|
200
219
|
const result = await printVNode(child)
|
|
201
220
|
if (result.isText) {
|
|
202
221
|
if (isAllChildText === undefined) {
|
|
@@ -230,6 +249,7 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
230
249
|
return () => false
|
|
231
250
|
}
|
|
232
251
|
})
|
|
252
|
+
// @ts-ignore
|
|
233
253
|
const children = vnode.children[key](autoObject.proxy)
|
|
234
254
|
const slotLines: string[] = []
|
|
235
255
|
for (const child of children) {
|
|
@@ -286,6 +306,7 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
286
306
|
lines.push(`${tag[0]}${isVoid ? '' : ' /'}>`)
|
|
287
307
|
}
|
|
288
308
|
} else if (vnode?.shapeFlag & 1 << 4) {
|
|
309
|
+
// @ts-ignore
|
|
289
310
|
for (const child of vnode.children) {
|
|
290
311
|
lines.push(...(await printVNode(child)).lines)
|
|
291
312
|
}
|
|
@@ -296,15 +317,21 @@ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: bo
|
|
|
296
317
|
}
|
|
297
318
|
}
|
|
298
319
|
|
|
299
|
-
function getTagName (vnode: VNode) {
|
|
320
|
+
export function getTagName (vnode: VNode) {
|
|
300
321
|
if (typeof vnode.type === 'string') {
|
|
301
322
|
return vnode.type
|
|
323
|
+
// @ts-ignore
|
|
302
324
|
} else if (vnode.type?.__asyncResolved) {
|
|
325
|
+
// @ts-ignore
|
|
303
326
|
const asyncComp = vnode.type?.__asyncResolved
|
|
304
327
|
return asyncComp.name ?? getNameFromFile(asyncComp.__file)
|
|
328
|
+
// @ts-ignore
|
|
305
329
|
} else if (vnode.type?.name) {
|
|
330
|
+
// @ts-ignore
|
|
306
331
|
return vnode.type.name
|
|
332
|
+
// @ts-ignore
|
|
307
333
|
} else if (vnode.type?.__file) {
|
|
334
|
+
// @ts-ignore
|
|
308
335
|
return getNameFromFile(vnode.type.__file)
|
|
309
336
|
}
|
|
310
337
|
return 'Anonymous'
|
|
@@ -322,6 +349,7 @@ function serializeAndCleanJs (value: any) {
|
|
|
322
349
|
const isAutoBuildingObject = !!value?.__autoBuildingObject
|
|
323
350
|
const result = serializeJs(value)
|
|
324
351
|
if (isAutoBuildingObject) {
|
|
352
|
+
// @ts-ignore
|
|
325
353
|
return [cleanupExpression(result.__autoBuildingObjectGetKey)]
|
|
326
354
|
} else {
|
|
327
355
|
return cleanupExpression(result).split('\n')
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import { reactive } from 'vue'
|
|
3
|
+
|
|
4
|
+
const progress = reactive({
|
|
5
|
+
loaded: 0,
|
|
6
|
+
total: 0,
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
const maxCols = window.innerWidth / 20
|
|
10
|
+
|
|
11
|
+
// @ts-expect-error vite hot api
|
|
12
|
+
if (import.meta.hot) {
|
|
13
|
+
// @ts-expect-error vite hot api
|
|
14
|
+
import.meta.hot.on('histoire:stories-loading-progress', (data) => {
|
|
15
|
+
progress.loaded = data.loadedFileCount
|
|
16
|
+
progress.total = data.totalFileCount
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<template>
|
|
22
|
+
<div class="htw-fixed htw-inset-0 htw-bg-white dark:htw-bg-gray-700 htw-flex htw-flex-col htw-gap-6 htw-items-center htw-justify-center">
|
|
23
|
+
<transition name="__histoire-fade">
|
|
24
|
+
<div
|
|
25
|
+
v-if="progress.total > 0"
|
|
26
|
+
class="htw-grid htw-gap-2"
|
|
27
|
+
:style="{
|
|
28
|
+
gridTemplateColumns: `repeat(${Math.min(Math.ceil(Math.sqrt(progress.total)), maxCols)}, minmax(0, 1fr))`,
|
|
29
|
+
}"
|
|
30
|
+
>
|
|
31
|
+
<div
|
|
32
|
+
v-for="n in progress.total"
|
|
33
|
+
:key="n"
|
|
34
|
+
class="htw-bg-primary-500/10 htw-rounded-full"
|
|
35
|
+
>
|
|
36
|
+
<div
|
|
37
|
+
class="htw-w-3 htw-h-3 htw-bg-primary-500 htw-rounded-full htw-duration-150 htw-ease-out"
|
|
38
|
+
:class="{
|
|
39
|
+
'htw-transition-transform htw-scale-0': n >= progress.loaded,
|
|
40
|
+
}"
|
|
41
|
+
/>
|
|
42
|
+
</div>
|
|
43
|
+
</div>
|
|
44
|
+
</transition>
|
|
45
|
+
</div>
|
|
46
|
+
</template>
|
|
@@ -10,7 +10,7 @@ const emits = defineEmits<{(e: 'close'): void}>()
|
|
|
10
10
|
</script>
|
|
11
11
|
|
|
12
12
|
<template>
|
|
13
|
-
<transition name="__histoire-fade">
|
|
13
|
+
<transition name="__histoire-fade-bottom">
|
|
14
14
|
<div
|
|
15
15
|
v-if="opened"
|
|
16
16
|
class="htw-absolute htw-z-10 htw-bg-white dark:htw-bg-gray-700 htw-w-screen htw-h-screen htw-inset-0 htw-overflow-hidden htw-flex htw-flex-col"
|
|
@@ -46,7 +46,8 @@ export default defineComponent({
|
|
|
46
46
|
for (const key in storyComponent.devtoolsRawSetupState) {
|
|
47
47
|
addImplicitState(key, storyComponent.devtoolsRawSetupState[key])
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
// Shallow copy to prevent sharing object with different variants
|
|
50
|
+
provide('implicitState', () => ({ ...implicitState }))
|
|
50
51
|
|
|
51
52
|
function updateStory () {
|
|
52
53
|
Object.assign(attrs.story, {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { defineComponent, getCurrentInstance, inject, PropType, useAttrs, watch } from 'vue'
|
|
3
3
|
import { Variant } from '../../types'
|
|
4
|
+
import { applyStateToVariant } from '../../util/state'
|
|
4
5
|
|
|
5
6
|
// const logLocation = location.href.includes('__sandbox') ? '[Sandbox]' : '[Host]'
|
|
6
7
|
|
|
@@ -41,9 +42,7 @@ export default defineComponent({
|
|
|
41
42
|
const implicitState = inject<() => any>('implicitState')
|
|
42
43
|
|
|
43
44
|
watch(() => implicitState, value => {
|
|
44
|
-
|
|
45
|
-
attrs.variant.state = value()
|
|
46
|
-
}
|
|
45
|
+
applyStateToVariant(attrs.variant, value())
|
|
47
46
|
}, {
|
|
48
47
|
immediate: true,
|
|
49
48
|
})
|
|
@@ -52,7 +51,8 @@ export default defineComponent({
|
|
|
52
51
|
Object.assign(attrs.variant, {
|
|
53
52
|
initState: async () => {
|
|
54
53
|
if (typeof props.initState === 'function') {
|
|
55
|
-
|
|
54
|
+
const state = await props.initState()
|
|
55
|
+
applyStateToVariant(attrs.variant, state)
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
slots: () => vm.proxy.$slots,
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import { computed, ref } from 'vue'
|
|
3
|
+
import { Icon } from '@iconify/vue'
|
|
4
|
+
import type { Variant, PropDefinition, AutoPropComponentDefinition } from '../../types'
|
|
5
|
+
|
|
6
|
+
const props = defineProps<{
|
|
7
|
+
variant: Variant
|
|
8
|
+
component: AutoPropComponentDefinition
|
|
9
|
+
definition: PropDefinition
|
|
10
|
+
}>()
|
|
11
|
+
|
|
12
|
+
const comp = computed(() => {
|
|
13
|
+
switch (props.definition.types?.[0]) {
|
|
14
|
+
case 'string':
|
|
15
|
+
return 'HstText'
|
|
16
|
+
case 'number':
|
|
17
|
+
return 'HstNumber'
|
|
18
|
+
case 'boolean':
|
|
19
|
+
return 'HstCheckbox'
|
|
20
|
+
case 'object':
|
|
21
|
+
default:
|
|
22
|
+
return 'HstTextarea'
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
const isJSON = computed(() => comp.value === 'HstTextarea')
|
|
27
|
+
|
|
28
|
+
const invalidValue = ref('')
|
|
29
|
+
|
|
30
|
+
const model = computed({
|
|
31
|
+
get: () => {
|
|
32
|
+
if (invalidValue.value) {
|
|
33
|
+
return invalidValue.value
|
|
34
|
+
}
|
|
35
|
+
let val = props.variant.state._hPropState[props.component.index]?.[props.definition.name]
|
|
36
|
+
if (val && isJSON.value) {
|
|
37
|
+
val = JSON.stringify(val, null, 2)
|
|
38
|
+
}
|
|
39
|
+
return val
|
|
40
|
+
},
|
|
41
|
+
set: (value) => {
|
|
42
|
+
invalidValue.value = ''
|
|
43
|
+
if (isJSON.value) {
|
|
44
|
+
try {
|
|
45
|
+
value = JSON.parse(value)
|
|
46
|
+
} catch (e) {
|
|
47
|
+
invalidValue.value = value
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!props.variant.state._hPropState[props.component.index]) {
|
|
52
|
+
// eslint-disable-next-line vue/no-mutating-props
|
|
53
|
+
props.variant.state._hPropState[props.component.index] = {}
|
|
54
|
+
}
|
|
55
|
+
// eslint-disable-next-line vue/no-mutating-props
|
|
56
|
+
props.variant.state._hPropState[props.component.index][props.definition.name] = value
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
function reset () {
|
|
61
|
+
if (props.variant.state._hPropState[props.component.index]) {
|
|
62
|
+
delete props.variant.state._hPropState[props.component.index][props.definition.name]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const canReset = computed(() => props.variant.state?._hPropState?.[props.component.index] && props.definition.name in props.variant.state._hPropState[props.component.index])
|
|
67
|
+
</script>
|
|
68
|
+
|
|
69
|
+
<template>
|
|
70
|
+
<component
|
|
71
|
+
:is="comp"
|
|
72
|
+
v-if="comp"
|
|
73
|
+
v-model="model"
|
|
74
|
+
:title="definition.name"
|
|
75
|
+
:placeholder="isJSON ? 'Enter JSON' : null"
|
|
76
|
+
>
|
|
77
|
+
<template #actions>
|
|
78
|
+
<Icon
|
|
79
|
+
v-if="invalidValue"
|
|
80
|
+
v-tooltip="'JSON error'"
|
|
81
|
+
icon="carbon:warning-alt"
|
|
82
|
+
class="htw-text-orange-500"
|
|
83
|
+
/>
|
|
84
|
+
|
|
85
|
+
<Icon
|
|
86
|
+
v-tooltip="'Remove override'"
|
|
87
|
+
icon="carbon:erase"
|
|
88
|
+
class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
89
|
+
:class="[
|
|
90
|
+
canReset ? 'htw-opacity-50 hover:htw-opacity-100' : 'htw-opacity-25 htw-pointer-events-none',
|
|
91
|
+
]"
|
|
92
|
+
@click.stop="reset()"
|
|
93
|
+
/>
|
|
94
|
+
</template>
|
|
95
|
+
</component>
|
|
96
|
+
</template>
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import type { AutoPropComponentDefinition, Variant } from '../../types'
|
|
3
|
+
import { Icon } from '@iconify/vue'
|
|
4
|
+
import ControlsComponentPropItem from './ControlsComponentPropItem.vue'
|
|
5
|
+
|
|
6
|
+
defineProps<{
|
|
7
|
+
variant: Variant
|
|
8
|
+
definition: AutoPropComponentDefinition
|
|
9
|
+
}>()
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<template>
|
|
13
|
+
<div>
|
|
14
|
+
<div class="htw-font-mono htw-p-2 htw-flex htw-items-center htw-gap-1">
|
|
15
|
+
<Icon
|
|
16
|
+
v-tooltip="'Auto-detected props'"
|
|
17
|
+
icon="carbon:flash"
|
|
18
|
+
class="htw-w-4 htw-h-4 htw-text-primary-500 htw-flex-none"
|
|
19
|
+
/>
|
|
20
|
+
<div>
|
|
21
|
+
<span class="htw-opacity-30"><</span>{{ definition.name }}<span class="htw-opacity-30">></span>
|
|
22
|
+
</div>
|
|
23
|
+
</div>
|
|
24
|
+
|
|
25
|
+
<ControlsComponentPropItem
|
|
26
|
+
v-for="prop of definition.props"
|
|
27
|
+
:key="prop.name"
|
|
28
|
+
:variant="variant"
|
|
29
|
+
:component="definition"
|
|
30
|
+
:definition="prop"
|
|
31
|
+
/>
|
|
32
|
+
</div>
|
|
33
|
+
</template>
|
|
@@ -3,7 +3,7 @@ import { ref, computed, nextTick, onMounted } from 'vue'
|
|
|
3
3
|
import { Icon } from '@iconify/vue'
|
|
4
4
|
import { useStorage, onClickOutside, useTimeoutFn } from '@vueuse/core'
|
|
5
5
|
import BaseSelect from '../base/BaseSelect.vue'
|
|
6
|
-
import { applyStateToVariant, clone, toRawDeep } from '../../util/state'
|
|
6
|
+
import { applyStateToVariant, clone, omit, toRawDeep } from '../../util/state'
|
|
7
7
|
import type { Story, Variant } from '../../types'
|
|
8
8
|
|
|
9
9
|
const DEFAULT_ID = 'default'
|
|
@@ -15,7 +15,9 @@ const props = defineProps<{
|
|
|
15
15
|
|
|
16
16
|
const saveId = computed(() => `${props.story.id}:${props.variant.id}`)
|
|
17
17
|
|
|
18
|
-
const
|
|
18
|
+
const omitKeys = ['_hPropDefs']
|
|
19
|
+
|
|
20
|
+
const defaultState = clone(omit(toRawDeep(props.variant.state), omitKeys))
|
|
19
21
|
|
|
20
22
|
const selectedOption = useStorage<string>(
|
|
21
23
|
`_histoire-presets/${saveId.value}/selected`,
|
|
@@ -37,14 +39,14 @@ const presetsOptions = computed(() => {
|
|
|
37
39
|
|
|
38
40
|
function resetState () {
|
|
39
41
|
selectedOption.value = DEFAULT_ID
|
|
40
|
-
applyStateToVariant(props.variant, defaultState)
|
|
42
|
+
applyStateToVariant(props.variant, clone(defaultState))
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
function applyPreset (id) {
|
|
44
46
|
if (id === DEFAULT_ID) {
|
|
45
47
|
resetState()
|
|
46
48
|
} else if (presetStates.value.has(id)) {
|
|
47
|
-
applyStateToVariant(props.variant, presetStates.value.get(id).state)
|
|
49
|
+
applyStateToVariant(props.variant, clone(presetStates.value.get(id).state))
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
52
|
|
|
@@ -62,7 +64,7 @@ const isEditing = ref(false)
|
|
|
62
64
|
async function createPreset () {
|
|
63
65
|
const id = (new Date()).getTime().toString()
|
|
64
66
|
|
|
65
|
-
presetStates.value.set(id, { state: clone(toRawDeep(props.variant.state)), label: 'New preset' })
|
|
67
|
+
presetStates.value.set(id, { state: clone(omit(toRawDeep(props.variant.state), omitKeys)), label: 'New preset' })
|
|
66
68
|
selectedOption.value = id
|
|
67
69
|
isEditing.value = true
|
|
68
70
|
await nextTick()
|
|
@@ -78,7 +80,7 @@ async function savePreset () {
|
|
|
78
80
|
if (!canEdit.value) return
|
|
79
81
|
|
|
80
82
|
const preset = presetStates.value.get(selectedOption.value)
|
|
81
|
-
preset.state = clone(toRawDeep(props.variant.state))
|
|
83
|
+
preset.state = clone(omit(toRawDeep(props.variant.state), omitKeys))
|
|
82
84
|
savedNotif.value = true
|
|
83
85
|
savedTimeout.start()
|
|
84
86
|
}
|
|
@@ -5,6 +5,7 @@ import type { Story, Variant } from '../../types'
|
|
|
5
5
|
import SandboxVue3 from '../sandbox/SandboxVue3.vue'
|
|
6
6
|
import BaseEmpty from '../base/BaseEmpty.vue'
|
|
7
7
|
import StatePresets from './StatePresets.vue'
|
|
8
|
+
import ControlsComponentProps from './ControlsComponentProps.vue'
|
|
8
9
|
|
|
9
10
|
defineProps({
|
|
10
11
|
variant: {
|
|
@@ -22,11 +23,11 @@ defineProps({
|
|
|
22
23
|
<template>
|
|
23
24
|
<div
|
|
24
25
|
data-test-id="story-controls"
|
|
25
|
-
class="htw-flex htw-flex-col"
|
|
26
|
+
class="htw-flex htw-flex-col htw-divide-y htw-divide-gray-100 dark:htw-divide-gray-750"
|
|
26
27
|
>
|
|
27
28
|
<!-- Toolbar -->
|
|
28
29
|
<div
|
|
29
|
-
class="htw-h-9 htw-flex-none htw-
|
|
30
|
+
class="htw-h-9 htw-flex-none htw-px-2 htw-flex htw-items-center"
|
|
30
31
|
>
|
|
31
32
|
<StatePresets
|
|
32
33
|
:story="story"
|
|
@@ -40,14 +41,28 @@ defineProps({
|
|
|
40
41
|
slot-name="controls"
|
|
41
42
|
:variant="variant"
|
|
42
43
|
:story="story"
|
|
44
|
+
class="htw-flex-none"
|
|
43
45
|
/>
|
|
44
46
|
|
|
45
|
-
<BaseEmpty v-else>
|
|
47
|
+
<BaseEmpty v-else-if="!variant.state?._hPropDefs?.length">
|
|
46
48
|
<Icon
|
|
47
49
|
icon="carbon:audio-console"
|
|
48
50
|
class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
|
|
49
51
|
/>
|
|
50
52
|
<span>No controls available for this story</span>
|
|
51
53
|
</BaseEmpty>
|
|
54
|
+
|
|
55
|
+
<!-- Auto props -->
|
|
56
|
+
<div
|
|
57
|
+
v-if="variant.state?._hPropDefs?.length"
|
|
58
|
+
>
|
|
59
|
+
<ControlsComponentProps
|
|
60
|
+
v-for="(def, index) of variant.state._hPropDefs"
|
|
61
|
+
:key="index"
|
|
62
|
+
:variant="variant"
|
|
63
|
+
:definition="def"
|
|
64
|
+
class="htw-flex-none htw-my-2"
|
|
65
|
+
/>
|
|
66
|
+
</div>
|
|
52
67
|
</div>
|
|
53
68
|
</template>
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<script lang="ts" setup>
|
|
2
2
|
import { App, createApp, onMounted, onUnmounted, PropType, ref, watch } from 'vue'
|
|
3
|
-
import { Story, Variant } from '../../types'
|
|
3
|
+
import { Story, Variant, PropDefinition, AutoPropComponentDefinition } from '../../types'
|
|
4
|
+
import { getTagName } from '../../codegen/vue3'
|
|
5
|
+
import { applyStateToVariant } from '../../util/state'
|
|
4
6
|
// @ts-expect-error virtual module id
|
|
5
7
|
import * as setup from '$histoire-setup'
|
|
6
8
|
|
|
@@ -34,15 +36,81 @@ async function mountVariant () {
|
|
|
34
36
|
|
|
35
37
|
await props.variant.initState()
|
|
36
38
|
|
|
39
|
+
let lastPropsTypesSnapshot: string
|
|
40
|
+
|
|
37
41
|
app = createApp({
|
|
38
42
|
name: 'SandboxVue3',
|
|
39
43
|
|
|
40
44
|
render: () => {
|
|
41
|
-
|
|
45
|
+
const vnodes = props.variant.slots()?.[props.slotName]?.({
|
|
42
46
|
state: props.variant.state ?? {},
|
|
43
47
|
}) ?? props.story.slots()?.[props.slotName]?.({
|
|
44
48
|
state: props.variant.state ?? {},
|
|
45
49
|
})
|
|
50
|
+
|
|
51
|
+
// Auto detect props
|
|
52
|
+
if (props.slotName === 'default') {
|
|
53
|
+
const propsTypes: AutoPropComponentDefinition[] = vnodes.filter(vnode => typeof vnode.type === 'object').map((vnode, index) => {
|
|
54
|
+
const propDefs: PropDefinition[] = []
|
|
55
|
+
for (const key in vnode.type.props) {
|
|
56
|
+
const prop = vnode.type.props[key]
|
|
57
|
+
let types
|
|
58
|
+
let defaultValue
|
|
59
|
+
if (prop) {
|
|
60
|
+
const rawTypes = Array.isArray(prop.type) ? prop.type : [prop.type]
|
|
61
|
+
types = rawTypes.map(t => {
|
|
62
|
+
switch (t) {
|
|
63
|
+
case String:
|
|
64
|
+
return 'string'
|
|
65
|
+
case Number:
|
|
66
|
+
return 'number'
|
|
67
|
+
case Boolean:
|
|
68
|
+
return 'boolean'
|
|
69
|
+
case Object:
|
|
70
|
+
return 'object'
|
|
71
|
+
case Array:
|
|
72
|
+
return 'array'
|
|
73
|
+
default:
|
|
74
|
+
return 'unknown'
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
defaultValue = typeof prop.default === 'function' ? prop.default.toString() : prop.default
|
|
78
|
+
}
|
|
79
|
+
propDefs.push({
|
|
80
|
+
name: key,
|
|
81
|
+
types,
|
|
82
|
+
required: prop?.required,
|
|
83
|
+
default: defaultValue,
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
// Props overrides
|
|
87
|
+
if (props.variant.state?._hPropState?.[index]?.[key]) {
|
|
88
|
+
vnode.props[key] = props.variant.state._hPropState[index][key]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
name: getTagName(vnode),
|
|
94
|
+
index,
|
|
95
|
+
props: propDefs,
|
|
96
|
+
} as AutoPropComponentDefinition
|
|
97
|
+
}).filter(def => def.props.length)
|
|
98
|
+
|
|
99
|
+
const snapshot = JSON.stringify(propsTypes)
|
|
100
|
+
if (!lastPropsTypesSnapshot || lastPropsTypesSnapshot !== snapshot) {
|
|
101
|
+
applyStateToVariant(props.variant, {
|
|
102
|
+
_hPropDefs: propsTypes,
|
|
103
|
+
})
|
|
104
|
+
if (!props.variant.state._hPropState) {
|
|
105
|
+
applyStateToVariant(props.variant, {
|
|
106
|
+
_hPropState: {},
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
lastPropsTypesSnapshot = snapshot
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return vnodes
|
|
46
114
|
},
|
|
47
115
|
})
|
|
48
116
|
|
|
@@ -25,6 +25,18 @@
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
.__histoire-fade {
|
|
28
|
+
&-enter-active,
|
|
29
|
+
&-leave-active {
|
|
30
|
+
transition: opacity .15s;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
&-enter-from,
|
|
34
|
+
&-leave-to {
|
|
35
|
+
opacity: 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.__histoire-fade-bottom {
|
|
28
40
|
&-enter-active,
|
|
29
41
|
&-leave-active {
|
|
30
42
|
transition: transform .15s, opacity .15s;
|
package/src/client/app/types.ts
CHANGED
|
@@ -9,18 +9,20 @@ export interface StoryFile {
|
|
|
9
9
|
path: string[]
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
export type StoryLayout = {
|
|
13
|
+
type: 'single'
|
|
14
|
+
iframe?: boolean
|
|
15
|
+
} | {
|
|
16
|
+
type: 'grid'
|
|
17
|
+
width?: number | string
|
|
18
|
+
}
|
|
19
|
+
|
|
12
20
|
export interface Story {
|
|
13
21
|
id: string
|
|
14
22
|
title: string
|
|
15
23
|
group?: string
|
|
16
24
|
variants: Variant[]
|
|
17
|
-
layout?:
|
|
18
|
-
type: 'single'
|
|
19
|
-
iframe?: boolean
|
|
20
|
-
} | {
|
|
21
|
-
type: 'grid'
|
|
22
|
-
width?: number | string
|
|
23
|
-
}
|
|
25
|
+
layout?: StoryLayout
|
|
24
26
|
icon?: string
|
|
25
27
|
iconColor?: string
|
|
26
28
|
docsOnly?: boolean
|
|
@@ -81,3 +83,16 @@ export interface PreviewSettings {
|
|
|
81
83
|
backgroundColor: string
|
|
82
84
|
checkerboard: boolean
|
|
83
85
|
}
|
|
86
|
+
|
|
87
|
+
export interface PropDefinition {
|
|
88
|
+
name: string
|
|
89
|
+
types?: string[]
|
|
90
|
+
required?: boolean
|
|
91
|
+
default?: any
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface AutoPropComponentDefinition {
|
|
95
|
+
name: string
|
|
96
|
+
index: number
|
|
97
|
+
props: PropDefinition[]
|
|
98
|
+
}
|
|
@@ -26,7 +26,7 @@ export function clone (data) {
|
|
|
26
26
|
try {
|
|
27
27
|
return structuredClone(data)
|
|
28
28
|
} catch (e) {
|
|
29
|
-
console.
|
|
29
|
+
console.warn(e, `Fallback to JSON cloning`)
|
|
30
30
|
try {
|
|
31
31
|
return JSON.parse(JSON.stringify(data))
|
|
32
32
|
} catch (e) {
|
|
@@ -36,10 +36,20 @@ export function clone (data) {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
export function omit (data, keys: string[]) {
|
|
40
|
+
const copy = {}
|
|
41
|
+
for (const key in data) {
|
|
42
|
+
if (!keys.includes(key)) {
|
|
43
|
+
copy[key] = data[key]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return copy
|
|
47
|
+
}
|
|
48
|
+
|
|
39
49
|
export function applyStateToVariant (variant: Variant, state: any) {
|
|
40
50
|
if (variant.state) {
|
|
41
51
|
for (const key in state) {
|
|
42
|
-
if (typeof variant.state[key] === 'object') {
|
|
52
|
+
if (variant.state[key] && !key.startsWith('_h') && typeof variant.state[key] === 'object' && !Array.isArray(variant.state[key])) {
|
|
43
53
|
Object.assign(variant.state[key], state[key])
|
|
44
54
|
} else {
|
|
45
55
|
variant.state[key] = state[key]
|