1
0
Fork 0

Workaround for #7 (#8)

* 🚧 Initial workaround for #7

* 🚧 Added workaround for preSample and postSample hooks
This commit is contained in:
Joost De Cock 2018-09-30 16:25:03 +02:00 committed by GitHub
parent 8f207a1904
commit 0963a4f330
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 33 deletions

8
package-lock.json generated
View file

@ -2835,10 +2835,10 @@
"os-tmpdir": "^1.0.1" "os-tmpdir": "^1.0.1"
} }
}, },
"hooks": { "hooks-fixed": {
"version": "0.3.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/hooks/-/hooks-0.3.2.tgz", "resolved": "https://registry.npmjs.org/hooks-fixed/-/hooks-fixed-2.0.2.tgz",
"integrity": "sha1-ox8GDCAmzqbPHKPrF4Qw5xjhxKM=" "integrity": "sha512-YurCM4gQSetcrhwEtpQHhQ4M7Zo7poNGqY4kQGeBS6eZtOcT3tnNs01ThFa0jYBByAiYt1MjMjP/YApG0EnAvQ=="
}, },
"hosted-git-info": { "hosted-git-info": {
"version": "2.7.1", "version": "2.7.1",

View file

@ -27,9 +27,9 @@
"pretty": "npx prettier --write 'src/*.js'", "pretty": "npx prettier --write 'src/*.js'",
"lint": "eslint --fix '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", "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", "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", "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", "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", "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" "watch": "rollup -c rollup.js -o dist/index.js -f cjs -m true --watch"
}, },
@ -53,7 +53,7 @@
"dependencies": { "dependencies": {
"bezier-js": "^2.2.15", "bezier-js": "^2.2.15",
"bin-pack": "1.0.2", "bin-pack": "1.0.2",
"hooks": "^0.3.2" "hooks-fixed": "2.0.2"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^6.26.3", "babel-core": "^6.26.3",

View file

@ -22,25 +22,7 @@ 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;
if (hook === "preDraft") { for (let func of this._hooks[hook]) obj.pre(hook, func);
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;

View file

@ -3,7 +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 * as hooklib from "hooks"; import * as hooklib from "hooks-fixed";
function Part() { function Part() {
this.attributes = new Attributes(); this.attributes = new Attributes();

View file

@ -7,7 +7,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 * as hooklib from "hooks"; import * as hooklib from "hooks-fixed";
export default function Pattern(config = false) { export default function Pattern(config = false) {
// width and height properties // width and height properties
@ -72,12 +72,35 @@ export default function Pattern(config = false) {
this.Part.prototype.hooks = this.hooks; 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 * @throws Will throw an error when called
*/ */
Pattern.prototype.draft = function() { Pattern.prototype._draft = function() {
throw Error( 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 * Handles option sampling
*/ */
Pattern.prototype.sampleOption = function(optionName) { Pattern.prototype.sampleOption = function(optionName) {
this.preSample();
let step, val; let step, val;
let factor = 1; let factor = 1;
let anchors = {}; let anchors = {};
@ -177,6 +201,7 @@ Pattern.prototype.sampleOption = function(optionName) {
val += step; val += step;
} }
this.parts = parts; this.parts = parts;
this.postSample();
return this; return this;
}; };
@ -206,6 +231,7 @@ Pattern.prototype.sampleListOption = function(optionName) {
* Handles measurement sampling * Handles measurement sampling
*/ */
Pattern.prototype.sampleMeasurement = function(measurementName) { Pattern.prototype.sampleMeasurement = function(measurementName) {
this.preSample();
let anchors = {}; let anchors = {};
let parts = this.sampleParts(); let parts = this.sampleParts();
let val = this.settings.measurements[measurementName]; let val = this.settings.measurements[measurementName];
@ -223,6 +249,7 @@ Pattern.prototype.sampleMeasurement = function(measurementName) {
val += step; val += step;
} }
this.parts = parts; this.parts = parts;
this.postSample();
return this; return this;
}; };
@ -231,6 +258,7 @@ Pattern.prototype.sampleMeasurement = function(measurementName) {
* Handles models sampling * Handles models sampling
*/ */
Pattern.prototype.sampleModels = function(models, focus = false) { Pattern.prototype.sampleModels = function(models, focus = false) {
this.preSample();
let anchors = {}; let anchors = {};
let parts = this.sampleParts(); let parts = this.sampleParts();
let run = 0; let run = 0;
@ -243,6 +271,7 @@ Pattern.prototype.sampleModels = function(models, focus = false) {
this.sampleRun(parts, anchors, run, runs, className); this.sampleRun(parts, anchors, run, runs, className);
} }
this.parts = parts; this.parts = parts;
this.postSample();
return this; return this;
}; };
@ -277,7 +306,11 @@ Pattern.prototype.on = function(hook, method) {
}; };
Pattern.prototype.with = function(plugin) { 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.hooks) this.loadPluginHooks(plugin);
if (plugin.macros) this.loadPluginMacros(plugin); if (plugin.macros) this.loadPluginMacros(plugin);

View file

@ -1,5 +1,5 @@
import Attributes from "./attributes"; import Attributes from "./attributes";
import * as hooklib from "hooks"; import * as hooklib from "hooks-fixed";
import { version } from "../package.json"; import { version } from "../package.json";