From 7294aab0c879ef96c0fde15c389a2c4c1463d3c7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 20 Aug 2020 16:18:16 +0200 Subject: Add ability to set custom field to video form --- client/src/root-helpers/plugins.ts | 41 ++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'client/src/root-helpers') 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 @@ -import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' -import { ClientHookName, ClientScript, RegisterClientHookOptions, ServerConfigPlugin, PluginType, clientHookObject } from '../../../shared/models' import { RegisterClientHelpers } from 'src/types/register-client-option.model' +import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' +import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model' +import { + ClientHookName, + clientHookObject, + ClientScript, + PluginType, + RegisterClientHookOptions, + ServerConfigPlugin +} from '../../../shared/models' import { ClientScript as ClientScriptModule } from '../types/client-script.model' import { importModule } from './utils' @@ -18,6 +26,13 @@ type PluginInfo = { isTheme: boolean } +type FormFields = { + video: { + commonOptions: RegisterClientFormFieldOptions + videoFormOptions: RegisterClientVideoFieldOptions + }[] +} + async function runHook (hooks: Hooks, hookName: ClientHookName, result?: T, params?: any) { if (!hooks[hookName]) return result @@ -34,7 +49,13 @@ async function runHook (hooks: Hooks, hookName: ClientHookName, result?: T, p return result } -function loadPlugin (hooks: Hooks, pluginInfo: PluginInfo, peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers) { +function loadPlugin (options: { + hooks: Hooks + pluginInfo: PluginInfo + peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers + formFields?: FormFields +}) { + const { hooks, pluginInfo, peertubeHelpersFactory, formFields } = options const { plugin, clientScript } = pluginInfo const registerHook = (options: RegisterClientHookOptions) => { @@ -54,12 +75,23 @@ function loadPlugin (hooks: Hooks, pluginInfo: PluginInfo, peertubeHelpersFactor }) } + const registerVideoField = (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => { + if (!formFields) { + throw new Error('Video field registration is not supported') + } + + formFields.video.push({ + commonOptions, + videoFormOptions + }) + } + const peertubeHelpers = peertubeHelpersFactory(pluginInfo) console.log('Loading script %s of plugin %s.', clientScript.script, plugin.name) return importModule(clientScript.script) - .then((script: ClientScriptModule) => script.register({ registerHook, peertubeHelpers })) + .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, peertubeHelpers })) .then(() => sortHooksByPriority(hooks)) .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) } @@ -68,6 +100,7 @@ export { HookStructValue, Hooks, PluginInfo, + FormFields, loadPlugin, runHook } -- cgit v1.2.3