histoire 0.3.3 → 0.4.2

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 (95) hide show
  1. package/dist/bundled/BaseEmpty.js +13 -0
  2. package/dist/bundled/HomeView.js +23 -0
  3. package/dist/bundled/MobileOverlay.js +303 -0
  4. package/dist/bundled/SearchPane.js +504 -0
  5. package/dist/bundled/StoryView.js +2330 -0
  6. package/dist/bundled/api.js +32 -0
  7. package/dist/bundled/config.js +4 -0
  8. package/dist/bundled/const.js +5 -0
  9. package/dist/bundled/dark.js +4 -0
  10. package/dist/bundled/events.js +29 -0
  11. package/dist/bundled/global-components.js +203 -0
  12. package/dist/bundled/index.js +7 -0
  13. package/dist/bundled/index2.js +762 -0
  14. package/dist/bundled/reactivity.js +96 -0
  15. package/dist/bundled/sandbox.js +97 -0
  16. package/dist/bundled/search-docs-data.js +7 -0
  17. package/dist/bundled/story.js +59 -0
  18. package/dist/bundled/style.css +102 -0
  19. package/dist/bundled/vue.runtime.esm-bundler.js +8657 -0
  20. package/dist/client/app/App.vue +2 -2
  21. package/dist/client/app/api.d.ts +1 -0
  22. package/dist/client/app/api.js +1 -0
  23. package/dist/client/app/codegen/vue3.js +1 -2
  24. package/dist/client/app/components/HomeView.vue +1 -1
  25. package/dist/client/app/components/app/AppLogo.vue +1 -1
  26. package/dist/client/app/components/app/MobileOverlay.vue +0 -1
  27. package/dist/client/app/components/base/BaseOverflowMenu.vue +1 -1
  28. package/dist/client/app/components/exposed/Story.vue +1 -1
  29. package/dist/client/app/components/search/SearchItem.vue +1 -1
  30. package/dist/client/app/components/search/SearchPane.vue +96 -21
  31. package/dist/client/app/components/search/search-docs-data.d.ts +3 -0
  32. package/dist/client/app/components/search/{search-data.js → search-docs-data.js} +1 -1
  33. package/dist/client/app/components/search/search-title-data.d.ts +3 -0
  34. package/dist/client/app/components/search/search-title-data.js +7 -0
  35. package/dist/client/app/components/search/{search-data.d.ts → types.d.ts} +0 -2
  36. package/dist/client/app/components/search/types.js +1 -0
  37. package/dist/client/app/components/story/StoryVariantGridItem.vue +1 -1
  38. package/dist/client/app/components/story/StoryVariantListItem.vue +1 -1
  39. package/dist/client/app/components/story/StoryVariantSinglePreviewNative.vue +6 -3
  40. package/dist/client/app/components/tree/StoryListItem.vue +1 -1
  41. package/dist/client/app/index.d.ts +1 -1
  42. package/dist/client/app/index.js +21 -18
  43. package/dist/client/app/sandbox.d.ts +1 -1
  44. package/dist/client/app/sandbox.js +0 -1
  45. package/dist/client/app/util/events.d.ts +1 -1
  46. package/dist/client/app/util/events.js +2 -2
  47. package/dist/client/bundle-index.js +5 -0
  48. package/dist/client/bundle-main.js +5 -0
  49. package/dist/client/bundle-sandbox.js +3 -0
  50. package/dist/client/index.d.ts +1 -1
  51. package/dist/client/index.js +1 -1
  52. package/dist/client/server/vue3/run.js +8 -0
  53. package/dist/node/alias.js +1 -1
  54. package/dist/node/build.js +4 -4
  55. package/dist/node/builtin-plugins/tailwind-tokens.js +1 -1
  56. package/dist/node/search.d.ts +9 -1
  57. package/dist/node/search.js +69 -32
  58. package/dist/node/server.js +2 -2
  59. package/dist/node/types.d.ts +1 -0
  60. package/dist/node/vite.d.ts +4 -2
  61. package/dist/node/vite.js +22 -29
  62. package/package.json +7 -6
  63. package/src/client/app/App.vue +2 -2
  64. package/src/client/app/api.ts +1 -0
  65. package/src/client/app/codegen/vue3.ts +1 -2
  66. package/src/client/app/components/HomeView.vue +1 -1
  67. package/src/client/app/components/app/AppLogo.vue +1 -1
  68. package/src/client/app/components/app/MobileOverlay.vue +0 -1
  69. package/src/client/app/components/base/BaseOverflowMenu.vue +1 -1
  70. package/src/client/app/components/exposed/Story.vue +1 -1
  71. package/src/client/app/components/search/SearchItem.vue +1 -1
  72. package/src/client/app/components/search/SearchPane.vue +96 -21
  73. package/src/client/app/components/search/{search-data.ts → search-docs-data.ts} +2 -6
  74. package/src/client/app/components/search/search-title-data.ts +10 -0
  75. package/src/client/app/components/search/types.ts +4 -0
  76. package/src/client/app/components/story/StoryVariantGridItem.vue +1 -1
  77. package/src/client/app/components/story/StoryVariantListItem.vue +1 -1
  78. package/src/client/app/components/story/StoryVariantSinglePreviewNative.vue +6 -3
  79. package/src/client/app/components/tree/StoryListItem.vue +1 -1
  80. package/src/client/app/index.ts +23 -19
  81. package/src/client/app/sandbox.ts +0 -1
  82. package/src/client/app/util/events.ts +2 -2
  83. package/src/client/bundle-index.js +5 -0
  84. package/src/client/bundle-main.js +5 -0
  85. package/src/client/bundle-sandbox.js +3 -0
  86. package/src/client/index.ts +1 -1
  87. package/src/client/server/vue3/run.ts +11 -0
  88. package/src/node/alias.ts +1 -1
  89. package/src/node/build.ts +4 -4
  90. package/src/node/builtin-plugins/tailwind-tokens.ts +1 -1
  91. package/src/node/search.ts +77 -33
  92. package/src/node/server.ts +2 -2
  93. package/src/node/types.ts +1 -0
  94. package/src/node/vite.ts +22 -30
  95. package/vite.config.ts +46 -0
