* 🚧 Initial workaround for #7 * 🚧 Added workaround for preSample and postSample hooks
This commit is contained in:
parent
8f207a1904
commit
0963a4f330
6 changed files with 48 additions and 33 deletions
8
package-lock.json
generated
8
package-lock.json
generated
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
20
src/hooks.js
20
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;
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import Attributes from "./attributes";
|
||||
import * as hooklib from "hooks";
|
||||
import * as hooklib from "hooks-fixed";
|
||||
|
||||
import { version } from "../package.json";
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue