histoire 0.4.5 → 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/style.css CHANGED
@@ -891,6 +891,11 @@ button {
891
891
  margin:0.25rem
892
892
  }
893
893
 
894
+ .-htw-my-1{
895
+ margin-top:-0.25rem;
896
+ margin-bottom:-0.25rem
897
+ }
898
+
894
899
  .htw-mx-1{
895
900
  margin-left:0.25rem;
896
901
  margin-right:0.25rem
@@ -921,11 +926,6 @@ button {
921
926
  margin-right:1rem
922
927
  }
923
928
 
924
- .-htw-my-1{
925
- margin-top:-0.25rem;
926
- margin-bottom:-0.25rem
927
- }
928
-
929
929
  .htw-mt-2{
930
930
  margin-top:0.5rem
931
931
  }
@@ -1026,10 +1026,18 @@ button {
1026
1026
  height:2.5rem
1027
1027
  }
1028
1028
 
1029
+ .htw-h-\[27px\]{
1030
+ height:27px
1031
+ }
1032
+
1029
1033
  .htw-h-\[2px\]{
1030
1034
  height:2px
1031
1035
  }
1032
1036
 
1037
+ .htw-h-9{
1038
+ height:2.25rem
1039
+ }
1040
+
1033
1041
  .htw-h-\[51px\]{
1034
1042
  height:51px
1035
1043
  }
@@ -1156,14 +1164,14 @@ button {
1156
1164
  min-width:1rem
1157
1165
  }
1158
1166
 
1159
- .htw-min-w-16{
1160
- min-width:4rem
1161
- }
1162
-
1163
1167
  .htw-min-w-0{
1164
1168
  min-width:0px
1165
1169
  }
1166
1170
 
1171
+ .htw-min-w-16{
1172
+ min-width:4rem
1173
+ }
1174
+
1167
1175
  .htw-max-w-full{
1168
1176
  max-width:100%
1169
1177
  }
@@ -1463,6 +1471,11 @@ button {
1463
1471
  border-color:rgb(0 0 0 / 0.2)
1464
1472
  }
1465
1473
 
1474
+ .htw-border-gray-850{
1475
+ --tw-border-opacity:1;
1476
+ border-color:rgb(var(--_histoire-color-gray-850) / var(--tw-border-opacity))
1477
+ }
1478
+
1466
1479
  .htw-bg-white{
1467
1480
  --tw-bg-opacity:1;
1468
1481
  background-color:rgb(255 255 255 / var(--tw-bg-opacity))
@@ -1478,10 +1491,24 @@ button {
1478
1491
  background-color:rgb(var(--_histoire-color-primary-500) / var(--tw-bg-opacity))
1479
1492
  }
1480
1493
 
1494
+ .htw-bg-gray-50{
1495
+ --tw-bg-opacity:1;
1496
+ background-color:rgb(var(--_histoire-color-gray-50) / var(--tw-bg-opacity))
1497
+ }
1498
+
1499
+ .htw-bg-primary-200{
1500
+ --tw-bg-opacity:1;
1501
+ background-color:rgb(var(--_histoire-color-primary-200) / var(--tw-bg-opacity))
1502
+ }
1503
+
1481
1504
  .htw-bg-primary-500\/25{
1482
1505
  background-color:rgb(var(--_histoire-color-primary-500) / 0.25)
1483
1506
  }
1484
1507
 
1508
+ .htw-bg-transparent{
1509
+ background-color:transparent
1510
+ }
1511
+
1485
1512
  .htw-bg-primary-50{
1486
1513
  --tw-bg-opacity:1;
1487
1514
  background-color:rgb(var(--_histoire-color-primary-50) / var(--tw-bg-opacity))
@@ -1492,10 +1519,6 @@ button {
1492
1519
  background-color:rgb(var(--_histoire-color-gray-100) / var(--tw-bg-opacity))
1493
1520
  }
1494
1521
 
1495
- .htw-bg-transparent{
1496
- background-color:transparent
1497
- }
1498
-
1499
1522
  .htw-bg-white\/80{
1500
1523
  background-color:rgb(255 255 255 / 0.8)
1501
1524
  }
@@ -1505,16 +1528,6 @@ button {
1505
1528
  background-color:rgb(var(--_histoire-color-gray-200) / var(--tw-bg-opacity))
1506
1529
  }
1507
1530
 
1508
- .htw-bg-primary-200{
1509
- --tw-bg-opacity:1;
1510
- background-color:rgb(var(--_histoire-color-primary-200) / var(--tw-bg-opacity))
1511
- }
1512
-
1513
- .htw-bg-gray-50{
1514
- --tw-bg-opacity:1;
1515
- background-color:rgb(var(--_histoire-color-gray-50) / var(--tw-bg-opacity))
1516
- }
1517
-
1518
1531
  .htw-bg-gray-500\/10{
1519
1532
  background-color:rgb(var(--_histoire-color-gray-500) / 0.1)
1520
1533
  }
@@ -1523,6 +1536,11 @@ button {
1523
1536
  background-color:rgb(var(--_histoire-color-gray-500) / 0.5)
1524
1537
  }
1525
1538
 
1539
+ .htw-bg-gray-700{
1540
+ --tw-bg-opacity:1;
1541
+ background-color:rgb(var(--_histoire-color-gray-700) / var(--tw-bg-opacity))
1542
+ }
1543
+
1526
1544
  .htw-stroke-white{
1527
1545
  stroke:#fff
1528
1546
  }
@@ -1568,6 +1586,16 @@ button {
1568
1586
  padding-bottom:3rem
1569
1587
  }
1570
1588
 
1589
+ .htw-px-2{
1590
+ padding-left:0.5rem;
1591
+ padding-right:0.5rem
1592
+ }
1593
+
1594
+ .htw-py-1{
1595
+ padding-top:0.25rem;
1596
+ padding-bottom:0.25rem
1597
+ }
1598
+
1571
1599
  .htw-px-0\.5{
1572
1600
  padding-left:0.125rem;
1573
1601
  padding-right:0.125rem
@@ -1588,11 +1616,6 @@ button {
1588
1616
  padding-bottom:1rem
1589
1617
  }
1590
1618
 
1591
- .htw-px-2{
1592
- padding-left:0.5rem;
1593
- padding-right:0.5rem
1594
- }
1595
-
1596
1619
  .htw-py-0\.5{
1597
1620
  padding-top:0.125rem;
1598
1621
  padding-bottom:0.125rem
@@ -1603,11 +1626,6 @@ button {
1603
1626
  padding-bottom:0px
1604
1627
  }
1605
1628
 
1606
- .htw-py-1{
1607
- padding-top:0.25rem;
1608
- padding-bottom:0.25rem
1609
- }
1610
-
1611
1629
  .htw-pr-2{
1612
1630
  padding-right:0.5rem
1613
1631
  }
@@ -1701,6 +1719,10 @@ button {
1701
1719
  color:rgb(var(--_histoire-color-gray-400) / var(--tw-text-opacity))
1702
1720
  }
1703
1721
 
1722
+ .htw-text-inherit{
1723
+ color:inherit
1724
+ }
1725
+
1704
1726
  .htw-text-red-500{
1705
1727
  --tw-text-opacity:1;
1706
1728
  color:rgb(239 68 68 / var(--tw-text-opacity))
@@ -1719,10 +1741,6 @@ button {
1719
1741
  color:rgb(var(--_histoire-color-primary-800) / var(--tw-text-opacity))
1720
1742
  }
1721
1743
 
1722
- .htw-text-inherit{
1723
- color:inherit
1724
- }
1725
-
1726
1744
  .htw-opacity-25{
1727
1745
  opacity:0.25
1728
1746
  }
@@ -1862,6 +1880,11 @@ body {
1862
1880
  margin-bottom:0px
1863
1881
  }
1864
1882
 
1883
+ .hover\:htw-border-primary-500:hover{
1884
+ --tw-border-opacity:1;
1885
+ border-color:rgb(var(--_histoire-color-primary-500) / var(--tw-border-opacity))
1886
+ }
1887
+
1865
1888
  .hover\:htw-bg-primary-100:hover{
1866
1889
  --tw-bg-opacity:1;
1867
1890
  background-color:rgb(var(--_histoire-color-primary-100) / var(--tw-bg-opacity))
@@ -2010,6 +2033,11 @@ body {
2010
2033
  background-color:rgb(var(--_histoire-color-primary-400) / var(--tw-bg-opacity))
2011
2034
  }
2012
2035
 
2036
+ .htw-dark .dark\:htw-bg-primary-800{
2037
+ --tw-bg-opacity:1;
2038
+ background-color:rgb(var(--_histoire-color-primary-800) / var(--tw-bg-opacity))
2039
+ }
2040
+
2013
2041
  .htw-dark .dark\:htw-bg-primary-600{
2014
2042
  --tw-bg-opacity:1;
2015
2043
  background-color:rgb(var(--_histoire-color-primary-600) / var(--tw-bg-opacity))
@@ -2073,6 +2101,11 @@ body {
2073
2101
  color:rgb(var(--_histoire-color-primary-200) / var(--tw-text-opacity))
2074
2102
  }
2075
2103
 
2104
+ .htw-dark .dark\:hover\:htw-border-primary-500:hover{
2105
+ --tw-border-opacity:1;
2106
+ border-color:rgb(var(--_histoire-color-primary-500) / var(--tw-border-opacity))
2107
+ }
2108
+
2076
2109
  .htw-dark .dark\:hover\:htw-bg-primary-700:hover{
2077
2110
  --tw-bg-opacity:1;
2078
2111
  background-color:rgb(var(--_histoire-color-primary-700) / var(--tw-bg-opacity))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -33,7 +33,7 @@
33
33
  "histoire": "./bin.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@histoire/controls": "^0.4.5",
36
+ "@histoire/controls": "^0.4.6",
37
37
  "@iconify/vue": "^3.2.1",
38
38
  "@types/markdown-it": "^12.2.3",
39
39
  "@vueuse/core": "^8.2.5",
@@ -74,7 +74,7 @@
74
74
  "@types/flexsearch": "^0.7.3",
75
75
  "@types/fs-extra": "^9.0.13",
76
76
  "@types/markdown-it": "^12.2.3",
77
- "@types/node": "^17.0.23",
77
+ "@types/node": "^17.0.32",
78
78
  "@vitejs/plugin-vue": "^2.3.1",
79
79
  "autoprefixer": "^10.4.4",
80
80
  "concurrently": "^7.1.0",
@@ -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 data-test-id="story-controls">
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
- if (props.variant.state) {
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) {
@@ -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/reactivity'
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
- if (variant.value.state) {
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
  }
@@ -0,0 +1,51 @@
1
+ import { unref, isRef } from 'vue'
2
+ import type { Variant } from '../types'
3
+
4
+ const isObject = (val) => val !== null && typeof val === 'object'
5
+
6
+ export function toRawDeep (val) {
7
+ const unwrappedValue = isRef(val) ? unref(val) : val
8
+
9
+ if (!isObject(unwrappedValue)) {
10
+ return unwrappedValue
11
+ }
12
+
13
+ if (Array.isArray(unwrappedValue)) {
14
+ return unwrappedValue.map(toRawDeep)
15
+ }
16
+
17
+ return toRawObject(unwrappedValue)
18
+ }
19
+
20
+ const toRawObject = (obj: Record<any, any>) => Object.keys(obj).reduce((raw, key) => {
21
+ raw[key] = toRawDeep(obj[key])
22
+ return raw
23
+ }, {})
24
+
25
+ export function clone (data) {
26
+ try {
27
+ return structuredClone(data)
28
+ } catch (e) {
29
+ console.error(e)
30
+ try {
31
+ return JSON.parse(JSON.stringify(data))
32
+ } catch (e) {
33
+ console.error(e)
34
+ }
35
+ return data
36
+ }
37
+ }
38
+
39
+ export function applyStateToVariant (variant: Variant, state: any) {
40
+ if (variant.state) {
41
+ for (const key in state) {
42
+ if (typeof variant.state[key] === 'object') {
43
+ Object.assign(variant.state[key], state[key])
44
+ } else {
45
+ variant.state[key] = state[key]
46
+ }
47
+ }
48
+ } else {
49
+ variant.state = state
50
+ }
51
+ }
@@ -1,6 +1,6 @@
1
1
  import { computed, Ref } from 'vue'
2
2
  import { useRoute } from 'vue-router'
3
- import { Variant } from '../types'
3
+ import type { Variant } from '../types'
4
4
 
5
5
  export function useCurrentVariantRoute (variant: Ref<Variant>) {
6
6
  const route = useRoute()
@@ -1 +0,0 @@
1
- export declare function toRawDeep(val: any): any;
@@ -1,16 +0,0 @@
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
- }, {});