|
1 |
| -import { getLayoutProperty } from '../../../core/layout/getLayoutProperty' |
| 1 | +import type { JSXAttributes, PropertyPath } from 'utopia-shared/src/types' |
2 | 2 | import type { StyleLayoutProp } from '../../../core/layout/layout-helpers-new'
|
3 |
| -import { MetadataUtils } from '../../../core/model/element-metadata-utils' |
4 |
| -import { mapDropNulls } from '../../../core/shared/array-utils' |
5 |
| -import { defaultEither, isLeft, mapEither, right } from '../../../core/shared/either' |
6 |
| -import type { JSXElement } from '../../../core/shared/element-template' |
| 3 | +import * as Either from '../../../core/shared/either' |
| 4 | +import { |
| 5 | + getJSXAttributesAtPath, |
| 6 | + jsxSimpleAttributeToValue, |
| 7 | +} from '../../../core/shared/jsx-attribute-utils' |
| 8 | +import type { ModifiableAttribute } from '../../../core/shared/jsx-attributes' |
| 9 | +import { getJSXElementFromProjectContents } from '../../editor/store/editor-state' |
| 10 | +import { cssParsers, type ParsedCSSProperties } from '../../inspector/common/css-utils' |
| 11 | +import { stylePropPathMappingFn } from '../../inspector/common/property-path-hooks' |
| 12 | +import type { CSSStyleProperty } from '../canvas-types' |
7 | 13 | import {
|
8 |
| - emptyComments, |
9 |
| - isJSXElement, |
10 |
| - jsExpressionValue, |
11 |
| -} from '../../../core/shared/element-template' |
| 14 | + cssStyleProperty, |
| 15 | + cssStylePropertyNotParsable, |
| 16 | + cssStylePropertyNotFound, |
| 17 | +} from '../canvas-types' |
| 18 | +import { mapDropNulls } from '../../../core/shared/array-utils' |
| 19 | +import { emptyComments, jsExpressionValue } from '../../../core/shared/element-template' |
12 | 20 | import * as PP from '../../../core/shared/property-path'
|
13 |
| -import { styleStringInArray } from '../../../utils/common-constants' |
14 |
| -import type { ParsedCSSProperties } from '../../inspector/common/css-utils' |
15 |
| -import { withPropertyTag, type WithPropertyTag } from '../canvas-types' |
16 | 21 | import { applyValuesAtPath, deleteValuesAtPath } from '../commands/utils/property-utils'
|
17 | 22 | import type { StylePlugin } from './style-plugins'
|
18 | 23 |
|
| 24 | +function getPropValue(attributes: JSXAttributes, path: PropertyPath): ModifiableAttribute { |
| 25 | + const result = getJSXAttributesAtPath(attributes, path) |
| 26 | + if (result.remainingPath != null) { |
| 27 | + return { type: 'ATTRIBUTE_NOT_FOUND' } |
| 28 | + } |
| 29 | + return result.attribute |
| 30 | +} |
| 31 | + |
19 | 32 | function getPropertyFromInstance<P extends StyleLayoutProp, T = ParsedCSSProperties[P]>(
|
20 | 33 | prop: P,
|
21 |
| - element: JSXElement, |
22 |
| -): WithPropertyTag<T> | null { |
23 |
| - return defaultEither( |
24 |
| - null, |
25 |
| - mapEither(withPropertyTag, getLayoutProperty(prop, right(element.props), styleStringInArray)), |
26 |
| - ) as WithPropertyTag<T> | null |
| 34 | + attributes: JSXAttributes, |
| 35 | +): CSSStyleProperty<NonNullable<T>> | null { |
| 36 | + const attribute = getPropValue(attributes, stylePropPathMappingFn(prop, ['style'])) |
| 37 | + if (attribute.type === 'ATTRIBUTE_NOT_FOUND') { |
| 38 | + return cssStylePropertyNotFound() |
| 39 | + } |
| 40 | + const simpleValue = jsxSimpleAttributeToValue(attribute) |
| 41 | + if (Either.isLeft(simpleValue)) { |
| 42 | + return cssStylePropertyNotParsable() |
| 43 | + } |
| 44 | + const parser = cssParsers[prop] as (value: unknown) => Either.Either<string, T> |
| 45 | + const parsed = parser(simpleValue.value) |
| 46 | + if (Either.isLeft(parsed) || parsed.value == null) { |
| 47 | + return cssStylePropertyNotParsable() |
| 48 | + } |
| 49 | + return cssStyleProperty(parsed.value) |
27 | 50 | }
|
28 | 51 |
|
29 | 52 | export const InlineStylePlugin: StylePlugin = {
|
30 | 53 | name: 'Inline Style',
|
31 | 54 | styleInfoFactory:
|
32 |
| - ({ metadata }) => |
| 55 | + ({ projectContents }) => |
33 | 56 | (elementPath) => {
|
34 |
| - const instance = MetadataUtils.findElementByElementPath(metadata, elementPath) |
35 |
| - if (instance == null || isLeft(instance.element) || !isJSXElement(instance.element.value)) { |
| 57 | + const element = getJSXElementFromProjectContents(elementPath, projectContents) |
| 58 | + if (element == null) { |
36 | 59 | return null
|
37 | 60 | }
|
38 | 61 |
|
39 |
| - const gap = getPropertyFromInstance('gap', instance.element.value) |
40 |
| - const flexDirection = getPropertyFromInstance('flexDirection', instance.element.value) |
| 62 | + const gap = getPropertyFromInstance('gap', element.props) |
| 63 | + const flexDirection = getPropertyFromInstance('flexDirection', element.props) |
41 | 64 |
|
42 | 65 | return {
|
43 | 66 | gap: gap,
|
|
0 commit comments