💥 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() {
|
||||
this._hooks = {};
|
||||
this.all = ["preRenderSvg", "postRenderSvg", "insertText", "debug"];
|
||||
this.all = [
|
||||
"preDraft",
|
||||
"postDraft",
|
||||
"preSample",
|
||||
"postSample",
|
||||
"preRender",
|
||||
"postRender",
|
||||
"insertText",
|
||||
"debug"
|
||||
];
|
||||
}
|
||||
|
||||
Hooks.prototype.list = function(hook) {
|
||||
|
@ -13,7 +22,25 @@ Hooks.prototype.list = function(hook) {
|
|||
|
||||
Hooks.prototype.attach = function(hook, obj) {
|
||||
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;
|
||||
|
|
|
@ -8,7 +8,7 @@ import Svg from "./svg";
|
|||
import Hooks from "./hooks";
|
||||
import pack from "bin-pack";
|
||||
import Store from "./store";
|
||||
import Debug from "./debug";
|
||||
import * as hooklib from "hooks";
|
||||
|
||||
export default function Pattern(config = false) {
|
||||
// width and height properties
|
||||
|
@ -16,6 +16,7 @@ export default function Pattern(config = false) {
|
|||
this.height = false;
|
||||
|
||||
// Hooks and Svg instance
|
||||
for (let k in hooklib) this[k] = hooklib[k];
|
||||
this.hooks = new Hooks();
|
||||
Svg.prototype.hooks = this.hooks;
|
||||
|
||||
|
@ -174,9 +175,7 @@ Pattern.prototype.sampleModels = function(models) {
|
|||
};
|
||||
|
||||
/** Debug method, exposes debug hook */
|
||||
Pattern.prototype.debug = function(data) {
|
||||
this.dbg.debug(data);
|
||||
};
|
||||
Pattern.prototype.debug = function(data) {};
|
||||
|
||||
Pattern.prototype.render = function() {
|
||||
this.svg = new Svg(this);
|
||||
|
@ -189,11 +188,22 @@ Pattern.prototype.on = function(hook, method) {
|
|||
this.hooks._hooks[hook] = [];
|
||||
}
|
||||
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) {
|
||||
this.dbg = new Debug(this.hooks);
|
||||
|
||||
this.debug(`Plugin: ${plugin.name} v${plugin.version}`);
|
||||
if (plugin.hooks) this.loadPluginHooks(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
|
||||
let self = this;
|
||||
this.hooks.attach("preRenderSvg", self);
|
||||
this.hooks.attach("postRenderSvg", self);
|
||||
this.hooks.attach("preRender", self);
|
||||
this.hooks.attach("postRender", self);
|
||||
this.hooks.attach("insertText", self);
|
||||
this.hooks.attach("debug", self);
|
||||
}
|
||||
|
||||
/** Method to attach preRenderSvg hooks on */
|
||||
Svg.prototype.preRenderSvg = function() {};
|
||||
/** Method to attach preRender hooks on */
|
||||
Svg.prototype.preRender = function() {};
|
||||
|
||||
/** Method to attach postRenderSvg hooks on */
|
||||
Svg.prototype.postRenderSvg = function() {};
|
||||
/** Method to attach postRender hooks on */
|
||||
Svg.prototype.postRender = function() {};
|
||||
|
||||
/** Method to attach insertText hooks on */
|
||||
Svg.prototype.insertText = function() {};
|
||||
|
@ -47,7 +47,7 @@ Svg.prototype.debug = function() {};
|
|||
|
||||
/** Renders a draft object as SVG */
|
||||
Svg.prototype.render = function(pattern) {
|
||||
this.preRenderSvg();
|
||||
this.preRender();
|
||||
this.attributes.add("width", pattern.width + "mm");
|
||||
this.attributes.add("height", pattern.height + "mm");
|
||||
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.nl() + "</svg>";
|
||||
this.svg += this.renderComments(this.footer);
|
||||
this.postRenderSvg();
|
||||
this.postRender();
|
||||
return this.svg;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue