Skip to content

Commit

Permalink
fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Jan 22, 2025
1 parent d0b7ee9 commit 8531a17
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/dnd/src/hooks/useDropNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const useDropNode = (
if (!(dragItem as ElementDragItemNode).id) {
const result = getDropPath(editor, {
canDropNode,
dragItem: dragItem as any,
dragItem: dragItem,
element,
monitor,
nodeRef,
Expand Down
38 changes: 22 additions & 16 deletions packages/dnd/src/transforms/onDropNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@udecode/plate';

import type { UseDropNodeOptions } from '../hooks';
import type { ElementDragItemNode } from '../types';
import type { DragItemNode, ElementDragItemNode } from '../types';

import { getHoverDirection } from '../utils';

Expand All @@ -24,7 +24,7 @@ export const getDropPath = (
nodeRef,
orientation = 'vertical',
}: {
dragItem: ElementDragItemNode;
dragItem: DragItemNode;
monitor: DropTargetMonitor;
} & Pick<
UseDropNodeOptions,
Expand All @@ -41,29 +41,35 @@ export const getDropPath = (

if (!direction) return;

let dragPath = editor.api.findPath(dragItem.element);

const dragEntry: NodeEntry<TElement> | undefined = dragPath
? [dragItem.element, dragPath]
: editor.api.node<TElement>({ id: dragItem.id, at: [] });

if (!dragEntry) return;
let dragEntry: NodeEntry<TElement> | undefined;
let dropEntry: NodeEntry<TElement> | undefined;

let hoveredPath = editor.api.findPath(element);
if ('element' in dragItem) {
const dragPath = editor.api.findPath(dragItem.element);
const hoveredPath = editor.api.findPath(element);

const dropEntry: NodeEntry<TElement> | undefined = hoveredPath
? [element, hoveredPath]
: editor.api.node<TElement>({ id: element.id as string, at: [] });
if (!dragPath || !hoveredPath) return;

dragEntry = [dragItem.element, dragPath];
dropEntry = [element, hoveredPath];
} else {
dropEntry = editor.api.node<TElement>({ id: element.id as string, at: [] });
}
if (!dropEntry) return;
if (canDropNode && !canDropNode({ dragEntry, dragItem, dropEntry, editor })) {
if (
canDropNode &&
dragEntry &&
!canDropNode({ dragEntry, dragItem, dropEntry, editor })
) {
return;
}

let dropPath: Path | undefined;

if (!dragPath) dragPath = dragEntry[1];
if (!hoveredPath) hoveredPath = dropEntry[1];
// if drag from file system use [] as default path
const dragPath = dragEntry?.[1] ?? [];
const hoveredPath = dropEntry[1];

// Treat 'right' like 'bottom' (after hovered)
// Treat 'left' like 'top' (before hovered)
if (direction === 'bottom' || direction === 'right') {
Expand Down
2 changes: 1 addition & 1 deletion packages/dnd/src/transforms/onHoverNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const onHoverNode = (
// Check if the drop would actually move the node.
const result = getDropPath(editor, {
canDropNode,
dragItem: dragItem as any,
dragItem: dragItem,
element,
monitor,
nodeRef,
Expand Down

0 comments on commit 8531a17

Please sign in to comment.