histoire 0.2.4 → 0.2.5

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 (44) hide show
  1. package/dist/client/app/App.vue +1 -1
  2. package/dist/client/app/codegen/vue3.js +1 -1
  3. package/dist/client/app/codegen/vue3.ts +1 -1
  4. package/dist/client/app/components/app/Breadcrumb.vue +1 -1
  5. package/dist/client/app/components/search/SearchItem.vue +1 -0
  6. package/dist/client/app/components/tree/StoryGroup.vue +75 -0
  7. package/dist/client/app/components/{story → tree}/StoryList.vue +8 -2
  8. package/{src/client/app/components/story → dist/client/app/components/tree}/StoryListFolder.vue +6 -4
  9. package/dist/client/app/components/{story → tree}/StoryListItem.vue +3 -3
  10. package/dist/client/app/stores/story.d.ts +12 -6
  11. package/dist/client/app/stores/story.js +13 -8
  12. package/dist/client/app/stores/story.ts +13 -9
  13. package/dist/client/app/style/floating-vue.pcss +3 -1
  14. package/dist/client/app/types.d.ts +8 -1
  15. package/dist/client/app/types.ts +9 -1
  16. package/dist/client/server/Story.server.vue +8 -2
  17. package/dist/node/collect/index.js +2 -2
  18. package/dist/node/config.d.ts +8 -2
  19. package/dist/node/plugin.js +1 -1
  20. package/dist/node/stories.js +6 -1
  21. package/dist/node/tree.d.ts +16 -12
  22. package/dist/node/tree.js +45 -5
  23. package/dist/node/types.d.ts +10 -0
  24. package/dist/style.css +103 -29
  25. package/package.json +2 -2
  26. package/src/client/app/App.vue +1 -1
  27. package/src/client/app/codegen/vue3.ts +1 -1
  28. package/src/client/app/components/app/Breadcrumb.vue +1 -1
  29. package/src/client/app/components/search/SearchItem.vue +1 -0
  30. package/src/client/app/components/tree/StoryGroup.vue +75 -0
  31. package/src/client/app/components/{story → tree}/StoryList.vue +8 -2
  32. package/{dist/client/app/components/story → src/client/app/components/tree}/StoryListFolder.vue +6 -4
  33. package/src/client/app/components/{story → tree}/StoryListItem.vue +3 -3
  34. package/src/client/app/stores/story.ts +13 -9
  35. package/src/client/app/style/floating-vue.pcss +3 -1
  36. package/src/client/app/types.ts +9 -1
  37. package/src/client/server/Story.server.vue +8 -2
  38. package/src/node/collect/index.ts +3 -3
  39. package/src/node/config.ts +9 -2
  40. package/src/node/plugin.ts +1 -1
  41. package/src/node/stories.ts +6 -1
  42. package/src/node/tree.spec.ts +35 -17
  43. package/src/node/tree.ts +64 -10
  44. package/src/node/types.ts +11 -0
@@ -1,7 +1,7 @@
1
1
  <script lang="ts" setup>
2
2
  // @ts-expect-error virtual module
3
3
  import { files as rawFiles, tree as rawTree, onUpdate } from '$histoire-stories'
4
- import StoryList from './components/story/StoryList.vue'
4
+ import StoryList from './components/tree/StoryList.vue'
5
5
  import BaseSplitPane from './components/base/BaseSplitPane.vue'
6
6
  import { computed, ref, watch, defineAsyncComponent } from 'vue'
7
7
  import AppHeader from './components/app/AppHeader.vue'
@@ -132,7 +132,7 @@ async function printVNode(vnode) {
132
132
  serialized = [value.substring(2, value.length - 2).trim()];
133
133
  }
