From 8531a17b9534cf4dee54e008a7bae26ce3676b63 Mon Sep 17 00:00:00 2001 From: Felix Feng Date: Wed, 22 Jan 2025 23:14:29 +0800 Subject: [PATCH] fix type --- packages/dnd/src/hooks/useDropNode.ts | 2 +- packages/dnd/src/transforms/onDropNode.ts | 38 +++++++++++++--------- packages/dnd/src/transforms/onHoverNode.ts | 2 +- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/packages/dnd/src/hooks/useDropNode.ts b/packages/dnd/src/hooks/useDropNode.ts index 7933acf68..be00d0588 100644 --- a/packages/dnd/src/hooks/useDropNode.ts +++ b/packages/dnd/src/hooks/useDropNode.ts @@ -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, diff --git a/packages/dnd/src/transforms/onDropNode.ts b/packages/dnd/src/transforms/onDropNode.ts index 8b36e82f7..ea6a266eb 100644 --- a/packages/dnd/src/transforms/onDropNode.ts +++ b/packages/dnd/src/transforms/onDropNode.ts @@ -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'; @@ -24,7 +24,7 @@ export const getDropPath = ( nodeRef, orientation = 'vertical', }: { - dragItem: ElementDragItemNode; + dragItem: DragItemNode; monitor: DropTargetMonitor; } & Pick< UseDropNodeOptions, @@ -41,29 +41,35 @@ export const getDropPath = ( if (!direction) return; - let dragPath = editor.api.findPath(dragItem.element); - - const dragEntry: NodeEntry | undefined = dragPath - ? [dragItem.element, dragPath] - : editor.api.node({ id: dragItem.id, at: [] }); - - if (!dragEntry) return; + let dragEntry: NodeEntry | undefined; + let dropEntry: NodeEntry | 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 | undefined = hoveredPath - ? [element, hoveredPath] - : editor.api.node({ id: element.id as string, at: [] }); + if (!dragPath || !hoveredPath) return; + dragEntry = [dragItem.element, dragPath]; + dropEntry = [element, hoveredPath]; + } else { + dropEntry = editor.api.node({ 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') { diff --git a/packages/dnd/src/transforms/onHoverNode.ts b/packages/dnd/src/transforms/onHoverNode.ts index d110a2d66..7aaab393d 100644 --- a/packages/dnd/src/transforms/onHoverNode.ts +++ b/packages/dnd/src/transforms/onHoverNode.ts @@ -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,