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

Share target finding logic between paste and one-shot insertion #4369

Merged
merged 12 commits into from
Oct 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import type {
NodeModules,
} from '../../../../core/shared/project-file-types'
import { fixUtopiaElement } from '../../../../core/shared/uid-utils'
import { getTargetParentForPaste } from '../../../../utils/clipboard'
import type { ElementPasteWithMetadata, ReparentTargetForPaste } from '../../../../utils/clipboard'
import type { ElementPasteWithMetadata } from '../../../../utils/clipboard'
import type { IndexPosition } from '../../../../utils/utils'
import { absolute, front } from '../../../../utils/utils'
import type { ProjectContentTreeRoot } from '../../../assets'
Expand All @@ -45,7 +44,6 @@ import {
childInsertionPath,
replaceWithElementsWrappedInFragmentBehaviour,
} from '../../../editor/store/insertion-path'
import type { RemixRoutingTable } from '../../../editor/store/remix-derived-data'
import type { CanvasCommand } from '../../commands/commands'
import { foldAndApplyCommandsInner } from '../../commands/commands'
import { deleteElement } from '../../commands/delete-element-command'
Expand All @@ -64,8 +62,16 @@ import {
} from '../strategies/reparent-helpers/reparent-property-changes'
import { reparentStrategyForPaste } from '../strategies/reparent-helpers/reparent-strategy-helpers'
import type { ReparentStrategy } from '../strategies/reparent-helpers/reparent-strategy-helpers'
import type { ElementToReparent, PathToReparent } from '../strategies/reparent-utils'
import { elementToReparent, getReparentOutcomeMultiselect } from '../strategies/reparent-utils'
import type {
ElementToReparent,
PathToReparent,
ReparentTargetForPaste,
} from '../strategies/reparent-utils'
import {
elementToReparent,
getReparentOutcomeMultiselect,
getTargetParentForPaste,
} from '../strategies/reparent-utils'
import { adjustIntendedCoordinatesForGroups, collectGroupTrueUp } from './navigator-reparent'
import type { PostActionChoice } from './post-action-options'