@@ -4,57 +4,77 @@ import path from 'pathe';
4
4
  import Case from 'case';
5
5
  import { loadModule } from './load.js';
6
6
  const require = createRequire(import.meta.url);
7
- export async function generateSearchData(ctx) {
8
- let uid = 0;
9
- const idMap = {};
7
+ export async function generateTitleSearchData(ctx) {
8
+ const searchIndex = await createIndex();
9
+ const { idMap, addToIdMap } = createIdMap();
10
+ for (const storyFile of ctx.storyFiles) {
11
+ if (storyFile.story) {
12
+ searchIndex.add({
13
+ id: addToIdMap(storyFile.story.id, 'story'),
14
+ text: convertTitleToSentence(storyFile.story.title),
15
+ });
16
+ for (const variant of storyFile.story.variants) {
17
+ searchIndex.add({
18
+ id: addToIdMap(`${storyFile.story.id}:${variant.id}`, 'variant'),
19
+ text: convertTitleToSentence(`${storyFile.story.title} ${variant.title}`),
20
+ });
21
+ }
22
+ }
23
+ }
24
+ return {
25
+ index: await exportSearchIndex(searchIndex),
26
+ idMap,
27
+ };
28
+ }
29
+ export async function generateDocSearchData(ctx) {
30
+ const searchIndex = await createIndex();
31
+ const { idMap, addToIdMap } = createIdMap();
32
+ for (const storyFile of ctx.storyFiles) {
33
+ if (storyFile.story && storyFile.story.docsText) {
34
+ searchIndex.add({
35
+ id: addToIdMap(storyFile.story.id, 'story'),
36
+ text: storyFile.story.docsText,
37
+ });
38
+ }
39
+ }
40
+ return {
41
+ index: await exportSearchIndex(searchIndex),
42
+ idMap,
43
+ };
44
+ }
45
+ async function createIndex() {
10
46
  const flexsearchRoot = path.dirname(require.resolve('flexsearch/package.json'));
11
- const searchIndex = new flexsearch.Document({
12
- preset: 'memory',
47
+ return new flexsearch.Document({
48
+ preset: 'match',
13
49
  document: {
14
50
  id: 'id',
15
51
  index: [
16
- 'title',
17
- 'docs',
52
+ 'text',
18
53
  ],
19
54
  },
20
55
  charset: await loadModule(`${flexsearchRoot}/dist/module/lang/latin/advanced.js`),
21
56
  language: await loadModule(`${flexsearchRoot}/dist/module/lang/en.js`),
22
57
  tokenize: 'forward',
23
58
  });
59
+ }
60
+ function createIdMap() {
61
+ let uid = 0;
62
+ const idMap = {};
24
63
  function addToIdMap(id, kind) {
25
64
  const n = uid++;
26
65
  idMap[n] = { id, kind };
27
66
  return n;
28
67
  }
29
- for (const storyFile of ctx.storyFiles) {
30
- if (storyFile.story) {
31
- searchIndex.add({
32
- id: addToIdMap(storyFile.story.id, 'story'),
33
- title: processTitle(storyFile.story.title),
34
- docs: '', // @TODO
35
- });
36
- for (const variant of storyFile.story.variants) {
37
- searchIndex.add({
38
- id: addToIdMap(`${storyFile.story.id}:${variant.id}`, 'variant'),
39
- title: processTitle(`${storyFile.story.title} ${variant.title}`),
40
- docs: '', // @TODO
41
- });
42
- }
43
- }
44
- }
45
68
  return {
46
- index: await exportSearchIndex(searchIndex),
47
69
  idMap,
70
+ addToIdMap,
48
71
  };
49
72
  }
50
73
  const exportKeys = new Set([
51
74
  'reg',
52
- 'title.cfg',
53
- 'title.map',
54
- 'title.ctx',
55
- 'docs.cfg',
56
- 'docs.map',
57
- 'docs.ctx',
75
+ 'text.cfg',
76
+ 'text.map',
77
+ 'text.ctx',
58
78
  'tag',
59
79
  'store',
60
80
  ]);
@@ -72,6 +92,23 @@ async function exportSearchIndex(index) {
72
92
  });
73
93
  });
