Skip to content

Commit f22416c

Browse files
committed
readd conver to absolute command
1 parent cd8ea2f commit f22416c

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

editor/src/components/canvas/commands/commands.ts

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type { EditorState, EditorStatePatch } from '../../editor/store/editor-st
66
import type { CommandDescription } from '../canvas-strategies/interaction-state'
77
import type { AdjustCssLengthProperties } from './adjust-css-length-command'
88
import { runAdjustCssLengthProperties } from './adjust-css-length-command'
9+
import type { ConvertToAbsolute } from './convert-to-absolute-command'
10+
import { runConvertToAbsolute } from './convert-to-absolute-command'
911
import type { ReorderElement } from './reorder-element-command'
1012
import { runReorderElement } from './reorder-element-command'
1113
import type { ReparentElement } from './reparent-element-command'
@@ -102,6 +104,7 @@ export type CanvasCommand =
102104
| UpdateSelectedViews
103105
| UpdateHighlightedViews
104106
| SetSnappingGuidelines
107+
| ConvertToAbsolute
105108
| SetCssLengthProperty
106109
| ReorderElement
107110
| ShowOutlineHighlight
@@ -154,6 +157,8 @@ export function runCanvasCommand(
154157
return runUpdateHighlightedViews(editorState, command)
155158
case 'SET_SNAPPING_GUIDELINES':
156159
return runSetSnappingGuidelines(editorState, command)
160+
case 'CONVERT_TO_ABSOLUTE':
161+
return runConvertToAbsolute(editorState, command)
157162
case 'SET_CSS_LENGTH_PROPERTY':
158163
return runSetCssLengthProperty(editorState, command)
159164
case 'REORDER_ELEMENT':
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { styleStringInArray } from '../../../utils/common-constants'
2+
import { createBuiltInDependenciesList } from '../../../core/es-modules/package-manager/built-in-dependencies-list'
3+
import { isRight } from '../../../core/shared/either'
4+
import * as EP from '../../../core/shared/element-path'
5+
import {
6+
getJSXAttributesAtPath,
7+
jsxSimpleAttributeToValue,
8+
} from '../../../core/shared/jsx-attribute-utils'
9+
import { complexDefaultProjectPreParsed } from '../../../sample-projects/sample-project-utils.test-utils'
10+
import { withUnderlyingTargetFromEditorState } from '../../editor/store/editor-state'
11+
import { stylePropPathMappingFn } from '../../inspector/common/property-path-hooks'
12+
import { DefaultStartingFeatureSwitches, renderTestEditorWithModel } from '../ui-jsx.test-utils'
13+
import { updateEditorStateWithPatches } from './commands'
14+
import { convertToAbsolute, runConvertToAbsolute } from './convert-to-absolute-command'
15+
import { isJSXElement } from '../../../core/shared/element-template'
16+
17+
describe('convertToAbsolute', () => {
18+
it('sets position absolute', async () => {
19+
const renderResult = await renderTestEditorWithModel(
20+
complexDefaultProjectPreParsed(),
21+
'dont-await-first-dom-report',
22+
DefaultStartingFeatureSwitches,
23+
createBuiltInDependenciesList(null),
24+
)
25+
26+
const appInstancePath = EP.elementPath([['storyboard-entity', 'scene-1-entity', 'app-entity']])
27+
28+
const originalEditorState = renderResult.getEditorState().editor
29+
30+
const convertToAbsoluteCommand = convertToAbsolute('always', appInstancePath)
31+
const result = runConvertToAbsolute(
32+
renderResult.getEditorState().editor,
33+
convertToAbsoluteCommand,
34+
)
35+
36+
const patchedEditor = updateEditorStateWithPatches(
37+
renderResult.getEditorState().editor,
38+
result.editorStatePatches,
39+
)
40+
41+
const originalPositionProp = withUnderlyingTargetFromEditorState(
42+
appInstancePath,
43+
originalEditorState,
44+
null,
45+
(success, element, underlyingTarget, underlyingFilePath) => {
46+
if (isJSXElement(element)) {
47+
const jsxAttributeResult = getJSXAttributesAtPath(
48+
element.props,
49+
stylePropPathMappingFn('position', styleStringInArray),
50+
)
51+
const currentValue = jsxSimpleAttributeToValue(jsxAttributeResult.attribute)
52+
if (currentValue !== null && isRight(currentValue)) {
53+
return currentValue.value
54+
} else {
55+
return null
56+
}
57+
} else {
58+
return null
59+
}
60+
},
61+
)
62+
63+
const updatedPositionProp = withUnderlyingTargetFromEditorState(
64+
appInstancePath,
65+
patchedEditor,
66+
null,
67+
(success, element, underlyingTarget, underlyingFilePath) => {
68+
if (isJSXElement(element)) {
69+
const jsxAttributeResult = getJSXAttributesAtPath(
70+
element.props,
71+
stylePropPathMappingFn('position', styleStringInArray),
72+
)
73+
const currentValue = jsxSimpleAttributeToValue(jsxAttributeResult.attribute)
74+
if (currentValue !== null && isRight(currentValue)) {
75+
return currentValue.value
76+
} else {
77+
return null
78+
}
79+
} else {
80+
return null
81+
}
82+
},
83+
)
84+
85+
expect(originalPositionProp).toEqual(undefined)
86+
expect(updatedPositionProp).toEqual('absolute')
87+
})
88+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { styleStringInArray } from '../../../utils/common-constants'
2+
import { MetadataUtils } from '../../../core/model/element-metadata-utils'
3+
import * as EP from '../../../core/shared/element-path'
4+
import type { ElementInstanceMetadataMap } from '../../../core/shared/element-template'
5+
import { emptyComments, jsExpressionValue } from '../../../core/shared/element-template'
6+
import type { ValueAtPath } from '../../../core/shared/jsx-attributes'
7+
import type { ElementPath } from '../../../core/shared/project-file-types'
8+
import type { EditorState, EditorStatePatch } from '../../editor/store/editor-state'
9+
import { stylePropPathMappingFn } from '../../inspector/common/property-path-hooks'
10+
import type { BaseCommand, CommandFunction, WhenToRun } from './commands'
11+
import { applyValuesAtPath } from './utils/property-utils'
12+
13+
export interface ConvertToAbsolute extends BaseCommand {
14+
type: 'CONVERT_TO_ABSOLUTE'
15+
target: ElementPath
16+
}
17+
18+
export function convertToAbsolute(transient: WhenToRun, target: ElementPath): ConvertToAbsolute {
19+
return {
20+
type: 'CONVERT_TO_ABSOLUTE',
21+
whenToRun: transient,
22+
target: target,
23+
}
24+
}
25+
26+
export const runConvertToAbsolute: CommandFunction<ConvertToAbsolute> = (
27+
editorState: EditorState,
28+
command: ConvertToAbsolute,
29+
) => {
30+
const propsToUpdate: Array<ValueAtPath> = [
31+
{
32+
path: stylePropPathMappingFn('position', styleStringInArray),
33+
value: jsExpressionValue('absolute', emptyComments),
34+
},
35+
]
36+
37+
const { editorStatePatch: propertyUpdatePatch } = applyValuesAtPath(
38+
editorState,
39+
command.target,
40+
propsToUpdate,
41+
)
42+
43+
const updatedMetadataPatch = addPositionAbsoluteToMetadata(
44+
editorState.jsxMetadata,
45+
command.target,
46+
)
47+
48+
return {
49+
editorStatePatches: [propertyUpdatePatch, updatedMetadataPatch],
50+
commandDescription: 'Switch Position to Absolute',
51+
}
52+
}
53+
54+
function addPositionAbsoluteToMetadata(
55+
metadataMap: ElementInstanceMetadataMap,
56+
target: ElementPath,
57+
): EditorStatePatch {
58+
const existingMetadata = MetadataUtils.findElementByElementPath(metadataMap, target)
59+
if (existingMetadata == null) {
60+
return {}
61+
}
62+
63+
return {
64+
jsxMetadata: {
65+
[EP.toString(target)]: {
66+
specialSizeMeasurements: {
67+
position: { $set: 'absolute' },
68+
},
69+
},
70+
},
71+
}
72+
}

editor/src/components/inspector/inspector-common.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import {
7272
groupConversionCommands,
7373
} from '../canvas/canvas-strategies/strategies/group-conversion-helpers'
7474
import { fixedSizeDimensionHandlingText } from '../text-editor/text-handling'
75+
import { convertToAbsolute } from '../canvas/commands/convert-to-absolute-command'
7576
import { hugPropertiesFromStyleMap } from '../../core/shared/dom-utils'
7677
import { setHugContentForAxis } from './inspector-strategies/hug-contents-strategy'
7778

@@ -1130,7 +1131,7 @@ function addPositionAbsoluteTopLeft(
11301131
parentFlexDirection: FlexDirection | null,
11311132
): Array<CanvasCommand> {
11321133
return [
1133-
setProperty('always', elementPath, styleP('position'), 'absolute'),
1134+
convertToAbsolute('always', elementPath),
11341135
setCssLengthProperty(
11351136
'always',
11361137
elementPath,

0 commit comments

Comments
 (0)