|
| 1 | +# Plugin development |
| 2 | + |
| 3 | +This document describes the guides to develop your own plugin for the RedisInsight Workbench. |
| 4 | + |
| 5 | +## How it works |
| 6 | + |
| 7 | +Plugin visualization in the Workbench is rendered using Iframe to encapsulate plugin scripts and styles, described in |
| 8 | +the main plugin script and the stylesheet (if it has been specified in the `package.json`), |
| 9 | +iframe includes basic styles as well. |
| 10 | + |
| 11 | +## Plugin structure |
| 12 | + |
| 13 | +Each plugin should have a unique name with all its files [loaded](installation.md) to |
| 14 | +a separate folder inside the default `plugins` folder. |
| 15 | + |
| 16 | +> Default plugins are located inside the application. |
| 17 | +
|
| 18 | +### Files |
| 19 | +`package.json` should be located in the root folder of your plugins, all other files can be included into a subfolder. |
| 20 | + |
| 21 | +* **pluginName/package.json** *(required)* - Manifest of the plugin |
| 22 | +* **pluginName/{anyName}.js** *(required)* - Core script of the plugin |
| 23 | +* **pluginName/{anyName}.css** *(optional)* - File with styles for the plugin visualizations |
| 24 | +* **pluginName/{anyFileOrFolder}** *(optional)* - Specify any other file or folder inside the plugin folder |
| 25 | +to use by the core module script. *For example*: pluginName/images/image.png. |
| 26 | + |
| 27 | +## `package.json` structure |
| 28 | + |
| 29 | +This is the required manifest to use the plugin. `package.json` file should include |
| 30 | +the following **required** fields: |
| 31 | + |
| 32 | +<table> |
| 33 | + <tr> |
| 34 | + <td><i>name</i></td> |
| 35 | + <td>Plugin name. It is recommended to use the folder name as the plugin name in the package.json.</td> |
| 36 | + </tr> |
| 37 | + <tr> |
| 38 | + <td><i>main</i></td> |
| 39 | + <td>Relative path to the core script of the plugin. <i>Example: </i> "./dist/index.js"</td> |
| 40 | + </tr> |
| 41 | + <tr> |
| 42 | + <td><i>visualizations</i></td> |
| 43 | + <td> |
| 44 | + Array of visualizations (objects) to visualize the results in the Workbench. |
| 45 | + <br><br> |
| 46 | + Required fields in visualizations: |
| 47 | + <ul> |
| 48 | + <li><strong><i>id</i></strong> - visualization id</li> |
| 49 | + <li><strong><i>name</i></strong> - visualization name to display in the Workbench</li> |
| 50 | + <li><strong><i>activationMethod</i></strong> - name of the exported function to call when |
| 51 | +this visualization is selected in the Workbench</li> |
| 52 | + <li> |
| 53 | + <strong><i>matchCommands</i></strong> - array of commands to use the visualization for. Supports regex string. |
| 54 | + <i>Example: </i> ["CLIENT LIST", "FT.*"] |
| 55 | + </li> |
| 56 | + </ul> |
| 57 | + </td> |
| 58 | + </tr> |
| 59 | +</table> |
| 60 | + |
| 61 | +You can specify the path to a css file in the `styles` field. If specified, |
| 62 | +this file will be included inside the iframe plugin. |
| 63 | + |
| 64 | +Simple example of the `package.json` file with required and optional fields: |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "author": { |
| 69 | + "name": "Redis Ltd.", |
| 70 | + |
| 71 | + "url": "https://redis.com/redis-enterprise/redis-insight" |
| 72 | + }, |
| 73 | + "description": "Show client list as table", |
| 74 | + "styles": "./dist/styles.css", |
| 75 | + "main": "./dist/index.js", |
| 76 | + "name": "client-list", |
| 77 | + "version": "0.0.1", |
| 78 | + "scripts": {}, |
| 79 | + "visualizations": [ |
| 80 | + { |
| 81 | + "id": "clients-list", |
| 82 | + "name": "Table", |
| 83 | + "activationMethod": "renderClientsList", |
| 84 | + "matchCommands": [ |
| 85 | + "CLIENT LIST" |
| 86 | + ], |
| 87 | + "description": "Example of client list plugin", |
| 88 | + "default": true |
| 89 | + } |
| 90 | + ], |
| 91 | + "devDependencies": {}, |
| 92 | + "dependencies": {} |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Core script of the plugin |
| 97 | + |
| 98 | +This is the required script with defined visualization methods. |
| 99 | +The core script contains function and its export (functions - for multiple visualizations), |
| 100 | +which is run after the relevant visualization is selected in the Workbench. |
| 101 | + |
| 102 | +The following function receives props of the executed commands: |
| 103 | +```typescript |
| 104 | +interface Props { |
| 105 | + command: string; // executed command |
| 106 | + data: string; // result of the executed command |
| 107 | + status: 'success' | 'fail'; // response status of the executed command |
| 108 | +} |
| 109 | + |
| 110 | +const renderVisualization = (props: Props) => { |
| 111 | + // Do your magic |
| 112 | +} |
| 113 | + |
| 114 | +export default { renderVisualization } |
| 115 | +``` |
| 116 | + |
| 117 | +Each plugin iframe has basic styles of RedisInsight application, including fonts and color schemes. |
| 118 | + |
| 119 | +It is recommended to use the React & [Elastic UI library](https://elastic.github.io/eui/#/) for |
| 120 | +consistency with plugin visualisations and the entire application. |
| 121 | + |
| 122 | +Find the example of the plugin here. |
| 123 | + |
| 124 | +* [Client List Plugin README](../../redisinsight/ui/src/packages/clients-list-example/README.md) |
| 125 | +* [Client List Plugin dir](../../redisinsight/ui/src/packages/clients-list-example/) |
| 126 | + |
| 127 | +### Available parameters |
| 128 | + |
| 129 | +Additional information provided to the plugin iframe is included in the `window.state` |
| 130 | +inside of the plugin script. |
| 131 | + |
| 132 | +```javascript |
| 133 | +const { config, modules } = window.state |
| 134 | +const { baseUrl } = config |
| 135 | + |
| 136 | +// modules - the list of modules of the current database |
| 137 | +// baseUrl - url for your plugin folder - can be used to include your assets |
| 138 | +``` |
| 139 | + |
| 140 | +### Plugin rendering |
| 141 | +To render the plugin visualization, the iframe with basic html is generated which is |
| 142 | +then populated with relevant scripts and styles. To render the html data, use existing |
| 143 | +DOM Element `#app` or create your own DOM Elements. |
| 144 | +Rendered iframe also includes `theme_DARK` or `theme_LIGHT` className on `body` to indicate the application theme used. |
| 145 | + |
| 146 | +_Javascript Example:_ |
| 147 | +```javascript |
| 148 | +const renderVisualization = (props) => { |
| 149 | + const { command, data } = props; |
| 150 | + document.getElementById('app') |
| 151 | + .innerHTML = ` |
| 152 | + <h3>Executed command:<h3> |
| 153 | + <p>${command}</p> |
| 154 | + <h4>Result of the command</h4> |
| 155 | + <p>${data}</p> |
| 156 | + ` |
| 157 | +} |
| 158 | + |
| 159 | +export default { renderVisualization } |
| 160 | +``` |
| 161 | + |
| 162 | +_React Example:_ |
| 163 | +```javascript |
| 164 | +import { render } from 'react-dom' |
| 165 | +import App from './App' |
| 166 | + |
| 167 | +const renderVisualization = (props) => { |
| 168 | + const { command, status, data = '' } = props |
| 169 | + render( |
| 170 | + <App command={command} response={data} status={status} />, |
| 171 | + document.getElementById('app') |
| 172 | + ) |
| 173 | +} |
| 174 | + |
| 175 | +// This is a required action - export the main function for execution of the visualization |
| 176 | +export default { renderVisualization } |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | +## Plugins communication |
| 181 | +> **_Future updates:_** |
| 182 | +Support of communication with the main application via a third-party library - _redisinsight-plugin-sdk_. |
0 commit comments