Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iframed storybook sites #46

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 7 additions & 16 deletions scripts/createChildProcess.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-check

import { spawn } from "node:child_process";
import { spawn } from 'node:child_process';

// Collect all child processes and kill them when this node process exits.
const childProcesses = new Set();
process.on("exit", () => childProcesses.forEach((child) => child.kill()));
process.on('exit', () => childProcesses.forEach((child) => child.kill()));

export function createChildProcess(command, args, options = {}) {
let resolve;
Expand All @@ -18,30 +18,21 @@ export function createChildProcess(command, args, options = {}) {

childProcesses.add(childP);

const stdoutData = [];

childP.stdout?.on("data", (data) => {
stdoutData.push(data);
});

childP.once("error", (err) => {
childP.once('error', (err) => {
reject(err);
});

childP.once("close", (code) => {
childP.once('close', (code) => {
childProcesses.delete(childP);
if (code !== 0) {
reject(new Error(`Command "${command} ${args.join(" ")}" exited with code ${code}`));
reject(new Error(`Command "${command} ${args.join(' ')}" exited with code ${code}`));
return;
}
resolve({
process: childP,
stdout: stdoutData.join("").trim(),
});
resolve();
});

return {
childProcess: childP,
promise,
}
};
}
9 changes: 8 additions & 1 deletion src/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,12 @@ function newKeyStore<T>(): KeyStore<T> {
}

export function createStore<T>(): KeyStore<T> {
return ((window.top as any)._addon_code_editor_store ||= newKeyStore());
try {
return ((window.top as any)._addon_code_editor_store ||= newKeyStore());
} catch {
// Storybook sites can be embedded in iframes. Using window.top will fail in that case.
// Try window.parent as a fallback. This can break if Storybook changes how previews are rendered.
// TODO: Use Storybook Channels to communicate between the manager and preview.
return ((window.parent as any)._addon_code_editor_store ||= newKeyStore());
}
}