1
0
Fork 0
freesewing/markdown/dev/guides/plugins/macros/en.md
Joost De Cock b34a2ee2ed feat: Flat import of markdown repo
This is a flat (without history) import of (some of) the content
from our markdown module.

We've imported this without history because the repo contains our
blog posts and showcases posts content prior to porting them to strapi.

Since this contains many images, it would balloon the size of this repo
to import the full history.

Instead, please refer to the history of the (archived) markdown repo
at: https://github.com/freesewing/markdown
2021-08-25 16:09:31 +02:00

1.6 KiB

title order
Macros 90

Plugin structure for macros is similar, with a few changes:

  • Rather than the hook name, you provide the macro name (that you choose yourself)
  • The context (this) of a macro method is always a Part object.

Apart from these, the structure is very similar:

import {name, version} from '../package.json';

export default {
  name,
  version,
  macros: {
    box: function(so) {
      this.points.boxTopLeft = so.anchor;
      this.points.boxTopRight = so.anchor.shift(0, so.size);
      this.points.boxBottomRight = this.points.boxTopRight.shift(-90, so.size);
      this.points.boxBottomLeft = new this.Point(so.anchor.x, this.points.boxBottomRight.y);
     
      this.paths.box = new this.Path()
        .move(this.points.boxTopLeft)
        .line(this.points.boxTopRight)
        .line(this.points.boxBottomRight)
        .line(this.points.boxBottomLeft)
        .close()
        .attr('class', 'box');
		}
	}
}

Did you figure out what this plugin does? It provides a box macro that draws a box on our pattern in a given location with a give size.

We can use it like this:

points.boxAnchor = new Point(100, 100);
macro('box', {
  anchor: points.boxAnchor
  size: 25
}); 

Obviously, you can expect to learn how to call a macro in its documentation, rather than have to comb through its code.

Macros take only 1 argument

When writing a macro, keep in mind that all information that needs to be passed to a macro needs to be contained in a single argument.

Typically, you use a single plain object to configure the macro.