feat(core): New mergeOptions method and passing mergedOtions to toAbs
When using toAbs for all but the most trivial cases, one needs access to the options. Unlike measurements which are always there, options aren't always set since they are only set when they differ from the default. This would shift a lot of responsibility on the pattern designer who would have to check whether an option is set, and if it's not use the default which in turn depends on what type of an option it is (and it's pct for example you have to remember to divide it by 100 and so on). This feels footgun-y so instead I've added a `mergeOptions` method that merges the options passed by the user with the defaults from the pattern config so that you have a simple object with all option values. This should get passed to `toAbs()` as the 3rd parameter.
This commit is contained in:
parent
55b76d03f1
commit
631ea8a50d
5 changed files with 39 additions and 5 deletions
|
@ -26,6 +26,7 @@ import {
|
||||||
lineIntersectsCircle,
|
lineIntersectsCircle,
|
||||||
lineIntersectsCurve,
|
lineIntersectsCurve,
|
||||||
linesIntersect,
|
linesIntersect,
|
||||||
|
mergeOptions,
|
||||||
pctBasedOn,
|
pctBasedOn,
|
||||||
pointOnBeam,
|
pointOnBeam,
|
||||||
pointOnCurve,
|
pointOnCurve,
|
||||||
|
@ -70,6 +71,7 @@ export {
|
||||||
lineIntersectsCircle,
|
lineIntersectsCircle,
|
||||||
lineIntersectsCurve,
|
lineIntersectsCurve,
|
||||||
linesIntersect,
|
linesIntersect,
|
||||||
|
mergeOptions,
|
||||||
pctBasedOn,
|
pctBasedOn,
|
||||||
pointOnBeam,
|
pointOnBeam,
|
||||||
pointOnCurve,
|
pointOnCurve,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { PatternDraftQueue } from './pattern-draft-queue.mjs'
|
import { PatternDraftQueue } from './pattern-draft-queue.mjs'
|
||||||
import { Part } from '../part.mjs'
|
import { Part } from '../part.mjs'
|
||||||
import { __macroName } from '../utils.mjs'
|
import { __macroName, mergeOptions } from '../utils.mjs'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class to handle drafting a pattern
|
* A class to handle drafting a pattern
|
||||||
|
@ -202,7 +202,11 @@ PatternDrafter.prototype.__loadAbsoluteOptionsSet = function (set) {
|
||||||
*/
|
*/
|
||||||
PatternDrafter.prototype.__snappedPercentageOption = function (optionName, set) {
|
PatternDrafter.prototype.__snappedPercentageOption = function (optionName, set) {
|
||||||
const conf = this.pattern.config.options[optionName]
|
const conf = this.pattern.config.options[optionName]
|
||||||
const abs = conf.toAbs(this.pattern.settings[set].options[optionName], this.pattern.settings[set])
|
const abs = conf.toAbs(
|
||||||
|
this.pattern.settings[set].options[optionName],
|
||||||
|
this.pattern.settings[set],
|
||||||
|
mergeOptions(this.pattern.settings[set], this.pattern.config.options)
|
||||||
|
)
|
||||||
// Handle units-specific config - Side-step immutability for the snap conf
|
// Handle units-specific config - Side-step immutability for the snap conf
|
||||||
let snapConf = conf.snap
|
let snapConf = conf.snap
|
||||||
if (!Array.isArray(snapConf) && snapConf.metric && snapConf.imperial)
|
if (!Array.isArray(snapConf) && snapConf.metric && snapConf.imperial)
|
||||||
|
|
|
@ -441,6 +441,29 @@ export function lineIntersectsCurve(start, end, from, cp1, cp2, to) {
|
||||||
else return intersections
|
else return intersections
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to merge passed in options with default options from the pattern config
|
||||||
|
*
|
||||||
|
* @param {object} settings - The settings passed to the pattern
|
||||||
|
* @param {object} optionsConfig - The pattern's options config
|
||||||
|
* @return {object} result - An object with the merged options and their values
|
||||||
|
*/
|
||||||
|
export function mergeOptions(settings, optionsConfig) {
|
||||||
|
const merged = typeof settings.options === 'undefined' ? {} : { ...settings.option }
|
||||||
|
for (const [key, option] of Object.entries(optionsConfig)) {
|
||||||
|
if (typeof option === 'object') {
|
||||||
|
if (typeof option.pct !== 'undefined') merged[key] = option.pct / 100
|
||||||
|
else if (typeof option.mm !== 'undefined') merged[key] = option.mm
|
||||||
|
else if (typeof option.deg !== 'undefined') merged[key] = option.deg
|
||||||
|
else if (typeof option.count !== 'undefined') merged[key] = option.count
|
||||||
|
else if (typeof option.bool !== 'undefined') merged[key] = option.bool
|
||||||
|
else if (typeof option.dflt !== 'undefined') merged[key] = option.dflt
|
||||||
|
} else merged[key] = option
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method to calculate abolute option value based on a measurement
|
* Helper method to calculate abolute option value based on a measurement
|
||||||
*
|
*
|
||||||
|
|
|
@ -90,7 +90,7 @@ export const DesignOptions = ({
|
||||||
name: 'design-options:designOptions',
|
name: 'design-options:designOptions',
|
||||||
language,
|
language,
|
||||||
ns: menuNs,
|
ns: menuNs,
|
||||||
passProps: { settings },
|
passProps: { settings, patternConfig },
|
||||||
updateFunc: (name, value) => update.settings(['options', ...name], value),
|
updateFunc: (name, value) => update.settings(['options', ...name], value),
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
import { formatMm, formatPercentage } from 'shared/utils.mjs'
|
import { formatMm, formatPercentage } from 'shared/utils.mjs'
|
||||||
import { ListValue, HighlightedValue, PlainValue, BoolValue } from '../shared/values'
|
import { ListValue, HighlightedValue, PlainValue, BoolValue } from '../shared/values'
|
||||||
|
import { mergeOptions } from '@freesewing/core'
|
||||||
|
|
||||||
/** Displays the current percentatge value, and the absolute value if configured */
|
/** Displays the current percentatge value, and the absolute value if configured */
|
||||||
export const PctOptionValue = ({ config, current, settings, changed }) => {
|
export const PctOptionValue = ({ config, current, settings, changed, patternConfig }) => {
|
||||||
const val = changed ? current : config.pct / 100
|
const val = changed ? current : config.pct / 100
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HighlightedValue changed={changed}>
|
<HighlightedValue changed={changed}>
|
||||||
{formatPercentage(val)}
|
{formatPercentage(val)}
|
||||||
{config.toAbs && settings.measurements ? ` | ${formatMm(config.toAbs(val, settings))}` : null}
|
{config.toAbs && settings.measurements
|
||||||
|
? ` | ${formatMm(
|
||||||
|
config.toAbs(val, settings, mergeOptions(settings, patternConfig.options))
|
||||||
|
)}`
|
||||||
|
: null}
|
||||||
</HighlightedValue>
|
</HighlightedValue>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue