From 3c47fa3bc0e3f2362bb17976057287a7e9aba46b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 9 Apr 2021 13:50:31 +0200 Subject: Add ability to hide plugin settings --- .../plugin-show-installed.component.ts | 18 +++++++++++------- client/src/app/core/plugins/plugin.service.ts | 7 +++++++ client/src/root-helpers/plugins.ts | 16 +++++++++++++--- client/src/standalone/videos/embed.ts | 1 + client/src/types/register-client-option.model.ts | 12 +++++++++--- 5 files changed, 41 insertions(+), 13 deletions(-) (limited to 'client') diff --git a/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts b/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts index 2c5dbea95..ca9ad9922 100644 --- a/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts +++ b/client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts @@ -19,6 +19,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit pluginTypeLabel: string private sub: Subscription + private npmName: string constructor ( protected formValidatorService: FormValidatorService, @@ -33,9 +34,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit ngOnInit () { this.sub = this.route.params.subscribe( routeParams => { - const npmName = routeParams['npmName'] + this.npmName = routeParams['npmName'] - this.loadPlugin(npmName) + this.loadPlugin(this.npmName) } ) } @@ -62,7 +63,11 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit } isSettingHidden (setting: RegisterServerSettingOptions) { - return false + const script = this.pluginService.getRegisteredSettingsScript(this.npmName) + + if (!script?.isSettingHidden) return false + + return script.isSettingHidden({ setting, formValues: this.form.value }) } private loadPlugin (npmName: string) { @@ -74,6 +79,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit .subscribe( async ({ plugin, registeredSettings }) => { this.plugin = plugin + this.registeredSettings = await this.translateSettings(registeredSettings) this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type) @@ -110,11 +116,9 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit } private async translateSettings (settings: RegisterServerSettingOptions[]) { - const npmName = this.pluginService.nameToNpmName(this.plugin.name, this.plugin.type) - for (const setting of settings) { for (const key of [ 'label', 'html', 'descriptionHTML' ]) { - if (setting[key]) setting[key] = await this.pluginService.translateBy(npmName, setting[key]) + if (setting[key]) setting[key] = await this.pluginService.translateBy(this.npmName, setting[key]) } if (Array.isArray(setting.options)) { @@ -123,7 +127,7 @@ export class PluginShowInstalledComponent extends FormReactive implements OnInit for (const o of setting.options) { newOptions.push({ value: o.value, - label: await this.pluginService.translateBy(npmName, o.label) + label: await this.pluginService.translateBy(this.npmName, o.label) }) } diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index c2dcf9fef..1243bac67 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts @@ -19,6 +19,7 @@ import { PluginTranslation, PluginType, PublicServerSetting, + RegisterClientSettingsScript, ServerConfigPlugin } from '@shared/models' import { environment } from '../../../environments/environment' @@ -58,6 +59,7 @@ export class PluginService implements ClientHook { private formFields: FormFields = { video: [] } + private settingsScripts: { [ npmName: string ]: RegisterClientSettingsScript } = {} constructor ( private authService: AuthService, @@ -200,6 +202,10 @@ export class PluginService implements ClientHook { return this.formFields.video.filter(f => f.videoFormOptions.type === type) } + getRegisteredSettingsScript (npmName: string) { + return this.settingsScripts[npmName] + } + translateBy (npmName: string, toTranslate: string) { const helpers = this.helpers[npmName] if (!helpers) { @@ -220,6 +226,7 @@ export class PluginService implements ClientHook { return loadPlugin({ hooks: this.hooks, formFields: this.formFields, + onSettingsScripts: options => this.settingsScripts[npmName] = options, pluginInfo, peertubeHelpersFactory: () => helpers }) diff --git a/client/src/root-helpers/plugins.ts b/client/src/root-helpers/plugins.ts index 4bc2c8eb2..5344c0468 100644 --- a/client/src/root-helpers/plugins.ts +++ b/client/src/root-helpers/plugins.ts @@ -7,7 +7,8 @@ import { ClientScript, PluginType, RegisterClientHookOptions, - ServerConfigPlugin + ServerConfigPlugin, + RegisterClientSettingsScript } from '../../../shared/models' import { ClientScript as ClientScriptModule } from '../types/client-script.model' import { importModule } from './utils' @@ -54,8 +55,9 @@ function loadPlugin (options: { pluginInfo: PluginInfo peertubeHelpersFactory: (pluginInfo: PluginInfo) => RegisterClientHelpers formFields?: FormFields + onSettingsScripts?: (options: RegisterClientSettingsScript) => void }) { - const { hooks, pluginInfo, peertubeHelpersFactory, formFields } = options + const { hooks, pluginInfo, peertubeHelpersFactory, formFields, onSettingsScripts } = options const { plugin, clientScript } = pluginInfo const registerHook = (options: RegisterClientHookOptions) => { @@ -86,12 +88,20 @@ function loadPlugin (options: { }) } + const registerSettingsScript = (options: RegisterClientSettingsScript) => { + if (!onSettingsScripts) { + throw new Error('Registering settings script is not supported') + } + + return onSettingsScripts(options) + } + 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, registerVideoField, peertubeHelpers })) + .then((script: ClientScriptModule) => script.register({ registerHook, registerVideoField, registerSettingsScript, peertubeHelpers })) .then(() => sortHooksByPriority(hooks)) .catch(err => console.error('Cannot import or register plugin %s.', pluginInfo.plugin.name, err)) } diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index ae8f176b7..103014bb0 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -759,6 +759,7 @@ export class PeerTubeEmbed { await loadPlugin({ hooks: this.peertubeHooks, pluginInfo, + onSettingsScripts: () => undefined, peertubeHelpersFactory: _ => this.buildPeerTubeHelpers(translations) }) } diff --git a/client/src/types/register-client-option.model.ts b/client/src/types/register-client-option.model.ts index 7e5356a2b..16c921344 100644 --- a/client/src/types/register-client-option.model.ts +++ b/client/src/types/register-client-option.model.ts @@ -1,12 +1,18 @@ -import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model' -import { RegisterClientHookOptions } from '@shared/models/plugins/register-client-hook.model' -import { ServerConfig } from '@shared/models/server' +import { + RegisterClientFormFieldOptions, + RegisterClientHookOptions, + RegisterClientSettingsScript, + RegisterClientVideoFieldOptions, + ServerConfig +} from '@shared/models' export type RegisterClientOptions = { registerHook: (options: RegisterClientHookOptions) => void registerVideoField: (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) => void + registerSettingsScript: (options: RegisterClientSettingsScript) => void + peertubeHelpers: RegisterClientHelpers } -- cgit v1.2.3