1
0
Fork 0

chore(markdown): Updated plugin guide for v3

This commit is contained in:
Joost De Cock 2022-10-12 21:52:47 +02:00
parent d7442b9bc4
commit cac698027c
15 changed files with 195 additions and 348 deletions

View file

@ -1,20 +1,53 @@
---
title: Hooks
order: 60
title: Lifecycle hook methods
order: 110
---
A **hook** is a lifecycle event. The available hooks are:
FreeSewing plugins can provide hooks, which is a way to hook into the pattern's
lifecycle.
- [preRender](/reference/hooks/prerender/): Called at the start of [`Pattern.render()`](/reference/api/pattern/render)
- [postRender](/reference/hooks/postrender/): Called at the end of [`Pattern.render()`](/reference/api/pattern/render)
- [insertText](/reference/hooks/inserttext/): Called when inserting text
- [preDraft](/reference/hooks/predraft/): Called at the start of [`Pattern.draft()`](/reference/api/pattern/draft)
- [postDraft](/reference/hooks/postdraft/): Called at the end of [`Pattern.draft()`](/reference/api/pattern/draft)
- [preSample](/reference/hooks/presample/): Called at the start of [`Pattern.sample()`](/reference/api/pattern/sample)
- [postSample](/reference/hooks/postsample/): Called at the end of [`Pattern.sample()`](/reference/api/pattern/sample)
## Signature
You can register a method for a hook. When the hook is triggered, your method will be
called. It will receive two parameters:
To provide one or more hooks, your plugin should have a `hooks` property that
is an object where the keys are the lifecycle hook name, and the value holds a
method. When the lifecycle hook is triggered, your method will be called.
- An object relevant to the hook. See the [hooks API reference](/reference/hooks/) for details.
```mjs
const myPlugin = {
name: 'example',
version: '0.0.1',
hooks: {
hookName: function (obj, data = {}) {
}
}
}
```
If you want to attach multiple methods to the same lifecycle hook, you can pass
them as an array:
```mjs
const myPlugin = {
name: 'example',
version: '0.0.1',
hooks: {
hookName: [
function one (obj, data = {}) { },
function two (obj, data = {}) { }
]
}
}
```
## Arguments
All lifecycle methods will receive two parameters:
- An object relevant to the lifecycle hook. See the [hooks API reference](/reference/hooks/) for details.
- Data passed when the hook was registered (optional)
## Notes
Refer to the [hooks API reference](/reference/hooks/) for a list of all
available lifecycle hooks.