Build a custom plugin
This doc demonstrates how to create a custom plugin that adds a word counter to Builder's Visual Editor. Follow the instructions in this doc to learn how to create your own custom plugin.
Tip: Learn more about creating your own plugins in Builder Academy's Extending Builder with Plugins course.
Prerequisites
This doc assumes you have followed the instructions in Custom plugin setup to create a local directory with the boilerplate code needed to create a custom plugin.
End result
Create a toolbar plugin that, when clicked, provides a count of words within the selected Builder block.
Step 1: register a component
Begin by registering a component. The Builder.register() method accepts two arguments:
- A string, representing the plugin type.
- An object, providing configuration details for the plugin, including the component to render.
The following code includes the plugin type 'editor.toolbarButton', which adds a button to the toolbar at the top of the Visual Editor.
For the second argument, an object is passed in with a single key, component. The value for this key is a function that returns a ToolbarButton component.
This code should be added to the src/plugin.jsx file.
Next, create the component. In the code block below, a ToolbarButton component is created that launches an alert when clicked.
Refresh your Visual Editor to see your toolbar button. If you do not see a button, review your code or return to Custom plugin setup.
The video below demonstrates what to expect at this point in the process.
Step 2: implement component feature
To implement the word count feature, make the following adjustments to the ToolbarButton component:
- Use the
builderobject attached to the browser'swindowobject to access currently selected elements. - Select the first element of selected elements.
- Access the
innerTextof that element. - Count the number of words within the text.
Refresh the Visual Editor, select a containing block that contains text, and press your custom plugin button. You should see a change to the alert message.
Note: Your word count value may say 0. This is addressed in the next step.
Step 3: improve your plugin
At this point, you have implemented a custom plugin. You can now continue to improve the implementation of your plugin.
Consider styling your plugin to fit Builder's branding or your own. Additionally, continue to test your plugin's implementation to solve bugs and edge cases.
For example, the current implementation of this plugin assumes the user will select a wrapping block that contains text. A more robust implementation will be needed to select wrapping containers, text blocks, and more.
See a more complete plugin.jsx file below.