74
94
  }
75
- function processTitle(title) {
76
- return title.split(' ').map(str => Case.lower(str)).join(' ');
95
+ function convertTitleToSentence(text) {
96
+ return text.split(' ').map(str => Case.lower(str)).join(' ');
97
+ }
98
+ // @TODO clear handlers when SearchPane unmounts
99
+ export function getSearchDataJS(data) {
100
+ return `export let searchData = ${JSON.stringify(data)}
101
+ const handlers = []
102
+ export function onUpdate (cb) {
103
+ handlers.push(cb)
104
+ }
105
+ if (import.meta.hot) {
106
+ import.meta.hot.accept(newModule => {
107
+ searchData = newModule.searchData
108
+ handlers.forEach(h => {
109
+ h(newModule.searchData)
110
+ newModule.onUpdate(h)
111
+ })
112
+ })
113
+ }`;
77
114
  }
@@ -1,5 +1,5 @@
1
1
  import { createServer as createViteServer } from 'vite';
2
- import { createVitePlugins, RESOLVED_SEARCH_DATA_ID, RESOLVED_STORIES_ID } from './vite.js';
2
+ import { createVitePlugins, RESOLVED_SEARCH_TITLE_DATA_ID, RESOLVED_STORIES_ID } from './vite.js';
3
3
  import { notifyStoryChange, onStoryChange, watchStories } from './stories.js';
4
4
  import { useCollectStories } from './collect/index.js';
5
5
  import { DevPluginApi } from './plugin.js';
@@ -94,7 +94,7 @@ export async function createServer(ctx, port) {
94
94
  });
95
95
  }
96
96
  invalidateModule(RESOLVED_STORIES_ID);
97
- invalidateModule(RESOLVED_SEARCH_DATA_ID);
97
+ invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID);
98
98
  });
