histoire 0.0.0 → 0.0.3

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 (204) hide show
  1. package/LICENSE +21 -0
  2. package/client.d.ts +2 -0
  3. package/dist/client/app/App.vue +84 -21
  4. package/dist/client/app/assets/histoire-text-dark.svg +89 -0
  5. package/dist/client/app/assets/histoire-text.svg +89 -0
  6. package/dist/client/app/assets/histoire.svg +51 -0
  7. package/dist/client/app/codegen/serialize-js.d.ts +1 -0
  8. package/dist/client/app/codegen/serialize-js.js +113 -0
  9. package/dist/client/app/codegen/serialize-js.ts +119 -0
  10. package/dist/client/app/codegen/util.d.ts +10 -0
  11. package/dist/client/app/codegen/util.js +70 -0
  12. package/dist/client/app/codegen/util.ts +79 -0
  13. package/dist/client/app/codegen/vue3.d.ts +2 -0
  14. package/dist/client/app/codegen/vue3.js +238 -0
  15. package/dist/client/app/codegen/vue3.ts +240 -0
  16. package/dist/client/app/components/HomeView.vue +15 -1
  17. package/dist/client/app/components/StoryControls.vue +7 -2
  18. package/dist/client/app/components/StoryList.vue +20 -13
  19. package/dist/client/app/components/StoryListFolder.vue +87 -0
  20. package/dist/client/app/components/StoryListItem.vue +38 -12
  21. package/dist/client/app/components/StorySourceCode.vue +121 -0
  22. package/dist/client/app/components/StoryVariantGridItem.vue +32 -17
  23. package/dist/client/app/components/StoryVariantListItem.vue +46 -0
  24. package/dist/client/app/components/StoryVariantSingleView.vue +145 -15
  25. package/dist/client/app/components/StoryView.vue +88 -54
  26. package/dist/client/app/components/app/AppHeader.vue +56 -0
  27. package/dist/client/app/components/app/AppLogo.vue +24 -0
  28. package/dist/client/app/components/base/BaseEmpty.vue +1 -1
  29. package/dist/client/app/components/base/BaseListItemLink.vue +55 -0
  30. package/dist/client/app/components/base/BaseSplitPane.vue +57 -21
  31. package/dist/client/app/components/exposed/Story.vue +23 -58
  32. package/dist/client/app/components/exposed/Variant.vue +24 -47
  33. package/dist/client/app/components/misc/HatchedPattern.vue +25 -0
  34. package/dist/client/app/components/{SandboxVue.vue → sandbox/SandboxVue3.vue} +31 -12
  35. package/dist/client/app/components/search/SearchItem.vue +110 -0
  36. package/dist/client/app/components/search/SearchModal.vue +179 -0
  37. package/dist/client/app/composable/variant.js +0 -1
  38. package/dist/client/app/composable/variant.ts +20 -0
  39. package/dist/client/app/global-components.js +0 -1
  40. package/dist/client/app/global-components.ts +10 -0
  41. package/dist/client/app/index.d.ts +1 -0
  42. package/dist/client/app/index.js +10 -2
  43. package/dist/client/app/index.ts +24 -0
  44. package/dist/client/app/router.js +0 -1
  45. package/dist/client/app/router.ts +17 -0
  46. package/dist/client/app/sandbox.d.ts +1 -0
  47. package/dist/client/app/sandbox.js +53 -0
  48. package/dist/client/app/sandbox.ts +60 -0
  49. package/dist/client/app/stores/story.d.ts +167 -50
  50. package/dist/client/app/stores/story.js +33 -16
  51. package/dist/client/app/stores/story.ts +58 -0
  52. package/dist/client/app/style/floating-vue.css +31 -0
  53. package/dist/client/app/style/main.css +17 -2
  54. package/dist/client/app/style/sandbox.css +14 -0
  55. package/dist/client/app/types.d.ts +36 -4
  56. package/dist/client/app/types.js +0 -1
  57. package/dist/client/app/types.ts +59 -0
  58. package/dist/client/app/util/config.d.ts +3 -0
  59. package/dist/client/app/util/config.js +4 -0
  60. package/dist/client/app/util/config.ts +6 -0
  61. package/dist/client/app/util/const.d.ts +1 -0
  62. package/dist/client/app/util/const.js +1 -0
  63. package/dist/client/app/util/const.ts +1 -0
  64. package/dist/client/app/util/dark.d.ts +2 -0
  65. package/dist/client/app/util/dark.js +3 -0
  66. package/dist/client/app/util/dark.ts +4 -0
  67. package/dist/client/app/util/env.d.ts +1 -0
  68. package/dist/client/app/util/env.js +1 -0
  69. package/dist/client/app/util/env.ts +1 -0
  70. package/dist/client/app/util/keyboard.d.ts +8 -0
  71. package/dist/client/app/util/keyboard.js +82 -0
  72. package/dist/client/app/util/keyboard.ts +99 -0
  73. package/dist/client/app/util/mapping.d.ts +3 -0
  74. package/dist/client/app/util/mapping.js +39 -0
  75. package/dist/client/app/util/mapping.ts +41 -0
  76. package/dist/client/app/util/tooltip.d.ts +6 -0
  77. package/dist/client/app/util/tooltip.js +11 -0
  78. package/dist/client/app/util/tooltip.ts +13 -0
  79. package/dist/client/index.d.ts +8 -0
  80. package/dist/client/index.js +3 -0
  81. package/dist/client/index.ts +12 -0
  82. package/dist/client/server/Story.server.vue +65 -0
  83. package/dist/client/server/Variant.server.vue +50 -0
  84. package/dist/client/server/index.d.ts +2 -0
  85. package/dist/client/server/index.js +25 -0
  86. package/dist/client/server/index.ts +29 -0
  87. package/dist/client/shim.d.ts +5 -0
  88. package/dist/node/alias.js +0 -1
  89. package/dist/node/bin.js +8 -3
  90. package/dist/node/build.d.ts +2 -0
  91. package/dist/node/build.js +71 -0
  92. package/dist/node/collect.d.ts +12 -0
  93. package/dist/node/collect.js +65 -0
  94. package/dist/node/colors.d.ts +286 -0
  95. package/dist/node/colors.js +324 -0
  96. package/dist/node/commands/build.d.ts +1 -0
  97. package/dist/node/commands/build.js +10 -0
  98. package/dist/node/commands/dev.d.ts +1 -1
  99. package/dist/node/commands/dev.js +45 -13
  100. package/dist/node/config.d.ts +112 -1
  101. package/dist/node/config.js +124 -2
  102. package/dist/node/context.d.ts +13 -0
  103. package/dist/node/context.js +12 -2
  104. package/dist/node/dom/dom-keys.d.ts +4 -0
  105. package/dist/node/dom/dom-keys.js +223 -0
  106. package/dist/node/dom/env.d.ts +5 -0
  107. package/dist/node/dom/env.js +22 -0
  108. package/dist/node/index.d.ts +2 -0
  109. package/dist/node/index.js +2 -2
  110. package/dist/node/plugin.d.ts +8 -2
  111. package/dist/node/plugin.js +150 -22
  112. package/dist/node/server.d.ts +4 -1
  113. package/dist/node/server.js +80 -21
  114. package/dist/node/stories.d.ts +8 -9
  115. package/dist/node/stories.js +42 -23
  116. package/dist/node/tree.d.ts +19 -0
  117. package/dist/node/tree.js +54 -0
  118. package/dist/node/types.d.ts +38 -0
  119. package/dist/node/types.js +1 -0
  120. package/dist/style.css +610 -53
  121. package/package.json +45 -25
  122. package/scripts/copy.mjs +11 -0
  123. package/src/client/app/App.vue +84 -21
  124. package/src/client/app/assets/histoire-text-dark.svg +89 -0
  125. package/src/client/app/assets/histoire-text.svg +89 -0
  126. package/src/client/app/assets/histoire.svg +51 -0
  127. package/src/client/app/codegen/serialize-js.ts +119 -0
  128. package/src/client/app/codegen/util.ts +79 -0
  129. package/src/client/app/codegen/vue3.ts +240 -0
  130. package/src/client/app/components/HomeView.vue +15 -1
  131. package/src/client/app/components/StoryControls.vue +7 -2
  132. package/src/client/app/components/StoryList.vue +20 -13
  133. package/src/client/app/components/StoryListFolder.vue +87 -0
  134. package/src/client/app/components/StoryListItem.vue +38 -12
  135. package/src/client/app/components/StorySourceCode.vue +121 -0
  136. package/src/client/app/components/StoryVariantGridItem.vue +32 -17
  137. package/src/client/app/components/StoryVariantListItem.vue +46 -0
  138. package/src/client/app/components/StoryVariantSingleView.vue +145 -15
  139. package/src/client/app/components/StoryView.vue +88 -54
  140. package/src/client/app/components/app/AppHeader.vue +56 -0
  141. package/src/client/app/components/app/AppLogo.vue +24 -0
  142. package/src/client/app/components/base/BaseEmpty.vue +1 -1
  143. package/src/client/app/components/base/BaseListItemLink.vue +55 -0
  144. package/src/client/app/components/base/BaseSplitPane.vue +57 -21
  145. package/src/client/app/components/exposed/Story.vue +23 -58
  146. package/src/client/app/components/exposed/Variant.vue +24 -47
  147. package/src/client/app/components/misc/HatchedPattern.vue +25 -0
  148. package/src/client/app/components/{SandboxVue.vue → sandbox/SandboxVue3.vue} +31 -12
  149. package/src/client/app/components/search/SearchItem.vue +110 -0
  150. package/src/client/app/components/search/SearchModal.vue +179 -0
  151. package/src/client/app/index.ts +10 -1
  152. package/src/client/app/sandbox.ts +60 -0
  153. package/src/client/app/stores/story.ts +39 -15
  154. package/src/client/app/style/floating-vue.css +31 -0
  155. package/src/client/app/style/main.css +17 -2
  156. package/src/client/app/style/sandbox.css +14 -0
  157. package/src/client/app/types.ts +42 -4
  158. package/src/client/app/util/config.ts +6 -0
  159. package/src/client/app/util/const.ts +1 -0
  160. package/src/client/app/util/dark.ts +4 -0
  161. package/src/client/app/util/env.ts +1 -0
  162. package/src/client/app/util/keyboard.ts +99 -0
  163. package/src/client/app/util/mapping.ts +41 -0
  164. package/src/client/app/util/tooltip.ts +13 -0
  165. package/src/client/index.ts +12 -0
  166. package/src/client/server/Story.server.vue +65 -0
  167. package/src/client/server/Variant.server.vue +50 -0
  168. package/src/client/server/index.ts +29 -0
  169. package/src/node/bin.ts +9 -2
  170. package/src/node/build.ts +80 -0
  171. package/src/node/collect.ts +78 -0
  172. package/src/node/colors.ts +340 -0
  173. package/src/node/commands/build.ts +12 -0
  174. package/src/node/commands/dev.ts +49 -12
  175. package/src/node/config.ts +240 -1
  176. package/src/node/context.ts +21 -1
  177. package/src/node/dom/dom-keys.ts +227 -0
  178. package/src/node/dom/env.ts +26 -0
  179. package/src/node/index.ts +2 -0
  180. package/src/node/plugin.ts +155 -22
  181. package/src/node/server.ts +95 -20
  182. package/src/node/stories.ts +53 -31
  183. package/src/node/tree.spec.ts +28 -0
  184. package/src/node/tree.ts +88 -0
  185. package/src/node/types.ts +40 -0
  186. package/tailwind.config.cjs +41 -1
  187. package/tsconfig.json +2 -1
  188. package/dist/client/app/components/StoryVariantItem.vue +0 -29
  189. package/dist/client/app/composable/variant.js.map +0 -1
  190. package/dist/client/app/global-components.js.map +0 -1
  191. package/dist/client/app/index.js.map +0 -1
  192. package/dist/client/app/router.js.map +0 -1
  193. package/dist/client/app/stores/story.js.map +0 -1
  194. package/dist/client/app/types.js.map +0 -1
  195. package/dist/node/alias.js.map +0 -1
  196. package/dist/node/bin.js.map +0 -1
  197. package/dist/node/commands/dev.js.map +0 -1
  198. package/dist/node/config.js.map +0 -1
  199. package/dist/node/context.js.map +0 -1
  200. package/dist/node/index.js.map +0 -1
  201. package/dist/node/plugin.js.map +0 -1
  202. package/dist/node/server.js.map +0 -1
  203. package/dist/node/stories.js.map +0 -1
  204. package/src/client/app/components/StoryVariantItem.vue +0 -29
