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

Fix dnd from file system #4020

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/wet-doors-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-dnd': patch
---

Fix dnd from file system
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,
element,
monitor,
nodeRef,
Expand Down
39 changes: 25 additions & 14 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,28 +41,38 @@ export const getDropPath = (

if (!direction) return;

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

if (!dragPath) return;

const dragEntry: NodeEntry<TElement> = [dragItem.element, dragPath];
let dragEntry: NodeEntry<TElement> | undefined;
let dropEntry: NodeEntry<TElement> | undefined;

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

if (!hoveredPath) return;

const dropEntry: NodeEntry<TElement> = [element, hoveredPath];
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 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') {
if (dragPath && (direction === 'bottom' || direction === 'right')) {
// Insert after hovered node
dropPath = hoveredPath;

Expand All @@ -74,11 +84,12 @@ export const getDropPath = (
dropPath = [...hoveredPath.slice(0, -1), hoveredPath.at(-1)! - 1];

// If the dragged node is already right before hovered node, no change
if (PathApi.equals(dragPath, dropPath)) return;
if (dragPath && PathApi.equals(dragPath, dropPath)) return;
}

const _dropPath = dropPath as Path;
const before =
dragPath &&
PathApi.isBefore(dragPath, _dropPath) &&
PathApi.isSibling(dragPath, _dropPath);
const to = before ? _dropPath : PathApi.next(_dropPath);
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,
element,
monitor,
nodeRef,
Expand Down
Loading