Skip to content

Commit 993a6bf

Browse files
prevwongankri
andauthored
chore: merge improvements from next (#310)
* feat: throw error if component is missing from resolver (#293) * feat: throw error if component is missing from resolver * chore: feedback * debug * feat: improve dnd (#300) * fix: drag shadow (#304) * chore: comment Co-authored-by: Andy Krings-Stern <[email protected]> Co-authored-by: Andy Krings-Stern <[email protected]>
1 parent 5a81740 commit 993a6bf

40 files changed

+1105
-540
lines changed

cypress/support/dnd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ Cypress.Commands.add(
199199
});
200200

201201
if (position === 'inside') {
202-
cy.get('@target').trigger('dragenter', {
202+
cy.get('@target').trigger('dragover', {
203203
clientX: Math.floor(x),
204204
clientY: Math.floor(y),
205205
dataTransfer,
206206
force: true,
207207
});
208208
} else {
209-
cy.get('@parent').trigger('dragenter', {
209+
cy.get('@parent').trigger('dragover', {
210210
clientX: Math.floor(x),
211211
clientY: Math.floor(y),
212212
dataTransfer,

packages/core/src/editor/actions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ERROR_DELETE_TOP_LEVEL_NODE,
99
CallbacksFor,
1010
Delete,
11+
ERROR_NOT_IN_RESOLVER,
1112
} from '@craftjs/utils';
1213
import invariant from 'tiny-invariant';
1314

@@ -48,6 +49,16 @@ const Methods = (
4849
const iterateChildren = (id: NodeId, parentId?: NodeId) => {
4950
const node = tree.nodes[id];
5051

52+
if (typeof node.data.type !== 'string') {
53+
invariant(
54+
state.options.resolver[node.data.name],
55+
ERROR_NOT_IN_RESOLVER.replace(
56+
'%node_type%',
57+
`${(node.data.type as any).name}`
58+
)
59+
);
60+
}
61+
5162
state.nodes[id] = {
5263
...node,
5364
data: {

packages/core/src/editor/query.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export function QueryMethods(state: EditorState) {
4040
return {
4141
/**
4242
* Determine the best possible location to drop the source Node relative to the target Node
43+
*
44+
* TODO: replace with Positioner.computeIndicator();
4345
*/
4446
getDropPlaceholder: (
4547
source: NodeId | Node,
@@ -91,7 +93,7 @@ export function QueryMethods(state: EditorState) {
9193
...dropAction,
9294
currentNode,
9395
},
94-
error: false,
96+
error: null,
9597
};
9698

9799
// If source Node is already in the editor, check if it's draggable
@@ -116,6 +118,10 @@ export function QueryMethods(state: EditorState) {
116118
return options;
117119
},
118120

121+
getNodes() {
122+
return state.nodes;
123+
},
124+
119125
/**
120126
* Helper methods to describe the specified Node
121127
* @param id
@@ -148,7 +154,7 @@ export function QueryMethods(state: EditorState) {
148154
): NodeTree {
149155
let node = parseNodeFromJSX(reactElement, (node, jsx) => {
150156
const name = resolveComponent(state.options.resolver, node.data.type);
151-
invariant(name !== null, ERROR_NOT_IN_RESOLVER);
157+
152158
node.data.displayName = node.data.displayName || name;
153159
node.data.name = name;
154160

packages/core/src/editor/useInternalEditor.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
useCollectorReturnType,
44
QueryCallbacksFor,
55
wrapConnectorHooks,
6-
ChainableConnectors,
6+
EventHandlerConnectors,
77
ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT,
88
} from '@craftjs/utils';
9-
import { useContext, useMemo } from 'react';
9+
import { useContext, useEffect, useMemo } from 'react';
1010
import invariant from 'tiny-invariant';
1111

1212
import { EditorContext } from './EditorContext';
@@ -28,24 +28,36 @@ export type useInternalEditorReturnType<C = null> = useCollectorReturnType<
2828
> & {
2929
inContext: boolean;
3030
store: EditorStore;
31-
connectors: ChainableConnectors<
32-
CoreEventHandlers['connectors'],
33-
React.ReactElement
34-
>;
31+
connectors: EventHandlerConnectors<CoreEventHandlers, React.ReactElement>;
3532
};
3633

3734
export function useInternalEditor<C>(
3835
collector?: EditorCollector<C>
3936
): useInternalEditorReturnType<C> {
37+
const handler = useEventHandler();
4038
const store = useContext(EditorContext);
4139
invariant(store, ERROR_USE_EDITOR_OUTSIDE_OF_EDITOR_CONTEXT);
4240

43-
const handlers = useEventHandler();
4441
const collected = useCollector(store, collector);
4542

43+
const connectorsUsage = useMemo(
44+
() => handler && handler.createConnectorsUsage(),
45+
[handler]
46+
);
47+
48+
useEffect(() => {
49+
return () => {
50+
if (!connectorsUsage) {
51+
return;
52+
}
53+
54+
connectorsUsage.cleanup();
55+
};
56+
}, [connectorsUsage]);
57+
4658
const connectors = useMemo(
47-
() => handlers && wrapConnectorHooks(handlers.connectors),
48-
[handlers]
59+
() => connectorsUsage && wrapConnectorHooks(connectorsUsage.connectors),
60+
[connectorsUsage]
4961
);
5062

5163
return {

0 commit comments

Comments
 (0)