From ea0801949e1ec9fbdb7af424eae965bc23fb33f3 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Fri, 10 Aug 2018 15:36:39 +0200 Subject: [PATCH] :boom: Changed hook structure --- src/debug.js | 16 ---------------- src/hooks.js | 31 +++++++++++++++++++++++++++++-- src/pattern.js | 22 ++++++++++++++++------ src/svg.js | 16 ++++++++-------- 4 files changed, 53 insertions(+), 32 deletions(-) delete mode 100644 src/debug.js diff --git a/src/debug.js b/src/debug.js deleted file mode 100644 index 4b05eddae31..00000000000 --- a/src/debug.js +++ /dev/null @@ -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; diff --git a/src/hooks.js b/src/hooks.js index ecffad7f069..449bbd21532 100644 --- a/src/hooks.js +++ b/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; diff --git a/src/pattern.js b/src/pattern.js index b9c0d4b06a4..4c3a8461ce1 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -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); diff --git a/src/svg.js b/src/svg.js index 61f72ca58ad..3e6af2a84fa 100644 --- a/src/svg.js +++ b/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() + ""; this.svg += this.renderComments(this.footer); - this.postRenderSvg(); + this.postRender(); return this.svg; };