histoire 0.4.3 → 0.4.6
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/StoryView.js +376 -136
- package/dist/bundled/global-components.js +11 -1
- package/dist/bundled/index2.js +3 -0
- package/dist/bundled/sandbox.js +2 -10
- package/dist/bundled/{reactivity.js → state.js} +27 -1
- package/dist/bundled/story.js +1 -1
- package/dist/client/app/components/base/BaseSelect.vue +72 -0
- package/dist/client/app/components/panel/StatePresets.vue +193 -0
- package/dist/client/app/components/panel/StoryControls.vue +15 -1
- 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/util/mapping.js +11 -1
- package/dist/client/app/util/state.d.ts +4 -0
- package/dist/client/app/util/state.js +46 -0
- package/dist/client/app/util/variant.d.ts +1 -1
- package/dist/node/build.js +3 -3
- package/dist/node/collect/worker.js +0 -4
- package/dist/node/commands/preview.js +1 -1
- package/dist/node/server.js +53 -45
- package/dist/node/vite.d.ts +1 -1
- package/dist/node/vite.js +11 -2
- package/dist/style.css +70 -37
- package/package.json +4 -4
- package/src/client/app/components/base/BaseSelect.vue +72 -0
- package/src/client/app/components/panel/StatePresets.vue +193 -0
- package/src/client/app/components/panel/StoryControls.vue +15 -1
- 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/util/mapping.ts +12 -1
- package/src/client/app/util/state.ts +51 -0
- package/src/client/app/util/variant.ts +1 -1
- package/src/node/build.ts +3 -3
- package/src/node/collect/worker.ts +0 -4
- package/src/node/commands/preview.ts +1 -1
- package/src/node/server.ts +55 -47
- package/src/node/vite.ts +12 -2
- package/vite.config.ts +25 -0
- 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
|
@@ -19,6 +19,16 @@ var __spreadValues = (a, b) => {
|
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
20
|
import { markRaw, defineComponent, useAttrs, getCurrentInstance, inject, watch, computed, provide, h } from "vue";
|
|
21
21
|
import { registerVueComponents } from "@histoire/controls";
|
|
22
|
+
const copiedFromExistingVariant = [
|
|
23
|
+
"state",
|
|
24
|
+
"slots",
|
|
25
|
+
"initState",
|
|
26
|
+
"source",
|
|
27
|
+
"responsiveDisabled",
|
|
28
|
+
"setupApp",
|
|
29
|
+
"configReady",
|
|
30
|
+
"previewReady"
|
|
31
|
+
];
|
|
22
32
|
function mapFile(file, existingFile) {
|
|
23
33
|
const result = __spreadProps(__spreadValues({}, file), {
|
|
24
34
|
component: markRaw(file.component),
|
|
@@ -34,7 +44,7 @@ function mapFile(file, existingFile) {
|
|
|
34
44
|
const variant = result.story.variants[index];
|
|
35
45
|
const existingVariant = existingFile.story.variants[index];
|
|
36
46
|
if (existingVariant) {
|
|
37
|
-
for (const key
|
|
47
|
+
for (const key of copiedFromExistingVariant) {
|
|
38
48
|
if (typeof existingVariant[key] !== "undefined") {
|
|
39
49
|
if (key === "state") {
|
|
40
50
|
variant.state = existingVariant.state;
|
package/dist/bundled/index2.js
CHANGED
package/dist/bundled/sandbox.js
CHANGED
|
@@ -2,7 +2,7 @@ import { parseQuery } from "vue-router";
|
|
|
2
2
|
import { reactive, ref, createApp, computed, watch, onMounted, h } from "vue";
|
|
3
3
|
import { createPinia } from "pinia";
|
|
4
4
|
import { m as mapFile, r as registerGlobalComponents } from "./global-components.js";
|
|
5
|
-
import { t as toRawDeep, _ as _sfc_main } from "./
|
|
5
|
+
import { a as applyStateToVariant, t as toRawDeep, _ as _sfc_main } from "./state.js";
|
|
6
6
|
import { files } from "$histoire-stories";
|
|
7
7
|
import { S as STATE_SYNC, P as PREVIEW_SETTINGS_SYNC, a as SANDBOX_READY } from "./const.js";
|
|
8
8
|
import { i as isDark } from "./dark.js";
|
|
@@ -29,15 +29,7 @@ const app = createApp({
|
|
|
29
29
|
if (!mounted)
|
|
30
30
|
return;
|
|
31
31
|
synced = true;
|
|
32
|
-
|
|
33
|
-
for (const key in event.data.state) {
|
|
34
|
-
if (typeof variant.value.state[key] === "object") {
|
|
35
|
-
Object.assign(variant.value.state[key], event.data.state[key]);
|
|
36
|
-
} else {
|
|
37
|
-
variant.value.state[key] = event.data.state[key];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
32
|
+
applyStateToVariant(variant.value, event.data.state);
|
|
41
33
|
} else if (((_b = event.data) == null ? void 0 : _b.type) === PREVIEW_SETTINGS_SYNC) {
|
|
42
34
|
applyPreviewSettings(event.data.settings);
|
|
43
35
|
}
|
|
@@ -93,4 +93,30 @@ const toRawObject = (obj) => Object.keys(obj).reduce((raw, key) => {
|
|
|
93
93
|
raw[key] = toRawDeep(obj[key]);
|
|
94
94
|
return raw;
|
|
95
95
|
}, {});
|
|
96
|
-
|
|
96
|
+
function clone(data) {
|
|
97
|
+
try {
|
|
98
|
+
return structuredClone(data);
|
|
99
|
+
} catch (e) {
|
|
100
|
+
console.error(e);
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(JSON.stringify(data));
|
|
103
|
+
} catch (e2) {
|
|
104
|
+
console.error(e2);
|
|
105
|
+
}
|
|
106
|
+
return data;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function applyStateToVariant(variant, state) {
|
|
110
|
+
if (variant.state) {
|
|
111
|
+
for (const key in state) {
|
|
112
|
+
if (typeof variant.state[key] === "object") {
|
|
113
|
+
Object.assign(variant.state[key], state[key]);
|
|
114
|
+
} else {
|
|
115
|
+
variant.state[key] = state[key];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
variant.state = state;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export { _sfc_main as _, applyStateToVariant as a, clone as c, toRawDeep as t };
|
package/dist/bundled/story.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineStore } from "pinia";
|
|
2
2
|
import { ref, computed } from "vue";
|
|
3
3
|
import { createRouter, createWebHistory } from "vue-router";
|
|
4
|
-
const base =
|
|
4
|
+
const base = import.meta.env.BASE_URL;
|
|
5
5
|
const router = createRouter({
|
|
6
6
|
history: createWebHistory(base),
|
|
7
7
|
routes: [
|
|
@@ -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>
|
|
@@ -0,0 +1,193 @@
|
|
|
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, 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 defaultState = clone(toRawDeep(props.variant.state))
|
|
19
|
+
|
|
20
|
+
const selectedOption = useStorage<string>(
|
|
21
|
+
`_histoire-presets/${saveId.value}/selected`,
|
|
22
|
+
DEFAULT_ID,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
const presetStates = useStorage<Map<string, { state: Record<string, unknown>, label: string }>>(
|
|
26
|
+
`_histoire-presets/${saveId.value}/states`,
|
|
27
|
+
new Map(),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const presetsOptions = computed(() => {
|
|
31
|
+
const options = { [DEFAULT_ID]: 'Initial state' }
|
|
32
|
+
presetStates.value.forEach((value, key) => {
|
|
33
|
+
options[key] = value.label
|
|
34
|
+
})
|
|
35
|
+
return options
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
function resetState () {
|
|
39
|
+
selectedOption.value = DEFAULT_ID
|
|
40
|
+
applyStateToVariant(props.variant, defaultState)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function applyPreset (id) {
|
|
44
|
+
if (id === DEFAULT_ID) {
|
|
45
|
+
resetState()
|
|
46
|
+
} else if (presetStates.value.has(id)) {
|
|
47
|
+
applyStateToVariant(props.variant, presetStates.value.get(id).state)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
onMounted(() => {
|
|
52
|
+
if (selectedOption.value !== DEFAULT_ID) {
|
|
53
|
+
applyPreset(selectedOption.value)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const input = ref<HTMLInputElement>()
|
|
58
|
+
const select = ref<HTMLInputElement>()
|
|
59
|
+
const canEdit = computed(() => selectedOption.value !== DEFAULT_ID)
|
|
60
|
+
const isEditing = ref(false)
|
|
61
|
+
|
|
62
|
+
async function createPreset () {
|
|
63
|
+
const id = (new Date()).getTime().toString()
|
|
64
|
+
|
|
65
|
+
presetStates.value.set(id, { state: clone(toRawDeep(props.variant.state)), label: 'New preset' })
|
|
66
|
+
selectedOption.value = id
|
|
67
|
+
isEditing.value = true
|
|
68
|
+
await nextTick()
|
|
69
|
+
input.value.select()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const savedNotif = ref(false)
|
|
73
|
+
const savedTimeout = useTimeoutFn(() => {
|
|
74
|
+
savedNotif.value = false
|
|
75
|
+
}, 1000)
|
|
76
|
+
|
|
77
|
+
async function savePreset () {
|
|
78
|
+
if (!canEdit.value) return
|
|
79
|
+
|
|
80
|
+
const preset = presetStates.value.get(selectedOption.value)
|
|
81
|
+
preset.state = clone(toRawDeep(props.variant.state))
|
|
82
|
+
savedNotif.value = true
|
|
83
|
+
savedTimeout.start()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function deletePreset (id) {
|
|
87
|
+
if (!confirm('Are you sure you want to delete this preset?')) {
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (selectedOption.value === id) {
|
|
92
|
+
resetState()
|
|
93
|
+
}
|
|
94
|
+
presetStates.value.delete(id)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function startEditing () {
|
|
98
|
+
if (!canEdit.value) {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
isEditing.value = true
|
|
103
|
+
await nextTick()
|
|
104
|
+
input.value.select()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function stopEditing () {
|
|
108
|
+
isEditing.value = false
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
onClickOutside(select, stopEditing)
|
|
112
|
+
</script>
|
|
113
|
+
|
|
114
|
+
<template>
|
|
115
|
+
<div class="htw-flex htw-gap-2 htw-w-full htw-items-center">
|
|
116
|
+
<div
|
|
117
|
+
ref="select"
|
|
118
|
+
class="htw-flex-1 htw-min-w-0"
|
|
119
|
+
>
|
|
120
|
+
<BaseSelect
|
|
121
|
+
v-model="selectedOption"
|
|
122
|
+
:options="presetsOptions"
|
|
123
|
+
@dblclick="startEditing()"
|
|
124
|
+
@keydown.enter="stopEditing()"
|
|
125
|
+
@keydown.escape="stopEditing()"
|
|
126
|
+
@select="id => applyPreset(id)"
|
|
127
|
+
>
|
|
128
|
+
<template #default="{ label }">
|
|
129
|
+
<input
|
|
130
|
+
v-if="isEditing"
|
|
131
|
+
ref="input"
|
|
132
|
+
v-model="presetStates.get(selectedOption).label"
|
|
133
|
+
type="text"
|
|
134
|
+
class="htw-text-inherit htw-bg-transparent htw-w-full htw-h-full htw-outline-none"
|
|
135
|
+
@click.stop.prevent
|
|
136
|
+
>
|
|
137
|
+
|
|
138
|
+
<div
|
|
139
|
+
v-else
|
|
140
|
+
class="htw-flex htw-items-center htw-gap-2"
|
|
141
|
+
>
|
|
142
|
+
<span class="htw-flex-1 htw-truncate">
|
|
143
|
+
{{ label }}
|
|
144
|
+
</span>
|
|
145
|
+
<Icon
|
|
146
|
+
v-if="canEdit"
|
|
147
|
+
v-tooltip="'Rename this preset'"
|
|
148
|
+
icon="carbon:edit"
|
|
149
|
+
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"
|
|
150
|
+
@click.stop="startEditing()"
|
|
151
|
+
/>
|
|
152
|
+
</div>
|
|
153
|
+
</template>
|
|
154
|
+
|
|
155
|
+
<template #option="{ label, value }">
|
|
156
|
+
<div class="htw-flex htw-gap-2 htw-items-center">
|
|
157
|
+
<span
|
|
158
|
+
class="htw-flex-1 htw-truncate"
|
|
159
|
+
>{{ label }}</span>
|
|
160
|
+
<Icon
|
|
161
|
+
v-if="value !== DEFAULT_ID"
|
|
162
|
+
v-tooltip="'Delete this preset'"
|
|
163
|
+
icon="carbon:trash-can"
|
|
164
|
+
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"
|
|
165
|
+
@click.stop="deletePreset(value)"
|
|
166
|
+
/>
|
|
167
|
+
</div>
|
|
168
|
+
</template>
|
|
169
|
+
</BaseSelect>
|
|
170
|
+
</div>
|
|
171
|
+
<Icon
|
|
172
|
+
v-tooltip="savedNotif ? 'Saved!' : canEdit ? 'Save to preset' : null"
|
|
173
|
+
:icon="savedNotif ? 'carbon:checkmark' : 'carbon:save'"
|
|
174
|
+
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"
|
|
175
|
+
:class="[
|
|
176
|
+
canEdit ? 'htw-opacity-50 hover:htw-opacity-100' : 'htw-opacity-25 htw-pointer-events-none',
|
|
177
|
+
]"
|
|
178
|
+
@click="savePreset()"
|
|
179
|
+
/>
|
|
180
|
+
<Icon
|
|
181
|
+
v-tooltip="'Create new preset'"
|
|
182
|
+
icon="carbon:add-alt"
|
|
183
|
+
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"
|
|
184
|
+
@click="createPreset()"
|
|
185
|
+
/>
|
|
186
|
+
<Icon
|
|
187
|
+
v-tooltip="'Reset to initial state'"
|
|
188
|
+
icon="carbon:reset"
|
|
189
|
+
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"
|
|
190
|
+
@click="resetState()"
|
|
191
|
+
/>
|
|
192
|
+
</div>
|
|
193
|
+
</template>
|
|
@@ -4,6 +4,7 @@ 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'
|
|
7
8
|
|
|
8
9
|
defineProps({
|
|
9
10
|
variant: {
|
|
@@ -19,7 +20,20 @@ defineProps({
|
|
|
19
20
|
</script>
|
|
20
21
|
|
|
21
22
|
<template>
|
|
22
|
-
<div
|
|
23
|
+
<div
|
|
24
|
+
data-test-id="story-controls"
|
|
25
|
+
class="htw-flex htw-flex-col"
|
|
26
|
+
>
|
|
27
|
+
<!-- Toolbar -->
|
|
28
|
+
<div
|
|
29
|
+
class="htw-h-9 htw-flex-none htw-border-b htw-border-solid htw-border-gray-100 dark:htw-border-gray-750 htw-px-2 htw-flex htw-items-center"
|
|
30
|
+
>
|
|
31
|
+
<StatePresets
|
|
32
|
+
:story="story"
|
|
33
|
+
:variant="variant"
|
|
34
|
+
/>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
23
37
|
<!-- Custom controls -->
|
|
24
38
|
<SandboxVue3
|
|
25
39
|
v-if="variant.slots().controls || story.slots().controls"
|
|
@@ -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/dist/client/app/index.js
CHANGED
|
@@ -26,4 +26,9 @@ export function mountMainApp() {
|
|
|
26
26
|
app.use(router);
|
|
27
27
|
registerGlobalComponents(app);
|
|
28
28
|
app.mount('#app');
|
|
29
|
+
// @ts-expect-error vite hot api
|
|
30
|
+
if (import.meta.hot) {
|
|
31
|
+
// @ts-expect-error vite hot api
|
|
32
|
+
import.meta.hot.send('histoire:mount', {});
|
|
33
|
+
}
|
|
29
34
|
}
|
|
@@ -10,7 +10,7 @@ import { PREVIEW_SETTINGS_SYNC, STATE_SYNC, SANDBOX_READY } from './util/const.j
|
|
|
10
10
|
import { applyPreviewSettings } from './util/preview-settings.js';
|
|
11
11
|
import { isDark } from './util/dark.js';
|
|
12
12
|
import { histoireConfig } from './util/config.js';
|
|
13
|
-
import { toRawDeep } from './util/
|
|
13
|
+
import { applyStateToVariant, toRawDeep } from './util/state';
|
|
14
14
|
const query = parseQuery(window.location.search);
|
|
15
15
|
const file = ref(mapFile(files.find(f => f.id === query.storyId)));
|
|
16
16
|
const app = createApp({
|
|
@@ -25,16 +25,7 @@ const app = createApp({
|
|
|
25
25
|
if (!mounted)
|
|
26
26
|
return;
|
|
27
27
|
synced = true;
|
|
28
|
-
|
|
29
|
-
for (const key in event.data.state) {
|
|
30
|
-
if (typeof variant.value.state[key] === 'object') {
|
|
31
|
-
Object.assign(variant.value.state[key], event.data.state[key]);
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
variant.value.state[key] = event.data.state[key];
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
28
|
+
applyStateToVariant(variant.value, event.data.state);
|
|
38
29
|
}
|
|
39
30
|
else if (event.data?.type === PREVIEW_SETTINGS_SYNC) {
|
|
40
31
|
applyPreviewSettings(event.data.settings);
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { markRaw } from 'vue';
|
|
2
|
+
const copiedFromExistingVariant = [
|
|
3
|
+
'state',
|
|
4
|
+
'slots',
|
|
5
|
+
'initState',
|
|
6
|
+
'source',
|
|
7
|
+
'responsiveDisabled',
|
|
8
|
+
'setupApp',
|
|
9
|
+
'configReady',
|
|
10
|
+
'previewReady',
|
|
11
|
+
];
|
|
2
12
|
export function mapFile(file, existingFile) {
|
|
3
13
|
const result = {
|
|
4
14
|
...file,
|
|
@@ -16,7 +26,7 @@ export function mapFile(file, existingFile) {
|
|
|
16
26
|
const variant = result.story.variants[index];
|
|
17
27
|
const existingVariant = existingFile.story.variants[index];
|
|
18
28
|
if (existingVariant) {
|
|
19
|
-
for (const key
|
|
29
|
+
for (const key of copiedFromExistingVariant) {
|
|
20
30
|
if (typeof existingVariant[key] !== 'undefined') {
|
|
21
31
|
if (key === 'state') {
|
|
22
32
|
variant.state = existingVariant.state;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { unref, isRef } from 'vue';
|
|
2
|
+
const isObject = (val) => val !== null && typeof val === 'object';
|
|
3
|
+
export function toRawDeep(val) {
|
|
4
|
+
const unwrappedValue = isRef(val) ? unref(val) : val;
|
|
5
|
+
if (!isObject(unwrappedValue)) {
|
|
6
|
+
return unwrappedValue;
|
|
7
|
+
}
|
|
8
|
+
if (Array.isArray(unwrappedValue)) {
|
|
9
|
+
return unwrappedValue.map(toRawDeep);
|
|
10
|
+
}
|
|
11
|
+
return toRawObject(unwrappedValue);
|
|
12
|
+
}
|
|
13
|
+
const toRawObject = (obj) => Object.keys(obj).reduce((raw, key) => {
|
|
14
|
+
raw[key] = toRawDeep(obj[key]);
|
|
15
|
+
return raw;
|
|
16
|
+
}, {});
|
|
17
|
+
export function clone(data) {
|
|
18
|
+
try {
|
|
19
|
+
return structuredClone(data);
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
console.error(e);
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(JSON.stringify(data));
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
console.error(e);
|
|
28
|
+
}
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export function applyStateToVariant(variant, state) {
|
|
33
|
+
if (variant.state) {
|
|
34
|
+
for (const key in state) {
|
|
35
|
+
if (typeof variant.state[key] === 'object') {
|
|
36
|
+
Object.assign(variant.state[key], state[key]);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
variant.state[key] = state[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
variant.state = state;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Ref } from 'vue';
|
|
2
|
-
import { Variant } from '../types';
|
|
2
|
+
import type { Variant } from '../types';
|
|
3
3
|
export declare function useCurrentVariantRoute(variant: Ref<Variant>): {
|
|
4
4
|
isActive: import("vue").ComputedRef<boolean>;
|
|
5
5
|
targetRoute: import("vue").ComputedRef<{
|
package/dist/node/build.js
CHANGED
|
@@ -23,7 +23,7 @@ export async function build(ctx) {
|
|
|
23
23
|
const startTime = performance.now();
|
|
24
24
|
await findAllStories(ctx);
|
|
25
25
|
const server = await createViteServer({
|
|
26
|
-
plugins: await createVitePlugins(ctx),
|
|
26
|
+
plugins: await createVitePlugins(true, ctx),
|
|
27
27
|
});
|
|
28
28
|
await server.pluginContainer.buildStart({});
|
|
29
29
|
const moduleLoader = useModuleLoader({
|
|
@@ -53,7 +53,7 @@ export async function build(ctx) {
|
|
|
53
53
|
const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0);
|
|
54
54
|
const emptyStoryCount = ctx.storyFiles.length - storyCount;
|
|
55
55
|
const results = await viteBuild({
|
|
56
|
-
plugins: await createVitePlugins(ctx),
|
|
56
|
+
plugins: await createVitePlugins(false, ctx),
|
|
57
57
|
build: {
|
|
58
58
|
rollupOptions: {
|
|
59
59
|
input: [
|
|
@@ -76,7 +76,7 @@ export async function build(ctx) {
|
|
|
76
76
|
});
|
|
77
77
|
const result = Array.isArray(results) ? results[0] : results;
|
|
78
78
|
const resolvedViteConfig = await resolveViteConfig({
|
|
79
|
-
plugins: await createVitePlugins(ctx),
|
|
79
|
+
plugins: await createVitePlugins(false, ctx),
|
|
80
80
|
}, 'build');
|
|
81
81
|
const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
|
|
82
82
|
// Preload
|
|
@@ -8,7 +8,7 @@ export async function previewCommand(options) {
|
|
|
8
8
|
mode: 'build',
|
|
9
9
|
});
|
|
10
10
|
const viteConfig = await resolveViteConfig({
|
|
11
|
-
plugins: await createVitePlugins(ctx),
|
|
11
|
+
plugins: await createVitePlugins(false, ctx),
|
|
12
12
|
}, 'build');
|
|
13
13
|
const { baseUrl } = await startPreview(viteConfig, options.port, ctx);
|
|
14
14
|
console.log(`Preview server listening on ${pc.cyan(baseUrl)}`);
|