react-cosmos-core 6.0.0-alpha.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/dist/fixture/FixtureContext.d.ts +7 -0
- package/dist/fixture/FixtureContext.js +5 -0
- package/dist/fixture/Viewport.d.ts +8 -0
- package/dist/fixture/Viewport.js +19 -0
- package/dist/fixture/types.d.ts +13 -0
- package/dist/fixture/types.js +1 -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 +35 -0
- package/dist/fixture/useValue/useCurrentValue.d.ts +2 -0
- package/dist/fixture/useValue/useCurrentValue.js +13 -0
- package/dist/fixture/useValue/useSetValue.d.ts +3 -0
- package/dist/fixture/useValue/useSetValue.js +36 -0
- package/dist/fixtureState/classState.d.ts +22 -0
- package/dist/fixtureState/classState.js +40 -0
- package/dist/fixtureState/controls.d.ts +2 -0
- package/dist/fixtureState/controls.js +4 -0
- package/dist/fixtureState/createValues.d.ts +3 -0
- package/dist/fixtureState/createValues.js +39 -0
- package/dist/fixtureState/extendWithValues.d.ts +3 -0
- package/dist/fixtureState/extendWithValues.js +22 -0
- package/dist/fixtureState/props.d.ts +30 -0
- package/dist/fixtureState/props.js +49 -0
- package/dist/fixtureState/shared.d.ts +9 -0
- package/dist/fixtureState/shared.js +30 -0
- package/dist/fixtureState/types.d.ts +59 -0
- package/dist/fixtureState/types.js +1 -0
- package/dist/fixtureTree/createFixtureTree/collapseFixtureDirs.d.ts +2 -0
- package/dist/fixtureTree/createFixtureTree/collapseFixtureDirs.js +33 -0
- package/dist/fixtureTree/createFixtureTree/collapseIndexes.d.ts +2 -0
- package/dist/fixtureTree/createFixtureTree/collapseIndexes.js +15 -0
- package/dist/fixtureTree/createFixtureTree/collapseNamedIndexes.d.ts +2 -0
- package/dist/fixtureTree/createFixtureTree/collapseNamedIndexes.js +30 -0
- package/dist/fixtureTree/createFixtureTree/collapseOuterDirs.d.ts +2 -0
- package/dist/fixtureTree/createFixtureTree/collapseOuterDirs.js +13 -0
- package/dist/fixtureTree/createFixtureTree/createRawFixtureTree.d.ts +3 -0
- package/dist/fixtureTree/createFixtureTree/createRawFixtureTree.js +66 -0
- package/dist/fixtureTree/createFixtureTree/hideFixtureSuffix.d.ts +2 -0
- package/dist/fixtureTree/createFixtureTree/hideFixtureSuffix.js +24 -0
- package/dist/fixtureTree/createFixtureTree/index.d.ts +7 -0
- package/dist/fixtureTree/createFixtureTree/index.js +16 -0
- package/dist/fixtureTree/flattenFixtureTree.d.ts +10 -0
- package/dist/fixtureTree/flattenFixtureTree.js +28 -0
- package/dist/fixtureTree/types.d.ts +11 -0
- package/dist/fixtureTree/types.js +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +34 -0
- package/dist/playground/playgroundUrl.d.ts +13 -0
- package/dist/playground/playgroundUrl.js +36 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/decorateFixtureRefs/index.d.ts +5 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/decorateFixtureRefs/index.js +46 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.d.ts +4 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.js +19 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/index.d.ts +3 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/index.js +13 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/shared.d.ts +18 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/shared.js +14 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/useFixtureState.d.ts +4 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/useFixtureState.js +156 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/useReadClassState.d.ts +4 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/classState/useReadClassState.js +60 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/index.d.ts +7 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/index.js +9 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/props/index.d.ts +3 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/props/index.js +77 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/props/useFixtureProps.d.ts +3 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/props/useFixtureProps.js +70 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/findRelevantElementPaths.d.ts +2 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/findRelevantElementPaths.js +23 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/findElementPaths.d.ts +2 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/findElementPaths.js +22 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/getElementAtPath.d.ts +3 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/getElementAtPath.js +26 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/index.d.ts +11 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/index.js +11 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/setElementAtPath.d.ts +2 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/setElementAtPath.js +35 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/shared.d.ts +2 -0
- package/dist/renderer/FixtureLoader/FixtureCapture/shared/nodeTree/shared.js +6 -0
- package/dist/renderer/FixtureLoader/FixtureLoader.d.ts +52 -0
- package/dist/renderer/FixtureLoader/FixtureLoader.js +226 -0
- package/dist/renderer/FixtureLoader/FixtureProvider.d.ts +18 -0
- package/dist/renderer/FixtureLoader/FixtureProvider.js +18 -0
- package/dist/renderer/FixtureLoader/fixtureHelpers.d.ts +2 -0
- package/dist/renderer/FixtureLoader/fixtureHelpers.js +15 -0
- package/dist/renderer/FixtureLoader/getDecoratedFixtureElement/FixtureElement.d.ts +9 -0
- package/dist/renderer/FixtureLoader/getDecoratedFixtureElement/FixtureElement.js +5 -0
- package/dist/renderer/FixtureLoader/getDecoratedFixtureElement/index.d.ts +2 -0
- package/dist/renderer/FixtureLoader/getDecoratedFixtureElement/index.js +15 -0
- package/dist/renderer/FixtureLoader/postMessage.d.ts +2 -0
- package/dist/renderer/FixtureLoader/postMessage.js +22 -0
- package/dist/renderer/FixtureLoader/webSockets.d.ts +2 -0
- package/dist/renderer/FixtureLoader/webSockets.js +32 -0
- package/dist/renderer/getFixtureList.d.ts +8 -0
- package/dist/renderer/getFixtureList.js +19 -0
- package/dist/renderer/getSortedDecoratorsForFixturePath.d.ts +2 -0
- package/dist/renderer/getSortedDecoratorsForFixturePath.js +25 -0
- package/dist/renderer/isMultiFixture.d.ts +2 -0
- package/dist/renderer/isMultiFixture.js +6 -0
- package/dist/renderer/reactTypes.d.ts +28 -0
- package/dist/renderer/reactTypes.js +1 -0
- package/dist/renderer/registerPlaygroundShortcuts.d.ts +1 -0
- package/dist/renderer/registerPlaygroundShortcuts.js +40 -0
- package/dist/renderer/rendererConfig.d.ts +3 -0
- package/dist/renderer/rendererConfig.js +1 -0
- package/dist/renderer/types.d.ts +69 -0
- package/dist/renderer/types.js +1 -0
- package/dist/server/cosmosPluginConfig.d.ts +16 -0
- package/dist/server/cosmosPluginConfig.js +1 -0
- package/dist/server/serverMessage.d.ts +10 -0
- package/dist/server/serverMessage.js +1 -0
- package/dist/server/socketMessage.d.ts +10 -0
- package/dist/server/socketMessage.js +6 -0
- package/dist/utils/array.d.ts +3 -0
- package/dist/utils/array.js +21 -0
- package/dist/utils/keys.d.ts +6 -0
- package/dist/utils/keys.js +6 -0
- package/dist/utils/react/areNodesEqual.d.ts +2 -0
- package/dist/utils/react/areNodesEqual.js +38 -0
- package/dist/utils/react/getComponentName.d.ts +2 -0
- package/dist/utils/react/getComponentName.js +11 -0
- package/dist/utils/react/isReactElement.d.ts +2 -0
- package/dist/utils/react/isReactElement.js +4 -0
- package/dist/utils/string.d.ts +5 -0
- package/dist/utils/string.js +9 -0
- package/dist/utils/tree.d.ts +6 -0
- package/dist/utils/tree.js +21 -0
- package/dist/utils/types.d.ts +1 -0
- package/dist/utils/types.js +1 -0
- package/dist/utils/uuid.d.ts +1 -0
- package/dist/utils/uuid.js +6 -0
- package/package.json +15 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { StateUpdater } from '../utils/types.js';
|
|
2
|
+
export type FixtureDecoratorId = string;
|
|
3
|
+
export type FixtureElementId = {
|
|
4
|
+
decoratorId: string;
|
|
5
|
+
elPath: string;
|
|
6
|
+
};
|
|
7
|
+
export type FixtureStateUnserializableValue = {
|
|
8
|
+
type: 'unserializable';
|
|
9
|
+
stringifiedData: string;
|
|
10
|
+
};
|
|
11
|
+
export type PrimitiveData = string | number | boolean | null | undefined;
|
|
12
|
+
export type ObjectData = Record<string, unknown>;
|
|
13
|
+
export type ArrayData = unknown[];
|
|
14
|
+
export type FixtureStateData = PrimitiveData | ObjectData | ArrayData;
|
|
15
|
+
export type FixtureStatePrimitiveValue = {
|
|
16
|
+
type: 'primitive';
|
|
17
|
+
data: PrimitiveData;
|
|
18
|
+
};
|
|
19
|
+
export type FixtureStateObjectValue = {
|
|
20
|
+
type: 'object';
|
|
21
|
+
values: FixtureStateValues;
|
|
22
|
+
};
|
|
23
|
+
export type FixtureStateArrayValue = {
|
|
24
|
+
type: 'array';
|
|
25
|
+
values: FixtureStateValue[];
|
|
26
|
+
};
|
|
27
|
+
export type FixtureStateValue = FixtureStateUnserializableValue | FixtureStatePrimitiveValue | FixtureStateObjectValue | FixtureStateArrayValue;
|
|
28
|
+
export type FixtureStateValues = Record<string, FixtureStateValue>;
|
|
29
|
+
export type FixtureRenderKey = number;
|
|
30
|
+
export type FixtureStateProps = {
|
|
31
|
+
elementId: FixtureElementId;
|
|
32
|
+
values: FixtureStateValues;
|
|
33
|
+
renderKey: FixtureRenderKey;
|
|
34
|
+
componentName: string;
|
|
35
|
+
};
|
|
36
|
+
export type FixtureStateClassState = {
|
|
37
|
+
elementId: FixtureElementId;
|
|
38
|
+
values: FixtureStateValues;
|
|
39
|
+
componentName: string;
|
|
40
|
+
};
|
|
41
|
+
export type FixtureStateStandardControl = {
|
|
42
|
+
type: 'standard';
|
|
43
|
+
defaultValue: FixtureStateValue;
|
|
44
|
+
currentValue: FixtureStateValue;
|
|
45
|
+
};
|
|
46
|
+
export type FixtureStateSelectControl = {
|
|
47
|
+
type: 'select';
|
|
48
|
+
options: string[];
|
|
49
|
+
defaultValue: string;
|
|
50
|
+
currentValue: string;
|
|
51
|
+
};
|
|
52
|
+
export type FixtureStateControl = FixtureStateStandardControl | FixtureStateSelectControl;
|
|
53
|
+
export type FixtureStateControls = Record<string, FixtureStateControl>;
|
|
54
|
+
export type FixtureState = {
|
|
55
|
+
props?: FixtureStateProps[];
|
|
56
|
+
classState?: FixtureStateClassState[];
|
|
57
|
+
controls?: FixtureStateControls;
|
|
58
|
+
} & Record<string, unknown>;
|
|
59
|
+
export type SetFixtureState = (update: StateUpdater<FixtureState>) => unknown;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { omit } from 'lodash-es';
|
|
2
|
+
export function collapseFixtureDirs(treeNode, fixturesDir) {
|
|
3
|
+
const { data, children } = treeNode;
|
|
4
|
+
if (data.type !== 'fileDir' || !children)
|
|
5
|
+
return treeNode;
|
|
6
|
+
const collapsableDirNode = children[fixturesDir];
|
|
7
|
+
if (collapsableDirNode && collapsableDirNode.data.type === 'fileDir') {
|
|
8
|
+
const otherChildren = omit(children, fixturesDir);
|
|
9
|
+
const innerChildren = collapsableDirNode.children;
|
|
10
|
+
// Make sure children of the collapsed dir don't overlap with children of
|
|
11
|
+
// the parent dir
|
|
12
|
+
const collapsable = innerChildren &&
|
|
13
|
+
Object.keys(otherChildren).every(childName => !innerChildren[childName]);
|
|
14
|
+
if (collapsable)
|
|
15
|
+
return {
|
|
16
|
+
data: { type: 'fileDir' },
|
|
17
|
+
children: {
|
|
18
|
+
...collapseChildrenFixtureDirs(otherChildren, fixturesDir),
|
|
19
|
+
...innerChildren,
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
...treeNode,
|
|
25
|
+
children: collapseChildrenFixtureDirs(children, fixturesDir),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function collapseChildrenFixtureDirs(children, fixturesDir) {
|
|
29
|
+
return Object.keys(children).reduce((newChildren, childName) => ({
|
|
30
|
+
...newChildren,
|
|
31
|
+
[childName]: collapseFixtureDirs(children[childName], fixturesDir),
|
|
32
|
+
}), {});
|
|
33
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function collapseIndexes(treeNode) {
|
|
2
|
+
const { data, children } = treeNode;
|
|
3
|
+
if (data.type !== 'fileDir' || !children)
|
|
4
|
+
return treeNode;
|
|
5
|
+
const indexChild = Object.keys(children).length === 1 && children.index;
|
|
6
|
+
if (indexChild && indexChild.data.type !== 'fileDir')
|
|
7
|
+
return children.index;
|
|
8
|
+
return {
|
|
9
|
+
...treeNode,
|
|
10
|
+
children: Object.keys(children).reduce((newChildren, childName) => ({
|
|
11
|
+
...newChildren,
|
|
12
|
+
[childName]: collapseIndexes(children[childName]),
|
|
13
|
+
}), {}),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function collapseNamedIndexes(treeNode) {
|
|
2
|
+
const { data, children } = treeNode;
|
|
3
|
+
if (data.type !== 'fileDir' || !children)
|
|
4
|
+
return treeNode;
|
|
5
|
+
return {
|
|
6
|
+
...treeNode,
|
|
7
|
+
children: Object.keys(children).reduce((newChildren, childName) => {
|
|
8
|
+
const childNode = children[childName];
|
|
9
|
+
const next = () => ({
|
|
10
|
+
...newChildren,
|
|
11
|
+
[childName]: collapseNamedIndexes(childNode),
|
|
12
|
+
});
|
|
13
|
+
const grandchildren = childNode.children;
|
|
14
|
+
if (childNode.data.type !== 'fileDir' || !grandchildren)
|
|
15
|
+
return next();
|
|
16
|
+
const grandchildNames = Object.keys(grandchildren);
|
|
17
|
+
if (grandchildNames.length !== 1)
|
|
18
|
+
return next();
|
|
19
|
+
const [firstGrandchildName] = grandchildNames;
|
|
20
|
+
const firstGrandchildNode = grandchildren[firstGrandchildName];
|
|
21
|
+
if (firstGrandchildNode.data.type !== 'fileDir' &&
|
|
22
|
+
noCaseEqual(childName, firstGrandchildName))
|
|
23
|
+
return { ...newChildren, [firstGrandchildName]: firstGrandchildNode };
|
|
24
|
+
return next();
|
|
25
|
+
}, {}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function noCaseEqual(a, b) {
|
|
29
|
+
return a.toUpperCase() === b.toUpperCase();
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function collapseOuterDirs(treeNode) {
|
|
2
|
+
const { children } = treeNode;
|
|
3
|
+
if (!children)
|
|
4
|
+
return treeNode;
|
|
5
|
+
const childNames = Object.keys(children);
|
|
6
|
+
const containsSingleChild = Object.keys(children).length === 1;
|
|
7
|
+
if (containsSingleChild) {
|
|
8
|
+
const singleChildNode = children[childNames[0]];
|
|
9
|
+
if (singleChildNode.data.type === 'fileDir')
|
|
10
|
+
return collapseOuterDirs(singleChildNode);
|
|
11
|
+
}
|
|
12
|
+
return treeNode;
|
|
13
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { get, set } from 'lodash-es';
|
|
2
|
+
import { addTreeNodeChild } from '../../utils/tree.js';
|
|
3
|
+
export function createRawFixtureTree(fixtures) {
|
|
4
|
+
const rootNode = {
|
|
5
|
+
data: { type: 'fileDir' },
|
|
6
|
+
};
|
|
7
|
+
Object.keys(fixtures).forEach(fixturePath => addFixturePathToTree(rootNode, fixturePath, fixtures[fixturePath]));
|
|
8
|
+
return rootNode;
|
|
9
|
+
}
|
|
10
|
+
function addFixturePathToTree(rootNode, fixturePath, fixtureItem) {
|
|
11
|
+
const { parents, fileName } = parseFixturePath(fixturePath);
|
|
12
|
+
if (fixtureItem.type == 'single') {
|
|
13
|
+
injectNode(rootNode, parents, fileName, {
|
|
14
|
+
data: {
|
|
15
|
+
type: 'fixture',
|
|
16
|
+
fixtureId: { path: fixturePath },
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
else if (fixtureItem.type == 'multi') {
|
|
21
|
+
injectNode(rootNode, parents, fileName, {
|
|
22
|
+
data: {
|
|
23
|
+
type: 'multiFixture',
|
|
24
|
+
fixtureIds: createFixtureIds(fixturePath, fixtureItem.fixtureNames),
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function parseFixturePath(fixturePath) {
|
|
30
|
+
const parents = fixturePath.split('/');
|
|
31
|
+
const rawFixtureName = parents.pop();
|
|
32
|
+
if (!rawFixtureName)
|
|
33
|
+
throw new Error('Fixture name is empty');
|
|
34
|
+
return {
|
|
35
|
+
parents,
|
|
36
|
+
fileName: removeFixtureNameExtension(rawFixtureName),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function removeFixtureNameExtension(fixtureName) {
|
|
40
|
+
return fixtureName.replace(/\.(j|t)sx?$/, '');
|
|
41
|
+
}
|
|
42
|
+
function createFixtureIds(fixturePath, fixtureNames) {
|
|
43
|
+
return fixtureNames.reduce((fixtureIds, fixtureName) => ({
|
|
44
|
+
...fixtureIds,
|
|
45
|
+
[fixtureName]: { path: fixturePath, name: fixtureName },
|
|
46
|
+
}), {});
|
|
47
|
+
}
|
|
48
|
+
function injectNode(rootNode, parents, childName, childNode) {
|
|
49
|
+
if (parents.length === 0)
|
|
50
|
+
return addTreeNodeChild(rootNode, childName, childNode);
|
|
51
|
+
let curParentDepth = 1;
|
|
52
|
+
let curParent;
|
|
53
|
+
do {
|
|
54
|
+
const curParents = parents.slice(0, curParentDepth);
|
|
55
|
+
const curPath = curParents.map(p => `children["${p}"]`).join('.');
|
|
56
|
+
curParent = get(rootNode, curPath);
|
|
57
|
+
if (!curParent) {
|
|
58
|
+
curParent = {
|
|
59
|
+
data: { type: 'fileDir' },
|
|
60
|
+
};
|
|
61
|
+
set(rootNode, curPath, curParent);
|
|
62
|
+
}
|
|
63
|
+
curParentDepth += 1;
|
|
64
|
+
} while (curParentDepth <= parents.length);
|
|
65
|
+
addTreeNodeChild(curParent, childName, childNode);
|
|
66
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function hideFixtureSuffix(treeNode, suffix) {
|
|
2
|
+
const { children } = treeNode;
|
|
3
|
+
if (!children)
|
|
4
|
+
return treeNode;
|
|
5
|
+
return {
|
|
6
|
+
...treeNode,
|
|
7
|
+
children: Object.keys(children).reduce((newChildren, childName) => {
|
|
8
|
+
const childNode = children[childName];
|
|
9
|
+
if (childNode.data.type === 'fileDir')
|
|
10
|
+
return {
|
|
11
|
+
...newChildren,
|
|
12
|
+
[childName]: hideFixtureSuffix(childNode, suffix),
|
|
13
|
+
};
|
|
14
|
+
const cleanFixtureName = removeFixtureNameSuffix(childName, suffix);
|
|
15
|
+
return {
|
|
16
|
+
...newChildren,
|
|
17
|
+
[cleanFixtureName]: hideFixtureSuffix(childNode, suffix),
|
|
18
|
+
};
|
|
19
|
+
}, {}),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function removeFixtureNameSuffix(fixtureNameWithoutExtension, suffix) {
|
|
23
|
+
return fixtureNameWithoutExtension.replace(new RegExp(`\\.${suffix}$`), '');
|
|
24
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FixtureList } from '../../fixture/types.js';
|
|
2
|
+
import { FixtureTreeNode } from '../types.js';
|
|
3
|
+
export declare function createFixtureTree({ fixtures, fixturesDir, fixtureFileSuffix, }: {
|
|
4
|
+
fixtures: FixtureList;
|
|
5
|
+
fixturesDir: string;
|
|
6
|
+
fixtureFileSuffix: string;
|
|
7
|
+
}): FixtureTreeNode;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { sortTreeChildren } from '../../utils/tree.js';
|
|
2
|
+
import { collapseFixtureDirs } from './collapseFixtureDirs.js';
|
|
3
|
+
import { collapseIndexes } from './collapseIndexes.js';
|
|
4
|
+
import { collapseNamedIndexes } from './collapseNamedIndexes.js';
|
|
5
|
+
import { collapseOuterDirs } from './collapseOuterDirs.js';
|
|
6
|
+
import { createRawFixtureTree } from './createRawFixtureTree.js';
|
|
7
|
+
import { hideFixtureSuffix } from './hideFixtureSuffix.js';
|
|
8
|
+
export function createFixtureTree({ fixtures, fixturesDir, fixtureFileSuffix, }) {
|
|
9
|
+
let tree = createRawFixtureTree(fixtures);
|
|
10
|
+
tree = collapseFixtureDirs(tree, fixturesDir);
|
|
11
|
+
tree = hideFixtureSuffix(tree, fixtureFileSuffix);
|
|
12
|
+
tree = collapseIndexes(tree);
|
|
13
|
+
tree = collapseNamedIndexes(tree);
|
|
14
|
+
tree = collapseOuterDirs(tree);
|
|
15
|
+
return sortTreeChildren(tree);
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { FixtureId } from '../fixture/types.js';
|
|
2
|
+
import { FixtureTreeNode } from './types.js';
|
|
3
|
+
export type FlatFixtureTreeItem = {
|
|
4
|
+
fileName: string;
|
|
5
|
+
fixtureId: FixtureId;
|
|
6
|
+
name: string | null;
|
|
7
|
+
parents: string[];
|
|
8
|
+
};
|
|
9
|
+
export type FlatFixtureTree = FlatFixtureTreeItem[];
|
|
10
|
+
export declare function flattenFixtureTree(treeNode: FixtureTreeNode, parents?: string[]): FlatFixtureTree;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function flattenFixtureTree(treeNode, parents = []) {
|
|
2
|
+
const { data, children } = treeNode;
|
|
3
|
+
if (data.type === 'fixture' || !children)
|
|
4
|
+
return [];
|
|
5
|
+
const flatFixtureTree = [];
|
|
6
|
+
if (children)
|
|
7
|
+
Object.keys(children).forEach(childName => {
|
|
8
|
+
const childNode = children[childName];
|
|
9
|
+
const { data: childData } = childNode;
|
|
10
|
+
if (childData.type === 'fileDir')
|
|
11
|
+
flatFixtureTree.push(...flattenFixtureTree(childNode, [...parents, childName]));
|
|
12
|
+
if (childData.type === 'multiFixture')
|
|
13
|
+
Object.keys(childData.fixtureIds).forEach(fixtureName => flatFixtureTree.push({
|
|
14
|
+
fileName: childName,
|
|
15
|
+
fixtureId: childData.fixtureIds[fixtureName],
|
|
16
|
+
parents,
|
|
17
|
+
name: fixtureName,
|
|
18
|
+
}));
|
|
19
|
+
if (childData.type === 'fixture')
|
|
20
|
+
flatFixtureTree.push({
|
|
21
|
+
fileName: childName,
|
|
22
|
+
fixtureId: childData.fixtureId,
|
|
23
|
+
parents,
|
|
24
|
+
name: null,
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
return flatFixtureTree;
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { FixtureId } from '../fixture/types.js';
|
|
2
|
+
import { TreeNode } from '../utils/tree.js';
|
|
3
|
+
export type FixtureTreeNode = TreeNode<{
|
|
4
|
+
type: 'fileDir';
|
|
5
|
+
} | {
|
|
6
|
+
type: 'fixture';
|
|
7
|
+
fixtureId: FixtureId;
|
|
8
|
+
} | {
|
|
9
|
+
type: 'multiFixture';
|
|
10
|
+
fixtureIds: Record<string, FixtureId>;
|
|
11
|
+
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export * from './fixture/FixtureContext.js';
|
|
2
|
+
export * from './fixture/types.js';
|
|
3
|
+
export * from './fixture/useSelect/index.js';
|
|
4
|
+
export * from './fixture/useValue/index.js';
|
|
5
|
+
export * from './fixture/Viewport.js';
|
|
6
|
+
export * from './fixtureState/classState.js';
|
|
7
|
+
export * from './fixtureState/props.js';
|
|
8
|
+
export * from './fixtureState/types.js';
|
|
9
|
+
export * from './fixtureTree/createFixtureTree/index.js';
|
|
10
|
+
export * from './fixtureTree/flattenFixtureTree.js';
|
|
11
|
+
export * from './fixtureTree/types.js';
|
|
12
|
+
export * from './playground/playgroundUrl.js';
|
|
13
|
+
export * from './renderer/FixtureLoader/FixtureLoader.js';
|
|
14
|
+
export * from './renderer/FixtureLoader/getDecoratedFixtureElement/index.js';
|
|
15
|
+
export * from './renderer/FixtureLoader/postMessage.js';
|
|
16
|
+
export * from './renderer/FixtureLoader/webSockets.js';
|
|
17
|
+
export * from './renderer/getFixtureList.js';
|
|
18
|
+
export * from './renderer/getSortedDecoratorsForFixturePath.js';
|
|
19
|
+
export * from './renderer/reactTypes.js';
|
|
20
|
+
export * from './renderer/registerPlaygroundShortcuts.js';
|
|
21
|
+
export * from './renderer/rendererConfig.js';
|
|
22
|
+
export * from './renderer/types.js';
|
|
23
|
+
export * from './server/cosmosPluginConfig.js';
|
|
24
|
+
export * from './server/serverMessage.js';
|
|
25
|
+
export * from './server/socketMessage.js';
|
|
26
|
+
export * from './utils/array.js';
|
|
27
|
+
export * from './utils/keys.js';
|
|
28
|
+
export * from './utils/react/areNodesEqual.js';
|
|
29
|
+
export * from './utils/react/getComponentName.js';
|
|
30
|
+
export * from './utils/react/isReactElement.js';
|
|
31
|
+
export * from './utils/string.js';
|
|
32
|
+
export * from './utils/tree.js';
|
|
33
|
+
export * from './utils/types.js';
|
|
34
|
+
export * from './utils/uuid.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export * from './fixture/FixtureContext.js';
|
|
2
|
+
export * from './fixture/types.js';
|
|
3
|
+
export * from './fixture/useSelect/index.js';
|
|
4
|
+
export * from './fixture/useValue/index.js';
|
|
5
|
+
export * from './fixture/Viewport.js';
|
|
6
|
+
export * from './fixtureState/classState.js';
|
|
7
|
+
export * from './fixtureState/props.js';
|
|
8
|
+
export * from './fixtureState/types.js';
|
|
9
|
+
export * from './fixtureTree/createFixtureTree/index.js';
|
|
10
|
+
export * from './fixtureTree/flattenFixtureTree.js';
|
|
11
|
+
export * from './fixtureTree/types.js';
|
|
12
|
+
export * from './playground/playgroundUrl.js';
|
|
13
|
+
export * from './renderer/FixtureLoader/FixtureLoader.js';
|
|
14
|
+
export * from './renderer/FixtureLoader/getDecoratedFixtureElement/index.js';
|
|
15
|
+
export * from './renderer/FixtureLoader/postMessage.js';
|
|
16
|
+
export * from './renderer/FixtureLoader/webSockets.js';
|
|
17
|
+
export * from './renderer/getFixtureList.js';
|
|
18
|
+
export * from './renderer/getSortedDecoratorsForFixturePath.js';
|
|
19
|
+
export * from './renderer/reactTypes.js';
|
|
20
|
+
export * from './renderer/registerPlaygroundShortcuts.js';
|
|
21
|
+
export * from './renderer/rendererConfig.js';
|
|
22
|
+
export * from './renderer/types.js';
|
|
23
|
+
export * from './server/cosmosPluginConfig.js';
|
|
24
|
+
export * from './server/serverMessage.js';
|
|
25
|
+
export * from './server/socketMessage.js';
|
|
26
|
+
export * from './utils/array.js';
|
|
27
|
+
export * from './utils/keys.js';
|
|
28
|
+
export * from './utils/react/areNodesEqual.js';
|
|
29
|
+
export * from './utils/react/getComponentName.js';
|
|
30
|
+
export * from './utils/react/isReactElement.js';
|
|
31
|
+
export * from './utils/string.js';
|
|
32
|
+
export * from './utils/tree.js';
|
|
33
|
+
export * from './utils/types.js';
|
|
34
|
+
export * from './utils/uuid.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FixtureId } from '../fixture/types.js';
|
|
2
|
+
export type PlaygroundUrlParams = {
|
|
3
|
+
fixtureId?: FixtureId;
|
|
4
|
+
};
|
|
5
|
+
export type RendererUrlParams = {
|
|
6
|
+
_fixtureId?: FixtureId;
|
|
7
|
+
};
|
|
8
|
+
export declare function stringifyPlaygroundUrlQuery(urlParams: PlaygroundUrlParams): string;
|
|
9
|
+
export declare function parsePlaygroundUrlQuery(query: string): PlaygroundUrlParams;
|
|
10
|
+
export declare function stringifyRendererUrlQuery(urlParams: RendererUrlParams): string;
|
|
11
|
+
export declare function parseRendererUrlQuery(query: string): RendererUrlParams;
|
|
12
|
+
export declare function parseUrlQuery<T extends {}>(query: string): T;
|
|
13
|
+
export declare function stringifyUrlQuery(params: {}): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export function stringifyPlaygroundUrlQuery(urlParams) {
|
|
2
|
+
const encodedUrlParams = {};
|
|
3
|
+
if (urlParams.fixtureId) {
|
|
4
|
+
encodedUrlParams.fixtureId = JSON.stringify(urlParams.fixtureId);
|
|
5
|
+
}
|
|
6
|
+
return stringifyUrlQuery(encodedUrlParams);
|
|
7
|
+
}
|
|
8
|
+
export function parsePlaygroundUrlQuery(query) {
|
|
9
|
+
const encodedUrlParams = parseUrlQuery(query);
|
|
10
|
+
const decoded = {};
|
|
11
|
+
if (encodedUrlParams.fixtureId) {
|
|
12
|
+
decoded.fixtureId = JSON.parse(encodedUrlParams.fixtureId);
|
|
13
|
+
}
|
|
14
|
+
return decoded;
|
|
15
|
+
}
|
|
16
|
+
export function stringifyRendererUrlQuery(urlParams) {
|
|
17
|
+
const encodedUrlParams = {};
|
|
18
|
+
if (urlParams._fixtureId) {
|
|
19
|
+
encodedUrlParams._fixtureId = JSON.stringify(urlParams._fixtureId);
|
|
20
|
+
}
|
|
21
|
+
return stringifyUrlQuery(encodedUrlParams);
|
|
22
|
+
}
|
|
23
|
+
export function parseRendererUrlQuery(query) {
|
|
24
|
+
const encodedUrlParams = parseUrlQuery(query);
|
|
25
|
+
const decoded = {};
|
|
26
|
+
if (encodedUrlParams._fixtureId) {
|
|
27
|
+
decoded._fixtureId = JSON.parse(encodedUrlParams._fixtureId);
|
|
28
|
+
}
|
|
29
|
+
return decoded;
|
|
30
|
+
}
|
|
31
|
+
export function parseUrlQuery(query) {
|
|
32
|
+
return Object.fromEntries(new URLSearchParams(query));
|
|
33
|
+
}
|
|
34
|
+
export function stringifyUrlQuery(params) {
|
|
35
|
+
return new URLSearchParams(params).toString();
|
|
36
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Component, ReactNode } from 'react';
|
|
2
|
+
import { CachedRefHandlers } from '../shared.js';
|
|
3
|
+
type SpyRef = (elPath: string, elRef: null | Component) => unknown;
|
|
4
|
+
export declare function decorateFixtureRefs(fixture: ReactNode, spyRef: SpyRef, cachedRefHandlers: CachedRefHandlers): ReactNode;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { cloneElement, } from 'react';
|
|
2
|
+
import { findRelevantElementPaths } from '../../shared/findRelevantElementPaths.js';
|
|
3
|
+
import { setElementAtPath } from '../../shared/nodeTree/index.js';
|
|
4
|
+
import { isRefSupported } from './isRefSupported.js';
|
|
5
|
+
export function decorateFixtureRefs(fixture, spyRef, cachedRefHandlers) {
|
|
6
|
+
const elPaths = findRelevantElementPaths(fixture);
|
|
7
|
+
return elPaths.reduce((decoratedFixture, elPath) => {
|
|
8
|
+
return setElementAtPath(decoratedFixture, elPath, element => {
|
|
9
|
+
if (!isRefSupported(element.type)) {
|
|
10
|
+
return element;
|
|
11
|
+
}
|
|
12
|
+
return cloneElement(element, {
|
|
13
|
+
ref: getDecoratedRef(element.ref, spyRef, elPath, cachedRefHandlers),
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
}, fixture);
|
|
17
|
+
}
|
|
18
|
+
function getDecoratedRef(origRef, spyRef, elPath, cachedRefHandlers) {
|
|
19
|
+
const found = cachedRefHandlers[elPath];
|
|
20
|
+
if (found && found.origRef === origRef) {
|
|
21
|
+
return found.handler;
|
|
22
|
+
}
|
|
23
|
+
const handler = decorateRefWithSpy(origRef, spyRef, elPath);
|
|
24
|
+
cachedRefHandlers[elPath] = { origRef, handler };
|
|
25
|
+
return handler;
|
|
26
|
+
}
|
|
27
|
+
function decorateRefWithSpy(origRef, spyRef, elPath) {
|
|
28
|
+
return (elRef) => {
|
|
29
|
+
if (origRef) {
|
|
30
|
+
callOriginalRef(origRef, elRef);
|
|
31
|
+
}
|
|
32
|
+
spyRef(elPath, elRef);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function callOriginalRef(ref, elRef) {
|
|
36
|
+
if (typeof ref === 'string') {
|
|
37
|
+
console.warn('[decorateFixtureRefs] String refs are not supported');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (typeof ref === 'function') {
|
|
41
|
+
ref(elRef);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const refObj = ref;
|
|
45
|
+
refObj.current = elRef;
|
|
46
|
+
}
|
package/dist/renderer/FixtureLoader/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ForwardRef } from 'react-is';
|
|
3
|
+
export function isRefSupported(elementType) {
|
|
4
|
+
if (typeof elementType === 'string') {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
const { $$typeof, prototype } = elementType;
|
|
8
|
+
return ($$typeof === ForwardRef ||
|
|
9
|
+
// Warning: Some functions don't have the .prototype property
|
|
10
|
+
(prototype &&
|
|
11
|
+
// ES6 Class
|
|
12
|
+
// Warning: This will return false is the component is extending a
|
|
13
|
+
// different copy of React than the one used by Cosmos. This is relevant
|
|
14
|
+
// when running Cosmos from an external location instead of node_modules.
|
|
15
|
+
(prototype instanceof React.Component ||
|
|
16
|
+
// React.createClass
|
|
17
|
+
prototype.getInitialState !== undefined) &&
|
|
18
|
+
true));
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useFixtureState } from './useFixtureState.js';
|
|
3
|
+
import { useReadClassState } from './useReadClassState.js';
|
|
4
|
+
export function useClassStateCapture(fixture, decoratorId) {
|
|
5
|
+
const elRefs = React.useRef({});
|
|
6
|
+
React.useEffect(() => {
|
|
7
|
+
return () => {
|
|
8
|
+
elRefs.current = {};
|
|
9
|
+
};
|
|
10
|
+
}, []);
|
|
11
|
+
useReadClassState(fixture, decoratorId, elRefs);
|
|
12
|
+
return useFixtureState(fixture, decoratorId, elRefs);
|
|
13
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export type ElRefs = {
|
|
3
|
+
[elPath: string]: React.Component;
|
|
4
|
+
};
|
|
5
|
+
export type InitialStates = {
|
|
6
|
+
[elPath: string]: {
|
|
7
|
+
type: React.ComponentClass<any>;
|
|
8
|
+
state: {};
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export type CachedRefHandler = {
|
|
12
|
+
origRef: null | React.Ref<any>;
|
|
13
|
+
handler: (elRef: null | React.Component) => unknown;
|
|
14
|
+
};
|
|
15
|
+
export type CachedRefHandlers = {
|
|
16
|
+
[elPath: string]: CachedRefHandler;
|
|
17
|
+
};
|
|
18
|
+
export declare function replaceState(elRef: React.Component, nextState: {}): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isEqual } from 'lodash-es';
|
|
2
|
+
// We need to do this because React doesn't provide a replaceState method
|
|
3
|
+
// (anymore) https://reactjs.org/docs/react-component.html#setstate
|
|
4
|
+
export function replaceState(elRef, nextState) {
|
|
5
|
+
const fullState = resetOriginalKeys(elRef.state, nextState);
|
|
6
|
+
if (!isEqual(fullState, elRef.state)) {
|
|
7
|
+
elRef.setState(fullState);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function resetOriginalKeys(original, current) {
|
|
11
|
+
return Object.keys(original).reduce((result, key) => Object.keys(result).indexOf(key) === -1
|
|
12
|
+
? { ...result, [key]: undefined }
|
|
13
|
+
: result, current);
|
|
14
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { MutableRefObject, ReactNode } from 'react';
|
|
2
|
+
import { FixtureDecoratorId } from '../../../../fixtureState/types.js';
|
|
3
|
+
import { ElRefs } from './shared.js';
|
|
4
|
+
export declare function useFixtureState(fixture: ReactNode, decoratorId: FixtureDecoratorId, elRefs: MutableRefObject<ElRefs>): ReactNode;
|