@aurelia/storybook 1.0.1 → 2.0.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 (39) hide show
  1. package/README.md +12 -20
  2. package/apps/hello-world/.storybook/main.ts +27 -5
  3. package/apps/hello-world/package-lock.json +781 -3474
  4. package/apps/hello-world/package.json +12 -8
  5. package/apps/hello-world/src/stories/hello-world.stories.ts +6 -6
  6. package/apps/hello-world-webpack/.storybook/main.ts +0 -1
  7. package/apps/hello-world-webpack/package-lock.json +229 -2042
  8. package/apps/hello-world-webpack/package.json +8 -7
  9. package/apps/hello-world-webpack/src/hello-world.html +6 -0
  10. package/apps/hello-world-webpack/src/hello-world.ts +17 -0
  11. package/apps/hello-world-webpack/src/my-app.stories.ts +10 -7
  12. package/apps/hello-world-webpack/src/stories/hello-world.stories.ts +54 -0
  13. package/dist/index.js +65 -121
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +62 -115
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/preset.js +17 -10
  18. package/dist/preset.js.map +1 -1
  19. package/dist/preset.mjs +15 -1
  20. package/dist/preset.mjs.map +1 -1
  21. package/dist/preview/render.js +44 -124
  22. package/dist/preview/render.js.map +1 -1
  23. package/dist/preview/render.mjs +41 -116
  24. package/dist/preview/render.mjs.map +1 -1
  25. package/dist/preview/types.js +0 -1
  26. package/dist/preview/types.js.map +1 -1
  27. package/dist/preview.js +84 -44
  28. package/dist/preview.js.map +1 -1
  29. package/dist/preview.mjs +81 -39
  30. package/dist/preview.mjs.map +1 -1
  31. package/dist/webpack.js +1 -3
  32. package/dist/webpack.js.map +1 -1
  33. package/package.json +16 -19
  34. package/rollup.config.mjs +10 -8
  35. package/src/index.ts +28 -0
  36. package/src/preset.ts +20 -2
  37. package/src/preview/render.ts +49 -47
  38. package/src/preview.ts +1 -50
  39. package/tsconfig.json +1 -1
@@ -1,89 +1,5 @@
1
1
  import Aurelia, { CustomElement } from 'aurelia';
2
2
 
3
- // src/core-events/index.ts
4
- var R = /* @__PURE__ */ ((E) => (E.CHANNEL_WS_DISCONNECT = "channelWSDisconnect", E.CHANNEL_CREATED = "channelCreated", E.CONFIG_ERROR = "co\
5
- nfigError", E.STORY_INDEX_INVALIDATED = "storyIndexInvalidated", E.STORY_SPECIFIED = "storySpecified", E.SET_CONFIG = "setConfig", E.SET_STORIES =
6
- "setStories", E.SET_INDEX = "setIndex", E.SET_CURRENT_STORY = "setCurrentStory", E.CURRENT_STORY_WAS_SET = "currentStoryWasSet", E.FORCE_RE_RENDER =
7
- "forceReRender", E.FORCE_REMOUNT = "forceRemount", E.PRELOAD_ENTRIES = "preloadStories", E.STORY_PREPARED = "storyPrepared", E.DOCS_PREPARED =
8
- "docsPrepared", E.STORY_CHANGED = "storyChanged", E.STORY_UNCHANGED = "storyUnchanged", E.STORY_RENDERED = "storyRendered", E.STORY_FINISHED =
9
- "storyFinished", E.STORY_MISSING = "storyMissing", E.STORY_ERRORED = "storyErrored", E.STORY_THREW_EXCEPTION = "storyThrewException", E.STORY_RENDER_PHASE_CHANGED =
10
- "storyRenderPhaseChanged", E.STORY_HOT_UPDATED = "storyHotUpdated", E.PLAY_FUNCTION_THREW_EXCEPTION = "playFunctionThrewException", E.UNHANDLED_ERRORS_WHILE_PLAYING =
11
- "unhandledErrorsWhilePlaying", E.UPDATE_STORY_ARGS = "updateStoryArgs", E.STORY_ARGS_UPDATED = "storyArgsUpdated", E.RESET_STORY_ARGS = "res\
12
- etStoryArgs", E.SET_FILTER = "setFilter", E.SET_GLOBALS = "setGlobals", E.UPDATE_GLOBALS = "updateGlobals", E.GLOBALS_UPDATED = "globalsUpda\
13
- ted", E.REGISTER_SUBSCRIPTION = "registerSubscription", E.PREVIEW_KEYDOWN = "previewKeydown", E.PREVIEW_BUILDER_PROGRESS = "preview_builder_\
14
- progress", E.SELECT_STORY = "selectStory", E.STORIES_COLLAPSE_ALL = "storiesCollapseAll", E.STORIES_EXPAND_ALL = "storiesExpandAll", E.DOCS_RENDERED =
15
- "docsRendered", E.SHARED_STATE_CHANGED = "sharedStateChanged", E.SHARED_STATE_SET = "sharedStateSet", E.NAVIGATE_URL = "navigateUrl", E.UPDATE_QUERY_PARAMS =
16
- "updateQueryParams", E.REQUEST_WHATS_NEW_DATA = "requestWhatsNewData", E.RESULT_WHATS_NEW_DATA = "resultWhatsNewData", E.SET_WHATS_NEW_CACHE =
17
- "setWhatsNewCache", E.TOGGLE_WHATS_NEW_NOTIFICATIONS = "toggleWhatsNewNotifications", E.TELEMETRY_ERROR = "telemetryError", E.FILE_COMPONENT_SEARCH_REQUEST =
18
- "fileComponentSearchRequest", E.FILE_COMPONENT_SEARCH_RESPONSE = "fileComponentSearchResponse", E.SAVE_STORY_REQUEST = "saveStoryRequest", E.
19
- SAVE_STORY_RESPONSE = "saveStoryResponse", E.ARGTYPES_INFO_REQUEST = "argtypesInfoRequest", E.ARGTYPES_INFO_RESPONSE = "argtypesInfoResponse",
20
- E.CREATE_NEW_STORYFILE_REQUEST = "createNewStoryfileRequest", E.CREATE_NEW_STORYFILE_RESPONSE = "createNewStoryfileResponse", E))(R || {}), {
21
- CHANNEL_WS_DISCONNECT: _,
22
- CHANNEL_CREATED: T,
23
- CONFIG_ERROR: O,
24
- CREATE_NEW_STORYFILE_REQUEST: A,
25
- CREATE_NEW_STORYFILE_RESPONSE: N,
26
- CURRENT_STORY_WAS_SET: r,
27
- DOCS_PREPARED: D,
28
- DOCS_RENDERED: I,
29
- FILE_COMPONENT_SEARCH_REQUEST: o,
30
- FILE_COMPONENT_SEARCH_RESPONSE: C,
31
- FORCE_RE_RENDER: P,
32
- FORCE_REMOUNT: L,
33
- GLOBALS_UPDATED: Y,
34
- NAVIGATE_URL: a,
35
- PLAY_FUNCTION_THREW_EXCEPTION: U,
36
- UNHANDLED_ERRORS_WHILE_PLAYING: H,
37
- PRELOAD_ENTRIES: G,
38
- PREVIEW_BUILDER_PROGRESS: W,
39
- PREVIEW_KEYDOWN: d,
40
- REGISTER_SUBSCRIPTION: t,
41
- RESET_STORY_ARGS: p,
42
- SELECT_STORY: i,
43
- SET_CONFIG: F,
44
- SET_CURRENT_STORY: l,
45
- SET_FILTER: y,
46
- SET_GLOBALS: e,
47
- SET_INDEX: c,
48
- SET_STORIES: h,
49
- SHARED_STATE_CHANGED: f,
50
- SHARED_STATE_SET: g,
51
- STORIES_COLLAPSE_ALL: u,
52
- STORIES_EXPAND_ALL: s,
53
- STORY_ARGS_UPDATED: x,
54
- STORY_CHANGED: m,
55
- STORY_ERRORED: M,
56
- STORY_INDEX_INVALIDATED: Q,
57
- STORY_MISSING: V,
58
- STORY_PREPARED: w,
59
- STORY_RENDER_PHASE_CHANGED: B,
60
- STORY_RENDERED: X,
61
- STORY_FINISHED: b,
62
- STORY_SPECIFIED: q,
63
- STORY_THREW_EXCEPTION: K,
64
- STORY_UNCHANGED: j,
65
- STORY_HOT_UPDATED: k,
66
- UPDATE_GLOBALS: z,
67
- UPDATE_QUERY_PARAMS: J,
68
- UPDATE_STORY_ARGS: Z,
69
- REQUEST_WHATS_NEW_DATA: $,
70
- RESULT_WHATS_NEW_DATA: n,
71
- SET_WHATS_NEW_CACHE: v,
72
- TOGGLE_WHATS_NEW_NOTIFICATIONS: EE,
73
- TELEMETRY_ERROR: RE,
74
- SAVE_STORY_REQUEST: SE,
75
- SAVE_STORY_RESPONSE: _E,
76
- ARGTYPES_INFO_REQUEST: TE,
77
- ARGTYPES_INFO_RESPONSE: OE
78
- } = R;
79
-
80
- /**
81
- * Merges multiple sources into a single object.
82
- * Sources can be story parameters, args, or story.props.
83
- */
84
- function mergeStoryProps(...sources) {
85
- return Object.assign({}, ...sources);
86
- }
87
3
  // Track Aurelia apps for cleanup
