1
0
Fork 0

Merge branch 'develop' into joost

This commit is contained in:
joostdecock 2023-06-12 18:22:14 +02:00
commit 01e2d4e41a
8 changed files with 75 additions and 6 deletions

View file

@ -74,6 +74,7 @@ The following named exports are **utility methods**:
| `lineIntersectsCircle` | See the [lineIntersectsCircle](/reference/api/utils/lineintersectscircle) documentation |
| `lineIntersectsCurve` | See the [lineIntersectsCurve](/reference/api/utils/lineintersectscurve) documentation |
| `linesIntersect` | See the [linesIntersect](/reference/api/utils/linesintersect) documentation |
| `mergeOptions` | See the [mergeOptions](/reference/api/utils/mergeoptions) documentation |
| `pctBasedOn` | See the [pctBasedOn](/reference/api/utils/pctbasedon) documentation |
| `pointOnBeam` | See the [pointOnBeam](/reference/api/utils/pointonbeam) documentation |
| `pointOnCurve` | See the [pointOnCurve](/reference/api/utils/pointoncurve) documentation |

View file

@ -17,16 +17,23 @@ The `toAbs` property should hold a function with the following
signature:
```js
function toAbs(percentage, settings) {
function toAbs(percentage, settings, mergeOptions) {
// return value in millimeter here
}
```
The first parameter is the percentage value provided by the user (for example
`0.5` for `50%`).
The second parameter holds the pattern's [settings](/reference/settings) object
which holds -- among other things -- the measurements provided by the user.
The third parameter should be the return value of
[utils.mergeOptions()](/reference/api/utils/mergeoptions), which provides an
object with all option values populated. Although this parameter is not
required for simple values based on measurements, it is often required when the
result depends on several options.
## Example
In our example above, let's say that the `chestEase` option is

View file

@ -0,0 +1,27 @@
---
title: utils.mergeOptions()
---
The `utils.mergeOptions()` function merges the user-provided options with the
options from the pattern configuration.
## Signature
```js
float deg2rad(object settings, object optionsConfig)
```
## Notes
Typically the only options that are passed as part of settings to the pattern
are those that differ from the defaults. This means that if you want to check
an option outside a draft method, you need to check whether the option is set,
and if it's not get the default value from the pattern config. Furthermore,
where the default is stored and whether or not it should be further transformed
depends on the option type.
This method exists to facilitate this. You pass it the user-provided settings,
and the pattern config options key, and it will return an object where all
options are populated with the user-provided values, or their defaults if the
user did not provide any input.

View file

@ -26,6 +26,7 @@ import {
lineIntersectsCircle,
lineIntersectsCurve,
linesIntersect,
mergeOptions,
pctBasedOn,
pointOnBeam,
pointOnCurve,
@ -70,6 +71,7 @@ export {
lineIntersectsCircle,
lineIntersectsCurve,
linesIntersect,
mergeOptions,
pctBasedOn,
pointOnBeam,
pointOnCurve,

View file

@ -1,6 +1,6 @@
import { PatternDraftQueue } from './pattern-draft-queue.mjs'
import { Part } from '../part.mjs'
import { __macroName } from '../utils.mjs'
import { __macroName, mergeOptions } from '../utils.mjs'
/**
* A class to handle drafting a pattern
@ -210,7 +210,11 @@ PatternDrafter.prototype.__loadAbsoluteOptionsSet = function (set) {
*/
PatternDrafter.prototype.__snappedPercentageOption = function (optionName, set) {
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
let snapConf = conf.snap
if (!Array.isArray(snapConf) && snapConf.metric && snapConf.imperial)

View file

@ -441,6 +441,29 @@ export function lineIntersectsCurve(start, end, from, cp1, cp2, to) {
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
*

View file

@ -90,7 +90,7 @@ export const DesignOptions = ({
name: 'design-options:designOptions',
language,
ns: menuNs,
passProps: { settings },
passProps: { settings, patternConfig },
updateFunc: (name, value) => update.settings(['options', ...name], value),
}}
/>

View file

@ -1,14 +1,19 @@
import { formatMm, formatPercentage } from 'shared/utils.mjs'
import { ListValue, HighlightedValue, PlainValue, BoolValue } from '../shared/values'
import { mergeOptions } from '@freesewing/core'
/** 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
return (
<HighlightedValue changed={changed}>
{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>
)
}