@muban/storybook
Version:
Storybook for Muban: View and Test Muban components.
60 lines (59 loc) • 3.34 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/* eslint-disable no-param-reassign */
import { createApp } from '@muban/muban';
import { renderClientTemplate } from './render/renderClientTemplate.js';
import { renderServerTemplate } from './render/renderServerTemplate.js';
import { applyActionArgs } from './render/utils/applyActionArgs.js';
import { validateServerConfig } from './render/utils/validateServerConfig.js';
import { parseServerConfig } from './utils/parseServerConfig.js';
// Unsure what's done here, how it's used, when it's called
export const render = (args, context) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { id, component: Component } = context;
if (!Component) {
throw new Error(`Unable to render story ${id} as the component annotation is missing from the default export`);
}
if (typeof Component === 'function') {
return Component(args, context);
}
return Component;
};
export function renderToCanvas(options, canvasElement) {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
const { storyFn, showMain, storyContext, storyContext: { args, argTypes, globals }, } = options;
const componentStory = storyFn(args);
const data = ((_a = componentStory.data) !== null && _a !== void 0 ? _a : args) || {};
showMain();
// eslint-disable-next-line no-param-reassign
canvasElement.textContent = '';
const container = document.createElement('div');
canvasElement.append(container);
const { renderMode, serverConfig } = parseServerConfig(globals);
if (renderMode === 'server' && !validateServerConfig(options)) {
return;
}
const shouldRenderOnServer = renderMode === 'server' ||
(!renderMode &&
((_b = storyContext.parameters['server']) === null || _b === void 0 ? void 0 : _b.url) &&
((_c = storyContext.parameters['server']) === null || _c === void 0 ? void 0 : _c.id) &&
!((_d = storyContext.parameters['server']) === null || _d === void 0 ? void 0 : _d.disabled));
let app;
if (componentStory.component) {
app = createApp(componentStory.component);
app.component(...((_e = componentStory.appComponents) !== null && _e !== void 0 ? _e : []));
}
const componentInstance = shouldRenderOnServer
? yield renderServerTemplate(app, container, componentStory, data, { serverConfig, options })
: renderClientTemplate(app, container, componentStory, data, { argTypes });
applyActionArgs(componentInstance, argTypes, args);
});
}