Popover & text analysis
Last updated
Last updated
This tutorial will show you how to build a simple text analysis plugin. You will analyze the text, as it is written, and display the information in a popover from the top bar.
Let's jump ahead and see exactly what it is you will create when following the steps in this tutorial.
For simplicity's sake these instructions assumes that you have followed the quickstart guide first to get a working plugin environment.
If you have followed the instructions in the quickstart guide you should now have a simple plugin displaying some UI components in the sidebar.
Start by removing the DevKitComponent.js
file.
Then remove related code as well as all label handling in DevKitPackage.js
. You should be left with two files in the src
directory: DevKitPackage.js
and index.js
.
The DevKitPackage.js
should, maybe minus some comments, look like below.
In order to create a popover dialog you will need to create a UI component and register it with the configurator in the DevKitPackage.js
.
Create a basic component, TextanalyzerComponent.js
, in the src
directory and add the following code.
As you can see this is a basic component, inherits from Component, with a render function that outputs a header and paragraph. In order to have this component rendered in a popover you will use the addPopover()
method.
Import the component in the package file.
Then use this component in a popover. It will use the Font Awesome icon fa-info
aligned to the right. In addition it will be sticky, meaning that it won't automatically be closed when it looses focus. The first parameter should be the name of the plugin.
The complete DevKitPackage.js
file now looks like below. And this is actually all you need to do to have a popover rendered in the Writer.
If you reload it, a popover i icon will be visible somewhere to the right in top bar. If clicked you will see the rendered component.
In order to analyze the text as the user writes you need to listen to the DOCUMENT_CHANGED
event. When adding event listeners in either the didMount()
lifecycle method or the constructor()
it is also always a good idea to unregister event listeners in the component lifecycle method dispose()
.
Import the Writer api in the top of the TextanalyzerComponent.js file.
Then add a constructor and dispose method to the class that register and unregister events respectively. As you will see in many api methods the first parameter is the name of the plugin.
So every time the document is changed calculateText()
is called. Now use the api method api.document.getDocumentNodes()
to fetch all document elements of the article to count the words and characters.
When done you can extend the components state using this.extendState({})
.
So nothing really happens until it shows up in the popover of course. Go back to the render function you created earlier. Render an empty popover if the data has not been collected.
It doesn't look very nice. And unless you write something nothing will be displayed at all. To fix that you need to add a call the calculateText()
as soon as the component has mounted, in the didMount()
lifecycle method, and also add some css styling.
First add the didMount()
method to your component below the constructor
.
Then create a scss file (which if you use the DevKit is already there) with some nice styling. Create (or edit) index.scss
in the scss
directory.
Import the style file in the top and add the class to the wrapper div element in the render function.
The css file will not be loaded automatically. You need to change the plugin configuration and add it. Bring up the Configuration test tool using the keyboard shortcut cmd+shift+y
or ctrl+shift+y
depending on whether you are using OS X or Windows. Then add the css file that should be loaded. The DevKit plugin is configured to create a style.css
in the dist directory.
You should now have a simple, but fully working, text analysis plugin that lives in the top bar. Exactly as the preview in the beginning of the tutorial.
To wrap it up, here are all the relevant files and their content.
As you haven't renamed the package file this file has not been altered, but here it is for clarity's sake.