diff options
author | Chocobozzz <me@florianbigard.com> | 2020-08-20 16:18:16 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-21 15:39:51 +0200 |
commit | 7294aab0c879ef96c0fde15c389a2c4c1463d3c7 (patch) | |
tree | bad1176720ee04266eba5470e9956e3a7871e349 /client/src/root-helpers/plugins.ts | |
parent | f95628636b6ccdf3eae2449ca718e075b072f678 (diff) | |
download | PeerTube-7294aab0c879ef96c0fde15c389a2c4c1463d3c7.tar.gz PeerTube-7294aab0c879ef96c0fde15c389a2c4c1463d3c7.tar.zst PeerTube-7294aab0c879ef96c0fde15c389a2c4c1463d3c7.zip |
Add ability to set custom field to video form
Diffstat (limited to 'client/src/root-helpers/plugins.ts')
-rw-r--r-- | client/src/root-helpers/plugins.ts | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/client/src/root-helpers/plugins.ts b/client/src/root-helpers/plugins.ts index 011721761..4bc2c8eb2 100644 --- a/client/src/root-helpers/plugins.ts +++ b/client/src/root-helpers/plugins.ts | |||
@@ -1,6 +1,14 @@ | |||
1 | import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' | ||
2 | import { ClientHookName, ClientScript, RegisterClientHookOptions, ServerConfigPlugin, PluginType, clientHookObject } from '../../../shared/models' | ||
3 | import { RegisterClientHelpers } from 'src/types/register-client-option.model' | 1 | import { RegisterClientHelpers } from 'src/types/register-client-option.model' |
2 | import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' | ||
3 | import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model' | ||
4 | import { | ||
5 | ClientHookName, | ||
6 | clientHookObject, | ||
7 | ClientScript, | ||
8 | PluginType, | ||
9 | RegisterClientHookOptions, | ||
10 | ServerConfigPlugin | ||
11 | } from '../../../shared/models' | ||
4 | import { ClientScript as ClientScriptModule } from '../types/client-script.model' | 12 | import { ClientScript as ClientScriptModule } from '../types/client-script.model' |
5 | import { importModule } from './utils' | 13 | import { importModule } from './utils' |
6 | 14 | ||
@@ -18,6 +26,13 @@ type PluginInfo = { | |||
18 | isTheme: boolean | 26 | isTheme: boolean |
19 | } | 27 | } |
20 | 28 | ||
29 | type FormFields = { | ||
30 | video: { | ||
31 | commonOptions: RegisterClientFormFieldOptions | ||
32 | videoFormOptions: RegisterClientVideoFieldOptions | ||
33 | }[] | ||
34 | } | ||
35 | |||
21 | async function runHook<T> (hooks: Hooks, hookName: ClientHookName, result?: T, params?: any) { | 36 | async function runHook<T> (hooks: Hooks, hookName: ClientHookName, result?: T, params?: any) { |
22 | if (!hooks[hookName]) return result | 37 | if (!hooks[hookName]) return result |
23 | 38 | ||
@@ -34,7 +49,13 @@ async function runHook<T> (hooks: Hooks, hookName: ClientHookName, result?: T, p | |||
34 | return result | 49 | return result |
35 | } | 50 | } |
36 | 51 | ||
37 | function loadPlugin (hooks: Hooks, pluginInfo: PluginInfo, peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers) { | 52 | function loadPlugin (options: { |
53 | hooks: Hooks | ||
54 | pluginInfo: PluginInfo | ||
55 | peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers | ||
56 | formFields?: FormFields | ||
57 | }) { | ||
58 | const { hooks, pluginInfo, peertubeHelpersFactory, formFields } = options | ||
38 | const { plugin, clientScript } = pluginInfo | 59 | const { plugin, clientScript } = pluginInfo |
39 | 60 | ||
40 | const registerHook = (options: RegisterClientHookOptions) => { | 61 | const registerHook = (options: RegisterClientHookOptions) => { |
@@ -54,12 +75,23 @@ function loadPlugin (hooks: Hooks, pluginInfo: PluginInfo, peertubeHelpersFactor | |||
54 | }) | 75 | }) |
55 | } | 76 | } |
56 | 77 | ||
78 | const registerVideoField = (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => { | ||
79 | if (!formFields) { | ||
80 | throw new Error('Video field registration is not supported') | ||
81 | } | ||
82 | |||
83 | formFields.video.push({ | ||
84 | commonOptions, | ||
85 | videoFormOptions | ||
86 | }) | ||
87 | } | ||
88 | |||
57 | const peertubeHelpers = peertubeHelpersFactory(pluginInfo) | 89 | const peertubeHelpers = peertubeHelpersFactory(pluginInfo) |
58 | 90 | ||
59 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) | 91 | console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) |
60 | 92 | ||
61 | return importModule(clientScript.script) | 93 | return importModule(clientScript.script) |
62 | .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) | 94 | .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, peertubeHelpers })) |
63 | .then(() => sortHooksByPriority(hooks)) | 95 | .then(() => sortHooksByPriority(hooks)) |
64 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) | 96 | .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) |
65 | } | 97 | } |
@@ -68,6 +100,7 @@ export { | |||
68 | HookStructValue, | 100 | HookStructValue, |
69 | Hooks, | 101 | Hooks, |
70 | PluginInfo, | 102 | PluginInfo, |
103 | FormFields, | ||
71 | loadPlugin, | 104 | loadPlugin, |
72 | runHook | 105 | runHook |
73 | } | 106 | } |