1
0
Fork 0
freesewing/sites/shared/components/workbench/menus/design-options/index.mjs

146 lines
3.8 KiB
JavaScript
Raw Normal View History

// Components
2023-05-29 23:22:26 -05:00
import { OptionsIcon } from 'shared/components/icons.mjs'
2023-05-11 19:14:48 +02:00
import { optionsMenuStructure } from 'shared/utils.mjs'
2023-05-31 17:42:16 -05:00
import { optionType, formatMm } from 'shared/utils.mjs'
2023-05-11 19:14:48 +02:00
import {
2023-05-26 18:16:48 -05:00
BoolInput,
ConstantInput,
SliderInput,
DegInput,
ListInput,
PctInput,
} from '../shared/inputs.mjs'
2023-05-11 19:14:48 +02:00
import {
BoolOptionValue,
ConstantOptionValue,
CountOptionValue,
DegOptionValue,
ListOptionValue,
MmOptionValue,
PctOptionValue,
} from './values.mjs'
2023-05-29 23:22:26 -05:00
import { WorkbenchMenu } from '../shared/index.mjs'
import { MenuItem } from '../shared/menu-item.mjs'
2023-05-11 19:14:48 +02:00
export const ns = ['design-options']
2023-05-31 17:42:16 -05:00
const PctOptionInput = (props) => {
const { config, settings, changed } = props
const currentOrDefault = changed ? props.current : config.dflt / 100
return (
<PctInput {...props}>
<div className="flex flex-row justify-around">
<span className={changed ? 'text-accent' : 'text-secondary'}>
{config.toAbs && settings.measurements
? formatMm(config.toAbs(currentOrDefault, settings))
: ' '}
</span>
</div>
</PctInput>
)
}
2023-05-11 19:14:48 +02:00
// Facilitate lookup of the input component
const inputs = {
2023-05-26 18:16:48 -05:00
bool: BoolInput,
constant: ConstantInput,
count: SliderInput,
deg: DegInput,
list: ListInput,
mm: () => <span>FIXME: Mm options are deprecated. Please report this </span>,
2023-05-31 17:42:16 -05:00
pct: PctOptionInput,
2023-05-11 19:14:48 +02:00
}
// Facilitate lookup of the value component
const values = {
bool: BoolOptionValue,
constant: ConstantOptionValue,
count: CountOptionValue,
deg: DegOptionValue,
list: ListOptionValue,
mm: MmOptionValue,
pct: PctOptionValue,
}
// Emojis for option groups :)
const emojis = {
advanced: '🤓',
fit: '👕',
style: '💃🏽',
dflt: '🕹️',
groupDflt: '📁',
}
2023-05-31 17:42:16 -05:00
/**
* A wrapper for {@see MenuItem} to handle design option-specific business
* @param {Object} options.config the config for the item
* @param {Object} options.settings core settings
* @param {Object} options.rest the rest of the props
*/
export const DesignOption = ({ config, settings, ...rest }) => {
2023-05-11 19:14:48 +02:00
const type = optionType(config)
const Input = inputs[type]
const Value = values[type]
2023-05-26 18:16:48 -05:00
const allowOverride = ['pct', 'count', 'deg'].includes(type)
2023-05-11 19:14:48 +02:00
// Hide option?
if (config?.hide || (typeof config?.hide === 'function' && config.hide(settings))) return null
return (
2023-05-26 18:16:48 -05:00
<MenuItem
{...{
config,
2023-05-31 17:42:16 -05:00
...rest,
2023-05-26 18:16:48 -05:00
Input,
Value,
allowOverride,
}}
/>
2023-05-11 19:14:48 +02:00
)
}
2023-05-31 17:42:16 -05:00
/**
* The design options menu
* @param {String} options.design the name of the design
* @param {Object} options.patternConfig the configuration from the pattern
* @param {Object} options.settings core settings
* @param {Object} options.update settings and ui update functions
* @param {String} options.language the menu language
* @param {Object} options.account the user account data
* @param {Boolean|React.component} options.DynamicDocs A docs component
*/
export const DesignOptions = ({
design,
patternConfig,
settings,
update,
language,
2023-05-26 18:16:48 -05:00
account,
DynamicDocs = false,
}) => {
2023-05-26 18:16:48 -05:00
const menuNs = [`o_${design}`, ...ns]
2023-05-11 19:14:48 +02:00
const optionsMenu = optionsMenuStructure(patternConfig.options)
2023-05-26 18:16:48 -05:00
const getDocsPath = (option) =>
`patterns/${design}/options${option ? '/' + option.toLowerCase() : ''}`
2023-05-11 19:14:48 +02:00
return (
2023-05-26 18:16:48 -05:00
<WorkbenchMenu
{...{
config: optionsMenu,
2023-05-29 23:22:26 -05:00
control: account.control,
2023-05-26 18:16:48 -05:00
currentValues: settings.options,
DynamicDocs,
emojis,
2023-05-26 18:16:48 -05:00
getDocsPath,
Icon: OptionsIcon,
Item: DesignOption,
name: 'design-options:designOptions',
language,
ns: menuNs,
passProps: { settings },
updateFunc: (name, value) => update.settings(['options', name], value),
2023-05-26 18:16:48 -05:00
}}
/>
2023-05-11 19:14:48 +02:00
)
}