2023-03-20 10:05:00 -07:00
|
|
|
import { addons } from '@storybook/addons';
|
|
|
|
import { FORCE_REMOUNT } from '@storybook/core-events';
|
|
|
|
import { type Preview, forceReRender, setup } from '@storybook/vue3';
|
2023-03-20 00:13:07 -07:00
|
|
|
import { initialize, mswDecorator } from 'msw-storybook-addon';
|
2023-03-19 22:56:34 -07:00
|
|
|
import locale from './locale';
|
2023-03-20 06:12:11 -07:00
|
|
|
import { commonHandlers, onUnhandledRequest } from './mocks';
|
2023-03-20 10:05:00 -07:00
|
|
|
import themes from './themes';
|
2023-03-19 06:22:14 -07:00
|
|
|
import '../src/style.scss';
|
|
|
|
|
2023-03-20 10:05:00 -07:00
|
|
|
let initialized = false;
|
|
|
|
let unobserve = () => {};
|
|
|
|
|
|
|
|
function loadTheme(applyTheme: typeof import('../src/scripts/theme')['applyTheme']) {
|
|
|
|
unobserve();
|
|
|
|
const theme = themes[document.documentElement.dataset.misskeyTheme];
|
|
|
|
if (theme) {
|
|
|
|
applyTheme(themes[document.documentElement.dataset.misskeyTheme]);
|
|
|
|
}
|
|
|
|
const observer = new MutationObserver((entries) => {
|
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.attributeName === 'data-misskey-theme') {
|
|
|
|
const target = entry.target as HTMLElement;
|
|
|
|
const theme = themes[target.dataset.misskeyTheme];
|
|
|
|
if (theme) {
|
|
|
|
applyTheme(themes[target.dataset.misskeyTheme]);
|
|
|
|
} else {
|
|
|
|
target.removeAttribute('style');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
observer.observe(document.documentElement, {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ['data-misskey-theme'],
|
|
|
|
});
|
|
|
|
unobserve = () => observer.disconnect();
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:12:11 -07:00
|
|
|
initialize({
|
|
|
|
onUnhandledRequest,
|
|
|
|
});
|
2023-03-19 22:56:34 -07:00
|
|
|
localStorage.setItem("locale", JSON.stringify(locale));
|
2023-03-20 10:05:00 -07:00
|
|
|
queueMicrotask(() => {
|
|
|
|
Promise.all([
|
|
|
|
import('../src/components'),
|
|
|
|
import('../src/directives'),
|
|
|
|
import('../src/widgets'),
|
|
|
|
import('../src/scripts/theme'),
|
|
|
|
]).then(([{ default: components }, { default: directives }, { default: widgets }, { applyTheme }]) => {
|
|
|
|
setup((app) => {
|
|
|
|
initialized = true;
|
|
|
|
loadTheme(applyTheme);
|
|
|
|
components(app);
|
|
|
|
directives(app);
|
|
|
|
widgets(app);
|
|
|
|
});
|
2023-03-19 22:56:34 -07:00
|
|
|
});
|
2023-03-20 00:13:07 -07:00
|
|
|
});
|
2023-03-19 06:22:14 -07:00
|
|
|
|
|
|
|
const preview = {
|
2023-03-20 00:13:07 -07:00
|
|
|
decorators: [
|
|
|
|
mswDecorator,
|
2023-03-20 10:05:00 -07:00
|
|
|
(Story, context) => {
|
|
|
|
const story = Story();
|
|
|
|
if (!initialized) {
|
|
|
|
const channel = addons.getChannel();
|
|
|
|
requestIdleCallback(() => {
|
|
|
|
channel.emit(FORCE_REMOUNT, { storyId: context.id });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return story;
|
|
|
|
}
|
2023-03-20 00:13:07 -07:00
|
|
|
],
|
2023-03-20 06:12:11 -07:00
|
|
|
parameters: {
|
|
|
|
msw: {
|
|
|
|
handlers: commonHandlers,
|
|
|
|
},
|
|
|
|
},
|
2023-03-19 06:22:14 -07:00
|
|
|
} satisfies Preview;
|
|
|
|
|
|
|
|
export default preview;
|