Skip to content

Commit 32b2212

Browse files
authored
Merge pull request #4530 from udecode/fix/dnd
Fix table dnd
2 parents 2f55e28 + 5bf9ea9 commit 32b2212

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

apps/www/src/registry/ui/block-draggable.tsx

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,16 @@ const DragHandle = React.memo(function DragHandle({
263263
.getApi(BlockSelectionPlugin)
264264
.blockSelection.getNodes({ sort: true });
265265

266-
const selectedBlocks =
266+
let selectedBlocks =
267267
blockSelection.length > 0
268268
? blockSelection
269269
: editor.api.blocks({ mode: 'highest' });
270270

271+
// If current block is not in selection, use it as the starting point
272+
if (!selectedBlocks.some(([node]) => node.id === element.id)) {
273+
selectedBlocks = [[element, editor.api.findPath(element)!]];
274+
}
275+
271276
// Process selection to include list children
272277
const processedBlocks = expandListItemsWithChildren(
273278
editor,
@@ -374,9 +379,39 @@ const createDragPreviewElements = (
374379

375380
const resolveElement = (node: TElement, index: number) => {
376381
const domNode = editor.api.toDOMNode(node)!;
377-
378382
const newDomNode = domNode.cloneNode(true) as HTMLElement;
379383

384+
// Apply visual compensation for horizontal scroll
385+
const applyScrollCompensation = (original: Element, cloned: HTMLElement) => {
386+
const scrollLeft = original.scrollLeft;
387+
if (scrollLeft > 0) {
388+
// Create a wrapper to handle the scroll offset
389+
const scrollWrapper = document.createElement('div');
390+
scrollWrapper.style.overflow = 'hidden';
391+
scrollWrapper.style.width = `${original.clientWidth}px`;
392+
393+
// Create inner container with the full content
394+
const innerContainer = document.createElement('div');
395+
innerContainer.style.transform = `translateX(-${scrollLeft}px)`;
396+
innerContainer.style.width = `${original.scrollWidth}px`;
397+
398+
// Move all children to the inner container
399+
while (cloned.firstChild) {
400+
innerContainer.append(cloned.firstChild);
401+
}
402+
403+
// Apply the original element's styles to maintain appearance
404+
const originalStyles = window.getComputedStyle(original);
405+
cloned.style.padding = '0';
406+
innerContainer.style.padding = originalStyles.padding;
407+
408+
scrollWrapper.append(innerContainer);
409+
cloned.append(scrollWrapper);
410+
}
411+
};
412+
413+
applyScrollCompensation(domNode, newDomNode);
414+
380415
ids.push(node.id as string);
381416
const wrapper = document.createElement('div');
382417
wrapper.append(newDomNode);

docs/components/changelog.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Use the [CLI](https://platejs.org/docs/components/cli) to install the latest ver
1111
## July 2025 #24
1212

1313
### July 29 #24.11
14+
- `block-draggable`: Fixed table drag and drop preview display with horizontal scroll compensation. Drag preview elements now correctly display content even when the original element has horizontal scroll
1415
- `block-draggable`: Added `isAboutToDrag` state to improve preview handling - tracks when drag is about to start (mousedown but not yet dragging) for better preview cleanup
1516

1617
### July 27 #24.10

0 commit comments

Comments
 (0)