-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data-can-condense initial support (#5757)
- Loading branch information
Showing
9 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { emptyComments, type ElementPath } from 'utopia-shared/src/types' | ||
import { MetadataUtils } from '../core/model/element-metadata-utils' | ||
import { isRight } from '../core/shared/either' | ||
import type { | ||
JSXAttributesEntry, | ||
JSXAttributesPart, | ||
JSXElementChild, | ||
} from '../core/shared/element-template' | ||
import { | ||
isJSXAttributeValue, | ||
isJSXAttributesEntry, | ||
isJSXElement, | ||
jsExpressionValue, | ||
jsxAttributesEntry, | ||
type ElementInstanceMetadataMap, | ||
} from '../core/shared/element-template' | ||
|
||
export const DataCanCondense = 'data-can-condense' | ||
|
||
export function dataCanCondenseFromMetadata( | ||
metadata: ElementInstanceMetadataMap, | ||
path: ElementPath, | ||
): boolean { | ||
const target = MetadataUtils.findElementByElementPath(metadata, path) | ||
return ( | ||
target != null && | ||
isRight(target.element) && | ||
isJSXElement(target.element.value) && | ||
canCondenseJSXElementChild(target.element.value) | ||
) | ||
} | ||
|
||
export function canCondenseJSXElementChild(element: JSXElementChild) { | ||
return ( | ||
isJSXElement(element) && | ||
element.props.some( | ||
(prop) => | ||
isDataCanCondenseProp(prop) && isJSXAttributeValue(prop.value) && prop.value.value === true, | ||
) | ||
) | ||
} | ||
|
||
interface DataCanCondenseProp extends JSXAttributesEntry { | ||
key: typeof DataCanCondense | ||
} | ||
|
||
export function isDataCanCondenseProp(prop: JSXAttributesPart): prop is DataCanCondenseProp { | ||
return isJSXAttributesEntry(prop) && (prop as DataCanCondenseProp).key === 'data-can-condense' | ||
} | ||
|
||
export function dataCanCondenseProp(value: boolean): JSXAttributesEntry { | ||
return jsxAttributesEntry(DataCanCondense, jsExpressionValue(value, emptyComments), emptyComments) | ||
} |