ActivityPub-federated video streaming platform using P2P directly in your web browser
feat(colorpicker): add optional input field to enter css color code (#7337)
Gianantonio Pinib939020236ca
diff --git a/client/src/app/+admin/config/pages/admin-config-customization.component.html b/client/src/app/+admin/config/pages/admin-config-customization.component.htmlindex 7c2fc6eb5c..1eb4d46aaf 100644--- a/client/src/app/+admin/config/pages/admin-config-customization.component.html+++ b/client/src/app/+admin/config/pages/admin-config-customization.component.html@@ -91,7 +91,11 @@}@if (field.type === 'color') {- <p-colorpicker class="d-block" [inputId]="field.inputId" [formControlName]="field.name" />+ <my-peertube-color-picker+ [inputId]="field.inputId"+ [formControlName]="field.name"+ [formError]="formErrors.theme.customization[field.name]"+ />} @else if (field.type === 'radius') {<my-select-custom-value[labelId]="field.inputId"diff --git a/client/src/app/+admin/config/pages/admin-config-customization.component.ts b/client/src/app/+admin/config/pages/admin-config-customization.component.tsindex 0d5d9b4cdb..ff8f5dc566 100644--- a/client/src/app/+admin/config/pages/admin-config-customization.component.ts+++ b/client/src/app/+admin/config/pages/admin-config-customization.component.ts@@ -3,9 +3,11 @@ import { Component, inject, OnDestroy, OnInit } from '@angular/core'import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, ValueChangeEvent } from '@angular/forms'import { ActivatedRoute, RouterModule } from '@angular/router'import { CanComponentDeactivate, ServerService, ThemeService } from '@app/core'+import { HEX_COLOR_CODE_VALIDATOR } from '@app/shared/form-validators/common-validators'import { BuildFormArgumentTyped, FormDefaultTyped, FormReactiveMessagesTyped } from '@app/shared/form-validators/form-validator.model'import { FormReactiveErrorsTyped, FormReactiveService } from '@app/shared/shared-forms/form-reactive.service'import { PeertubeCheckboxComponent } from '@app/shared/shared-forms/peertube-checkbox.component'+import { PeertubeColorPickerComponent } from '@app/shared/shared-forms/peertube-color-picker.component'import { SelectCustomValueComponent } from '@app/shared/shared-forms/select/select-custom-value.component'import { SelectOptionsComponent } from '@app/shared/shared-forms/select/select-options.component'import { objectKeysTyped } from '@peertube/peertube-core-utils'@@ -13,7 +15,6 @@ import { CustomConfig, PlayerTheme } from '@peertube/peertube-models'import { capitalizeFirstLetter } from '@root-helpers/string'import { ColorPaletteThemeConfig, ThemeCustomizationKey } from '@root-helpers/theme-manager'import debug from 'debug'-import { ColorPickerModule } from 'primeng/colorpicker'import { debounceTime, Subscription } from 'rxjs'import { SelectOptionsItem } from 'src/types'import { AdminConfigService } from '../../../shared/shared-admin/admin-config.service'@@ -86,7 +87,7 @@ type FieldType = 'color' | 'radius'RouterModule,ReactiveFormsModule,AdminSaveBarComponent,- ColorPickerModule,+ PeertubeColorPickerComponent,AlertComponent,SelectOptionsComponent,HelpComponent,@@ -186,6 +187,7 @@ export class AdminConfigCustomizationComponent implements OnInit, OnDestroy, Canthis.customConfig = customConfigthis.form.patchValue(this.getDefaultFormValues(), { emitEvent: false })+ this.form.setErrors(this.form.errors)})}@@ -268,15 +270,15 @@ export class AdminConfigCustomizationComponent implements OnInit, OnDestroy, Cantheme: {default: null,customization: {- primaryColor: null,- foregroundColor: null,- backgroundColor: null,- backgroundSecondaryColor: null,- menuForegroundColor: null,- menuBackgroundColor: null,+ primaryColor: HEX_COLOR_CODE_VALIDATOR,+ foregroundColor: HEX_COLOR_CODE_VALIDATOR,+ backgroundColor: HEX_COLOR_CODE_VALIDATOR,+ backgroundSecondaryColor: HEX_COLOR_CODE_VALIDATOR,+ menuForegroundColor: HEX_COLOR_CODE_VALIDATOR,+ menuBackgroundColor: HEX_COLOR_CODE_VALIDATOR,menuBorderRadius: null,- headerForegroundColor: null,- headerBackgroundColor: null,+ headerForegroundColor: HEX_COLOR_CODE_VALIDATOR,+ headerBackgroundColor: HEX_COLOR_CODE_VALIDATOR,inputBorderRadius: null}},@@ -332,6 +334,7 @@ export class AdminConfigCustomizationComponent implements OnInit, OnDestroy, Cancontrol.patchValue(value, { emitEvent: false })control.markAsDirty()+ control.setErrors(control.errors)}save () {diff --git a/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.html b/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.htmlindex 5a7d0c9e9e..a62adf0729 100644--- a/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.html+++ b/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.html@@ -47,7 +47,7 @@<div class="form-group"><label i18n for="primaryColor">Primary color</label>- <p-colorpicker class="d-block" inputId="primaryColor" formControlName="primaryColor" />+ <my-peertube-color-picker inputId="primaryColor" formControlName="primaryColor" [formError]="formErrors.primaryColor" /></div><div class="buttons">diff --git a/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.ts b/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.tsindex 964aec6021..dee1085f8e 100644--- a/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.ts+++ b/client/src/app/modal/admin-config-wizard/steps/admin-config-wizard-edit-info.component.ts@@ -3,6 +3,7 @@ import { CommonModule } from '@angular/common'import { booleanAttribute, Component, inject, input, numberAttribute, OnInit, output } from '@angular/core'import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms'import { ServerService, ThemeService } from '@app/core'+import { HEX_COLOR_CODE_VALIDATOR } from '@app/shared/form-validators/common-validators'import { INSTANCE_NAME_VALIDATOR, INSTANCE_SHORT_DESCRIPTION_VALIDATOR } from '@app/shared/form-validators/custom-config-validators'import {BuildFormArgumentTyped,@@ -11,8 +12,8 @@ import {FormReactiveMessagesTyped} from '@app/shared/form-validators/form-validator.model'import { FormReactiveService } from '@app/shared/shared-forms/form-reactive.service'+import { PeertubeColorPickerComponent } from '@app/shared/shared-forms/peertube-color-picker.component'import { PreviewUploadComponent } from '@app/shared/shared-forms/preview-upload.component'-import { ColorPickerModule } from 'primeng/colorpicker'import { ButtonComponent } from '../../../shared/shared-main/buttons/button.component'import { findAppropriateImage } from '@peertube/peertube-core-utils'import { logger } from '@root-helpers/logger'@@ -30,7 +31,15 @@ export type FormEditInfo = FormDefaultTyped<Form>selector: 'my-admin-config-wizard-edit-info',templateUrl: './admin-config-wizard-edit-info.component.html',styleUrls: [ './admin-config-wizard-edit-info.component.scss', '../shared/admin-config-wizard-modal-common.scss' ],- imports: [ CommonModule, FormsModule, ReactiveFormsModule, ColorPickerModule, CdkStepperModule, ButtonComponent, PreviewUploadComponent ]+ imports: [+ CommonModule,+ FormsModule,+ ReactiveFormsModule,+ PeertubeColorPickerComponent,+ CdkStepperModule,+ ButtonComponent,+ PreviewUploadComponent+ ]})export class AdminConfigWizardEditInfoComponent implements OnInit {private server = inject(ServerService)@@ -68,7 +77,7 @@ export class AdminConfigWizardEditInfoComponent implements OnInit {platformName: INSTANCE_NAME_VALIDATOR,shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,avatar: null,- primaryColor: null+ primaryColor: HEX_COLOR_CODE_VALIDATOR}const {diff --git a/client/src/app/shared/form-validators/common-validators.ts b/client/src/app/shared/form-validators/common-validators.tsindex 69b482788c..d9ac9b45b3 100644--- a/client/src/app/shared/form-validators/common-validators.ts+++ b/client/src/app/shared/form-validators/common-validators.ts@@ -14,3 +14,11 @@ export const URL_VALIDATOR: BuildFormValidator = {pattern: $localize`This field must be a URL`}}++export const HEX_COLOR_CODE_VALIDATOR: BuildFormValidator = {+ VALIDATORS: [ Validators.required, Validators.pattern(/^#[A-Fa-f0-9]{6}$/) ],+ MESSAGES: {+ required: $localize`This field is required.`,+ pattern: $localize`This field must be a valid 6-digit hexadecimal color code.`+ }+}diff --git a/client/src/app/shared/shared-forms/peertube-color-picker.component.html b/client/src/app/shared/shared-forms/peertube-color-picker.component.htmlnew file mode 100644index 0000000000..f8373b7547--- /dev/null+++ b/client/src/app/shared/shared-forms/peertube-color-picker.component.html@@ -0,0 +1,24 @@+<div class="d-flex align-items-center gap-4">+ <p-colorpicker+ class="d-block"+ [inputId]="inputId()"+ [(ngModel)]="value"+ (ngModelChange)="onValueChange()"+ />+ <div class="d-flex align-items-center gap-1">+ {{ numberSign }}+ <input+ [id]="hexCodeInputId"+ [name]="hexCodeInputId"+ [(ngModel)]="hexCode"+ (ngModelChange)="onHexCodeChange()"+ type="text"+ class="form-control"+ [ngClass]="{ 'input-error': formError() }"+ />+ </div>+</div>++@if (formError()) {+ <div class="form-error" role="alert">{{ formError() }}</div>+}diff --git a/client/src/app/shared/shared-forms/peertube-color-picker.component.scss b/client/src/app/shared/shared-forms/peertube-color-picker.component.scssnew file mode 100644index 0000000000..10689dd7c7--- /dev/null+++ b/client/src/app/shared/shared-forms/peertube-color-picker.component.scss@@ -0,0 +1,5 @@+@use "_form-mixins" as *;++input[type=text] {+ @include peertube-input-text(100px);+}diff --git a/client/src/app/shared/shared-forms/peertube-color-picker.component.ts b/client/src/app/shared/shared-forms/peertube-color-picker.component.tsnew file mode 100644index 0000000000..94e89f9cf9--- /dev/null+++ b/client/src/app/shared/shared-forms/peertube-color-picker.component.ts@@ -0,0 +1,66 @@+import { NgClass } from '@angular/common'+import { Component, forwardRef, input, model } from '@angular/core'+import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms'+import { ColorPickerModule } from 'primeng/colorpicker'++@Component({+ selector: 'my-peertube-color-picker',+ templateUrl: './peertube-color-picker.component.html',+ styleUrls: [ './peertube-color-picker.component.scss' ],+ providers: [+ {+ provide: NG_VALUE_ACCESSOR,+ useExisting: forwardRef(() => PeertubeColorPickerComponent),+ multi: true+ }+ ],+ imports: [ FormsModule, NgClass, ColorPickerModule ]+})+export class PeertubeColorPickerComponent implements ControlValueAccessor {+ readonly inputId = input.required<string>()++ readonly formError = input<string>()++ readonly value = model('')+ readonly numberSign = '#'+ hexCode: string++ get hexCodeInputId () {+ return `${this.inputId()}HexCode`+ }++ propagateChange = (_: any) => {+ // empty+ }++ writeValue (value: string) {+ this.value.set(value)+ this.updateHexCodeOnValueChange()+ }++ registerOnChange (fn: (_: any) => void) {+ this.propagateChange = fn+ }++ registerOnTouched () {+ // Unused+ }++ onValueChange () {+ this.updateHexCodeOnValueChange()+ this.propagateChange(this.value())+ }++ onHexCodeChange(){+ this.updateValueOnHexCodeChange()+ this.propagateChange(this.value())+ }++ updateHexCodeOnValueChange(){+ this.hexCode = this.value().substring(1)+ }++ updateValueOnHexCodeChange(){+ this.value.set(`${this.numberSign}${this.hexCode}`)+ }+}