histoire 0.4.4 → 0.5.0
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/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 +551 -622
- package/dist/bundled/global-components.js +16 -6
- package/dist/bundled/index2.js +114 -43
- package/dist/bundled/sandbox.js +3 -11
- package/dist/bundled/state.js +55 -0
- package/dist/bundled/story.js +1 -1
- 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/base/BaseSelect.vue +72 -0
- 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 +195 -0
- package/dist/client/app/components/panel/StoryControls.vue +31 -2
- package/dist/client/app/components/sandbox/SandboxVue3.vue +70 -2
- package/dist/client/app/components/story/StoryVariantSinglePreviewRemote.vue +2 -14
- package/dist/client/app/index.js +5 -0
- package/dist/client/app/sandbox.js +2 -11
- package/dist/client/app/style/transitions.pcss +12 -0
- package/dist/client/app/types.d.ts +11 -0
- package/dist/client/app/util/mapping.js +11 -1
- package/dist/client/app/util/state.d.ts +5 -0
- package/dist/client/app/util/state.js +55 -0
- package/dist/client/app/util/variant.d.ts +1 -1
- package/dist/node/build.js +4 -6
- package/dist/node/collect/index.js +6 -0
- package/dist/node/collect/worker.js +0 -4
- package/dist/node/commands/preview.js +1 -1
- package/dist/node/config.d.ts +23 -23
- package/dist/node/plugin.d.ts +2 -2
- package/dist/node/server.js +66 -47
- package/dist/node/vite.d.ts +1 -1
- package/dist/node/vite.js +10 -2
- package/dist/style.css +162 -50
- package/package.json +4 -4
- 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/base/BaseSelect.vue +72 -0
- 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 +195 -0
- package/src/client/app/components/panel/StoryControls.vue +31 -2
- package/src/client/app/components/sandbox/SandboxVue3.vue +70 -2
- package/src/client/app/components/story/StoryVariantSinglePreviewRemote.vue +2 -14
- package/src/client/app/index.ts +6 -0
- package/src/client/app/sandbox.ts +2 -10
- package/src/client/app/style/transitions.pcss +12 -0
- package/src/client/app/types.ts +13 -0
- package/src/client/app/util/mapping.ts +12 -1
- package/src/client/app/util/state.ts +61 -0
- package/src/client/app/util/variant.ts +1 -1
- package/src/node/build.ts +4 -6
- package/src/node/collect/index.ts +7 -0
- package/src/node/collect/worker.ts +0 -4
- package/src/node/commands/preview.ts +1 -1
- package/src/node/server.ts +72 -49
- package/src/node/vite.ts +11 -2
- package/vite.config.ts +25 -0
- package/dist/bundled/dark.js +0 -4
- package/dist/bundled/reactivity.js +0 -96
- package/dist/client/app/util/reactivity.d.ts +0 -1
- package/dist/client/app/util/reactivity.js +0 -16
- package/src/client/app/util/reactivity.ts +0 -22
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import { Dropdown as VDropdown } from 'floating-vue'
|
|
3
|
+
import { computed, ComputedRef } from 'vue'
|
|
4
|
+
import { Icon } from '@iconify/vue'
|
|
5
|
+
|
|
6
|
+
const props = defineProps<{
|
|
7
|
+
modelValue: string
|
|
8
|
+
options: Record<string, string> | Array<string>
|
|
9
|
+
}>()
|
|
10
|
+
|
|
11
|
+
const emits = defineEmits<{
|
|
12
|
+
(e: 'update:modelValue', value: string): void
|
|
13
|
+
(e: 'select', value: string): void
|
|
14
|
+
}>()
|
|
15
|
+
|
|
16
|
+
const formattedOptions: ComputedRef<Record<string, string>> = computed(() => {
|
|
17
|
+
if (Array.isArray(props.options)) {
|
|
18
|
+
return Object.fromEntries(props.options.map((value) => [value, value]))
|
|
19
|
+
}
|
|
20
|
+
return props.options
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const selectedLabel = computed(() => formattedOptions.value[props.modelValue])
|
|
24
|
+
|
|
25
|
+
function selectValue (value: string, hide: () => void) {
|
|
26
|
+
emits('update:modelValue', value)
|
|
27
|
+
emits('select', value)
|
|
28
|
+
hide()
|
|
29
|
+
}
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<template>
|
|
33
|
+
<VDropdown
|
|
34
|
+
auto-size
|
|
35
|
+
>
|
|
36
|
+
<div
|
|
37
|
+
class="htw-cursor-pointer htw-w-full htw-outline-none htw-px-2 htw-h-[27px] -htw-my-1 htw-border htw-border-solid htw-border-black/25 dark:htw-border-white/25 hover:htw-border-primary-500 dark:hover:htw-border-primary-500 htw-rounded-sm htw-flex htw-gap-2 htw-items-center htw-leading-normal"
|
|
38
|
+
>
|
|
39
|
+
<div class="htw-flex-1 htw-truncate">
|
|
40
|
+
<slot :label="selectedLabel">
|
|
41
|
+
{{ selectedLabel }}
|
|
42
|
+
</slot>
|
|
43
|
+
</div>
|
|
44
|
+
<Icon
|
|
45
|
+
icon="carbon:chevron-sort"
|
|
46
|
+
class="htw-w-4 htw-h-4 htw-flex-none htw-ml-auto"
|
|
47
|
+
/>
|
|
48
|
+
</div>
|
|
49
|
+
<template #popper="{ hide }">
|
|
50
|
+
<div class="htw-flex htw-flex-col htw-bg-gray-50 dark:htw-bg-gray-700">
|
|
51
|
+
<div
|
|
52
|
+
v-for="( label, value ) in formattedOptions"
|
|
53
|
+
v-bind="{ ...$attrs, class: null, style: null }"
|
|
54
|
+
:key="label"
|
|
55
|
+
class="htw-px-2 htw-py-1 htw-cursor-pointer hover:htw-bg-primary-100 dark:hover:htw-bg-primary-700"
|
|
56
|
+
:class="{
|
|
57
|
+
'htw-bg-primary-200 dark:htw-bg-primary-800': props.modelValue === value,
|
|
58
|
+
}"
|
|
59
|
+
@click="selectValue(value, hide)"
|
|
60
|
+
>
|
|
61
|
+
<slot
|
|
62
|
+
name="option"
|
|
63
|
+
:label="label"
|
|
64
|
+
:value="value"
|
|
65
|
+
>
|
|
66
|
+
{{ label }}
|
|
67
|
+
</slot>
|
|
68
|
+
</div>
|
|
69
|
+
</div>
|
|
70
|
+
</template>
|
|
71
|
+
</VDropdown>
|
|
72
|
+
</template>
|
|
@@ -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>
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import { ref, computed, nextTick, onMounted } from 'vue'
|
|
3
|
+
import { Icon } from '@iconify/vue'
|
|
4
|
+
import { useStorage, onClickOutside, useTimeoutFn } from '@vueuse/core'
|
|
5
|
+
import BaseSelect from '../base/BaseSelect.vue'
|
|
6
|
+
import { applyStateToVariant, clone, omit, toRawDeep } from '../../util/state'
|
|
7
|
+
import type { Story, Variant } from '../../types'
|
|
8
|
+
|
|
9
|
+
const DEFAULT_ID = 'default'
|
|
10
|
+
|
|
11
|
+
const props = defineProps<{
|
|
12
|
+
story: Story
|
|
13
|
+
variant: Variant
|
|
14
|
+
}>()
|
|
15
|
+
|
|
16
|
+
const saveId = computed(() => `${props.story.id}:${props.variant.id}`)
|
|
17
|
+
|
|
18
|
+
const omitKeys = ['_hPropDefs']
|
|
19
|
+
|
|
20
|
+
const defaultState = clone(omit(toRawDeep(props.variant.state), omitKeys))
|
|
21
|
+
|
|
22
|
+
const selectedOption = useStorage<string>(
|
|
23
|
+
`_histoire-presets/${saveId.value}/selected`,
|
|
24
|
+
DEFAULT_ID,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
const presetStates = useStorage<Map<string, { state: Record<string, unknown>, label: string }>>(
|
|
28
|
+
`_histoire-presets/${saveId.value}/states`,
|
|
29
|
+
new Map(),
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
const presetsOptions = computed(() => {
|
|
33
|
+
const options = { [DEFAULT_ID]: 'Initial state' }
|
|
34
|
+
presetStates.value.forEach((value, key) => {
|
|
35
|
+
options[key] = value.label
|
|
36
|
+
})
|
|
37
|
+
return options
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
function resetState () {
|
|
41
|
+
selectedOption.value = DEFAULT_ID
|
|
42
|
+
applyStateToVariant(props.variant, clone(defaultState))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function applyPreset (id) {
|
|
46
|
+
if (id === DEFAULT_ID) {
|
|
47
|
+
resetState()
|
|
48
|
+
} else if (presetStates.value.has(id)) {
|
|
49
|
+
applyStateToVariant(props.variant, clone(presetStates.value.get(id).state))
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
onMounted(() => {
|
|
54
|
+
if (selectedOption.value !== DEFAULT_ID) {
|
|
55
|
+
applyPreset(selectedOption.value)
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const input = ref<HTMLInputElement>()
|
|
60
|
+
const select = ref<HTMLInputElement>()
|
|
61
|
+
const canEdit = computed(() => selectedOption.value !== DEFAULT_ID)
|
|
62
|
+
const isEditing = ref(false)
|
|
63
|
+
|
|
64
|
+
async function createPreset () {
|
|
65
|
+
const id = (new Date()).getTime().toString()
|
|
66
|
+
|
|
67
|
+
presetStates.value.set(id, { state: clone(omit(toRawDeep(props.variant.state), omitKeys)), label: 'New preset' })
|
|
68
|
+
selectedOption.value = id
|
|
69
|
+
isEditing.value = true
|
|
70
|
+
await nextTick()
|
|
71
|
+
input.value.select()
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const savedNotif = ref(false)
|
|
75
|
+
const savedTimeout = useTimeoutFn(() => {
|
|
76
|
+
savedNotif.value = false
|
|
77
|
+
}, 1000)
|
|
78
|
+
|
|
79
|
+
async function savePreset () {
|
|
80
|
+
if (!canEdit.value) return
|
|
81
|
+
|
|
82
|
+
const preset = presetStates.value.get(selectedOption.value)
|
|
83
|
+
preset.state = clone(omit(toRawDeep(props.variant.state), omitKeys))
|
|
84
|
+
savedNotif.value = true
|
|
85
|
+
savedTimeout.start()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function deletePreset (id) {
|
|
89
|
+
if (!confirm('Are you sure you want to delete this preset?')) {
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (selectedOption.value === id) {
|
|
94
|
+
resetState()
|
|
95
|
+
}
|
|
96
|
+
presetStates.value.delete(id)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function startEditing () {
|
|
100
|
+
if (!canEdit.value) {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
isEditing.value = true
|
|
105
|
+
await nextTick()
|
|
106
|
+
input.value.select()
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function stopEditing () {
|
|
110
|
+
isEditing.value = false
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
onClickOutside(select, stopEditing)
|
|
114
|
+
</script>
|
|
115
|
+
|
|
116
|
+
<template>
|
|
117
|
+
<div class="htw-flex htw-gap-2 htw-w-full htw-items-center">
|
|
118
|
+
<div
|
|
119
|
+
ref="select"
|
|
120
|
+
class="htw-flex-1 htw-min-w-0"
|
|
121
|
+
>
|
|
122
|
+
<BaseSelect
|
|
123
|
+
v-model="selectedOption"
|
|
124
|
+
:options="presetsOptions"
|
|
125
|
+
@dblclick="startEditing()"
|
|
126
|
+
@keydown.enter="stopEditing()"
|
|
127
|
+
@keydown.escape="stopEditing()"
|
|
128
|
+
@select="id => applyPreset(id)"
|
|
129
|
+
>
|
|
130
|
+
<template #default="{ label }">
|
|
131
|
+
<input
|
|
132
|
+
v-if="isEditing"
|
|
133
|
+
ref="input"
|
|
134
|
+
v-model="presetStates.get(selectedOption).label"
|
|
135
|
+
type="text"
|
|
136
|
+
class="htw-text-inherit htw-bg-transparent htw-w-full htw-h-full htw-outline-none"
|
|
137
|
+
@click.stop.prevent
|
|
138
|
+
>
|
|
139
|
+
|
|
140
|
+
<div
|
|
141
|
+
v-else
|
|
142
|
+
class="htw-flex htw-items-center htw-gap-2"
|
|
143
|
+
>
|
|
144
|
+
<span class="htw-flex-1 htw-truncate">
|
|
145
|
+
{{ label }}
|
|
146
|
+
</span>
|
|
147
|
+
<Icon
|
|
148
|
+
v-if="canEdit"
|
|
149
|
+
v-tooltip="'Rename this preset'"
|
|
150
|
+
icon="carbon:edit"
|
|
151
|
+
class="htw-flex-none htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
152
|
+
@click.stop="startEditing()"
|
|
153
|
+
/>
|
|
154
|
+
</div>
|
|
155
|
+
</template>
|
|
156
|
+
|
|
157
|
+
<template #option="{ label, value }">
|
|
158
|
+
<div class="htw-flex htw-gap-2 htw-items-center">
|
|
159
|
+
<span
|
|
160
|
+
class="htw-flex-1 htw-truncate"
|
|
161
|
+
>{{ label }}</span>
|
|
162
|
+
<Icon
|
|
163
|
+
v-if="value !== DEFAULT_ID"
|
|
164
|
+
v-tooltip="'Delete this preset'"
|
|
165
|
+
icon="carbon:trash-can"
|
|
166
|
+
class="htw-flex-none htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
167
|
+
@click.stop="deletePreset(value)"
|
|
168
|
+
/>
|
|
169
|
+
</div>
|
|
170
|
+
</template>
|
|
171
|
+
</BaseSelect>
|
|
172
|
+
</div>
|
|
173
|
+
<Icon
|
|
174
|
+
v-tooltip="savedNotif ? 'Saved!' : canEdit ? 'Save to preset' : null"
|
|
175
|
+
:icon="savedNotif ? 'carbon:checkmark' : 'carbon:save'"
|
|
176
|
+
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"
|
|
177
|
+
:class="[
|
|
178
|
+
canEdit ? 'htw-opacity-50 hover:htw-opacity-100' : 'htw-opacity-25 htw-pointer-events-none',
|
|
179
|
+
]"
|
|
180
|
+
@click="savePreset()"
|
|
181
|
+
/>
|
|
182
|
+
<Icon
|
|
183
|
+
v-tooltip="'Create new preset'"
|
|
184
|
+
icon="carbon:add-alt"
|
|
185
|
+
class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
186
|
+
@click="createPreset()"
|
|
187
|
+
/>
|
|
188
|
+
<Icon
|
|
189
|
+
v-tooltip="'Reset to initial state'"
|
|
190
|
+
icon="carbon:reset"
|
|
191
|
+
class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
192
|
+
@click="resetState()"
|
|
193
|
+
/>
|
|
194
|
+
</div>
|
|
195
|
+
</template>
|
|
@@ -4,6 +4,8 @@ import { Icon } from '@iconify/vue'
|
|
|
4
4
|
import type { Story, Variant } from '../../types'
|
|
5
5
|
import SandboxVue3 from '../sandbox/SandboxVue3.vue'
|
|
6
6
|
import BaseEmpty from '../base/BaseEmpty.vue'
|
|
7
|
+
import StatePresets from './StatePresets.vue'
|
|
8
|
+
import ControlsComponentProps from './ControlsComponentProps.vue'
|
|
7
9
|
|
|
8
10
|
defineProps({
|
|
9
11
|
variant: {
|
|
@@ -19,21 +21,48 @@ defineProps({
|
|
|
19
21
|
</script>
|
|
20
22
|
|
|
21
23
|
<template>
|
|
22
|
-
<div
|
|
24
|
+
<div
|
|
25
|
+
data-test-id="story-controls"
|
|
26
|
+
class="htw-flex htw-flex-col htw-divide-y htw-divide-gray-100 dark:htw-divide-gray-750"
|
|
27
|
+
>
|
|
28
|
+
<!-- Toolbar -->
|
|
29
|
+
<div
|
|
30
|
+
class="htw-h-9 htw-flex-none htw-px-2 htw-flex htw-items-center"
|
|
31
|
+
>
|
|
32
|
+
<StatePresets
|
|
33
|
+
:story="story"
|
|
34
|
+
:variant="variant"
|
|
35
|
+
/>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
23
38
|
<!-- Custom controls -->
|
|
24
39
|
<SandboxVue3
|
|
25
40
|
v-if="variant.slots().controls || story.slots().controls"
|
|
26
41
|
slot-name="controls"
|
|
27
42
|
:variant="variant"
|
|
28
43
|
:story="story"
|
|
44
|
+
class="htw-flex-none"
|
|
29
45
|
/>
|
|
30
46
|
|
|
31
|
-
<BaseEmpty v-else>
|
|
47
|
+
<BaseEmpty v-else-if="!variant.state?._hPropDefs?.length">
|
|
32
48
|
<Icon
|
|
33
49
|
icon="carbon:audio-console"
|
|
34
50
|
class="htw-w-8 htw-h-8 htw-opacity-50 htw-mb-6"
|
|
35
51
|
/>
|
|
36
52
|
<span>No controls available for this story</span>
|
|
37
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>
|
|
38
67
|
</div>
|
|
39
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
|
|
|
@@ -6,11 +6,11 @@ import { STATE_SYNC, PREVIEW_SETTINGS_SYNC, SANDBOX_READY, EVENT_SEND } from '..
|
|
|
6
6
|
import type { Story, Variant } from '../../types'
|
|
7
7
|
import HatchedPattern from '../misc/HatchedPattern.vue'
|
|
8
8
|
import CheckerboardPattern from '../misc/CheckerboardPattern.vue'
|
|
9
|
-
import { toRawDeep } from '../../util/reactivity'
|
|
10
9
|
import { getSandboxUrl } from '../sandbox/lib'
|
|
11
10
|
import { usePreviewSettingsStore } from '../../stores/preview-settings'
|
|
12
11
|
import { HstEvent, useEventsStore } from '../../stores/events'
|
|
13
12
|
import StoryResponsivePreview from './StoryResponsivePreview.vue'
|
|
13
|
+
import { applyStateToVariant, toRawDeep } from '../../util/state'
|
|
14
14
|
|
|
15
15
|
const props = defineProps<{
|
|
16
16
|
story: Story
|
|
@@ -65,19 +65,7 @@ useEventListener(window, 'message', (event) => {
|
|
|
65
65
|
|
|
66
66
|
function updateVariantState (state: any) {
|
|
67
67
|
synced = true
|
|
68
|
-
|
|
69
|
-
for (const key in state) {
|
|
70
|
-
if (typeof props.variant.state[key] === 'object') {
|
|
71
|
-
Object.assign(props.variant.state[key], state[key])
|
|
72
|
-
} else {
|
|
73
|
-
// eslint-disable-next-line vue/no-mutating-props
|
|
74
|
-
props.variant.state[key] = state[key]
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
// eslint-disable-next-line vue/no-mutating-props
|
|
79
|
-
props.variant.state = state
|
|
80
|
-
}
|
|
68
|
+
applyStateToVariant(props.variant, state)
|
|
81
69
|
}
|
|
82
70
|
|
|
83
71
|
function logEvent (event: HstEvent) {
|
package/src/client/app/index.ts
CHANGED
|
@@ -29,4 +29,10 @@ export function mountMainApp () {
|
|
|
29
29
|
app.use(router)
|
|
30
30
|
registerGlobalComponents(app)
|
|
31
31
|
app.mount('#app')
|
|
32
|
+
|
|
33
|
+
// @ts-expect-error vite hot api
|
|
34
|
+
if (import.meta.hot) {
|
|
35
|
+
// @ts-expect-error vite hot api
|
|
36
|
+
import.meta.hot.send('histoire:mount', {})
|
|
37
|
+
}
|
|
32
38
|
}
|
|
@@ -11,7 +11,7 @@ import { PREVIEW_SETTINGS_SYNC, STATE_SYNC, SANDBOX_READY } from './util/const.j
|
|
|
11
11
|
import { applyPreviewSettings } from './util/preview-settings.js'
|
|
12
12
|
import { isDark } from './util/dark.js'
|
|
13
13
|
import { histoireConfig } from './util/config.js'
|
|
14
|
-
import { toRawDeep } from './util/
|
|
14
|
+
import { applyStateToVariant, toRawDeep } from './util/state'
|
|
15
15
|
|
|
16
16
|
const query = parseQuery(window.location.search)
|
|
17
17
|
const file = ref<StoryFile>(mapFile(files.find(f => f.id === query.storyId)))
|
|
@@ -30,15 +30,7 @@ const app = createApp({
|
|
|
30
30
|
if (event.data?.type === STATE_SYNC) {
|
|
31
31
|
if (!mounted) return
|
|
32
32
|
synced = true
|
|
33
|
-
|
|
34
|
-
for (const key in event.data.state) {
|
|
35
|
-
if (typeof variant.value.state[key] === 'object') {
|
|
36
|
-
Object.assign(variant.value.state[key], event.data.state[key])
|
|
37
|
-
} else {
|
|
38
|
-
variant.value.state[key] = event.data.state[key]
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
33
|
+
applyStateToVariant(variant.value, event.data.state)
|
|
42
34
|
} else if (event.data?.type === PREVIEW_SETTINGS_SYNC) {
|
|
43
35
|
applyPreviewSettings(event.data.settings)
|
|
44
36
|
}
|
|
@@ -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;
|