@@ -0,0 +1,70 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+ export function indent(lines, count = 1) {
3
+ return lines.map(line => `${' '.repeat(count)}${line}`);
4
+ }
5
+ export function unindent(code) {
6
+ const lines = code.split('\n');
7
+ let indentLevel = -1;
8
+ let indentText;
9
+ const linesToAnalyze = lines.filter(line => line.trim().length > 0);
10
+ for (const line of linesToAnalyze) {
11
+ const match = /^\s*/.exec(line);
12
+ if (match && (indentLevel === -1 || indentLevel > match[0].length)) {
13
+ indentLevel = match[0].length;
14
+ indentText = match[0];
15
+ }
16
+ }
17
+ const result = [];
18
+ for (const line of lines) {
19
+ result.push(line.replace(indentText, ''));
20
+ }
21
+ return result.join('\n').trim();
22
+ }
23
+ export function createAutoBuildingObject(format, specialKeysHandler, key = '', depth = 0) {
24
+ const cache = {};
25
+ if (depth > 32)
26
+ return { key, cache, target: {}, proxy: () => key };
27
+ const target = () => {
28
+ const k = key + '()';
29
+ return format ? format(k) : k;
30
+ };
31
+ const proxy = new Proxy(target, {
32
+ get(_, p) {
33
+ if (p === '__autoBuildingObject') {
34
+ return true;
35
+ }
36
+ if (p === '__autoBuildingObjectGetKey') {
37
+ return key;
38
+ }
39
+ if (specialKeysHandler) {
40
+ const fn = specialKeysHandler(target, p);
41
+ if (fn) {
42
+ return fn();
43
+ }
44
+ }
45
+ if (p === 'toString') {
46
+ const k = key + '.toString()';
47
+ return () => format ? format(k) : k;
48
+ }
49
+ if (p === Symbol.toPrimitive) {
50
+ return () => format ? format(key) : key;
51
+ }
52
+ if (!cache[p]) {
53
+ const childKey = key ? `${key}.${p.toString()}` : p.toString();
54
+ const child = createAutoBuildingObject(format, specialKeysHandler, childKey, depth + 1);
55
+ cache[p] = { key: childKey, ...child };
56
+ }
57
+ return cache[p].proxy;
58
+ },
59
+ apply(_, thisArg, args) {
60
+ const k = `${key}(${args.join(', ')})`;
61
+ return format ? format(k) : k;
62
+ },
63
+ });
64
+ return {
65
+ key,
66
+ cache,
67
+ target,
68
+ proxy,
69
+ };
70
+ }
@@ -0,0 +1,79 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+
3
+ export function indent (lines: string[], count = 1) {
4
+ return lines.map(line => `${' '.repeat(count)}${line}`)
5
+ }
6
+
7
+ export function unindent (code: string) {
8
+ const lines = code.split('\n')
9
+ let indentLevel = -1
10
+ let indentText: string
11
+ const linesToAnalyze = lines.filter(line => line.trim().length > 0)
12
+ for (const line of linesToAnalyze) {
13
+ const match = /^\s*/.exec(line)
14
+ if (match && (indentLevel === -1 || indentLevel > match[0].length)) {
15
+ indentLevel = match[0].length
16
+ indentText = match[0]
17
+ }
18
+ }
19
+ const result: string[] = []
20
+ for (const line of lines) {
21
+ result.push(line.replace(indentText, ''))
22
+ }
23
+ return result.join('\n').trim()
24
+ }
25
+
26
+ interface AutoBuildingOject {
27
+ key: string
28
+ cache: Record<string | symbol, AutoBuildingOject>
29
+ target: any
30
+ proxy: any
31
+ }
32
+
33
+ export function createAutoBuildingObject (format?: (key: string) => string, specialKeysHandler?: (target: any, p: string | symbol) => (() => unknown) | null, key = '', depth = 0): AutoBuildingOject {
34
+ const cache: Record<string | symbol, AutoBuildingOject> = {}
35
+ if (depth > 32) return { key, cache, target: {}, proxy: () => key }
36
+ const target: any = () => {
37
+ const k = key + '()'
38
+ return format ? format(k) : k
39
+ }
40
+ const proxy = new Proxy(target, {
41
+ get (_, p) {
42
+ if (p === '__autoBuildingObject') {
43
+ return true
44
+ }
45
+ if (p === '__autoBuildingObjectGetKey') {
46
+ return key
47
+ }
48
+ if (specialKeysHandler) {
49
+ const fn = specialKeysHandler(target, p)
50
+ if (fn) {
51
+ return fn()
52
+ }
53
+ }
54
+ if (p === 'toString') {
55
+ const k = key + '.toString()'
56
+ return () => format ? format(k) : k
57
+ }
58
+ if (p === Symbol.toPrimitive) {
59
+ return () => format ? format(key) : key
60
+ }
61
+ if (!cache[p]) {
62
+ const childKey = key ? `${key}.${p.toString()}` : p.toString()
63
+ const child = createAutoBuildingObject(format, specialKeysHandler, childKey, depth + 1)
64
+ cache[p] = { key: childKey, ...child }
65
+ }
66
+ return cache[p].proxy
67
+ },
68
+ apply (_, thisArg, args) {
69
+ const k = `${key}(${args.join(', ')})`
70
+ return format ? format(k) : k
71
+ },
72
+ })
73
+ return {
74
+ key,
75
+ cache,
76
+ target,
77
+ proxy,
78
+ }
79
+ }
@@ -0,0 +1,2 @@
1
+ import type { Variant } from '../types';
2
+ export declare function generateSourceCode(variant: Variant): Promise<string>;
@@ -0,0 +1,238 @@
1
+ // @ts-nocheck
2
+ // @TODO remove no-check and fix vnode type errors
3
+ import { vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic } from 'vue';
4
+ import { Text } from '@vue/runtime-core';
5
+ import { pascal, kebab } from 'case';
6
+ import { createAutoBuildingObject, indent } from './util';
7
+ import { serializeJs } from './serialize-js';
8
+ export async function generateSourceCode(variant) {
9
+ const vnode = variant.slots().default?.({ state: variant.state }) ?? [];
10
+ const list = Array.isArray(vnode) ? vnode : [vnode];
11
+ const lines = [];
12
+ for (const vnode of list) {
13
+ lines.push(...await printVNode(vnode));
14
+ }
15
+ return lines.join('\n');
16
+ }
17
+ async function printVNode(vnode) {
18
+ if (vnode.type === Text) {
19
+ return [vnode.children.trim()];
20
+ }
21
+ const lines = [];
22
+ if (typeof vnode.type === 'object' || typeof vnode.type === 'string') {
23
+ // Wait for async component
24
+ if (vnode.type?.__asyncLoader && !vnode.type.__asyncResolved) {
25
+ await vnode.type.__asyncLoader();
26
+ }
27
+ const attrs = [];
28
+ let multilineAttrs = false;
29
+ const skipProps = [];
30
+ // Directives
31
+ function genDirective(dirName, dir, valueCode = null) {
32
+ let modifiers = '';
33
+ for (const key in dir.modifiers) {
34
+ if (dir.modifiers[key]) {
35
+ modifiers += `.${key}`;
36
+ }
37
+ }
38
+ let arg = '';
39
+ if (dir.arg) {
40
+ arg = `:${dir.arg}`;
41
+ }
42
+ const valueLines = valueCode ? [valueCode] : serializeAndCleanJs(dir.value);
43
+ const attr = [];
44
+ const dirAttr = `v-${dirName}${modifiers}${arg}="`;
45
+ if (valueLines.length > 1) {
46
+ attr.push(`${dirAttr}${valueLines[0]}`);
47
+ attr.push(...valueLines.slice(1, valueLines.length - 1));
48
+ attr.push(`${valueLines[valueLines.length - 1]}"`);
49
+ multilineAttrs = true;
50
+ }
51
+ else {
52
+ attr.push(`${dirAttr}${valueLines[0] ?? ''}"`);
53
+ }
54
+ attrs.push(attr);
55
+ }
56
+ if (vnode.dirs) {
57
+ for (const dir of vnode.dirs) {
58
+ // Vmodel
59
+ if (dir.dir === vModelText || dir.dir === vModelSelect || dir.dir === vModelRadio || dir.dir === vModelCheckbox || dir.dir === vModelDynamic) {
60
+ const listenerKey = `onUpdate:${dir.arg ?? 'modelValue'}`;
61
+ const listener = vnode.props[listenerKey];
62
+ let valueCode = null;
63
+ if (listener) {
64
+ skipProps.push(listenerKey);
65
+ const listenerSource = listener.toString();
66
+ const result = /\(\$event\) => (.*?) = \$event/.exec(listenerSource);
67
+ if (result) {
68
+ valueCode = result[1];
69
+ }
70
+ }
71
+ genDirective('model', dir, valueCode);
72
+ }
73
+ else {
74
+ let dirName;
75
+ for (const directives of [dir.instance._.directives, dir.instance._.appContext.directives]) {
76
+ for (const key in directives) {
77
+ if (dir.instance._.directives[key] === dir.dir) {
78
+ dirName = key;
79
+ break;
80
+ }
81
+ }
82
+ if (dirName)
83
+ break;
84
+ }
85
+ if (dirName) {
86
+ genDirective(dirName, dir);
87
+ }
88
+ }
89
+ }
90
+ }
91
+ // Attributes
92
+ for (const prop in vnode.props) {
93
+ if (skipProps.includes(prop)) {
94
+ continue;
95
+ }
96
+ const value = vnode.props[prop];
97
+ if (typeof value !== 'string' || vnode.dynamicProps?.includes(prop)) {
98
+ let directive = ':';
99
+ if (prop.startsWith('on')) {
100
+ directive = '@';
101
+ }
102
+ const arg = kebab(directive === '@' ? prop.slice(2) : prop);
103
+ let serialized;
104
+ if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
105
+ // It was formatted from auto building object (slot props)
106
+ serialized = [value.substring(2, value.length - 2).trim()];
107
+ }
108
+ else {
109
+ serialized = serializeAndCleanJs(value);
110
+ }
111
+ if (serialized.length > 1) {
112
+ multilineAttrs = true;
113
+ const indented = [`${directive}${arg}="${serialized[0]}`];
114
+ indented.push(...serialized.slice(1, serialized.length - 1));
115
+ indented.push(`${serialized[serialized.length - 1]}"`);
116
+ attrs.push(indented);
117
+ }
118
+ else {
119
+ attrs.push([`${directive}${arg}="${serialized[0]}"`]);
120
+ }
121
+ }
122
+ else {
123
+ attrs.push([`${kebab(prop)}="${value}"`]);
124
+ }
125
+ }
126
+ if (attrs.length > 1) {
127
+ multilineAttrs = true;
128
+ }
129
+ // Tags
130
+ const tagName = getTagName(vnode);
131
+ // Children
132
+ const childLines = [];
133
+ if (typeof vnode.children === 'string') {
134
+ childLines.push(vnode.children);
135
+ }
136
+ else if (Array.isArray(vnode.children)) {
137
+ for (const child of vnode.children) {
138
+ childLines.push(...await printVNode(child));
139
+ }
140
+ }
141
+ // Slots
142
+ if (vnode.children && typeof vnode.children === 'object' && !Array.isArray(vnode.children)) {
143
+ for (const key in vnode.children) {
144
+ if (typeof vnode.children[key] === 'function') {
145
+ const autoObject = createAutoBuildingObject(key => `{{ ${key} }}`, (target, p) => {
146
+ // Vue 3
147
+ if (p === '__v_isRef') {
148
+ return () => false;
149
+ }
150
+ });
151
+ const children = vnode.children[key](autoObject.proxy);
152
+ const slotLines = [];
153
+ for (const child of children) {
154
+ slotLines.push(...await printVNode(child));
155
+ }
156
+ const slotProps = Object.keys(autoObject.cache);
157
+ if (slotProps.length) {
158
+ childLines.push(`<template #${key}="{ ${slotProps.join(', ')} }">`);
159
+ childLines.push(...indent(slotLines));
160
+ childLines.push('</template>');
161
+ }
162
+ else if (key === 'default') {
163
+ childLines.push(...slotLines);
164
+ }
165
+ else {
166
+ childLines.push(`<template #${key}>`);
167
+ childLines.push(...indent(slotLines));
168
+ childLines.push(`</template>`);
169
+ }
170
+ }
171
+ }
172
+ }
173
+ // Template
174
+ const tag = [`<${tagName}`];
175
+ if (multilineAttrs) {
176
+ for (const attrLines of attrs) {
177
+ tag.push(...indent(attrLines));
178
+ }
179
+ if (childLines.length > 0) {
180
+ tag.push('>');
181
+ }
182
+ }
183
+ else {
184
+ if (attrs.length === 1) {
185
+ tag[0] += ` ${attrs[0]}`;
186
+ }
187
+ if (childLines.length > 0) {
188
+ tag[0] += '>';
189
+ }
190
+ }
191
+ if (childLines.length > 0) {
192
+ lines.push(...tag);
193
+ lines.push(...indent(childLines));
194
+ lines.push(`</${tagName}>`);
195
+ }
196
+ else if (tag.length > 1) {
197
+ lines.push(...tag);
198
+ lines.push('/>');
199
+ }
200
+ else {
201
+ lines.push(`${tag[0]} />`);
202
+ }
203
+ }
204
+ return lines;
205
+ }
206
+ function getTagName(vnode) {
207
+ if (typeof vnode.type === 'string') {
208
+ return vnode.type;
209
+ }
210
+ else if (vnode.type?.__asyncResolved) {
211
+ const asyncComp = vnode.type?.__asyncResolved;
212
+ return asyncComp.name ?? getNameFromFile(asyncComp.__file);
213
+ }
214
+ else if (vnode.type?.name) {
215
+ return vnode.type.name;
216
+ }
217
+ else if (vnode.type?.__file) {
218
+ return getNameFromFile(vnode.type.__file);
219
+ }
220
+ return 'Anonymous';
221
+ }
222
+ function getNameFromFile(file) {
223
+ const parts = /\/([^/]+)\.vue$/.exec(file);
224
+ if (parts) {
225
+ return pascal(parts[1]);
226
+ }
227
+ return 'Anonymous';
228
+ }
229
+ function serializeAndCleanJs(value) {
230
+ const isAutoBuildingObject = !!value?.__autoBuildingObject;
231
+ const result = serializeJs(value);
232
+ if (isAutoBuildingObject) {
233
+ return [result.__autoBuildingObjectGetKey];
234
+ }
235
+ else {
236
+ return result.replace(/\$setup\./g, '').split('\n');
237
+ }
238
+ }
@@ -0,0 +1,240 @@
1
+ // @ts-nocheck
2
+ // @TODO remove no-check and fix vnode type errors
3
+
4
+ import { VNode, vModelText, vModelCheckbox, vModelSelect, vModelRadio, vModelDynamic } from 'vue'
5
+ import { Text } from '@vue/runtime-core'
6
+ import { pascal, kebab } from 'case'
7
+ import { createAutoBuildingObject, indent } from './util'
8
+ import { serializeJs } from './serialize-js'
9
+ import type { Variant } from '../types'
10
+
11
+ export async function generateSourceCode (variant: Variant) {
12
+ const vnode = variant.slots().default?.({ state: variant.state }) ?? []
13
+ const list = Array.isArray(vnode) ? vnode : [vnode]
14
+ const lines: string[] = []
15
+ for (const vnode of list) {
16
+ lines.push(...await printVNode(vnode))
17
+ }
18
+ return lines.join('\n')
19
+ }
20
+
21
+ async function printVNode (vnode: VNode): Promise<string[]> {
22
+ if (vnode.type === Text) {
23
+ return [vnode.children.trim()]
24
+ }
25
+
26
+ const lines: string[] = []
27
+
28
+ if (typeof vnode.type === 'object' || typeof vnode.type === 'string') {
29
+ // Wait for async component
30
+ if (vnode.type?.__asyncLoader && !vnode.type.__asyncResolved) {
31
+ await vnode.type.__asyncLoader()
32
+ }
33
+
34
+ const attrs: string[][] = []
35
+ let multilineAttrs = false
36
+ const skipProps: string[] = []
37
+
38
+ // Directives
39
+ function genDirective (dirName: string, dir, valueCode: string = null) {
40
+ let modifiers = ''
41
+ for (const key in dir.modifiers) {
42
+ if (dir.modifiers[key]) {
43
+ modifiers += `.${key}`
44
+ }
45
+ }
46
+ let arg = ''
47
+ if (dir.arg) {
48
+ arg = `:${dir.arg}`
49
+ }
50
+ const valueLines = valueCode ? [valueCode] : serializeAndCleanJs(dir.value)
51
+ const attr: string[] = []
52
+ const dirAttr = `v-${dirName}${modifiers}${arg}="`
53
+ if (valueLines.length > 1) {
54
+ attr.push(`${dirAttr}${valueLines[0]}`)
55
+ attr.push(...valueLines.slice(1, valueLines.length - 1))
56
+ attr.push(`${valueLines[valueLines.length - 1]}"`)
57
+ multilineAttrs = true
58
+ } else {
59
+ attr.push(`${dirAttr}${valueLines[0] ?? ''}"`)
60
+ }
61
+ attrs.push(attr)
62
+ }
63
+ if (vnode.dirs) {
64
+ for (const dir of vnode.dirs) {
65
+ // Vmodel
66
+ if (dir.dir === vModelText || dir.dir === vModelSelect || dir.dir === vModelRadio || dir.dir === vModelCheckbox || dir.dir === vModelDynamic) {
67
+ const listenerKey = `onUpdate:${dir.arg ?? 'modelValue'}`
68
+ const listener = vnode.props[listenerKey]
69
+ let valueCode: string = null
70
+ if (listener) {
71
+ skipProps.push(listenerKey)
72
+ const listenerSource = listener.toString()
73
+ const result = /\(\$event\) => (.*?) = \$event/.exec(listenerSource)
74
+ if (result) {
75
+ valueCode = result[1]
76
+ }
77
+ }
78
+ genDirective('model', dir, valueCode)
79
+ } else {
80
+ let dirName: string
81
+ for (const directives of [dir.instance._.directives, dir.instance._.appContext.directives]) {
82
+ for (const key in directives) {
83
+ if (dir.instance._.directives[key] === dir.dir) {
84
+ dirName = key
85
+ break
86
+ }
87
+ }
88
+ if (dirName) break
89
+ }
90
+ if (dirName) {
91
+ genDirective(dirName, dir)
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ // Attributes
98
+ for (const prop in vnode.props) {
99
+ if (skipProps.includes(prop)) {
100
+ continue
101
+ }
102
+ const value = vnode.props[prop]
103
+ if (typeof value !== 'string' || vnode.dynamicProps?.includes(prop)) {
104
+ let directive = ':'
105
+ if (prop.startsWith('on')) {
106
+ directive = '@'
107
+ }
108
+ const arg = kebab(directive === '@' ? prop.slice(2) : prop)
109
+ let serialized: string[]
110
+ if (typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}')) {
111
+ // It was formatted from auto building object (slot props)
112
+ serialized = [value.substring(2, value.length - 2).trim()]
113
+ } else {
114
+ serialized = serializeAndCleanJs(value)
115
+ }
116
+ if (serialized.length > 1) {
117
+ multilineAttrs = true
118
+ const indented: string[] = [`${directive}${arg}="${serialized[0]}`]
119
+ indented.push(...serialized.slice(1, serialized.length - 1))
120
+ indented.push(`${serialized[serialized.length - 1]}"`)
121
+ attrs.push(indented)
122
+ } else {
123
+ attrs.push([`${directive}${arg}="${serialized[0]}"`])
124
+ }
125
+ } else {
126
+ attrs.push([`${kebab(prop)}="${value}"`])
127
+ }
128
+ }
129
+ if (attrs.length > 1) {
130
+ multilineAttrs = true
131
+ }
132
+
133
+ // Tags
134
+ const tagName = getTagName(vnode)
135
+
136
+ // Children
137
+ const childLines: string[] = []
138
+ if (typeof vnode.children === 'string') {
139
+ childLines.push(vnode.children)
140
+ } else if (Array.isArray(vnode.children)) {
141
+ for (const child of vnode.children) {
142
+ childLines.push(...await printVNode(child))
143
+ }
144
+ }
145
+
146
+ // Slots
147
+ if (vnode.children && typeof vnode.children === 'object' && !Array.isArray(vnode.children)) {
148
+ for (const key in vnode.children) {
149
+ if (typeof vnode.children[key] === 'function') {
150
+ const autoObject = createAutoBuildingObject(key => `{{ ${key} }}`, (target, p) => {
151
+ // Vue 3
152
+ if (p === '__v_isRef') {
153
+ return () => false
154
+ }
155
+ })
156
+ const children = vnode.children[key](autoObject.proxy)
157
+ const slotLines: string[] = []
158
+ for (const child of children) {
159
+ slotLines.push(...await printVNode(child))
160
+ }
161
+ const slotProps = Object.keys(autoObject.cache)
162
+ if (slotProps.length) {
163
+ childLines.push(`<template #${key}="{ ${slotProps.join(', ')} }">`)
164
+ childLines.push(...indent(slotLines))
165
+ childLines.push('</template>')
166
+ } else if (key === 'default') {
167
+ childLines.push(...slotLines)
168
+ } else {
169
+ childLines.push(`<template #${key}>`)
170
+ childLines.push(...indent(slotLines))
171
+ childLines.push(`</template>`)
172
+ }
173
+ }
174
+ }
175
+ }
176
+
177
+ // Template
178
+ const tag = [`<${tagName}`]
179
+ if (multilineAttrs) {
180
+ for (const attrLines of attrs) {
181
+ tag.push(...indent(attrLines))
182
+ }
183
+ if (childLines.length > 0) {
184
+ tag.push('>')
185
+ }
186
+ } else {
187
+ if (attrs.length === 1) {
188
+ tag[0] += ` ${attrs[0]}`
189
+ }
190
+ if (childLines.length > 0) {
191
+ tag[0] += '>'
192
+ }
193
+ }
194
+
195
+ if (childLines.length > 0) {
196
+ lines.push(...tag)
197
+ lines.push(...indent(childLines))
198
+ lines.push(`</${tagName}>`)
199
+ } else if (tag.length > 1) {
200
+ lines.push(...tag)
201
+ lines.push('/>')
202
+ } else {
203
+ lines.push(`${tag[0]} />`)
204
+ }
205
+ }
206
+
207
+ return lines
208
+ }
209
+
210
+ function getTagName (vnode: VNode) {
211
+ if (typeof vnode.type === 'string') {
212
+ return vnode.type
213
+ } else if (vnode.type?.__asyncResolved) {
214
+ const asyncComp = vnode.type?.__asyncResolved
215
+ return asyncComp.name ?? getNameFromFile(asyncComp.__file)
216
+ } else if (vnode.type?.name) {
217
+ return vnode.type.name
218
+ } else if (vnode.type?.__file) {
219
+ return getNameFromFile(vnode.type.__file)
220
+ }
221
+ return 'Anonymous'
222
+ }
223
+
224
+ function getNameFromFile (file: string) {
225
+ const parts = /\/([^/]+)\.vue$/.exec(file)
226
+ if (parts) {
227
+ return pascal(parts[1])
228
+ }
229
+ return 'Anonymous'
230
+ }
231
+
232
+ function serializeAndCleanJs (value: any) {
233
+ const isAutoBuildingObject = !!value?.__autoBuildingObject
234
+ const result = serializeJs(value)
235
+ if (isAutoBuildingObject) {
236
+ return [result.__autoBuildingObjectGetKey]
237
+ } else {
238
+ return result.replace(/\$setup\./g, '').split('\n')
239
+ }
240
+ }
@@ -1,3 +1,17 @@
1
+ <script lang="ts" setup>
2
+ import { computed } from 'vue'
3
+ import { histoireConfig, customLogos } from '../util/config.js'
4
+ import HistoireLogo from '../assets/histoire.svg'
5
+
6
+ const logoUrl = computed(() => histoireConfig.theme?.logo?.square ? customLogos.square : HistoireLogo)
7
+ </script>
8
+
1
9
  <template>
2
- Home
10
+ <div class="htw-flex htw-items-center htw-justify-center htw-h-full">
11
+ <img
12
+ :src="logoUrl"
13
+ alt="Logo"
14
+ class="htw-w-64 htw-h-64 htw-opacity-25"
15
+ >
16
+ </div>
3
17
  </template>
@@ -1,7 +1,8 @@
1
1
  <script lang="ts" setup>
2
2
  import { PropType } from 'vue'
3
3
  import { Story, Variant } from '../types'
4
- import SandboxVue from './SandboxVue.vue'
4
+ import SandboxVue3 from './sandbox/SandboxVue3.vue'
5
+ import BaseEmpty from './base/BaseEmpty.vue'
5
6
 
6
7
  defineProps({
7
8
  variant: {
@@ -19,11 +20,15 @@ defineProps({
19
20
  <template>
20
21
  <div>
21
22
  <!-- Custom controls -->
22
- <SandboxVue
23
+ <SandboxVue3
23
24
  v-if="variant.slots().controls"
24
25
  slot-name="controls"
25
26
  :variant="variant"
26
27
  :story="story"
27
28
  />
29
+
30
+ <BaseEmpty v-else>
31
+ <span>No controls available for this story</span>
32
+ </BaseEmpty>
28
33
  </div>
29
34
  </template>