🚧 preSvgRender and postSvgRender hooks
This commit is contained in:
parent
5d81c6d50a
commit
8050628848
2 changed files with 26 additions and 27 deletions
|
@ -48,7 +48,8 @@ export class Pattern {
|
||||||
|
|
||||||
render(): string {
|
render(): string {
|
||||||
let svg = new Svg(this);
|
let svg = new Svg(this);
|
||||||
this.hooks.attach('loadStyle', svg);
|
this.hooks.attach('preSvgRender', svg);
|
||||||
|
this.hooks.attach('postSvgRender', svg);
|
||||||
//svg.pre('preRenderSvg', function(next) {
|
//svg.pre('preRenderSvg', function(next) {
|
||||||
// console.log('manual attach');
|
// console.log('manual attach');
|
||||||
// this.style += "path {stroke: #000; fill: none;}";
|
// this.style += "path {stroke: #000; fill: none;}";
|
||||||
|
|
50
lib/svg.ts
50
lib/svg.ts
|
@ -17,6 +17,7 @@ export class Svg {
|
||||||
attributes: Attributes = new Attributes();
|
attributes: Attributes = new Attributes();
|
||||||
tabs: number = 0;
|
tabs: number = 0;
|
||||||
freeId: number = 1;
|
freeId: number = 1;
|
||||||
|
svg: string = '';
|
||||||
openGroups: string[] = [];
|
openGroups: string[] = [];
|
||||||
hook: any;
|
hook: any;
|
||||||
hooks: string[];
|
hooks: string[];
|
||||||
|
@ -31,44 +32,42 @@ export class Svg {
|
||||||
this.attributes.add("xmlns:freesewing", "http://freesewing.org/namespaces/freesewing");
|
this.attributes.add("xmlns:freesewing", "http://freesewing.org/namespaces/freesewing");
|
||||||
this.attributes.add("freesewing:foo", "bar");
|
this.attributes.add("freesewing:foo", "bar");
|
||||||
this.pattern = pattern;
|
this.pattern = pattern;
|
||||||
this.hooks = ['loadStyle'];
|
this.hooks = ['preSvgRender', 'postSvgRender'];
|
||||||
for(let k in hooklib) this[k] = hooklib[k];
|
for(let k in hooklib) this[k] = hooklib[k];
|
||||||
this.hook('loadStyle', this.loadStyle);
|
for(let k in this.hooks) this.hook(k, this[k]);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Loads CSS styles */
|
/** Method to attach preSvgRender hooks on */
|
||||||
loadStyle(): string {
|
preSvgRender(): void {}
|
||||||
return this.style;
|
|
||||||
}
|
/** Method to attach postSvgRender hooks on */
|
||||||
|
postSvgRender(): void {}
|
||||||
|
|
||||||
loadStyle() {
|
|
||||||
return this.style;
|
|
||||||
}
|
|
||||||
/** Renders a draft object as SVG */
|
/** Renders a draft object as SVG */
|
||||||
render(pattern: Pattern): string {
|
render(pattern: Pattern): string {
|
||||||
this.loadStyle();
|
this.preSvgRender();
|
||||||
let svg = this.prefix;
|
this.svg = this.prefix;
|
||||||
svg += this.renderComments(this.header);
|
this.svg += this.renderComments(this.header);
|
||||||
svg += this.renderSvgTag(pattern);
|
this.svg += this.renderSvgTag(pattern);
|
||||||
svg += this.renderStyle();
|
this.svg += this.renderStyle();
|
||||||
svg += this.renderScript();
|
this.svg += this.renderScript();
|
||||||
svg += this.renderDefs();
|
this.svg += this.renderDefs();
|
||||||
svg += this.openGroup('draftContainer');
|
this.svg += this.openGroup('draftContainer');
|
||||||
for (let partId in pattern.parts) {
|
for (let partId in pattern.parts) {
|
||||||
let part = pattern.parts[partId];
|
let part = pattern.parts[partId];
|
||||||
if (part.render) {
|
if (part.render) {
|
||||||
svg += this.openGroup(part.id, part.attributes);
|
this.svg += this.openGroup(part.id, part.attributes);
|
||||||
svg += this.renderPart(part);
|
this.svg += this.renderPart(part);
|
||||||
svg += this.closeGroup();
|
this.svg += this.closeGroup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
svg += this.closeGroup();
|
this.svg += this.closeGroup();
|
||||||
svg += this.nl()+'</svg>';
|
this.svg += this.nl()+'</svg>';
|
||||||
svg += this.renderComments(this.footer);
|
this.svg += this.renderComments(this.footer);
|
||||||
|
this.postSvgRender();
|
||||||
return svg;
|
return this.svg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns SVG code for the opening SVG tag */
|
/** Returns SVG code for the opening SVG tag */
|
||||||
|
@ -84,7 +83,6 @@ export class Svg {
|
||||||
|
|
||||||
/** Returns SVG code for the style block */
|
/** Returns SVG code for the style block */
|
||||||
renderStyle() {
|
renderStyle() {
|
||||||
this.loadStyle(); // TODO: Hooks docs
|
|
||||||
let svg = '<style type="text/css"> <![CDATA[ ';
|
let svg = '<style type="text/css"> <![CDATA[ ';
|
||||||
this.indent();
|
this.indent();
|
||||||
svg += this.nl()+this.style;
|
svg += this.nl()+this.style;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue