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

213 lines
4.6 KiB
JavaScript
Raw Normal View History

// Hooks
import { useContext, useState } from 'react'
import { useTranslation } from 'next-i18next'
// Context
import { ModalContext } from 'shared/context/modal-context.mjs'
// Components
import { ModalWrapper } from 'shared/components/wrappers/modal.mjs'
import { Collapse } from 'shared/components/collapse.mjs'
2023-05-19 18:27:36 +02:00
import { OptionsIcon, ClearIcon, HelpIcon, EditIcon } from 'shared/components/icons.mjs'
2023-05-11 19:14:48 +02:00
import { optionsMenuStructure } from 'shared/utils.mjs'
import { optionType } from 'shared/utils.mjs'
import {
2023-05-26 18:16:48 -05:00
BoolInput,
ConstantInput,
SliderInput,
DegInput,
ListInput,
MmInput,
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-26 18:16:48 -05:00
import { WorkbenchMenu, useDocsLoader, wasChanged } from '../shared/index.mjs'
import { MenuItem, ItemTitle, MenuItemGroup } from '../shared/menu-item.mjs'
2023-05-11 19:14:48 +02:00
export const ns = ['design-options']
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: MmInput,
pct: PctInput,
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: '📁',
}
export const DesignOption = ({
name,
current,
config,
settings,
update,
t,
loadDocs,
changed = false,
}) => {
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
if (type === 'bool') {
config = {
...config,
list: [0, 1],
choiceTitles: {
0: `${name}No`,
1: `${name}Yes`,
},
valueTitles: {
0: 'no',
1: 'yes',
},
dflt: config.dflt ? 1 : 0,
2023-05-11 19:14:48 +02:00
}
}
2023-05-11 19:14:48 +02:00
return (
2023-05-26 18:16:48 -05:00
<MenuItem
{...{
name,
config,
current,
updater: update.settings,
updatePath: ['options'],
t,
changed,
loadDocs,
Input,
Value,
allowOverride,
}}
/>
2023-05-11 19:14:48 +02:00
)
}
export const DesignOptionGroup = ({
design,
patternConfig,
settings,
update,
group,
options,
t,
loadDocs,
2023-05-11 19:14:48 +02:00
}) => (
<Collapse
bottom
2023-05-26 18:16:48 -05:00
color="secondary"
title={
<ItemTitle
{...{
name: group,
t,
emoji: emojis[group] ? emojis[group] : emojis.groupDflt,
}}
/>
}
openTitle={t(group)}
>
{Object.entries(options).map(([option, type]) =>
typeof type === 'string' ? (
2023-05-26 18:16:48 -05:00
<DesignOption
{...{ t, design, update, settings, loadDocs }}
key={option}
name={option}
settings={settings}
current={settings.options?.[option]}
config={patternConfig.options[option]}
2023-05-26 18:16:48 -05:00
changed={wasChanged(settings.options?.[option], option, patternConfig.options)}
/>
) : (
<DesignOptionGroup
{...{ design, patternConfig, settings, update, Option, t, loadDocs }}
group={option}
options={type}
key={option}
/>
)
)}
</Collapse>
2023-05-11 19:14:48 +02:00
)
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]
const { t } = useTranslation(menuNs)
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() : ''}`
const loadDocs = useDocsLoader(DynamicDocs, getDocsPath, language)
2023-05-11 19:14:48 +02:00
return (
2023-05-26 18:16:48 -05:00
<WorkbenchMenu
{...{
name: 'design-options:designOptions',
updater: update.settings,
ns: menuNs,
Icon: OptionsIcon,
inputs,
values,
currentValues: settings.options,
language,
DynamicDocs,
getDocsPath,
}}
>
<MenuItemGroup
{...{
wrapped: false,
groupConfig: patternConfig.options,
currents: settings.options,
items: optionsMenu,
Item: DesignOption,
loadDocs,
itemProps: { design, update, settings },
emojis,
t,
}}
/>
</WorkbenchMenu>
2023-05-11 19:14:48 +02:00
)
}