1
0
Fork 0

fix(core): Issue with hiding dependencies

This commit is contained in:
joostdecock 2022-09-11 18:00:41 +02:00
parent 32890819bf
commit 48b366c57e
10 changed files with 165 additions and 167 deletions

View file

@ -21,6 +21,7 @@ export function Part() {
// Enumerable properties
this.render = true // FIXME: Replace render with hide
this.hide = false // FIXME: Replace render with hide
this.attributes = new Attributes()
this.points = {}
this.paths = {}
@ -323,7 +324,9 @@ Part.prototype.shorthand = function () {
const optionsProxy = {
get: function (options, name) {
if (typeof options[name] === 'undefined')
self.context.store.log.warning(`Tried to access \`options.${name}\` but it is \`undefined\``)
self.context.store.log.warning(
`Tried to access \`options.${name}\` but it is \`undefined\``
)
return Reflect.get(...arguments)
},
set: (options, name, value) => (self.context.settings.options[name] = value),

View file

@ -13,9 +13,7 @@ import {
} from './utils.mjs'
export function Path(debug = false) {
// Non-enumerable properties
addNonEnumProp(this, 'render', true) // FIXME: replace with hide
//addNonEnumProp(this, 'topLeft', false)
//addNonEnumProp(this, 'bottomRight', false)
//addNonEnumProp(this, 'attributes', new Attributes())
@ -28,6 +26,7 @@ export function Path(debug = false) {
this.attributes = new Attributes()
this.topLeft = false
this.bottomRight = false
this.render = true
}
/** Adds the log method for a path not created through the proxy **/
@ -632,9 +631,7 @@ Path.prototype.divide = function () {
if (!op.to.sitsRoughlyOn(current))
paths.push(new Path(this.debug).withLog(this.log).move(current).line(op.to))
} else if (op.type === 'curve') {
paths.push(
new Path(this.debug).withLog(this.log).move(current).curve(op.cp1, op.cp2, op.to)
)
paths.push(new Path(this.debug).withLog(this.log).move(current).curve(op.cp1, op.cp2, op.to))
} else if (op.type === 'close') {
paths.push(new Path(this.debug).withLog(this.log).move(current).line(start))
}
@ -646,15 +643,13 @@ Path.prototype.divide = function () {
/** Finds intersections between this path and an X value */
Path.prototype.intersectsX = function (x) {
if (typeof x !== 'number')
this.log.error('Called `Path.intersectsX(x)` but `x` is not a number')
if (typeof x !== 'number') this.log.error('Called `Path.intersectsX(x)` but `x` is not a number')
return this.intersectsAxis(x, 'x')
}
/** Finds intersections between this path and an Y value */
Path.prototype.intersectsY = function (y) {
if (typeof y !== 'number')
this.log.error('Called `Path.intersectsX(y)` but `y` is not a number')
if (typeof y !== 'number') this.log.error('Called `Path.intersectsX(y)` but `y` is not a number')
return this.intersectsAxis(y, 'y')
}
@ -781,9 +776,7 @@ Path.prototype.split = function (point) {
firstHalf.push(new Path(this.debug).withLog(this.log).move(path.ops[0].to).line(point))
pi++
secondHalf = divided.slice(pi)
secondHalf.unshift(
new Path(this.debug).withLog(this.log).move(point).line(path.ops[1].to)
)
secondHalf.unshift(new Path(this.debug).withLog(this.log).move(point).line(path.ops[1].to))
}
} else if (path.ops[1].type === 'curve') {
if (path.ops[0].to.sitsRoughlyOn(point)) {

View file

@ -239,7 +239,9 @@ Pattern.prototype.draft = function () {
try {
const result = this.__parts[partName].draft(this.parts[partName].shorthand())
if (typeof result === 'undefined') {
this.store.log.error(`Result of drafting part ${partName} was undefined. Did you forget to return the part?`)
this.store.log.error(
`Result of drafting part ${partName} was undefined. Did you forget to return the part?`
)
} else this.parts[partName] = result
} catch (err) {
this.store.log.error([`Unable to draft part \`${partName}\``, err])
@ -657,26 +659,27 @@ Pattern.prototype.__resolveParts = function (count = 0) {
}
}
for (const [name, part] of Object.entries(this.__parts)) {
// Hide when hideAll is set
if (part.hideAll) part.hide = true
// Inject (from)
if (part.from) {
if (part.hideDependencies || part.hideAll) {
part.from.hide = true
part.from.hideAll = true
}
this.__parts[part.from.name] = part.from
this.__inject[name] = part.from.name
if (typeof this.__parts[part.from.name] === 'undefined') {
if (part.hideDependencies || part.hideAll) {
this.__parts[part.from.name].hide = true
this.__parts[part.from.name].hideAll = true
}
//this.config = addPartConfig(this.__parts[part.from.name], this.config, this.store)
}
}
// Simple dependency (after)
if (part.after) {
if (Array.isArray(part.after)) {
for (const dep of part.after) {
if (part.hideDependencies) dep.hide = true
this.__parts[dep.name] = dep
this.__addDependency(name, part, dep)
}
} else {
if (part.hideDependencies) part.after.hide = true
this.__parts[part.after.name] = part.after
this.__addDependency(name, part, part.after)
}
@ -729,18 +732,16 @@ Pattern.prototype.__resolveDependencies = function (graph = false) {
* configured dependencies.
*/
Pattern.prototype.needs = function (partName) {
// If only is unset, all parts are needed
if (
typeof this.settings.only === 'undefined' ||
this.settings.only === false ||
( Array.isArray(this.settings.only) && this.settings.only.length === 0 )
) return true
(Array.isArray(this.settings.only) && this.settings.only.length === 0)
)
return true
// Make only to always be an array
const only = typeof this.settings.only === 'string'
? [ this.settings.only ]
: this.settings.only
const only = typeof this.settings.only === 'string' ? [this.settings.only] : this.settings.only
// Walk the only parts, checking each one for a match in its dependencies
for (const part of only) {
@ -777,7 +778,6 @@ Pattern.prototype.isHidden = function (partName) {
if (Array.isArray(this.settings.only)) {
if (this.settings.only.includes(partName)) return false
}
if (this.__parts?.[partName]?.hide) return true
if (this.__parts?.[partName]?.hideAll) return true

View file

@ -548,7 +548,6 @@ export const addPartConfig = (part, config, store) => {
config = addPartOptions(part, config, store)
config = addPartMeasurements(part, config, store)
config = addPartOptionalMeasurements(part, config, store)
//config = addPartDependencies(part, config, store)
config = addPartPlugins(part, config, store)
return config

View file

@ -4,7 +4,6 @@ import { Design, Pattern, Path } from '../src/index.mjs'
const expect = chai.expect
describe('Part', () => {
it('Shorthand should contain the part itself', () => {
let dp
const part = {
@ -12,9 +11,9 @@ describe('Part', () => {
draft: ({ part }) => {
dp = part
return part
}
},
}
const design = new Design({ parts: [ part ]})
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(typeof dp).to.equal('object')
@ -181,7 +180,7 @@ describe('Part', () => {
expect(boundary.width).to.equal(124)
expect(boundary.height).to.equal(400)
})
/*
/*
it('Should stack a part', () => {
const part = {
name: 'test',

View file

@ -7,13 +7,13 @@ describe('Path', () => {
it('Should offset a line', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part}) => {
draft: ({ paths, Path, Point, part }) => {
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40))
paths.offset = paths.line.offset(10)
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(pattern.parts.test.paths.offset.bottomRight.x).to.equal(-10)
@ -29,9 +29,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
paths.offset = paths.curve.offset(10)
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.18)
@ -42,14 +42,12 @@ describe('Path', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
._curve(new Point(123, 34), new Point(23, 4))
paths.curve = new Path().move(new Point(0, 0))._curve(new Point(123, 34), new Point(23, 4))
paths.offset = paths.curve.offset(10)
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(72.63)
@ -60,14 +58,12 @@ describe('Path', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part }) => {
paths.curve = new Path()
.move(new Point(0, 0))
.curve_(new Point(40, 0), new Point(123, 34))
paths.curve = new Path().move(new Point(0, 0)).curve_(new Point(40, 0), new Point(123, 34))
paths.offset = paths.curve.offset(10)
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts.test.paths.offset.bottomRight.x)).to.equal(119.26)
@ -98,13 +94,11 @@ describe('Path', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, part }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(40, 0))
paths.line = new Path().move(new Point(0, 0)).line(new Point(40, 0))
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(pattern.parts.test.paths.line.length()).to.equal(40)
@ -119,9 +113,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(round(pattern.parts.test.paths.curve.length())).to.equal(145.11)
@ -136,9 +130,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(pattern.parts.test.paths.curve.start().x).to.equal(123)
@ -154,9 +148,9 @@ describe('Path', () => {
.curve(new Point(0, 40), new Point(123, 34), new Point(23, 4))
.close()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
expect(pattern.parts.test.paths.curve.end().x).to.equal(123)
@ -825,9 +819,9 @@ describe('Path', () => {
.attr('class', 'bar')
.attr('class', 'overwritten', true)
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft().render()
@ -1080,16 +1074,13 @@ describe('Path', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, points }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
.attr('class', 'foo')
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).attr('class', 'foo')
paths.a = new Path().move(points.a).line(points.b)
paths.b = paths.a.offset()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.store.logs.error.length).to.equal(2)
@ -1102,15 +1093,12 @@ describe('Path', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, points }) => {
paths.line = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
.attr('class', 'foo')
paths.line = new Path().move(new Point(0, 0)).line(new Point(0, 40)).attr('class', 'foo')
paths.a = new Path().move(points.a).line(points.b).join()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.store.logs.error.length).to.equal(2)
@ -1155,34 +1143,31 @@ describe('Path', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, points }) => {
points.a = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
.shiftFractionAlong()
points.a = new Path().move(new Point(0, 0)).line(new Point(0, 40)).shiftFractionAlong()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.store.logs.error[0]).to.equal('Called `Path.shiftFractionAlong(fraction)` but `fraction` is not a number')
expect(pattern.store.logs.error[0]).to.equal(
'Called `Path.shiftFractionAlong(fraction)` but `fraction` is not a number'
)
})
it('Should log a warning when splitting a path on a non-point', () => {
const part = {
name: 'test',
draft: ({ paths, Path, Point, points }) => {
points.a = new Path()
.move(new Point(0, 0))
.line(new Point(0, 40))
.split()
points.a = new Path().move(new Point(0, 0)).line(new Point(0, 40)).split()
return part
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const pattern = new design()
pattern.draft()
expect(pattern.store.logs.error[0]).to.equal('Called `Path.split(point)` but `point` is not a `Point` object')
expect(pattern.store.logs.error[0]).to.equal(
'Called `Path.split(point)` but `point` is not a `Point` object'
)
})
})

View file

@ -5,41 +5,42 @@ const expect = chai.expect
describe('Pattern', () => {
describe('Pattern.draft()', () => {
const partA = {
name: 'test.partA',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'chest', 'waist'],
optionalMeasurements: ['chest', 'waist'],
options: {
optA: { pct: 40, min: 20, max: 80 }
optA: { pct: 40, min: 20, max: 80 },
},
draft: () => { }
draft: () => {},
}
const partB = {
name: 'test.partB',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'],
optionalMeasurements: ['knee'],
after: partA,
plugins: [{
name: 'testPlugin',
hooks: {
preRender: () => {}
}
}],
plugins: [
{
name: 'testPlugin',
hooks: {
preRender: () => {},
},
},
],
options: {
optB: { deg: 40, min: 20, max: 80 }
optB: { deg: 40, min: 20, max: 80 },
},
draft: () => { }
draft: () => {},
}
const partC = {
name: 'test.partC',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'],
optionalMeasurements: ['knee'],
from: partB,
options: {
optC: { pct: 20, min: 10, max: 30 }
optC: { pct: 20, min: 10, max: 30 },
},
draft: () => { }
draft: () => {},
}
const Pattern = new Design({
@ -47,7 +48,7 @@ describe('Pattern', () => {
name: 'test',
version: '1.2.3',
},
parts: [ partC ]
parts: [partC],
})
const pattern = new Pattern()
@ -84,42 +85,44 @@ describe('Pattern', () => {
const partA = {
name: 'test.partA',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'chest', 'waist'],
optionalMeasurements: ['chest', 'waist'],
options: {
optA: { pct: 40, min: 20, max: 80 }
optA: { pct: 40, min: 20, max: 80 },
},
draft: () => { }
draft: () => {},
}
const partB = {
name: 'test.partB',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'],
optionalMeasurements: ['knee'],
after: partA,
plugins: [{
name: 'testPlugin',
hooks: {
preRender: () => {}
}
}],
plugins: [
{
name: 'testPlugin',
hooks: {
preRender: () => {},
},
},
],
options: {
optB: { deg: 40, min: 20, max: 80 }
optB: { deg: 40, min: 20, max: 80 },
},
draft: () => { }
draft: () => {},
}
const partC = {
name: 'test.partC',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'],
optionalMeasurements: ['knee'],
options: {
optC: { pct: 20, min: 10, max: 30 }
optC: { pct: 20, min: 10, max: 30 },
},
draft: () => { }
draft: () => {},
}
const Pattern = new Design({
parts: [ partA, partB, partC ]
parts: [partA, partB, partC],
})
const pattern = new Pattern({
only: [ 'test.partB' ]
only: ['test.partB'],
})
pattern.init()
expect(pattern.needs('test.partA')).to.equal(true)
@ -131,42 +134,44 @@ describe('Pattern', () => {
const partA = {
name: 'test.partA',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'chest', 'waist'],
optionalMeasurements: ['chest', 'waist'],
options: {
optA: { pct: 40, min: 20, max: 80 }
optA: { pct: 40, min: 20, max: 80 },
},
draft: () => { }
draft: () => {},
}
const partB = {
name: 'test.partB',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'],
optionalMeasurements: ['knee'],
after: partA,
plugins: [{
name: 'testPlugin',
hooks: {
preRender: () => {}
}
}],
plugins: [
{
name: 'testPlugin',
hooks: {
preRender: () => {},
},
},
],
options: {
optB: { deg: 40, min: 20, max: 80 }
optB: { deg: 40, min: 20, max: 80 },
},
draft: () => { }
draft: () => {},
}
const partC = {
name: 'test.partC',
measurements: ['head', 'knee'],
optionalMeasurements: [ 'knee'],
optionalMeasurements: ['knee'],
options: {
optC: { pct: 20, min: 10, max: 30 }
optC: { pct: 20, min: 10, max: 30 },
},
draft: () => { }
draft: () => {},
}
const Pattern = new Design({
parts: [ partA, partB, partC ]
parts: [partA, partB, partC],
})
const pattern = new Pattern({
only: [ 'test.partB' ]
only: ['test.partB'],
})
pattern.init()
expect(pattern.wants('test.partA')).to.equal(false)
@ -337,5 +342,4 @@ describe('Pattern', () => {
*/
})

View file

@ -610,29 +610,29 @@ describe('Pattern', () => {
expect(count).to.equal(1)
})
it("Pattern.init() should register multiple methods on a single hook", () => {
it('Pattern.init() should register multiple methods on a single hook', () => {
const plugin = {
name: "test",
version: "0.1-test",
name: 'test',
version: '0.1-test',
hooks: {
preDraft: [
function(pattern) {
count++;
function (pattern) {
count++
},
function(pattern) {
count++;
}
]
}
};
function (pattern) {
count++
},
],
},
}
const Pattern = new Design()
const pattern = new Pattern()
let count = 0
pattern._draft = () => {}
pattern.use(plugin)
pattern.draft()
expect(count).to.equal(2);
});
expect(count).to.equal(2)
})
it('Should check whether created parts get the pattern context', () => {
const Pattern = new Design()
@ -660,7 +660,6 @@ describe('Pattern', () => {
expect(Array.isArray(part.context.store.logs.warning)).to.equal(true)
expect(Array.isArray(part.context.store.logs.error)).to.equal(true)
})
})
describe('Pattern.settings', () => {

View file

@ -12,9 +12,9 @@ describe('Snapped options', () => {
name: 'test',
options: {
test: { pct: 30, min: 0, max: 100, snap: 12, toAbs },
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(60)
@ -32,9 +32,9 @@ describe('Snapped options', () => {
toAbs,
snap: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144],
},
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const patternA = new design({ options: { test: 0.13 }, measurements }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements }).draft()
@ -57,9 +57,9 @@ describe('Snapped options', () => {
imperial: [25.4, 50.8, 76.2, 101.6],
},
},
}
},
}
const design = new Design({ parts: [ part ] })
const design = new Design({ parts: [part] })
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'metric' }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'metric' }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'metric' }).draft()
@ -84,13 +84,29 @@ describe('Snapped options', () => {
imperial: [25.4, 50.8, 76.2, 101.6],
},
},
}
},
}
const design = new Design({ parts: [ part ] })
const patternA = new design({ options: { test: 0.13 }, measurements, units: 'imperial' }).draft()
const patternB = new design({ options: { test: 0.27 }, measurements, units: 'imperial' }).draft()
const patternC = new design({ options: { test: 0.97 }, measurements, units: 'imperial' }).draft()
const patternD = new design({ options: { test: 0.01 }, measurements, units: 'imperial' }).draft()
const design = new Design({ parts: [part] })
const patternA = new design({
options: { test: 0.13 },
measurements,
units: 'imperial',
}).draft()
const patternB = new design({
options: { test: 0.27 },
measurements,
units: 'imperial',
}).draft()
const patternC = new design({
options: { test: 0.97 },
measurements,
units: 'imperial',
}).draft()
const patternD = new design({
options: { test: 0.01 },
measurements,
units: 'imperial',
}).draft()
expect(patternA.settings.absoluteOptions.test).to.equal(50.8)
expect(patternB.settings.absoluteOptions.test).to.equal(101.6)
expect(patternC.settings.absoluteOptions.test).to.equal(388)

View file

@ -514,7 +514,7 @@ describe('Utils', () => {
expect(result.toAbs(0.0123, { measurements })).to.equal(12.3)
expect(result.fromAbs(12.3, { measurements })).to.equal(0.0123)
})
/*
/*
it('Should generate a part transform', () => {
const part = {
name: 'test',