1
0
Fork 0
freesewing/sites/shared/components/mdx/tabbed-example.mjs

165 lines
4.7 KiB
JavaScript
Raw Normal View History

import { Tab, Tabs } from './tabs.mjs'
import Md from 'react-markdown'
import { pluginBundle } from '@freesewing/plugin-bundle'
import { pluginFlip } from '@freesewing/plugin-flip'
import { pluginGore } from '@freesewing/plugin-gore'
import { Design } from '@freesewing/core'
2023-06-07 14:12:51 -05:00
// import { Svg } from 'pkgs/react-components/src/index.mjs'
//import { Defs } from '../workbench/pattern/defs'
//import { Stack } from '../workbench/pattern/stack'
2023-06-07 14:12:51 -05:00
// import { useState } from 'react'
import yaml from 'js-yaml'
2022-09-22 09:05:20 +02:00
// Get code from children
2022-09-30 00:08:53 +02:00
export const asText = (reactEl) => {
if (typeof reactEl.props.children === 'string') return reactEl.props.children
if (Array.isArray(reactEl.props.children)) {
return reactEl.props.children.map((el) => (typeof el === 'string' ? el : asText(el))).join('')
}
if (typeof reactEl.props.children === 'object') return asText(reactEl.props.children)
return ''
2022-09-22 09:05:20 +02:00
}
// The actual example
2023-05-19 11:46:17 +02:00
export const Example = ({
patternProps,
2023-06-07 14:12:51 -05:00
// settings,
// showInfo,
// xray = false,
2023-05-19 11:46:17 +02:00
}) => {
2023-06-07 14:12:51 -05:00
// const [ui, setUi] = useState({ renderer: 'react', xray: { enabled: xray } })
2022-09-22 14:17:29 +02:00
if (patternProps.logs.pattern.error.length > 0 || patternProps.logs.sets[0].error.length > 0)
return (
<div className="max-w-full p-4">
<pre>{patternProps.logs.pattern.error.join('\n')}</pre>
<pre>{patternProps.logs.sets[0].error.join('\n')}</pre>
</div>
)
2022-09-22 09:05:20 +02:00
return null
// FIXME
//return (
// <Svg {...patternProps} settings={settings} embed={true}>
// <Defs {...patternProps} />
// <style>{`:root { --pattern-scale: 1} ${patternProps.svg.style}`}</style>
// <g>
// {Object.keys(patternProps.stacks).map((stackName) => (
// <Stack
// {...{ showInfo, patternProps, settings, ui }}
// key={stackName}
// stackName={stackName}
// stack={patternProps.stacks[stackName]}
// />
// ))}
// </g>
// </Svg>
//)
2022-09-22 09:05:20 +02:00
}
// Returns a FreeSewing draft based on code in children
const buildExample = (children, settings = { margin: 5 }, tutorial = false, paperless = false) => {
let code = asText(children)
// FIXME: Refactor to not use eval
let draft
if (code.split('\n')[0].includes('function')) {
code = '(' + code.split('(').slice(1).join('(')
code = code.split(')')
code = code[0] + ') => ' + code.slice(1).join(')')
}
try {
draft = eval(code)
} catch (err) {
console.log(err, code)
}
const part = {
draft: draft,
name: tutorial ? 'tutorial.bib' : 'example',
measurements: tutorial ? [] : ['head'],
options: tutorial
? {
neckRatio: { pct: 80, min: 70, max: 90, menu: 'fit' },
widthRatio: { pct: 45, min: 35, max: 55, menu: 'style' },
lengthRatio: { pct: 75, min: 55, max: 85, menu: 'style' },
}
: {},
plugins: [pluginBundle, pluginFlip, pluginGore],
}
const design = new Design({
parts: [part],
data: tutorial ? { name: 'Tutorial', version: '0.0.1' } : { name: 'Example', version: '0.0.1' },
})
if (tutorial) settings.measurements = { head: 380 }
if (paperless) settings.paperless = true
return new design(settings)
}
// Wrapper component dealing with the tabs and code view
export const TabbedExample = ({
children,
caption,
tutorial,
previewFirst,
withHead,
paperless,
settings,
}) => {
if (settings)
settings = {
margin: 5,
...yaml.load(settings),
}
else settings = { margin: 5 }
if (withHead) settings.measurements = { head: 300 }
const draft = buildExample(children, settings, tutorial, paperless)
if (!draft.sample) return null
const patternProps = settings.sample
? draft.sample().getRenderProps()
: draft.draft().getRenderProps()
if (tutorial && !previewFirst)
return (
<div className="my-8">
<Tabs tabs="Code, Preview, X-Ray">
<Tab key="code">{children}</Tab>
<Tab key="preview">
2023-06-07 14:12:51 -05:00
<Example {...{ patternProps, settings }} />
</Tab>
<Tab key="xray">
2023-06-07 14:12:51 -05:00
<Example {...{ patternProps, settings }} xray={true} />
</Tab>
</Tabs>
{caption && (
<div className="text-center italic -mt-4">
<Md>{caption}</Md>
</div>
)}
</div>
)
return (
<div className="my-8">
<Tabs tabs="Preview, Code, X-Ray">
<Tab key="preview">
2023-06-07 14:12:51 -05:00
<Example {...{ patternProps, settings }} />
</Tab>
<Tab key="code">{children}</Tab>
<Tab key="xray">
2023-06-07 14:12:51 -05:00
<Example {...{ patternProps, settings }} xray={true} />
</Tab>
</Tabs>
{caption && (
<div className="text-center italic -mt-4">
<Md>{caption}</Md>
</div>
)}
</div>
)
}