💥 Changed hook structure
This commit is contained in:
parent
513e29dac6
commit
ea0801949e
4 changed files with 53 additions and 32 deletions
16
src/debug.js
16
src/debug.js
|
@ -1,16 +0,0 @@
|
||||||
import * as hooklib from "hooks";
|
|
||||||
|
|
||||||
function Debug(hooks) {
|
|
||||||
for (let k in hooklib) this[k] = hooklib[k];
|
|
||||||
this.hooks = hooks;
|
|
||||||
|
|
||||||
let self = this;
|
|
||||||
this.hooks.attach("debug", self);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Debug method, exposes debug hook */
|
|
||||||
Debug.prototype.debug = function(data) {};
|
|
||||||
|
|
||||||
export default Debug;
|
|
31
src/hooks.js
31
src/hooks.js
|
@ -1,6 +1,15 @@
|
||||||
function Hooks() {
|
function Hooks() {
|
||||||
this._hooks = {};
|
this._hooks = {};
|
||||||
this.all = ["preRenderSvg", "postRenderSvg", "insertText", "debug"];
|
this.all = [
|
||||||
|
"preDraft",
|
||||||
|
"postDraft",
|
||||||
|
"preSample",
|
||||||
|
"postSample",
|
||||||
|
"preRender",
|
||||||
|
"postRender",
|
||||||
|
"insertText",
|
||||||
|
"debug"
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
Hooks.prototype.list = function(hook) {
|
Hooks.prototype.list = function(hook) {
|
||||||
|
@ -13,7 +22,25 @@ Hooks.prototype.list = function(hook) {
|
||||||
|
|
||||||
Hooks.prototype.attach = function(hook, obj) {
|
Hooks.prototype.attach = function(hook, obj) {
|
||||||
if (typeof this._hooks[hook] === "undefined") return;
|
if (typeof this._hooks[hook] === "undefined") return;
|
||||||
for (let func of this._hooks[hook]) obj.pre(hook, func);
|
if (hook === "preDraft") {
|
||||||
|
for (let func of this._hooks[hook]) obj.pre("draft", func);
|
||||||
|
} else if (hook === "postDraft") {
|
||||||
|
for (let func of this._hooks[hook]) obj.post("draft", func);
|
||||||
|
} else if (hook === "preSample") {
|
||||||
|
for (let func of this._hooks[hook]) {
|
||||||
|
obj.pre("sampleOption", func);
|
||||||
|
obj.pre("sampleMeasurement", func);
|
||||||
|
obj.pre("sampleModels", func);
|
||||||
|
}
|
||||||
|
} else if (hook === "postSample") {
|
||||||
|
for (let func of this._hooks[hook]) {
|
||||||
|
obj.post("sampleOption", func);
|
||||||
|
obj.post("sampleMeasurement", func);
|
||||||
|
obj.post("sampleModels", func);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (let func of this._hooks[hook]) obj.pre(hook, func);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Hooks;
|
export default Hooks;
|
||||||
|
|
|
@ -8,7 +8,7 @@ import Svg from "./svg";
|
||||||
import Hooks from "./hooks";
|
import Hooks from "./hooks";
|
||||||
import pack from "bin-pack";
|
import pack from "bin-pack";
|
||||||
import Store from "./store";
|
import Store from "./store";
|
||||||
import Debug from "./debug";
|
import * as hooklib from "hooks";
|
||||||
|
|
||||||
export default function Pattern(config = false) {
|
export default function Pattern(config = false) {
|
||||||
// width and height properties
|
// width and height properties
|
||||||
|
@ -16,6 +16,7 @@ export default function Pattern(config = false) {
|
||||||
this.height = false;
|
this.height = false;
|
||||||
|
|
||||||
// Hooks and Svg instance
|
// Hooks and Svg instance
|
||||||
|
for (let k in hooklib) this[k] = hooklib[k];
|
||||||
this.hooks = new Hooks();
|
this.hooks = new Hooks();
|
||||||
Svg.prototype.hooks = this.hooks;
|
Svg.prototype.hooks = this.hooks;
|
||||||
|
|
||||||
|
@ -174,9 +175,7 @@ Pattern.prototype.sampleModels = function(models) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Debug method, exposes debug hook */
|
/** Debug method, exposes debug hook */
|
||||||
Pattern.prototype.debug = function(data) {
|
Pattern.prototype.debug = function(data) {};
|
||||||
this.dbg.debug(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
Pattern.prototype.render = function() {
|
Pattern.prototype.render = function() {
|
||||||
this.svg = new Svg(this);
|
this.svg = new Svg(this);
|
||||||
|
@ -189,11 +188,22 @@ Pattern.prototype.on = function(hook, method) {
|
||||||
this.hooks._hooks[hook] = [];
|
this.hooks._hooks[hook] = [];
|
||||||
}
|
}
|
||||||
this.hooks._hooks[hook].push(method);
|
this.hooks._hooks[hook].push(method);
|
||||||
|
|
||||||
|
// Pattern object hooks need to be attached on load
|
||||||
|
let localHooks = [
|
||||||
|
"preDraft",
|
||||||
|
"postDraft",
|
||||||
|
"preSample",
|
||||||
|
"postSample",
|
||||||
|
"debug"
|
||||||
|
];
|
||||||
|
if (localHooks.includes(hook)) {
|
||||||
|
let self = this;
|
||||||
|
this.hooks.attach(hook, self);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Pattern.prototype.with = function(plugin) {
|
Pattern.prototype.with = function(plugin) {
|
||||||
this.dbg = new Debug(this.hooks);
|
|
||||||
|
|
||||||
this.debug(`Plugin: ${plugin.name} v${plugin.version}`);
|
this.debug(`Plugin: ${plugin.name} v${plugin.version}`);
|
||||||
if (plugin.hooks) this.loadPluginHooks(plugin);
|
if (plugin.hooks) this.loadPluginHooks(plugin);
|
||||||
if (plugin.macros) this.loadPluginMacros(plugin);
|
if (plugin.macros) this.loadPluginMacros(plugin);
|
||||||
|
|
16
src/svg.js
16
src/svg.js
|
@ -27,17 +27,17 @@ function Svg(pattern) {
|
||||||
|
|
||||||
//this.hooks was injected into the prototype by pattern
|
//this.hooks was injected into the prototype by pattern
|
||||||
let self = this;
|
let self = this;
|
||||||
this.hooks.attach("preRenderSvg", self);
|
this.hooks.attach("preRender", self);
|
||||||
this.hooks.attach("postRenderSvg", self);
|
this.hooks.attach("postRender", self);
|
||||||
this.hooks.attach("insertText", self);
|
this.hooks.attach("insertText", self);
|
||||||
this.hooks.attach("debug", self);
|
this.hooks.attach("debug", self);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Method to attach preRenderSvg hooks on */
|
/** Method to attach preRender hooks on */
|
||||||
Svg.prototype.preRenderSvg = function() {};
|
Svg.prototype.preRender = function() {};
|
||||||
|
|
||||||
/** Method to attach postRenderSvg hooks on */
|
/** Method to attach postRender hooks on */
|
||||||
Svg.prototype.postRenderSvg = function() {};
|
Svg.prototype.postRender = function() {};
|
||||||
|
|
||||||
/** Method to attach insertText hooks on */
|
/** Method to attach insertText hooks on */
|
||||||
Svg.prototype.insertText = function() {};
|
Svg.prototype.insertText = function() {};
|
||||||
|
@ -47,7 +47,7 @@ Svg.prototype.debug = function() {};
|
||||||
|
|
||||||
/** Renders a draft object as SVG */
|
/** Renders a draft object as SVG */
|
||||||
Svg.prototype.render = function(pattern) {
|
Svg.prototype.render = function(pattern) {
|
||||||
this.preRenderSvg();
|
this.preRender();
|
||||||
this.attributes.add("width", pattern.width + "mm");
|
this.attributes.add("width", pattern.width + "mm");
|
||||||
this.attributes.add("height", pattern.height + "mm");
|
this.attributes.add("height", pattern.height + "mm");
|
||||||
this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`);
|
this.attributes.add("viewBox", `0 0 ${pattern.width} ${pattern.height}`);
|
||||||
|
@ -69,7 +69,7 @@ Svg.prototype.render = function(pattern) {
|
||||||
this.svg += this.closeGroup();
|
this.svg += this.closeGroup();
|
||||||
this.svg += this.nl() + "</svg>";
|
this.svg += this.nl() + "</svg>";
|
||||||
this.svg += this.renderComments(this.footer);
|
this.svg += this.renderComments(this.footer);
|
||||||
this.postRenderSvg();
|
this.postRender();
|
||||||
return this.svg;
|
return this.svg;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue