histoire 0.2.5 → 0.3.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.
Files changed (156) hide show
  1. package/dist/client/app/App.vue +8 -3
  2. package/dist/client/app/codegen/const.d.ts +1 -0
  3. package/dist/client/app/codegen/const.js +3 -0
  4. package/dist/client/app/codegen/vue3.js +82 -21
  5. package/dist/client/app/components/base/BaseEmpty.vue +17 -1
  6. package/dist/client/app/components/exposed/Story.vue +22 -1
  7. package/dist/client/app/components/exposed/Variant.vue +13 -1
  8. package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -4
  9. package/dist/client/app/components/sandbox/lib.js +2 -1
  10. package/dist/client/app/components/search/SearchLoading.vue +16 -0
  11. package/dist/client/app/components/search/SearchModal.vue +12 -149
  12. package/dist/client/app/components/search/SearchPane.vue +210 -0
  13. package/dist/client/app/components/search/search-data.d.ts +2 -0
  14. package/dist/client/app/components/search/search-data.js +7 -0
  15. package/dist/client/app/components/story/StoryControls.vue +5 -0
  16. package/dist/client/app/components/story/StoryDocs.vue +18 -11
  17. package/dist/client/app/components/story/StoryEvent.vue +53 -0
  18. package/dist/client/app/components/story/StoryEvents.vue +45 -0
  19. package/dist/client/app/components/story/StoryResponsivePreview.vue +41 -8
  20. package/dist/client/app/components/story/StorySidePanel.vue +28 -3
  21. package/dist/client/app/components/story/StorySourceCode.vue +24 -14
  22. package/dist/client/app/components/story/StoryView.vue +16 -2
  23. package/dist/client/app/components/tree/StoryGroup.vue +4 -4
  24. package/dist/client/app/components/tree/StoryListFolder.vue +4 -4
  25. package/dist/client/app/components/tree/StoryListItem.vue +4 -1
  26. package/dist/client/app/router.d.ts +1 -0
  27. package/dist/client/app/router.js +3 -1
  28. package/dist/client/app/sandbox.js +11 -1
  29. package/dist/client/app/stores/events.d.ts +29 -0
  30. package/dist/client/app/stores/events.js +25 -0
  31. package/dist/client/app/stores/folder.d.ts +10 -0
  32. package/dist/client/app/stores/folder.js +47 -0
  33. package/dist/client/app/stores/story.d.ts +14 -8
  34. package/dist/client/app/stores/story.js +20 -33
  35. package/dist/client/app/style/main.pcss +5 -0
  36. package/dist/client/app/style/sandbox.css +2 -0
  37. package/dist/client/app/types.d.ts +1 -0
  38. package/dist/client/app/util/const.d.ts +1 -0
  39. package/dist/client/app/util/const.js +1 -0
  40. package/dist/client/app/util/events.d.ts +1 -0
  41. package/dist/client/app/util/events.js +31 -0
  42. package/dist/client/app/util/mapping.js +2 -2
  43. package/dist/client/index.d.ts +2 -1
  44. package/dist/client/index.js +2 -1
  45. package/dist/client/server/Story.server.vue +13 -2
  46. package/dist/node/alias.d.ts +2 -1
  47. package/dist/node/alias.js +3 -2
  48. package/dist/node/build.js +74 -21
  49. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
  50. package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
  51. package/dist/node/collect/index.js +3 -1
  52. package/dist/node/commands/preview.js +4 -25
  53. package/dist/node/config.d.ts +103 -3
  54. package/dist/node/config.js +40 -23
  55. package/dist/node/context.d.ts +3 -3
  56. package/dist/node/context.js +1 -1
  57. package/dist/node/index.d.ts +1 -0
  58. package/dist/node/index.js +1 -0
  59. package/dist/node/load.d.ts +12 -0
  60. package/dist/node/load.js +42 -0
  61. package/dist/node/plugin.d.ts +67 -11
  62. package/dist/node/plugin.js +42 -245
  63. package/dist/node/preview.d.ts +8 -0
  64. package/dist/node/preview.js +41 -0
  65. package/dist/node/search.d.ts +2 -0
  66. package/dist/node/search.js +68 -0
  67. package/dist/node/server.js +43 -21
  68. package/dist/node/stories.d.ts +2 -0
  69. package/dist/node/stories.js +4 -4
  70. package/dist/node/types.d.ts +1 -0
  71. package/dist/node/util/find-up.d.ts +1 -0
  72. package/dist/node/util/find-up.js +20 -0
  73. package/dist/node/vite.d.ts +13 -0
  74. package/dist/node/vite.js +278 -0
  75. package/dist/style.css +111 -47
  76. package/package.json +5 -3
  77. package/scripts/copy.mjs +2 -1
  78. package/src/client/app/App.vue +8 -3
  79. package/src/client/app/codegen/const.ts +3 -0
  80. package/src/client/app/codegen/vue3.ts +80 -22
  81. package/src/client/app/components/base/BaseEmpty.vue +17 -1
  82. package/src/client/app/components/exposed/Story.vue +22 -1
  83. package/src/client/app/components/exposed/Variant.vue +13 -1
  84. package/src/client/app/components/sandbox/SandboxVue3.vue +6 -4
  85. package/src/client/app/components/sandbox/lib.ts +3 -1
  86. package/src/client/app/components/search/SearchLoading.vue +16 -0
  87. package/src/client/app/components/search/SearchModal.vue +12 -149
  88. package/src/client/app/components/search/SearchPane.vue +210 -0
  89. package/src/client/app/components/search/search-data.ts +9 -0
  90. package/src/client/app/components/story/StoryControls.vue +5 -0
  91. package/src/client/app/components/story/StoryDocs.vue +18 -11
  92. package/src/client/app/components/story/StoryEvent.vue +53 -0
  93. package/src/client/app/components/story/StoryEvents.vue +45 -0
  94. package/src/client/app/components/story/StoryResponsivePreview.vue +41 -8
  95. package/src/client/app/components/story/StorySidePanel.vue +28 -3
  96. package/src/client/app/components/story/StorySourceCode.vue +24 -14
  97. package/src/client/app/components/story/StoryView.vue +16 -2
  98. package/src/client/app/components/tree/StoryGroup.vue +4 -4
  99. package/src/client/app/components/tree/StoryListFolder.vue +4 -4
  100. package/src/client/app/components/tree/StoryListItem.vue +4 -1
  101. package/src/client/app/router.ts +4 -1
  102. package/src/client/app/sandbox.ts +12 -2
  103. package/src/client/app/stores/events.ts +36 -0
  104. package/{dist/client/app/stores/story.ts → src/client/app/stores/folder.ts} +16 -23
  105. package/src/client/app/stores/story.ts +21 -40
  106. package/src/client/app/style/main.pcss +5 -0
  107. package/src/client/app/style/sandbox.css +2 -0
  108. package/src/client/app/types.ts +1 -0
  109. package/src/client/app/util/const.ts +1 -0
  110. package/src/client/app/util/events.ts +30 -0
  111. package/src/client/app/util/mapping.ts +2 -2
  112. package/src/client/index.ts +2 -1
  113. package/src/client/server/Story.server.vue +13 -2
  114. package/src/node/alias.ts +4 -2
  115. package/src/node/build.ts +86 -21
  116. package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
  117. package/src/node/collect/index.ts +3 -1
  118. package/src/node/commands/preview.ts +4 -33
  119. package/src/node/config.ts +53 -27
  120. package/src/node/context.ts +3 -3
  121. package/src/node/index.ts +1 -0
  122. package/src/node/load.ts +63 -0
  123. package/src/node/plugin.ts +75 -252
  124. package/src/node/preview.ts +56 -0
  125. package/src/node/search.ts +75 -0
  126. package/src/node/server.ts +49 -23
  127. package/src/node/stories.ts +4 -4
  128. package/src/node/types.ts +1 -0
  129. package/src/node/util/find-up.ts +24 -0
  130. package/src/node/vite.ts +296 -0
  131. package/tailwind.config.cjs +1 -0
  132. package/dist/client/app/codegen/serialize-js.ts +0 -119
  133. package/dist/client/app/codegen/util.ts +0 -79
  134. package/dist/client/app/codegen/vue3.ts +0 -276
  135. package/dist/client/app/components/sandbox/lib.ts +0 -8
  136. package/dist/client/app/global-components.ts +0 -13
  137. package/dist/client/app/index.ts +0 -28
  138. package/dist/client/app/router.ts +0 -17
  139. package/dist/client/app/sandbox.ts +0 -94
  140. package/dist/client/app/stores/preview-settings.ts +0 -17
  141. package/dist/client/app/types.ts +0 -78
  142. package/dist/client/app/util/config.ts +0 -6
  143. package/dist/client/app/util/const.ts +0 -3
  144. package/dist/client/app/util/dark.ts +0 -4
  145. package/dist/client/app/util/env.ts +0 -1
  146. package/dist/client/app/util/keyboard.ts +0 -99
  147. package/dist/client/app/util/mapping.ts +0 -48
  148. package/dist/client/app/util/preview-settings.ts +0 -8
  149. package/dist/client/app/util/reactivity.ts +0 -22
  150. package/dist/client/app/util/responsive.ts +0 -3
  151. package/dist/client/app/util/scroll.ts +0 -31
  152. package/dist/client/app/util/tooltip.ts +0 -13
  153. package/dist/client/app/util/variant.ts +0 -20
  154. package/dist/client/index.ts +0 -12
  155. package/dist/client/server/index.ts +0 -32
  156. package/dist/client/shim.d.ts +0 -5
