1
0
Fork 0
freesewing/sites/shared/components/workbench/layout/cut/index.mjs

127 lines
4 KiB
JavaScript
Raw Normal View History

2022-02-20 18:46:21 +01:00
import { useTranslation } from 'next-i18next'
2023-02-05 19:58:25 +01:00
import { CutLayoutSettings } from './settings.mjs'
import { Draft } from '../draft/index.mjs'
2023-02-19 16:24:46 +02:00
import { fabricPlugin } from '../plugin-layout-part.mjs'
import { cutLayoutPlugin } from './plugin-cut-layout.mjs'
import { pluginAnnotations } from '@freesewing/plugin-annotations'
import { measurementAsMm } from 'shared/utils.mjs'
2023-03-08 17:06:46 -06:00
import { useEffect } from 'react'
import get from 'lodash.get'
2022-02-20 18:46:21 +01:00
2023-03-15 12:48:46 -05:00
export const fabricSettingsOrDefault = (gist, fabric) => {
2023-03-01 12:34:18 -06:00
const isImperial = gist.units === 'imperial'
const sheetHeight = measurementAsMm(isImperial ? 36 : 100, gist.units)
2023-03-15 12:48:46 -05:00
const gistSettings = get(gist, ['_state', 'layout', 'forCutting', 'fabric', fabric])
2023-03-01 12:34:18 -06:00
const sheetWidth = gistSettings?.sheetWidth || measurementAsMm(isImperial ? 54 : 120, gist.units)
const grainDirection =
gistSettings?.grainDirection === undefined ? 90 : gistSettings.grainDirection
2023-03-15 12:48:46 -05:00
return { activeFabric: fabric, sheetWidth, grainDirection, sheetHeight }
}
const activeFabricPath = ['_state', 'layout', 'forCutting', 'activeFabric']
const useFabricSettings = (gist) => {
const activeFabric = get(gist, activeFabricPath) || 'fabric'
return fabricSettingsOrDefault(gist, activeFabric)
2023-03-01 12:34:18 -06:00
}
2023-03-09 17:45:10 -06:00
const useFabricDraft = (gist, design, fabricSettings) => {
2023-03-01 12:34:18 -06:00
// get the appropriate layout for the view
const layout =
get(gist, ['layouts', gist._state.view, fabricSettings.activeFabric]) || gist.layout || true
// hand it separately to the design
const draft = new design({ ...gist, layout })
const layoutSettings = {
sheetWidth: fabricSettings.sheetWidth,
sheetHeight: fabricSettings.sheetHeight,
}
let patternProps
try {
// add the fabric plugin to the draft
draft.use(fabricPlugin(layoutSettings))
// add the cutLayout plugin
draft.use(cutLayoutPlugin(fabricSettings.activeFabric, fabricSettings.grainDirection))
// also, pluginAnnotations and pluginFlip are needed
draft.use(pluginAnnotations)
2023-03-01 12:34:18 -06:00
// draft the pattern
draft.draft()
patternProps = draft.getRenderProps()
} catch (err) {
console.log(err, gist)
}
return { draft, patternProps }
}
const useFabricList = (draft) => {
2023-03-15 12:48:46 -05:00
return draft.setStores[0].cutlist.getCutFabrics(draft.settings[0])
2023-03-01 12:34:18 -06:00
}
const bgProps = { fill: 'none' }
export const CutLayout = (props) => {
const { t } = useTranslation(['workbench', 'plugin'])
2023-03-01 12:34:18 -06:00
const { gist, design, updateGist } = props
2022-02-20 18:46:21 +01:00
// disable xray
useEffect(() => {
2023-03-01 12:34:18 -06:00
if (gist?._state?.xray?.enabled) updateGist(['_state', 'xray', 'enabled'], false)
})
2023-03-01 12:34:18 -06:00
const fabricSettings = useFabricSettings(gist)
2023-03-09 17:45:10 -06:00
const { draft, patternProps } = useFabricDraft(gist, design, fabricSettings)
2023-03-01 12:34:18 -06:00
const fabricList = useFabricList(draft)
2023-03-01 12:34:18 -06:00
const setCutFabric = (newFabric) => {
updateGist(activeFabricPath, newFabric)
}
2023-03-01 12:34:18 -06:00
let name = design.designConfig.data.name
name = name.replace('@freesewing/', '')
2023-03-01 12:34:18 -06:00
const settingsProps = {
gist,
updateGist,
patternProps,
unsetGist: props.unsetGist,
...fabricSettings,
}
return patternProps ? (
2022-02-20 18:46:21 +01:00
<div>
<h2 className="capitalize">{t('layoutThing', { thing: name }) + ': ' + t('forCutting')}</h2>
2023-03-01 12:34:18 -06:00
<CutLayoutSettings {...settingsProps} />
<div className="my-4">
2023-03-01 12:34:18 -06:00
{fabricList.length > 1 ? (
<div className="tabs">
{fabricList.map((title) => (
<button
key={title}
className={`text-xl font-bold capitalize tab tab-bordered grow ${
fabricSettings.activeFabric === title ? 'tab-active' : ''
}`}
onClick={() => setCutFabric(title)}
>
{t('plugin:' + title)}
2023-03-01 12:34:18 -06:00
</button>
))}
</div>
) : null}
<Draft
draft={draft}
2023-03-01 12:34:18 -06:00
gist={gist}
updateGist={updateGist}
patternProps={patternProps}
bgProps={bgProps}
gistReady={props.gistReady}
layoutPart="fabric"
2023-03-01 12:34:18 -06:00
layoutType={['cuttingLayout', fabricSettings.activeFabric]}
layoutSetType="forCutting"
/>
</div>
2022-02-20 18:46:21 +01:00
</div>
) : null
2022-02-20 18:46:21 +01:00
}