88
4
  const appMap = new Map();
89
5
  async function teardown(element) {
@@ -95,17 +11,37 @@ async function teardown(element) {
95
11
  }
96
12
  }
97
13
  }
98
- const render = (args, { id, component: Component }) => {
14
+ const render = (args, context) => {
15
+ const { id, component: Component } = context;
99
16
  if (!Component) {
100
17
  throw new Error(`Unable to render story ${id} as the component annotation is missing from the default export`);
101
18
  }
102
19
  return { Component, props: args, template: '' };
103
20
  };
104
21
  async function renderToCanvas({ storyFn, title, name, showMain, showError, storyContext, forceRemount, }, canvasElement, bootstrapAppFn) {
105
- const appBootstrapFn = bootstrapAppFn || bootstrapAureliaApp;
22
+ // Store reference to the original storybook root element
23
+ const rootElement = canvasElement;
24
+ // Ensure we have (or create) a single container inside the root where the Aurelia app actually renders
25
+ let hostElement;
26
+ if (rootElement.id === 'storybook-root') {
27
+ hostElement = rootElement.querySelector('.aurelia-story-container');
28
+ if (!hostElement) {
29
+ hostElement = document.createElement('div');
30
+ hostElement.className = 'aurelia-story-container';
31
+ hostElement.style.height = '100%';
32
+ rootElement.appendChild(hostElement);
33
+ }
34
+ }
35
+ else {
36
+ hostElement = rootElement;
37
+ }
38
+ // All app instances are now tracked by the *root* element, ensuring we only ever have one per story iframe
39
+ const appBootstrapFn = bootstrapAppFn ?? createAureliaApp;
106
40
  const { parameters, component, args } = storyContext;
107
- let app = appMap.get(canvasElement);
41
+ let app = appMap.get(rootElement);
108
42
  const story = storyFn();
43
+ // Temporary debug logging
44
+ console.log(`[DEBUG] Story: ${name}, forceRemount: ${forceRemount}, hasExistingApp: ${!!app}, canvasId: ${canvasElement.className}`);
109
45
  if (!story) {
110
46
  showError({
111
47
  title: `Expecting an Aurelia component from the story: "${name}" of "${title}".`,
@@ -117,43 +53,32 @@ async function renderToCanvas({ storyFn, title, name, showMain, showError, story
117
53
  return () => { };
118
54
  }
119
55
  showMain();
120
- let mergedProps;
121
- // Use full merge (including story.props) when bootstrapping a new app or force remounting.
122
56
  if (!app || forceRemount) {
123
- mergedProps = mergeStoryProps(parameters?.args, args, story.props);
124
- if (app) {
125
- await teardown(canvasElement);
57
+ if (forceRemount && app) {
58
+ await teardown(rootElement);
59
+ app = undefined;
126
60
  }
127
- app = appBootstrapFn(story, mergedProps, canvasElement, component);
128
- await app.start();
129
- appMap.set(canvasElement, app);
61
+ // Clear container before mounting new app
62
+ hostElement.innerHTML = '';
63
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
64
+ const aureliaApp = appBootstrapFn(story, mergedProps, hostElement, component);
65
+ await aureliaApp.start();
66
+ appMap.set(rootElement, aureliaApp);
67
+ app = aureliaApp;
130
68
  }
131
69
  else {
132
- // Update the existing app viewModel only with parameters and args (exclude story.props).
133
- mergedProps = mergeStoryProps(parameters?.args, args);
134
- if (app.root?.controller?.viewModel) {
70
+ // update existing app props
71
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
72
+ if (app?.root?.controller?.viewModel) {
135
73
  Object.assign(app.root.controller.viewModel, mergedProps);
136
74
  }
137
75
  }
138
- // Set up story change listener for cleanup
139
- const channel = storyContext.viewMode === 'story' ? storyContext.channel : null;
140
- let onStoryChange;
141
- if (channel) {
142
- onStoryChange = () => {
143
- // When the story changes, clean up the Aurelia app
144
- teardown(canvasElement);
145
- };
146
- channel.on(m, onStoryChange);
147
- }
148
- // Return teardown function that also unsubscribes from STORY_CHANGED
76
+ // Return cleanup fn
149
77
  return async () => {
150
- if (channel && onStoryChange) {
151
- channel.off(m, onStoryChange);
152
- }
153
- await teardown(canvasElement);
78
+ await teardown(rootElement);
154
79
  };
155
80
  }
156
- function bootstrapAureliaApp(story, args, domElement, component) {
81
+ function createAureliaApp(story, args, domElement, component) {
157
82
  const aurelia = new Aurelia(story.container);
158
83
  if (story.items?.length) {
159
84
  aurelia.register(...story.items);
@@ -167,7 +92,7 @@ function bootstrapAureliaApp(story, args, domElement, component) {
167
92
  aurelia.register(component);
168
93
  }
169
94
  const App = CustomElement.define({
170
- name: 'au-storybook',
95
+ name: 'sb-app',
171
96
  template,
172
97
  containerless: true,
173
98
  }, class {
@@ -185,5 +110,5 @@ function createComponentTemplate(component, innerHtml) {
185
110
  .join(' ')}>${innerHtml ?? ''}</${def.name}>`;
186
111
  }
187
112
 
188
- export { bootstrapAureliaApp, createComponentTemplate, render, renderToCanvas };
113
+ export { createAureliaApp, createComponentTemplate, render, renderToCanvas };
189
114
  //# sourceMappingURL=render.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"render.mjs","sources":["../../node_modules/storybook/dist/core-events/index.js","../../src/preview/render.ts"],"sourcesContent":["// src/core-events/index.ts\nvar R = /* @__PURE__ */ ((E) => (E.CHANNEL_WS_DISCONNECT = \"channelWSDisconnect\", E.CHANNEL_CREATED = \"channelCreated\", E.CONFIG_ERROR = \"co\\\nnfigError\", E.STORY_INDEX_INVALIDATED = \"storyIndexInvalidated\", E.STORY_SPECIFIED = \"storySpecified\", E.SET_CONFIG = \"setConfig\", E.SET_STORIES =\n\"setStories\", E.SET_INDEX = \"setIndex\", E.SET_CURRENT_STORY = \"setCurrentStory\", E.CURRENT_STORY_WAS_SET = \"currentStoryWasSet\", E.FORCE_RE_RENDER =\n\"forceReRender\", E.FORCE_REMOUNT = \"forceRemount\", E.PRELOAD_ENTRIES = \"preloadStories\", E.STORY_PREPARED = \"storyPrepared\", E.DOCS_PREPARED =\n\"docsPrepared\", E.STORY_CHANGED = \"storyChanged\", E.STORY_UNCHANGED = \"storyUnchanged\", E.STORY_RENDERED = \"storyRendered\", E.STORY_FINISHED =\n\"storyFinished\", E.STORY_MISSING = \"storyMissing\", E.STORY_ERRORED = \"storyErrored\", E.STORY_THREW_EXCEPTION = \"storyThrewException\", E.STORY_RENDER_PHASE_CHANGED =\n\"storyRenderPhaseChanged\", E.STORY_HOT_UPDATED = \"storyHotUpdated\", E.PLAY_FUNCTION_THREW_EXCEPTION = \"playFunctionThrewException\", E.UNHANDLED_ERRORS_WHILE_PLAYING =\n\"unhandledErrorsWhilePlaying\", E.UPDATE_STORY_ARGS = \"updateStoryArgs\", E.STORY_ARGS_UPDATED = \"storyArgsUpdated\", E.RESET_STORY_ARGS = \"res\\\netStoryArgs\", E.SET_FILTER = \"setFilter\", E.SET_GLOBALS = \"setGlobals\", E.UPDATE_GLOBALS = \"updateGlobals\", E.GLOBALS_UPDATED = \"globalsUpda\\\nted\", E.REGISTER_SUBSCRIPTION = \"registerSubscription\", E.PREVIEW_KEYDOWN = \"previewKeydown\", E.PREVIEW_BUILDER_PROGRESS = \"preview_builder_\\\nprogress\", E.SELECT_STORY = \"selectStory\", E.STORIES_COLLAPSE_ALL = \"storiesCollapseAll\", E.STORIES_EXPAND_ALL = \"storiesExpandAll\", E.DOCS_RENDERED =\n\"docsRendered\", E.SHARED_STATE_CHANGED = \"sharedStateChanged\", E.SHARED_STATE_SET = \"sharedStateSet\", E.NAVIGATE_URL = \"navigateUrl\", E.UPDATE_QUERY_PARAMS =\n\"updateQueryParams\", E.REQUEST_WHATS_NEW_DATA = \"requestWhatsNewData\", E.RESULT_WHATS_NEW_DATA = \"resultWhatsNewData\", E.SET_WHATS_NEW_CACHE =\n\"setWhatsNewCache\", E.TOGGLE_WHATS_NEW_NOTIFICATIONS = \"toggleWhatsNewNotifications\", E.TELEMETRY_ERROR = \"telemetryError\", E.FILE_COMPONENT_SEARCH_REQUEST =\n\"fileComponentSearchRequest\", E.FILE_COMPONENT_SEARCH_RESPONSE = \"fileComponentSearchResponse\", E.SAVE_STORY_REQUEST = \"saveStoryRequest\", E.\nSAVE_STORY_RESPONSE = \"saveStoryResponse\", E.ARGTYPES_INFO_REQUEST = \"argtypesInfoRequest\", E.ARGTYPES_INFO_RESPONSE = \"argtypesInfoResponse\",\nE.CREATE_NEW_STORYFILE_REQUEST = \"createNewStoryfileRequest\", E.CREATE_NEW_STORYFILE_RESPONSE = \"createNewStoryfileResponse\", E))(R || {}), S = R,\n{\n CHANNEL_WS_DISCONNECT: _,\n CHANNEL_CREATED: T,\n CONFIG_ERROR: O,\n CREATE_NEW_STORYFILE_REQUEST: A,\n CREATE_NEW_STORYFILE_RESPONSE: N,\n CURRENT_STORY_WAS_SET: r,\n DOCS_PREPARED: D,\n DOCS_RENDERED: I,\n FILE_COMPONENT_SEARCH_REQUEST: o,\n FILE_COMPONENT_SEARCH_RESPONSE: C,\n FORCE_RE_RENDER: P,\n FORCE_REMOUNT: L,\n GLOBALS_UPDATED: Y,\n NAVIGATE_URL: a,\n PLAY_FUNCTION_THREW_EXCEPTION: U,\n UNHANDLED_ERRORS_WHILE_PLAYING: H,\n PRELOAD_ENTRIES: G,\n PREVIEW_BUILDER_PROGRESS: W,\n PREVIEW_KEYDOWN: d,\n REGISTER_SUBSCRIPTION: t,\n RESET_STORY_ARGS: p,\n SELECT_STORY: i,\n SET_CONFIG: F,\n SET_CURRENT_STORY: l,\n SET_FILTER: y,\n SET_GLOBALS: e,\n SET_INDEX: c,\n SET_STORIES: h,\n SHARED_STATE_CHANGED: f,\n SHARED_STATE_SET: g,\n STORIES_COLLAPSE_ALL: u,\n STORIES_EXPAND_ALL: s,\n STORY_ARGS_UPDATED: x,\n STORY_CHANGED: m,\n STORY_ERRORED: M,\n STORY_INDEX_INVALIDATED: Q,\n STORY_MISSING: V,\n STORY_PREPARED: w,\n STORY_RENDER_PHASE_CHANGED: B,\n STORY_RENDERED: X,\n STORY_FINISHED: b,\n STORY_SPECIFIED: q,\n STORY_THREW_EXCEPTION: K,\n STORY_UNCHANGED: j,\n STORY_HOT_UPDATED: k,\n UPDATE_GLOBALS: z,\n UPDATE_QUERY_PARAMS: J,\n UPDATE_STORY_ARGS: Z,\n REQUEST_WHATS_NEW_DATA: $,\n RESULT_WHATS_NEW_DATA: n,\n SET_WHATS_NEW_CACHE: v,\n TOGGLE_WHATS_NEW_NOTIFICATIONS: EE,\n TELEMETRY_ERROR: RE,\n SAVE_STORY_REQUEST: SE,\n SAVE_STORY_RESPONSE: _E,\n ARGTYPES_INFO_REQUEST: TE,\n ARGTYPES_INFO_RESPONSE: OE\n} = R;\nexport {\n TE as ARGTYPES_INFO_REQUEST,\n OE as ARGTYPES_INFO_RESPONSE,\n T as CHANNEL_CREATED,\n _ as CHANNEL_WS_DISCONNECT,\n O as CONFIG_ERROR,\n A as CREATE_NEW_STORYFILE_REQUEST,\n N as CREATE_NEW_STORYFILE_RESPONSE,\n r as CURRENT_STORY_WAS_SET,\n D as DOCS_PREPARED,\n I as DOCS_RENDERED,\n o as FILE_COMPONENT_SEARCH_REQUEST,\n C as FILE_COMPONENT_SEARCH_RESPONSE,\n L as FORCE_REMOUNT,\n P as FORCE_RE_RENDER,\n Y as GLOBALS_UPDATED,\n a as NAVIGATE_URL,\n U as PLAY_FUNCTION_THREW_EXCEPTION,\n G as PRELOAD_ENTRIES,\n W as PREVIEW_BUILDER_PROGRESS,\n d as PREVIEW_KEYDOWN,\n t as REGISTER_SUBSCRIPTION,\n $ as REQUEST_WHATS_NEW_DATA,\n p as RESET_STORY_ARGS,\n n as RESULT_WHATS_NEW_DATA,\n SE as SAVE_STORY_REQUEST,\n _E as SAVE_STORY_RESPONSE,\n i as SELECT_STORY,\n F as SET_CONFIG,\n l as SET_CURRENT_STORY,\n y as SET_FILTER,\n e as SET_GLOBALS,\n c as SET_INDEX,\n h as SET_STORIES,\n v as SET_WHATS_NEW_CACHE,\n f as SHARED_STATE_CHANGED,\n g as SHARED_STATE_SET,\n u as STORIES_COLLAPSE_ALL,\n s as STORIES_EXPAND_ALL,\n x as STORY_ARGS_UPDATED,\n m as STORY_CHANGED,\n M as STORY_ERRORED,\n b as STORY_FINISHED,\n k as STORY_HOT_UPDATED,\n Q as STORY_INDEX_INVALIDATED,\n V as STORY_MISSING,\n w as STORY_PREPARED,\n X as STORY_RENDERED,\n B as STORY_RENDER_PHASE_CHANGED,\n q as STORY_SPECIFIED,\n K as STORY_THREW_EXCEPTION,\n j as STORY_UNCHANGED,\n RE as TELEMETRY_ERROR,\n EE as TOGGLE_WHATS_NEW_NOTIFICATIONS,\n H as UNHANDLED_ERRORS_WHILE_PLAYING,\n z as UPDATE_GLOBALS,\n J as UPDATE_QUERY_PARAMS,\n Z as UPDATE_STORY_ARGS,\n S as default\n};\n","import { STORY_CHANGED } from 'storybook/internal/core-events';\nimport type { RenderContext, ArgsStoryFn } from 'storybook/internal/types';\nimport type { AureliaRenderer } from './types';\nimport Aurelia, { Constructable, CustomElement } from 'aurelia';\n\ninterface AureliaStoryResult {\n template: string;\n components?: unknown[];\n Component?: unknown;\n container?: any;\n items?: unknown[];\n innerHtml?: string;\n props?: Record<string, any>;\n}\n\n/**\n * Merges multiple sources into a single object.\n * Sources can be story parameters, args, or story.props.\n */\nfunction mergeStoryProps(\n ...sources: Array<Record<string, any> | undefined>\n): Record<string, any> {\n return Object.assign({}, ...sources);\n}\n\n// Track Aurelia apps for cleanup\nconst appMap = new Map<HTMLElement, Aurelia>();\n\nasync function teardown(element: HTMLElement) {\n if (appMap.has(element)) {\n const app = appMap.get(element);\n if (app) {\n await app.stop();\n appMap.delete(element);\n }\n }\n}\n\nexport const render: ArgsStoryFn<AureliaRenderer> = (args, { id, component: Component }) => {\n if (!Component) {\n throw new Error(\n `Unable to render story ${id} as the component annotation is missing from the default export`\n );\n }\n return { Component, props: args, template: '' };\n};\n\nexport async function renderToCanvas(\n {\n storyFn,\n title,\n name,\n showMain,\n showError,\n storyContext,\n forceRemount,\n }: RenderContext<AureliaRenderer>,\n canvasElement: HTMLElement,\n bootstrapAppFn?: typeof bootstrapAureliaApp\n) {\n const appBootstrapFn = bootstrapAppFn || bootstrapAureliaApp;\n\n const { parameters, component, args } = storyContext;\n let app = appMap.get(canvasElement);\n\n const story = storyFn() as AureliaStoryResult;\n\n if (!story) {\n showError({\n title: `Expecting an Aurelia component from the story: \"${name}\" of \"${title}\".`,\n description: `\n Did you forget to return the Aurelia component from the story?\n Use \"() => ({ template: '<custom-component></custom-component>' })\" when defining the story.\n `,\n });\n return () => {};\n }\n\n showMain();\n\n let mergedProps;\n // Use full merge (including story.props) when bootstrapping a new app or force remounting.\n if (!app || forceRemount) {\n mergedProps = mergeStoryProps(parameters?.args, args, story.props);\n if (app) {\n await teardown(canvasElement);\n }\n app = appBootstrapFn(\n story,\n mergedProps,\n canvasElement,\n component as Constructable\n ) as Aurelia;\n await app.start();\n appMap.set(canvasElement, app);\n } else {\n // Update the existing app viewModel only with parameters and args (exclude story.props).\n mergedProps = mergeStoryProps(parameters?.args, args);\n if (app.root?.controller?.viewModel) {\n Object.assign(app.root.controller.viewModel, mergedProps);\n }\n }\n\n // Set up story change listener for cleanup\n const channel = storyContext.viewMode === 'story' ? storyContext.channel : null;\n let onStoryChange: () => void;\n if (channel) {\n onStoryChange = () => {\n // When the story changes, clean up the Aurelia app\n teardown(canvasElement);\n };\n channel.on(STORY_CHANGED, onStoryChange);\n }\n\n // Return teardown function that also unsubscribes from STORY_CHANGED\n return async () => {\n if (channel && onStoryChange) {\n channel.off(STORY_CHANGED, onStoryChange);\n }\n await teardown(canvasElement);\n };\n}\n\nexport function bootstrapAureliaApp(\n story: AureliaStoryResult,\n args: Record<string, any>,\n domElement: HTMLElement,\n component?: Constructable\n) {\n const aurelia = new Aurelia(story.container);\n\n if (story.items?.length) {\n aurelia.register(...story.items);\n }\n\n if (story.components?.length) {\n aurelia.register(...story.components);\n }\n\n let { template } = story;\n\n if (component) {\n template = template ?? createComponentTemplate(component, story.innerHtml);\n aurelia.register(component);\n }\n\n const App = CustomElement.define(\n {\n name: 'au-storybook',\n template,\n containerless: true,\n },\n class {}\n );\n\n const app = Object.assign(new App(), args);\n\n return aurelia.app({\n host: domElement,\n component: app,\n });\n}\n\nexport function createComponentTemplate(\n component: Constructable,\n innerHtml?: string\n): string {\n const def = CustomElement.getDefinition(component);\n\n return `<${def.name} ${Object.values(def.bindables)\n .map((bindable) => `${bindable.attribute}.bind=\"${bindable.name}\"`)\n .join(' ')}>${innerHtml ?? ''}</${def.name}>`;\n}"],"names":["STORY_CHANGED"],"mappings":";;AAAA;AACG,IAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,qBAAqB,GAAG,qBAAqB,EAAE,CAAC,CAAC,eAAe,GAAG,gBAAgB,EAAE,CAAC,CAAC,YAAY,GAAG;AACzI,UAAU,EAAE,CAAC,CAAC,uBAAuB,GAAG,uBAAuB,EAAE,CAAC,CAAC,eAAe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AC;AACJ;AAEM,SAAU,uBAAuB,CACrC,SAAwB,EACxB,SAAkB,EAAA;IAElB,MAAM,GAAG,GAAG,aAAa,CAAC,aAAa,CAAC,SAAS,CAAC;AAElD,IAAA,OAAO,CAAA,CAAA,EAAI,GAAG,CAAC,IAAI,CAAA,CAAA,EAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS;AAC/C,SAAA,GAAG,CAAC,CAAC,QAAQ,KAAK,CAAA,EAAG,QAAQ,CAAC,SAAS,CAAA,OAAA,EAAU,QAAQ,CAAC,IAAI,GAAG;AACjE,SAAA,IAAI,CAAC,GAAG,CAAC,CAAA,CAAA,EAAI,SAAS,IAAI,EAAE,CAAA,EAAA,EAAK,GAAG,CAAC,IAAI,CAAA,CAAA,CAAG;AACjD;;;;","x_google_ignoreList":[0]}
1
+ {"version":3,"file":"render.mjs","sources":["../../src/preview/render.ts"],"sourcesContent":["import { STORY_CHANGED } from 'storybook/internal/core-events';\nimport type { RenderContext, ArgsStoryFn } from 'storybook/internal/types';\nimport type { AureliaRenderer } from './types';\nimport Aurelia, { Constructable, CustomElement } from 'aurelia';\n\ninterface AureliaStoryResult {\n template: string;\n components?: unknown[];\n Component?: unknown;\n container?: any;\n items?: unknown[];\n innerHtml?: string;\n props?: Record<string, any>;\n}\n\n// Track Aurelia apps for cleanup\nconst appMap = new Map<HTMLElement, any>();\n\nasync function teardown(element: HTMLElement) {\n if (appMap.has(element)) {\n const app = appMap.get(element);\n if (app) {\n await app.stop();\n appMap.delete(element);\n }\n }\n}\n\nexport const render: ArgsStoryFn<AureliaRenderer> = (args, context) => {\n const { id, component: Component } = context;\n \n if (!Component) {\n throw new Error(\n `Unable to render story ${id} as the component annotation is missing from the default export`\n );\n }\n return { Component, props: args, template: '' };\n};\n\nexport async function renderToCanvas(\n {\n storyFn,\n title,\n name,\n showMain,\n showError,\n storyContext,\n forceRemount,\n }: RenderContext<AureliaRenderer>,\n canvasElement: HTMLElement,\n bootstrapAppFn?: typeof createAureliaApp\n) {\n // Store reference to the original storybook root element\n const rootElement = canvasElement;\n\n // Ensure we have (or create) a single container inside the root where the Aurelia app actually renders\n let hostElement: HTMLElement;\n if (rootElement.id === 'storybook-root') {\n hostElement = rootElement.querySelector('.aurelia-story-container') as HTMLElement;\n if (!hostElement) {\n hostElement = document.createElement('div');\n hostElement.className = 'aurelia-story-container';\n hostElement.style.height = '100%';\n rootElement.appendChild(hostElement);\n }\n } else {\n hostElement = rootElement;\n }\n\n // All app instances are now tracked by the *root* element, ensuring we only ever have one per story iframe\n const appBootstrapFn = bootstrapAppFn ?? createAureliaApp;\n const { parameters, component, args } = storyContext;\n \n let app = appMap.get(rootElement);\n const story = storyFn() as AureliaStoryResult;\n \n // Temporary debug logging\n console.log(`[DEBUG] Story: ${name}, forceRemount: ${forceRemount}, hasExistingApp: ${!!app}, canvasId: ${canvasElement.className}`);\n\n if (!story) {\n showError({\n title: `Expecting an Aurelia component from the story: \"${name}\" of \"${title}\".`,\n description: `\n Did you forget to return the Aurelia component from the story?\n Use \"() => ({ template: '<custom-component></custom-component>' })\" when defining the story.\n `,\n });\n return () => {};\n }\n\n showMain();\n\n if (!app || forceRemount) {\n if (forceRemount && app) {\n await teardown(rootElement);\n app = undefined;\n }\n // Clear container before mounting new app\n hostElement.innerHTML = '';\n\n const mergedProps = { ...parameters?.args, ...args, ...story.props };\n\n const aureliaApp = appBootstrapFn(\n story,\n mergedProps,\n hostElement,\n component as Constructable\n );\n await aureliaApp.start();\n appMap.set(rootElement, aureliaApp);\n app = aureliaApp;\n } else {\n // update existing app props\n const mergedProps = { ...parameters?.args, ...args, ...story.props };\n if (app?.root?.controller?.viewModel) {\n Object.assign(app.root.controller.viewModel, mergedProps);\n }\n }\n\n // Return cleanup fn\n return async () => {\n await teardown(rootElement);\n };\n}\n\nexport function createAureliaApp(\n story: AureliaStoryResult,\n args: Record<string, any>,\n domElement: HTMLElement,\n component?: Constructable\n) {\n const aurelia = new Aurelia(story.container);\n\n if (story.items?.length) {\n aurelia.register(...story.items);\n }\n\n if (story.components?.length) {\n aurelia.register(...story.components);\n }\n\n let { template } = story;\n\n if (component) {\n template = template ?? createComponentTemplate(component, story.innerHtml);\n aurelia.register(component);\n }\n\n const App = CustomElement.define(\n {\n name: 'sb-app',\n template,\n containerless: true,\n },\n class {}\n );\n\n const app = Object.assign(new App(), args);\n\n return aurelia.app({\n host: domElement,\n component: app,\n });\n}\n\nexport function createComponentTemplate(\n component: Constructable,\n innerHtml?: string\n): string {\n const def = CustomElement.getDefinition(component);\n\n return `<${def.name} ${Object.values(def.bindables)\n .map((bindable) => `${bindable.attribute}.bind=\"${bindable.name}\"`)\n .join(' ')}>${innerHtml ?? ''}</${def.name}>`;\n}"],"names":[],"mappings":";;AAeA;AACA,MAAM,MAAM,GAAG,IAAI,GAAG,EAAoB;AAE1C,eAAe,QAAQ,CAAC,OAAoB,EAAA;AAC1C,IAAA,IAAI,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;QACvB,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC;QAC/B,IAAI,GAAG,EAAE;AACP,YAAA,MAAM,GAAG,CAAC,IAAI,EAAE;AAChB,YAAA,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;;;AAG5B;MAEa,MAAM,GAAiC,CAAC,IAAI,EAAE,OAAO,KAAI;IACpE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,OAAO;IAE5C,IAAI,CAAC,SAAS,EAAE;AACd,QAAA,MAAM,IAAI,KAAK,CACb,0BAA0B,EAAE,CAAA,+DAAA,CAAiE,CAC9F;;IAEH,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE;AACjD;AAEO,eAAe,cAAc,CAClC,EACE,OAAO,EACP,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,YAAY,GACmB,EACjC,aAA0B,EAC1B,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ExB,IAAI,SAAS,EAAE;QACb,QAAQ,GAAG,QAAQ,IAAI,uBAAuB,CAAC,SAAS,EAAE,KAAK,CAAC,SAAS,CAAC;AAC1E,QAAA,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC;;AAG7B,IAAA,MAAM,GAAG,GAAG,aAAa,CAAC,MAAM,CAC9B;AACE,QAAA,IAAI,EAAE,QAAQ;QACd,QAAQ;AACR,QAAA,aAAa,EAAE,IAAI;KACpB,EACD,MAAA;AAAQ,KAAA,CACT;AAED,IAAA,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,EAAE,EAAE,IAAI,CAAC;IAE1C,OAAO,OAAO,CAAC,GAAG,CAAC;AACjB,QAAA,IAAI,EAAE,UAAU;AAChB,QAAA,SAAS,EAAE,GAAG;AACf,KAAA,CAAC;AACJ;AAEM,SAAU,uBAAuB,CACrC,SAAwB,EACxB,SAAkB,EAAA;IAElB,MAAM,GAAG,GAAG,aAAa,CAAC,aAAa,CAAC,SAAS,CAAC;AAElD,IAAA,OAAO,CAAA,CAAA,EAAI,GAAG,CAAC,IAAI,CAAA,CAAA,EAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS;AAC/C,SAAA,GAAG,CAAC,CAAC,QAAQ,KAAK,CAAA,EAAG,QAAQ,CAAC,SAAS,CAAA,OAAA,EAAU,QAAQ,CAAC,IAAI,GAAG;AACjE,SAAA,IAAI,CAAC,GAAG,CAAC,CAAA,CAAA,EAAI,SAAS,IAAI,EAAE,CAAA,EAAA,EAAK,GAAG,CAAC,IAAI,CAAA,CAAA,CAAG;AACjD;;;;"}
@@ -1,3 +1,2 @@
1
- 'use strict';
2
1
 
3
2
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
1
+ {"version":3,"file":"types.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/preview.js CHANGED
@@ -1,8 +1,84 @@
1
- 'use strict';
1
+ import Aurelia, { CustomElement } from 'aurelia';
2
2
 
3
- var Aurelia = require('aurelia');
4
-
5
- function bootstrapAureliaApp(story, args, domElement, component) {
3
+ // Track Aurelia apps for cleanup
4
+ const appMap = new Map();
5
+ async function teardown(element) {
6
+ if (appMap.has(element)) {
7
+ const app = appMap.get(element);
8
+ if (app) {
9
+ await app.stop();
10
+ appMap.delete(element);
11
+ }
12
+ }
13
+ }
14
+ const render = (args, context) => {
15
+ const { id, component: Component } = context;
16
+ if (!Component) {
17
+ throw new Error(`Unable to render story ${id} as the component annotation is missing from the default export`);
18
+ }
19
+ return { Component, props: args, template: '' };
20
+ };
21
+ async function renderToCanvas({ storyFn, title, name, showMain, showError, storyContext, forceRemount, }, canvasElement, bootstrapAppFn) {
22
+ // Store reference to the original storybook root element
23
+ const rootElement = canvasElement;
24
+ // Ensure we have (or create) a single container inside the root where the Aurelia app actually renders
25
+ let hostElement;
26
+ if (rootElement.id === 'storybook-root') {
27
+ hostElement = rootElement.querySelector('.aurelia-story-container');
28
+ if (!hostElement) {
29
+ hostElement = document.createElement('div');
30
+ hostElement.className = 'aurelia-story-container';
31
+ hostElement.style.height = '100%';
32
+ rootElement.appendChild(hostElement);
33
+ }
34
+ }
35
+ else {
36
+ hostElement = rootElement;
37
+ }
38
+ // All app instances are now tracked by the *root* element, ensuring we only ever have one per story iframe
39
+ const appBootstrapFn = bootstrapAppFn ?? createAureliaApp;
40
+ const { parameters, component, args } = storyContext;
41
+ let app = appMap.get(rootElement);
42
+ const story = storyFn();
43
+ // Temporary debug logging
44
+ console.log(`[DEBUG] Story: ${name}, forceRemount: ${forceRemount}, hasExistingApp: ${!!app}, canvasId: ${canvasElement.className}`);
45
+ if (!story) {
46
+ showError({
47
+ title: `Expecting an Aurelia component from the story: "${name}" of "${title}".`,
48
+ description: `
49
+ Did you forget to return the Aurelia component from the story?
50
+ Use "() => ({ template: '<custom-component></custom-component>' })" when defining the story.
51
+ `,
52
+ });
53
+ return () => { };
54
+ }
55
+ showMain();
56
+ if (!app || forceRemount) {
57
+ if (forceRemount && app) {
58
+ await teardown(rootElement);
59
+ app = undefined;
60
+ }
61
+ // Clear container before mounting new app
62
+ hostElement.innerHTML = '';
63
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
64
+ const aureliaApp = appBootstrapFn(story, mergedProps, hostElement, component);
65
+ await aureliaApp.start();
66
+ appMap.set(rootElement, aureliaApp);
67
+ app = aureliaApp;
68
+ }
69
+ else {
70
+ // update existing app props
71
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
72
+ if (app?.root?.controller?.viewModel) {
73
+ Object.assign(app.root.controller.viewModel, mergedProps);
74
+ }
75
+ }
76
+ // Return cleanup fn
77
+ return async () => {
78
+ await teardown(rootElement);
79
+ };
80
+ }
81
+ function createAureliaApp(story, args, domElement, component) {
6
82
  const aurelia = new Aurelia(story.container);
7
83
  if (story.items?.length) {
8
84
  aurelia.register(...story.items);
@@ -15,8 +91,8 @@ function bootstrapAureliaApp(story, args, domElement, component) {
15
91
  template = template ?? createComponentTemplate(component, story.innerHtml);
16
92
  aurelia.register(component);
17
93
  }
18
- const App = Aurelia.CustomElement.define({
19
- name: 'au-storybook',
94
+ const App = CustomElement.define({
95
+ name: 'sb-app',
20
96
  template,
21
97
  containerless: true,
22
98
  }, class {
@@ -28,47 +104,11 @@ function bootstrapAureliaApp(story, args, domElement, component) {
28
104
  });
29
105
  }
30
106
  function createComponentTemplate(component, innerHtml) {
31
- const def = Aurelia.CustomElement.getDefinition(component);
107
+ const def = CustomElement.getDefinition(component);
32
108
  return `<${def.name} ${Object.values(def.bindables)
33
109
  .map((bindable) => `${bindable.attribute}.bind="${bindable.name}"`)
34
110
  .join(' ')}>${innerHtml ?? ''}</${def.name}>`;
35
111
  }
36
112
 
37
- // Track the current story's cleanup function
38
- let currentCleanup = null;
39
- const render = (args, context) => {
40
- // Clean up previous story if exists
41
- if (currentCleanup) {
42
- currentCleanup();
43
- currentCleanup = null;
44
- }
45
- // Create a container element
46
- const container = document.createElement('div');
47
- // Get the story function result
48
- const story = context.storyFn();
49
- // Bootstrap Aurelia app immediately
50
- if (story && (story.Component || story.template)) {
51
- const app = bootstrapAureliaApp(story, args, container, story.Component || context.component);
52
- // Start the app asynchronously
53
- const startPromise = app.start();
54
- if (startPromise && typeof startPromise.catch === 'function') {
55
- startPromise.catch((error) => {
56
- console.error('Failed to start Aurelia app:', error);
57
- });
58
- }
59
- // Set cleanup function
60
- currentCleanup = () => {
61
- const stopPromise = app.stop();
62
- if (stopPromise && typeof stopPromise.catch === 'function') {
63
- stopPromise.catch((error) => {
64
- console.error('Failed to stop Aurelia app:', error);
65
- });
66
- }
67
- };
68
- }
69
- // Return the container element immediately
70
- return container;
71
- };
72
-
73
- exports.render = render;
113
+ export { render, renderToCanvas };
74
114
  //# sourceMappingURL=preview.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"preview.js","sources":["../src/preview/render.ts","../src/preview.ts"],"sourcesContent":["import { STORY_CHANGED } from 'storybook/internal/core-events';\nimport type { RenderContext, ArgsStoryFn } from 'storybook/internal/types';\nimport type { AureliaRenderer } from './types';\nimport Aurelia, { Constructable, CustomElement } from 'aurelia';\n\ninterface AureliaStoryResult {\n template: string;\n components?: unknown[];\n Component?: unknown;\n container?: any;\n items?: unknown[];\n innerHtml?: string;\n props?: Record<string, any>;\n}\n\n/**\n * Merges multiple sources into a single object.\n * Sources can be story parameters, args, or story.props.\n */\nfunction mergeStoryProps(\n ...sources: Array<Record<string, any> | undefined>\n): Record<string, any> {\n return Object.assign({}, ...sources);\n}\n\n// Track Aurelia apps for cleanup\nconst appMap = new Map<HTMLElement, Aurelia>();\n\nasync function teardown(element: HTMLElement) {\n if (appMap.has(element)) {\n const app = appMap.get(element);\n if (app) {\n await app.stop();\n appMap.delete(element);\n }\n }\n}\n\nexport const render: ArgsStoryFn<AureliaRenderer> = (args, { id, component: Component }) => {\n if (!Component) {\n throw new Error(\n `Unable to render story ${id} as the component annotation is missing from the default export`\n );\n }\n return { Component, props: args, template: '' };\n};\n\nexport async function renderToCanvas(\n {\n storyFn,\n title,\n name,\n showMain,\n showError,\n storyContext,\n forceRemount,\n }: RenderContext<AureliaRenderer>,\n canvasElement: HTMLElement,\n bootstrapAppFn?: typeof bootstrapAureliaApp\n) {\n const appBootstrapFn = bootstrapAppFn || bootstrapAureliaApp;\n\n const { parameters, component, args } = storyContext;\n let app = appMap.get(canvasElement);\n\n const story = storyFn() as AureliaStoryResult;\n\n if (!story) {\n showError({\n title: `Expecting an Aurelia component from the story: \"${name}\" of \"${title}\".`,\n description: `\n Did you forget to return the Aurelia component from the story?\n Use \"() => ({ template: '<custom-component></custom-component>' })\" when defining the story.\n `,\n });\n return () => {};\n }\n\n showMain();\n\n let mergedProps;\n // Use full merge (including story.props) when bootstrapping a new app or force remounting.\n if (!app || forceRemount) {\n mergedProps = mergeStoryProps(parameters?.args, args, story.props);\n if (app) {\n await teardown(canvasElement);\n }\n app = appBootstrapFn(\n story,\n mergedProps,\n canvasElement,\n component as Constructable\n ) as Aurelia;\n await app.start();\n appMap.set(canvasElement, app);\n } else {\n // Update the existing app viewModel only with parameters and args (exclude story.props).\n mergedProps = mergeStoryProps(parameters?.args, args);\n if (app.root?.controller?.viewModel) {\n Object.assign(app.root.controller.viewModel, mergedProps);\n }\n }\n\n // Set up story change listener for cleanup\n const channel = storyContext.viewMode === 'story' ? storyContext.channel : null;\n let onStoryChange: () => void;\n if (channel) {\n onStoryChange = () => {\n // When the story changes, clean up the Aurelia app\n teardown(canvasElement);\n };\n channel.on(STORY_CHANGED, onStoryChange);\n }\n\n // Return teardown function that also unsubscribes from STORY_CHANGED\n return async () => {\n if (channel && onStoryChange) {\n channel.off(STORY_CHANGED, onStoryChange);\n }\n await teardown(canvasElement);\n };\n}\n\nexport function bootstrapAureliaApp(\n story: AureliaStoryResult,\n args: Record<string, any>,\n domElement: HTMLElement,\n component?: Constructable\n) {\n const aurelia = new Aurelia(story.container);\n\n if (story.items?.length) {\n aurelia.register(...story.items);\n }\n\n if (story.components?.length) {\n aurelia.register(...story.components);\n }\n\n let { template } = story;\n\n if (component) {\n template = template ?? createComponentTemplate(component, story.innerHtml);\n aurelia.register(component);\n }\n\n const App = CustomElement.define(\n {\n name: 'au-storybook',\n template,\n containerless: true,\n },\n class {}\n );\n\n const app = Object.assign(new App(), args);\n\n return aurelia.app({\n host: domElement,\n component: app,\n });\n}\n\nexport function createComponentTemplate(\n component: Constructable,\n innerHtml?: string\n): string {\n const def = CustomElement.getDefinition(component);\n\n return `<${def.name} ${Object.values(def.bindables)\n .map((bindable) => `${bindable.attribute}.bind=\"${bindable.name}\"`)\n .join(' ')}>${innerHtml ?? ''}</${def.name}>`;\n}","import { renderToCanvas, bootstrapAureliaApp } from './preview/render';\nimport Aurelia from 'aurelia';\n\n// Track the current story's cleanup function\nlet currentCleanup: (() => void) | null = null;\n\nexport const render = (args: any, context: any) => {\n // Clean up previous story if exists\n if (currentCleanup) {\n currentCleanup();\n currentCleanup = null;\n }\n\n // Create a container element\n const container = document.createElement('div');\n \n // Get the story function result\n const story = context.storyFn();\n \n // Bootstrap Aurelia app immediately\n if (story && (story.Component || story.template)) {\n const app = bootstrapAureliaApp(\n story,\n args,\n container,\n story.Component || context.component\n ) as Aurelia;\n \n // Start the app asynchronously\n const startPromise = app.start();\n if (startPromise && typeof startPromise.catch === 'function') {\n startPromise.catch((error: any) => {\n console.error('Failed to start Aurelia app:', error);\n });\n }\n \n // Set cleanup function\n currentCleanup = () => {\n const stopPromise = app.stop();\n if (stopPromise && typeof stopPromise.catch === 'function') {\n stopPromise.catch((error: any) => {\n console.error('Failed to stop Aurelia app:', error);\n });\n }\n };\n }\n \n // Return the container element immediately\n return container;\n}; "],"names":["CustomElement"],"mappings":";;;;AA2HM,SAAU,mBAAmB,CACjC,KAAyB,EACzB,IAAyB,EACzB,UAAuB,EACvB,SAAyB,EAAA;IAEzB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,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Content":["import { STORY_CHANGED } from 'storybook/internal/core-events';\nimport type { RenderContext, ArgsStoryFn } from 'storybook/internal/types';\nimport type { AureliaRenderer } from './types';\nimport Aurelia, { Constructable, CustomElement } from 'aurelia';\n\ninterface AureliaStoryResult {\n template: string;\n components?: unknown[];\n Component?: unknown;\n container?: any;\n items?: unknown[];\n innerHtml?: string;\n props?: Record<string, any>;\n}\n\n// Track Aurelia apps for cleanup\nconst appMap = new Map<HTMLElement, any>();\n\nasync function teardown(element: HTMLElement) {\n if (appMap.has(element)) {\n const app = appMap.get(element);\n if (app) {\n await app.stop();\n appMap.delete(element);\n }\n }\n}\n\nexport const render: ArgsStoryFn<AureliaRenderer> = (args, context) => {\n const { id, component: Component } = context;\n \n if (!Component) {\n throw new Error(\n `Unable to render story ${id} as the component annotation is missing from the default export`\n );\n }\n return { Component, props: args, template: '' };\n};\n\nexport async function renderToCanvas(\n {\n storyFn,\n title,\n name,\n showMain,\n showError,\n storyContext,\n forceRemount,\n }: RenderContext<AureliaRenderer>,\n canvasElement: HTMLElement,\n bootstrapAppFn?: typeof createAureliaApp\n) {\n // Store reference to the original storybook root element\n const rootElement = canvasElement;\n\n // Ensure we have (or create) a single container inside the root where the Aurelia app actually renders\n let hostElement: HTMLElement;\n if (rootElement.id === 'storybook-root') {\n hostElement = rootElement.querySelector('.aurelia-story-container') as HTMLElement;\n if (!hostElement) {\n hostElement = document.createElement('div');\n hostElement.className = 'aurelia-story-container';\n hostElement.style.height = '100%';\n rootElement.appendChild(hostElement);\n }\n } else {\n hostElement = rootElement;\n }\n\n // All app instances are now tracked by the *root* element, ensuring we only ever have one per story iframe\n const appBootstrapFn = bootstrapAppFn ?? createAureliaApp;\n const { parameters, component, args } = storyContext;\n \n let app = appMap.get(rootElement);\n const story = storyFn() as AureliaStoryResult;\n \n // Temporary debug logging\n console.log(`[DEBUG] Story: ${name}, forceRemount: ${forceRemount}, hasExistingApp: ${!!app}, canvasId: ${canvasElement.className}`);\n\n if (!story) {\n showError({\n title: `Expecting an Aurelia component from the story: \"${name}\" of \"${title}\".`,\n description: `\n Did you forget to return the Aurelia component from the story?\n Use \"() => ({ template: '<custom-component></custom-component>' })\" when defining the story.\n `,\n });\n return () => {};\n }\n\n showMain();\n\n if (!app || forceRemount) {\n if (forceRemount && app) {\n await teardown(rootElement);\n app = undefined;\n }\n // Clear container before mounting new app\n hostElement.innerHTML = '';\n\n const mergedProps = { ...parameters?.args, ...args, ...story.props };\n\n const aureliaApp = appBootstrapFn(\n story,\n mergedProps,\n hostElement,\n component as Constructable\n );\n await aureliaApp.start();\n appMap.set(rootElement, aureliaApp);\n app = aureliaApp;\n } else {\n // update existing app props\n const mergedProps = { ...parameters?.args, ...args, ...story.props };\n if (app?.root?.controller?.viewModel) {\n Object.assign(app.root.controller.viewModel, mergedProps);\n }\n }\n\n // Return cleanup fn\n return async () => {\n await teardown(rootElement);\n };\n}\n\nexport function createAureliaApp(\n story: AureliaStoryResult,\n args: Record<string, any>,\n domElement: HTMLElement,\n component?: Constructable\n) {\n const aurelia = new Aurelia(story.container);\n\n if (story.items?.length) {\n aurelia.register(...story.items);\n }\n\n if (story.components?.length) {\n aurelia.register(...story.components);\n }\n\n let { template } = story;\n\n if (component) {\n template = template ?? createComponentTemplate(component, story.innerHtml);\n aurelia.register(component);\n }\n\n const App = CustomElement.define(\n {\n name: 'sb-app',\n template,\n containerless: true,\n },\n class {}\n );\n\n const app = Object.assign(new App(), args);\n\n return aurelia.app({\n host: domElement,\n component: app,\n });\n}\n\nexport function createComponentTemplate(\n component: Constructable,\n innerHtml?: string\n): string {\n const def = CustomElement.getDefinition(component);\n\n return `<${def.name} ${Object.values(def.bindables)\n .map((bindable) => `${bindable.attribute}.bind=\"${bindable.name}\"`)\n .join(' ')}>${innerHtml ?? ''}</${def.name}>`;\n}"],"names":[],"mappings":";;AAeA;AACA,MAAM,MAAM,GAAG,IAAI,GAAG,EAAoB;AAE1C,eAAe,QAAQ,CAAC,OAAoB,EAAA;AAC1C,IAAA,I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E,GAAG;AACf,KAAA,CAAC;AACJ;AAEM,SAAU,uBAAuB,CACrC,SAAwB,EACxB,SAAkB,EAAA;IAElB,MAAM,GAAG,GAAG,aAAa,CAAC,aAAa,CAAC,SAAS,CAAC;AAElD,IAAA,OAAO,CAAA,CAAA,EAAI,GAAG,CAAC,IAAI,CAAA,CAAA,EAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS;AAC/C,SAAA,GAAG,CAAC,CAAC,QAAQ,KAAK,CAAA,EAAG,QAAQ,CAAC,SAAS,CAAA,OAAA,EAAU,QAAQ,CAAC,IAAI,GAAG;AACjE,SAAA,IAAI,CAAC,GAAG,CAAC,CAAA,CAAA,EAAI,SAAS,IAAI,EAAE,CAAA,EAAA,EAAK,GAAG,CAAC,IAAI,CAAA,CAAA,CAAG;AACjD;;;;"}
package/dist/preview.mjs CHANGED
@@ -1,6 +1,84 @@
1
1
  import Aurelia, { CustomElement } from 'aurelia';
2
2
 
3
- function bootstrapAureliaApp(story, args, domElement, component) {
3
+ // Track Aurelia apps for cleanup
4
+ const appMap = new Map();
5
+ async function teardown(element) {
6
+ if (appMap.has(element)) {
7
+ const app = appMap.get(element);
8
+ if (app) {
9
+ await app.stop();
10
+ appMap.delete(element);
11
+ }
12
+ }
13
+ }
14
+ const render = (args, context) => {
15
+ const { id, component: Component } = context;
16
+ if (!Component) {
17
+ throw new Error(`Unable to render story ${id} as the component annotation is missing from the default export`);
18
+ }
19
+ return { Component, props: args, template: '' };
20
+ };
21
+ async function renderToCanvas({ storyFn, title, name, showMain, showError, storyContext, forceRemount, }, canvasElement, bootstrapAppFn) {
22
+ // Store reference to the original storybook root element
23
+ const rootElement = canvasElement;
24
+ // Ensure we have (or create) a single container inside the root where the Aurelia app actually renders
25
+ let hostElement;
26
+ if (rootElement.id === 'storybook-root') {
27
+ hostElement = rootElement.querySelector('.aurelia-story-container');
28
+ if (!hostElement) {
29
+ hostElement = document.createElement('div');
30
+ hostElement.className = 'aurelia-story-container';
31
+ hostElement.style.height = '100%';
32
+ rootElement.appendChild(hostElement);
33
+ }
34
+ }
35
+ else {
36
+ hostElement = rootElement;
37
+ }
38
+ // All app instances are now tracked by the *root* element, ensuring we only ever have one per story iframe
39
+ const appBootstrapFn = bootstrapAppFn ?? createAureliaApp;
40
+ const { parameters, component, args } = storyContext;
41
+ let app = appMap.get(rootElement);
42
+ const story = storyFn();
43
+ // Temporary debug logging
44
+ console.log(`[DEBUG] Story: ${name}, forceRemount: ${forceRemount}, hasExistingApp: ${!!app}, canvasId: ${canvasElement.className}`);
45
+ if (!story) {
46
+ showError({
47
+ title: `Expecting an Aurelia component from the story: "${name}" of "${title}".`,
48
+ description: `
49
+ Did you forget to return the Aurelia component from the story?
50
+ Use "() => ({ template: '<custom-component></custom-component>' })" when defining the story.
51
+ `,
52
+ });
53
+ return () => { };
54
+ }
55
+ showMain();
56
+ if (!app || forceRemount) {
57
+ if (forceRemount && app) {
58
+ await teardown(rootElement);
59
+ app = undefined;
60
+ }
61
+ // Clear container before mounting new app
62
+ hostElement.innerHTML = '';
63
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
64
+ const aureliaApp = appBootstrapFn(story, mergedProps, hostElement, component);
65
+ await aureliaApp.start();
66
+ appMap.set(rootElement, aureliaApp);
67
+ app = aureliaApp;
68
+ }
69
+ else {
70
+ // update existing app props
71
+ const mergedProps = { ...parameters?.args, ...args, ...story.props };
72
+ if (app?.root?.controller?.viewModel) {
73
+ Object.assign(app.root.controller.viewModel, mergedProps);
74
+ }
75
+ }
76
+ // Return cleanup fn
77
+ return async () => {
78
+ await teardown(rootElement);
79
+ };
80
+ }
81
+ function createAureliaApp(story, args, domElement, component) {
4
82
  const aurelia = new Aurelia(story.container);
5
83
  if (story.items?.length) {
6
84
  aurelia.register(...story.items);
@@ -14,7 +92,7 @@ function bootstrapAureliaApp(story, args, domElement, component) {
14
92
  aurelia.register(component);
15
93
  }
16
94
  const App = CustomElement.define({
17
- name: 'au-storybook',
95
+ name: 'sb-app',
18
96
  template,
19
97
  containerless: true,
20
98
  }, class {
@@ -32,41 +110,5 @@ function createComponentTemplate(component, innerHtml) {
32
110
  .join(' ')}>${innerHtml ?? ''}</${def.name}>`;
33
111
  }
34
112
 
35
- // Track the current story's cleanup function
36
- let currentCleanup = null;
37
- const render = (args, context) => {
38
- // Clean up previous story if exists
39
- if (currentCleanup) {
40
- currentCleanup();
41
- currentCleanup = null;
42
- }
43
- // Create a container element
44
- const container = document.createElement('div');
45
- // Get the story function result
46
- const story = context.storyFn();
47
- // Bootstrap Aurelia app immediately
48
- if (story && (story.Component || story.template)) {
49
- const app = bootstrapAureliaApp(story, args, container, story.Component || context.component);
50
- // Start the app asynchronously
51
- const startPromise = app.start();
52
- if (startPromise && typeof startPromise.catch === 'function') {
53
- startPromise.catch((error) => {
54
- console.error('Failed to start Aurelia app:', error);
55
- });
56
- }
57
- // Set cleanup function
58
- currentCleanup = () => {
59
- const stopPromise = app.stop();
60
- if (stopPromise && typeof stopPromise.catch === 'function') {
61
- stopPromise.catch((error) => {
62
- console.error('Failed to stop Aurelia app:', error);
63
- });
64
- }
65
- };
66
- }
67
- // Return the container element immediately
68
- return container;
69
- };
70
-
71
- export { render };
113
+ export { render, renderToCanvas };
72
114
  //# sourceMappingURL=preview.mjs.map