134
134
  else if (typeof value === 'function') {
135
- serialized = [value.toString()];
135
+ serialized = [value.toString().replace(/'/g, '\\\'').replace(/"/g, '\'')];
136
136
  }
137
137
  else {
138
138
  serialized = serializeAndCleanJs(value);
@@ -141,7 +141,7 @@ async function printVNode (vnode: VNode): Promise<string[]> {
141
141
  // It was formatted from auto building object (slot props)
142
142
  serialized = [value.substring(2, value.length - 2).trim()]
143
143
  } else if (typeof value === 'function') {
144
- serialized = [value.toString()]
144
+ serialized = [value.toString().replace(/'/g, '\\\'').replace(/"/g, '\'')]
145
145
  } else {
146
146
  serialized = serializeAndCleanJs(value)
147
147
  }
@@ -4,7 +4,7 @@ import { Icon } from '@iconify/vue'
4
4
  import { useStoryStore } from '../../stores/story'
5
5
  import { Story, Tree } from '../../types'
6
6
  import MobileOverlay from './MobileOverlay.vue'
7
- import StoryList from '../story/StoryList.vue'
7
+ import StoryList from '../tree/StoryList.vue'
8
8
 
9
9
  const props = defineProps<{
10
10
  tree: Tree
@@ -51,6 +51,7 @@ const kindLabels = {
51
51
  <div
52
52
  ref="el"
53
53
  data-test-id="search-item"
54
+ :data-selected="selected ? '' : undefined"
54
55
  >
55
56
  <BaseListItemLink
56
57
  :to="result.route"
@@ -0,0 +1,75 @@
1
+ <script lang="ts" setup>
2
+ import type { Story, TreeGroup, TreeFolder, TreeLeaf } from '../../types'
3
+ import StoryListItem from './StoryListItem.vue'
4
+ import StoryListFolder from './StoryListFolder.vue'
5
+ import { Icon } from '@iconify/vue'
6
+ import { computed, withDefaults } from 'vue'
7
+ import { useStoryStore } from '../../stores/story'
8
+
9
+ const props = withDefaults(defineProps<{
10
+ path?: Array<string>
11
+ group: TreeGroup
12
+ stories: Story[]
13
+ }>(), {
14
+ path: () => [],
15
+ })
16
+
17
+ const storyStore = useStoryStore()
18
+
19
+ const folderPath = computed(() => [...props.path, props.group.title])
20
+ const isFolderOpen = computed(() => storyStore.isFolderOpened(folderPath.value, true))
21
+
22
+ function toggleOpen () {
23
+ storyStore.toggleFolder(folderPath.value, false)
24
+ }
25
+ </script>
26
+
27
+ <template>
28
+ <div
29
+ data-test-id="story-group"
30
+ class="htw-my-2 first:htw-mt-0 last:htw-mb-0"
31
+ >
32
+ <div
33
+ v-if="group.title"
34
+ role="button"
35
+ tabindex="0"
36
+ class="htw-px-0.5 htw-py-2 md:htw-py-1.5 htw-mx-1 htw-rounded-sm hover:htw-bg-primary-100 dark:hover:htw-bg-primary-900 htw-cursor-pointer htw-select-none htw-flex htw-items-center htw-gap-1 htw-min-w-0"
37
+ @click="toggleOpen"
38
+ @keyup.enter="toggleOpen"
39
+ @keyup.space="toggleOpen"
40
+ >
41
+ <Icon
42
+ icon="carbon:caret-right"
43
+ class="htw-w-4 htw-h-4 htw-transition-transform htw-duration-150 htw-opacity-30 htw-flex-none"
44
+ :class="{
45
+ 'htw-rotate-90': isFolderOpen,
46
+ }"
47
+ />
48
+ <span class="htw-truncate htw-opacity-50">{{ group.title }}</span>
49
+ <span class="htw-h-[1px] htw-flex-1 htw-bg-gray-500/10 htw-mx-2" />
50
+ </div>
51
+
52
+ <!-- Children -->
53
+ <div
54
+ v-if="isFolderOpen"
55
+ >
56
+ <template
57
+ v-for="element of group.children"
58
+ :key="element.title"
59
+ >
60
+ <StoryListFolder
61
+ v-if="(element as TreeFolder).children"
62
+ :path="folderPath"
63
+ :folder="(element as TreeFolder)"
64
+ :stories="stories"
65
+ :depth="0"
66
+ />
67
+ <StoryListItem
68
+ v-else
69
+ :story="stories[(element as TreeLeaf).index]"
70
+ :depth="0"
71
+ />
72
+ </template>
73
+ </div>
74
+ </div>
75
+ </template>
@@ -1,7 +1,8 @@
1
1
  <script lang="ts" setup>
2
- import type { Story, Tree, TreeFolder, TreeLeaf } from '../../types'
2
+ import type { Story, Tree, TreeGroup, TreeFolder, TreeLeaf } from '../../types'
3
3
  import StoryListItem from './StoryListItem.vue'
4
4
  import StoryListFolder from './StoryListFolder.vue'
5
+ import StoryGroup from './StoryGroup.vue'
5
6
 
6
7
  const props = defineProps<{
7
8
  tree: Tree
@@ -15,8 +16,13 @@ const props = defineProps<{
15
16
  v-for="element of tree"
16
17
  :key="element.title"
17
18
  >
19
+ <StoryGroup
20
+ v-if="(element as TreeGroup).group"
21
+ :group="(element as TreeGroup)"
22
+ :stories="stories"
23
+ />
18
24
  <StoryListFolder
19
- v-if="(element as TreeFolder).children"
25
+ v-else-if="(element as TreeFolder).children"
20
26
  :folder="(element as TreeFolder)"
21
27
  :stories="stories"
22
28
  />
@@ -25,7 +25,7 @@ function toggleOpen () {
25
25
  }
26
26
 
27
27
  const folderPadding = computed(() => {
28
- return (props.depth * 16) + 'px'
28
+ return (props.depth * 12) + 'px'
29
29
  })
30
30
  </script>
31
31
 
@@ -34,13 +34,13 @@ const folderPadding = computed(() => {
34
34
  <div
35
35
  role="button"
36
36
  tabindex="0"
37
- class="htw-px-0.5 htw-py-2 md:htw-py-1.5 htw-m-1 htw-rounded-sm hover:htw-bg-primary-100 dark:hover:htw-bg-primary-900 htw-cursor-pointer htw-select-none htw-flex"
37
+ class="htw-px-0.5 htw-py-2 md:htw-py-1.5 htw-mx-1 htw-rounded-sm hover:htw-bg-primary-100 dark:hover:htw-bg-primary-900 htw-cursor-pointer htw-select-none htw-flex"
38
38
  @click="toggleOpen"
39
39
  @keyup.enter="toggleOpen"
40
40
  @keyup.space="toggleOpen"
41
41
  >
42
42
  <span class="bind-tree-padding htw-flex htw-items-center htw-gap-2 htw-min-w-0">
43
- <span class="htw-flex htw-gap-1 htw-flex-none htw-items-center">
43
+ <span class="htw-flex htw-flex-none htw-items-center">
44
44
  <Icon
45
45
  icon="carbon:caret-right"
46
46
  class="htw-w-4 htw-h-4 htw-transition-transform htw-duration-150 htw-opacity-30"
@@ -56,6 +56,8 @@ const folderPadding = computed(() => {
56
56
  <span class="htw-truncate">{{ folder.title }}</span>
57
57
  </span>
58
58
  </div>
59
+
60
+ <!-- Children -->
59
61
  <div
60
62
  v-if="isFolderOpen"
61
63
  >
@@ -66,7 +68,7 @@ const folderPadding = computed(() => {
66
68
  <StoryListFolder
67
69
  v-if="(element as TreeFolder).children"
68
70
  :path="folderPath"
69
- :folder="element"
71
+ :folder="(element as TreeFolder)"
70
72
  :stories="stories"
71
73
  :depth="depth + 1"
72
74
  />
@@ -14,7 +14,7 @@ const props = withDefaults(defineProps<{
14
14
  })
15
15
 
16
16
  const filePadding = computed(() => {
17
- return (props.depth * 16) + 'px'
17
+ return (props.depth * 12) + 'px'
18
18
  })
19
19
 
20
20
  const route = useRoute()
@@ -36,9 +36,9 @@ useScrollOnActive(active, el)
36
36
  storyId: story.id,
37
37
  },
38
38
  }"
39
- class="htw-pl-0.5 htw-pr-2 htw-py-2 md:htw-py-1.5 htw-m-1 htw-rounded-sm"
39
+ class="htw-pl-0.5 htw-pr-2 htw-py-2 md:htw-py-1.5 htw-mx-1 htw-rounded-sm"
40
40
  >
41
- <span class="bind-tree-margin htw-flex htw-items-center htw-gap-2 htw-pl-5 htw-min-w-0">
41
+ <span class="bind-tree-margin htw-flex htw-items-center htw-gap-2 htw-pl-4 htw-min-w-0">
42
42
  <Icon
43
43
  :icon="story.icon ?? 'carbon:cube'"
44
44
  class="base-list-item-link-icon htw-w-5 htw-h-5 sm:htw-w-4 sm:htw-h-4 htw-flex-none"
@@ -3,6 +3,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
3
3
  stories: import("vue").Ref<{
4
4
  id: string;
5
5
  title: string;
6
+ group?: string;
6
7
  variants: {
7
8
  id: string;
8
9
  title: string;
@@ -49,6 +50,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
49
50
  currentStory: import("vue").ComputedRef<{
50
51
  id: string;
51
52
  title: string;
53
+ group?: string;
52
54
  variants: {
53
55
  id: string;
54
56
  title: string;
@@ -104,12 +106,13 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
104
106
  configReady?: boolean;
105
107
  previewReady?: boolean;
106
108
  }>;
107
- isFolderOpened: (path: Array<string>) => boolean;
108
- toggleFolder: (path: Array<string>, force?: boolean) => void;
109
+ isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
110
+ toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
109
111
  }, "stories">>, import("pinia")._UnwrapAll<Pick<{
110
112
  stories: import("vue").Ref<{
111
113
  id: string;
112
114
  title: string;
115
+ group?: string;
113
116
  variants: {
114
117
  id: string;
115
118
  title: string;
@@ -156,6 +159,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
156
159
  currentStory: import("vue").ComputedRef<{
157
160
  id: string;
158
161
  title: string;
162
+ group?: string;
159
163
  variants: {
160
164
  id: string;
161
165
  title: string;
@@ -211,12 +215,13 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
211
215
  configReady?: boolean;
212
216
  previewReady?: boolean;
213
217
  }>;
214
- isFolderOpened: (path: Array<string>) => boolean;
215
- toggleFolder: (path: Array<string>, force?: boolean) => void;
218
+ isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
219
+ toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
216
220
  }, "currentStory" | "currentVariant">>, Pick<{
217
221
  stories: import("vue").Ref<{
218
222
  id: string;
219
223
  title: string;
224
+ group?: string;
220
225
  variants: {
221
226
  id: string;
222
227
  title: string;
@@ -263,6 +268,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
263
268
  currentStory: import("vue").ComputedRef<{
264
269
  id: string;
265
270
  title: string;
271
+ group?: string;
266
272
  variants: {
267
273
  id: string;
268
274
  title: string;
@@ -318,6 +324,6 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
318
324
  configReady?: boolean;
319
325
  previewReady?: boolean;
320
326
  }>;
321
- isFolderOpened: (path: Array<string>) => boolean;
322
- toggleFolder: (path: Array<string>, force?: boolean) => void;
327
+ isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
328
+ toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
323
329
  }, "setStories" | "isFolderOpened" | "toggleFolder">>;
@@ -13,20 +13,25 @@ export const useStoryStore = defineStore('story', () => {
13
13
  function getStringPath(path) {
14
14
  return path.join('␜');
15
15
  }
16
- function toggleFolder(path, force) {
16
+ function toggleFolder(path, defaultToggleValue = true) {
17
17
  const stringPath = getStringPath(path);
18
- if (force === undefined) {
19
- force = !openedFolders.value.get(stringPath);
18
+ const currentValue = openedFolders.value.get(stringPath);
19
+ if (currentValue == null) {
20
+ openedFolders.value.set(stringPath, defaultToggleValue);
20
21
  }
21
- if (force) {
22
- openedFolders.value.set(stringPath, true);
22
+ else if (currentValue) {
23
+ openedFolders.value.set(stringPath, false);
23
24
  }
24
25
  else {
25
- openedFolders.value.delete(stringPath);
26
+ openedFolders.value.set(stringPath, true);
26
27
  }
27
28
  }
28
- function isFolderOpened(path) {
29
- return openedFolders.value.get(getStringPath(path));
29
+ function isFolderOpened(path, defaultValue = false) {
30
+ const value = openedFolders.value.get(getStringPath(path));
31
+ if (value == null) {
32
+ return defaultValue;
33
+ }
34
+ return value;
30
35
  }
31
36
  function openFileFolders(path) {
32
37
  for (let pathLength = 1; pathLength < path.length; pathLength++) {
@@ -23,22 +23,26 @@ export const useStoryStore = defineStore('story', () => {
23
23
  return path.join('␜')
24
24
  }
25
25
 
26
- function toggleFolder (path: Array<string>, force?: boolean) {
26
+ function toggleFolder (path: Array<string>, defaultToggleValue = true) {
27
27
  const stringPath = getStringPath(path)
28
28
 
29
- if (force === undefined) {
30
- force = !openedFolders.value.get(stringPath)
31
- }
29
+ const currentValue = openedFolders.value.get(stringPath)
32
30
 
33
- if (force) {
34
- openedFolders.value.set(stringPath, true)
31
+ if (currentValue == null) {
32
+ openedFolders.value.set(stringPath, defaultToggleValue)
33
+ } else if (currentValue) {
34
+ openedFolders.value.set(stringPath, false)
35
35
  } else {
36
- openedFolders.value.delete(stringPath)
36
+ openedFolders.value.set(stringPath, true)
37
37
  }
38
38
  }
39
39
 
40
- function isFolderOpened (path: Array<string>) {
41
- return openedFolders.value.get(getStringPath(path))
40
+ function isFolderOpened (path: Array<string>, defaultValue = false) {
41
+ const value = openedFolders.value.get(getStringPath(path))
42
+ if (value == null) {
43
+ return defaultValue
44
+ }
45
+ return value
42
46
  }
43
47
 
44
48
  function openFileFolders (path: Array<string>) {
@@ -29,4 +29,6 @@
29
29
  }
30
30
  }
31
31
 
32
-
32
+ .v-popper__popper:focus-visible {
33
+ outline: none;
34
+ }
@@ -9,6 +9,7 @@ export interface StoryFile {
9
9
  export interface Story {
10
10
  id: string;
11
11
  title: string;
12
+ group?: string;
12
13
  variants: Variant[];
13
14
  layout?: {
14
15
  type: 'single';
@@ -42,7 +43,13 @@ export declare type TreeFolder = {
42
43
  title: string;
43
44
  children: (TreeFolder | TreeLeaf)[];
44
45
  };
45
- export declare type Tree = (TreeFolder | TreeLeaf)[];
46
+ export interface TreeGroup {
47
+ group: true;
48
+ id: string;
49
+ title: string;
50
+ children: (TreeFolder | TreeLeaf)[];
51
+ }
52
+ export declare type Tree = (TreeGroup | TreeFolder | TreeLeaf)[];
46
53
  export interface SearchResult {
47
54
  kind: 'story' | 'variant';
48
55
  rank: number;
@@ -11,6 +11,7 @@ export interface StoryFile {
11
11
  export interface Story {
12
12
  id: string
13
13
  title: string
14
+ group?: string
14
15
  variants: Variant[]
15
16
  layout?: {
16
17
  type: 'single'
@@ -48,7 +49,14 @@ export type TreeFolder = {
48
49
  children: (TreeFolder | TreeLeaf)[]
49
50
  }
50
51
 
51
- export type Tree = (TreeFolder | TreeLeaf)[]
52
+ export interface TreeGroup {
53
+ group: true
54
+ id: string
55
+ title: string
56
+ children: (TreeFolder | TreeLeaf)[]
57
+ }
58
+
59
+ export type Tree = (TreeGroup | TreeFolder | TreeLeaf)[]
52
60
 
53
61
  export interface SearchResult {
54
62
  kind: 'story' | 'variant'
@@ -8,7 +8,12 @@ export default defineComponent({
8
8
  props: {
9
9
  title: {
10
10
  type: String,
11
- required: true,
11
+ default: null,
12
+ },
13
+
14
+ group: {
15
+ type: String,
16
+ default: null,
12
17
  },
13
18
 
14
19
  layout: {
@@ -36,7 +41,8 @@ export default defineComponent({
36
41
 
37
42
  const story: Story = {
38
43
  id: attrs.data.id,
39
- title: props.title,
44
+ title: props.title ?? attrs.data.fileName,
45
+ group: props.group,
40
46
  layout: props.layout,
41
47
  icon: props.icon,
42
48
  iconColor: props.iconColor,
@@ -59,11 +59,11 @@ export function useCollectStories(options, ctx) {
59
59
  console.warn(pc.yellow(`⚠️ Multiple stories not supported: ${storyFile.path}`));
60
60
  }
61
61
  storyFile.story = storyData[0];
62
- const file = {
62
+ storyFile.treeFile = {
63
63
  title: storyData[0].title,
64
64
  path: relative(server.config.root, storyFile.path),
65
65
  };
66
- storyFile.treePath = createPath(ctx.config, file);
66
+ storyFile.treePath = createPath(ctx.config, storyFile.treeFile);
67
67
  storyFile.story.title = storyFile.treePath[storyFile.treePath.length - 1];
68
68
  }
69
69
  catch (e) {
@@ -4,15 +4,20 @@ import type MarkdownIt from 'markdown-it';
4
4
  declare type CustomizableColors = 'primary' | 'gray';
5
5
  declare type ColorKeys = '50' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';
6
6
  declare type GrayColorKeys = ColorKeys | '750' | '850' | '950';
7
- interface ResponsivePreset {
7
+ export interface ResponsivePreset {
8
8
  label: string;
9
9
  width: number;
10
10
  height: number;
11
11
  }
12
- interface BackgroundPreset {
12
+ export interface BackgroundPreset {
13
13
  label: string;
14
14
  color: string;
15
15
  }
16
+ export interface TreeGroupConfig {
17
+ title: string;
18
+ id?: string;
19
+ include?: (file: TreeFile) => boolean;
20
+ }
16
21
  export interface HistoireConfig {
17
22
  /**
18
23
  * Output directory.
@@ -37,6 +42,7 @@ export interface HistoireConfig {
37
42
  */
38
43
  file?: 'title' | 'path' | ((file: TreeFile) => string[]);
39
44
  order?: 'asc' | ((a: string, b: string) => number);
45
+ groups?: TreeGroupConfig[];
40
46
  };
41
47
  /**
42
48
  * Customize the look of the histoire book.
@@ -89,7 +89,7 @@ export async function createVitePlugins(ctx) {
89
89
  return `import { defineAsyncComponent } from 'vue'
90
90
  ${resolvedStories.map((file, index) => `const Comp${index} = defineAsyncComponent(() => import('${file.path}'))`).join('\n')}
91
91
  export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
92
- export let tree = ${JSON.stringify(makeTree(ctx.config, files))}
92
+ export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
93
93
  const handlers = []
94
94
  export function onUpdate (cb) {
95
95
  handlers.push(cb)
@@ -1,7 +1,7 @@
1
1
  import chokidar from 'chokidar';
2
2
  import { globby } from 'globby';
3
3
  import Case from 'case';
4
- import { join } from 'pathe';
4
+ import { join, basename } from 'pathe';
5
5
  const handlers = [];
6
6
  let context;
7
7
  export function onStoryChange(handler) {
@@ -35,9 +35,14 @@ function getAbsoluteFilePath(relativeFilePath) {
35
35
  function addStory(relativeFilePath) {
36
36
  const absoluteFilePath = getAbsoluteFilePath(relativeFilePath);
37
37
  const fileId = Case.kebab(relativeFilePath);
38
+ let fileName = basename(relativeFilePath);
39
+ if (fileName.includes('.')) {
40
+ fileName = fileName.substring(0, fileName.indexOf('.'));
41
+ }
38
42
  const file = {
39
43
  id: fileId,
40
44
  path: absoluteFilePath,
45
+ fileName,
41
46
  moduleId: `/${relativeFilePath}`,
42
47
  };
43
48
  context.storyFiles.push(file);
@@ -1,19 +1,23 @@
1
- import { HistoireConfig } from './config.js';
2
- export declare type TreeFile = {
1
+ import type { HistoireConfig } from './config.js';
2
+ import type { StoryFile } from './types.js';
3
+ export interface TreeFile {
3
4
  title: string;
4
5
  path: string;
5
- };
6
- export declare type TreeLeaf = {
6
+ }
7
+ export interface TreeLeaf {
7
8
  title: string;
8
9
  index: number;
9
- };
10
- export declare type TreeFolder = {
10
+ }
11
+ export interface TreeFolder {
11
12
  title: string;
12
13
  children: (TreeFolder | TreeLeaf)[];
13
- };
14
- export declare type Tree = (TreeFolder | TreeLeaf)[];
14
+ }
15
+ export interface TreeGroup {
16
+ group: true;
17
+ id: string;
18
+ title: string;
19
+ children: (TreeFolder | TreeLeaf)[];
20
+ }
21
+ export declare type Tree = (TreeGroup | TreeFolder | TreeLeaf)[];
15
22
  export declare function createPath(config: HistoireConfig, file: TreeFile): string[];
16
- export declare function makeTree(config: HistoireConfig, files: {
17
- path: string[];
18
- index: number;
19
- }[]): Tree;
23
+ export declare function makeTree(config: HistoireConfig, files: StoryFile[]): Tree;
package/dist/node/tree.js CHANGED
@@ -1,3 +1,4 @@
1
+ import pc from 'picocolors';
1
2
  export function createPath(config, file) {
2
3
  if (config.tree.file === 'title') {
3
4
  return file.title.split('/');
@@ -11,15 +12,54 @@ export function createPath(config, file) {
11
12
  return config.tree.file(file);
12
13
  }
13
14
  export function makeTree(config, files) {
14
- const treeObject = {};
15
- for (const file of files) {
16
- setPath(file.path, file.index, treeObject);
17
- }
15
+ const groups = config.tree?.groups?.map(g => ({
16
+ groupConfig: g,
17
+ treeObject: {},
18
+ })) || [];
19
+ const defaultGroup = {
20
+ treeObject: {},
21
+ };
22
+ groups.push(defaultGroup);
23
+ files.forEach((file, index) => {
24
+ const group = getGroup(file);
25
+ setPath(file.treePath, index, group.treeObject);
26
+ });
18
27
  let sortingFunction = (a, b) => a.localeCompare(b);
19
28
  if (config.tree.order !== 'asc') {
20
29
  sortingFunction = config.tree.order;
21
30
  }
22
- return buildTree(treeObject);
31
+ const result = [];
32
+ for (const group of groups) {
33
+ if (group === defaultGroup) {
34
+ result.push(...buildTree(group.treeObject));
35
+ }
36
+ else {
37
+ result.push({
38
+ group: true,
39
+ id: group.groupConfig.id,
40
+ title: group.groupConfig.title,
41
+ children: buildTree(group.treeObject),
42
+ });
43
+ }
44
+ }
45
+ return result;
46
+ function getGroup(file) {
47
+ if (file.story?.group) {
48
+ const group = groups.find(g => g.groupConfig?.id === file.story.group);
49
+ if (group) {
50
+ return group;
51
+ }
52
+ else {
53
+ console.error(pc.red(`Group ${file.story.group} not found for story ${file.path}`));
54
+ }
55
+ }
56
+ for (const group of groups) {
57
+ if (group.groupConfig?.include && group.groupConfig.include(file.treeFile)) {
58
+ return group;
59
+ }
60
+ }
61
+ return defaultGroup;
62
+ }
23
63
  function setPath(path, value, tree) {
24
64
  path.reduce((subtree, key, i) => {
25
65
  if (i === path.length - 1) {
@@ -1,9 +1,14 @@
1
+ import type { TreeFile } from './tree.js';
1
2
  export interface StoryFile {
2
3
  id: string;
3
4
  /**
4
5
  * Absolute path
5
6
  */
6
7
  path: string;
8
+ /**
9
+ * File name without extension
10
+ */
11
+ fileName: string;
7
12
  /**
8
13
  * Generated path for tree UI
9
14
  */
@@ -16,10 +21,15 @@ export interface StoryFile {
16
21
  * Resolved story data from story file execution
17
22
  */
18
23
  story?: Story;
24
+ /**
25
+ * Data sent to user tree config functions
26
+ */
27
+ treeFile?: TreeFile;
19
28
  }
20
29
  export interface Story {
21
30
  id: string;
22
31
  title: string;
32
+ group?: string;
23
33
  variants: Variant[];
24
34
  layout?: {
25
35
  type: 'single';