@@ -7,20 +7,24 @@ import { pascal } from 'case'
7
7
  import { createAutoBuildingObject, indent } from './util'
8
8
  import { serializeJs } from './serialize-js'
9
9
  import type { Variant } from '../types'
10
+ import { voidElements } from './const'
10
11
 
11
12
  export async function generateSourceCode (variant: Variant) {
12
- const vnode = variant.slots().default?.({ state: variant.state }) ?? []
13
+ const vnode = variant.slots().default?.({ state: variant.state ?? {} }) ?? []
13
14
  const list = Array.isArray(vnode) ? vnode : [vnode]
14
15
  const lines: string[] = []
15
16
  for (const vnode of list) {
16
- lines.push(...await printVNode(vnode))
17
+ lines.push(...(await printVNode(vnode)).lines)
17
18
  }
18
19
  return lines.join('\n')
19
20
  }
20
21
 
21
- async function printVNode (vnode: VNode): Promise<string[]> {
22
+ async function printVNode (vnode: VNode): Promise<{ lines: string[], isText?: boolean }> {
22
23
  if (vnode.type === Text) {
23
- return [vnode.children.trim()]
24
+ return {
25
+ lines: [vnode.children],
26
+ isText: true,
27
+ }
24
28
  }
25
29
 
26
30
  const lines: string[] = []
@@ -49,6 +53,10 @@ async function printVNode (vnode: VNode): Promise<string[]> {
49
53
  if (dir.arg) {
50
54
  arg = `:${dir.arg}`
51
55
  }
56
+ if (valueCode) {
57
+ // Cleanup render code
58
+ valueCode = valueCode.replace(/^\$(setup|props|data)\./g, '')
59
+ }
52
60
  const valueLines = valueCode ? [valueCode] : serializeAndCleanJs(dir.value)
53
61
  const attr: string[] = []
54
62
  const dirAttr = `v-${dirName}${arg}${modifiers}="`
@@ -78,11 +86,12 @@ async function printVNode (vnode: VNode): Promise<string[]> {
78
86
  }
79
87
  }
80
88
  genDirective('model', dir, valueCode)
81
- } else {
89
+ } else if (dir.instance._ || dir.instance.$) {
90
+ const target = dir.instance.$ ?? dir.instance._
82
91
  let dirName: string
83
- for (const directives of [dir.instance._.directives, dir.instance._.appContext.directives]) {
92
+ for (const directives of [target.directives, target.appContext.directives]) {
84
93
  for (const key in directives) {
85
- if (dir.instance._.directives[key] === dir.dir) {
94
+ if (target.directives[key] === dir.dir) {
86
95
  dirName = key
87
96
  break
88
97
  }
@@ -139,9 +148,20 @@ async function printVNode (vnode: VNode): Promise<string[]> {
139
148
  let serialized: string[]
140
149
  if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
141
150
  // It was formatted from auto building object (slot props)
142
- serialized = [value.substring(2, value.length - 2).trim()]
151
+ serialized = cleanupExpression(value.substring(2, value.length - 2).trim()).split('\n')
143
152
  } else if (typeof value === 'function') {
144
- serialized = [value.toString().replace(/'/g, '\\\'').replace(/"/g, '\'')]
153
+ let code = cleanupExpression(value.toString().replace(/'/g, '\\\'').replace(/"/g, '\''))
154
+ const testResult = /function ([^\s]+)\(/.exec(code)
155
+ if (testResult) {
156
+ // Function name only
157
+ serialized = [testResult[1]]
158
+ } else {
159
+ if (code.startsWith('($event) => ')) {
160
+ // Remove unnecessary `($event) => `
161
+ code = code.substring('($event) => '.length)
162
+ }
163
+ serialized = code.split('\n')
164
+ }
145
165
  } else {
146
166
  serialized = serializeAndCleanJs(value)
147
167
  }
@@ -166,12 +186,38 @@ async function printVNode (vnode: VNode): Promise<string[]> {
166
186
  const tagName = getTagName(vnode)
167
187
 
168
188
  // Children
189
+ let isChildText = false
169
190
  const childLines: string[] = []
170
191
  if (typeof vnode.children === 'string') {
171
- childLines.push(vnode.children)
192
+ if (tagName === 'pre') {
193
+ childLines.push(vnode.children)
194
+ } else {
195
+ childLines.push(...vnode.children.split('\n'))
196
+ }
197
+ isChildText = true
172
198
  } else if (Array.isArray(vnode.children)) {
199
+ let isAllChildText
173
200
  for (const child of vnode.children) {
174
- childLines.push(...await printVNode(child))
201
+ const result = await printVNode(child)
202
+ if (result.isText) {
203
+ if (isAllChildText === undefined) {
204
+ isAllChildText = true
205
+ }
206
+ const text = result.lines[0]
207
+ if (!childLines.length || /^\s/.test(text)) {
208
+ childLines.push(text.trim())
209
+ } else {
210
+ childLines[childLines.length - 1] += text
211
+ }
212
+ } else {
213
+ if (isAllChildText === undefined) {
214
+ isAllChildText = false
215
+ }
216
+ childLines.push(...result.lines)
217
+ }
218
+ }
219
+ if (isAllChildText !== undefined) {
220
+ isChildText = isAllChildText
175
221
  }
176
222
  }
177
223
 
@@ -188,7 +234,7 @@ async function printVNode (vnode: VNode): Promise<string[]> {
188
234
  const children = vnode.children[key](autoObject.proxy)
189
235
  const slotLines: string[] = []
190
236
  for (const child of children) {
191
- slotLines.push(...await printVNode(child))
237
+ slotLines.push(...(await printVNode(child)).lines)
192
238
  }
193
239
  const slotProps = Object.keys(autoObject.cache)
194
240
  if (slotProps.length) {
@@ -224,23 +270,31 @@ async function printVNode (vnode: VNode): Promise<string[]> {
224
270
  }
225
271
  }
226
272
 
273
+ const isVoid = voidElements.includes(tagName.toLowerCase())
274
+
227
275
  if (childLines.length > 0) {
228
- lines.push(...tag)
229
- lines.push(...indent(childLines))
230
- lines.push(`</${tagName}>`)
276
+ if (childLines.length === 1 && tag.length === 1 && !attrs.length && isChildText) {
277
+ lines.push(`${tag[0]}${childLines[0]}</${tagName}>`)
278
+ } else {
279
+ lines.push(...tag)
280
+ lines.push(...indent(childLines))
281
+ lines.push(`</${tagName}>`)
282
+ }
231
283
  } else if (tag.length > 1) {
232
284
  lines.push(...tag)
233
- lines.push('/>')
285
+ lines.push(isVoid ? '>' : '/>')
234
286
  } else {
235
- lines.push(`${tag[0]} />`)
287
+ lines.push(`${tag[0]}${isVoid ? '' : ' /'}>`)
236
288
  }
237
289
  } else if (vnode?.shapeFlag & 1 << 4) {
238
290
  for (const child of vnode.children) {
239
- lines.push(...await printVNode(child))
291
+ lines.push(...(await printVNode(child)).lines)
240
292
  }
241
293
  }
242
294
 
243
- return lines
295
+ return {
296
+ lines,
297
+ }
244
298
  }
245
299
 
246
300
  function getTagName (vnode: VNode) {
@@ -258,7 +312,7 @@ function getTagName (vnode: VNode) {
258
312
  }
259
313
 
260
314
  function getNameFromFile (file: string) {
261
- const parts = /\/([^/]+)\.vue$/.exec(file)
315
+ const parts = /([^/]+)\.vue$/.exec(file)
262
316
  if (parts) {
263
317
  return pascal(parts[1])
264
318
  }
@@ -269,8 +323,12 @@ function serializeAndCleanJs (value: any) {
269
323
  const isAutoBuildingObject = !!value?.__autoBuildingObject
270
324
  const result = serializeJs(value)
271
325
  if (isAutoBuildingObject) {
272
- return [result.__autoBuildingObjectGetKey]
326
+ return [cleanupExpression(result.__autoBuildingObjectGetKey)]
273
327
  } else {
274
- return result.replace(/\$setup\./g, '').split('\n')
328
+ return cleanupExpression(result).split('\n')
275
329
  }
276
330
  }
331
+
332
+ function cleanupExpression (expr: string) {
333
+ return expr.replace(/\$setup\./g, '')
334
+ }
@@ -1,5 +1,21 @@
1
1
  <template>
2
- <div class="htw-flex htw-flex-col htw-items-center htw-justify-center htw-space-y-4 htw-py-12 htw-h-full htw-text-center htw-text-gray-400 htw-text-lg">
2
+ <div class="htw-base-empty htw-flex htw-flex-col htw-items-center htw-justify-center htw-space-y-4 htw-py-12 htw-h-full htw-text-center htw-text-gray-400 htw-text-lg">
3
3
  <slot />
4
4
  </div>
5
5
  </template>
6
+
7
+ <style scoped>
8
+ .htw-base-empty {
9
+ animation: htw-base-empty .3s 1s both;
10
+ }
11
+
12
+ @keyframes htw-base-empty {
13
+ 0% {
14
+ opacity: 0;
15
+ }
16
+
17
+ 100% {
18
+ opacity: 1;
19
+ }
20
+ }
21
+ </style>
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { computed, defineComponent, provide, useAttrs, VNode, h, PropType } from 'vue'
2
+ import { computed, defineComponent, provide, useAttrs, VNode, h, PropType, getCurrentInstance } from 'vue'
3
3
  import { Story } from '../../types.js'
4
4
  import Variant from './Variant.vue'
5
5
 
@@ -25,6 +25,27 @@ export default defineComponent({
25
25
  const story = computed(() => attrs.story)
26
26
  provide('story', story)
27
27
 
28
+ const storyComponent: any = getCurrentInstance().parent
29
+ // Allows tracking reactivity with watchers to sync state
30
+ const implicitState = {
31
+ $data: storyComponent.data,
32
+ }
33
+ function addImplicitState (key, value) {
34
+ if (typeof value === 'function' || (value && value.__file)) {
35
+ return
36
+ }
37
+ implicitState[key] = value
38
+ }
39
+ // From `<script setup>`'s `defineExpose`
40
+ for (const key in storyComponent.exposed) {
41
+ addImplicitState(key, storyComponent.exposed[key])
42
+ }
43
+ // We needs __VUE_PROD_DEVTOOLS__ flag set to `true` to enable `devtoolsRawSetupState`
44
+ for (const key in storyComponent.devtoolsRawSetupState) {
45
+ addImplicitState(key, storyComponent.devtoolsRawSetupState[key])
46
+ }
47
+ provide('implicitState', () => implicitState)
48
+
28
49
  return {
29
50
  story,
30
51
  }
@@ -1,7 +1,9 @@
1
1
  <script lang="ts">
2
- import { defineComponent, getCurrentInstance, PropType, useAttrs } from 'vue'
2
+ import { defineComponent, getCurrentInstance, inject, PropType, useAttrs, watch } from 'vue'
3
3
  import { Variant } from '../../types'
4
4
 
5
+ // const logLocation = location.href.includes('__sandbox') ? '[Sandbox]' : '[Host]'
6
+
5
7
  export default defineComponent({
6
8
  // eslint-disable-next-line vue/multi-word-component-names
7
9
  name: 'Variant',
@@ -31,6 +33,16 @@ export default defineComponent({
31
33
 
32
34
  const vm = getCurrentInstance()
33
35
 
36
+ const implicitState = inject<() => any>('implicitState')
37
+
38
+ watch(() => implicitState, value => {
39
+ if (typeof props.initState !== 'function') {
40
+ attrs.variant.state = value()
41
+ }
42
+ }, {
43
+ immediate: true,
44
+ })
45
+
34
46
  function updateVariant () {
35
47
  Object.assign(attrs.variant, {
36
48
  initState: async () => {
@@ -2,7 +2,7 @@
2
2
  import { App, createApp, onMounted, onUnmounted, PropType, ref, watch } from 'vue'
3
3
  import { Story, Variant } from '../../types'
4
4
  // @ts-expect-error virtual module id
5
- import setup from '$histoire-setup'
5
+ import * as setup from '$histoire-setup'
6
6
 
7
7
  const props = defineProps({
8
8
  variant: {
@@ -35,15 +35,17 @@ async function mountVariant () {
35
35
  await props.variant.initState()
36
36
 
37
37
  app = createApp({
38
+ name: 'SandboxVue3',
39
+
38
40
  render: () => {
39
41
  return props.variant.slots()?.[props.slotName]?.({
40
- state: props.variant.state,
42
+ state: props.variant.state ?? {},
41
43
  })
42
44
  },
43
45
  })
44
46
 
45
- if (typeof setup === 'function') {
46
- await setup({
47
+ if (typeof setup?.setupVue3 === 'function') {
48
+ await setup.setupVue3({
47
49
  app,
48
50
  story: props.story,
49
51
  variant: props.variant,
@@ -1,8 +1,10 @@
1
+
2
+ import { base } from '../../router'
1
3
  import { Story, Variant } from '../../types'
2
4
 
3
5
  export function getSandboxUrl (story: Story, variant: Variant) {
4
6
  const url = new URLSearchParams()
5
7
  url.append('storyId', story.id)
6
8
  url.append('variantId', variant.id)
7
- return '/__sandbox?' + url.toString()
9
+ return `${base}__sandbox?${url.toString()}`
8
10
  }
@@ -0,0 +1,16 @@
1
+ <script lang="ts" setup>
2
+ import { Icon } from '@iconify/vue'
3
+ </script>
4
+
5
+ <template>
6
+ <div
7
+ class="htw-flex htw-items-center htw-gap-4 htw-pl-6 htw-border htw-border-transparent focus-visible:htw-border-primary-500 htw-h-[51px] htw-opacity-30"
8
+ >
9
+ <Icon
10
+ icon="carbon:search"
11
+ class="flex-none htw-w-4 htw-h-4"
12
+ />
13
+
14
+ Loading...
15
+ </div>
16
+ </template>
@@ -1,13 +1,13 @@
1
1
  <script lang="ts" setup>
2
- import { useFocus, useDebounce } from '@vueuse/core'
3
- import { computed, ref, watch } from 'vue'
4
- import { Icon } from '@iconify/vue'
5
- import { useStoryStore } from '../../stores/story'
6
- import BaseEmpty from '../base/BaseEmpty.vue'
7
- import type { SearchResult, Story, Variant } from '../../types'
8
- import SearchItem from './SearchItem.vue'
2
+ import { defineAsyncComponent } from 'vue'
3
+ import SearchLoading from './SearchLoading.vue'
4
+ const SearchPane = defineAsyncComponent({
5
+ loader: () => import('./SearchPane.vue'),
6
+ loadingComponent: SearchLoading,
7
+ delay: 0,
8
+ })
9
9
 
10
- const props = defineProps({
10
+ defineProps({
11
11
  shown: {
12
12
  type: Boolean,
13
13
  default: false,
@@ -21,111 +21,6 @@ const emit = defineEmits({
21
21
  function close () {
22
22
  emit('close')
23
23
  }
24
-
25
- // Autofocus
26
-
27
- const input = ref<HTMLInputElement>()
28
- const { focused } = useFocus(input, {
29
- initialValue: true,
30
- })
31
-
32
- watch(() => props.shown, value => {
33
- if (value) {
34
- requestAnimationFrame(() => {
35
- focused.value = true
36
- input.value.select()
37
- })
38
- }
39
- })
40
-
41
- // Search
42
-
43
- const searchInputText = ref('')
44
- const rateLimitedSearch = useDebounce(searchInputText, 300)
45
-
46
- const storyStore = useStoryStore()
47
-
48
- function storyResultFactory (story: Story, rank: number): SearchResult {
49
- return {
50
- kind: 'story',
51
- rank,
52
- id: story.id,
53
- title: story.title,
54
- route: {
55
- name: 'story',
56
- params: {
57
- storyId: story.id,
58
- },
59
- },
60
- path: story.file.path.slice(0, -1),
61
- icon: story.icon,
62
- iconColor: story.iconColor,
63
- }
64
- }
65
-
66
- function variantResultFactory (story: Story, variant: Variant, rank: number): SearchResult {
67
- return {
68
- kind: 'variant',
69
- rank,
70
- id: variant.id,
71
- title: variant.title,
72
- route: {
73
- name: 'story',
74
- params: {
75
- storyId: story.id,
76
- },
77
- query: {
78
- variantId: variant.id,
79
- },
80
- },
81
- path: [...story.file.path ?? [], story.title],
82
- icon: variant.icon,
83
- iconColor: variant.iconColor,
84
- }
85
- }
86
-
87
- const results = computed(() => {
88
- const list: SearchResult[] = []
89
- if (rateLimitedSearch.value) {
90
- const s = rateLimitedSearch.value.toLowerCase()
91
- for (const story of storyStore.stories) {
92
- const storyMatched = story.title.toLowerCase().includes(s)
93
- let storyPathMatched = false
94
- if (storyMatched || (storyPathMatched = story.file.path.some(p => p.toLowerCase().includes(s)))) {
95
- list.push(storyResultFactory(story, storyMatched ? 1 : 4))
96
- }
97
- for (const variant of story.variants) {
98
- const variantMatched = variant.title.toLowerCase().includes(s)
99
- if (storyMatched || storyPathMatched || variantMatched) {
100
- list.push(variantResultFactory(story, variant, variantMatched ? 2 : storyMatched ? 3 : 5))
101
- }
102
- }
103
- }
104
- }
105
- return list.sort((a, b) => a.rank - b.rank)
106
- })
107
-
108
- // Selection
109
-
110
- const selectedIndex = ref(0)
111
-
112
- watch(results, () => {
113
- selectedIndex.value = 0
114
- })
115
-
116
- function selectNext () {
117
- selectedIndex.value++
118
- if (selectedIndex.value > results.value.length - 1) {
119
- selectedIndex.value = 0
120
- }
121
- }
122
-
123
- function selectPrevious () {
124
- selectedIndex.value--
125
- if (selectedIndex.value < 0) {
126
- selectedIndex.value = results.value.length - 1
127
- }
128
- }
129
24
  </script>
130
25
 
131
26
  <template>
@@ -139,42 +34,10 @@ function selectPrevious () {
139
34
  @click="close()"
140
35
  />
141
36
  <div class="htw-bg-white dark:htw-bg-gray-700 md:htw-mt-16 md:htw-mx-auto htw-w-screen htw-max-w-[512px] htw-shadow-xl htw-border htw-border-gray-200 dark:htw-border-gray-850 htw-rounded-lg htw-relative htw-divide-y htw-divide-gray-200 dark:htw-divide-gray-850">
142
- <div
143
- class="htw-flex htw-items-center htw-gap-4 htw-pl-6 htw-border htw-border-transparent focus-visible:htw-border-primary-500"
144
- @click="focused = true"
145
- >
146
- <Icon
147
- icon="carbon:search"
148
- class="flex-none htw-w-4 htw-h-4"
149
- />
150
-
151
- <input
152
- ref="input"
153
- v-model="searchInputText"
154
- placeholder="Search for stories, variants..."
155
- class="htw-bg-transparent htw-w-full htw-flex-1 htw-pl-0 htw-pr-6 htw-py-4 htw-outline-none"
156
- @keydown.down.prevent="selectNext()"
157
- @keydown.up.prevent="selectPrevious()"
158
- @keydown.escape="close()"
159
- >
160
- </div>
161
-
162
- <BaseEmpty v-if="rateLimitedSearch && !results.length">
163
- No results
164
- </BaseEmpty>
165
-
166
- <div
167
- v-else-if="results.length"
168
- class="htw-max-h-[400px] htw-overflow-y-auto htw-rounded-b-lg"
169
- >
170
- <SearchItem
171
- v-for="(result, index) of results"
172
- :key="`${result.kind}-${result.id}`"
173
- :result="result"
174
- :selected="index === selectedIndex"
175
- @close="close()"
176
- />
177
- </div>
37
+ <SearchPane
38
+ :shown="shown"
39
+ @close="close()"
40
+ />
178
41
  </div>
179
42
  </div>
180
43
  </template>