forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.js
464 lines (400 loc) · 16.3 KB
/
registry.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import reducerRegistry from 'mattermost-redux/store/reducer_registry';
import {
registerPluginWebSocketEvent,
unregisterPluginWebSocketEvent,
registerPluginReconnectHandler,
unregisterPluginReconnectHandler,
} from 'actions/websocket_actions.jsx';
import {showRHSPlugin} from 'actions/views/rhs';
import {
registerPluginTranslationsSource,
} from 'actions/views/root';
import store from 'stores/redux_store.jsx';
import {ActionTypes} from 'utils/constants.jsx';
import {generateId} from 'utils/utils.jsx';
function dispatchPluginComponentAction(name, pluginId, component, id = generateId()) {
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name,
data: {
id,
pluginId,
component,
},
});
return id;
}
const resolveReactElement = (element) => {
if (typeof element === 'function') {
// Allow element to be passed as the name of the component, instead of a React element.
return React.createElement(element);
}
return element;
};
export default class PluginRegistry {
constructor(id) {
this.id = id;
}
// Register a component at the root of the channel view of the app.
// Accepts a React component. Returns a unique identifier.
registerRootComponent(component) {
return dispatchPluginComponentAction('Root', this.id, component);
}
// Register a component in the user attributes section of the profile popover (hovercard), below the default user attributes.
// Accepts a React component. Returns a unique identifier.
registerPopoverUserAttributesComponent(component) {
return dispatchPluginComponentAction('PopoverUserAttributes', this.id, component);
}
// Register a component in the user actions of the profile popover (hovercard), below the default actions.
// Accepts a React component. Returns a unique identifier.
registerPopoverUserActionsComponent(component) {
return dispatchPluginComponentAction('PopoverUserActions', this.id, component);
}
// Register a component fixed to the top of the left-hand channel sidebar.
// Accepts a React component. Returns a unique identifier.
registerLeftSidebarHeaderComponent(component) {
return dispatchPluginComponentAction('LeftSidebarHeader', this.id, component);
}
// Register a component fixed to the bottom of the team sidebar. Does not render if
// user is only on one team and the team sidebar is not shown.
// Accepts a React component. Returns a unique identifier.
registerBottomTeamSidebarComponent(component) {
return dispatchPluginComponentAction('BottomTeamSidebar', this.id, component);
}
// Register a component fixed to the bottom of the post message.
// Accepts a React component. Returns a unique identifier.
registerPostMessageAttachmentComponent(component) {
return dispatchPluginComponentAction('PostMessageAttachment', this.id, component);
}
// Register a component to show as a tooltip when a user hovers on a link in a post.
// Accepts a React component. Returns a unique identifier.
registerLinkTooltipComponent(component) {
return dispatchPluginComponentAction('LinkTooltip', this.id, component);
}
// Add a button to the channel header. If there are more than one buttons registered by any
// plugin, a dropdown menu is created to contain all the plugin buttons.
// Accepts the following:
// - icon - React element to use as the button's icon
// - action - a function called when the button is clicked, passed the channel and channel member as arguments
// - dropdown_text - string or React element shown for the dropdown button description
// - tooltip_text - string shown for tooltip appear on hover
registerChannelHeaderButtonAction(icon, action, dropdownText, tooltipText) {
const id = generateId();
const data = {
id,
pluginId: this.id,
icon: resolveReactElement(icon),
action,
dropdownText: resolveReactElement(dropdownText),
tooltipText,
};
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'ChannelHeaderButton',
data,
});
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'MobileChannelHeaderButton',
data,
});
return id;
}
// Register a component to render a custom body for posts with a specific type.
// Custom post types must be prefixed with 'custom_'.
// Custom post types can also apply for ephemeral posts.
// Accepts a string type and a component.
// Returns a unique identifier.
registerPostTypeComponent(type, component) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_POST_COMPONENT,
data: {
id,
pluginId: this.id,
type,
component,
},
});
return id;
}
// Register a component to render a custom body for post cards with a specific type.
// Custom post types must be prefixed with 'custom_'.
// Accepts a string type and a component.
// Returns a unique identifier.
registerPostCardTypeComponent(type, component) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_POST_CARD_COMPONENT,
data: {
id,
pluginId: this.id,
type,
component,
},
});
return id;
}
// Register a main menu list item by providing some text and an action function.
// Accepts the following:
// - text - A string or React element to display in the menu
// - action - A function to trigger when component is clicked on
// - mobileIcon - A React element to display as the icon in the menu in mobile view
// Returns a unique identifier.
registerMainMenuAction(text, action, mobileIcon) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'MainMenu',
data: {
id,
pluginId: this.id,
text: resolveReactElement(text),
action,
mobileIcon: resolveReactElement(mobileIcon),
},
});
return id;
}
// Register a post menu list item by providing some text and an action function.
// Accepts the following:
// - text - A string or React element to display in the menu
// - action - A function to trigger when component is clicked on
// - filter - A function whether to apply the plugin into the post' dropdown menu
// Returns a unique identifier.
registerPostDropdownMenuAction(text, action, filter) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'PostDropdownMenu',
data: {
id,
pluginId: this.id,
text: resolveReactElement(text),
action,
filter,
},
});
return id;
}
// Register a component at the bottom of the post dropdown menu.
// Accepts a React component. Returns a unique identifier.
registerPostDropdownMenuComponent(component) {
return dispatchPluginComponentAction('PostDropdownMenuItem', this.id, component);
}
// Register a file upload method by providing some text, an icon, and an action function.
// Accepts the following:
// - icon - JSX element to use as the button's icon
// - text - A string or JSX element to display in the file upload menu
// - action - A function to trigger when the menu item is selected.
// Returns a unique identifier.
registerFileUploadMethod(icon, action, text) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'FileUploadMethod',
data: {
id,
pluginId: this.id,
text,
action,
icon,
},
});
return id;
}
// Register a hook to intercept file uploads before they take place.
// Accepts a function to run before files get uploaded. Receives an array of
// files and a function to upload files at a later time as arguments. Must
// return an object that can contain two properties:
// - message - An error message to display, leave blank or null to display no message
// - files - Modified array of files to upload, set to null to reject all files
// Returns a unique identifier.
registerFilesWillUploadHook(hook) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'FilesWillUploadHook',
data: {
id,
pluginId: this.id,
hook,
},
});
return id;
}
// Unregister a component, action or hook using the unique identifier returned after registration.
// Accepts a string id.
// Returns undefined in all cases.
unregisterComponent(componentId) {
store.dispatch({
type: ActionTypes.REMOVED_PLUGIN_COMPONENT,
id: componentId,
});
}
// Unregister a component that provided a custom body for posts with a specific type.
// Accepts a string id.
// Returns undefined in all cases.
unregisterPostTypeComponent(componentId) {
store.dispatch({
type: ActionTypes.REMOVED_PLUGIN_POST_COMPONENT,
id: componentId,
});
}
// Register a reducer against the Redux store. It will be accessible in redux state
// under "state['plugins-<yourpluginid>']"
// Accepts a reducer. Returns undefined.
registerReducer(reducer) {
reducerRegistry.register('plugins-' + this.id, reducer);
}
// Register a handler for WebSocket events.
// Accepts the following:
// - event - the event type, can be a regular server event or an event from plugins.
// Plugin events will have "custom_<pluginid>_" prepended
// - handler - a function to handle the event, receives the event message as an argument
// Returns undefined.
registerWebSocketEventHandler(event, handler) {
registerPluginWebSocketEvent(this.id, event, handler);
}
// Unregister a handler for a custom WebSocket event.
// Accepts a string event type.
// Returns undefined.
unregisterWebSocketEventHandler(event) {
unregisterPluginWebSocketEvent(this.id, event);
}
// Register a handler that will be called when the app reconnects to the
// internet after previously disconnecting.
// Accepts a function to handle the event. Returns undefined.
registerReconnectHandler(handler) {
registerPluginReconnectHandler(this.id, handler);
}
// Unregister a previously registered reconnect handler.
// Returns undefined.
unregisterReconnectHandler() {
unregisterPluginReconnectHandler(this.id);
}
// Register a hook that will be called when a message is posted by the user before it
// is sent to the server. Accepts a function that receives the post as an argument.
//
// To reject a post, return an object containing an error such as
// {error: {message: 'Rejected'}}
// To modify or allow the post without modifcation, return an object containing the post
// such as
// {post: {...}}
//
// If the hook function is asynchronous, the message will not be sent to the server
// until the hook returns.
registerMessageWillBePostedHook(hook) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'MessageWillBePosted',
data: {
id,
pluginId: this.id,
hook,
},
});
return id;
}
// Register a hook that will be called when a slash command is posted by the user before it
// is sent to the server. Accepts a function that receives the message (string) and the args
// (object) as arguments.
// The args object is:
// {
// channel_id: channelId,
// team_id: teamId,
// root_id: rootId,
// parent_id: rootId,
// }
//
// To reject a command, return an object containing an error:
// {error: {message: 'Rejected'}}
// To ignore a command, return an empty object (to prevent an error from being displayed):
// {}
// To modify or allow the command without modification, return an object containing the new message
// and args. It is not likely that you will need to change the args, so return the object that was provided:
// {message: {...}, args}
//
// If the hook function is asynchronous, the command will not be sent to the server
// until the hook returns.
registerSlashCommandWillBePostedHook(hook) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'SlashCommandWillBePosted',
data: {
id,
pluginId: this.id,
hook,
},
});
return id;
}
// Register a hook that will be called before a message is formatted into Markdown.
// Accepts a function that receives the unmodified post and the message (potentially
// already modified by other hooks) as arguments. This function must return a string
// message that will be formatted.
// Returns a unique identifier.
registerMessageWillFormatHook(hook) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'MessageWillFormat',
data: {
id,
pluginId: this.id,
hook,
},
});
return id;
}
// Register a component to override file previews. Accepts a function to run before file is
// previewed and a react component to be rendered as the file preview.
// - override - A function to check whether preview needs to be overridden. Receives fileInfo and post as arguments.
// Returns true is preview should be overridden and false otherwise.
// - component - A react component to display instead of original preview. Receives fileInfo and post as props.
// Returns a unique identifier.
// Only one plugin can override a file preview at a time. If two plugins try to override the same file preview, the first plugin will perform the override and the second will not. Plugin precedence is ordered alphabetically by plugin ID.
registerFilePreviewComponent(override, component) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'FilePreview',
data: {
id,
pluginId: this.id,
override,
component,
},
});
return id;
}
registerTranslations(getTranslationsForLocale) {
registerPluginTranslationsSource(this.id, getTranslationsForLocale);
}
// Register a Right-Hand Sidebar component by providing a title for the right hand component.
// Accepts the following:
// - title - A string or JSX element to display as a title for the RHS.
// - component - A react component to display in the Right-Hand Sidebar.
// Returns:
// - id: a unique identifier
// - showRHSPlugin: the action to dispatch that will open the RHS.
registerRightHandSidebarComponent(component, title) {
const id = generateId();
store.dispatch({
type: ActionTypes.RECEIVED_PLUGIN_COMPONENT,
name: 'RightHandSidebarComponent',
data: {
id,
pluginId: this.id,
component,
title,
},
});
return {id, showRHSPlugin: showRHSPlugin(id)};
}
}