@makeswift/runtime 0.21.3 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/api/react.js +45 -0
- package/dist/cjs/api/react.js.map +1 -1
- package/dist/cjs/components/shared/Link/index.js +14 -69
- package/dist/cjs/components/shared/Link/index.js.map +1 -1
- package/dist/cjs/components/utils/responsive-style.js +2 -1
- package/dist/cjs/components/utils/responsive-style.js.map +1 -1
- package/dist/cjs/controls/link.js +46 -2
- package/dist/cjs/controls/link.js.map +1 -1
- package/dist/cjs/controls/rich-text/rich-text.js +12 -0
- package/dist/cjs/controls/rich-text/rich-text.js.map +1 -1
- package/dist/cjs/controls/rich-text-v2/rich-text-v2.js +12 -0
- package/dist/cjs/controls/rich-text-v2/rich-text-v2.js.map +1 -1
- package/dist/cjs/controls/slot.js +12 -0
- package/dist/cjs/controls/slot.js.map +1 -1
- package/dist/cjs/next/api-handler/handlers/manifest.js +1 -1
- package/dist/cjs/next/components/tests/controls/fixtures/rich-text-v2.js +203 -0
- package/dist/cjs/next/components/tests/controls/fixtures/rich-text-v2.js.map +1 -0
- package/dist/cjs/runtimes/react/components/ElementData.js +1 -1
- package/dist/cjs/runtimes/react/components/ElementData.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/control.js +11 -156
- package/dist/cjs/runtimes/react/controls/control.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/rich-text/EditableText/editable-text.js +1 -0
- package/dist/cjs/runtimes/react/controls/rich-text/EditableText/editable-text.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/rich-text/rich-text.js +9 -3
- package/dist/cjs/runtimes/react/controls/rich-text/rich-text.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/rich-text-v2/EditableTextV2/editable-text-v2.js +1 -0
- package/dist/cjs/runtimes/react/controls/rich-text-v2/EditableTextV2/editable-text-v2.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/rich-text-v2/ReadOnlyTextV2.js +2 -2
- package/dist/cjs/runtimes/react/controls/rich-text-v2/ReadOnlyTextV2.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/rich-text-v2/rich-text-v2.js +12 -9
- package/dist/cjs/runtimes/react/controls/rich-text-v2/rich-text-v2.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/slot.js +5 -4
- package/dist/cjs/runtimes/react/controls/slot.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/style.js +6 -47
- package/dist/cjs/runtimes/react/controls/style.js.map +1 -1
- package/dist/cjs/runtimes/react/controls/typography.js +49 -51
- package/dist/cjs/runtimes/react/controls/typography.js.map +1 -1
- package/dist/cjs/runtimes/react/controls.js +15 -111
- package/dist/cjs/runtimes/react/controls.js.map +1 -1
- package/dist/cjs/{utils/coalesce.js → runtimes/react/hooks/use-css-id.js} +13 -14
- package/dist/cjs/runtimes/react/hooks/use-css-id.js.map +1 -0
- package/dist/cjs/runtimes/react/hooks/use-resolved-props.js +85 -0
- package/dist/cjs/runtimes/react/hooks/use-resolved-props.js.map +1 -0
- package/dist/cjs/runtimes/react/hooks/use-resolved-value.js +9 -2
- package/dist/cjs/runtimes/react/hooks/use-resolved-value.js.map +1 -1
- package/dist/cjs/runtimes/react/hooks/use-resource-resolver.js +68 -0
- package/dist/cjs/runtimes/react/hooks/use-resource-resolver.js.map +1 -0
- package/dist/cjs/runtimes/react/hooks/use-stylesheet-factory.js +115 -0
- package/dist/cjs/runtimes/react/hooks/use-stylesheet-factory.js.map +1 -0
- package/dist/cjs/runtimes/react/resolvable-record.js +67 -0
- package/dist/cjs/runtimes/react/resolvable-record.js.map +1 -0
- package/dist/cjs/runtimes/react/use-style.js +20 -4
- package/dist/cjs/runtimes/react/use-style.js.map +1 -1
- package/dist/cjs/slate/TypographyPlugin/index.js +2 -1
- package/dist/cjs/slate/TypographyPlugin/index.js.map +1 -1
- package/dist/cjs/slate/TypographyPlugin/normalizeTypographyDown.js +2 -12
- package/dist/cjs/slate/TypographyPlugin/normalizeTypographyDown.js.map +1 -1
- package/dist/cjs/slate/index.js.map +1 -1
- package/dist/cjs/state/modules/breakpoints.js +7 -87
- package/dist/cjs/state/modules/breakpoints.js.map +1 -1
- package/dist/esm/api/react.js +45 -0
- package/dist/esm/api/react.js.map +1 -1
- package/dist/esm/components/shared/Link/index.js +14 -69
- package/dist/esm/components/shared/Link/index.js.map +1 -1
- package/dist/esm/components/utils/responsive-style.js +2 -3
- package/dist/esm/components/utils/responsive-style.js.map +1 -1
- package/dist/esm/controls/link.js +37 -2
- package/dist/esm/controls/link.js.map +1 -1
- package/dist/esm/controls/rich-text/rich-text.js +16 -1
- package/dist/esm/controls/rich-text/rich-text.js.map +1 -1
- package/dist/esm/controls/rich-text-v2/rich-text-v2.js +14 -1
- package/dist/esm/controls/rich-text-v2/rich-text-v2.js.map +1 -1
- package/dist/esm/controls/slot.js +14 -1
- package/dist/esm/controls/slot.js.map +1 -1
- package/dist/esm/next/api-handler/handlers/manifest.js +1 -1
- package/dist/esm/next/components/tests/controls/fixtures/rich-text-v2.js +179 -0
- package/dist/esm/next/components/tests/controls/fixtures/rich-text-v2.js.map +1 -0
- package/dist/esm/runtimes/react/components/ElementData.js +2 -2
- package/dist/esm/runtimes/react/components/ElementData.js.map +1 -1
- package/dist/esm/runtimes/react/controls/control.js +11 -175
- package/dist/esm/runtimes/react/controls/control.js.map +1 -1
- package/dist/esm/runtimes/react/controls/rich-text/EditableText/editable-text.js +1 -0
- package/dist/esm/runtimes/react/controls/rich-text/EditableText/editable-text.js.map +1 -1
- package/dist/esm/runtimes/react/controls/rich-text/rich-text.js +8 -2
- package/dist/esm/runtimes/react/controls/rich-text/rich-text.js.map +1 -1
- package/dist/esm/runtimes/react/controls/rich-text-v2/EditableTextV2/editable-text-v2.js +1 -0
- package/dist/esm/runtimes/react/controls/rich-text-v2/EditableTextV2/editable-text-v2.js.map +1 -1
- package/dist/esm/runtimes/react/controls/rich-text-v2/ReadOnlyTextV2.js +1 -1
- package/dist/esm/runtimes/react/controls/rich-text-v2/ReadOnlyTextV2.js.map +1 -1
- package/dist/esm/runtimes/react/controls/rich-text-v2/rich-text-v2.js +14 -9
- package/dist/esm/runtimes/react/controls/rich-text-v2/rich-text-v2.js.map +1 -1
- package/dist/esm/runtimes/react/controls/slot.js +4 -3
- package/dist/esm/runtimes/react/controls/slot.js.map +1 -1
- package/dist/esm/runtimes/react/controls/style.js +6 -37
- package/dist/esm/runtimes/react/controls/style.js.map +1 -1
- package/dist/esm/runtimes/react/controls/typography.js +49 -48
- package/dist/esm/runtimes/react/controls/typography.js.map +1 -1
- package/dist/esm/runtimes/react/controls.js +14 -129
- package/dist/esm/runtimes/react/controls.js.map +1 -1
- package/dist/esm/runtimes/react/hooks/use-css-id.js +8 -0
- package/dist/esm/runtimes/react/hooks/use-css-id.js.map +1 -0
- package/dist/esm/runtimes/react/hooks/use-resolved-props.js +53 -0
- package/dist/esm/runtimes/react/hooks/use-resolved-props.js.map +1 -0
- package/dist/esm/runtimes/react/hooks/use-resolved-value.js +9 -2
- package/dist/esm/runtimes/react/hooks/use-resolved-value.js.map +1 -1
- package/dist/esm/runtimes/react/hooks/use-resource-resolver.js +34 -0
- package/dist/esm/runtimes/react/hooks/use-resource-resolver.js.map +1 -0
- package/dist/esm/runtimes/react/hooks/use-stylesheet-factory.js +94 -0
- package/dist/esm/runtimes/react/hooks/use-stylesheet-factory.js.map +1 -0
- package/dist/esm/runtimes/react/resolvable-record.js +43 -0
- package/dist/esm/runtimes/react/resolvable-record.js.map +1 -0
- package/dist/esm/runtimes/react/use-style.js +17 -3
- package/dist/esm/runtimes/react/use-style.js.map +1 -1
- package/dist/esm/slate/TypographyPlugin/index.js +2 -1
- package/dist/esm/slate/TypographyPlugin/index.js.map +1 -1
- package/dist/esm/slate/TypographyPlugin/normalizeTypographyDown.js +1 -1
- package/dist/esm/slate/TypographyPlugin/normalizeTypographyDown.js.map +1 -1
- package/dist/esm/slate/index.js.map +1 -1
- package/dist/esm/state/modules/breakpoints.js +9 -70
- package/dist/esm/state/modules/breakpoints.js.map +1 -1
- package/dist/types/api/react.d.ts +11 -1
- package/dist/types/api/react.d.ts.map +1 -1
- package/dist/types/components/builtin/Button/Button.d.ts +2 -2
- package/dist/types/components/shared/Link/index.d.ts.map +1 -1
- package/dist/types/components/utils/responsive-style.d.ts +1 -1
- package/dist/types/components/utils/responsive-style.d.ts.map +1 -1
- package/dist/types/controls/link.d.ts +16 -2
- package/dist/types/controls/link.d.ts.map +1 -1
- package/dist/types/controls/rich-text/rich-text.d.ts +2 -1
- package/dist/types/controls/rich-text/rich-text.d.ts.map +1 -1
- package/dist/types/controls/rich-text-v2/rich-text-v2.d.ts +2 -1
- package/dist/types/controls/rich-text-v2/rich-text-v2.d.ts.map +1 -1
- package/dist/types/controls/slot.d.ts +2 -1
- package/dist/types/controls/slot.d.ts.map +1 -1
- package/dist/types/locale.d.ts +1 -1
- package/dist/types/next/components/tests/controls/fixtures/rich-text-v2.d.ts +40 -0
- package/dist/types/next/components/tests/controls/fixtures/rich-text-v2.d.ts.map +1 -0
- package/dist/types/next/components/tests/controls/rich-text-v2-control.test.d.ts +2 -0
- package/dist/types/next/components/tests/controls/rich-text-v2-control.test.d.ts.map +1 -0
- package/dist/types/runtimes/react/controls/control.d.ts +1 -1
- package/dist/types/runtimes/react/controls/control.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/rich-text/EditableText/editable-text.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/rich-text/rich-text.d.ts +1 -1
- package/dist/types/runtimes/react/controls/rich-text/rich-text.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/rich-text-v2/EditableTextV2/editable-text-v2.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/rich-text-v2/ReadOnlyTextV2.d.ts +3 -3
- package/dist/types/runtimes/react/controls/rich-text-v2/ReadOnlyTextV2.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/rich-text-v2/rich-text-v2.d.ts +1 -1
- package/dist/types/runtimes/react/controls/rich-text-v2/rich-text-v2.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/slot.d.ts +4 -1
- package/dist/types/runtimes/react/controls/slot.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/style.d.ts +4 -3
- package/dist/types/runtimes/react/controls/style.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls/typography.d.ts +5 -6
- package/dist/types/runtimes/react/controls/typography.d.ts.map +1 -1
- package/dist/types/runtimes/react/controls.d.ts +1 -1
- package/dist/types/runtimes/react/controls.d.ts.map +1 -1
- package/dist/types/runtimes/react/hooks/use-css-id.d.ts +2 -0
- package/dist/types/runtimes/react/hooks/use-css-id.d.ts.map +1 -0
- package/dist/types/runtimes/react/hooks/use-resolved-props.d.ts +3 -0
- package/dist/types/runtimes/react/hooks/use-resolved-props.d.ts.map +1 -0
- package/dist/types/runtimes/react/hooks/use-resolved-value.d.ts +2 -2
- package/dist/types/runtimes/react/hooks/use-resolved-value.d.ts.map +1 -1
- package/dist/types/runtimes/react/hooks/use-resource-resolver.d.ts +3 -0
- package/dist/types/runtimes/react/hooks/use-resource-resolver.d.ts.map +1 -0
- package/dist/types/runtimes/react/hooks/use-stylesheet-factory.d.ts +7 -0
- package/dist/types/runtimes/react/hooks/use-stylesheet-factory.d.ts.map +1 -0
- package/dist/types/runtimes/react/resolvable-record.d.ts +3 -0
- package/dist/types/runtimes/react/resolvable-record.d.ts.map +1 -0
- package/dist/types/runtimes/react/use-style.d.ts +4 -0
- package/dist/types/runtimes/react/use-style.d.ts.map +1 -1
- package/dist/types/slate/BlockPlugin/index.d.ts +5 -5
- package/dist/types/slate/LinkPlugin/getValue.d.ts +1 -1
- package/dist/types/slate/LinkPlugin/index.d.ts +1 -1
- package/dist/types/slate/TypographyPlugin/index.d.ts.map +1 -1
- package/dist/types/slate/TypographyPlugin/normalizeTypographyDown.d.ts.map +1 -1
- package/dist/types/slate/index.d.ts +1 -0
- package/dist/types/slate/index.d.ts.map +1 -1
- package/dist/types/slate/test-helpers/slate-test-helper.d.ts +4 -4
- package/dist/types/slate/test-helpers/test-elements.d.ts +1 -1
- package/dist/types/state/modules/breakpoints.d.ts +2 -21
- package/dist/types/state/modules/breakpoints.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/cjs/runtimes/react/controls/color.js +0 -58
- package/dist/cjs/runtimes/react/controls/color.js.map +0 -1
- package/dist/cjs/runtimes/react/controls/image.js +0 -53
- package/dist/cjs/runtimes/react/controls/image.js.map +0 -1
- package/dist/cjs/runtimes/react/controls/link.js +0 -110
- package/dist/cjs/runtimes/react/controls/link.js.map +0 -1
- package/dist/cjs/runtimes/react/controls/list.js +0 -58
- package/dist/cjs/runtimes/react/controls/list.js.map +0 -1
- package/dist/cjs/runtimes/react/controls/shape.js +0 -44
- package/dist/cjs/runtimes/react/controls/shape.js.map +0 -1
- package/dist/cjs/runtimes/react/controls/style-v2.js +0 -92
- package/dist/cjs/runtimes/react/controls/style-v2.js.map +0 -1
- package/dist/cjs/utils/coalesce.js.map +0 -1
- package/dist/cjs/utils/shallowMerge.js +0 -44
- package/dist/cjs/utils/shallowMerge.js.map +0 -1
- package/dist/esm/runtimes/react/controls/color.js +0 -24
- package/dist/esm/runtimes/react/controls/color.js.map +0 -1
- package/dist/esm/runtimes/react/controls/image.js +0 -29
- package/dist/esm/runtimes/react/controls/image.js.map +0 -1
- package/dist/esm/runtimes/react/controls/link.js +0 -76
- package/dist/esm/runtimes/react/controls/link.js.map +0 -1
- package/dist/esm/runtimes/react/controls/list.js +0 -34
- package/dist/esm/runtimes/react/controls/list.js.map +0 -1
- package/dist/esm/runtimes/react/controls/shape.js +0 -20
- package/dist/esm/runtimes/react/controls/shape.js.map +0 -1
- package/dist/esm/runtimes/react/controls/style-v2.js +0 -72
- package/dist/esm/runtimes/react/controls/style-v2.js.map +0 -1
- package/dist/esm/utils/coalesce.js +0 -13
- package/dist/esm/utils/coalesce.js.map +0 -1
- package/dist/esm/utils/shallowMerge.js +0 -14
- package/dist/esm/utils/shallowMerge.js.map +0 -1
- package/dist/types/runtimes/react/controls/color.d.ts +0 -4
- package/dist/types/runtimes/react/controls/color.d.ts.map +0 -1
- package/dist/types/runtimes/react/controls/image.d.ts +0 -4
- package/dist/types/runtimes/react/controls/image.d.ts.map +0 -1
- package/dist/types/runtimes/react/controls/link.d.ts +0 -4
- package/dist/types/runtimes/react/controls/link.d.ts.map +0 -1
- package/dist/types/runtimes/react/controls/list.d.ts +0 -12
- package/dist/types/runtimes/react/controls/list.d.ts.map +0 -1
- package/dist/types/runtimes/react/controls/shape.d.ts +0 -12
- package/dist/types/runtimes/react/controls/shape.d.ts.map +0 -1
- package/dist/types/runtimes/react/controls/style-v2.d.ts +0 -13
- package/dist/types/runtimes/react/controls/style-v2.d.ts.map +0 -1
- package/dist/types/utils/coalesce.d.ts +0 -3
- package/dist/types/utils/coalesce.d.ts.map +0 -1
- package/dist/types/utils/shallowMerge.d.ts +0 -2
- package/dist/types/utils/shallowMerge.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/runtimes/react/controls/style.ts"],"sourcesContent":["import { CSSObject } from '@emotion/css'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/controls/style.ts"],"sourcesContent":["import { CSSObject } from '@emotion/css'\nimport {\n Style,\n type StyleProperty,\n type ResolvedBorderSideData,\n type FontSizePropertyData,\n type WidthPropertyData,\n type ResolvedStyleData,\n} from '@makeswift/controls'\n\nimport { colorToString } from '../../../components/utils/colorToString'\nimport { responsiveStyle } from '../../../components/utils/responsive-style'\n\nimport { BorderRadiusLonghandPropertyData } from '../../../css/border-radius'\nimport { lengthPercentageDataToString } from '../../../css/length-percentage'\nimport { marginPropertyDataToStyle } from '../../../css/margin'\nimport { paddingPropertyDataToStyle } from '../../../css/padding'\nimport { Breakpoints } from '../../../state/modules/breakpoints'\n\nconst defaultMargin = {\n marginTop: 0,\n marginRight: 'auto',\n marginBottom: 0,\n marginLeft: 'auto',\n}\n\nconst defaultPadding = {\n paddingTop: 0,\n paddingRight: 0,\n paddingBottom: 0,\n paddingLeft: 0,\n}\n\nexport function styleV1Css(\n breakpoints: Breakpoints,\n style: ResolvedStyleData | undefined,\n properties: StyleProperty[],\n): CSSObject {\n return {\n ...(properties.includes(Style.Width) && {\n maxWidth: '100%',\n }),\n ...responsiveStyle(\n breakpoints,\n [\n style?.width,\n style?.margin,\n style?.padding,\n style?.border,\n style?.borderRadius,\n style?.textStyle,\n ] as const,\n ([width, margin, padding, border, borderRadius, textStyle]) => ({\n ...(properties.includes(Style.Width) && {\n width: widthToString(width) ?? '100%',\n }),\n ...(properties.includes(Style.Margin) &&\n marginPropertyDataToStyle(margin ?? defaultMargin, defaultMargin)),\n ...(properties.includes(Style.Padding) &&\n paddingPropertyDataToStyle(padding ?? defaultPadding, defaultPadding)),\n ...(properties.includes(Style.Border) && {\n borderTop: borderSideToString(border?.borderTop) ?? '0 solid black',\n borderRight: borderSideToString(border?.borderRight) ?? '0 solid black',\n borderBottom: borderSideToString(border?.borderBottom) ?? '0 solid black',\n borderLeft: borderSideToString(border?.borderLeft) ?? '0 solid black',\n }),\n ...(properties.includes(Style.BorderRadius) && {\n borderTopLeftRadius: borderRadiusToString(borderRadius?.borderTopLeftRadius) ?? 0,\n borderTopRightRadius: borderRadiusToString(borderRadius?.borderTopRightRadius) ?? 0,\n borderBottomRightRadius: borderRadiusToString(borderRadius?.borderBottomRightRadius) ?? 0,\n borderBottomLeftRadius: borderRadiusToString(borderRadius?.borderBottomLeftRadius) ?? 0,\n }),\n ...(properties.includes(Style.TextStyle) && {\n ...(textStyle?.fontFamily && { fontFamily: `\"${textStyle.fontFamily}\"` }),\n ...(textStyle?.letterSpacing && { letterSpacing: textStyle.letterSpacing }),\n ...(textStyle?.fontSize && { fontSize: fontSizeToString(textStyle.fontSize) }),\n ...(textStyle?.fontWeight && { fontWeight: textStyle.fontWeight }),\n textTransform: textStyle?.textTransform ?? [],\n fontStyle: textStyle?.fontStyle ?? [],\n }),\n }),\n ),\n }\n\n function widthToString(widthProperty: WidthPropertyData | undefined): string | null {\n if (widthProperty == null) return null\n\n return lengthPercentageDataToString(widthProperty)\n }\n\n function borderSideToString(\n borderSide: ResolvedBorderSideData | null | undefined,\n ): string | null {\n if (borderSide == null) return null\n\n const { width, color, style } = borderSide\n return `${width != null ? width : 0}px ${style} ${\n color != null ? colorToString(color) : 'black'\n }`\n }\n\n function borderRadiusToString(\n borderRadius: BorderRadiusLonghandPropertyData | null | undefined,\n ): string | null {\n if (borderRadius == null) return null\n\n return lengthPercentageDataToString(borderRadius)\n }\n\n function fontSizeToString(fontSize: NonNullable<FontSizePropertyData>) {\n return `${fontSize.value}${fontSize.unit}`\n }\n}\n"],"mappings":"AACA;AAAA,EACE;AAAA,OAMK;AAEP,SAAS,qBAAqB;AAC9B,SAAS,uBAAuB;AAGhC,SAAS,oCAAoC;AAC7C,SAAS,iCAAiC;AAC1C,SAAS,kCAAkC;AAG3C,MAAM,gBAAgB;AAAA,EACpB,WAAW;AAAA,EACX,aAAa;AAAA,EACb,cAAc;AAAA,EACd,YAAY;AACd;AAEA,MAAM,iBAAiB;AAAA,EACrB,YAAY;AAAA,EACZ,cAAc;AAAA,EACd,eAAe;AAAA,EACf,aAAa;AACf;AAEO,SAAS,WACd,aACA,OACA,YACW;AACX,SAAO;AAAA,IACL,GAAI,WAAW,SAAS,MAAM,KAAK,KAAK;AAAA,MACtC,UAAU;AAAA,IACZ;AAAA,IACA,GAAG;AAAA,MACD;AAAA,MACA;AAAA,QACE,OAAO;AAAA,QACP,OAAO;AAAA,QACP,OAAO;AAAA,QACP,OAAO;AAAA,QACP,OAAO;AAAA,QACP,OAAO;AAAA,MACT;AAAA,MACA,CAAC,CAAC,OAAO,QAAQ,SAAS,QAAQ,cAAc,SAAS,OAAO;AAAA,QAC9D,GAAI,WAAW,SAAS,MAAM,KAAK,KAAK;AAAA,UACtC,OAAO,cAAc,KAAK,KAAK;AAAA,QACjC;AAAA,QACA,GAAI,WAAW,SA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AM,IAAI;AAChC,WAAO,GAAG,SAAS,OAAO,QAAQ,CAAC,MAAMA,MAAK,IAC5C,SAAS,OAAO,cAAc,KAAK,IAAI,OACzC;AAAA,EACF;AAEA,WAAS,qBACP,cACe;AACf,QAAI,gBAAgB;AAAM,aAAO;AAEjC,WAAO,6BAA6B,YAAY;AAAA,EAClD;AAEA,WAAS,iBAAiB,UAA6C;AACrE,WAAO,GAAG,SAAS,KAAK,GAAG,SAAS,IAAI;AAAA,EAC1C;AACF;","names":["style"]}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
findBreakpointOverride,
|
|
3
|
+
shallowMergeFallbacks
|
|
4
|
+
} from "@makeswift/controls";
|
|
1
5
|
import { useSwatches, useTypography } from "../hooks/makeswift-api";
|
|
2
6
|
import { colorToString } from "../../../components/utils/colorToString";
|
|
3
|
-
import {
|
|
4
|
-
import { findBreakpointOverride, shallowMergeFallbacks } from "../../../state/modules/breakpoints";
|
|
7
|
+
import { responsiveStyle } from "../../../components/utils/responsive-style";
|
|
5
8
|
import { isNonNullable } from "../../../utils/isNonNullable";
|
|
6
9
|
import { useStyle } from "../use-style";
|
|
7
10
|
import { useBreakpoints } from "../hooks/use-breakpoints";
|
|
8
|
-
function typographyFragementToTypographyControlData(
|
|
9
|
-
if (
|
|
11
|
+
function typographyFragementToTypographyControlData(fragment) {
|
|
12
|
+
if (fragment == null)
|
|
10
13
|
return void 0;
|
|
11
14
|
return {
|
|
12
|
-
id:
|
|
13
|
-
style:
|
|
15
|
+
id: fragment.id,
|
|
16
|
+
style: fragment.style.map(({ deviceId, value }) => ({
|
|
14
17
|
deviceId,
|
|
15
18
|
value: {
|
|
16
19
|
fontFamily: value.fontFamily ?? void 0,
|
|
@@ -52,13 +55,13 @@ const withColor = (swatches) => (deviceRawTypographyValue) => {
|
|
|
52
55
|
};
|
|
53
56
|
};
|
|
54
57
|
const getDeviceId = ({ deviceId }) => deviceId;
|
|
55
|
-
function useEnhancedTypography(
|
|
56
|
-
const typography = typographyFragementToTypographyControlData(useTypography(
|
|
58
|
+
function useEnhancedTypography(data) {
|
|
59
|
+
const typography = typographyFragementToTypographyControlData(useTypography(data?.id ?? null));
|
|
57
60
|
const source = typography?.style ?? [];
|
|
58
|
-
const override =
|
|
61
|
+
const override = data?.style ?? [];
|
|
59
62
|
const breakpoints = useBreakpoints();
|
|
60
63
|
const swatchIds = [
|
|
61
|
-
...getTypographyStyleSwatchIds(
|
|
64
|
+
...getTypographyStyleSwatchIds(data?.style),
|
|
62
65
|
...getTypographyStyleSwatchIds(typography?.style)
|
|
63
66
|
];
|
|
64
67
|
const swatches = useSwatches(swatchIds).filter(isNonNullable);
|
|
@@ -94,50 +97,48 @@ function useEnhancedTypography(value) {
|
|
|
94
97
|
return null;
|
|
95
98
|
}).filter(isNonNullable);
|
|
96
99
|
}
|
|
97
|
-
function
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
shallowMergeFallbacks
|
|
129
|
-
)
|
|
100
|
+
function typographyToCssObject(value) {
|
|
101
|
+
let styles = {};
|
|
102
|
+
if (value.color != null)
|
|
103
|
+
styles.color = colorToString(value.color);
|
|
104
|
+
if (value.fontFamily != null)
|
|
105
|
+
styles.fontFamily = value.fontFamily;
|
|
106
|
+
if (value.fontSize != null && value.fontSize.value != null && value.fontSize.unit != null)
|
|
107
|
+
styles.fontSize = `${value.fontSize.value}${value.fontSize.unit}`;
|
|
108
|
+
if (value.fontWeight != null)
|
|
109
|
+
styles.fontWeight = value.fontWeight;
|
|
110
|
+
if (value.lineHeight != null)
|
|
111
|
+
styles.lineHeight = value.lineHeight;
|
|
112
|
+
if (value.letterSpacing != null)
|
|
113
|
+
styles.letterSpacing = `${value.letterSpacing / 10}em`;
|
|
114
|
+
if (value.uppercase != null)
|
|
115
|
+
styles.textTransform = value.uppercase === true ? "uppercase" : "initial";
|
|
116
|
+
if (value.underline != null || value.strikethrough != null)
|
|
117
|
+
styles.textDecoration = [
|
|
118
|
+
Boolean(value.underline) && "underline",
|
|
119
|
+
Boolean(value.strikethrough) && "line-through"
|
|
120
|
+
].filter(Boolean).join(" ");
|
|
121
|
+
if (value.italic != null)
|
|
122
|
+
styles.fontStyle = value.italic === true ? "italic" : "initial";
|
|
123
|
+
return styles;
|
|
124
|
+
}
|
|
125
|
+
function typographyCss(breakpoints, style) {
|
|
126
|
+
return responsiveStyle(
|
|
127
|
+
breakpoints,
|
|
128
|
+
[style],
|
|
129
|
+
([value]) => value !== void 0 ? typographyToCssObject(value) : {},
|
|
130
|
+
shallowMergeFallbacks
|
|
130
131
|
);
|
|
131
132
|
}
|
|
132
|
-
function
|
|
133
|
-
const
|
|
134
|
-
return
|
|
133
|
+
function useTypographyClassName(value) {
|
|
134
|
+
const breakpoints = useBreakpoints();
|
|
135
|
+
return useStyle(typographyCss(breakpoints, value));
|
|
135
136
|
}
|
|
136
137
|
export {
|
|
137
138
|
useEnhancedTypography as default,
|
|
138
139
|
getTypographyStyleSwatchIds,
|
|
140
|
+
typographyCss,
|
|
139
141
|
typographyFragementToTypographyControlData,
|
|
140
|
-
useTypographyClassName
|
|
141
|
-
useTypographyValue
|
|
142
|
+
useTypographyClassName
|
|
142
143
|
};
|
|
143
144
|
//# sourceMappingURL=typography.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/runtimes/react/controls/typography.ts"],"sourcesContent":["import { CSSObject } from '@emotion/serialize'\nimport { unstable_TypographyDefinition, type DataType } from '@makeswift/controls'\n\nimport { useSwatches, useTypography } from '../hooks/makeswift-api'\nimport { Typography, Swatch } from '../../../api'\nimport { colorToString } from '../../../components/utils/colorToString'\nimport { useResponsiveStyle } from '../../../components/utils/responsive-style'\nimport { ColorValue } from '../../../components/utils/types'\nimport { DeviceOverride, ResponsiveValue } from '../../../prop-controllers'\nimport { findBreakpointOverride, shallowMergeFallbacks } from '../../../state/modules/breakpoints'\nimport { isNonNullable } from '../../../utils/isNonNullable'\nimport { useStyle } from '../use-style'\nimport { useBreakpoints } from '../hooks/use-breakpoints'\n\nexport function typographyFragementToTypographyControlData(\n typography: Typography | null,\n): DataType<unstable_TypographyDefinition> | undefined {\n if (typography == null) return undefined\n return {\n id: typography.id,\n style: typography.style.map(({ deviceId, value }) => ({\n deviceId,\n value: {\n fontFamily: value.fontFamily ?? undefined,\n lineHeight: value.lineHeight ?? undefined,\n letterSpacing: value.letterSpacing ?? undefined,\n fontWeight: value.fontWeight ?? undefined,\n textAlign: value.textAlign ?? undefined,\n uppercase: value.uppercase ?? undefined,\n underline: value.underline ?? undefined,\n strikethrough: value.strikethrough ?? undefined,\n italic: value.italic ?? undefined,\n fontSize: value.fontSize ?? undefined,\n color: value.color ?? undefined,\n },\n })),\n }\n}\n\ntype EnhancedColor = {\n color?: ColorValue\n}\n\nexport type TypographyControlDataValue = Exclude<\n DataType<unstable_TypographyDefinition>,\n undefined\n>['style'][number]['value']\n\ntype EnhancedTypographyValue = Omit<TypographyControlDataValue, keyof EnhancedColor> & EnhancedColor\n\nexport type EnhancedTypography = Array<DeviceOverride<EnhancedTypographyValue>>\n\nexport function getTypographyStyleSwatchIds(\n style:\n | Exclude<DataType<unstable_TypographyDefinition>, undefined>['style']\n | Typography['style']\n | null\n | undefined,\n): string[] {\n return (\n style\n ?.map(override => override.value)\n .flatMap(typographyStyle => typographyStyle.color?.swatchId)\n .filter(isNonNullable) ?? []\n )\n}\n\nconst withColor =\n (swatches: Swatch[]) =>\n (\n deviceRawTypographyValue: DeviceOverride<TypographyControlDataValue>,\n ): DeviceOverride<EnhancedTypographyValue> => {\n const { value, deviceId } = deviceRawTypographyValue\n\n if (value.color == null) {\n const { color, ...nextValue } = value\n return {\n deviceId,\n value: nextValue,\n }\n }\n return {\n deviceId,\n value: {\n ...value,\n color: {\n swatch: swatches.find(s => s && s.id === value.color?.swatchId),\n alpha: value.color?.alpha ?? undefined,\n },\n },\n }\n }\n\nconst getDeviceId = ({ deviceId }: DeviceOverride<unknown>) => deviceId\n\n/**\n * `enhanced` here just means typography ids have been replaced with the related entity.\n */\nexport default function useEnhancedTypography(\n value?: DataType<unstable_TypographyDefinition> | null,\n): EnhancedTypography {\n const typography = typographyFragementToTypographyControlData(useTypography(value?.id ?? null))\n const source = typography?.style ?? []\n const override = value?.style ?? []\n const breakpoints = useBreakpoints()\n\n const swatchIds = [\n ...getTypographyStyleSwatchIds(value?.style),\n ...getTypographyStyleSwatchIds(typography?.style),\n ]\n const swatches = useSwatches(swatchIds).filter(isNonNullable)\n\n const enhancedSource = source.map(withColor(swatches))\n const enhancedOverride = override.map(withColor(swatches))\n\n const devices = [\n ...new Set(enhancedSource.map(getDeviceId).concat(enhancedOverride.map(getDeviceId))),\n ]\n\n return devices\n .map(deviceId => {\n const deviceSource = findBreakpointOverride(breakpoints, enhancedSource, deviceId)?.value\n const deviceOverride = findBreakpointOverride(\n breakpoints,\n enhancedOverride,\n deviceId,\n v => v,\n )?.value\n\n if (deviceSource && deviceOverride) {\n return {\n deviceId,\n value: { ...deviceSource, ...deviceOverride },\n }\n } else if (deviceOverride) {\n return {\n deviceId,\n value: deviceOverride,\n }\n } else if (deviceSource) {\n return {\n deviceId,\n value: deviceSource,\n }\n }\n return null\n })\n .filter(isNonNullable)\n}\n\nexport function useTypographyClassName(value: EnhancedTypography): string {\n return useStyle(\n useResponsiveStyle<\n EnhancedTypographyValue,\n [ResponsiveValue<EnhancedTypographyValue> | null | undefined]\n >(\n [value],\n ([value]) => {\n if (value === undefined) return {}\n\n let styles: CSSObject = {}\n if (value.color != null) styles.color = colorToString(value.color)\n if (value.fontFamily != null) styles.fontFamily = value.fontFamily\n if (value.fontSize != null && value.fontSize.value != null && value.fontSize.unit != null)\n styles.fontSize = `${value.fontSize.value}${value.fontSize.unit}`\n if (value.fontWeight != null) styles.fontWeight = value.fontWeight\n if (value.lineHeight != null) styles.lineHeight = value.lineHeight\n if (value.letterSpacing != null) styles.letterSpacing = `${value.letterSpacing / 10}em`\n if (value.uppercase != null)\n styles.textTransform = value.uppercase === true ? 'uppercase' : 'initial'\n if (value.underline != null || value.strikethrough != null)\n styles.textDecoration = [\n Boolean(value.underline) && 'underline',\n Boolean(value.strikethrough) && 'line-through',\n ]\n .filter(Boolean)\n .join(' ')\n if (value.italic != null) styles.fontStyle = value.italic === true ? 'italic' : 'initial'\n\n return styles\n },\n shallowMergeFallbacks,\n ),\n )\n}\n\nexport type TypographyControlValue = string\n\nexport function useTypographyValue(\n data: DataType<unstable_TypographyDefinition> | undefined,\n): TypographyControlValue {\n // for each breakpoint fetch related resources and merge its value with its override\n const enhancedTypography = useEnhancedTypography(data)\n\n // for each breakpoint shallow merge back up through the breakpoints and create a className\n return useTypographyClassName(enhancedTypography)\n}\n"],"mappings":"AAGA,SAAS,aAAa,qBAAqB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;AAGnC,SAAS,wBAAwB,6BAA6B;AAC9D,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,sBAAsB;AAExB,SAAS,2CACd,YACqD;AACrD,MAAI,cAAc;AAAM,WAAO;AAC/B,SAAO;AAAA,IACL,IAAI,WAAW;AAAA,IACf,OAAO,WAAW,MAAM,IAAI,CAAC,EAAE,UAAU,MAAM,OAAO;AAAA,MACpD;AAAA,MACA,OAAO;AAAA,QACL,YAAY,MAAM,cAAc;AAAA,QAChC,YAAY,MAAM,cAAc;AAAA,QAChC,eAAe,MAAM,iBAAiB;AAAA,QACtC,YAAY,MAAM,c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;AAAM,iBAAO,aAAaA,OAAM;AACxD,YAAIA,OAAM,iBAAiB;AAAM,iBAAO,gBAAgB,GAAGA,OAAM,gBAAgB,EAAE;AACnF,YAAIA,OAAM,aAAa;AACrB,iBAAO,gBAAgBA,OAAM,cAAc,OAAO,cAAc;AAClE,YAAIA,OAAM,aAAa,QAAQA,OAAM,iBAAiB;AACpD,iBAAO,iBAAiB;AAAA,YACtB,QAAQA,OAAM,SAAS,KAAK;AAAA,YAC5B,QAAQA,OAAM,aAAa,KAAK;AAAA,UAClC,EACG,OAAO,OAAO,EACd,KAAK,GAAG;AACb,YAAIA,OAAM,UAAU;AAAM,iBAAO,YAAYA,OAAM,WAAW,OAAO,WAAW;AAEhF,eAAO;AAAA,MACT;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACF;AAIO,SAAS,mBACd,MACwB;AAExB,QAAM,qBAAqB,sBAAsB,IAAI;AAGrD,SAAO,uBAAuB,kBAAkB;AAClD;","names":["value"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/controls/typography.ts"],"sourcesContent":["import { CSSObject } from '@emotion/serialize'\nimport {\n unstable_TypographyDefinition,\n findBreakpointOverride,\n shallowMergeFallbacks,\n type DataType,\n type Breakpoints,\n type DeviceOverride,\n type ResponsiveValue,\n} from '@makeswift/controls'\n\nimport { useSwatches, useTypography } from '../hooks/makeswift-api'\nimport { Typography, Swatch } from '../../../api'\nimport { colorToString } from '../../../components/utils/colorToString'\nimport { responsiveStyle } from '../../../components/utils/responsive-style'\nimport { ColorValue } from '../../../components/utils/types'\nimport { isNonNullable } from '../../../utils/isNonNullable'\nimport { useStyle } from '../use-style'\nimport { useBreakpoints } from '../hooks/use-breakpoints'\n\nexport function typographyFragementToTypographyControlData(\n fragment: Typography | null,\n): DataType<unstable_TypographyDefinition> | undefined {\n if (fragment == null) return undefined\n return {\n id: fragment.id,\n style: fragment.style.map(({ deviceId, value }) => ({\n deviceId,\n value: {\n fontFamily: value.fontFamily ?? undefined,\n lineHeight: value.lineHeight ?? undefined,\n letterSpacing: value.letterSpacing ?? undefined,\n fontWeight: value.fontWeight ?? undefined,\n textAlign: value.textAlign ?? undefined,\n uppercase: value.uppercase ?? undefined,\n underline: value.underline ?? undefined,\n strikethrough: value.strikethrough ?? undefined,\n italic: value.italic ?? undefined,\n fontSize: value.fontSize ?? undefined,\n color: value.color ?? undefined,\n },\n })),\n }\n}\n\ntype EnhancedColor = {\n color?: ColorValue\n}\n\nexport type TypographyControlDataValue = Exclude<\n DataType<unstable_TypographyDefinition>,\n undefined\n>['style'][number]['value']\n\ntype EnhancedTypographyValue = Omit<TypographyControlDataValue, keyof EnhancedColor> & EnhancedColor\n\nexport type EnhancedTypography = Array<DeviceOverride<EnhancedTypographyValue>>\n\nexport function getTypographyStyleSwatchIds(\n style:\n | Exclude<DataType<unstable_TypographyDefinition>, undefined>['style']\n | Typography['style']\n | null\n | undefined,\n): string[] {\n return (\n style\n ?.map(override => override.value)\n .flatMap(typographyStyle => typographyStyle.color?.swatchId)\n .filter(isNonNullable) ?? []\n )\n}\n\nconst withColor =\n (swatches: Swatch[]) =>\n (\n deviceRawTypographyValue: DeviceOverride<TypographyControlDataValue>,\n ): DeviceOverride<EnhancedTypographyValue> => {\n const { value, deviceId } = deviceRawTypographyValue\n\n if (value.color == null) {\n const { color, ...nextValue } = value\n return {\n deviceId,\n value: nextValue,\n }\n }\n return {\n deviceId,\n value: {\n ...value,\n color: {\n swatch: swatches.find(s => s && s.id === value.color?.swatchId),\n alpha: value.color?.alpha ?? undefined,\n },\n },\n }\n }\n\nconst getDeviceId = ({ deviceId }: DeviceOverride<unknown>) => deviceId\n\n/**\n * `enhanced` here just means typography ids have been replaced with the related entity.\n */\nexport default function useEnhancedTypography(\n data?: DataType<unstable_TypographyDefinition> | null,\n): EnhancedTypography {\n const typography = typographyFragementToTypographyControlData(useTypography(data?.id ?? null))\n const source = typography?.style ?? []\n const override = data?.style ?? []\n const breakpoints = useBreakpoints()\n\n const swatchIds = [\n ...getTypographyStyleSwatchIds(data?.style),\n ...getTypographyStyleSwatchIds(typography?.style),\n ]\n const swatches = useSwatches(swatchIds).filter(isNonNullable)\n\n const enhancedSource = source.map(withColor(swatches))\n const enhancedOverride = override.map(withColor(swatches))\n\n const devices = [\n ...new Set(enhancedSource.map(getDeviceId).concat(enhancedOverride.map(getDeviceId))),\n ]\n\n return devices\n .map(deviceId => {\n const deviceSource = findBreakpointOverride(breakpoints, enhancedSource, deviceId)?.value\n const deviceOverride = findBreakpointOverride(\n breakpoints,\n enhancedOverride,\n deviceId,\n v => v,\n )?.value\n\n if (deviceSource && deviceOverride) {\n return {\n deviceId,\n value: { ...deviceSource, ...deviceOverride },\n }\n } else if (deviceOverride) {\n return {\n deviceId,\n value: deviceOverride,\n }\n } else if (deviceSource) {\n return {\n deviceId,\n value: deviceSource,\n }\n }\n return null\n })\n .filter(isNonNullable)\n}\n\nfunction typographyToCssObject(value: EnhancedTypographyValue): CSSObject {\n let styles: CSSObject = {}\n if (value.color != null) styles.color = colorToString(value.color)\n if (value.fontFamily != null) styles.fontFamily = value.fontFamily\n if (value.fontSize != null && value.fontSize.value != null && value.fontSize.unit != null)\n styles.fontSize = `${value.fontSize.value}${value.fontSize.unit}`\n if (value.fontWeight != null) styles.fontWeight = value.fontWeight\n if (value.lineHeight != null) styles.lineHeight = value.lineHeight\n if (value.letterSpacing != null) styles.letterSpacing = `${value.letterSpacing / 10}em`\n if (value.uppercase != null)\n styles.textTransform = value.uppercase === true ? 'uppercase' : 'initial'\n if (value.underline != null || value.strikethrough != null)\n styles.textDecoration = [\n Boolean(value.underline) && 'underline',\n Boolean(value.strikethrough) && 'line-through',\n ]\n .filter(Boolean)\n .join(' ')\n if (value.italic != null) styles.fontStyle = value.italic === true ? 'italic' : 'initial'\n\n return styles\n}\n\nexport function typographyCss(breakpoints: Breakpoints, style: EnhancedTypography): CSSObject {\n return responsiveStyle<\n EnhancedTypographyValue,\n [ResponsiveValue<EnhancedTypographyValue> | null | undefined]\n >(\n breakpoints,\n [style],\n ([value]) => (value !== undefined ? typographyToCssObject(value) : {}),\n shallowMergeFallbacks,\n )\n}\n\nexport function useTypographyClassName(value: EnhancedTypography): string {\n const breakpoints = useBreakpoints()\n return useStyle(typographyCss(breakpoints, value))\n}\n"],"mappings":"AACA;AAAA,EAEE;AAAA,EACA;AAAA,OAKK;AAEP,SAAS,aAAa,qBAAqB;AAE3C,SAAS,qBAAqB;AAC9B,SAAS,uBAAuB;AAEhC,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,sBAAsB;AAExB,SAAS,2CACd,UACqD;AACrD,MAAI,YAAY;AAAM,WAAO;AAC7B,SAAO;AAAA,IACL,IAAI,SAAS;AAAA,IACb,OAAO,SAAS,MAAM,IAAI,CAAC,EAAE,UAAU,MAAM,OAAO;AAAA,MAClD;AAAA,MACA,OAAO;AAAA,QACL,YAAY,MAAM,cAAc;AAAA,QAChC,YAAY,MAAM,cAAc;AAAA,QAChC,eAAe,MAAM,iBAAiB;AAAA,QACtC,YAAY,MAAM,cAAc;AAAA,QAChC,WAAW,MAAM,aAAa;AAAA,QAC9B,WAAW,MAAM,aAAa;AAAA,QAC9B,WAAW,MAAM,aAAa;AAAA,QAC9B,eAAe,MAAM,iBAAiB;AAAA,QACtC,QAAQ,MAAM,UAAU;AAAA,QACxB,UAAU,MAAM,YAAY;AAAA,QAC5B,OAAO,MAAM,SAAS;AAAA,MACxB;AAAA,IACF,EAAE;AAAA,EACJ;AACF;AAeO,SAAS,4BACd,OAKU;AACV,SACE,OACI,IAAI,cAAY,SAAS,KAAK,EAC/B,QAAQ,qBAAmB,gBAAgB,OAAO,QAAQ,EAC1D,OAAO,aAAa,KAAK,CAAC;AAEjC;AAEA,MAAM,YACJ,CAAC,aACD,CACE,6BAC4C;AAC5C,QAAM,EAAE,OAAO,SAAS,IAAI;AAE5B,MAAI,MAAM,SAAS,MAAM;AACvB,UAAM,EAAE,OAAO,GAAG,UAAU,IAAI;AAChC,WAAO;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D;","names":[]}
|
|
@@ -1,140 +1,25 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
1
|
import { useRef } from "react";
|
|
2
|
+
import { partitionRecord } from "../../utils/partition";
|
|
3
3
|
import * as ReactPage from "../../state/react-page";
|
|
4
|
-
import {
|
|
5
|
-
CheckboxDefinition,
|
|
6
|
-
NumberDefinition,
|
|
7
|
-
RichTextV2Definition,
|
|
8
|
-
ColorDefinition,
|
|
9
|
-
ComboboxDefinition,
|
|
10
|
-
IconRadioGroupDefinition,
|
|
11
|
-
ImageDefinition,
|
|
12
|
-
LinkDefinition,
|
|
13
|
-
ListDefinition,
|
|
14
|
-
RichTextV1Definition,
|
|
15
|
-
SelectDefinition,
|
|
16
|
-
ShapeDefinition,
|
|
17
|
-
SlotDefinition,
|
|
18
|
-
StyleDefinition,
|
|
19
|
-
StyleV2Definition,
|
|
20
|
-
TextAreaDefinition,
|
|
21
|
-
TextInputDefinition,
|
|
22
|
-
unstable_TypographyDefinition
|
|
23
|
-
} from "../../controls";
|
|
24
4
|
import { isLegacyDescriptor } from "../../prop-controllers/descriptors";
|
|
25
|
-
import { useFormattedStyle } from "./controls/style";
|
|
26
|
-
import { ControlValue } from "./controls/control";
|
|
27
|
-
import { useSlot } from "./controls/slot";
|
|
28
|
-
import { useRichText } from "./controls/rich-text/rich-text";
|
|
29
|
-
import { useRichTextV2 } from "./controls/rich-text-v2";
|
|
30
5
|
import { useStore } from "./hooks/use-store";
|
|
31
|
-
import { useDocumentKey } from "./hooks/use-document-key";
|
|
32
|
-
import { useSelector } from "./hooks/use-selector";
|
|
33
|
-
import { RenderHook } from "./components";
|
|
34
6
|
import { resolveLegacyDescriptorProp } from "./legacy-controls";
|
|
35
|
-
|
|
7
|
+
import { useResolvedProps } from "./hooks/use-resolved-props";
|
|
8
|
+
function useControlDefs(elementType) {
|
|
36
9
|
const store = useStore();
|
|
37
|
-
const
|
|
38
|
-
|
|
10
|
+
const all = ReactPage.getComponentPropControllerDescriptors(store.getState(), elementType) ?? {};
|
|
11
|
+
return useRef(partitionRecord(all, isLegacyDescriptor)).current;
|
|
12
|
+
}
|
|
13
|
+
function ResolveProps({ element, children: renderComponent }) {
|
|
14
|
+
const [legacyDescriptors, definitions] = useControlDefs(element.type);
|
|
15
|
+
const resolvedProps = useResolvedProps(definitions, element.props, element.key);
|
|
16
|
+
const renderFn = Object.entries(legacyDescriptors).reduceRight(
|
|
17
|
+
(renderFn2, [propName, descriptor]) => (props) => resolveLegacyDescriptorProp(descriptor, propName, element.props[propName], props, renderFn2),
|
|
18
|
+
renderComponent
|
|
39
19
|
);
|
|
40
|
-
|
|
41
|
-
const documentKey = useDocumentKey();
|
|
42
|
-
const propControllers = useSelector((state) => {
|
|
43
|
-
if (documentKey == null)
|
|
44
|
-
return null;
|
|
45
|
-
return ReactPage.getPropControllers(state, documentKey, element.key);
|
|
46
|
-
});
|
|
47
|
-
return Object.entries(propControllerDescriptorsRef.current).reduceRight(
|
|
48
|
-
(renderFn, [propName, descriptor]) => (propsValue) => {
|
|
49
|
-
if (isLegacyDescriptor(descriptor)) {
|
|
50
|
-
return resolveLegacyDescriptorProp(
|
|
51
|
-
descriptor,
|
|
52
|
-
propName,
|
|
53
|
-
props[propName],
|
|
54
|
-
propsValue,
|
|
55
|
-
renderFn
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
switch (descriptor.controlType) {
|
|
59
|
-
case CheckboxDefinition.type:
|
|
60
|
-
case NumberDefinition.type:
|
|
61
|
-
case TextInputDefinition.type:
|
|
62
|
-
case TextAreaDefinition.type:
|
|
63
|
-
case SelectDefinition.type:
|
|
64
|
-
case ColorDefinition.type:
|
|
65
|
-
case IconRadioGroupDefinition.type:
|
|
66
|
-
case ImageDefinition.type:
|
|
67
|
-
case ComboboxDefinition.type:
|
|
68
|
-
case ShapeDefinition.type:
|
|
69
|
-
case ListDefinition.type:
|
|
70
|
-
case LinkDefinition.type:
|
|
71
|
-
case StyleV2Definition.type:
|
|
72
|
-
case unstable_TypographyDefinition.type:
|
|
73
|
-
return /* @__PURE__ */ jsx(
|
|
74
|
-
ControlValue,
|
|
75
|
-
{
|
|
76
|
-
definition: descriptor,
|
|
77
|
-
data: props[propName],
|
|
78
|
-
control: propControllers?.[propName],
|
|
79
|
-
children: (value) => renderFn({ ...propsValue, [propName]: value })
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
case StyleDefinition.type: {
|
|
83
|
-
const control = propControllers?.[propName] ?? null;
|
|
84
|
-
return /* @__PURE__ */ jsx(
|
|
85
|
-
RenderHook,
|
|
86
|
-
{
|
|
87
|
-
hook: useFormattedStyle,
|
|
88
|
-
parameters: [props[propName], descriptor, control],
|
|
89
|
-
children: (value) => renderFn({ ...propsValue, [propName]: value })
|
|
90
|
-
},
|
|
91
|
-
descriptor.controlType
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
case RichTextV1Definition.type: {
|
|
95
|
-
const control = propControllers?.[propName] ?? null;
|
|
96
|
-
return /* @__PURE__ */ jsx(
|
|
97
|
-
RenderHook,
|
|
98
|
-
{
|
|
99
|
-
hook: useRichText,
|
|
100
|
-
parameters: [props[propName], control],
|
|
101
|
-
children: (value) => renderFn({ ...propsValue, [propName]: value })
|
|
102
|
-
},
|
|
103
|
-
descriptor.controlType
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
case RichTextV2Definition.type: {
|
|
107
|
-
const control = propControllers?.[propName] ?? null;
|
|
108
|
-
return /* @__PURE__ */ jsx(
|
|
109
|
-
RenderHook,
|
|
110
|
-
{
|
|
111
|
-
hook: useRichTextV2,
|
|
112
|
-
parameters: [props[propName], descriptor, control],
|
|
113
|
-
children: (value) => renderFn({ ...propsValue, [propName]: value })
|
|
114
|
-
},
|
|
115
|
-
descriptor.controlType
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
case SlotDefinition.type: {
|
|
119
|
-
const control = propControllers?.[propName] ?? null;
|
|
120
|
-
return /* @__PURE__ */ jsx(
|
|
121
|
-
RenderHook,
|
|
122
|
-
{
|
|
123
|
-
hook: useSlot,
|
|
124
|
-
parameters: [props[propName], control],
|
|
125
|
-
children: (value) => renderFn({ ...propsValue, [propName]: value })
|
|
126
|
-
},
|
|
127
|
-
descriptor.controlType
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
console.error(`Unknown control type: ${descriptor.controlType}`);
|
|
132
|
-
return renderFn({ ...propsValue, [propName]: props[propName] });
|
|
133
|
-
},
|
|
134
|
-
children
|
|
135
|
-
)({});
|
|
20
|
+
return renderFn(resolvedProps);
|
|
136
21
|
}
|
|
137
22
|
export {
|
|
138
|
-
|
|
23
|
+
ResolveProps
|
|
139
24
|
};
|
|
140
25
|
//# sourceMappingURL=controls.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/runtimes/react/controls.tsx"],"sourcesContent":["import { useRef } from 'react'\n\nimport
|
|
1
|
+
{"version":3,"sources":["../../../../src/runtimes/react/controls.tsx"],"sourcesContent":["import { useRef } from 'react'\n\nimport { ControlDefinition } from '@makeswift/controls'\n\nimport { partitionRecord } from '../../utils/partition'\n\nimport * as ReactPage from '../../state/react-page'\nimport { isLegacyDescriptor, LegacyDescriptor } from '../../prop-controllers/descriptors'\n\nimport { useStore } from './hooks/use-store'\n\nimport { resolveLegacyDescriptorProp } from './legacy-controls'\nimport { useResolvedProps } from './hooks/use-resolved-props'\n\ntype PropsValueProps = {\n element: ReactPage.ElementData\n children(props: Record<string, unknown>): JSX.Element\n}\n\nfunction useControlDefs(\n elementType: string,\n): readonly [Record<string, LegacyDescriptor>, Record<string, ControlDefinition>] {\n const store = useStore()\n const all = ReactPage.getComponentPropControllerDescriptors(store.getState(), elementType) ?? {}\n return useRef(partitionRecord(all, isLegacyDescriptor)).current\n}\n\nexport function ResolveProps({ element, children: renderComponent }: PropsValueProps): JSX.Element {\n const [legacyDescriptors, definitions] = useControlDefs(element.type)\n\n const resolvedProps = useResolvedProps(definitions, element.props, element.key)\n\n const renderFn = Object.entries(legacyDescriptors).reduceRight(\n (renderFn, [propName, descriptor]) =>\n props =>\n resolveLegacyDescriptorProp(descriptor, propName, element.props[propName], props, renderFn),\n renderComponent,\n )\n\n return renderFn(resolvedProps)\n}\n"],"mappings":"AAAA,SAAS,cAAc;AAIvB,SAAS,uBAAuB;AAEhC,YAAY,eAAe;AAC3B,SAAS,0BAA4C;AAErD,SAAS,gBAAgB;AAEzB,SAAS,mCAAmC;AAC5C,SAAS,wBAAwB;AAOjC,SAAS,eACP,aACgF;AAChF,QAAM,QAAQ,SAAS;AACvB,QAAM,MAAM,UAAU,sCAAsC,MAAM,SAAS,GAAG,WAAW,KAAK,CAAC;AAC/F,SAAO,OAAO,gBAAgB,KAAK,kBAAkB,CAAC,EAAE;AAC1D;AAEO,SAAS,aAAa,EAAE,SAAS,UAAU,gBAAgB,GAAiC;AACjG,QAAM,CAAC,mBAAmB,WAAW,IAAI,eAAe,QAAQ,IAAI;AAEpE,QAAM,gBAAgB,iBAAiB,aAAa,QAAQ,OAAO,QAAQ,GAAG;AAE9E,QAAM,WAAW,OAAO,QAAQ,iBAAiB,EAAE;AAAA,IACjD,CAACA,WAAU,CAAC,UAAU,UAAU,MAC9B,WACE,4BAA4B,YAAY,UAAU,QAAQ,MAAM,QAAQ,GAAG,OAAOA,SAAQ;AAAA,IAC9F;AAAA,EACF;AAEA,SAAO,SAAS,aAAa;AAC/B;","names":["renderFn"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-css-id.ts"],"sourcesContent":["import { useId } from 'react'\n\nexport function useCssId(): string {\n return useId().replaceAll(':', '') // CSS class names prohibit colons\n}\n"],"mappings":"AAAA,SAAS,aAAa;AAEf,SAAS,WAAmB;AACjC,SAAO,MAAM,EAAE,WAAW,KAAK,EAAE;AACnC;","names":[]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useMemo, useEffect, useRef, useSyncExternalStore, useCallback } from "react";
|
|
2
|
+
import {
|
|
3
|
+
mapValues
|
|
4
|
+
} from "@makeswift/controls";
|
|
5
|
+
import * as ReactPage from "../../../state/react-page";
|
|
6
|
+
import { useResourceResolver } from "./use-resource-resolver";
|
|
7
|
+
import { useDocumentKey } from "./use-document-key";
|
|
8
|
+
import { useSelector } from "./use-selector";
|
|
9
|
+
import { useStylesheetFactory } from "./use-stylesheet-factory";
|
|
10
|
+
import { resolvableRecord } from "../resolvable-record";
|
|
11
|
+
function useControlInstances(elementKey) {
|
|
12
|
+
const documentKey = useDocumentKey();
|
|
13
|
+
return useSelector((state) => {
|
|
14
|
+
if (documentKey == null)
|
|
15
|
+
return null;
|
|
16
|
+
return ReactPage.getPropControllers(state, documentKey, elementKey);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function useResolvedProps(propDefs, elementData, elementKey) {
|
|
20
|
+
const stylesheetFactory = useStylesheetFactory();
|
|
21
|
+
const resourceResolver = useResourceResolver();
|
|
22
|
+
const controls = useControlInstances(elementKey);
|
|
23
|
+
const cache = useRef({}).current;
|
|
24
|
+
const resolveProp = useCallback(
|
|
25
|
+
(def, propName) => {
|
|
26
|
+
const data = elementData[propName];
|
|
27
|
+
const control = controls?.[propName];
|
|
28
|
+
if (cache[propName] != null && data === cache[propName].data && control === cache[propName].control) {
|
|
29
|
+
return cache[propName].resolvedValue;
|
|
30
|
+
}
|
|
31
|
+
const resolvedValue = def.resolveValue(
|
|
32
|
+
data,
|
|
33
|
+
resourceResolver,
|
|
34
|
+
stylesheetFactory.get(propName),
|
|
35
|
+
control
|
|
36
|
+
);
|
|
37
|
+
cache[propName] = { data, control, resolvedValue };
|
|
38
|
+
return resolvedValue;
|
|
39
|
+
},
|
|
40
|
+
[controls, elementData, resourceResolver, stylesheetFactory]
|
|
41
|
+
);
|
|
42
|
+
const resolvables = useMemo(() => mapValues(propDefs, resolveProp), [propDefs, resolveProp]);
|
|
43
|
+
const props = useMemo(() => resolvableRecord(resolvables), [resolvables]);
|
|
44
|
+
stylesheetFactory.useDefinedStyles();
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
props.triggerResolve();
|
|
47
|
+
}, []);
|
|
48
|
+
return useSyncExternalStore(props.subscribe, props.readStable, props.readStable);
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
useResolvedProps
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=use-resolved-props.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-resolved-props.ts"],"sourcesContent":["import { useMemo, useEffect, useRef, useSyncExternalStore, useCallback } from 'react'\nimport {\n ControlDefinition,\n ControlInstance,\n mapValues,\n type Data,\n type Resolvable,\n} from '@makeswift/controls'\n\nimport * as ReactPage from '../../../state/react-page'\nimport { useResourceResolver } from './use-resource-resolver'\nimport { useDocumentKey } from './use-document-key'\nimport { useSelector } from './use-selector'\n\nimport { useStylesheetFactory } from './use-stylesheet-factory'\n\nimport { resolvableRecord } from '../resolvable-record'\n\nfunction useControlInstances(elementKey: string): Record<string, ControlInstance> | null {\n const documentKey = useDocumentKey()\n\n return useSelector(state => {\n if (documentKey == null) return null\n\n return ReactPage.getPropControllers(state, documentKey, elementKey)\n })\n}\n\ntype CacheItem = {\n data: Data\n control: ControlInstance | undefined\n resolvedValue: Resolvable<unknown>\n}\n\nexport function useResolvedProps(\n propDefs: Record<string, ControlDefinition>,\n elementData: Record<string, Data>,\n elementKey: string,\n): Record<string, unknown> {\n const stylesheetFactory = useStylesheetFactory()\n const resourceResolver = useResourceResolver()\n const controls = useControlInstances(elementKey)\n\n const cache = useRef<Record<string, CacheItem>>({}).current\n const resolveProp = useCallback(\n (def: ControlDefinition, propName: string) => {\n const data = elementData[propName]\n const control = controls?.[propName]\n\n if (\n cache[propName] != null &&\n data === cache[propName].data &&\n control === cache[propName].control\n ) {\n return cache[propName].resolvedValue\n }\n\n const resolvedValue = def.resolveValue(\n data,\n resourceResolver,\n stylesheetFactory.get(propName),\n control,\n )\n\n cache[propName] = { data, control, resolvedValue }\n return resolvedValue\n },\n [controls, elementData, resourceResolver, stylesheetFactory],\n )\n\n const resolvables = useMemo(() => mapValues(propDefs, resolveProp), [propDefs, resolveProp])\n const props = useMemo(() => resolvableRecord(resolvables), [resolvables])\n\n stylesheetFactory.useDefinedStyles()\n\n useEffect(() => {\n props.triggerResolve()\n }, [])\n\n return useSyncExternalStore(props.subscribe, props.readStable, props.readStable)\n}\n"],"mappings":"AAAA,SAAS,SAAS,WAAW,QAAQ,sBAAsB,mBAAmB;AAC9E;AAAA,EAGE;AAAA,OAGK;AAEP,YAAY,eAAe;AAC3B,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAE5B,SAAS,4BAA4B;AAErC,SAAS,wBAAwB;AAEjC,SAAS,oBAAoB,YAA4D;AACvF,QAAM,cAAc,eAAe;AAEnC,SAAO,YAAY,WAAS;AAC1B,QAAI,eAAe;AAAM,aAAO;AAEhC,WAAO,UAAU,mBAAmB,OAAO,aAAa,UAAU;AAAA,EACpE,CAAC;AACH;AAQO,SAAS,iBACd,UACA,aACA,YACyB;AACzB,QAAM,oBAAoB,qBAAqB;AAC/C,QAAM,mBAAmB,oBAAoB;AAC7C,QAAM,WAAW,oBAAoB,UAAU;AAE/C,QAAM,QAAQ,OAAkC,CAAC,CAAC,EAAE;AACpD,QAAM,cAAc;AAAA,IAClB,CAAC,KAAwB,aAAqB;AAC5C,YAAM,OAAO,YAAY,QAAQ;AACjC,YAAM,UAAU,WAAW,QAAQ;AAEnC,UACE,MAAM,QAAQ,KAAK,QACnB,SAAS,MAAM,QAAQ,EAAE,QACzB,YAAY,MAAM,QAAQ,EAAE,SAC5B;AACA,eAAO,MAAM,QAAQ,EAAE;AAAA,MACzB;AAEA,YAAM,gBAAgB,IAAI;AAAA,QACxB;AAAA,QACA;AAAA,QACA,kBAAkB,IAAI,QAAQ;AAAA,QAC9B;AAAA,MACF;AAEA,YAAM,QAAQ,IAAI,EAAE,MAAM,SAAS,cAAc;AACjD,aAAO;AAAA,IACT;AAAA,IACA,CAAC,UAAU,aAAa,kBAAkB,iBAAiB;AAAA,EAC7D;AAEA,QAAM,cAAc,QAAQ,MAAM,UAAU,UAAU,WAAW,GAAG,CAAC,UAAU,WAAW,CAAC;AAC3F,QAAM,QAAQ,QAAQ,MAAM,iBAAiB,WAAW,GAAG,CAAC,WAAW,CAAC;AAExE,oBAAkB,iBAAiB;AAEnC,YAAU,MAAM;AACd,UAAM,eAAe;AAAA,EACvB,GAAG,CAAC,CAAC;AAEL,SAAO,qBAAqB,MAAM,WAAW,MAAM,YAAY,MAAM,UAAU;AACjF;","names":[]}
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { useMemo, useEffect, useSyncExternalStore } from "react";
|
|
2
|
+
import { useResourceResolver } from "./use-resource-resolver";
|
|
3
|
+
function useResolvedValue(data, resolver) {
|
|
4
|
+
const resourceResolver = useResourceResolver();
|
|
5
|
+
const resolvable = useMemo(() => resolver(data, resourceResolver), [data, resourceResolver]);
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
resolvable.triggerResolve();
|
|
8
|
+
}, []);
|
|
9
|
+
return useSyncExternalStore(resolvable.subscribe, resolvable.readStable, resolvable.readStable);
|
|
3
10
|
}
|
|
4
11
|
export {
|
|
5
12
|
useResolvedValue
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-resolved-value.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-resolved-value.ts"],"sourcesContent":["import { useMemo, useEffect, useSyncExternalStore } from 'react'\nimport { Resolvable, ResourceResolver } from '@makeswift/controls'\n\nimport { useResourceResolver } from './use-resource-resolver'\n\nexport function useResolvedValue<D, T>(\n data: D,\n resolver: (data: D, resourceResolver: ResourceResolver) => Resolvable<T>,\n): T {\n const resourceResolver = useResourceResolver()\n const resolvable = useMemo(() => resolver(data, resourceResolver), [data, resourceResolver])\n\n useEffect(() => {\n resolvable.triggerResolve()\n }, [])\n\n return useSyncExternalStore(resolvable.subscribe, resolvable.readStable, resolvable.readStable)\n}\n"],"mappings":"AAAA,SAAS,SAAS,WAAW,4BAA4B;AAGzD,SAAS,2BAA2B;AAE7B,SAAS,iBACd,MACA,UACG;AACH,QAAM,mBAAmB,oBAAoB;AAC7C,QAAM,aAAa,QAAQ,MAAM,SAAS,MAAM,gBAAgB,GAAG,CAAC,MAAM,gBAAgB,CAAC;AAE3F,YAAU,MAAM;AACd,eAAW,eAAe;AAAA,EAC5B,GAAG,CAAC,CAAC;AAEL,SAAO,qBAAqB,WAAW,WAAW,WAAW,YAAY,WAAW,UAAU;AAChG;","names":[]}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import * as ReactPage from "../../../state/react-page";
|
|
3
|
+
import { useStore } from "./use-store";
|
|
4
|
+
import { useDocumentKey } from "./use-document-key";
|
|
5
|
+
import { useMakeswiftHostApiClient } from "../../../next/context/makeswift-host-api-client";
|
|
6
|
+
function useResourceResolver() {
|
|
7
|
+
const store = useStore();
|
|
8
|
+
const client = useMakeswiftHostApiClient();
|
|
9
|
+
const documentKey = useDocumentKey();
|
|
10
|
+
return useMemo(() => {
|
|
11
|
+
return {
|
|
12
|
+
resolveSwatch: (swatchId) => client.resolveSwatch(swatchId),
|
|
13
|
+
resolveFile: (fileId) => client.resolveFile(fileId),
|
|
14
|
+
resolveTypography: (typographyId) => client.resolveTypography(typographyId),
|
|
15
|
+
resolvePagePathnameSlice: (pageId) => client.resolvePagePathnameSlice(pageId),
|
|
16
|
+
resolveElementId: (elementKey) => {
|
|
17
|
+
const read = () => documentKey == null ? null : ReactPage.getElementId(store.getState(), documentKey, elementKey);
|
|
18
|
+
let lastValue = null;
|
|
19
|
+
return {
|
|
20
|
+
name: `element-id:${documentKey}:${elementKey}`,
|
|
21
|
+
readStable: () => lastValue = read(),
|
|
22
|
+
subscribe: (onUpdate) => store.subscribe(() => {
|
|
23
|
+
if (read() !== lastValue)
|
|
24
|
+
onUpdate();
|
|
25
|
+
})
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}, [client, store, documentKey]);
|
|
30
|
+
}
|
|
31
|
+
export {
|
|
32
|
+
useResourceResolver
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=use-resource-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/runtimes/react/hooks/use-resource-resolver.ts"],"sourcesContent":["import { useMemo } from 'react'\nimport { type ResourceResolver } from '@makeswift/controls'\n\nimport * as ReactPage from '../../../state/react-page'\nimport { useStore } from './use-store'\nimport { useDocumentKey } from './use-document-key'\nimport { useMakeswiftHostApiClient } from '../../../next/context/makeswift-host-api-client'\n\nexport function useResourceResolver(): ResourceResolver {\n const store = useStore()\n const client = useMakeswiftHostApiClient()\n const documentKey = useDocumentKey()\n\n return useMemo<ResourceResolver>(() => {\n return {\n resolveSwatch: swatchId => client.resolveSwatch(swatchId),\n resolveFile: fileId => client.resolveFile(fileId),\n resolveTypography: typographyId => client.resolveTypography(typographyId),\n resolvePagePathnameSlice: pageId => client.resolvePagePathnameSlice(pageId),\n resolveElementId: elementKey => {\n const read = () =>\n documentKey == null\n ? null\n : ReactPage.getElementId(store.getState(), documentKey, elementKey)\n\n let lastValue: string | null = null\n return {\n name: `element-id:${documentKey}:${elementKey}`,\n readStable: () => (lastValue = read()),\n subscribe: (onUpdate: () => void) =>\n store.subscribe(() => {\n if (read() !== lastValue) onUpdate()\n }),\n }\n },\n }\n }, [client, store, documentKey])\n}\n"],"mappings":"AAAA,SAAS,eAAe;AAGxB,YAAY,eAAe;AAC3B,SAAS,gBAAgB;AACzB,SAAS,sBAAsB;AAC/B,SAAS,iCAAiC;AAEnC,SAAS,sBAAwC;AACtD,QAAM,QAAQ,SAAS;AACvB,QAAM,SAAS,0BAA0B;AACzC,QAAM,cAAc,eAAe;AAEnC,SAAO,QAA0B,MAAM;AACrC,WAAO;AAAA,MACL,eAAe,cAAY,OAAO,cAAc,QAAQ;AAAA,MACxD,aAAa,YAAU,OAAO,YAAY,MAAM;AAAA,MAChD,mBAAmB,kBAAgB,OAAO,kBAAkB,YAAY;AAAA,MACxE,0BAA0B,YAAU,OAAO,yBAAyB,MAAM;AAAA,MAC1E,kBAAkB,gBAAc;AAC9B,cAAM,OAAO,MACX,eAAe,OACX,OACA,UAAU,aAAa,MAAM,SAAS,GAAG,aAAa,UAAU;AAEtE,YAAI,YAA2B;AAC/B,eAAO;AAAA,UACL,MAAM,cAAc,WAAW,IAAI,UAAU;AAAA,UAC7C,YAAY,MAAO,YAAY,KAAK;AAAA,UACpC,WAAW,CAAC,aACV,MAAM,UAAU,MAAM;AACpB,gBAAI,KAAK,MAAM;AAAW,uBAAS;AAAA,UACrC,CAAC;AAAA,QACL;AAAA,MACF;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,OAAO,WAAW,CAAC;AACjC;","names":[]}
|