2024-12-22 19:00:06 +01:00
|
|
|
// Dependencies
|
|
|
|
import { cloudflareImageUrl } from '@freesewing/utils'
|
|
|
|
// Components
|
2023-06-06 13:24:20 -05:00
|
|
|
import React, { forwardRef } from 'react'
|
2023-06-01 18:28:22 +02:00
|
|
|
import { Svg as DefaultSvg } from './svg.mjs'
|
|
|
|
import { Defs as DefaultDefs } from './defs.mjs'
|
|
|
|
import { Group as DefaultGroup } from './group.mjs'
|
|
|
|
import { Stack as DefaultStack } from './stack.mjs'
|
|
|
|
import { Part as DefaultPart } from './part.mjs'
|
|
|
|
import { Point as DefaultPoint } from './point.mjs'
|
|
|
|
import { Snippet as DefaultSnippet } from './snippet.mjs'
|
|
|
|
import { Path as DefaultPath } from './path.mjs'
|
|
|
|
import { Grid as DefaultGrid } from './grid.mjs'
|
|
|
|
import { Text as DefaultText, TextOnPath as DefaultTextOnPath } from './text.mjs'
|
2023-06-04 23:28:43 -05:00
|
|
|
import { Circle as DefaultCircle } from './circle.mjs'
|
2024-12-10 18:03:03 +01:00
|
|
|
import { getId, getProps, withinPartBounds, translateStrings } from './utils.mjs'
|
2024-12-22 19:00:06 +01:00
|
|
|
import { Link as WebLink } from '@freesewing/react/components/Link'
|
2023-06-01 18:28:22 +02:00
|
|
|
|
|
|
|
/*
|
2024-12-10 18:03:03 +01:00
|
|
|
* Allow people to override these components
|
2023-06-01 18:28:22 +02:00
|
|
|
*/
|
2024-12-10 18:03:03 +01:00
|
|
|
const defaultComponents = {
|
2023-06-01 18:28:22 +02:00
|
|
|
Svg: DefaultSvg,
|
|
|
|
Defs: DefaultDefs,
|
|
|
|
Group: DefaultGroup,
|
|
|
|
Stack: DefaultStack,
|
|
|
|
Part: DefaultPart,
|
|
|
|
Point: DefaultPoint,
|
|
|
|
Path: DefaultPath,
|
|
|
|
Snippet: DefaultSnippet,
|
|
|
|
Grid: DefaultGrid,
|
|
|
|
Text: DefaultText,
|
|
|
|
TextOnPath: DefaultTextOnPath,
|
2023-06-04 23:28:43 -05:00
|
|
|
Circle: DefaultCircle,
|
2023-06-01 18:28:22 +02:00
|
|
|
}
|
|
|
|
|
2024-12-10 18:03:03 +01:00
|
|
|
/*
|
|
|
|
* The pattern component
|
|
|
|
* FIXME: document props
|
|
|
|
*/
|
|
|
|
const Pattern = forwardRef((props, ref) => {
|
|
|
|
if (!props.renderProps) return null
|
|
|
|
|
|
|
|
// Destructure props
|
|
|
|
const {
|
|
|
|
renderProps = false,
|
|
|
|
t = (string) => string,
|
|
|
|
children = false,
|
|
|
|
className = 'freesewing pattern',
|
|
|
|
components = {},
|
|
|
|
} = props
|
2023-06-01 18:28:22 +02:00
|
|
|
|
2024-12-10 18:03:03 +01:00
|
|
|
// Merge default and swizzled components
|
|
|
|
const mergedComponents = {
|
|
|
|
...defaultComponents,
|
|
|
|
...components,
|
|
|
|
}
|
2023-06-01 18:28:22 +02:00
|
|
|
|
2024-12-10 18:03:03 +01:00
|
|
|
const { Svg, Defs, Stack, Group } = mergedComponents
|
2023-06-01 18:28:22 +02:00
|
|
|
|
2024-12-10 18:03:03 +01:00
|
|
|
const optionalProps = {}
|
|
|
|
if (className) optionalProps.className = className
|
2023-06-01 18:28:22 +02:00
|
|
|
|
2024-12-10 18:03:03 +01:00
|
|
|
return (
|
|
|
|
<Svg
|
|
|
|
viewBox={`0 0 ${renderProps.width} ${renderProps.height}`}
|
|
|
|
embed={renderProps.settings.embed}
|
|
|
|
{...renderProps}
|
|
|
|
{...optionalProps}
|
|
|
|
ref={ref}
|
|
|
|
>
|
|
|
|
<Defs {...renderProps} />
|
|
|
|
<style>{`:root { --pattern-scale: ${renderProps.settings.scale || 1}} ${
|
|
|
|
renderProps.svg.style
|
|
|
|
}`}</style>
|
|
|
|
<Group>
|
|
|
|
{children
|
|
|
|
? children
|
|
|
|
: Object.keys(renderProps.stacks).map((stackName) => (
|
|
|
|
<Stack
|
|
|
|
key={stackName}
|
|
|
|
stackName={stackName}
|
|
|
|
stack={renderProps.stacks[stackName]}
|
|
|
|
settings={renderProps.settings}
|
|
|
|
components={mergedComponents}
|
|
|
|
t={t}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Group>
|
|
|
|
</Svg>
|
|
|
|
)
|
|
|
|
})
|
2023-06-07 10:06:30 -05:00
|
|
|
|
2024-12-10 18:03:03 +01:00
|
|
|
export {
|
|
|
|
// utils
|
|
|
|
getId,
|
|
|
|
getProps,
|
|
|
|
withinPartBounds,
|
|
|
|
translateStrings,
|
|
|
|
// default components
|
|
|
|
defaultComponents,
|
|
|
|
// The Pattern component itself
|
|
|
|
Pattern,
|
|
|
|
}
|