2023-02-27 17:47:34 -06:00
|
|
|
import { useMemo, useEffect, useState, useCallback, useRef } from 'react'
|
|
|
|
import { ClearIcon, PageIcon } from 'shared/components/icons.mjs'
|
2023-02-16 15:21:55 -06:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-02-18 16:18:35 +02:00
|
|
|
import { formatFraction128, measurementAsMm, round, formatMm } from 'shared/utils.mjs'
|
2023-02-27 17:47:34 -06:00
|
|
|
import get from 'lodash.get'
|
2023-02-16 15:21:55 -06:00
|
|
|
|
2023-02-27 17:47:34 -06:00
|
|
|
const FabricSizer = ({ gist, updateGist, cutFabric, sheetWidth }) => {
|
2023-02-16 15:21:55 -06:00
|
|
|
const { t } = useTranslation(['workbench'])
|
2023-02-18 16:18:35 +02:00
|
|
|
|
2023-02-27 17:47:34 -06:00
|
|
|
let val = formatMm(sheetWidth, gist.units, 'none')
|
2023-02-18 16:18:35 +02:00
|
|
|
// onChange
|
2023-02-27 17:47:34 -06:00
|
|
|
const update = (evt) => {
|
|
|
|
evt.stopPropagation()
|
|
|
|
let evtVal = evt.target.value
|
|
|
|
// set Val immediately so that the input reflects it
|
|
|
|
val = evtVal
|
2023-02-18 16:18:35 +02:00
|
|
|
|
2023-02-27 17:47:34 -06:00
|
|
|
let useVal = measurementAsMm(evtVal, gist.units)
|
|
|
|
// only set to the gist if it's valid
|
|
|
|
if (!isNaN(useVal)) {
|
|
|
|
updateGist(['_state', 'layout', 'forCutting', 'fabric', cutFabric, 'sheetWidth'], useVal)
|
|
|
|
}
|
|
|
|
}
|
2023-02-18 16:18:35 +02:00
|
|
|
|
|
|
|
return (
|
2023-02-27 17:47:34 -06:00
|
|
|
<div className="flex gap-4 px-0 font-bold items-center">
|
2023-02-18 16:18:35 +02:00
|
|
|
<div className="form-control mb-2 flex flex-row" key="wrap-fabricWidth">
|
|
|
|
<label className="input-group input-group-xs">
|
2023-02-27 17:47:34 -06:00
|
|
|
<span className="label-text font-bold">{`${t(cutFabric)} ${t('width')}`}</span>
|
2023-02-18 16:18:35 +02:00
|
|
|
<input
|
|
|
|
key="input-fabricWidth"
|
|
|
|
type="text"
|
|
|
|
className="input input-sm input-bordered grow text-base-content border-r-0"
|
|
|
|
value={val}
|
|
|
|
onChange={update}
|
|
|
|
/>
|
|
|
|
<span className="bg-transparent border input-bordered">
|
|
|
|
{gist.units == 'metric' ? 'cm' : 'in'}
|
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const useFabricLength = (isImperial, height) => {
|
|
|
|
// regular conversion from mm to inches or cm
|
|
|
|
const unit = isImperial ? 25.4 : 10
|
|
|
|
// conversion from inches or cm to yards or meters
|
|
|
|
const fabricUnit = isImperial ? 36 : 100
|
|
|
|
// for fabric, these divisions are granular enough
|
|
|
|
const rounder = isImperial ? 16 : 10
|
|
|
|
|
|
|
|
// we convert the used fabric height to the right units so we can round it
|
|
|
|
const inFabricUnits = height / (fabricUnit * unit)
|
|
|
|
// we multiply it by the rounder, round it up, then divide by the rounder again to get the rounded amount
|
|
|
|
const roundCount = Math.ceil(rounder * inFabricUnits) / rounder
|
|
|
|
// format as a fraction for imperial, a decimal for metric
|
|
|
|
const count = isImperial ? formatFraction128(roundCount, 'none') : round(roundCount, 1)
|
|
|
|
|
|
|
|
return `${count}${isImperial ? 'yds' : 'm'}`
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:47:34 -06:00
|
|
|
export const CutLayoutSettings = ({
|
|
|
|
gist,
|
|
|
|
patternProps,
|
|
|
|
unsetGist,
|
|
|
|
updateGist,
|
|
|
|
cutFabric,
|
|
|
|
sheetWidth,
|
|
|
|
}) => {
|
2023-02-18 16:18:35 +02:00
|
|
|
const { t } = useTranslation(['workbench'])
|
|
|
|
|
|
|
|
const fabricLength = useFabricLength(gist.units === 'imperial', patternProps.height)
|
2023-02-16 15:21:55 -06:00
|
|
|
|
2022-02-20 18:46:21 +01:00
|
|
|
return (
|
2023-02-27 17:47:34 -06:00
|
|
|
<div className="flex flex-row justify-between mb-2 items-baseline">
|
|
|
|
<FabricSizer {...{ gist, updateGist, cutFabric, sheetWidth }} />
|
|
|
|
<div>
|
|
|
|
<PageIcon className="h-6 w-6 mr-2 inline align-middle" />
|
|
|
|
<span className="text-xl font-bold align-middle">{fabricLength}</span>
|
2023-02-18 16:18:35 +02:00
|
|
|
</div>
|
2023-02-27 17:47:34 -06:00
|
|
|
<button
|
|
|
|
key="reset"
|
|
|
|
onClick={() => unsetGist(['layouts', 'cuttingLayout', cutFabric])}
|
|
|
|
className="btn btn-primary btn-outline"
|
|
|
|
>
|
|
|
|
<ClearIcon className="h-6 w-6 mr-2" />
|
|
|
|
{t('reset')}
|
|
|
|
</button>
|
2022-02-20 18:46:21 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|