Skip to content

Commit 3730db6

Browse files
authored
Paste grid cell as sibling (#6516)
**Problem:** Copy-pasting a grid cell ends up pasting it inside the selected element instead of next to it. **Fix:** If the copied element is a grid cell and the target is another grid cell, paste next to it. | Before | After | |------------|-------------| | ![Kapture 2024-10-10 at 16 18 06](https://github.com/user-attachments/assets/ff77dac5-c62b-4a60-b4a2-2e98d2fde8b4) | ![Kapture 2024-10-10 at 16 16 49](https://github.com/user-attachments/assets/326e63f1-3796-4592-b7de-e9b8a5a4a471) | Fixes #6515
1 parent da6bf80 commit 3730db6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

editor/src/components/canvas/canvas-strategies/strategies/reparent-utils.ts

+35
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,36 @@ function pasteNextToSameSizedElement(
538538
return null
539539
}
540540

541+
function pasteIntoNextGridCell(
542+
copyData: ParsedCopyData,
543+
selectedViews: NonEmptyArray<ElementPath>,
544+
metadata: ElementInstanceMetadataMap,
545+
): ReparentTargetForPaste | null {
546+
if (selectedViews.length === 0) {
547+
return null
548+
}
549+
const selectedView = lastOfNonEmptyArray(selectedViews)
550+
551+
// if the copied elements are grid cells and the target is a grid cell, paste next to it
552+
553+
const copyDataElementsAreGridCells = copyData.elementPaste.every(({ originalElementPath }) =>
554+
MetadataUtils.isGridCell(copyData.originalContextMetadata, originalElementPath),
555+
)
556+
if (!copyDataElementsAreGridCells) {
557+
return null
558+
}
559+
560+
const targetIsGridChild = MetadataUtils.isGridCell(metadata, selectedView)
561+
if (!targetIsGridChild) {
562+
return null
563+
}
564+
565+
return {
566+
type: 'parent',
567+
parentPath: childInsertionPath(EP.parentPath(selectedView)),
568+
}
569+
}
570+
541571
function canInsertIntoTarget(
542572
projectContents: ProjectContentTreeRoot,
543573
metadata: ElementInstanceMetadataMap,
@@ -762,6 +792,11 @@ export function getTargetParentForPaste(
762792
return right(pasteNextToSameSizedElementResult)
763793
}
764794

795+
const paseIntoNextGridCellResult = pasteIntoNextGridCell(copyData, selectedViews, metadata)
796+
if (paseIntoNextGridCellResult != null) {
797+
return right(paseIntoNextGridCellResult)
798+
}
799+
765800
const pasteIntoParentOrGrandparentResult = pasteIntoParentOrGrandparent(
766801
copyData.elementPaste.map((e) => e.element),
767802
projectContents,

0 commit comments

Comments
 (0)