Skip to content

Commit

Permalink
REPLACE_JSX_ELEMENT to preserve UID of element-to-be-replaced (#5976)
Browse files Browse the repository at this point in the history
**Problem:**
When we Replace This or Replace Everything a selected element, the
selection is lost.

**Fix:**
Turns out the code was straight up cleaning selected views to [].

**Commit Details:** 
- REPLACE_JSX_ELEMENT no longer changes EditorState.selectedViews
- REPLACE_JSX_ELEMENT now takes the UID of the old element, and sets it
as the UID of the new element
  • Loading branch information
balazsbajorics authored Jun 17, 2024
1 parent 746c839 commit 0e67d44
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions editor/src/components/editor/actions/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ import {
fixUtopiaElement,
generateConsistentUID,
getUtopiaID,
setUtopiaID,
} from '../../../core/shared/uid-utils'
import {
DefaultPostCSSConfig,
Expand Down Expand Up @@ -2358,8 +2359,6 @@ export const UPDATE_FNS = {
}
},
REPLACE_JSX_ELEMENT: (action: ReplaceJSXElement, editor: EditorModel): EditorModel => {
let newSelectedViews: ElementPath[] = []

const withNewElement = modifyUnderlyingParseSuccessOnly(
action.target,
editor,
Expand All @@ -2382,7 +2381,15 @@ export const UPDATE_FNS = {
)

const fixedElement = (() => {
const renamedJsxElement = renameJsxElementChild(action.jsxElement, duplicateNameMapping)
const elemenWithOriginalUid = setUtopiaID(
action.jsxElement,
getUtopiaID(originalElement),
) as JSXElement

const renamedJsxElement = renameJsxElementChild(
elemenWithOriginalUid,
duplicateNameMapping,
)
if (
!isReplaceKeepChildrenAndStyleTarget(action.behaviour) ||
originalElement.type !== 'JSX_ELEMENT'
Expand Down Expand Up @@ -2435,7 +2442,6 @@ export const UPDATE_FNS = {
return {
...withNewElement,
leftMenu: { visible: editor.leftMenu.visible, selectedTab: LeftMenuTab.Navigator },
selectedViews: newSelectedViews,
}
},
REPLACE_MAPPED_ELEMENT: (action: ReplaceMappedElement, editor: EditorModel): EditorModel => {
Expand Down

0 comments on commit 0e67d44

Please sign in to comment.