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

hotfix: migrate immer #214

Merged
merged 8 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: remove immer from core
  • Loading branch information
prevwong committed Mar 11, 2021
commit 2f252b8af92c09cf21a3c57cc4a505094c84a351
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@craftjs/utils": "^0.2.0-alpha.17",
"@types/react": "^16.9.11",
"debounce": "^1.2.0",
"immer": "^3.1.3",
"lodash": "^4.17.20",
"shortid": "^2.2.15",
"tiny-invariant": "^1.0.6"
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/editor/tests/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { EditorState } from '@craftjs/core';
import { produce } from 'immer';
import mapValues from 'lodash/mapValues';

import { QueryMethods } from '../../editor/query';
Expand All @@ -11,10 +9,12 @@ import {
} from '../../utils/testHelpers';
import { ActionMethods } from '../actions';

const Actions = (state) => (cb) =>
produce<EditorState>(state, (draft) =>
cb(ActionMethods(draft as any, QueryMethods(state) as any))
);
// TODO: create a cleaner way to test Action methods
const Actions = (state) => (cb) => {
const methods = ActionMethods(state as any, QueryMethods(state) as any);
cb(methods);
return state;
};

describe('actions.add', () => {
let state, rootNode;
Expand Down
180 changes: 89 additions & 91 deletions packages/core/src/utils/createNode.ts
Original file line number Diff line number Diff line change
@@ -1,129 +1,127 @@
import { produce } from 'immer';
import React from 'react';

import { getRandomNodeId } from './getRandomNodeId';

import { NodeData, Node, FreshNode } from '../interfaces';
import { Canvas, deprecateCanvasComponent } from '../nodes/Canvas';
import {
defaultElementProps,
Element,
elementPropToNodeData,
} from '../nodes/Element';
import { Node, FreshNode, UserComponentConfig } from '../interfaces';
import { defaultElementProps, Element, elementPropToNodeData } from '../nodes';
import { NodeProvider } from '../nodes/NodeContext';

const getNodeTypeName = (type: any) =>
typeof type == 'string' ? type : (type as any).name;

export function createNode(
newNode: FreshNode,
normalize?: (node: Node) => void
) {
let actualType = newNode.data.type as any;
let id = newNode.id || getRandomNodeId();

return produce({}, (node: Node) => {
node.id = id;
node._hydrationTimestamp = Date.now();

node.data = {
const node: Node = {
...newNode,
id,
_hydrationTimestamp: Date.now(),
data: {
type: actualType,
props: { ...newNode.data.props },
name:
typeof actualType == 'string' ? actualType : (actualType as any).name,
displayName:
typeof actualType == 'string' ? actualType : (actualType as any).name,
custom: {},
name: getNodeTypeName(actualType),
displayName: getNodeTypeName(actualType),
props: {},
custom: newNode.data.custom || {},
parent: null,
isCanvas: false,
hidden: false,
nodes: [],
linkedNodes: {},
...newNode.data,
} as NodeData;

node.related = {};

node.events = {
},
related: {},
events: {
selected: false,
dragged: false,
hovered: false,
};

node.rules = {
},
rules: {
canDrag: () => true,
canDrop: () => true,
canMoveIn: () => true,
canMoveOut: () => true,
...((actualType.craft && actualType.craft.rules) || {}),
},
dom: null,
};

// @ts-ignore
if (node.data.type === Element) {
const mergedProps = {
...defaultElementProps,
...node.data.props,
};

// @ts-ignore
if (node.data.type === Element || node.data.type === Canvas) {
let usingDeprecatedCanvas = node.data.type === Canvas;
const mergedProps = {
...defaultElementProps,
...node.data.props,
};

Object.keys(defaultElementProps).forEach((key) => {
node.data.props = Object.keys(node.data.props).reduce((props, key) => {
if (Object.keys(defaultElementProps).includes(key)) {
// If a <Element /> specific props is found (ie: "is", "canvas")
// Replace the node.data with the value specified in the prop
node.data[elementPropToNodeData[key] || key] = mergedProps[key];
delete node.data.props[key];
});

actualType = node.data.type;

if (usingDeprecatedCanvas) {
node.data.isCanvas = true;
deprecateCanvasComponent();
} else {
// Otherwise include the props in the node as usual
props[key] = node.data.props[key];
}
}

if (normalize) {
normalize(node);
return props;
}, {});

actualType = node.data.type;
node.data.name = getNodeTypeName(actualType);
node.data.displayName = getNodeTypeName(actualType);
}

if (normalize) {
normalize(node);
}

const userComponentConfig: UserComponentConfig<any> = actualType.craft;
if (userComponentConfig) {
node.data.displayName =
userComponentConfig.displayName || node.data.displayName;

node.data.isCanvas =
userComponentConfig.isCanvas !== undefined &&
userComponentConfig.isCanvas !== null
? userComponentConfig.isCanvas
: node.data.isCanvas;

if (userComponentConfig.rules) {
Object.keys(userComponentConfig.rules).forEach((key) => {
if (['canDrag', 'canDrop', 'canMoveIn', 'canMoveOut'].includes(key)) {
node.rules[key] = userComponentConfig.rules[key];
}
});
}

if (actualType.craft) {
node.data.props = {
...(actualType.craft.props || actualType.craft.defaultProps || {}),
...node.data.props,
};

const displayName = actualType.craft.displayName || actualType.craft.name;
if (displayName) {
node.data.displayName = displayName;
}

if (actualType.craft.isCanvas) {
node.data.isCanvas = node.data.isCanvas || actualType.craft.isCanvas;
}
node.data.props = {
...(userComponentConfig.props || {}),
...node.data.props,
};

if (actualType.craft.rules) {
Object.keys(actualType.craft.rules).forEach((key) => {
if (['canDrag', 'canDrop', 'canMoveIn', 'canMoveOut'].includes(key)) {
node.rules[key] = actualType.craft.rules[key];
}
});
}
node.data.custom = {
...(userComponentConfig.custom || {}),
...node.data.custom,
};

if (actualType.craft.custom) {
node.data.custom = {
...actualType.craft.custom,
...node.data.custom,
};
}
if (userComponentConfig.related) {
const relatedNodeContext = {
id: node.id,
related: true,
};

if (actualType.craft.related) {
node.related = {};
const relatedNodeContext = {
id: node.id,
related: true,
};
Object.keys(actualType.craft.related).forEach((comp) => {
node.related[comp] = () =>
React.createElement(
NodeProvider,
relatedNodeContext,
React.createElement(actualType.craft.related[comp])
);
});
}
Object.keys(userComponentConfig.related).forEach((comp) => {
node.related[comp] = () =>
React.createElement(
NodeProvider,
relatedNodeContext,
React.createElement(userComponentConfig.related[comp])
);
});
}
}) as Node;
}

return node;
}
70 changes: 52 additions & 18 deletions packages/core/src/utils/tests/createNode.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Element } from '../../nodes';
import { createNode } from '../createNode';
import { createTestNode } from '../createTestNode';

Expand All @@ -8,7 +9,7 @@ const expectNode = (node, testData) => {
const match = createTestNode(node.id, {
...testData,
props: isUserComponent
? { ...(type.craft.defaultProps || {}), ...testData.props }
? { ...(type.craft.props || {}), ...testData.props }
: testData.props || {},
custom: isUserComponent ? type.craft.custom : {},
name: typeof type === 'string' ? type : type.name,
Expand Down Expand Up @@ -38,9 +39,10 @@ describe('createNode', () => {
const props = { href: 'href' };

describe('Returns correct type and props', () => {
it('should transform a link correctly', () => {
it('should create a Node object correctly', () => {
const data = {
type: 'a',
parent: null,
props,
};

Expand Down Expand Up @@ -73,29 +75,61 @@ describe('createNode', () => {
},
});
});
describe('When type=Element', () => {
it('should parse Element props as node config', () => {
const testNode = {
data: {
type: Element,
parent: 'ROOT',
props: {
is: 'a',
href: 'craft.js.org',
style: { color: '#fff' },
},
},
};
const node = createNode(testNode);

const { is: type, ...props } = testNode.data.props;

expectNode(node, {
type,
props,
parent: 'ROOT',
});
});
});
describe('when a User Component is passed', () => {
const Component = () => {};
Component.craft = {
custom: {
css: {
background: '#fff',
let Component;

beforeEach(() => {
Component = () => {
return null;
};

Component.craft = {
custom: {
css: {
background: '#fff',
},
},
},
rules: {
canMoveIn: () => false,
},
defaultProps: {
text: '#000',
},
related: {
settings: () => {},
},
};
rules: {
canMoveIn: () => false,
},
props: {
text: '#000',
},
related: {
settings: () => {},
},
};
});

it('should return node with correct type and user component config', () => {
const data = {
type: Component,
parent: null,
props: {},
};

const node = createNode({
Expand Down