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

View file

@ -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);