Skip to content

Commit

Permalink
simplify scene getter code
Browse files Browse the repository at this point in the history
  • Loading branch information
liady committed Dec 10, 2024
1 parent e2bdbac commit 1b4ff8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ export const TailwindPlugin = (config: Config | null): StylePlugin => ({

const mapping = getTailwindClassMapping(classList.split(' '), config)
const parseTailwindProperty = parseTailwindPropertyFactory(config, {
sceneSize: getContainingSceneSize({
selectedViews: [elementPath],
jsxMetadata: jsxMetadata,
}),
sceneSize: getContainingSceneSize(elementPath, jsxMetadata),
})

return {
Expand Down
27 changes: 18 additions & 9 deletions editor/src/components/canvas/responsive-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ import type { ElementPath } from '../../core/shared/project-file-types'
import type { ElementInstanceMetadataMap } from '../../core/shared/element-template'
import { MetadataUtils } from '../../core/model/element-metadata-utils'
import { noSceneSize, sceneSize, type SceneSize } from './plugins/style-plugins'
import type { EditorState } from '../editor/store/editor-state'

export function getContainingSceneSize({
selectedViews,
jsxMetadata,
}: {
selectedViews: ElementPath[]
jsxMetadata: ElementInstanceMetadataMap
}): SceneSize {
// TODO: support multiple selected elements in different scenes?
const selectedElement = selectedViews.at(0)
export function getContainingSceneSize(
selectedElement: ElementPath,
jsxMetadata: ElementInstanceMetadataMap,
): SceneSize {
if (selectedElement == null) {
return noSceneSize()
}
const containingScene = MetadataUtils.getParentSceneMetadata(jsxMetadata, selectedElement)
return sceneSize(containingScene?.specialSizeMeasurements?.clientWidth)
}

export function getContainingSceneSizeFromEditorState(editor: EditorState): SceneSize {
if (editor == null) {
return noSceneSize()
}
// we're taking the first selected element because we're assuming elements are in the same scene
// TODO: support multiple selected elements that are in different scenes?
const selectedElement = editor.selectedViews.at(0)
if (selectedElement == null) {
return noSceneSize()
}
return getContainingSceneSize(selectedElement, editor.jsxMetadata)
}
9 changes: 3 additions & 6 deletions editor/src/components/inspector/common/property-path-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as PP from '../../../core/shared/property-path'
import * as EP from '../../../core/shared/element-path'

import deepEqual from 'fast-deep-equal'
import * as ObjectPath from 'object-path'
Expand Down Expand Up @@ -92,11 +91,11 @@ import type { EditorAction } from '../../editor/action-types'
import { useDispatch } from '../../editor/store/dispatch-context'
import { eitherRight, fromTypeGuard } from '../../../core/shared/optics/optic-creators'
import { modify } from '../../../core/shared/optics/optic-utilities'
import { getActivePlugin, noSceneSize } from '../../canvas/plugins/style-plugins'
import { getActivePlugin } from '../../canvas/plugins/style-plugins'
import { isStyleInfoKey, type StyleInfo } from '../../canvas/canvas-types'
import { assertNever } from '../../../core/shared/utils'
import { maybeCssPropertyFromInlineStyle } from '../../canvas/commands/utils/property-utils'
import { getContainingSceneSize } from '../../canvas/responsive-utils'
import { getContainingSceneSizeFromEditorState } from '../../canvas/responsive-utils'

export interface InspectorPropsContextData {
selectedViews: Array<ElementPath>
Expand Down Expand Up @@ -764,10 +763,8 @@ export function useGetMultiselectedProps<P extends ParsedPropertiesKeys>(
const styleInfoReaderRef = useRefEditorState(
(store) =>
(props: JSXAttributes, prop: keyof StyleInfo): GetModifiableAttributeResult => {
const containingSceneSize =
store.editor != null ? getContainingSceneSize(store.editor) : noSceneSize()
const elementStyle = getActivePlugin(store.editor).readStyleFromElementProps(props, prop, {
sceneSize: containingSceneSize,
sceneSize: getContainingSceneSizeFromEditorState(store.editor),
})
if (elementStyle == null) {
return right({ type: 'ATTRIBUTE_NOT_FOUND' })
Expand Down

0 comments on commit 1b4ff8c

Please sign in to comment.