2023-06-01 18:28:22 +02:00
|
|
|
import React from 'react'
|
|
|
|
import { translateStrings } from './utils.mjs'
|
|
|
|
|
2025-05-25 16:29:57 +02:00
|
|
|
/**
|
|
|
|
* A component to render a tspan tag in a FreeSewing pattern
|
|
|
|
*
|
|
|
|
* @component
|
|
|
|
* @param {object} props - All component props
|
|
|
|
* @param {object} props.point - The point that the text is defined on
|
|
|
|
* @param {object} strings - The translation strings
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
*/
|
2025-04-01 16:15:20 +02:00
|
|
|
export const TextSpans = ({ point, strings }) => {
|
|
|
|
const translated = translateStrings(point.attributes.list['data-text'], strings)
|
2023-06-01 18:28:22 +02:00
|
|
|
const text = []
|
|
|
|
if (translated.indexOf('\n') !== -1) {
|
|
|
|
// Handle muti-line text
|
|
|
|
let key = 0
|
|
|
|
let lines = translated.split('\n')
|
|
|
|
text.push(<tspan key={'tspan-' + key}>{lines.shift()}</tspan>)
|
|
|
|
for (let line of lines) {
|
|
|
|
key++
|
|
|
|
text.push(
|
|
|
|
<tspan
|
|
|
|
key={'tspan-' + key}
|
|
|
|
x={point.x}
|
|
|
|
dy={point.attributes.list['data-text-lineheight']?.[0] || 12}
|
|
|
|
>
|
|
|
|
{line.toString().replace(/"/g, '"')}
|
|
|
|
</tspan>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else text.push(<tspan key="tspan">{translated}</tspan>)
|
|
|
|
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2025-05-25 16:29:57 +02:00
|
|
|
/**
|
|
|
|
* A component to render a text tag in a FreeSewing pattern
|
|
|
|
*
|
|
|
|
* @component
|
|
|
|
* @param {object} props - All component props
|
|
|
|
* @param {object} props.point - The point that the text is defined on
|
|
|
|
* @param {object} strings - The translation strings
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
*/
|
2025-04-01 16:15:20 +02:00
|
|
|
export const Text = ({ point, strings }) => (
|
2023-06-01 18:28:22 +02:00
|
|
|
<text x={point.x} y={point.y} {...point.attributes.textProps}>
|
2025-04-01 16:15:20 +02:00
|
|
|
<TextSpans point={point} strings={strings} />
|
2023-06-01 18:28:22 +02:00
|
|
|
</text>
|
|
|
|
)
|
|
|
|
|
2025-05-25 16:29:57 +02:00
|
|
|
/**
|
|
|
|
* A component to render a text along a path in a FreeSewing pattern
|
|
|
|
*
|
|
|
|
* @component
|
|
|
|
* @param {object} props - All component props
|
|
|
|
* @param {object} props.path - The path that the text is to be rendered along
|
|
|
|
* @param {string} props.pathId - The ID of the path
|
|
|
|
* @param {object} strings - The translation strings
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
*/
|
2025-04-01 16:15:20 +02:00
|
|
|
export const TextOnPath = ({ path, pathId, strings }) => {
|
2023-06-01 18:28:22 +02:00
|
|
|
const textPathProps = {
|
|
|
|
xlinkHref: '#' + pathId,
|
|
|
|
startOffset: '0%',
|
|
|
|
}
|
2025-04-01 16:15:20 +02:00
|
|
|
const translated = translateStrings(path.attributes.text, strings)
|
2023-06-11 18:03:50 +02:00
|
|
|
const align = path.attributes.list['data-text-class']
|
|
|
|
? path.attributes.list['data-text-class'].join(' ')
|
|
|
|
: false
|
2023-06-01 18:28:22 +02:00
|
|
|
if (align && align.indexOf('center') > -1) textPathProps.startOffset = '50%'
|
|
|
|
else if (align && align.indexOf('right') > -1) textPathProps.startOffset = '100%'
|
|
|
|
|
|
|
|
return (
|
|
|
|
<text>
|
|
|
|
<textPath {...textPathProps}>
|
|
|
|
<tspan {...path.attributes.textProps} dangerouslySetInnerHTML={{ __html: translated }} />
|
|
|
|
</textPath>
|
|
|
|
</text>
|
|
|
|
)
|
|
|
|
}
|