2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
title: Using hooks
|
|
|
|
order: 70
|
2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
For each hook, your plugin should provide a method that takes the relevant data
|
|
|
|
as its first argument. If data was passed when the hook was loaded, you will receive
|
|
|
|
that as the second object.
|
|
|
|
|
|
|
|
Remember that:
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
- The `insertText` hook will receive a locale and string and you should return a string.
|
|
|
|
- All other hooks receive an object. You don't need to return anything, but rather modify the object you receive.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
Let's look at an example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
import myStyle from './style';
|
|
|
|
import myDefs from './defs';
|
|
|
|
import {name, version} from '../package.json';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name,
|
|
|
|
version,
|
|
|
|
hooks: {
|
|
|
|
preRender: function(svg) {
|
|
|
|
if (svg.attributes.get("freesewing:plugin-"+name) === false) {
|
|
|
|
svg.style += myStyle;
|
|
|
|
svg.defs += myDefs;
|
|
|
|
svg.attributes.add("freesewing:plugin-"+name, version);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
insertText: function(text) {
|
|
|
|
return text.toUpperCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This is a complete plugin, ready to be published on NPM. It uses two hooks:
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
- `preRender` : We add some style and defs to our SVG
|
|
|
|
- `insertText` : We transfer all text to UPPERCASE
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
<Note>
|
|
|
|
|
|
|
|
###### Note that we avoid running our hook twice
|
|
|
|
|
|
|
|
As you can see, the last thing we do in the `preRender` hook is set an attribute on
|
|
|
|
the SVG tag with the name and version of our plugin.
|
|
|
|
|
|
|
|
We check for this attribute when the `preRender` hook runs, thereby avoiding that
|
|
|
|
our styles and defs will be added twice.
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
It is good practice to wrap you hook methods in a call like this, because you have
|
2021-08-25 16:09:31 +02:00
|
|
|
no guarantee the user won't render your pattern more than once.
|
|
|
|
|
|
|
|
</Note>
|