2023-05-13 19:58:50 +02:00
|
|
|
// Hooks
|
2023-05-18 11:26:10 +02:00
|
|
|
import { useContext, useState } from 'react'
|
2023-05-13 19:58:50 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-05-18 11:26:10 +02:00
|
|
|
// Context
|
|
|
|
import { ModalContext } from 'shared/context/modal-context.mjs'
|
2023-05-13 19:58:50 +02:00
|
|
|
// Components
|
2023-05-18 11:26:10 +02:00
|
|
|
import { ModalWrapper } from 'shared/components/wrappers/modal.mjs'
|
2023-05-13 19:58:50 +02:00
|
|
|
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
|
|
|
|
2023-05-13 19:58:50 +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,
|
|
|
|
}
|
|
|
|
|
2023-05-18 11:26:10 +02:00
|
|
|
// Emojis for option groups :)
|
|
|
|
const emojis = {
|
2023-05-18 14:06:22 +02:00
|
|
|
advanced: '🤓',
|
2023-05-18 11:26:10 +02:00
|
|
|
fit: '👕',
|
|
|
|
style: '💃🏽',
|
|
|
|
dflt: '🕹️',
|
2023-05-18 14:06:22 +02:00
|
|
|
groupDflt: '📁',
|
2023-05-18 11:26:10 +02:00
|
|
|
}
|
|
|
|
|
2023-05-13 19:58:50 +02:00
|
|
|
export const DesignOption = ({
|
|
|
|
name,
|
|
|
|
current,
|
|
|
|
config,
|
|
|
|
settings,
|
|
|
|
update,
|
|
|
|
t,
|
2023-05-18 11:26:10 +02:00
|
|
|
loadDocs,
|
2023-05-13 19:58:50 +02:00
|
|
|
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') {
|
2023-05-13 19:58:50 +02:00
|
|
|
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-13 19:58:50 +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,
|
2023-05-18 11:26:10 +02:00
|
|
|
loadDocs,
|
2023-05-11 19:14:48 +02:00
|
|
|
}) => (
|
2023-05-31 15:32:54 +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,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
}
|
2023-05-31 15:32:54 +02:00
|
|
|
openTitle={t(group)}
|
|
|
|
>
|
2023-05-13 19:58:50 +02:00
|
|
|
{Object.entries(options).map(([option, type]) =>
|
|
|
|
typeof type === 'string' ? (
|
2023-05-26 18:16:48 -05:00
|
|
|
<DesignOption
|
2023-05-18 11:26:10 +02:00
|
|
|
{...{ t, design, update, settings, loadDocs }}
|
2023-05-13 19:58:50 +02:00
|
|
|
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)}
|
2023-05-13 19:58:50 +02:00
|
|
|
/>
|
|
|
|
) : (
|
2023-05-18 14:06:22 +02:00
|
|
|
<DesignOptionGroup
|
2023-05-18 11:26:10 +02:00
|
|
|
{...{ design, patternConfig, settings, update, Option, t, loadDocs }}
|
2023-05-13 19:58:50 +02:00
|
|
|
group={option}
|
|
|
|
options={type}
|
|
|
|
key={option}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Collapse>
|
2023-05-11 19:14:48 +02:00
|
|
|
)
|
|
|
|
|
2023-05-18 11:26:10 +02:00
|
|
|
export const DesignOptions = ({
|
|
|
|
design,
|
|
|
|
patternConfig,
|
|
|
|
settings,
|
|
|
|
update,
|
|
|
|
language,
|
2023-05-26 18:16:48 -05:00
|
|
|
account,
|
2023-05-18 15:25:40 +02:00
|
|
|
DynamicDocs = false,
|
2023-05-18 11:26:10 +02:00
|
|
|
}) => {
|
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-18 11:26:10 +02:00
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|