react-cosmos-renderer 6.0.0-canary.6e6c00e.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.
- package/LICENSE +21 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/dist/client.d.ts +8 -0
- package/dist/client.js +8 -0
- package/dist/fixture/FixtureCapture/FixtureCapture.d.ts +8 -0
- package/dist/fixture/FixtureCapture/FixtureCapture.js +17 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/index.d.ts +5 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/index.js +46 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.d.ts +4 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.js +19 -0
- package/dist/fixture/FixtureCapture/classState/index.d.ts +3 -0
- package/dist/fixture/FixtureCapture/classState/index.js +13 -0
- package/dist/fixture/FixtureCapture/classState/shared.d.ts +18 -0
- package/dist/fixture/FixtureCapture/classState/shared.js +14 -0
- package/dist/fixture/FixtureCapture/classState/useFixtureState.d.ts +4 -0
- package/dist/fixture/FixtureCapture/classState/useFixtureState.js +153 -0
- package/dist/fixture/FixtureCapture/classState/useReadClassState.d.ts +4 -0
- package/dist/fixture/FixtureCapture/classState/useReadClassState.js +58 -0
- package/dist/fixture/FixtureCapture/props/index.d.ts +3 -0
- package/dist/fixture/FixtureCapture/props/index.js +74 -0
- package/dist/fixture/FixtureCapture/props/useFixtureProps.d.ts +3 -0
- package/dist/fixture/FixtureCapture/props/useFixtureProps.js +68 -0
- package/dist/fixture/FixtureCapture/shared/findRelevantElementPaths.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/findRelevantElementPaths.js +23 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/findElementPaths.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/findElementPaths.js +22 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/getElementAtPath.d.ts +3 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/getElementAtPath.js +26 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/index.d.ts +11 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/index.js +11 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/setElementAtPath.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/setElementAtPath.js +35 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/shared.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/shared.js +6 -0
- package/dist/fixture/FixtureContext.d.ts +7 -0
- package/dist/fixture/FixtureContext.js +5 -0
- package/dist/fixture/Viewport.d.ts +11 -0
- package/dist/fixture/Viewport.js +15 -0
- package/dist/fixture/useSelect/index.d.ts +2 -0
- package/dist/fixture/useSelect/index.js +11 -0
- package/dist/fixture/useSelect/shared.d.ts +6 -0
- package/dist/fixture/useSelect/shared.js +3 -0
- package/dist/fixture/useSelect/useCreateFixtureState.d.ts +2 -0
- package/dist/fixture/useSelect/useCreateFixtureState.js +32 -0
- package/dist/fixture/useSelect/useCurrentValue.d.ts +2 -0
- package/dist/fixture/useSelect/useCurrentValue.js +11 -0
- package/dist/fixture/useSelect/useSetValue.d.ts +2 -0
- package/dist/fixture/useSelect/useSetValue.js +25 -0
- package/dist/fixture/useValue/index.d.ts +7 -0
- package/dist/fixture/useValue/index.js +9 -0
- package/dist/fixture/useValue/shared.d.ts +3 -0
- package/dist/fixture/useValue/shared.js +1 -0
- package/dist/fixture/useValue/useCreateFixtureState.d.ts +2 -0
- package/dist/fixture/useValue/useCreateFixtureState.js +33 -0
- package/dist/fixture/useValue/useCurrentValue.d.ts +2 -0
- package/dist/fixture/useValue/useCurrentValue.js +12 -0
- package/dist/fixture/useValue/useSetValue.d.ts +3 -0
- package/dist/fixture/useValue/useSetValue.js +34 -0
- package/dist/fixtureLoaders/ClientFixtureLoader.d.ts +11 -0
- package/dist/fixtureLoaders/ClientFixtureLoader.js +17 -0
- package/dist/fixtureLoaders/FixtureListRendererResponse.d.ts +9 -0
- package/dist/fixtureLoaders/FixtureListRendererResponse.js +33 -0
- package/dist/fixtureLoaders/FixtureLoaderConnect.d.ts +12 -0
- package/dist/fixtureLoaders/FixtureLoaderConnect.js +19 -0
- package/dist/fixtureLoaders/ServerFixtureLoader.d.ts +11 -0
- package/dist/fixtureLoaders/ServerFixtureLoader.js +19 -0
- package/dist/fixtureLoaders/defaultRenderMessage.d.ts +2 -0
- package/dist/fixtureLoaders/defaultRenderMessage.js +4 -0
- package/dist/fixtureLoaders/useFixtureSelection.d.ts +7 -0
- package/dist/fixtureLoaders/useFixtureSelection.js +31 -0
- package/dist/fixtureModule/DecoratedFixture.d.ts +9 -0
- package/dist/fixtureModule/DecoratedFixture.js +13 -0
- package/dist/fixtureModule/FixtureModule.d.ts +13 -0
- package/dist/fixtureModule/FixtureModule.js +13 -0
- package/dist/fixtureModule/FixtureProvider.d.ts +10 -0
- package/dist/fixtureModule/FixtureProvider.js +63 -0
- package/dist/fixtureModule/createFixtureNode.d.ts +3 -0
- package/dist/fixtureModule/createFixtureNode.js +19 -0
- package/dist/fixtureModule/decorateFixture.d.ts +3 -0
- package/dist/fixtureModule/decorateFixture.js +4 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/moduleLoaders/AsyncModuleLoader.d.ts +9 -0
- package/dist/moduleLoaders/AsyncModuleLoader.js +14 -0
- package/dist/moduleLoaders/LazyModuleLoader.d.ts +10 -0
- package/dist/moduleLoaders/LazyModuleLoader.js +25 -0
- package/dist/moduleLoaders/StaticModuleLoader.d.ts +10 -0
- package/dist/moduleLoaders/StaticModuleLoader.js +8 -0
- package/dist/moduleLoaders/importLazyFixtureModules.d.ts +5 -0
- package/dist/moduleLoaders/importLazyFixtureModules.js +6 -0
- package/dist/rendererConnect/DomRendererProvider.d.ts +7 -0
- package/dist/rendererConnect/DomRendererProvider.js +52 -0
- package/dist/rendererConnect/NativeRendererProvider.d.ts +7 -0
- package/dist/rendererConnect/NativeRendererProvider.js +13 -0
- package/dist/rendererConnect/RendererContext.d.ts +8 -0
- package/dist/rendererConnect/RendererContext.js +8 -0
- package/dist/rendererConnect/createNoopRendererConnect.d.ts +2 -0
- package/dist/rendererConnect/createNoopRendererConnect.js +8 -0
- package/dist/rendererConnect/createPostMessageConnect.d.ts +2 -0
- package/dist/rendererConnect/createPostMessageConnect.js +22 -0
- package/dist/rendererConnect/createWebSocketsConnect.d.ts +2 -0
- package/dist/rendererConnect/createWebSocketsConnect.js +32 -0
- package/dist/rendererConnect/domRendererId.d.ts +2 -0
- package/dist/rendererConnect/domRendererId.js +16 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +19 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getFixtureFromExport, getFixtureItemFromExport, } from 'react-cosmos-core';
|
|
3
|
+
import { DecoratedFixture } from './DecoratedFixture.js';
|
|
4
|
+
import { FixtureProvider } from './FixtureProvider.js';
|
|
5
|
+
export function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, renderMessage, }) {
|
|
6
|
+
const fixtureItem = React.useMemo(() => getFixtureItemFromExport(fixtureModule.default), [fixtureModule.default]);
|
|
7
|
+
const fixture = getFixtureFromExport(fixtureModule.default, fixtureId.name);
|
|
8
|
+
if (typeof fixture === 'undefined') {
|
|
9
|
+
return renderMessage(`Invalid fixture name: ${fixtureId.name}`);
|
|
10
|
+
}
|
|
11
|
+
return (React.createElement(FixtureProvider, { key: renderKey, fixtureId: fixtureId, initialFixtureState: initialFixtureState, fixtureItem: fixtureItem },
|
|
12
|
+
React.createElement(DecoratedFixture, { fixture: fixture, userDecoratorModules: decoratorModules, globalDecorators: globalDecorators })));
|
|
13
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureId, FixtureListItem, FixtureState } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
fixtureId: FixtureId;
|
|
6
|
+
initialFixtureState?: FixtureState;
|
|
7
|
+
fixtureItem: FixtureListItem;
|
|
8
|
+
};
|
|
9
|
+
export declare function FixtureProvider(props: Props): JSX.Element;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { isEqual } from 'lodash-es';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { FixtureContext } from '../fixture/FixtureContext.js';
|
|
5
|
+
import { RendererContext } from '../rendererConnect/RendererContext.js';
|
|
6
|
+
export function FixtureProvider(props) {
|
|
7
|
+
const [state, setState] = React.useState({
|
|
8
|
+
fixtureState: props.initialFixtureState || {},
|
|
9
|
+
syncedFixtureState: {},
|
|
10
|
+
});
|
|
11
|
+
const { rendererId, rendererConnect } = React.useContext(RendererContext);
|
|
12
|
+
React.useEffect(() => {
|
|
13
|
+
rendererConnect.postMessage({
|
|
14
|
+
type: 'fixtureListItemUpdate',
|
|
15
|
+
payload: {
|
|
16
|
+
rendererId,
|
|
17
|
+
fixturePath: props.fixtureId.path,
|
|
18
|
+
fixtureItem: props.fixtureItem,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}, [props.fixtureId.path, props.fixtureItem, rendererConnect, rendererId]);
|
|
22
|
+
React.useEffect(() => {
|
|
23
|
+
if (!isEqual(state.fixtureState, state.syncedFixtureState)) {
|
|
24
|
+
rendererConnect.postMessage({
|
|
25
|
+
type: 'fixtureStateChange',
|
|
26
|
+
payload: {
|
|
27
|
+
rendererId,
|
|
28
|
+
fixtureId: props.fixtureId,
|
|
29
|
+
fixtureState: state.fixtureState,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
setState(prevState => ({
|
|
33
|
+
...prevState,
|
|
34
|
+
syncedFixtureState: state.fixtureState,
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
}, [
|
|
38
|
+
props.fixtureId,
|
|
39
|
+
rendererConnect,
|
|
40
|
+
rendererId,
|
|
41
|
+
state.fixtureState,
|
|
42
|
+
state.syncedFixtureState,
|
|
43
|
+
]);
|
|
44
|
+
React.useEffect(() => rendererConnect.onMessage(msg => {
|
|
45
|
+
if (msg.type === 'setFixtureState' &&
|
|
46
|
+
msg.payload.rendererId === rendererId) {
|
|
47
|
+
const { fixtureId, fixtureState } = msg.payload;
|
|
48
|
+
setState(prevState =>
|
|
49
|
+
// Ensure fixture state applies to currently selected fixture
|
|
50
|
+
isEqual(fixtureId, props.fixtureId)
|
|
51
|
+
? { ...prevState, fixtureState, syncedFixtureState: fixtureState }
|
|
52
|
+
: prevState);
|
|
53
|
+
}
|
|
54
|
+
}), [props.fixtureId, rendererConnect, rendererId]);
|
|
55
|
+
const setFixtureState = React.useCallback(stateUpdate => {
|
|
56
|
+
setState(prevState => ({
|
|
57
|
+
...prevState,
|
|
58
|
+
fixtureState: stateUpdate(prevState.fixtureState),
|
|
59
|
+
}));
|
|
60
|
+
}, [setState]);
|
|
61
|
+
const contextValue = React.useMemo(() => ({ fixtureState: state.fixtureState, setFixtureState }), [setFixtureState, state.fixtureState]);
|
|
62
|
+
return (React.createElement(FixtureContext.Provider, { value: contextValue }, props.children));
|
|
63
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export function createFixtureNode(fixture) {
|
|
3
|
+
// Warning: In a React Server Components setup this function is called on the
|
|
4
|
+
// server. When a fixture module uses the 'use client' directive, the fixture
|
|
5
|
+
// arg received here will be a function wrapper regardless of the fixture
|
|
6
|
+
// module contents. In this scenario, the fixture module will be automatically
|
|
7
|
+
// rendered on the client side, where React expects a component default export.
|
|
8
|
+
// This results in the following limitation: In a React Server Components
|
|
9
|
+
// setup, Client fixtures have to export a single function component. They
|
|
10
|
+
// can't be multi fixtures and they can't export React elements directly.
|
|
11
|
+
return isFunctionFixture(fixture) ? (React.createElement(FixtureElement, { Component: fixture })) : (fixture);
|
|
12
|
+
}
|
|
13
|
+
function isFunctionFixture(fixture) {
|
|
14
|
+
return typeof fixture === 'function';
|
|
15
|
+
}
|
|
16
|
+
function FixtureElement({ Component }) {
|
|
17
|
+
return React.createElement(Component, null);
|
|
18
|
+
}
|
|
19
|
+
FixtureElement.cosmosCapture = false;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureModules, UserModuleWrappers } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
moduleWrappers: UserModuleWrappers;
|
|
5
|
+
fixturePath: string;
|
|
6
|
+
renderModules: (modules: FixtureModules) => React.ReactElement;
|
|
7
|
+
};
|
|
8
|
+
export declare function AsyncModuleLoader({ moduleWrappers, fixturePath, renderModules, }: Props): Promise<React.ReactElement<any, string | React.JSXElementConstructor<any>>>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
|
|
2
|
+
import { importLazyFixtureModules } from './importLazyFixtureModules.js';
|
|
3
|
+
export async function AsyncModuleLoader({ moduleWrappers, fixturePath, renderModules, }) {
|
|
4
|
+
return renderModules(await getModules(moduleWrappers, fixturePath));
|
|
5
|
+
}
|
|
6
|
+
async function getModules(moduleWrappers, fixturePath) {
|
|
7
|
+
if (moduleWrappers.lazy) {
|
|
8
|
+
return await importLazyFixtureModules(moduleWrappers.fixtures[fixturePath], getSortedDecoratorsForFixturePath(fixturePath, moduleWrappers.decorators));
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
fixtureModule: moduleWrappers.fixtures[fixturePath].module,
|
|
12
|
+
decoratorModules: getSortedDecoratorsForFixturePath(fixturePath, moduleWrappers.decorators).map(d => d.module),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ByPath, FixtureModules, LazyReactDecoratorWrapper, LazyReactFixtureWrapper } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
fixtureWrapper: LazyReactFixtureWrapper;
|
|
5
|
+
decorators: ByPath<LazyReactDecoratorWrapper>;
|
|
6
|
+
fixturePath: string;
|
|
7
|
+
renderModules: (modules: FixtureModules) => React.ReactElement;
|
|
8
|
+
};
|
|
9
|
+
export declare function LazyModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>> | null;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
|
|
3
|
+
import { importLazyFixtureModules } from './importLazyFixtureModules.js';
|
|
4
|
+
export function LazyModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }) {
|
|
5
|
+
const modules = useLazyFixtureModules(fixturePath, fixtureWrapper, decorators);
|
|
6
|
+
return modules && renderModules(modules);
|
|
7
|
+
}
|
|
8
|
+
function useLazyFixtureModules(fixturePath, fixtureWrapper, decoratorWrappers) {
|
|
9
|
+
const [state, setState] = React.useState(null);
|
|
10
|
+
React.useEffect(() => {
|
|
11
|
+
let canceled = false;
|
|
12
|
+
(async () => {
|
|
13
|
+
const modules = await importLazyFixtureModules(fixtureWrapper, getSortedDecoratorsForFixturePath(fixturePath, decoratorWrappers));
|
|
14
|
+
if (!canceled) {
|
|
15
|
+
setState({ fixturePath, modules });
|
|
16
|
+
}
|
|
17
|
+
})();
|
|
18
|
+
return () => {
|
|
19
|
+
canceled = true;
|
|
20
|
+
};
|
|
21
|
+
}, [decoratorWrappers, fixturePath, fixtureWrapper]);
|
|
22
|
+
// Stop returning modules once fixturePath changed to prevent rendering
|
|
23
|
+
// the previous fixture until the new fixture modules are loaded
|
|
24
|
+
return state && state.fixturePath === fixturePath ? state.modules : null;
|
|
25
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ByPath, FixtureModules, ReactDecoratorWrapper, ReactFixtureWrapper } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
fixtureWrapper: ReactFixtureWrapper;
|
|
5
|
+
decorators: ByPath<ReactDecoratorWrapper>;
|
|
6
|
+
fixturePath: string;
|
|
7
|
+
renderModules: (modules: FixtureModules) => React.ReactElement;
|
|
8
|
+
};
|
|
9
|
+
export declare function StaticModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
|
|
3
|
+
export function StaticModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }) {
|
|
4
|
+
return renderModules(React.useMemo(() => ({
|
|
5
|
+
fixtureModule: fixtureWrapper.module,
|
|
6
|
+
decoratorModules: getSortedDecoratorsForFixturePath(fixturePath, decorators).map(d => d.module),
|
|
7
|
+
}), [decorators, fixturePath, fixtureWrapper.module]));
|
|
8
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { LazyReactDecoratorWrapper, LazyReactFixtureWrapper } from 'react-cosmos-core';
|
|
2
|
+
export declare function importLazyFixtureModules(fixtureWrapper: LazyReactFixtureWrapper, decoratorWrappers: LazyReactDecoratorWrapper[]): Promise<{
|
|
3
|
+
fixtureModule: import("react-cosmos-core").ReactFixtureModule;
|
|
4
|
+
decoratorModules: import("react-cosmos-core").ReactDecoratorModule[];
|
|
5
|
+
}>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { isInsideWindowIframe } from 'react-cosmos-core';
|
|
4
|
+
import { RendererContext } from './RendererContext.js';
|
|
5
|
+
import { createNoopRendererConnect } from './createNoopRendererConnect.js';
|
|
6
|
+
import { createPostMessageConnect } from './createPostMessageConnect.js';
|
|
7
|
+
import { createWebSocketsConnect } from './createWebSocketsConnect.js';
|
|
8
|
+
import { getDomRendererId } from './domRendererId.js';
|
|
9
|
+
export function DomRendererProvider({ children, playgroundUrl }) {
|
|
10
|
+
const value = React.useMemo(() => {
|
|
11
|
+
return {
|
|
12
|
+
rendererId: getDomRendererId(),
|
|
13
|
+
rendererConnect: createDomRendererConnect(playgroundUrl),
|
|
14
|
+
};
|
|
15
|
+
}, [playgroundUrl]);
|
|
16
|
+
return (React.createElement(RendererContext.Provider, { value: value },
|
|
17
|
+
children,
|
|
18
|
+
typeof window !== 'undefined' && React.createElement(GlobalErrorHandler, null)));
|
|
19
|
+
}
|
|
20
|
+
function GlobalErrorHandler() {
|
|
21
|
+
const { rendererId, rendererConnect } = React.useContext(RendererContext);
|
|
22
|
+
React.useEffect(() => {
|
|
23
|
+
function handleGlobalError() {
|
|
24
|
+
rendererConnect.postMessage({
|
|
25
|
+
type: 'rendererError',
|
|
26
|
+
payload: { rendererId },
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
// Unhandled errors from async code will not be caught by the error event, but
|
|
30
|
+
// the unhandledrejection event instead.
|
|
31
|
+
window.addEventListener('error', handleGlobalError);
|
|
32
|
+
window.addEventListener('unhandledrejection', handleGlobalError);
|
|
33
|
+
return () => {
|
|
34
|
+
window.removeEventListener('error', handleGlobalError);
|
|
35
|
+
window.removeEventListener('unhandledrejection', handleGlobalError);
|
|
36
|
+
};
|
|
37
|
+
}, [rendererConnect, rendererId]);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function createDomRendererConnect(playgroundUrl) {
|
|
41
|
+
if (typeof window === 'undefined') {
|
|
42
|
+
return createNoopRendererConnect();
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
return isInsideWindowIframe()
|
|
46
|
+
? createPostMessageConnect()
|
|
47
|
+
: createWebSocketsConnect(getWebSocketsUrl(playgroundUrl));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function getWebSocketsUrl(playgroundUrl) {
|
|
51
|
+
return playgroundUrl.replace(/^https?:/, 'ws:');
|
|
52
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RendererContext } from './RendererContext.js';
|
|
3
|
+
import { createWebSocketsConnect } from './createWebSocketsConnect.js';
|
|
4
|
+
export function NativeRendererProvider({ children, socketUrl }) {
|
|
5
|
+
const value = React.useMemo(() => {
|
|
6
|
+
return {
|
|
7
|
+
// TODO: Generate unique ID per device
|
|
8
|
+
rendererId: 'native-renderer',
|
|
9
|
+
rendererConnect: createWebSocketsConnect(socketUrl),
|
|
10
|
+
};
|
|
11
|
+
}, [socketUrl]);
|
|
12
|
+
return (React.createElement(RendererContext.Provider, { value: value }, children));
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RendererConnect } from 'react-cosmos-core';
|
|
3
|
+
type RendererContextValue = {
|
|
4
|
+
rendererId: string;
|
|
5
|
+
rendererConnect: RendererConnect;
|
|
6
|
+
};
|
|
7
|
+
export declare const RendererContext: React.Context<RendererContextValue>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { registerPlaygroundShortcuts, } from 'react-cosmos-core';
|
|
2
|
+
export function createPostMessageConnect() {
|
|
3
|
+
function postMessage(msg) {
|
|
4
|
+
parent.postMessage(msg, '*');
|
|
5
|
+
}
|
|
6
|
+
return {
|
|
7
|
+
postMessage,
|
|
8
|
+
onMessage(onMessage) {
|
|
9
|
+
function handleMessage(msg) {
|
|
10
|
+
onMessage(msg.data);
|
|
11
|
+
}
|
|
12
|
+
window.addEventListener('message', handleMessage, false);
|
|
13
|
+
const removeShortcuts = registerPlaygroundShortcuts(command => {
|
|
14
|
+
postMessage({ type: 'playgroundCommand', payload: { command } });
|
|
15
|
+
});
|
|
16
|
+
return () => {
|
|
17
|
+
window.removeEventListener('message', handleMessage);
|
|
18
|
+
removeShortcuts();
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { rendererSocketMessage, } from 'react-cosmos-core';
|
|
2
|
+
export function createWebSocketsConnect(url) {
|
|
3
|
+
let pendingMessages = [];
|
|
4
|
+
const socket = new WebSocket(url);
|
|
5
|
+
socket.addEventListener('open', () => {
|
|
6
|
+
if (pendingMessages.length > 0) {
|
|
7
|
+
pendingMessages.forEach(msg => socket.send(JSON.stringify(msg)));
|
|
8
|
+
pendingMessages = [];
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
postMessage(rendererResponse) {
|
|
13
|
+
const socketMessage = rendererSocketMessage(rendererResponse);
|
|
14
|
+
if (socket && socket.readyState === WebSocket.OPEN) {
|
|
15
|
+
socket.send(JSON.stringify(socketMessage));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
pendingMessages.push(socketMessage);
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
onMessage(onMessage) {
|
|
22
|
+
function handleMessage(msg) {
|
|
23
|
+
const socketMessage = JSON.parse(msg.data);
|
|
24
|
+
if (socketMessage.channel === 'renderer') {
|
|
25
|
+
onMessage(socketMessage.message);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
socket.addEventListener('message', handleMessage);
|
|
29
|
+
return () => socket.removeEventListener('message', handleMessage);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { uuid } from 'react-cosmos-core';
|
|
2
|
+
// Renderer ID is remembered to avoid announcing a new renderer when reloading
|
|
3
|
+
// the renderer window. Note that each tab has creates a new session and thus
|
|
4
|
+
// a new rendererId.
|
|
5
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
|
|
6
|
+
export function getDomRendererId() {
|
|
7
|
+
if (typeof sessionStorage === 'undefined') {
|
|
8
|
+
return uuid();
|
|
9
|
+
}
|
|
10
|
+
let id = sessionStorage.getItem('cosmosRendererId');
|
|
11
|
+
if (!id) {
|
|
12
|
+
id = uuid();
|
|
13
|
+
sessionStorage.setItem('cosmosRendererId', id);
|
|
14
|
+
}
|
|
15
|
+
return id;
|
|
16
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js';
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-cosmos-renderer",
|
|
3
|
+
"version": "6.0.0-canary.6e6c00e.0+6e6c00e",
|
|
4
|
+
"description": "React Cosmos Renderer",
|
|
5
|
+
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-renderer",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./index.js",
|
|
11
|
+
"./client": "./client.js"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"lodash-es": "^4.17.21",
|
|
15
|
+
"react-cosmos-core": "6.0.0-canary.6e6c00e.0+6e6c00e",
|
|
16
|
+
"react-is": "^18.2.0"
|
|
17
|
+
},
|
|
18
|
+
"gitHead": "6e6c00e973fc0d0ef43e1851492e93b554fd1baa"
|
|
19
|
+
}
|