diff --git a/src/part.js b/src/part.js index df6a3d8bd97..27610f3dfdd 100644 --- a/src/part.js +++ b/src/part.js @@ -3,6 +3,7 @@ import Point from "./point"; import Path from "./path"; import Snippet from "./snippet"; import Attributes from "./attributes"; +import hooks from "./hooks"; function Part() { this.attributes = new Attributes(); @@ -22,6 +23,8 @@ function Part() { this.Path = Path; this.Snippet = Snippet; + this.hooks = hooks; // Hooks container + return this; } @@ -32,11 +35,11 @@ Part.prototype.macroClosure = function(args) { if (typeof self[macro] === "function") { self[macro](args); } else { - self.debug( - "warning", - "🚨 Macro not found", - `Macro ${key} is not registered` - ); + self.debug({ + type: "warning", + label: "🚨 Macro not found", + msg: `Macro ${key} is not registered` + }); } }; @@ -45,15 +48,27 @@ Part.prototype.macroClosure = function(args) { Part.prototype.debugClosure = function() { let self = this; - let method = function(d, e, b, u, g) { - self.debug(d, e, b, u, g); + let method = function(data) { + self.debug(data); }; return method; }; -/** Debug method, exposes debug hook */ -Part.prototype.debug = function(data) {}; +Part.prototype.runHooks = function(hookName, data = false) { + if (data === false) data = this; + let hooks = this.hooks[hookName]; + if (hooks.length > 0) { + for (let hook of hooks) { + hook.method(data, hook.data); + } + } +}; + +/** Debug method */ +Part.prototype.debug = function(data) { + this.runHooks("debug", data); +}; /** Returns an unused ID */ Part.prototype.getId = function() { diff --git a/src/pattern.js b/src/pattern.js index ce75f1513d9..b61e2d9beaa 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -7,6 +7,7 @@ import Svg from "./svg"; import pack from "bin-pack"; import Store from "./store"; import hooks from "./hooks"; +import Attributes from "./attributes"; export default function Pattern(config = false) { this.config = config || {}; // Pattern configuration @@ -21,6 +22,7 @@ export default function Pattern(config = false) { this.Point = Point; // Point constructor this.Path = Path; // Path constructor this.Snippet = Snippet; // Snippet constructor + this.Attributes = Attributes; // Attributes constructor // Default settings this.settings = { @@ -225,11 +227,11 @@ Pattern.prototype.sampleOption = function(optionName) { step = (option.max / factor - val) / 9; for (let run = 1; run < 11; run++) { this.settings.options[optionName] = val; - this.debug( - "info", - "🏃🏿‍♀️ Sample run", - `Sampling option ${optionName} with value ${round(val)}` - ); + this.debug({ + type: "info", + label: "🏃🏿‍♀️ Sample run", + msg: `Sampling option ${optionName} with value ${round(val)}` + }); this.sampleRun(parts, anchors, run, 10); val += step; } @@ -247,11 +249,11 @@ Pattern.prototype.sampleListOption = function(optionName) { let runs = option.list.length; for (let val of option.list) { this.settings.options[optionName] = val; - this.debug( - "info", - "🏃🏿‍♀️ Sample run", - `Sampling option ${optionName} with value ${round(val)}` - ); + this.debug({ + type: "info", + label: "🏃🏿‍♀️ Sample run", + msg: `Sampling option ${optionName} with value ${round(val)}` + }); this.sampleRun(parts, anchors, run, runs); run++; } @@ -274,11 +276,11 @@ Pattern.prototype.sampleMeasurement = function(measurementName) { val = val * 0.9; for (let run = 1; run < 11; run++) { this.settings.measurements[measurementName] = val; - this.debug( - "info", - "🏃🏿‍♀️ Sample run", - `Sampling measurement ${measurementName} with value ${round(val)}` - ); + this.debug({ + type: "info", + label: "🏃🏿‍♀️ Sample run", + msg: `Sampling option ${measurementName} with value ${round(val)}` + }); this.sampleRun(parts, anchors, run, 10); val += step; } @@ -301,7 +303,11 @@ Pattern.prototype.sampleModels = function(models, focus = false) { for (let l in models) { run++; this.settings.measurements = models[l]; - this.debug("info", "🏃🏿‍♀️ Sample run", `Sampling model ${l}`); + this.debug({ + type: "info", + label: "🏃🏿‍♀️ Sample run", + msg: `Sampling model ${l}` + }); let className = l === focus ? "sample-focus" : ""; this.sampleRun(parts, anchors, run, runs, className); } @@ -312,7 +318,7 @@ Pattern.prototype.sampleModels = function(models, focus = false) { }; /** Debug method, exposes debug hook */ -Pattern.prototype.debug = function(...data) { +Pattern.prototype.debug = function(data) { this.runHooks("debug", data); }; @@ -328,11 +334,11 @@ Pattern.prototype.on = function(hook, method, data) { }; Pattern.prototype.with = function(plugin, data = false) { - this.debug( - "success", - "🔌 Plugin loaded", - `${plugin.name} v${plugin.version}` - ); + this.debug({ + type: "success", + label: "🔌 Plugin loaded", + msg: `${plugin.name} v${plugin.version}` + }); if (plugin.hooks) this.loadPluginHooks(plugin, data); if (plugin.macros) this.loadPluginMacros(plugin); diff --git a/tests/svg.test.js b/tests/svg.test.js index 02a59ee6da3..7dfec73e448 100644 --- a/tests/svg.test.js +++ b/tests/svg.test.js @@ -229,7 +229,7 @@ it("Should run preRender hook", () => { it("Should run insertText hook", () => { let pattern = new freesewing.Pattern(); - pattern.on("insertText", text => { + pattern.on("insertText", (locale, text) => { return text.toUpperCase(); }); pattern.parts.test = new pattern.Part();