1
0
Fork 0

Updated debug format, and fixed insertText hook test

This commit is contained in:
Joost De Cock 2018-12-18 15:35:07 +01:00
parent 2c276998e0
commit fcbb5aa3b5
3 changed files with 53 additions and 32 deletions

View file

@ -3,6 +3,7 @@ import Point from "./point";
import Path from "./path"; import Path from "./path";
import Snippet from "./snippet"; import Snippet from "./snippet";
import Attributes from "./attributes"; import Attributes from "./attributes";
import hooks from "./hooks";
function Part() { function Part() {
this.attributes = new Attributes(); this.attributes = new Attributes();
@ -22,6 +23,8 @@ function Part() {
this.Path = Path; this.Path = Path;
this.Snippet = Snippet; this.Snippet = Snippet;
this.hooks = hooks; // Hooks container
return this; return this;
} }
@ -32,11 +35,11 @@ Part.prototype.macroClosure = function(args) {
if (typeof self[macro] === "function") { if (typeof self[macro] === "function") {
self[macro](args); self[macro](args);
} else { } else {
self.debug( self.debug({
"warning", type: "warning",
"🚨 Macro not found", label: "🚨 Macro not found",
`Macro ${key} is not registered` msg: `Macro ${key} is not registered`
); });
} }
}; };
@ -45,15 +48,27 @@ Part.prototype.macroClosure = function(args) {
Part.prototype.debugClosure = function() { Part.prototype.debugClosure = function() {
let self = this; let self = this;
let method = function(d, e, b, u, g) { let method = function(data) {
self.debug(d, e, b, u, g); self.debug(data);
}; };
return method; return method;
}; };
/** Debug method, exposes debug hook */ Part.prototype.runHooks = function(hookName, data = false) {
Part.prototype.debug = function(data) {}; 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 */ /** Returns an unused ID */
Part.prototype.getId = function() { Part.prototype.getId = function() {

View file

@ -7,6 +7,7 @@ import Svg from "./svg";
import pack from "bin-pack"; import pack from "bin-pack";
import Store from "./store"; import Store from "./store";
import hooks from "./hooks"; import hooks from "./hooks";
import Attributes from "./attributes";
export default function Pattern(config = false) { export default function Pattern(config = false) {
this.config = config || {}; // Pattern configuration this.config = config || {}; // Pattern configuration
@ -21,6 +22,7 @@ export default function Pattern(config = false) {
this.Point = Point; // Point constructor this.Point = Point; // Point constructor
this.Path = Path; // Path constructor this.Path = Path; // Path constructor
this.Snippet = Snippet; // Snippet constructor this.Snippet = Snippet; // Snippet constructor
this.Attributes = Attributes; // Attributes constructor
// Default settings // Default settings
this.settings = { this.settings = {
@ -225,11 +227,11 @@ Pattern.prototype.sampleOption = function(optionName) {
step = (option.max / factor - val) / 9; step = (option.max / factor - val) / 9;
for (let run = 1; run < 11; run++) { for (let run = 1; run < 11; run++) {
this.settings.options[optionName] = val; this.settings.options[optionName] = val;
this.debug( this.debug({
"info", type: "info",
"🏃🏿‍♀️ Sample run", label: "🏃🏿‍♀️ Sample run",
`Sampling option ${optionName} with value ${round(val)}` msg: `Sampling option ${optionName} with value ${round(val)}`
); });
this.sampleRun(parts, anchors, run, 10); this.sampleRun(parts, anchors, run, 10);
val += step; val += step;
} }
@ -247,11 +249,11 @@ Pattern.prototype.sampleListOption = function(optionName) {
let runs = option.list.length; let runs = option.list.length;
for (let val of option.list) { for (let val of option.list) {
this.settings.options[optionName] = val; this.settings.options[optionName] = val;
this.debug( this.debug({
"info", type: "info",
"🏃🏿‍♀️ Sample run", label: "🏃🏿‍♀️ Sample run",
`Sampling option ${optionName} with value ${round(val)}` msg: `Sampling option ${optionName} with value ${round(val)}`
); });
this.sampleRun(parts, anchors, run, runs); this.sampleRun(parts, anchors, run, runs);
run++; run++;
} }
@ -274,11 +276,11 @@ Pattern.prototype.sampleMeasurement = function(measurementName) {
val = val * 0.9; val = val * 0.9;
for (let run = 1; run < 11; run++) { for (let run = 1; run < 11; run++) {
this.settings.measurements[measurementName] = val; this.settings.measurements[measurementName] = val;
this.debug( this.debug({
"info", type: "info",
"🏃🏿‍♀️ Sample run", label: "🏃🏿‍♀️ Sample run",
`Sampling measurement ${measurementName} with value ${round(val)}` msg: `Sampling option ${measurementName} with value ${round(val)}`
); });
this.sampleRun(parts, anchors, run, 10); this.sampleRun(parts, anchors, run, 10);
val += step; val += step;
} }
@ -301,7 +303,11 @@ Pattern.prototype.sampleModels = function(models, focus = false) {
for (let l in models) { for (let l in models) {
run++; run++;
this.settings.measurements = models[l]; 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" : ""; let className = l === focus ? "sample-focus" : "";
this.sampleRun(parts, anchors, run, runs, className); this.sampleRun(parts, anchors, run, runs, className);
} }
@ -312,7 +318,7 @@ Pattern.prototype.sampleModels = function(models, focus = false) {
}; };
/** Debug method, exposes debug hook */ /** Debug method, exposes debug hook */
Pattern.prototype.debug = function(...data) { Pattern.prototype.debug = function(data) {
this.runHooks("debug", data); this.runHooks("debug", data);
}; };
@ -328,11 +334,11 @@ Pattern.prototype.on = function(hook, method, data) {
}; };
Pattern.prototype.with = function(plugin, data = false) { Pattern.prototype.with = function(plugin, data = false) {
this.debug( this.debug({
"success", type: "success",
"🔌 Plugin loaded", label: "🔌 Plugin loaded",
`${plugin.name} v${plugin.version}` msg: `${plugin.name} v${plugin.version}`
); });
if (plugin.hooks) this.loadPluginHooks(plugin, data); if (plugin.hooks) this.loadPluginHooks(plugin, data);
if (plugin.macros) this.loadPluginMacros(plugin); if (plugin.macros) this.loadPluginMacros(plugin);

View file

@ -229,7 +229,7 @@ it("Should run preRender hook", () => {
it("Should run insertText hook", () => { it("Should run insertText hook", () => {
let pattern = new freesewing.Pattern(); let pattern = new freesewing.Pattern();
pattern.on("insertText", text => { pattern.on("insertText", (locale, text) => {
return text.toUpperCase(); return text.toUpperCase();
}); });
pattern.parts.test = new pattern.Part(); pattern.parts.test = new pattern.Part();