2023-03-21 02:05:00 +09:00
|
|
|
import { addons } from '@storybook/addons';
|
|
|
|
import { FORCE_REMOUNT } from '@storybook/core-events';
|
2023-03-23 16:44:41 +09:00
|
|
|
import { type Preview, setup } from '@storybook/vue3';
|
2023-03-20 16:13:07 +09:00
|
|
|
import { initialize, mswDecorator } from 'msw-storybook-addon';
|
2023-03-20 14:56:34 +09:00
|
|
|
import locale from './locale';
|
2023-03-20 22:12:11 +09:00
|
|
|
import { commonHandlers, onUnhandledRequest } from './mocks';
|
2023-03-21 02:05:00 +09:00
|
|
|
import themes from './themes';
|
2023-03-19 22:22:14 +09:00
|
|
|
import '../src/style.scss';
|
|
|
|
|
2023-03-23 16:44:41 +09:00
|
|
|
// TODO: HMR が壊れているのを直す
|
|
|
|
import.meta.hot.invalidate();
|
|
|
|
|
|
|
|
const appInitialized = Symbol();
|
|
|
|
|
|
|
|
let moduleInitialized = false;
|
2023-03-21 02:05:00 +09:00
|
|
|
let unobserve = () => {};
|
2023-03-25 12:21:16 +09:00
|
|
|
let misskeyOS = null;
|
2023-03-21 02:05:00 +09:00
|
|
|
|
|
|
|
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 22:12:11 +09:00
|
|
|
initialize({
|
|
|
|
onUnhandledRequest,
|
|
|
|
});
|
2023-03-20 14:56:34 +09:00
|
|
|
localStorage.setItem("locale", JSON.stringify(locale));
|
2023-03-21 02:05:00 +09:00
|
|
|
queueMicrotask(() => {
|
|
|
|
Promise.all([
|
|
|
|
import('../src/components'),
|
|
|
|
import('../src/directives'),
|
|
|
|
import('../src/widgets'),
|
|
|
|
import('../src/scripts/theme'),
|
2023-03-25 12:21:16 +09:00
|
|
|
import('../src/os'),
|
|
|
|
]).then(([{ default: components }, { default: directives }, { default: widgets }, { applyTheme }, os]) => {
|
2023-03-21 02:05:00 +09:00
|
|
|
setup((app) => {
|
2023-03-23 16:44:41 +09:00
|
|
|
moduleInitialized = true;
|
|
|
|
if (app[appInitialized]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
app[appInitialized] = true;
|
2023-03-21 02:05:00 +09:00
|
|
|
loadTheme(applyTheme);
|
|
|
|
components(app);
|
|
|
|
directives(app);
|
|
|
|
widgets(app);
|
2023-03-25 12:21:16 +09:00
|
|
|
misskeyOS = os;
|
2023-03-21 02:05:00 +09:00
|
|
|
});
|
2023-03-20 14:56:34 +09:00
|
|
|
});
|
2023-03-20 16:13:07 +09:00
|
|
|
});
|
2023-03-19 22:22:14 +09:00
|
|
|
|
|
|
|
const preview = {
|
2023-03-20 16:13:07 +09:00
|
|
|
decorators: [
|
2023-03-21 02:05:00 +09:00
|
|
|
(Story, context) => {
|
|
|
|
const story = Story();
|
2023-03-23 16:44:41 +09:00
|
|
|
if (!moduleInitialized) {
|
2023-03-21 02:05:00 +09:00
|
|
|
const channel = addons.getChannel();
|
2023-03-21 11:58:58 +09:00
|
|
|
(globalThis.requestIdleCallback || setTimeout)(() => {
|
2023-03-21 02:05:00 +09:00
|
|
|
channel.emit(FORCE_REMOUNT, { storyId: context.id });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return story;
|
2023-03-23 16:44:41 +09:00
|
|
|
},
|
|
|
|
mswDecorator,
|
2023-03-25 12:21:16 +09:00
|
|
|
(Story, context) => {
|
|
|
|
return {
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
context,
|
|
|
|
popups: misskeyOS.popups,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
template: '<div><component :is="popup.component" v-for="popup in popups" :key="popup.id" v-bind="popup.props" v-on="popup.events"/><story /></div>',
|
|
|
|
};
|
|
|
|
},
|
2023-03-20 16:13:07 +09:00
|
|
|
],
|
2023-03-20 22:12:11 +09:00
|
|
|
parameters: {
|
|
|
|
msw: {
|
|
|
|
handlers: commonHandlers,
|
|
|
|
},
|
|
|
|
},
|
2023-03-19 22:22:14 +09:00
|
|
|
} satisfies Preview;
|
|
|
|
|
|
|
|
export default preview;
|