-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into main-v4
- Loading branch information
Showing
31 changed files
with
408 additions
and
389 deletions.
There are no files selected for viewing
File renamed without changes.
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
63 changes: 63 additions & 0 deletions
63
scripts/generate-mjml-react-utils/getPropTypeFromMjmlAttributeType.ts
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,63 @@ | ||
export const ATTRIBUTES_TO_USE_CSSProperties_WITH = new Set([ | ||
"color", | ||
"textDecoration", | ||
"textTransform", | ||
|
||
"border", | ||
"borderRadius", | ||
"borderColor", | ||
"borderStyle", | ||
|
||
"backgroundColor", | ||
"backgroundPosition", | ||
"backgroundSize", | ||
]); | ||
|
||
/** | ||
* Converts an mjml type definition into a typescript type definition | ||
* Handles boolean, integer, enum, and | ||
* This is used to generate the types on the React <> mjml binding component. | ||
*/ | ||
export function getPropTypeFromMjmlAttributeType( | ||
attribute: string, | ||
mjmlAttributeType: string | ||
): string { | ||
if (mjmlAttributeType === "boolean") { | ||
return "boolean"; | ||
} | ||
if (mjmlAttributeType === "integer") { | ||
return "number"; | ||
} | ||
// e.g. "vertical-align": "enum(top,bottom,middle)" | ||
if (mjmlAttributeType.startsWith("enum(")) { | ||
return transformEnumType(mjmlAttributeType); | ||
} | ||
if (ATTRIBUTES_TO_USE_CSSProperties_WITH.has(attribute)) { | ||
// When possible prefer using the CSSProperties definitions over the | ||
// less helpful "string" or "string | number" type definitions. | ||
return `React.CSSProperties["${attribute}"]`; | ||
} | ||
if ( | ||
mjmlAttributeType.startsWith("unit") && | ||
mjmlAttributeType.includes("px") | ||
) { | ||
return "string | number"; | ||
} | ||
return "string"; | ||
} | ||
|
||
/** | ||
* Converts an mjml enum type definition into a typescript string literal type. | ||
* Strings like `"enum(a,b,c)"` become `"a" | "b" | "c"`. | ||
* This is used to generate the types on the React <> mjml binding component. | ||
*/ | ||
function transformEnumType(mjmlAttributeType: string): string { | ||
return ( | ||
mjmlAttributeType | ||
.match(/\(.*\)/)?.[0] | ||
?.slice(1, -1) | ||
.split(",") | ||
.map((str) => '"' + str + '"') | ||
.join(" | ") ?? "unknown" | ||
); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.