From 0963a4f330e2c9634bbf93e99cf0368d206aed62 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Sun, 30 Sep 2018 16:25:03 +0200 Subject: [PATCH] Workaround for #7 (#8) * :construction: Initial workaround for #7 * :construction: Added workaround for preSample and postSample hooks --- package-lock.json | 8 ++++---- package.json | 8 ++++---- src/hooks.js | 20 +------------------- src/part.js | 2 +- src/pattern.js | 41 +++++++++++++++++++++++++++++++++++++---- src/svg.js | 2 +- 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3a18ca7ffbc..b90d48f5ce5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2835,10 +2835,10 @@ "os-tmpdir": "^1.0.1" } }, - "hooks": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/hooks/-/hooks-0.3.2.tgz", - "integrity": "sha1-ox8GDCAmzqbPHKPrF4Qw5xjhxKM=" + "hooks-fixed": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hooks-fixed/-/hooks-fixed-2.0.2.tgz", + "integrity": "sha512-YurCM4gQSetcrhwEtpQHhQ4M7Zo7poNGqY4kQGeBS6eZtOcT3tnNs01ThFa0jYBByAiYt1MjMjP/YApG0EnAvQ==" }, "hosted-git-info": { "version": "2.7.1", diff --git a/package.json b/package.json index e467847d0a7..8cd9a483375 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,9 @@ "pretty": "npx prettier --write 'src/*.js'", "lint": "eslint --fix 'src/*.js'", "browserbuild": "BABEL_ENV=develop rollup -c rollup.js -o dist/browser.js -f iife -m true -n freesewing", - "nodebuild": "BABEL_ENV=production rollup -c rollup.js -o dist/index.js -f cjs -m true -e bezier-js,bin-pack,hooks", - "modulebuild": "BABEL_ENV=develop rollup -c rollup.js -o dist/index.mjs -f es -m true -e bezier-js,bin-pack,hooks", - "testbuild": "BABEL_ENV=test rollup -c rollup.js -o tests/dist/index.js -f cjs -e bezier-js,bin-pack,hooks", + "nodebuild": "BABEL_ENV=production rollup -c rollup.js -o dist/index.js -f cjs -m true -e bezier-js,bin-pack,hooks-fixed", + "modulebuild": "BABEL_ENV=develop rollup -c rollup.js -o dist/index.mjs -f es -m true -e bezier-js,bin-pack,hooks-fixed", + "testbuild": "BABEL_ENV=test rollup -c rollup.js -o tests/dist/index.js -f cjs -e bezier-js,bin-pack,hooks-fixed", "build": "npm run clean && npm run browserbuild && npm run nodebuild && npm run modulebuild && npm run testbuild", "watch": "rollup -c rollup.js -o dist/index.js -f cjs -m true --watch" }, @@ -53,7 +53,7 @@ "dependencies": { "bezier-js": "^2.2.15", "bin-pack": "1.0.2", - "hooks": "^0.3.2" + "hooks-fixed": "2.0.2" }, "devDependencies": { "babel-core": "^6.26.3", diff --git a/src/hooks.js b/src/hooks.js index 449bbd21532..78c6abd2fb0 100644 --- a/src/hooks.js +++ b/src/hooks.js @@ -22,25 +22,7 @@ Hooks.prototype.list = function(hook) { Hooks.prototype.attach = function(hook, obj) { if (typeof this._hooks[hook] === "undefined") return; - 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); - } + for (let func of this._hooks[hook]) obj.pre(hook, func); }; export default Hooks; diff --git a/src/part.js b/src/part.js index 49304b62d74..902ba5fa445 100644 --- a/src/part.js +++ b/src/part.js @@ -3,7 +3,7 @@ import Point from "./point"; import Path from "./path"; import Snippet from "./snippet"; import Attributes from "./attributes"; -import * as hooklib from "hooks"; +import * as hooklib from "hooks-fixed"; function Part() { this.attributes = new Attributes(); diff --git a/src/pattern.js b/src/pattern.js index a3f8f49288f..21e9de50a2c 100644 --- a/src/pattern.js +++ b/src/pattern.js @@ -7,7 +7,7 @@ import Svg from "./svg"; import Hooks from "./hooks"; import pack from "bin-pack"; import Store from "./store"; -import * as hooklib from "hooks"; +import * as hooklib from "hooks-fixed"; export default function Pattern(config = false) { // width and height properties @@ -72,12 +72,35 @@ export default function Pattern(config = false) { this.Part.prototype.hooks = this.hooks; } +/** Method to attach preDraft hooks on */ +Pattern.prototype.preDraft = function() {}; + +/** Method to attach postDraft hooks on */ +Pattern.prototype.postDraft = function() {}; + +/** Method to attach preSample hooks on */ +Pattern.prototype.preSample = function() {}; + +/** Method to attach postSample hooks on */ +Pattern.prototype.postSample = function() {}; + +/** + * Calls _draft in the method, and pre- and postDraft + */ +Pattern.prototype.draft = function() { + this.preDraft(); + this._draft(); + this.postDraft(); + + return this; +}; + /** * @throws Will throw an error when called */ -Pattern.prototype.draft = function() { +Pattern.prototype._draft = function() { throw Error( - "You have to implement the draft() method in your Pattern instance." + "You have to implement the _draft() method in your Pattern instance." ); }; @@ -150,6 +173,7 @@ Pattern.prototype.sampleRun = function( * Handles option sampling */ Pattern.prototype.sampleOption = function(optionName) { + this.preSample(); let step, val; let factor = 1; let anchors = {}; @@ -177,6 +201,7 @@ Pattern.prototype.sampleOption = function(optionName) { val += step; } this.parts = parts; + this.postSample(); return this; }; @@ -206,6 +231,7 @@ Pattern.prototype.sampleListOption = function(optionName) { * Handles measurement sampling */ Pattern.prototype.sampleMeasurement = function(measurementName) { + this.preSample(); let anchors = {}; let parts = this.sampleParts(); let val = this.settings.measurements[measurementName]; @@ -223,6 +249,7 @@ Pattern.prototype.sampleMeasurement = function(measurementName) { val += step; } this.parts = parts; + this.postSample(); return this; }; @@ -231,6 +258,7 @@ Pattern.prototype.sampleMeasurement = function(measurementName) { * Handles models sampling */ Pattern.prototype.sampleModels = function(models, focus = false) { + this.preSample(); let anchors = {}; let parts = this.sampleParts(); let run = 0; @@ -243,6 +271,7 @@ Pattern.prototype.sampleModels = function(models, focus = false) { this.sampleRun(parts, anchors, run, runs, className); } this.parts = parts; + this.postSample(); return this; }; @@ -277,7 +306,11 @@ Pattern.prototype.on = function(hook, method) { }; Pattern.prototype.with = function(plugin) { - this.debug("success", "🔌 Plugin loaded", `${plugin.name} v${plugin.version}`); + this.debug( + "success", + "🔌 Plugin loaded", + `${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 bdb7cd35aeb..ff9e39b3ad5 100644 --- a/src/svg.js +++ b/src/svg.js @@ -1,5 +1,5 @@ import Attributes from "./attributes"; -import * as hooklib from "hooks"; +import * as hooklib from "hooks-fixed"; import { version } from "../package.json";