1
0
Fork 0

🚧 repo move to freesewing org

This commit is contained in:
joostdecock 2018-07-24 14:38:03 +00:00
parent 27500d52e1
commit 014eee3209
2 changed files with 23 additions and 11 deletions

View file

@ -1,5 +1,7 @@
import { macroName } from "./utils"; import { macroName } from "./utils";
import point from "./point"; import point from "./point";
import path from "./path";
import snippet from "./snippet";
import attributes from "./attributes"; import attributes from "./attributes";
import * as hooklib from "hooks"; import * as hooklib from "hooks";
@ -13,6 +15,11 @@ function part(id) {
this.points.origin = new point(0, 0); this.points.origin = new point(0, 0);
for (let k in hooklib) this[k] = hooklib[k]; for (let k in hooklib) this[k] = hooklib[k];
// Constructors so macros can create objects
this.point = point;
this.path = path;
this.snippet = snippet;
return this; return this;
} }
@ -23,6 +30,8 @@ part.prototype.macroRunner = function(args) {
let macro = macroName(key); let macro = macroName(key);
if (typeof self[macro] === "function") { if (typeof self[macro] === "function") {
self[macro](data); self[macro](data);
} else {
console.log(`Warning: ${macro} is not registered`);
} }
}; };

View file

@ -83,17 +83,7 @@ pattern.prototype.on = function(hook, method) {
this.hooks._hooks[hook].push(method); this.hooks._hooks[hook].push(method);
}; };
pattern.prototype.macro = function(key, method) { pattern.prototype.with = function(plugin) {
let name = macroName(key);
this.on(name, method);
for (let partId in this.parts) {
let part = this.parts[partId];
part[name] = () => null;
this.hooks.attach(name, part);
}
};
pattern.prototype.withPlugin = function(plugin) {
if (plugin.hooks) this.loadPluginHooks(plugin); if (plugin.hooks) this.loadPluginHooks(plugin);
if (plugin.macros) this.loadPluginMacros(plugin); if (plugin.macros) this.loadPluginMacros(plugin);
@ -110,8 +100,21 @@ pattern.prototype.loadPluginHooks = function(plugin) {
pattern.prototype.loadPluginMacros = function(plugin) { pattern.prototype.loadPluginMacros = function(plugin) {
for (let macro in plugin.macros) { for (let macro in plugin.macros) {
console.log("loading ", macro);
if (typeof plugin.macros[macro] === "function") { if (typeof plugin.macros[macro] === "function") {
console.log(macro, "is a function");
this.macro(macro, plugin.macros[macro]); this.macro(macro, plugin.macros[macro]);
} }
} }
}; };
pattern.prototype.macro = function(key, method) {
let name = macroName(key);
this.on(name, method);
for (let partId in this.parts) {
console.log(`Attaching macro ${name} to part ${partId}`);
let part = this.parts[partId];
part[name] = () => null;
this.hooks.attach(name, part);
}
};