1
0
Fork 0

🚧 Working on hooks

This commit is contained in:
Joost De Cock 2018-08-07 13:46:38 +02:00
parent cff2a4023d
commit eda19195d1
5 changed files with 51 additions and 12 deletions

View file

@ -1,6 +1,6 @@
function Hooks() {
this._hooks = {};
this.all = ["preRenderSvg", "postRenderSvg", "insertText"];
this.all = ["preRenderSvg", "postRenderSvg", "insertText", "debug"];
}
Hooks.prototype.list = function(hook) {
@ -13,9 +13,7 @@ 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);
}
for (let func of this._hooks[hook]) obj.pre(hook, func);
};
export default Hooks;

View file

@ -20,6 +20,8 @@ function Part() {
this.render = true;
this.points.origin = new Point(0, 0);
for (let k in hooklib) this[k] = hooklib[k];
// Keep track of attached hooks
this.attached = { debug: false };
// Constructors so macros can create objects
this.Point = Point;
@ -47,6 +49,16 @@ Part.prototype.macroRunner = function(args) {
return method;
};
/** Debug method, exposes debug hook */
Part.prototype.debug = function(data) {
if (this.attached.debug === false) {
let self = this;
this.hooks.attach("debug", self);
this.attached.debug = true;
this.debug(data);
}
};
/** Returns an unused ID */
Part.prototype.getUid = function() {
this.freeId += 1;

View file

@ -54,6 +54,7 @@ export default function Pattern(config = false) {
};
this.Part.prototype.context = this.context;
this.Part.prototype.macros = {};
this.Part.prototype.hooks = this.hooks;
}
/**
@ -66,9 +67,9 @@ Pattern.prototype.draft = function() {
};
Pattern.prototype.render = function() {
this.hooks.attach("preRenderSvg", this.svg);
//this.hooks.attach("preRenderSvg", this.svg);
this.hooks.attach("postRenderSvg", this.svg);
//this.hooks.attach("postRenderSvg", this.svg);
//this.hooks.attach('insertText', this.svg);
return this.pack().svg.render(this);

View file

@ -23,19 +23,46 @@ function Svg(pattern) {
"http://freesewing.org/namespaces/freesewing"
);
this.attributes.add("freesewing", version);
this.hooks = pattern.hooks.all;
this.hooks = pattern.hooks;
for (let k in hooklib) this[k] = hooklib[k];
for (let k in this.hooks) this.hook(k, this[k]);
for (let k in pattern.hooks.all) this.hook(k, this[k]);
// Keep track of attached hooks
this.attached = {
preRenderSvg: false,
postRenderSvg: false,
insertText: false
};
}
/** Method to attach preRenderSvg hooks on */
Svg.prototype.preRenderSvg = function() {};
Svg.prototype.preRenderSvg = function() {
if (this.attached.preRenderSvg === false) {
let self = this;
this.hooks.attach("preRenderSvg", self);
this.attached.preRenderSvg = true;
this.preRenderSvg();
}
};
/** Method to attach postRenderSvg hooks on */
Svg.prototype.postRenderSvg = function() {};
Svg.prototype.postRenderSvg = function() {
if (this.attached.postRenderSvg === false) {
let self = this;
this.hooks.attach("postRenderSvg", self);
this.attached.postRenderSvg = true;
this.postRenderSvg();
}
};
/** Method to attach insertText hooks on */
Svg.prototype.insertText = function() {};
Svg.prototype.insertText = function(data) {
if (this.attached.inserText === false) {
let self = this;
this.hooks.attach("insertText", self);
this.attached.insertText = true;
this.insertText(data);
}
};
/** Renders a draft object as SVG */
Svg.prototype.render = function(pattern) {

View file

@ -83,7 +83,8 @@ export function shorthand(part) {
Path: part.Path,
Snippet: part.Snippet,
final,
paperless
paperless,
debug: part.debug
};
}