- Build
- Features
Custom Actions
Integrate workflow, automation and other other custom tasks into the visual editor.
Custom actions create the ability to perform site and content tasks in a single location.
Stackbit already handles normalizing and syncing content among any number of content sources. This makes the visual editing environment the perfect candidate for triggering content, workflow, automation, and other tasks for a site.
There are multiple points at which actions can hook into the visual editor and content flow. See below for explanations, use cases, and examples.
Types of Actions
There are four types of actions:
Each action differs in the following ways:
- Trigger location (in the UI)
- Configuration options
- Callback parameters
See below for use cases and further instruction on working with each of these types.
Global Actions
Global actions are performed on the site as a whole. For example:
- Trigger a deploy preview for the current version of a site.
- Send a custom workflow event to reviewers.
- Run a performance test on the entire site.
- Check for broken links throughout the site.
These actions are triggered from the top bar, next to the site name.
Global actions are configured as a property in the main Stackbit configuration object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
See the configuration reference for more information.
Bulk Document Actions
Bulk document actions are performed on a selected set of documents. For example:
- Send a set of pages to a translation service.
Editors can choose the set of documents on which to trigger the action.
Like global actions, bulk actions are configured as a property in the main Stackbit configuration object, specified by the type
property.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
See the configuration reference for more information.
Model Actions
Model actions are performed on an individual document. For example:
- Cloning an object based on input values
- Sending a document to a translation service
- Taking a snapshot of a document in its current state
These actions can be triggered where the document context is presented. When defined, it will always appear near the title in page and content editing modes.
If using inline editing and if a proper data-sb-object-id
annotation has been provided, the triggers will also be available in the toolbar when highlighting the document.
Model actions are configured directly on the model definition. (When using a headless CMS, the model definition is an extension of the schema defined in the source.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
See the reference for more information.
Field Actions
Field actions are performed against a field on a document. For example:
- Generate AI content for a specific field (optionally based on some input).
- Suggest fixing spelling and grammar.
- Fill certain fields with custom data from an external API.
- Translate a field using an external API.
These actions can be triggered wherever the field input is displayed.
If using inline editing and proper data-sb-object-id
and data-sb-field-path
annotations have been provided, the triggers will also be available in the toolbar when highlighting the field.
Field actions are configured as a property on a field within a model definition.
When using a headless CMS, the model definition is an extension of the schema defined in the source. Adding an action on a field that has been defined in an external schema only requires adding the name
to identify the field.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
See the reference for more information.
Accepting Input
Actions can accept input from editors by supplying the inputFields
property with field definitions. These field definitions are identical to Stackbit schema field definitions.
Supported Field Types
The following field types are supported:
boolean
color
date
datetime
enum
html
markdown
number
reference
slug
string
text
url
Using Input Data
The input data is passed to the run
function in an inputData
object, where the key is the name of the field and the value is the user value. Here's a simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
Handling State
The action trigger can be given a state to provide feedback to the user.
The state can be set in the return object from the run
function. Stackbit will also check for updates to the state using the state
property.
Supported State
The following states are supported:
enabled
disabled
hidden
running
State Example
The state function is most useful in long running actions when the state of the action may depend on external factors or maybe the field values of the document itself.
Let's assume the run
function calls a translation API that submits a whole document and requires humans to translate the content.
The whole translation process may take several days. We don't expect the run
function to run for several days. Instead, after calling the translation API, the run function will return immediately and return the proper state.
1
2
3
4
5
6
7
8
This overrides the default Stackbit behavior, which would change the state back to enabled
.
Every time the Studio requests the document with that action, the state
function will check with the translation service if that document is still being translated or is finished, and return a matching state.
1
2
3
4
5
6
7
8
9
10
11
Examples
Here are a few more complete examples to help get started with custom actions.
Generating a Title
This is a model action that uses Faker to generate a random title. This is shared for brevity. A more useful application might send a user prompt to an AI service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Fix Formatting on Field
In this example, we can force a field into a specific format. (Note that you could more strictly enforce this behavior with document hooks.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52