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'
|
2023-02-16 15:21:55 -06:00
|
|
|
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 { pluginCutlist } from '@freesewing/plugin-cutlist'
|
2023-02-18 16:18:35 +02:00
|
|
|
import { measurementAsMm } from 'shared/utils.mjs'
|
2023-03-08 17:06:46 -06:00
|
|
|
import { useEffect } from 'react'
|
2023-02-27 17:47:34 -06:00
|
|
|
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, pluginCutlist and pluginFlip are needed
|
|
|
|
draft.use(pluginCutlist)
|
|
|
|
|
|
|
|
// 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' }
|
2023-01-29 18:57:24 +01:00
|
|
|
export const CutLayout = (props) => {
|
2023-03-09 08:52:28 -06:00
|
|
|
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
|
|
|
|
2023-02-16 15:21:55 -06:00
|
|
|
// disable xray
|
|
|
|
useEffect(() => {
|
2023-03-01 12:34:18 -06:00
|
|
|
if (gist?._state?.xray?.enabled) updateGist(['_state', 'xray', 'enabled'], false)
|
2023-02-16 15:21:55 -06:00
|
|
|
})
|
|
|
|
|
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-02-27 17:47:34 -06:00
|
|
|
|
2023-03-01 12:34:18 -06:00
|
|
|
const setCutFabric = (newFabric) => {
|
|
|
|
updateGist(activeFabricPath, newFabric)
|
|
|
|
}
|
2023-02-27 17:47:34 -06:00
|
|
|
|
2023-03-01 12:34:18 -06:00
|
|
|
let name = design.designConfig.data.name
|
2022-09-01 23:06:37 -07:00
|
|
|
name = name.replace('@freesewing/', '')
|
2023-02-27 17:47:34 -06:00
|
|
|
|
2023-03-01 12:34:18 -06:00
|
|
|
const settingsProps = {
|
|
|
|
gist,
|
|
|
|
updateGist,
|
|
|
|
patternProps,
|
|
|
|
unsetGist: props.unsetGist,
|
|
|
|
...fabricSettings,
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:47:34 -06:00
|
|
|
return patternProps ? (
|
2022-02-20 18:46:21 +01:00
|
|
|
<div>
|
2023-01-29 18:57:24 +01:00
|
|
|
<h2 className="capitalize">{t('layoutThing', { thing: name }) + ': ' + t('forCutting')}</h2>
|
2023-03-01 12:34:18 -06:00
|
|
|
<CutLayoutSettings {...settingsProps} />
|
2023-02-27 17:47:34 -06:00
|
|
|
<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)}
|
|
|
|
>
|
2023-03-09 08:52:28 -06:00
|
|
|
{t('plugin:' + title)}
|
2023-03-01 12:34:18 -06:00
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2023-02-27 17:47:34 -06:00
|
|
|
<Draft
|
|
|
|
draft={draft}
|
2023-03-01 12:34:18 -06:00
|
|
|
gist={gist}
|
|
|
|
updateGist={updateGist}
|
2023-02-27 17:47:34 -06:00
|
|
|
patternProps={patternProps}
|
|
|
|
bgProps={bgProps}
|
|
|
|
gistReady={props.gistReady}
|
|
|
|
layoutPart="fabric"
|
2023-03-01 12:34:18 -06:00
|
|
|
layoutType={['cuttingLayout', fabricSettings.activeFabric]}
|
2023-03-09 08:52:28 -06:00
|
|
|
layoutSetType="forCutting"
|
2023-02-27 17:47:34 -06:00
|
|
|
/>
|
|
|
|
</div>
|
2022-02-20 18:46:21 +01:00
|
|
|
</div>
|
2023-02-27 17:47:34 -06:00
|
|
|
) : null
|
2022-02-20 18:46:21 +01:00
|
|
|
}
|