99
99
  async function close() {
100
100
  for (const cb of pluginOnCleanups) {
@@ -40,6 +40,7 @@ export interface Story {
40
40
  icon?: string;
41
41
  iconColor?: string;
42
42
  docsOnly?: boolean;
43
+ docsText?: string;
43
44
  }
44
45
  export interface Variant {
45
46
  id: string;
@@ -8,6 +8,8 @@ export declare const CONFIG_ID = "$histoire-config";
8
8
  export declare const RESOLVED_CONFIG_ID: string;
9
9
  export declare const THEME_ID = "$histoire-theme";
10
10
  export declare const RESOLVED_THEME_ID: string;
11
- export declare const SEARCH_DATA_ID = "$histoire-search-data";
12
- export declare const RESOLVED_SEARCH_DATA_ID: string;
11
+ export declare const SEARCH_TITLE_DATA_ID = "$histoire-search-title-data";
12
+ export declare const RESOLVED_SEARCH_TITLE_DATA_ID: string;
13
+ export declare const SEARCH_DOCS_DATA_ID = "$histoire-search-docs-data";
14
+ export declare const RESOLVED_SEARCH_DOCS_DATA_ID: string;
13
15
  export declare function createVitePlugins(ctx: Context): Promise<VitePlugin[]>;
package/dist/node/vite.js CHANGED
@@ -7,7 +7,7 @@ import { notifyStoryChange } from './stories.js';
7
7
  import { makeTree } from './tree.js';
8
8
  import { parseColor } from './colors.js';
9
9
  import { createMarkdownRenderer } from './markdown.js';
10
- import { generateSearchData } from './search.js';
10
+ import { getSearchDataJS, generateDocSearchData, generateTitleSearchData } from './search.js';
11
11
  const require = createRequire(import.meta.url);
12
12
  export const STORIES_ID = '$histoire-stories';
13
13
  export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`;
@@ -17,15 +17,13 @@ export const CONFIG_ID = '$histoire-config';
17
17
  export const RESOLVED_CONFIG_ID = `/${CONFIG_ID}-resolved`;
18
18
  export const THEME_ID = '$histoire-theme';
19
19
  export const RESOLVED_THEME_ID = `/${THEME_ID}-resolved.css`;
20
- export const SEARCH_DATA_ID = '$histoire-search-data';
21
- export const RESOLVED_SEARCH_DATA_ID = `/${SEARCH_DATA_ID}-resolved`;
20
+ export const SEARCH_TITLE_DATA_ID = '$histoire-search-title-data';
21
+ export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`;
22
+ export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data';
23
+ export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`;
22
24
  export async function createVitePlugins(ctx) {
23
25
  const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build');
24
- const hasVuePlugin = viteConfig.plugins.find(p => p.name === 'vite:vue');
25
26
  const plugins = [];
26
- if (!hasVuePlugin) {
27
- plugins.push((await import('@vitejs/plugin-vue')).default());
28
- }
29
27
  if (ctx.config.vite) {
30
28
  plugins.push({
31
29
  name: 'histoire-vite-config-override',
@@ -42,14 +40,13 @@ export async function createVitePlugins(ctx) {
42
40
  // force include vue to avoid duplicated copies when linked + optimized
43
41
  include: [
44
42
  'vue',
45
- dirname(require.resolve('@vue/runtime-core/package.json')),
46
43
  dirname(require.resolve('shiki/package.json')),
47
44
  dirname(require.resolve('vscode-textmate/package.json')),
48
45
  dirname(require.resolve('vscode-oniguruma/package.json')),
49
46
  dirname(require.resolve('case/package.json')),
50
47
  ],
51
48
  exclude: [
52
- 'histoire/client',
49
+ 'histoire',
53
50
  ],
54
51
  },
55
52
  server: {
@@ -83,8 +80,11 @@ export async function createVitePlugins(ctx) {
83
80
  if (id.startsWith(THEME_ID)) {
84
81
  return RESOLVED_THEME_ID;
85
82
  }
86
- if (id.startsWith(SEARCH_DATA_ID)) {
87
- return RESOLVED_SEARCH_DATA_ID;
83
+ if (id.startsWith(SEARCH_TITLE_DATA_ID)) {
84
+ return RESOLVED_SEARCH_TITLE_DATA_ID;
85
+ }
86
+ if (id.startsWith(SEARCH_DOCS_DATA_ID)) {
87
+ return RESOLVED_SEARCH_DOCS_DATA_ID;
88
88
  }
89
89
  },
90
90
  async load(id) {
@@ -94,7 +94,10 @@ export async function createVitePlugins(ctx) {
94
94
  return {
95
95
  id: file.id,
96
96
  path: file.treePath,
97
- story: file.story,
97
+ story: {
98
+ ...file.story,
99
+ docsText: undefined,
100
+ },
98
101
  framework: 'vue3',
99
102
  index,
100
103
  };
@@ -142,21 +145,11 @@ if (import.meta.hot) {
142
145
  css += '}';
143
146
  return css;
144
147
  }
145
- if (id === RESOLVED_SEARCH_DATA_ID) {
146
- return `export let searchData = ${JSON.stringify(await generateSearchData(ctx))}
147
- const handlers = []
148
- export function onUpdate (cb) {
149
- handlers.push(cb)
150
- }
151
- if (import.meta.hot) {
152
- import.meta.hot.accept(newModule => {
153
- searchData = newModule.searchData
154
- handlers.forEach(h => {
155
- h(newModule.searchData)
156
- newModule.onUpdate(h)
157
- })
158
- })
159
- }`;
148
+ if (id === RESOLVED_SEARCH_TITLE_DATA_ID) {
149
+ return getSearchDataJS(await generateTitleSearchData(ctx));
150
+ }
151
+ if (id === RESOLVED_SEARCH_DOCS_DATA_ID) {
152
+ return getSearchDataJS(await generateDocSearchData(ctx));
160
153
  }
161
154
  },
162
155
  handleHotUpdate(updateContext) {
@@ -189,7 +182,7 @@ if (import.meta.hot) {
189
182
  }
190
183
  }
191
184
  </script>
192
- <script type="module" src="/@fs/${APP_PATH}/sandbox.js"></script>
185
+ <script type="module" src="/@fs/${APP_PATH}/bundle-sandbox.js"></script>
193
186
  </body>
194
187
  </html>`;
195
188
  // Apply Vite HTML transforms. This injects the Vite HMR client, and
@@ -218,7 +211,7 @@ if (import.meta.hot) {
218
211
  </head>
219
212
  <body>
220
213
  <div id="app"></div>
221
- <script type="module" src="/@fs/${APP_PATH}/index.js"></script>
214
+ <script type="module" src="/@fs/${APP_PATH}/bundle-main.js"></script>
222
215
  </body>
223
216
  </html>`;
224
217
  // Apply Vite HTML transforms. This injects the Vite HMR client, and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "histoire",
3
- "version": "0.3.3",
3
+ "version": "0.4.2",
4
4
  "description": "A fast story book powered by Vite",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -21,7 +21,7 @@
21
21
  "types": "./dist/node/index.d.ts"
22
22
  },
23
23
  "./client": {
24
- "import": "./dist/client/index.js",
24
+ "import": "./dist/client/bundle-index.js",
25
25
  "types": "./dist/client/index.d.ts"
26
26
  },
27
27
  "./*": "./*"
@@ -33,9 +33,9 @@
33
33
  "histoire": "./bin.mjs"
34
34
  },
35
35
  "dependencies": {
36
- "@histoire/controls": "^0.3.3",
36
+ "@histoire/controls": "^0.4.2",
37
37
  "@iconify/vue": "^3.2.1",
38
- "@vitejs/plugin-vue": "^2.3.1",
38
+ "@types/markdown-it": "^12.2.3",
39
39
  "@vueuse/core": "^8.2.5",
40
40
  "birpc": "^0.1.1",
41
41
  "case": "^1.6.3",
@@ -75,6 +75,7 @@
75
75
  "@types/fs-extra": "^9.0.13",
76
76
  "@types/markdown-it": "^12.2.3",
77
77
  "@types/node": "^17.0.23",
78
+ "@vitejs/plugin-vue": "^2.3.1",
78
79
  "autoprefixer": "^10.4.4",
79
80
  "concurrently": "^7.1.0",
80
81
  "postcss": "^8.4.12",
@@ -85,9 +86,9 @@
85
86
  "vue": "^3.2.31"
86
87
  },
87
88
  "scripts": {
88
- "build": "rimraf dist && tsc -d && pnpm run build:css && node ./scripts/copy.mjs",
89
+ "build": "rimraf dist && tsc -d && vite build && pnpm run build:css && node ./scripts/copy.mjs",
89
90
  "build:css": "tailwindcss -i src/client/app/style/main.pcss -o ./dist/style.css -c tailwind.config.cjs --postcss postcss.config.cjs",
90
- "watch": "concurrently \"tsc -d -w --sourceMap\" \"pnpm run build:css --watch\" \"node ./scripts/watchAndCopy.mjs\"",
91
+ "watch": "concurrently \"tsc -d -w --sourceMap\" \"vite build --watch\" \"pnpm run build:css --watch\" \"node ./scripts/watchAndCopy.mjs\"",
91
92
  "test:open": "peeky open",
92
93
  "test": "peeky run"
93
94
  }
@@ -15,8 +15,8 @@ import type { StoryFile, Tree } from './types'
15
15
  import { useStoryStore } from './stores/story'
16
16
  import { mapFile } from './util/mapping'
17
17
  import { useTitle } from '@vueuse/core'
18
- import { histoireConfig } from './util/config.js'
19
- import { onKeyboardShortcut } from './util/keyboard.js'
18
+ import { histoireConfig } from './util/config'
19
+ import { onKeyboardShortcut } from './util/keyboard'
20
20
  import { isMobile } from './util/responsive'
21
21
  import Breadcrumb from './components/app/Breadcrumb.vue'
22
22
  import SearchModal from './components/search/SearchModal.vue'
@@ -0,0 +1 @@
1
+ export { hstEvent } from './util/events.js'
@@ -1,8 +1,7 @@
1
1
  // @ts-nocheck
2
2
  // @TODO remove no-check and fix vnode type errors
3
3
 
4
- import { VNode, vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic } from 'vue'
5
- import { Text } from '@vue/runtime-core'
4
+ import { VNode, vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic, Text } from 'vue'
6
5
  import { pascal } from 'case'
7
6
  import { createAutoBuildingObject, indent } from './util'
8
7
  import { serializeJs } from './serialize-js'
@@ -1,6 +1,6 @@
1
1
  <script lang="ts" setup>
2
2
  import { computed } from 'vue'
3
- import { histoireConfig, customLogos } from '../util/config.js'
3
+ import { histoireConfig, customLogos } from '../util/config'
4
4
  import HistoireLogo from '../assets/histoire.svg'
5
5
 
6
6
  const logoUrl = computed(() => histoireConfig.theme?.logo?.square ? customLogos.square : HistoireLogo)
@@ -1,7 +1,7 @@
1
1
  <script lang="ts" setup>
2
2
  import { isDark } from '../../util/dark'
3
3
  import { computed } from 'vue'
4
- import { histoireConfig, customLogos } from '../../util/config.js'
4
+ import { histoireConfig, customLogos } from '../../util/config'
5
5
 
6
6
  import HistoireLogoDark from '../../assets/histoire-text-dark.svg'
7
7
  import HistoireLogoLight from '../../assets/histoire-text.svg'
@@ -1,6 +1,5 @@
1
1
  <script lang="ts" setup>
2
2
  import { Icon } from '@iconify/vue'
3
- import { defineEmits } from '@vue/runtime-core'
4
3
 
5
4
  const props = defineProps<{
6
5
  title: string
@@ -70,7 +70,7 @@ const ChildWrapper = {
70
70
  */
71
71
  const ChildrenRender = (props, { slots }) => {
72
72
  const [fragment] = slots.default()
73
- return fragment.children.map((vnode, index) => h(ChildWrapper, { index }, [vnode]))
73
+ return fragment.children.map((vnode, index) => h(ChildWrapper, { index }, () => [vnode]))
74
74
  }
75
75
 
76
76
  /**
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
2
  import { computed, defineComponent, provide, useAttrs, VNode, h, PropType, getCurrentInstance } from 'vue'
3
- import { Story } from '../../types.js'
3
+ import type { Story } from '../../types.js'
4
4
  import Variant from './Variant.vue'
5
5
 
6
6
  export default defineComponent({
@@ -3,7 +3,7 @@ import { PropType, ref, toRefs } from 'vue'
3
3
  import { useRouter } from 'vue-router'
4
4
  import { Icon } from '@iconify/vue'
5
5
  import { SearchResult } from '../../types'
6
- import { onKeyboardShortcut } from '../../util/keyboard.js'
6
+ import { onKeyboardShortcut } from '../../util/keyboard'
7
7
  import BaseListItemLink from '../base/BaseListItemLink.vue'
8
8
  import { useScrollOnActive } from '../../util/scroll'
9
9
 
@@ -1,6 +1,6 @@
1
1
  <script lang="ts" setup>
2
2
  import { useFocus, useDebounce } from '@vueuse/core'
3
- import { ref, watch } from 'vue'
3
+ import { computed, ref, watch } from 'vue'
4
4
  import { Icon } from '@iconify/vue'
5
5
  import flexsearch from 'flexsearch'
6
6
  import charset from 'flexsearch/dist/module/lang/latin/advanced.js'
@@ -9,7 +9,10 @@ import { useStoryStore } from '../../stores/story'
9
9
  import BaseEmpty from '../base/BaseEmpty.vue'
10
10
  import type { SearchResult, Story, Variant } from '../../types'
11
11
  import SearchItem from './SearchItem.vue'
12
- import { searchData, onUpdate, SearchData } from './search-data.js'
12
+ import { searchData, onUpdate } from './search-title-data'
13
+ import type { SearchData } from './types'
14
+
15
+ const DocSearchData = () => import('./search-docs-data')
13
16
 
14
17
  const props = defineProps({
15
18
  shown: {
@@ -42,37 +45,40 @@ watch(() => props.shown, value => {
42
45
  }
43
46
  })
44
47
 
45
- // Search
48
+ // Index
46
49
 
47
50
  const searchInputText = ref('')
48
51
  const rateLimitedSearch = useDebounce(searchInputText, 50)
49
52
 
50
53
  const storyStore = useStoryStore()
51
54
 
52
- let searchIndex: flexsearch.Document<any, any>
53
- let idMap: SearchData['idMap']
55
+ let titleSearchIndex: flexsearch.Document<any, any>
56
+ let titleIdMap: SearchData['idMap']
54
57
 
55
- async function loadSearchIndex (data: SearchData) {
56
- searchIndex = new flexsearch.Document({
58
+ function createIndex () {
59
+ return new flexsearch.Document({
57
60
  preset: 'match',
58
61
  document: {
59
62
  id: 'id',
60
63
  index: [
61
- 'title',
62
- 'docs',
64
+ 'text',
63
65
  ],
64
66
  },
65
67
  worker: true,
66
68
  charset,
67
69
  language,
68
- tokenize: 'full',
70
+ tokenize: 'forward',
69
71
  })
72
+ }
73
+
74
+ async function loadSearchIndex (data: SearchData) {
75
+ titleSearchIndex = createIndex()
70
76
 
71
77
  for (const key of Object.keys(data.index)) {
72
- await searchIndex.import(key, data.index[key])
78
+ await titleSearchIndex.import(key, data.index[key])
73
79
  }
74
80
 
75
- idMap = data.idMap
81
+ titleIdMap = data.idMap
76
82
  }
77
83
 
78
84
  loadSearchIndex(searchData)
@@ -81,17 +87,46 @@ onUpdate(searchData => {
81
87
  loadSearchIndex(searchData)
82
88
  })
83
89
 
84
- const results = ref<SearchResult[]>([])
90
+ let docSearchIndex: flexsearch.Document<any, any>
91
+ let docIdMap: SearchData['idMap']
92
+
93
+ async function loadDocSearchIndex () {
94
+ async function load (data: SearchData) {
95
+ docSearchIndex = createIndex()
96
+
97
+ for (const key of Object.keys(data.index)) {
98
+ await docSearchIndex.import(key, data.index[key])
99
+ }
100
+
101
+ docIdMap = data.idMap
102
+
103
+ if (rateLimitedSearch.value) {
104
+ searchOnDocField(rateLimitedSearch.value)
105
+ }
106
+ }
107
+
108
+ const searchDataModule = await DocSearchData()
109
+
110
+ load(searchDataModule.searchData)
111
+ // Handle HMR
112
+ searchDataModule.onUpdate(searchData => {
113
+ load(searchData)
114
+ })
115
+ }
116
+
117
+ loadDocSearchIndex()
118
+
119
+ // Search
120
+
121
+ const titleResults = ref<SearchResult[]>([])
85
122
 
86
123
  watch(rateLimitedSearch, async value => {
87
124
  const list: SearchResult[] = []
88
- const raw = await searchIndex.search(value, {
89
- enrich: true,
90
- })
125
+ const raw = await titleSearchIndex.search(value)
91
126
  let rank = 0
92
127
  for (const field of raw) {
93
128
  for (const id of field.result) {
94
- const idMapData = idMap[id]
129
+ const idMapData = titleIdMap[id]
95
130
  if (!idMapData) continue
96
131
  switch (idMapData.kind) {
97
132
  case 'story': {
@@ -110,14 +145,40 @@ watch(rateLimitedSearch, async value => {
110
145
  }
111
146
  }
112
147
  }
113
- results.value = list
148
+ titleResults.value = list
114
149
  })
115
150
 
151
+ const docsResults = ref<SearchResult[]>([])
152
+
153
+ async function searchOnDocField (query: string) {
154
+ if (docSearchIndex) {
155
+ const list: SearchResult[] = []
156
+ const raw = await docSearchIndex.search(query)
157
+ let rank = 0
158
+ for (const field of raw) {
159
+ for (const id of field.result) {
160
+ const idMapData = docIdMap[id]
161
+ if (!idMapData) continue
162
+ switch (idMapData.kind) {
163
+ case 'story': {
164
+ list.push(storyResultFactory(storyStore.getStoryById(idMapData.id), rank))
165
+ rank++
166
+ break
167
+ }
168
+ }
169
+ }
170
+ }
171
+ docsResults.value = list
172
+ }
173
+ }
174
+
175
+ watch(rateLimitedSearch, searchOnDocField)
176
+
116
177
  function storyResultFactory (story: Story, rank: number): SearchResult {
117
178
  return {
118
179
  kind: 'story',
119
180
  rank,
120
- id: story.id,
181
+ id: `story:${story.id}`,
121
182
  title: story.title,
122
183
  route: {
123
184
  name: 'story',
@@ -135,7 +196,7 @@ function variantResultFactory (story: Story, variant: Variant, rank: number): Se
135
196
  return {
136
197
  kind: 'variant',
137
198
  rank,
138
- id: `${story.id}:${variant.id}`,
199
+ id: `variant:${story.id}:${variant.id}`,
139
200
  title: variant.title,
140
201
  route: {
141
202
  name: 'story',
@@ -152,6 +213,20 @@ function variantResultFactory (story: Story, variant: Variant, rank: number): Se
152
213
  }
153
214
  }
154
215
 
216
+ const results = computed(() => {
217
+ const list = [...titleResults.value]
218
+ const seen = {}
219
+ for (const r of titleResults.value) {
220
+ seen[r.id] = true
221
+ }
222
+ for (const r of docsResults.value) {
223
+ if (!seen[r.id]) {
224
+ list.push(r)
225
+ }
226
+ }
227
+ return list
228
+ })
229
+
155
230
  // Selection
156
231
 
157
232
  const selectedIndex = ref(0)
@@ -209,7 +284,7 @@ function selectPrevious () {
209
284
  >
210
285
  <SearchItem
211
286
  v-for="(result, index) of results"
212
- :key="`${result.kind}-${result.id}`"
287
+ :key="result.id"
213
288
  :result="result"
214
289
  :selected="index === selectedIndex"
215
290
  @close="close()"
@@ -1,11 +1,7 @@
1
1
  import { markRaw } from 'vue'
2
2
  // @ts-expect-error virtual module
3
- import { searchData as _searchData, onUpdate as _onUpdate } from '$histoire-search-data'
4
-
5
- export interface SearchData {
6
- index: Record<string, any>
7
- idMap: Record<number, { id: string, kind: string }>
8
- }
3
+ import { searchData as _searchData, onUpdate as _onUpdate } from '$histoire-search-docs-data'
4
+ import type { SearchData } from './types.js'
9
5
 
10
6
  export const searchData: SearchData = markRaw(_searchData)
11
7
 
@@ -0,0 +1,10 @@
1
+ import { markRaw } from 'vue'
2
+ // @ts-expect-error virtual module
3
+ import { searchData as _searchData, onUpdate as _onUpdate } from '$histoire-search-title-data'
4
+ import type { SearchData } from './types.js'
5
+
6
+ export const searchData: SearchData = markRaw(_searchData)
7
+
8
+ export function onUpdate (cb: (data: SearchData) => unknown) {
9
+ _onUpdate(cb)
10
+ }
@@ -0,0 +1,4 @@
1
+ export interface SearchData {
2
+ index: Record<string, any>
3
+ idMap: Record<number, { id: string, kind: string }>
4
+ }
@@ -6,7 +6,7 @@ import { useResizeObserver } from '@vueuse/core'
6
6
  import { useCurrentVariantRoute } from '../../util/variant'
7
7
  import type { Story, Variant } from '../../types'
8
8
  import SandboxVue3 from '../sandbox/SandboxVue3.vue'
9
- import { useScrollOnActive } from '../../util/scroll.js'
9
+ import { useScrollOnActive } from '../../util/scroll'
10
10
 
11
11
  const props = defineProps({
12
12
  variant: {