Expand Down Expand Up @@ -563,12 +569,20 @@ function getTargetParentForPasteHere(
const originalPathTree =
editor.internalClipboard.elements[0].targetOriginalContextElementPathTrees

const storyboardPath = getStoryboardElementPath(
editor.projectContents,
editor.canvas.openFile?.filename ?? null,
)

if (storyboardPath == null) {
return left('No storyboard found')
}

const target = getTargetParentForPaste(
storyboardPath,
editor.projectContents,
editor.selectedViews,
editor.canvas.openFile?.filename ?? null,
editor.jsxMetadata,
editor.pasteTargetsToIgnore,
{
elementPaste: elementToPaste,
originalContextMetadata: originalMetadata,
Expand All @@ -577,15 +591,6 @@ function getTargetParentForPasteHere(
editor.elementPathTree,
)

const storyboardPath = getStoryboardElementPath(
editor.projectContents,
editor.canvas.openFile?.filename ?? null,
)

if (storyboardPath == null) {
return left('No storyboard found')
}

if (isLeft(target)) {
return right({ type: 'parent', parentPath: childInsertionPath(storyboardPath) })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ import {
getConditionalActiveCase,
maybeBranchConditionalCase,
} from '../../../../../core/model/conditionals'
import {
MetadataUtils,
getSimpleAttributeAtPath,
} from '../../../../../core/model/element-metadata-utils'
import { MetadataUtils } from '../../../../../core/model/element-metadata-utils'
import type { Either } from '../../../../../core/shared/either'
import { foldEither, isLeft, left, right } from '../../../../../core/shared/either'
import { foldEither, left, right } from '../../../../../core/shared/either'
import * as EP from '../../../../../core/shared/element-path'
import type {
ElementInstanceMetadata,
Expand All @@ -24,8 +21,9 @@ import {
isJSXElement,
jsExpressionValue,
jsxElementNameEquals,
isIntrinsicElement,
} from '../../../../../core/shared/element-template'
import type { ElementPath, PropertyPath } from '../../../../../core/shared/project-file-types'
import type { ElementPath } from '../../../../../core/shared/project-file-types'
import type { ProjectContentTreeRoot } from '../../../../assets'
import type {
AllElementProps,
Expand All @@ -45,18 +43,14 @@ import { strategyApplicationResult } from '../../canvas-strategy-types'
import * as PP from '../../../../../core/shared/property-path'
import type { ValueAtPath } from '../../../../../core/shared/jsx-attributes'
import { setJSXValuesAtPaths } from '../../../../../core/shared/jsx-attributes'
import type {
ElementPasteWithMetadata,
ReparentTargetForPaste,
} from '../../../../../utils/clipboard'
import type { ElementPasteWithMetadata } from '../../../../../utils/clipboard'
import type { ElementPaste } from '../../../../editor/action-types'
import {
eitherRight,
fromField,
traverseArray,
} from '../../../../../core/shared/optics/optic-creators'
import { modify, set } from '../../../../../core/shared/optics/optic-utilities'
import type { IndexPosition } from '../../../../../utils/utils'
import Utils from '../../../../../utils/utils'
import type { CanvasPoint } from '../../../../../core/shared/math-utils'
import {
Expand All @@ -67,20 +61,16 @@ import {
canvasPoint,
roundTo,
zeroCanvasRect,
zeroRectangle,
zeroRectIfNullOrInfinity,
roundPointToNearestWhole,
} from '../../../../../core/shared/math-utils'
import type { MetadataSnapshots } from './reparent-property-strategies'
import type { BuiltInDependencies } from '../../../../../core/es-modules/package-manager/built-in-dependencies-list'
import type { ElementPathTrees } from '../../../../../core/shared/element-path-tree'
import type { CanvasCommand } from '../../../commands/commands'
import type { ToReparent } from '../reparent-utils'
import type { StaticReparentTarget } from './reparent-strategy-helpers'
import { mapDropNulls } from '../../../../../core/shared/array-utils'
import { treatElementAsFragmentLike } from '../fragment-like-helpers'
import { optionalMap } from '../../../../../core/shared/optional-utils'
import { setProperty } from '../../../commands/set-property-command'
import type { ReparentTargetForPaste } from '../reparent-utils'

export function isAllowedToReparent(
projectContents: ProjectContentTreeRoot,
Expand Down Expand Up @@ -331,40 +321,37 @@ export function getInsertionPathForReparentTarget(
}

function areElementsInstancesOfTheSameComponent(
firstInstance: ElementInstanceMetadata | null,
secondInstance: ElementInstanceMetadata | null,
firstElement: JSXElement,
secondElement: JSXElement,
): boolean {
if (
firstInstance == null ||
secondInstance == null ||
isLeft(firstInstance.element) ||
isLeft(secondInstance.element) ||
!isJSXElement(firstInstance.element.value) ||
!isJSXElement(secondInstance.element.value)
) {
return false
}

return jsxElementNameEquals(firstInstance.element.value.name, secondInstance.element.value.name)
return jsxElementNameEquals(firstElement.name, secondElement.name)
}

export function isElementRenderedBySameComponent(
metadata: ElementInstanceMetadataMap,
targetPath: ElementPath,
instance: ElementInstanceMetadata | null,
element: JSXElement,
): boolean {
if (EP.isEmptyPath(targetPath)) {
return false
}

const currentInstance = MetadataUtils.findElementByElementPath(
if (isIntrinsicElement(element.name)) {
return false
}

const targetElement = MetadataUtils.getJSXElementFromMetadata(
metadata,
EP.getContainingComponent(targetPath),
)

if (targetElement == null) {
return false
}

return (
areElementsInstancesOfTheSameComponent(currentInstance, instance) ||
isElementRenderedBySameComponent(metadata, EP.getContainingComponent(targetPath), instance)
areElementsInstancesOfTheSameComponent(targetElement, element) ||
isElementRenderedBySameComponent(metadata, EP.getContainingComponent(targetPath), element)
)
}

Expand Down
Loading
Loading