diff --git a/packages/core/src/path.js b/packages/core/src/path.js index 4372efc063b..1c31dd70e66 100644 --- a/packages/core/src/path.js +++ b/packages/core/src/path.js @@ -37,7 +37,7 @@ Path.prototype.setRender = function (render = true) { /** Adds a move operation to Point to */ Path.prototype.move = function (to) { - if (this.debug && to instanceof Point !== true) + if (to instanceof Point !== true) this.raise.warning('Called `Path.rotate(to)` but `to` is not a `Point` object') this.ops.push({ type: 'move', to }) @@ -46,7 +46,7 @@ Path.prototype.move = function (to) { /** Adds a line operation to Point to */ Path.prototype.line = function (to) { - if (this.debug && to instanceof Point !== true) + if (to instanceof Point !== true) this.raise.warning('Called `Path.line(to)` but `to` is not a `Point` object') this.ops.push({ type: 'line', to }) @@ -55,14 +55,12 @@ Path.prototype.line = function (to) { /** Adds a curve operation via cp1 & cp2 to Point to */ Path.prototype.curve = function (cp1, cp2, to) { - if (this.debug) { - if (to instanceof Point !== true) - this.raise.warning('Called `Path.curve(cp1, cp2, to)` but `to` is not a `Point` object') - if (cp1 instanceof Point !== true) - this.raise.warning('Called `Path.curve(cp1, cp2, to)` but `cp1` is not a `Point` object') - if (cp2 instanceof Point !== true) - this.raise.warning('Called `Path.curve(cp1, cp2, to)` but `cp2` is not a `Point` object') - } + if (to instanceof Point !== true) + this.raise.warning('Called `Path.curve(cp1, cp2, to)` but `to` is not a `Point` object') + if (cp1 instanceof Point !== true) + this.raise.warning('Called `Path.curve(cp1, cp2, to)` but `cp1` is not a `Point` object') + if (cp2 instanceof Point !== true) + this.raise.warning('Called `Path.curve(cp1, cp2, to)` but `cp2` is not a `Point` object') this.ops.push({ type: 'curve', cp1, cp2, to }) return this @@ -70,12 +68,10 @@ Path.prototype.curve = function (cp1, cp2, to) { /** Adds a curve operation without cp1 via cp2 to Point to */ Path.prototype._curve = function (cp2, to) { - if (this.debug) { - if (to instanceof Point !== true) - this.raise.warning('Called `Path._curve(cp2, to)` but `to` is not a `Point` object') - if (cp2 instanceof Point !== true) - this.raise.warning('Called `Path._curve(cp2, to)` but `cp2` is not a `Point` object') - } + if (to instanceof Point !== true) + this.raise.warning('Called `Path._curve(cp2, to)` but `to` is not a `Point` object') + if (cp2 instanceof Point !== true) + this.raise.warning('Called `Path._curve(cp2, to)` but `cp2` is not a `Point` object') let cp1 = this.ops.slice(-1).pop().to this.ops.push({ type: 'curve', cp1, cp2, to }) @@ -84,12 +80,10 @@ Path.prototype._curve = function (cp2, to) { /** Adds a curve operation via cp1 with no cp2 to Point to */ Path.prototype.curve_ = function (cp1, to) { - if (this.debug) { - if (to instanceof Point !== true) - this.raise.warning('Called `Path.curve_(cp1, to)` but `to` is not a `Point` object') - if (cp1 instanceof Point !== true) - this.raise.warning('Called `Path.curve_(cp1, to)` but `cp2` is not a `Point` object') - } + if (to instanceof Point !== true) + this.raise.warning('Called `Path.curve_(cp1, to)` but `to` is not a `Point` object') + if (cp1 instanceof Point !== true) + this.raise.warning('Called `Path.curve_(cp1, to)` but `cp2` is not a `Point` object') let cp2 = to.copy() this.ops.push({ type: 'curve', cp1, cp2, to }) @@ -112,12 +106,10 @@ Path.prototype.noop = function (id = false) { /** Replace a noop operation with the ops from path */ Path.prototype.insop = function (noopId, path) { - if (this.debug) { - if (!noopId) - this.raise.warning('Called `Path.insop(noopId, path)` but `noopId` is undefined or false') - if (path instanceof Path !== true) - this.raise.warning('Called `Path.insop(noopId, path) but `path` is not a `Path` object') - } + if (!noopId) + this.raise.warning('Called `Path.insop(noopId, path)` but `noopId` is undefined or false') + if (path instanceof Path !== true) + this.raise.warning('Called `Path.insop(noopId, path) but `path` is not a `Path` object') let newPath = this.clone() for (let i in newPath.ops) { if (newPath.ops[i].type === 'noop' && newPath.ops[i].id === noopId) { @@ -133,20 +125,18 @@ Path.prototype.insop = function (noopId, path) { /** Adds an attribute. This is here to make this call chainable in assignment */ Path.prototype.attr = function (name, value, overwrite = false) { - if (this.debug) { - if (!name) - this.raise.warning( - 'Called `Path.attr(name, value, overwrite=false)` but `name` is undefined or false' - ) - if (typeof value === 'undefined') - this.raise.warning( - 'Called `Path.attr(name, value, overwrite=false)` but `value` is undefined' - ) - if (overwrite) - this.raise.debug( - `Overwriting \`Path.attribute.${name}\` with ${value} (was: ${this.attributes.get(name)})` - ) - } + if (!name) + this.raise.warning( + 'Called `Path.attr(name, value, overwrite=false)` but `name` is undefined or false' + ) + if (typeof value === 'undefined') + this.raise.warning( + 'Called `Path.attr(name, value, overwrite=false)` but `value` is undefined' + ) + if (overwrite) + this.raise.debug( + `Overwriting \`Path.attribute.${name}\` with ${value} (was: ${this.attributes.get(name)})` + ) if (overwrite) this.attributes.set(name, value) else this.attributes.add(name, value) diff --git a/packages/core/src/pattern.js b/packages/core/src/pattern.js index 84c64e1aa36..90117a3d4b6 100644 --- a/packages/core/src/pattern.js +++ b/packages/core/src/pattern.js @@ -11,46 +11,6 @@ import Attributes from './attributes' import pkg from '../package.json' export default function Pattern(config = { options: {} }) { - // Events store and raise methods - this.events = { - info: [], - warning: [], - error: [], - debug: [], - } - const events = this.events - this.raise = { - info: function (data) { - events.info.push(data) - }, - warning: function (data) { - events.warning.push(data) - }, - error: function (data) { - events.error.push(data) - }, - debug: function (data) { - events.debug.push(data) - }, - } - this.raise.debug( - `New \`@freesewing/${config.name}:${config.version}\` pattern using \`@freesewing/core:${pkg.version}\`` - ) - - this.config = config // Pattern configuration - this.width = 0 // Will be set after render - this.height = 0 // Will be set after render - this.is = '' // Will be set when drafting/sampling - this.debug = true // Will be set when applying settings - - this.store = new Store(this.raise) // Store for sharing data across parts - this.parts = {} // Parts container - this.hooks = new Hooks() // Hooks container - this.Point = Point // Point constructor - this.Path = Path // Path constructor - this.Snippet = Snippet // Snippet constructor - this.Attributes = Attributes // Attributes constructor - // Default settings this.settings = { complete: true, @@ -65,6 +25,50 @@ export default function Pattern(config = { options: {} }) { absoluteOptions: {}, } + // Events store and raise methods + this.events = { + info: [], + warning: [], + error: [], + debug: [], + } + const events = this.events + // Make settings available in the raise.debug method + const settings = this.settings + this.raise = { + info: function (data) { + events.info.push(data) + }, + warning: function (data) { + events.warning.push(data) + }, + error: function (data) { + events.error.push(data) + }, + debug: function (data) { + // Debug only if debug is active + if (settings.debug) events.debug.push(data) + }, + } + this.raise.info( + `New \`@freesewing/${config.name}:${config.version}\` pattern using \`@freesewing/core:${pkg.version}\`` + ) + + this.config = config // Pattern configuration + this.width = 0 // Will be set after render + this.height = 0 // Will be set after render + this.is = '' // Will be set when drafting/sampling + //this.debug = true // Will be set when applying settings + + this.store = new Store(this.raise) // Store for sharing data across parts + this.parts = {} // Parts container + this.hooks = new Hooks() // Hooks container + this.Point = Point // Point constructor + this.Path = Path // Path constructor + this.Snippet = Snippet // Snippet constructor + this.Attributes = Attributes // Attributes constructor + + if (typeof this.config.dependencies === 'undefined') this.config.dependencies = {} if (typeof this.config.inject === 'undefined') this.config.inject = {} if (typeof this.config.hide === 'undefined') this.config.hide = [] @@ -175,7 +179,7 @@ Pattern.prototype.runHooks = function (hookName, data = false) { if (data === false) data = this let hooks = this.hooks[hookName] if (hooks.length > 0) { - if (this.debug) this.raise.debug(`Running \`${hookName}\` hooks`) + this.raise.debug(`Running \`${hookName}\` hooks`) for (let hook of hooks) { hook.method(data, hook.data) } @@ -188,7 +192,7 @@ Pattern.prototype.runHooks = function (hookName, data = false) { Pattern.prototype.draft = function () { if (this.is !== 'sample') { this.is = 'draft' - if (this.debug) this.raise.debug(`Drafting pattern`) + this.raise.debug(`Drafting pattern`) } // Handle snap for pct options for (let i in this.settings.options) { @@ -203,13 +207,12 @@ Pattern.prototype.draft = function () { this.runHooks('preDraft') for (let partName of this.config.draftOrder) { - if (this.debug) this.raise.debug(`Creating part \`${partName}\``) + this.raise.debug(`Creating part \`${partName}\``) this.parts[partName] = new this.Part(partName) if (typeof this.config.inject[partName] === 'string') { - if (this.debug) - this.raise.debug( - `Injecting part \`${this.config.inject[partName]}\` into part \`${partName}\`` - ) + this.raise.debug( + `Injecting part \`${this.config.inject[partName]}\` into part \`${partName}\`` + ) try { this.parts[partName].inject(this.parts[this.config.inject[partName]]) } catch (err) { @@ -242,10 +245,9 @@ Pattern.prototype.draft = function () { this.raise.error([`Unable to set \`render\` property on part \`${partName}\``, err]) } } else { - if (this.debug) - this.raise.debug( - `Part \`${partName}\` is not needed. Skipping draft and setting render to \`false\`` - ) + this.raise.debug( + `Part \`${partName}\` is not needed. Skipping draft and setting render to \`false\`` + ) this.parts[partName].render = false } } @@ -323,7 +325,7 @@ Pattern.prototype.sampleRun = function (parts, anchors, run, runs, extraClass = */ Pattern.prototype.sampleOption = function (optionName) { this.is = 'sample' - if (this.debug) this.raise.debug(`Sampling option \`${optionName}\``) + this.raise.debug(`Sampling option \`${optionName}\``) this.runHooks('preSample') let step, val let factor = 1 @@ -373,7 +375,7 @@ Pattern.prototype.sampleListOption = function (optionName) { */ Pattern.prototype.sampleMeasurement = function (measurementName) { this.is = 'sample' - if (this.debug) this.raise.debug(`Sampling measurement \`${measurementName}\``) + this.raise.debug(`Sampling measurement \`${measurementName}\``) this.runHooks('preSample') let anchors = {} let parts = this.sampleParts() @@ -398,7 +400,7 @@ Pattern.prototype.sampleMeasurement = function (measurementName) { */ Pattern.prototype.sampleModels = function (models, focus = false) { this.is = 'sample' - if (this.debug) this.raise.debug(`Sampling models`) + this.raise.debug(`Sampling models`) this.runHooks('preSample') let anchors = {} let parts = this.sampleParts() @@ -435,7 +437,7 @@ Pattern.prototype.on = function (hook, method, data) { } Pattern.prototype.use = function (plugin, data) { - if (this.debug) this.raise.debug(`Loaded plugin \`${plugin.name}:${plugin.version}\``) + this.raise.info(`Loaded plugin \`${plugin.name}:${plugin.version}\``) if (plugin.hooks) this.loadPluginHooks(plugin, data) if (plugin.macros) this.loadPluginMacros(plugin) @@ -444,16 +446,14 @@ Pattern.prototype.use = function (plugin, data) { Pattern.prototype.useIf = function (plugin, settings) { if (plugin.condition(settings)) { - if (this.debug) - this.raise.debug( - `Condition met: Loaded plugin \`${plugin.plugin.name}:${plugin.plugin.version}\`` - ) + this.raise.info( + `Condition met: Loaded plugin \`${plugin.plugin.name}:${plugin.plugin.version}\`` + ) this.loadPluginHooks(plugin.plugin, plugin.data) } else { - if (this.debug) - this.raise.debug( - `Condition not met: Skipped loading plugin \`${plugin.plugin.name}:${plugin.plugin.version}\`` - ) + this.raise.info( + `Condition not met: Skipped loading plugin \`${plugin.plugin.name}:${plugin.plugin.version}\`` + ) } return this diff --git a/packages/core/src/point.js b/packages/core/src/point.js index fab11ccd5f5..fd00e49f41b 100644 --- a/packages/core/src/point.js +++ b/packages/core/src/point.js @@ -32,7 +32,7 @@ Point.prototype.deg2rad = function (degrees) { /** Adds an attribute. This is here to make this call chainable in assignment */ Point.prototype.attr = function (name, value, overwrite = false) { - if (this.debug) this.check() + this.check() if (overwrite) this.attributes.set(name, value) else this.attributes.add(name, value) @@ -41,10 +41,8 @@ Point.prototype.attr = function (name, value, overwrite = false) { /** Returns the distance between this point and that point */ Point.prototype.dist = function (that) { - if (this.debug) { - this.check() - that.check() - } + this.check() + that.check() let dx = this.x - that.x let dy = this.y - that.y @@ -53,37 +51,32 @@ Point.prototype.dist = function (that) { /** Returns slope of a line made by this point and that point */ Point.prototype.slope = function (that) { - if (this.debug) { - this.check() - that.check() - } + this.check() + that.check() return (that.y - this.y) / (that.x - this.x) } /** Returns the x-delta between this point and that point */ Point.prototype.dx = function (that) { - if (this.debug) { - this.check() - that.check() - } + this.check() + that.check() + return that.x - this.x } /** Returns the y-delta between this point and that point */ Point.prototype.dy = function (that) { - if (this.debug) { - this.check() - that.check() - } + this.check() + that.check() + return that.y - this.y } /** Returns the angle between this point and that point */ Point.prototype.angle = function (that) { - if (this.debug) { - this.check() - that.check() - } + this.check() + that.check() + let rad = Math.atan2(-1 * this.dy(that), this.dx(that)) while (rad < 0) rad += 2 * Math.PI @@ -92,14 +85,12 @@ Point.prototype.angle = function (that) { /** Rotate this point deg around that point */ Point.prototype.rotate = function (deg, that) { - if (this.debug) { - this.check() - that.check() - if (typeof deg !== 'number') - this.raise.warning('Called `Point.rotate(deg,that)` but `deg` is not a number') - if (that instanceof Point !== true) - this.raise.warning('Called `Point.rotate(deg,that)` but `that` is not a `Point` object') - } + this.check() + that.check() + if (typeof deg !== 'number') + this.raise.warning('Called `Point.rotate(deg,that)` but `deg` is not a number') + if (that instanceof Point !== true) + this.raise.warning('Called `Point.rotate(deg,that)` but `that` is not a `Point` object') let radius = this.dist(that) let angle = this.angle(that) let x = that.x + radius * Math.cos(this.deg2rad(angle + deg)) * -1 @@ -110,19 +101,18 @@ Point.prototype.rotate = function (deg, that) { /** returns an identical copy of this point */ Point.prototype.copy = function () { - if (this.debug) this.check() + this.check() + return new Point(this.x, this.y, this.debug).withRaise(this.raise) } /** Mirrors this point around X value of that point */ Point.prototype.flipX = function (that = false) { - if (this.debug) { - this.check() - if (that) { - if (that instanceof Point !== true) - this.raise.warning('Called `Point.rotate(deg,that)` but `that` is not a `Point` object') - that.check() - } + this.check() + if (that) { + if (that instanceof Point !== true) + this.raise.warning('Called `Point.rotate(deg,that)` but `that` is not a `Point` object') + that.check() } if (that === false || that.x === 0) return new Point(this.x * -1, this.y, this.debug).withRaise(this.raise) @@ -131,13 +121,11 @@ Point.prototype.flipX = function (that = false) { /** Mirrors this point around Y value of that point */ Point.prototype.flipY = function (that = false) { - if (this.debug) { - this.check() - if (that) { - if (that instanceof Point !== true) - this.raise.warning('Called `Point.flipY(that)` but `that` is not a `Point` object') - that.check() - } + this.check() + if (that) { + if (that instanceof Point !== true) + this.raise.warning('Called `Point.flipY(that)` but `that` is not a `Point` object') + that.check() } if (that === false || that.y === 0) return new Point(this.x, this.y * -1, this.debug).withRaise(this.raise) @@ -146,11 +134,9 @@ Point.prototype.flipY = function (that = false) { /** Shifts this point distance in the deg direction */ Point.prototype.shift = function (deg, distance) { - if (this.debug) { - this.check() - if (typeof distance !== 'number') - this.raise.warning('Called `Point.shift` but `distance` is not a number') - } + this.check() + if (typeof distance !== 'number') + this.raise.warning('Called `Point.shift` but `distance` is not a number') let p = this.copy() p.x += distance @@ -159,40 +145,34 @@ Point.prototype.shift = function (deg, distance) { /** Shifts this point distance in the direction of that point */ Point.prototype.shiftTowards = function (that, distance) { - if (this.debug) { - if (typeof distance !== 'number') - this.raise.warning('Called `Point.shiftTowards` but `distance` is not a number') - if (that instanceof Point !== true) - this.raise.warning( - 'Called `Point.shiftTowards(that, distance)` but `that` is not a `Point` object' - ) - this.check() - that.check() - } - if (this.debug) this.check() + if (typeof distance !== 'number') + this.raise.warning('Called `Point.shiftTowards` but `distance` is not a number') + if (that instanceof Point !== true) + this.raise.warning( + 'Called `Point.shiftTowards(that, distance)` but `that` is not a `Point` object' + ) + this.check() + that.check() + return this.shift(this.angle(that), distance) } /** Checks whether this has the same coordinates as that */ Point.prototype.sitsOn = function (that) { - if (this.debug) { - if (that instanceof Point !== true) - this.raise.warning('Called `Point.sitsOn(that)` but `that` is not a `Point` object') - this.check() - that.check() - } + if (that instanceof Point !== true) + this.raise.warning('Called `Point.sitsOn(that)` but `that` is not a `Point` object') + this.check() + that.check() if (this.x === that.x && this.y === that.y) return true else return false } /** Checks whether this has roughly the same coordinates as that */ Point.prototype.sitsRoughlyOn = function (that) { - if (this.debug) { - if (that instanceof Point !== true) - this.raise.warning('Called `Point.sitsRoughlyOn(that)` but `that` is not a `Point` object') - this.check() - that.check() - } + if (that instanceof Point !== true) + this.raise.warning('Called `Point.sitsRoughlyOn(that)` but `that` is not a `Point` object') + this.check() + that.check() if (Math.round(this.x) === Math.round(that.x) && Math.round(this.y) === Math.round(that.y)) return true else return false @@ -200,40 +180,38 @@ Point.prototype.sitsRoughlyOn = function (that) { /** Shifts this point fraction of the distance towards that point */ Point.prototype.shiftFractionTowards = function (that, fraction) { - if (this.debug) { - if (that instanceof Point !== true) - this.raise.warning( - 'Called `Point.shiftFractionTowards(that, fraction)` but `that` is not a `Point` object' - ) - if (typeof fraction !== 'number') - this.raise.warning('Called `Point.shiftFractionTowards` but `fraction` is not a number') - this.check() - that.check() - } + if (that instanceof Point !== true) + this.raise.warning( + 'Called `Point.shiftFractionTowards(that, fraction)` but `that` is not a `Point` object' + ) + if (typeof fraction !== 'number') + this.raise.warning('Called `Point.shiftFractionTowards` but `fraction` is not a number') + this.check() + that.check() + return this.shiftTowards(that, this.dist(that) * fraction) } /** Shifts this point distance beyond that point */ Point.prototype.shiftOutwards = function (that, distance) { - if (this.debug) { - if (that instanceof Point !== true) - this.raise.warning( - 'Called `Point.shiftOutwards(that, distance)` but `that` is not a `Point` object' - ) - if (typeof distance !== 'number') - this.raise.warning( - 'Called `Point.shiftOutwards(that, distance)` but `distance` is not a number' - ) - this.check() - that.check() - } + if (that instanceof Point !== true) + this.raise.warning( + 'Called `Point.shiftOutwards(that, distance)` but `that` is not a `Point` object' + ) + if (typeof distance !== 'number') + this.raise.warning( + 'Called `Point.shiftOutwards(that, distance)` but `distance` is not a number' + ) + this.check() + that.check() + return this.shiftTowards(that, this.dist(that) + distance) } /** Returns a deep copy of this */ Point.prototype.clone = function () { - if (this.debug) this.check() - let clone = new Point(this.x, this.y, this.debug).withRaise(this.raise) + this.check() + const clone = new Point(this.x, this.y, this.debug).withRaise(this.raise) clone.attributes = this.attributes.clone() return clone @@ -241,14 +219,12 @@ Point.prototype.clone = function () { /** Applies a translate transform */ Point.prototype.translate = function (x, y) { - if (this.debug) { - this.check() - if (typeof x !== 'number') - this.raise.warning('Called `Point.translate(x,y)` but `x` is not a number') - if (typeof y !== 'number') - this.raise.warning('Called `Point.translate(x,y)` but `y` is not a number') - } - let p = this.copy() + this.check() + if (typeof x !== 'number') + this.raise.warning('Called `Point.translate(x,y)` but `x` is not a number') + if (typeof y !== 'number') + this.raise.warning('Called `Point.translate(x,y)` but `y` is not a number') + const p = this.copy() p.x += x p.y += y diff --git a/packages/core/tests/path.test.js b/packages/core/tests/path.test.js index 3921bac0602..40305327679 100644 --- a/packages/core/tests/path.test.js +++ b/packages/core/tests/path.test.js @@ -766,7 +766,9 @@ it("Should overwrite a path attribute", () => { pattern.parts.a = new pattern.Part(); let a = pattern.parts.a; - a.paths.line = new a.Path() + // Paths from shorthand have the raise method + const { Path } = a.shorthand() + a.paths.line = new Path() .move(new a.Point(0, 0)) .line(new a.Point(0, 40)) .attr("class", "foo")