1
0
Fork 0

feat[plugin-bust]: Move conditionality to preSetDraft lifecycle hook

This plugin is used to draft designs for high bust rather than full
chest circumference. To facilitate that, we provide(d) a named export
called `withCondition` that checks whether the plugin is wanted, and if
so loads it.

Problem with that is that the conditional loading of a plugin applied
to the entire pattern. And since we support drafting patterns for
multiple sets (and use this to sample) this means that all of these sets
would either get the plugin effect or not, based on the first set.

So, to fix that, we have changed the lifecycle hook used by this plugin
to `preSetDraft` (from `preDraft`) and changed the `withCondition`
method to always return true.

This means that the plugin will always be loaded, and we have moved the
check that used to be in `withCondition` to the lifecycle hook.

In other words, this plugin will now always be loaded and will check for
each set whether it needs to do something.

This allows the conditionality to apply to each set in the pattern,
rather than to the entire pattern.

Note that conditionally loading plugins pattern-wide is still a valid
use-case. It just so happens that for this plugin, it was the wrong
approach.
This commit is contained in:
joostdecock 2024-02-17 15:57:40 +01:00
parent 56fe9faa45
commit 4264c81070
3 changed files with 32 additions and 14 deletions

View file

@ -1,3 +1,18 @@
Unreleased:
Changed:
plugin-bust:
- This plugin now uses the `preSetDraft` rather than `preDraft` lifecycle hook
- Conditionality has been moved to the `preSetDraft` lifecycle hook, rather than exposing a `withCondition` named export
- The plugin will now always be loaded, but will check for each drafted set whether it should make any changes.
Deprecated:
plugin-bust:
- The `withCondition` named export is deprecated and will always return true.
Fixed:
hugo:
- Add missing dimension id attributes
3.2.0:
date: 2024-02-11

View file

@ -4,13 +4,12 @@ export const plugin = {
name,
version,
hooks: {
preDraft: function ({ settings }) {
for (const i in settings) {
if (settings[i].measurements) {
if (typeof settings[i].measurements.bust === 'undefined') {
settings[i].measurements.bust = settings[i].measurements.chest
settings[i].measurements.chest = settings[i].measurements.highBust
}
preSetDraft: function ({ settings, activeSet }) {
const set = settings[Number(activeSet)]
if (set.measurements && set.options?.draftForHighBust && set.measurements?.highBust) {
if (typeof set.measurements.bust === 'undefined') {
set.measurements.bust = set.measurements.chest
set.measurements.chest = set.measurements.highBust
}
}
},
@ -24,6 +23,11 @@ export const pluginBust = plugin
// Helper method to conditionally load this plugin
export const withCondition = {
plugin,
condition: (settings = false) =>
settings?.options?.draftForHighBust && settings?.measurements?.highBust ? true : false,
condition: (settings = false) => {
console.log(
"WARNING: The 'withCondition' named export in @freesewing/plugin-bust is deprecated. Conditionality has moved to the preSetDraft lifecycle hook"
)
return true
},
}

View file

@ -6,9 +6,10 @@ const measurements = {
chest: 100,
highBust: 90,
}
const options = { draftForHighBust: true }
const Pattern = new Design()
const pattern = new Pattern({ measurements }).use(plugin)
const pattern = new Pattern({ measurements, options }).use(plugin)
pattern.draft()
describe('Bust plugin Tests', () => {
@ -18,9 +19,7 @@ describe('Bust plugin Tests', () => {
})
it('Should copy measurement from chest to bust and from highBust to chest', function () {
const testPattern = new Design({
measurements: [],
})
const testPattern = new Design({ measurements: [], options })
const pattern = new testPattern().use(plugin)
const userMeasurements = { chest: 50, highBust: 60 }
pattern.settings[0].measurements = userMeasurements
@ -30,7 +29,7 @@ describe('Bust plugin Tests', () => {
})
it('Should not overwrite existing bust measurements', function () {
let config = { measurements: [] }
let config = { measurements: [], options }
const testPattern = new Design(config, plugin)
let pattern = new testPattern()
let userMeasurements = { chest: 50, highBust: 60, bust: 55 }