wip: Added part-level dependencies
Restructured code a bit to handle all part-level config in one call. Removed check in shorthand for debug as it's no longer used. Updated tests to not fall over on different error message format in newer NodeJS versions
This commit is contained in:
parent
689f908f68
commit
4cf9c3bd47
5 changed files with 187 additions and 175 deletions
|
@ -1,5 +1,5 @@
|
||||||
import Pattern from './pattern'
|
import Pattern from './pattern'
|
||||||
import { addOptions, addMeasurements, addOptionalMeasurements } from './utils.js'
|
import { addPartConfig } from './utils.js'
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The Design constructor. Returns a Pattern constructor
|
* The Design constructor. Returns a Pattern constructor
|
||||||
|
@ -11,11 +11,7 @@ export default function Design(config, plugins = false, conditionalPlugins = fal
|
||||||
if (!config.measurements) config.measurements = []
|
if (!config.measurements) config.measurements = []
|
||||||
if (!config.optionalMeasurements) config.optionalMeasurements = []
|
if (!config.optionalMeasurements) config.optionalMeasurements = []
|
||||||
if (config.parts) {
|
if (config.parts) {
|
||||||
for (const partName in config.parts) {
|
for (const partName in config.parts) config = addPartConfig(config.parts[partName], config)
|
||||||
config = addOptions(config.parts[partName], config)
|
|
||||||
config = addMeasurements(config.parts[partName], config)
|
|
||||||
config = addOptionalMeasurements(config.parts[partName], config)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure all options have a hide() method
|
// Ensure all options have a hide() method
|
||||||
|
|
|
@ -180,9 +180,9 @@ Part.prototype.units = function (input) {
|
||||||
|
|
||||||
/** Returns an object with shorthand access for pattern design */
|
/** Returns an object with shorthand access for pattern design */
|
||||||
Part.prototype.shorthand = function () {
|
Part.prototype.shorthand = function () {
|
||||||
let complete = this.context.settings.complete ? true : false
|
const complete = this.context.settings.complete ? true : false
|
||||||
let paperless = this.context.settings.paperless === true ? true : false
|
const paperless = this.context.settings.paperless === true ? true : false
|
||||||
let sa = this.context.settings.complete ? this.context.settings.sa || 0 : 0
|
const sa = this.context.settings.complete ? this.context.settings.sa || 0 : 0
|
||||||
const shorthand = {
|
const shorthand = {
|
||||||
sa,
|
sa,
|
||||||
scale: this.context.settings.scale,
|
scale: this.context.settings.scale,
|
||||||
|
@ -198,7 +198,6 @@ Part.prototype.shorthand = function () {
|
||||||
removeCut: this.removeCut,
|
removeCut: this.removeCut,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.context.settings.debug) {
|
|
||||||
// We'll need this
|
// We'll need this
|
||||||
let self = this
|
let self = this
|
||||||
|
|
||||||
|
@ -335,17 +334,6 @@ Part.prototype.shorthand = function () {
|
||||||
this.context.settings.absoluteOptions || {},
|
this.context.settings.absoluteOptions || {},
|
||||||
absoluteOptionsProxy
|
absoluteOptionsProxy
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
shorthand.Point = Point
|
|
||||||
shorthand.Path = Path
|
|
||||||
shorthand.Snippet = Snippet
|
|
||||||
shorthand.points = this.points || {}
|
|
||||||
shorthand.paths = this.paths || {}
|
|
||||||
shorthand.snippets = this.snippets || {}
|
|
||||||
shorthand.measurements = this.context.settings.measurements || {}
|
|
||||||
shorthand.options = this.context.settings.options || {}
|
|
||||||
shorthand.absoluteOptions = this.context.settings.absoluteOptions || {}
|
|
||||||
}
|
|
||||||
|
|
||||||
return shorthand
|
return shorthand
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,7 @@ import {
|
||||||
sampleStyle,
|
sampleStyle,
|
||||||
capitalize,
|
capitalize,
|
||||||
decoratePartDependency,
|
decoratePartDependency,
|
||||||
addOptions,
|
addPartConfig,
|
||||||
addMeasurements,
|
|
||||||
addOptionalMeasurements
|
|
||||||
} from './utils.js'
|
} from './utils.js'
|
||||||
import Part from './part'
|
import Part from './part'
|
||||||
import Point from './point'
|
import Point from './point'
|
||||||
|
@ -256,10 +254,8 @@ Pattern.prototype.addPart = function (part, name=false) {
|
||||||
if (typeof part?.draft === 'function') {
|
if (typeof part?.draft === 'function') {
|
||||||
if (part.name) {
|
if (part.name) {
|
||||||
this.config.parts[part.name] = part
|
this.config.parts[part.name] = part
|
||||||
// Add part options/measurements/optionalMeasurements to config
|
// Add part-level config to config
|
||||||
this.config = addOptions(part, this.config)
|
this.config = addPartConfig(part, this.config)
|
||||||
this.config = addMeasurements(part, this.config)
|
|
||||||
this.config = addOptionalMeasurements(part, this.config)
|
|
||||||
}
|
}
|
||||||
else this.raise.error(`Part must have a name`)
|
else this.raise.error(`Part must have a name`)
|
||||||
}
|
}
|
||||||
|
|
|
@ -390,26 +390,26 @@ export const generatePartTransform = (x, y, rotate, flipX, flipY, part) => {
|
||||||
export const decoratePartDependency = (obj, name) => (typeof obj === 'function') ? { draft: obj, name } : obj
|
export const decoratePartDependency = (obj, name) => (typeof obj === 'function') ? { draft: obj, name } : obj
|
||||||
|
|
||||||
// Add part-level options
|
// Add part-level options
|
||||||
export const addOptions = (part, config) => {
|
const addPartOptions = (part, config) => {
|
||||||
if (part.options) {
|
if (part.options) {
|
||||||
for (const optionName in part.options) {
|
for (const optionName in part.options) {
|
||||||
config.options[optionName] = part.options[optionName]
|
config.options[optionName] = part.options[optionName]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (part.from) addOptions(part.from, config)
|
if (part.from) addPartOptions(part.from, config)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add part-level measurements
|
// Add part-level measurements
|
||||||
export const addMeasurements = (part, config, list=false) => {
|
const addPartMeasurements = (part, config, list=false) => {
|
||||||
if (!list) list = config.measurements
|
if (!list) list = config.measurements
|
||||||
? [...config.measurements]
|
? [...config.measurements]
|
||||||
: []
|
: []
|
||||||
if (part.measurements) {
|
if (part.measurements) {
|
||||||
for (const m of part.measurements) list.push(m)
|
for (const m of part.measurements) list.push(m)
|
||||||
}
|
}
|
||||||
if (part.from) addMeasurements(part.from, config, list)
|
if (part.from) addPartMeasurements(part.from, config, list)
|
||||||
|
|
||||||
// Weed out duplicates
|
// Weed out duplicates
|
||||||
config.measurements = [...new Set(list)]
|
config.measurements = [...new Set(list)]
|
||||||
|
@ -418,7 +418,7 @@ export const addMeasurements = (part, config, list=false) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add part-level optional measurements
|
// Add part-level optional measurements
|
||||||
export const addOptionalMeasurements = (part, config, list=false) => {
|
const addPartOptionalMeasurements = (part, config, list=false) => {
|
||||||
if (!list) list = config.optionalMeasurements
|
if (!list) list = config.optionalMeasurements
|
||||||
? [...config.optionalMeasurements]
|
? [...config.optionalMeasurements]
|
||||||
: []
|
: []
|
||||||
|
@ -428,7 +428,7 @@ export const addOptionalMeasurements = (part, config, list=false) => {
|
||||||
if (config.measurements.indexOf(m) === -1) list.push(m)
|
if (config.measurements.indexOf(m) === -1) list.push(m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (part.from) addOptionalMeasurements(part.from, config, list)
|
if (part.from) addPartOptionalMeasurements(part.from, config, list)
|
||||||
|
|
||||||
// Weed out duplicates
|
// Weed out duplicates
|
||||||
config.optionalMeasurements = [...new Set(list)]
|
config.optionalMeasurements = [...new Set(list)]
|
||||||
|
@ -437,3 +437,34 @@ export const addOptionalMeasurements = (part, config, list=false) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const addDependencies = (dep, current) => {
|
||||||
|
// Current dependencies
|
||||||
|
const list = []
|
||||||
|
if (Array.isArray(current)) list.push(...current)
|
||||||
|
else if (typeof current === 'string') list.push(current)
|
||||||
|
|
||||||
|
if (Array.isArray(dep)) list.push(...dep)
|
||||||
|
else if (typeof dep === 'string') list.push(dep)
|
||||||
|
|
||||||
|
return [...new Set(list)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add part-level dependencies
|
||||||
|
export const addPartDependencies = (part, config) => {
|
||||||
|
if (part.after) {
|
||||||
|
config.dependencies[part.name] = addDependencies(config.dependencies[part.name], part.after)
|
||||||
|
}
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
export const addPartConfig = (part, config) => {
|
||||||
|
config = addPartOptions(part, config)
|
||||||
|
config = addPartMeasurements(part, config)
|
||||||
|
config = addPartOptionalMeasurements(part, config)
|
||||||
|
config = addPartDependencies(part, config)
|
||||||
|
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1017,7 +1017,7 @@ it("Should raise a warning when an insop operation used an falsy ID", () => {
|
||||||
new freesewing.Path().withRaise(raise).noop('test').insop('test')
|
new freesewing.Path().withRaise(raise).noop('test').insop('test')
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
expect(''+err).to.contain("Cannot read property 'ops")
|
expect(''+err).to.contain("Cannot read prop")
|
||||||
}
|
}
|
||||||
expect(invalid).to.equal(true);
|
expect(invalid).to.equal(true);
|
||||||
});
|
});
|
||||||
|
@ -1057,10 +1057,11 @@ it("Should raise a warning when calling join without a path", () => {
|
||||||
points.a = new Point(0,0)
|
points.a = new Point(0,0)
|
||||||
points.b = new Point(10,10)
|
points.b = new Point(10,10)
|
||||||
try {
|
try {
|
||||||
paths.a = new Path().move(points.a).line(points.b).join()
|
//paths.a = new Path().move(points.a).line(points.b).join()
|
||||||
|
pattern.parts.a.paths.a = new Path().move(points.a).line(points.b).join()
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
expect(''+err).to.contain("Cannot read property 'ops")
|
expect(''+err).to.contain("Cannot read prop")
|
||||||
}
|
}
|
||||||
expect(pattern.events.error.length).to.equal(1)
|
expect(pattern.events.error.length).to.equal(1)
|
||||||
expect(pattern.events.error[0]).to.equal("Called `Path.join(that)` but `that` is not a `Path` object")
|
expect(pattern.events.error[0]).to.equal("Called `Path.join(that)` but `that` is not a `Path` object")
|
||||||
|
@ -1074,7 +1075,7 @@ it("Should raise a warning when calling start on a path without drawing operatio
|
||||||
new freesewing.Path().withRaise(raise).start()
|
new freesewing.Path().withRaise(raise).start()
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
expect(''+err).to.contain("TypeError: Cannot read property")
|
expect(''+err).to.contain("TypeError: Cannot read prop")
|
||||||
}
|
}
|
||||||
expect(invalid).to.equal(true);
|
expect(invalid).to.equal(true);
|
||||||
});
|
});
|
||||||
|
@ -1087,7 +1088,7 @@ it("Should raise a warning when calling end on a path without drawing operations
|
||||||
new freesewing.Path().withRaise(raise).end()
|
new freesewing.Path().withRaise(raise).end()
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
expect(''+err).to.contain("TypeError: Cannot read property")
|
expect(''+err).to.contain("TypeError: Cannot read prop")
|
||||||
}
|
}
|
||||||
expect(invalid).to.equal(true);
|
expect(invalid).to.equal(true);
|
||||||
});
|
});
|
||||||
|
@ -1140,7 +1141,7 @@ it("Should raise a warning when splitting a path on a non-point", () => {
|
||||||
path.split()
|
path.split()
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
expect(''+err).to.contain("TypeError: Cannot read property")
|
expect(''+err).to.contain("TypeError: Cannot read prop")
|
||||||
}
|
}
|
||||||
expect(invalid).to.equal(true);
|
expect(invalid).to.equal(true);
|
||||||
});
|
});
|
||||||
|
@ -1164,7 +1165,7 @@ it("Should raise a warning when splitting a path on a non-point", () => {
|
||||||
path.split()
|
path.split()
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
expect(''+err).to.contain("TypeError: Cannot read property")
|
expect(''+err).to.contain("TypeError: Cannot read prop")
|
||||||
}
|
}
|
||||||
expect(invalid).to.equal(true);
|
expect(invalid).to.equal(true);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue