mirror of
https://github.com/projectsend/projectsend.git
synced 2026-12-06 18:59:38 +00:00
Add a second logo for the dark theme
A logo made for a light background can vanish on a dark one. The Branding screen now takes an optional second logo, shown instead of the main one to anyone using the dark theme. It uploads, crops, restores and removes like the main logo, on its own routes. - LogoVariant names the two logos and their three columns each (shown image, cropped-from upload, crop box). BrandingSetting, LogoCropper and BrandingController take a variant instead of hard-coding the main logo's columns. The dark routes pass it as a route default, so it never comes from the request. - The dark logo only stands in for the main one. Uploading it needs a main logo first, removing the main logo removes both, and logoUrl(Dark) answers null without a main logo, in case a restored row breaks that rule. - The shared `branding` prop gains logo_dark_url, gated like logo_url. BrandLogo renders both images and lets the `.dark` class pick, so the right one is there on the first paint. Every layout that showed the logo uses it. - The Gallery layouts are always dark and used to turn the main logo white. With a dark logo they show it as it is. - Emails keep the main logo. API: GET /api/v1/modules/branding/logo gains logo_dark_url. Additive. Checked in headless Chrome on the dev instance: the sign-in page and the sidebar switch between the two logos with the theme. Reported by @riegnilam (#1845)
This commit is contained in:
@@ -71,6 +71,9 @@ class BrandingServiceProvider extends ServiceProvider
|
||||
|
||||
return [
|
||||
'logo_url' => BrandingSetting::shownLogoUrl(),
|
||||
// Shown instead of the main logo in the dark theme; null
|
||||
// when there is none, and the main logo serves both.
|
||||
'logo_dark_url' => BrandingSetting::shownLogoUrl(LogoVariant::Dark),
|
||||
'show_site_name' => $setting->show_site_name ?? false,
|
||||
];
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace App\Modules\Platform\Branding\Http\Controllers\Api;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Routing\Controller;
|
||||
use App\Modules\Platform\Branding\LogoVariant;
|
||||
use App\Modules\Platform\Branding\Models\BrandingSetting;
|
||||
|
||||
/**
|
||||
@@ -31,8 +32,10 @@ class BrandingController extends Controller
|
||||
* Get this installation's logo.
|
||||
*
|
||||
* Returns a null `logo_url` when no logo has been uploaded, which is
|
||||
* the normal state rather than an error. `show_site_name` says whether
|
||||
* the sign-in and download pages print the site name under the logo.
|
||||
* the normal state rather than an error. `logo_dark_url` is the logo
|
||||
* shown instead to people using the dark theme, or null when the main
|
||||
* logo is used in both. `show_site_name` says whether the sign-in and
|
||||
* download pages print the site name under the logo.
|
||||
*/
|
||||
public function show(): JsonResponse
|
||||
{
|
||||
@@ -41,6 +44,7 @@ class BrandingController extends Controller
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'logo_url' => $setting?->logoUrl(),
|
||||
'logo_dark_url' => $setting?->logoUrl(LogoVariant::Dark),
|
||||
'show_site_name' => $setting->show_site_name ?? false,
|
||||
'updated_at' => $setting?->updated_at?->toIso8601String(),
|
||||
],
|
||||
|
||||
@@ -13,9 +13,11 @@ use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use App\Modules\Platform\Branding\LogoCropper;
|
||||
use App\Modules\Platform\Branding\LogoVariant;
|
||||
use App\Modules\Platform\Branding\Models\BrandingSetting;
|
||||
use App\Modules\Platform\Branding\Watermark\WatermarkPosition;
|
||||
use App\Modules\Platform\Branding\Watermark\WatermarkSample;
|
||||
@@ -48,6 +50,13 @@ class BrandingController extends Controller
|
||||
'logo_source_url' => $setting->logoSourceUrl(),
|
||||
'logo_crop' => $setting->logo_crop,
|
||||
'logo_cropped' => $setting->logoIsCropped(),
|
||||
// The same four answers for the logo shown in the dark theme.
|
||||
'logo_dark' => [
|
||||
'url' => $setting->logoUrl(LogoVariant::Dark),
|
||||
'source_url' => $setting->logoUrl(LogoVariant::Dark) === null ? null : $setting->logoSourceUrl(LogoVariant::Dark),
|
||||
'crop' => $setting->logo_dark_crop,
|
||||
'cropped' => $setting->logoIsCropped(LogoVariant::Dark),
|
||||
],
|
||||
// Read, never written here. Hiding attribution is the
|
||||
// white-label half and stays a hosted feature: the switch is
|
||||
// rendered only where Capability::AttributionHide is held, and
|
||||
@@ -67,24 +76,39 @@ class BrandingController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request, LogoCropper $cropper): RedirectResponse
|
||||
/**
|
||||
* Either logo, by the route's `variant` default: the main one, or the
|
||||
* one shown in the dark theme.
|
||||
*/
|
||||
public function store(Request $request, LogoCropper $cropper, string $variant = 'light'): RedirectResponse
|
||||
{
|
||||
$variant = LogoVariant::from($variant);
|
||||
$field = $variant->field();
|
||||
|
||||
$validated = $request->validate([
|
||||
'logo' => ['required', 'image', 'max:2048'],
|
||||
$field => ['required', 'image', 'max:2048'],
|
||||
]);
|
||||
|
||||
/** @var UploadedFile $upload */
|
||||
$upload = $validated['logo'];
|
||||
$upload = $validated[$field];
|
||||
|
||||
$setting = BrandingSetting::current();
|
||||
$previous = $cropper->files($setting);
|
||||
|
||||
// The dark-mode logo stands in for the main one and is never shown
|
||||
// without it (BrandingSetting::logoUrl()), so storing one first
|
||||
// would leave a file nobody sees.
|
||||
if ($variant === LogoVariant::Dark && $setting->logo_path === null) {
|
||||
throw ValidationException::withMessages([$field => __('Upload the main logo first.')]);
|
||||
}
|
||||
|
||||
$previous = $cropper->files($setting, $variant);
|
||||
|
||||
// A new upload starts uncropped: the old crop's box describes a
|
||||
// different picture.
|
||||
$setting->update([
|
||||
'logo_path' => $this->storeImage($upload),
|
||||
'logo_original_path' => null,
|
||||
'logo_crop' => null,
|
||||
$variant->pathColumn() => $this->storeImage($upload),
|
||||
$variant->originalColumn() => null,
|
||||
$variant->cropColumn() => null,
|
||||
]);
|
||||
|
||||
Storage::disk('public')->delete($previous);
|
||||
@@ -96,8 +120,10 @@ class BrandingController extends Controller
|
||||
* Cut the logo down to a box drawn on the uploaded image. Optional: an
|
||||
* uploaded logo is used whole until somebody crops it.
|
||||
*/
|
||||
public function cropLogo(Request $request, LogoCropper $cropper): RedirectResponse
|
||||
public function cropLogo(Request $request, LogoCropper $cropper, string $variant = 'light'): RedirectResponse
|
||||
{
|
||||
$variant = LogoVariant::from($variant);
|
||||
|
||||
$validated = $request->validate([
|
||||
'x' => ['required', 'integer', 'min:0'],
|
||||
'y' => ['required', 'integer', 'min:0'],
|
||||
@@ -108,7 +134,7 @@ class BrandingController extends Controller
|
||||
$setting = BrandingSetting::query()->first();
|
||||
|
||||
if ($setting === null) {
|
||||
return back()->withErrors(['logo' => __('Upload a logo before cropping it.')]);
|
||||
return back()->withErrors([$variant->field() => __('Upload a logo before cropping it.')]);
|
||||
}
|
||||
|
||||
$cropper->crop($setting, [
|
||||
@@ -116,7 +142,7 @@ class BrandingController extends Controller
|
||||
'y' => (int) $validated['y'],
|
||||
'width' => (int) $validated['width'],
|
||||
'height' => (int) $validated['height'],
|
||||
]);
|
||||
], $variant);
|
||||
|
||||
return back()->with('success', __('Logo cropped.'));
|
||||
}
|
||||
@@ -124,12 +150,12 @@ class BrandingController extends Controller
|
||||
/**
|
||||
* Go back to the logo exactly as it was uploaded.
|
||||
*/
|
||||
public function restoreLogo(LogoCropper $cropper): RedirectResponse
|
||||
public function restoreLogo(LogoCropper $cropper, string $variant = 'light'): RedirectResponse
|
||||
{
|
||||
$setting = BrandingSetting::query()->first();
|
||||
|
||||
if ($setting !== null) {
|
||||
$cropper->restore($setting);
|
||||
$cropper->restore($setting, LogoVariant::from($variant));
|
||||
}
|
||||
|
||||
return back()->with('success', __('Original logo restored.'));
|
||||
@@ -152,13 +178,31 @@ class BrandingController extends Controller
|
||||
return back();
|
||||
}
|
||||
|
||||
public function destroy(LogoCropper $cropper): RedirectResponse
|
||||
/**
|
||||
* Removing the main logo removes the dark-mode one with it: that one
|
||||
* only ever stands in for the main logo, and left behind it would be a
|
||||
* file nobody sees, waiting to reappear beside the next upload.
|
||||
*/
|
||||
public function destroy(LogoCropper $cropper, string $variant = 'light'): RedirectResponse
|
||||
{
|
||||
$variant = LogoVariant::from($variant);
|
||||
$setting = BrandingSetting::query()->first();
|
||||
|
||||
if ($setting !== null && $setting->logo_path !== null) {
|
||||
$files = $cropper->files($setting);
|
||||
$setting->update(['logo_path' => null, 'logo_original_path' => null, 'logo_crop' => null]);
|
||||
if ($setting === null) {
|
||||
return back()->with('success', __('Logo removed.'));
|
||||
}
|
||||
|
||||
$variants = $variant === LogoVariant::Light ? [LogoVariant::Light, LogoVariant::Dark] : [LogoVariant::Dark];
|
||||
$files = [];
|
||||
$attributes = [];
|
||||
|
||||
foreach ($variants as $each) {
|
||||
$files = [...$files, ...$cropper->files($setting, $each)];
|
||||
$attributes += [$each->pathColumn() => null, $each->originalColumn() => null, $each->cropColumn() => null];
|
||||
}
|
||||
|
||||
if ($files !== []) {
|
||||
$setting->update($attributes);
|
||||
Storage::disk('public')->delete($files);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,8 @@ use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
/**
|
||||
* Cutting the logo down to part of itself, and putting the whole of it back.
|
||||
* Cutting a logo down to part of itself, and putting the whole of it back.
|
||||
* Either logo: the main one or the dark-mode one (LogoVariant).
|
||||
*
|
||||
* The uploaded file is never changed. A crop is a new file written from it,
|
||||
* so cropping again starts from everything that was uploaded rather than
|
||||
@@ -35,26 +36,27 @@ class LogoCropper
|
||||
/**
|
||||
* @param array{x: int, y: int, width: int, height: int} $box
|
||||
*/
|
||||
public function crop(BrandingSetting $setting, array $box): void
|
||||
public function crop(BrandingSetting $setting, array $box, LogoVariant $variant = LogoVariant::Light): void
|
||||
{
|
||||
$source = $setting->logoSourcePath();
|
||||
$source = $setting->logoSourcePath($variant);
|
||||
$disk = Storage::disk('public');
|
||||
$field = $variant->field();
|
||||
|
||||
if ($source === null || ! $disk->exists($source)) {
|
||||
throw ValidationException::withMessages(['logo' => __('Upload a logo before cropping it.')]);
|
||||
throw ValidationException::withMessages([$field => __('Upload a logo before cropping it.')]);
|
||||
}
|
||||
|
||||
$absolute = $disk->path($source);
|
||||
$size = @getimagesize($absolute);
|
||||
|
||||
if ($size === false) {
|
||||
throw ValidationException::withMessages(['logo' => __('This logo cannot be cropped. Upload it again.')]);
|
||||
throw ValidationException::withMessages([$field => __('This logo cannot be cropped. Upload it again.')]);
|
||||
}
|
||||
|
||||
[$width, $height] = $size;
|
||||
|
||||
if ($width * $height > self::MAX_PIXELS) {
|
||||
throw ValidationException::withMessages(['logo' => __('This image is too large to crop. Upload a smaller one.')]);
|
||||
throw ValidationException::withMessages([$field => __('This image is too large to crop. Upload a smaller one.')]);
|
||||
}
|
||||
|
||||
if ($box['x'] + $box['width'] > $width || $box['y'] + $box['height'] > $height) {
|
||||
@@ -63,7 +65,7 @@ class LogoCropper
|
||||
|
||||
// The whole picture is the upload itself: no second copy of it.
|
||||
if ($box['x'] === 0 && $box['y'] === 0 && $box['width'] === $width && $box['height'] === $height) {
|
||||
$this->restore($setting);
|
||||
$this->restore($setting, $variant);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -76,12 +78,12 @@ class LogoCropper
|
||||
->crop($box['x'], $box['y'], $box['x'] + $box['width'], $box['y'] + $box['height'])
|
||||
->toFile($disk->path($cropped), $size['mime']);
|
||||
|
||||
$previous = $setting->logoIsCropped() ? $setting->logo_path : null;
|
||||
$previous = $setting->logoIsCropped($variant) ? $setting->logoPath($variant) : null;
|
||||
|
||||
$setting->update([
|
||||
'logo_original_path' => $source,
|
||||
'logo_path' => $cropped,
|
||||
'logo_crop' => $box,
|
||||
$variant->originalColumn() => $source,
|
||||
$variant->pathColumn() => $cropped,
|
||||
$variant->cropColumn() => $box,
|
||||
]);
|
||||
|
||||
if ($previous !== null) {
|
||||
@@ -89,18 +91,18 @@ class LogoCropper
|
||||
}
|
||||
}
|
||||
|
||||
public function restore(BrandingSetting $setting): void
|
||||
public function restore(BrandingSetting $setting, LogoVariant $variant = LogoVariant::Light): void
|
||||
{
|
||||
if (! $setting->logoIsCropped()) {
|
||||
if (! $setting->logoIsCropped($variant)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cropped = $setting->logo_path;
|
||||
$cropped = $setting->logoPath($variant);
|
||||
|
||||
$setting->update([
|
||||
'logo_path' => $setting->logo_original_path,
|
||||
'logo_original_path' => null,
|
||||
'logo_crop' => null,
|
||||
$variant->pathColumn() => $setting->logoSourcePath($variant),
|
||||
$variant->originalColumn() => null,
|
||||
$variant->cropColumn() => null,
|
||||
]);
|
||||
|
||||
if ($cropped !== null) {
|
||||
@@ -113,8 +115,11 @@ class LogoCropper
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function files(BrandingSetting $setting): array
|
||||
public function files(BrandingSetting $setting, LogoVariant $variant = LogoVariant::Light): array
|
||||
{
|
||||
return array_values(array_filter([$setting->logo_path, $setting->logo_original_path]));
|
||||
return array_values(array_filter([
|
||||
$setting->logoPath($variant),
|
||||
$setting->getAttribute($variant->originalColumn()),
|
||||
], fn (mixed $path): bool => is_string($path) && $path !== ''));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Platform\Branding;
|
||||
|
||||
/**
|
||||
* Which of the two logos: the main one, shown everywhere, or the one shown
|
||||
* instead of it to anyone using the dark theme (#1845).
|
||||
*
|
||||
* Each has the same three columns — the image shown, the upload it was
|
||||
* cropped from, the crop box — so uploading, cropping, restoring and
|
||||
* removing are one piece of code that is told which set to use.
|
||||
*/
|
||||
enum LogoVariant: string
|
||||
{
|
||||
case Light = 'light';
|
||||
case Dark = 'dark';
|
||||
|
||||
public function pathColumn(): string
|
||||
{
|
||||
return $this === self::Light ? 'logo_path' : 'logo_dark_path';
|
||||
}
|
||||
|
||||
public function originalColumn(): string
|
||||
{
|
||||
return $this === self::Light ? 'logo_original_path' : 'logo_dark_original_path';
|
||||
}
|
||||
|
||||
public function cropColumn(): string
|
||||
{
|
||||
return $this === self::Light ? 'logo_crop' : 'logo_dark_crop';
|
||||
}
|
||||
|
||||
/** The form field the upload arrives in, and where its errors go. */
|
||||
public function field(): string
|
||||
{
|
||||
return $this === self::Light ? 'logo' : 'logo_dark';
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Modules\Platform\Branding\Models;
|
||||
use App\Modules\Platform\Capabilities\Capability;
|
||||
use App\Modules\Platform\Capabilities\CapabilityRegistry;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Modules\Platform\Branding\LogoVariant;
|
||||
use App\Modules\Platform\Branding\Watermark\WatermarkPosition;
|
||||
|
||||
/**
|
||||
@@ -17,6 +18,9 @@ use App\Modules\Platform\Branding\Watermark\WatermarkPosition;
|
||||
* @property string|null $logo_path
|
||||
* @property string|null $logo_original_path
|
||||
* @property array{x: int, y: int, width: int, height: int}|null $logo_crop
|
||||
* @property string|null $logo_dark_path
|
||||
* @property string|null $logo_dark_original_path
|
||||
* @property array{x: int, y: int, width: int, height: int}|null $logo_dark_crop
|
||||
* @property bool $watermark_enabled
|
||||
* @property string|null $watermark_path
|
||||
* @property WatermarkPosition $watermark_position
|
||||
@@ -37,6 +41,7 @@ class BrandingSetting extends Model
|
||||
'watermark_enabled' => 'boolean',
|
||||
'show_site_name' => 'boolean',
|
||||
'logo_crop' => 'array',
|
||||
'logo_dark_crop' => 'array',
|
||||
'watermark_position' => WatermarkPosition::class,
|
||||
'watermark_size' => 'integer',
|
||||
'watermark_opacity' => 'integer',
|
||||
@@ -74,40 +79,67 @@ class BrandingSetting extends Model
|
||||
* Asked per call, so an upload or a plan change applies to the next
|
||||
* page and the next email.
|
||||
*/
|
||||
public static function shownLogoUrl(): ?string
|
||||
public static function shownLogoUrl(LogoVariant $variant = LogoVariant::Light): ?string
|
||||
{
|
||||
if (! app(CapabilityRegistry::class)->has(Capability::Branding)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return static::query()->first()?->logoUrl();
|
||||
return static::query()->first()?->logoUrl($variant);
|
||||
}
|
||||
|
||||
public function logoUrl(): ?string
|
||||
/**
|
||||
* The dark-mode logo only ever stands in for the main one, so it is
|
||||
* not shown without it. Removing the main logo removes both, but a
|
||||
* row restored from a backup may not have kept that promise.
|
||||
*/
|
||||
public function logoUrl(LogoVariant $variant = LogoVariant::Light): ?string
|
||||
{
|
||||
return $this->logo_path === null ? null : self::publicUrl($this->logo_path);
|
||||
$path = $this->logoPath($variant);
|
||||
|
||||
if ($path === null || $this->logo_path === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::publicUrl($path);
|
||||
}
|
||||
|
||||
public function logoPath(LogoVariant $variant = LogoVariant::Light): ?string
|
||||
{
|
||||
/** @var string|null */
|
||||
return $this->getAttribute($variant->pathColumn());
|
||||
}
|
||||
|
||||
/**
|
||||
* The file the logo was cut from: the upload itself when it was never
|
||||
* cropped, since then `logo_path` is that upload. A crop always starts
|
||||
* from here, never from a previous crop.
|
||||
* cropped, since then the shown path is that upload. A crop always
|
||||
* starts from here, never from a previous crop.
|
||||
*/
|
||||
public function logoSourcePath(): ?string
|
||||
public function logoSourcePath(LogoVariant $variant = LogoVariant::Light): ?string
|
||||
{
|
||||
return $this->logo_original_path ?? $this->logo_path;
|
||||
/** @var string|null */
|
||||
return $this->getAttribute($variant->originalColumn()) ?? $this->logoPath($variant);
|
||||
}
|
||||
|
||||
public function logoSourceUrl(): ?string
|
||||
public function logoSourceUrl(LogoVariant $variant = LogoVariant::Light): ?string
|
||||
{
|
||||
$path = $this->logoSourcePath();
|
||||
$path = $this->logoSourcePath($variant);
|
||||
|
||||
return $path === null ? null : self::publicUrl($path);
|
||||
}
|
||||
|
||||
public function logoIsCropped(): bool
|
||||
public function logoIsCropped(LogoVariant $variant = LogoVariant::Light): bool
|
||||
{
|
||||
return $this->logo_original_path !== null;
|
||||
return $this->getAttribute($variant->originalColumn()) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{x: int, y: int, width: int, height: int}|null
|
||||
*/
|
||||
public function logoCrop(LogoVariant $variant = LogoVariant::Light): ?array
|
||||
{
|
||||
/** @var array{x: int, y: int, width: int, height: int}|null */
|
||||
return $this->getAttribute($variant->cropColumn());
|
||||
}
|
||||
|
||||
public function watermarkUrl(): ?string
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* A second logo, shown instead of the first to anyone using the dark theme
|
||||
* (#1845). The same three columns the main logo has: the image shown, the
|
||||
* upload it was cropped from, and the crop box. All null on every existing
|
||||
* installation, which keeps showing its one logo in both themes.
|
||||
*/
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('branding_settings', function (Blueprint $table) {
|
||||
$table->string('logo_dark_path')->nullable()->after('logo_crop');
|
||||
$table->string('logo_dark_original_path')->nullable()->after('logo_dark_path');
|
||||
$table->json('logo_dark_crop')->nullable()->after('logo_dark_original_path');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('branding_settings', function (Blueprint $table) {
|
||||
$table->dropColumn(['logo_dark_path', 'logo_dark_original_path', 'logo_dark_crop']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { SharedData } from '@/types';
|
||||
import { usePage } from '@inertiajs/react';
|
||||
import AppLogoIcon from './app-logo-icon';
|
||||
@@ -8,7 +9,11 @@ export default function AppLogo() {
|
||||
return (
|
||||
<>
|
||||
<div className="flex aspect-square size-8 items-center justify-center">
|
||||
{branding?.logo_url ? <img src={branding.logo_url} alt="" className="size-7 object-contain" /> : <AppLogoIcon className="size-7" />}
|
||||
{branding?.logo_url ? (
|
||||
<BrandLogo src={branding.logo_url} darkSrc={branding.logo_dark_url} alt="" className="size-7 object-contain" />
|
||||
) : (
|
||||
<AppLogoIcon className="size-7" />
|
||||
)}
|
||||
</div>
|
||||
<div className="ml-1 grid flex-1 text-left text-sm">
|
||||
<span className="mb-0.5 truncate leading-none font-semibold">{name}</span>
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface BrandLogoProps {
|
||||
/** The main logo. */
|
||||
src: string;
|
||||
/** The logo for the dark theme, or null when the main one serves both. */
|
||||
darkSrc: string | null;
|
||||
alt: string;
|
||||
className?: string;
|
||||
/**
|
||||
* The surface is always dark, whatever the theme: the Gallery layouts.
|
||||
* The dark-mode logo is made for that, so it is used as it is; without
|
||||
* one, the main logo is turned white so it still shows (#1845).
|
||||
*/
|
||||
darkSurface?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The installation's uploaded logo, the right one for the theme.
|
||||
*
|
||||
* Both images are rendered and CSS picks one, rather than JavaScript
|
||||
* reading the theme: the `.dark` class is set before React mounts, so the
|
||||
* right logo is there on the first paint and follows a theme switch with
|
||||
* no flash.
|
||||
*/
|
||||
export default function BrandLogo({ src, darkSrc, alt, className, darkSurface = false }: BrandLogoProps) {
|
||||
if (darkSurface) {
|
||||
return darkSrc ? (
|
||||
<img src={darkSrc} alt={alt} className={className} />
|
||||
) : (
|
||||
<img src={src} alt={alt} className={cn(className, 'brightness-0 invert')} />
|
||||
);
|
||||
}
|
||||
|
||||
if (!darkSrc) {
|
||||
return <img src={src} alt={alt} className={className} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<img src={src} alt={alt} className={cn(className, 'dark:hidden')} />
|
||||
<img src={darkSrc} alt={alt} className={cn(className, 'hidden dark:block')} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -22,6 +22,10 @@ interface LogoCropDialogProps {
|
||||
sourceUrl: string;
|
||||
/** The last crop, in the upload's pixels, or null to start from the whole image. */
|
||||
savedCrop: LogoCropBox | null;
|
||||
/** Where the box is saved: the main logo's route, or the dark-mode logo's. */
|
||||
cropRoute?: string;
|
||||
/** The form field the server reports this logo's errors under. */
|
||||
errorField?: string;
|
||||
}
|
||||
|
||||
const WHOLE: PercentCrop = { unit: '%', x: 0, y: 0, width: 100, height: 100 };
|
||||
@@ -39,7 +43,14 @@ const WHOLE: PercentCrop = { unit: '%', x: 0, y: 0, width: 100, height: 100 };
|
||||
* orientation tag, the server cannot, and the box would land on the wrong
|
||||
* part of the picture.
|
||||
*/
|
||||
export default function LogoCropDialog({ open, onOpenChange, sourceUrl, savedCrop }: LogoCropDialogProps) {
|
||||
export default function LogoCropDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
sourceUrl,
|
||||
savedCrop,
|
||||
cropRoute = 'branding.logo.crop',
|
||||
errorField = 'logo',
|
||||
}: LogoCropDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [crop, setCrop] = useState<PercentCrop>(WHOLE);
|
||||
const [natural, setNatural] = useState<{ width: number; height: number } | null>(null);
|
||||
@@ -85,12 +96,12 @@ export default function LogoCropDialog({ open, onOpenChange, sourceUrl, savedCro
|
||||
setSaving(true);
|
||||
|
||||
router.patch(
|
||||
route('branding.logo.crop'),
|
||||
route(cropRoute),
|
||||
{ ...toPixels(crop, natural) },
|
||||
{
|
||||
preserveScroll: true,
|
||||
onSuccess: () => onOpenChange(false),
|
||||
onError: (errors) => setError(errors.logo ?? errors.width ?? errors.x ?? Object.values(errors)[0]),
|
||||
onError: (errors) => setError(errors[errorField] ?? errors.width ?? errors.x ?? Object.values(errors)[0]),
|
||||
onFinish: () => setSaving(false),
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { AppearanceSwitcher } from '@/components/appearance-switcher';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { LocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
import ProjectSendLogo from '@/components/projectsend-logo';
|
||||
@@ -41,7 +42,12 @@ export default function AuthSimpleLayout({ children, title, description }: AuthL
|
||||
reported as too small) and a wide one still
|
||||
fits a phone. */}
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="mb-1 h-32 w-auto max-w-80 object-contain" />
|
||||
<BrandLogo
|
||||
src={branding.logo_url}
|
||||
darkSrc={branding.logo_dark_url}
|
||||
alt={name}
|
||||
className="mb-1 h-32 w-auto max-w-80 object-contain"
|
||||
/>
|
||||
) : (
|
||||
<ProjectSendLogo className="text-foreground mb-1 h-12 w-auto" />
|
||||
)}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import AppLogoIcon from '@/components/app-logo-icon';
|
||||
import { AppearanceSwitcher } from '@/components/appearance-switcher';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { IconLocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { NotificationBell } from '@/components/notification-bell';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
@@ -44,11 +45,17 @@ export default function PortalLayoutGallery({ children }: PortalLayoutGalleryPro
|
||||
|
||||
return (
|
||||
<div className="bg-background flex min-h-svh flex-col">
|
||||
<header className="from-violet-700 to-violet-900 flex items-center justify-between bg-gradient-to-r px-4 py-3 md:px-8">
|
||||
<header className="flex items-center justify-between bg-gradient-to-r from-violet-700 to-violet-900 px-4 py-3 md:px-8">
|
||||
<div className="flex items-center gap-6">
|
||||
<Link href={route('home')} className="flex items-center gap-2 font-medium">
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="h-7 w-auto object-contain brightness-0 invert" />
|
||||
<BrandLogo
|
||||
src={branding.logo_url}
|
||||
darkSrc={branding.logo_dark_url}
|
||||
alt={name}
|
||||
className="h-7 w-auto object-contain"
|
||||
darkSurface
|
||||
/>
|
||||
) : (
|
||||
<AppLogoIcon className="h-7 w-auto text-white" />
|
||||
)}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import AppLogoIcon from '@/components/app-logo-icon';
|
||||
import { AppearanceSwitcher } from '@/components/appearance-switcher';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { IconLocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { NotificationBell } from '@/components/notification-bell';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
@@ -39,7 +40,7 @@ export default function PortalLayout({ children }: PortalLayoutProps) {
|
||||
<div className="flex items-center gap-6">
|
||||
<Link href={route('home')} className="flex items-center gap-2 font-medium">
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="size-7 object-contain" />
|
||||
<BrandLogo src={branding.logo_url} darkSrc={branding.logo_dark_url} alt={name} className="size-7 object-contain" />
|
||||
) : (
|
||||
<AppLogoIcon className="text-foreground size-7" />
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AppLogoIcon from '@/components/app-logo-icon';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { LocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
import { type SharedData } from '@/types';
|
||||
@@ -24,7 +25,7 @@ export default function PublicLayoutCompact({ children, title, description }: Pu
|
||||
<header className="flex items-center justify-between border-b px-4 py-2.5 md:px-8">
|
||||
<Link href={route('home')} className="flex items-center gap-2 font-medium">
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="size-6 object-contain" />
|
||||
<BrandLogo src={branding.logo_url} darkSrc={branding.logo_dark_url} alt={name} className="size-6 object-contain" />
|
||||
) : (
|
||||
<AppLogoIcon className="text-foreground size-6" />
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AppLogoIcon from '@/components/app-logo-icon';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { LocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
import { type SharedData } from '@/types';
|
||||
@@ -25,7 +26,7 @@ export default function PublicLayoutDrive({ children, title, description }: Publ
|
||||
<header className="flex items-center justify-between border-b border-neutral-200 px-4 py-3 md:px-8 dark:border-neutral-800">
|
||||
<Link href={route('home')} className="flex items-center gap-2 font-medium">
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="size-7 object-contain" />
|
||||
<BrandLogo src={branding.logo_url} darkSrc={branding.logo_dark_url} alt={name} className="size-7 object-contain" />
|
||||
) : (
|
||||
<AppLogoIcon className="size-7 text-blue-600" />
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AppLogoIcon from '@/components/app-logo-icon';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { LocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
import { type SharedData } from '@/types';
|
||||
@@ -21,10 +22,16 @@ export default function PublicLayoutGallery({ children, title, description }: Pu
|
||||
|
||||
return (
|
||||
<div className="bg-background flex min-h-svh flex-col">
|
||||
<div className="from-violet-700 to-violet-900 bg-gradient-to-br px-6 py-14 text-center text-white md:px-10">
|
||||
<div className="bg-gradient-to-br from-violet-700 to-violet-900 px-6 py-14 text-center text-white md:px-10">
|
||||
<Link href={route('home')} className="inline-flex flex-col items-center gap-3">
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="h-14 w-auto object-contain brightness-0 invert" />
|
||||
<BrandLogo
|
||||
src={branding.logo_url}
|
||||
darkSrc={branding.logo_dark_url}
|
||||
alt={name}
|
||||
className="h-14 w-auto object-contain"
|
||||
darkSurface
|
||||
/>
|
||||
) : (
|
||||
<AppLogoIcon className="h-14 w-auto text-white" />
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AppLogoIcon from '@/components/app-logo-icon';
|
||||
import BrandLogo from '@/components/brand-logo';
|
||||
import { LocaleSwitcher } from '@/components/locale-switcher';
|
||||
import { PoweredBy } from '@/components/powered-by';
|
||||
import { type SharedData } from '@/types';
|
||||
@@ -25,7 +26,7 @@ export default function PublicLayout({ children, title, description }: PublicLay
|
||||
<div className="flex flex-col items-center gap-4">
|
||||
<Link href={route('home')} className="flex flex-col items-center gap-2 font-medium">
|
||||
{branding?.logo_url ? (
|
||||
<img src={branding.logo_url} alt={name} className="h-12 w-auto object-contain" />
|
||||
<BrandLogo src={branding.logo_url} darkSrc={branding.logo_dark_url} alt={name} className="h-12 w-auto object-contain" />
|
||||
) : (
|
||||
<AppLogoIcon className="text-foreground h-12 w-auto" />
|
||||
)}
|
||||
|
||||
@@ -21,11 +21,19 @@ interface Watermark {
|
||||
opacity: number;
|
||||
}
|
||||
|
||||
interface LogoState {
|
||||
url: string | null;
|
||||
source_url: string | null;
|
||||
crop: LogoCropBox | null;
|
||||
cropped: boolean;
|
||||
}
|
||||
|
||||
interface BrandingEditProps {
|
||||
logo_url: string | null;
|
||||
logo_source_url: string | null;
|
||||
logo_crop: LogoCropBox | null;
|
||||
logo_cropped: boolean;
|
||||
logo_dark: LogoState;
|
||||
hide_attribution: boolean;
|
||||
show_site_name: boolean;
|
||||
watermark: Watermark;
|
||||
@@ -34,10 +42,19 @@ interface BrandingEditProps {
|
||||
|
||||
type Tab = 'logo' | 'watermark' | 'attribution';
|
||||
|
||||
export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, logo_cropped, hide_attribution, show_site_name, watermark, watermark_positions }: BrandingEditProps) {
|
||||
export default function BrandingEdit({
|
||||
logo_url,
|
||||
logo_source_url,
|
||||
logo_crop,
|
||||
logo_cropped,
|
||||
logo_dark,
|
||||
hide_attribution,
|
||||
show_site_name,
|
||||
watermark,
|
||||
watermark_positions,
|
||||
}: BrandingEditProps) {
|
||||
const { t } = useTranslation();
|
||||
const { capabilities } = usePage<SharedData>().props;
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const watermarkInputRef = useRef<HTMLInputElement>(null);
|
||||
const [tab, setTab] = useState<Tab>('logo');
|
||||
|
||||
@@ -53,9 +70,6 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
|
||||
const breadcrumbs: BreadcrumbItem[] = [{ title: t('Branding'), href: '/system/settings/branding' }];
|
||||
|
||||
const uploadForm = useForm<{ logo: File | null }>({ logo: null });
|
||||
const removeForm = useForm({});
|
||||
|
||||
const watermarkForm = useForm<{
|
||||
image: File | null;
|
||||
enabled: boolean;
|
||||
@@ -122,31 +136,6 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
'bottom-right': t('Bottom right'),
|
||||
};
|
||||
|
||||
const submit: FormEventHandler = (e) => {
|
||||
e.preventDefault();
|
||||
uploadForm.post(route('branding.store'), {
|
||||
forceFormData: true,
|
||||
preserveScroll: true,
|
||||
preserveState: true,
|
||||
onSuccess: () => {
|
||||
uploadForm.reset();
|
||||
if (fileInputRef.current) fileInputRef.current.value = '';
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const remove = () => {
|
||||
removeForm.delete(route('branding.destroy'), { preserveScroll: true, preserveState: true });
|
||||
};
|
||||
|
||||
// Cropping is optional: an upload is used whole until somebody crops it.
|
||||
const [cropping, setCropping] = useState(false);
|
||||
const restoreForm = useForm({});
|
||||
|
||||
const restore = () => {
|
||||
restoreForm.delete(route('branding.logo.restore'), { preserveScroll: true, preserveState: true });
|
||||
};
|
||||
|
||||
const submitWatermark: FormEventHandler = (e) => {
|
||||
e.preventDefault();
|
||||
watermarkForm.post(route('branding.watermark.update'), {
|
||||
@@ -196,61 +185,36 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
{/* No heading repeating the tab's own label — the tab is
|
||||
the heading. Same shape as the theming screen. */}
|
||||
<section className={`space-y-6 ${tab === 'logo' ? '' : 'hidden'}`}>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t('Show your own logo in the sidebar instead of the default icon.')}
|
||||
</p>
|
||||
<p className="text-muted-foreground text-sm">{t('Show your own logo in the sidebar instead of the default icon.')}</p>
|
||||
|
||||
<div className="space-y-2">
|
||||
{logo_url ? (
|
||||
<img src={logo_url} alt="" className="h-16 max-w-full rounded border object-contain p-2" />
|
||||
) : (
|
||||
<p className="text-muted-foreground text-sm">{t('No custom logo set — the default icon is shown.')}</p>
|
||||
)}
|
||||
</div>
|
||||
<LogoSlot
|
||||
variant="light"
|
||||
logo={{ url: logo_url, source_url: logo_source_url, crop: logo_crop, cropped: logo_cropped }}
|
||||
emptyText={t('No custom logo set — the default icon is shown.')}
|
||||
uploadLabel={t('Upload logo')}
|
||||
removeLabel={t('Remove logo')}
|
||||
/>
|
||||
|
||||
<form onSubmit={submit} className="space-y-2">
|
||||
<Input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={(e) => uploadForm.setData('logo', e.target.files?.[0] ?? null)}
|
||||
/>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t('Shown up to 320 × 128 pixels, so a wide logo and a square one both fit. PNG, JPG, GIF or WebP, up to 2 MB.')}
|
||||
</p>
|
||||
<InputError message={uploadForm.errors.logo} />
|
||||
<Button type="submit" disabled={uploadForm.processing || uploadForm.data.logo === null}>
|
||||
{t('Upload logo')}
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
{logo_url && logo_source_url && (
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Button variant="outline" onClick={() => setCropping(true)}>
|
||||
{t('Crop')}
|
||||
</Button>
|
||||
{logo_cropped && (
|
||||
<Button variant="outline" onClick={restore} disabled={restoreForm.processing}>
|
||||
{t('Restore original')}
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="outline" onClick={remove} disabled={removeForm.processing}>
|
||||
{t('Remove logo')}
|
||||
</Button>
|
||||
{logo_url && (
|
||||
<div className="space-y-4 border-t pt-6">
|
||||
<div className="grid gap-1">
|
||||
<Label>{t('Logo for dark mode')}</Label>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t(
|
||||
'Optional. Shown instead of the logo above to anyone using the dark theme, for a logo that would not show up on a dark background.',
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<LogoSlot
|
||||
variant="dark"
|
||||
logo={logo_dark}
|
||||
emptyText={t('None — the logo above is used in both themes.')}
|
||||
uploadLabel={t('Upload dark mode logo')}
|
||||
removeLabel={t('Remove dark mode logo')}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{logo_source_url && (
|
||||
// Keyed by the upload so a new logo opens on a fresh box.
|
||||
<LogoCropDialog
|
||||
key={logo_source_url}
|
||||
open={cropping}
|
||||
onOpenChange={setCropping}
|
||||
sourceUrl={logo_source_url}
|
||||
savedCrop={logo_crop}
|
||||
/>
|
||||
)}
|
||||
|
||||
<form onSubmit={submitSiteName} className="space-y-4 border-t pt-6">
|
||||
<div className="flex items-start gap-3">
|
||||
<Checkbox
|
||||
@@ -328,9 +292,7 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
</p>
|
||||
</>
|
||||
) : (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t('Upload a watermark image below to see a preview here.')}
|
||||
</p>
|
||||
<p className="text-muted-foreground text-sm">{t('Upload a watermark image below to see a preview here.')}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -338,11 +300,7 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
<Label htmlFor="watermark_image">{t('Watermark image')}</Label>
|
||||
|
||||
{watermark.image_url && (
|
||||
<img
|
||||
src={watermark.image_url}
|
||||
alt=""
|
||||
className="bg-muted h-16 max-w-full rounded border object-contain p-2"
|
||||
/>
|
||||
<img src={watermark.image_url} alt="" className="bg-muted h-16 max-w-full rounded border object-contain p-2" />
|
||||
)}
|
||||
|
||||
<Input
|
||||
@@ -363,10 +321,7 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="watermark_position">{t('Position')}</Label>
|
||||
|
||||
<Select
|
||||
value={watermarkForm.data.position}
|
||||
onValueChange={(value) => watermarkForm.setData('position', value)}
|
||||
>
|
||||
<Select value={watermarkForm.data.position} onValueChange={(value) => watermarkForm.setData('position', value)}>
|
||||
<SelectTrigger id="watermark_position" className="w-full">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
@@ -397,9 +352,7 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
aria-pressed={watermarkForm.data.position === position}
|
||||
onClick={() => watermarkForm.setData('position', position)}
|
||||
className={`rounded-sm transition-colors ${
|
||||
watermarkForm.data.position === position
|
||||
? 'bg-primary'
|
||||
: 'bg-background hover:bg-accent border'
|
||||
watermarkForm.data.position === position ? 'bg-primary' : 'bg-background hover:bg-accent border'
|
||||
}`}
|
||||
/>
|
||||
))}
|
||||
@@ -420,7 +373,9 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
onChange={(e) => watermarkForm.setData('size', Number(e.target.value))}
|
||||
/>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t('The watermark is scaled to fit inside this share of whatever it is drawn on, keeping its proportions — so a thumbnail and a preview look like the same design.')}
|
||||
{t(
|
||||
'The watermark is scaled to fit inside this share of whatever it is drawn on, keeping its proportions — so a thumbnail and a preview look like the same design.',
|
||||
)}
|
||||
</p>
|
||||
<InputError message={watermarkForm.errors.size} />
|
||||
</div>
|
||||
@@ -492,3 +447,106 @@ export default function BrandingEdit({ logo_url, logo_source_url, logo_crop, log
|
||||
</AppLayout>
|
||||
);
|
||||
}
|
||||
|
||||
interface LogoSlotProps {
|
||||
variant: 'light' | 'dark';
|
||||
logo: LogoState;
|
||||
emptyText: string;
|
||||
uploadLabel: string;
|
||||
removeLabel: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload, crop, restore and remove one logo. The main logo and the one
|
||||
* shown in the dark theme work the same way, on their own routes.
|
||||
*/
|
||||
function LogoSlot({ variant, logo, emptyText, uploadLabel, removeLabel }: LogoSlotProps) {
|
||||
const { t } = useTranslation();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const dark = variant === 'dark';
|
||||
const field = dark ? 'logo_dark' : 'logo';
|
||||
const routes = dark
|
||||
? {
|
||||
store: 'branding.logo-dark.store',
|
||||
destroy: 'branding.logo-dark.destroy',
|
||||
crop: 'branding.logo-dark.crop',
|
||||
restore: 'branding.logo-dark.restore',
|
||||
}
|
||||
: { store: 'branding.store', destroy: 'branding.destroy', crop: 'branding.logo.crop', restore: 'branding.logo.restore' };
|
||||
|
||||
const uploadForm = useForm<Record<string, File | null>>({ [field]: null });
|
||||
const removeForm = useForm({});
|
||||
const restoreForm = useForm({});
|
||||
|
||||
// Cropping is optional: an upload is used whole until somebody crops it.
|
||||
const [cropping, setCropping] = useState(false);
|
||||
|
||||
const submit: FormEventHandler = (e) => {
|
||||
e.preventDefault();
|
||||
uploadForm.post(route(routes.store), {
|
||||
forceFormData: true,
|
||||
preserveScroll: true,
|
||||
preserveState: true,
|
||||
onSuccess: () => {
|
||||
uploadForm.reset();
|
||||
if (fileInputRef.current) fileInputRef.current.value = '';
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const remove = () => removeForm.delete(route(routes.destroy), { preserveScroll: true, preserveState: true });
|
||||
const restore = () => restoreForm.delete(route(routes.restore), { preserveScroll: true, preserveState: true });
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="space-y-2">
|
||||
{logo.url ? (
|
||||
// The dark-mode logo is previewed on a dark background, as it will be seen.
|
||||
<img src={logo.url} alt="" className={`h-16 max-w-full rounded border object-contain p-2 ${dark ? 'bg-neutral-900' : ''}`} />
|
||||
) : (
|
||||
<p className="text-muted-foreground text-sm">{emptyText}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<form onSubmit={submit} className="space-y-2">
|
||||
<Input ref={fileInputRef} type="file" accept="image/*" onChange={(e) => uploadForm.setData(field, e.target.files?.[0] ?? null)} />
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t('Shown up to 320 × 128 pixels, so a wide logo and a square one both fit. PNG, JPG, GIF or WebP, up to 2 MB.')}
|
||||
</p>
|
||||
<InputError message={uploadForm.errors[field]} />
|
||||
<Button type="submit" disabled={uploadForm.processing || uploadForm.data[field] === null}>
|
||||
{uploadLabel}
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
{logo.url && logo.source_url && (
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Button variant="outline" onClick={() => setCropping(true)}>
|
||||
{t('Crop')}
|
||||
</Button>
|
||||
{logo.cropped && (
|
||||
<Button variant="outline" onClick={restore} disabled={restoreForm.processing}>
|
||||
{t('Restore original')}
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="outline" onClick={remove} disabled={removeForm.processing}>
|
||||
{removeLabel}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{logo.source_url && (
|
||||
// Keyed by the upload so a new logo opens on a fresh box.
|
||||
<LogoCropDialog
|
||||
key={logo.source_url}
|
||||
open={cropping}
|
||||
onOpenChange={setCropping}
|
||||
sourceUrl={logo.source_url}
|
||||
savedCrop={logo.crop}
|
||||
cropRoute={routes.crop}
|
||||
errorField={field}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -214,7 +214,7 @@ export interface SharedData {
|
||||
// Shared by the (cloud-only) projectsend/cloud-modules Branding
|
||||
// module's own ServiceProvider, not by this app's own
|
||||
// HandleInertiaRequests — absent entirely on community installs.
|
||||
branding?: { logo_url: string | null; show_site_name: boolean };
|
||||
branding?: { logo_url: string | null; logo_dark_url: string | null; show_site_name: boolean };
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
|
||||
@@ -344,6 +344,12 @@ Route::middleware('auth')->group(function () {
|
||||
Route::patch('system/settings/branding/site-name', [BrandingController::class, 'updateSiteName'])->name('branding.site-name.update');
|
||||
Route::patch('system/settings/branding/logo/crop', [BrandingController::class, 'cropLogo'])->name('branding.logo.crop');
|
||||
Route::delete('system/settings/branding/logo/crop', [BrandingController::class, 'restoreLogo'])->name('branding.logo.restore');
|
||||
// The logo shown in the dark theme (#1845): the same four
|
||||
// actions, told which logo by the `variant` default.
|
||||
Route::post('system/settings/branding/logo-dark', [BrandingController::class, 'store'])->defaults('variant', 'dark')->name('branding.logo-dark.store');
|
||||
Route::delete('system/settings/branding/logo-dark', [BrandingController::class, 'destroy'])->defaults('variant', 'dark')->name('branding.logo-dark.destroy');
|
||||
Route::patch('system/settings/branding/logo-dark/crop', [BrandingController::class, 'cropLogo'])->defaults('variant', 'dark')->name('branding.logo-dark.crop');
|
||||
Route::delete('system/settings/branding/logo-dark/crop', [BrandingController::class, 'restoreLogo'])->defaults('variant', 'dark')->name('branding.logo-dark.restore');
|
||||
|
||||
// POST rather than PATCH: the form carries a file, so it is
|
||||
// multipart, and PHP only populates $_FILES for POST.
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Modules\Platform\Branding\Models\BrandingSetting;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Testing\AssertableInertia;
|
||||
|
||||
beforeEach(function () {
|
||||
Storage::fake('public');
|
||||
$this->actingAs(staffWithPermissions(['edit_settings']));
|
||||
});
|
||||
|
||||
/** 200 × 100, red on the left half and blue on the right. */
|
||||
function darkTestLogo(): UploadedFile
|
||||
{
|
||||
$image = imagecreatetruecolor(200, 100);
|
||||
imagefilledrectangle($image, 0, 0, 99, 99, imagecolorallocate($image, 255, 0, 0));
|
||||
imagefilledrectangle($image, 100, 0, 199, 99, imagecolorallocate($image, 0, 0, 255));
|
||||
|
||||
$temp = tempnam(sys_get_temp_dir(), 'logo');
|
||||
imagepng($image, $temp);
|
||||
|
||||
return new UploadedFile($temp, 'logo.png', 'image/png', null, true);
|
||||
}
|
||||
|
||||
function uploadBothLogos(): BrandingSetting
|
||||
{
|
||||
test()->post(route('branding.store'), ['logo' => UploadedFile::fake()->image('light.png', 200, 80)])->assertRedirect();
|
||||
test()->post(route('branding.logo-dark.store'), ['logo_dark' => darkTestLogo()])->assertRedirect()->assertSessionHasNoErrors();
|
||||
|
||||
return BrandingSetting::query()->sole();
|
||||
}
|
||||
|
||||
test('a logo for dark mode is stored beside the main one and shared with every page', function () {
|
||||
$setting = uploadBothLogos();
|
||||
|
||||
expect($setting->logo_dark_path)->not->toBeNull()
|
||||
->and($setting->logo_dark_path)->not->toBe($setting->logo_path);
|
||||
Storage::disk('public')->assertExists($setting->logo_dark_path);
|
||||
|
||||
$darkUrl = $setting->logoUrl(\App\Modules\Platform\Branding\LogoVariant::Dark);
|
||||
|
||||
$this->get(route('branding.edit'))->assertInertia(fn (AssertableInertia $page) => $page
|
||||
->where('logo_dark.url', $darkUrl)
|
||||
->where('logo_dark.cropped', false));
|
||||
|
||||
auth()->logout();
|
||||
|
||||
$this->get(route('login'))->assertInertia(fn (AssertableInertia $page) => $page
|
||||
->where('branding.logo_url', $setting->logoUrl())
|
||||
->where('branding.logo_dark_url', $darkUrl));
|
||||
});
|
||||
|
||||
test('a dark-mode logo needs a main logo first', function () {
|
||||
$this->post(route('branding.logo-dark.store'), ['logo_dark' => darkTestLogo()])
|
||||
->assertSessionHasErrors('logo_dark');
|
||||
|
||||
expect(BrandingSetting::query()->first()?->logo_dark_path)->toBeNull();
|
||||
});
|
||||
|
||||
test('a dark-mode logo must be an image', function () {
|
||||
$this->post(route('branding.store'), ['logo' => UploadedFile::fake()->image('light.png')]);
|
||||
|
||||
$this->post(route('branding.logo-dark.store'), [
|
||||
'logo_dark' => UploadedFile::fake()->create('notes.pdf', 10, 'application/pdf'),
|
||||
])->assertSessionHasErrors('logo_dark');
|
||||
});
|
||||
|
||||
test('replacing the dark-mode logo deletes the previous one and leaves the main logo alone', function () {
|
||||
$first = uploadBothLogos();
|
||||
|
||||
$this->post(route('branding.logo-dark.store'), ['logo_dark' => darkTestLogo()])->assertRedirect();
|
||||
|
||||
$second = BrandingSetting::query()->sole();
|
||||
expect($second->logo_dark_path)->not->toBe($first->logo_dark_path)
|
||||
->and($second->logo_path)->toBe($first->logo_path);
|
||||
Storage::disk('public')->assertMissing($first->logo_dark_path);
|
||||
});
|
||||
|
||||
test('cropping the dark-mode logo keeps the right part and leaves the main logo alone', function () {
|
||||
$before = uploadBothLogos();
|
||||
|
||||
$this->patch(route('branding.logo-dark.crop'), ['x' => 100, 'y' => 0, 'width' => 100, 'height' => 100])
|
||||
->assertRedirect()->assertSessionHasNoErrors();
|
||||
|
||||
$after = BrandingSetting::query()->sole();
|
||||
$image = imagecreatefromstring(Storage::disk('public')->get($after->logo_dark_path));
|
||||
$rgb = imagecolorsforindex($image, imagecolorat($image, 10, 10));
|
||||
|
||||
expect(imagesx($image))->toBe(100)
|
||||
->and([$rgb['red'], $rgb['green'], $rgb['blue']])->toBe([0, 0, 255])
|
||||
->and($after->logo_dark_original_path)->toBe($before->logo_dark_path)
|
||||
->and($after->logo_dark_crop)->toBe(['x' => 100, 'y' => 0, 'width' => 100, 'height' => 100])
|
||||
->and($after->logo_path)->toBe($before->logo_path)
|
||||
->and($after->logo_crop)->toBeNull();
|
||||
|
||||
$this->delete(route('branding.logo-dark.restore'))->assertRedirect();
|
||||
|
||||
$restored = BrandingSetting::query()->sole();
|
||||
expect($restored->logo_dark_path)->toBe($before->logo_dark_path)
|
||||
->and($restored->logo_dark_original_path)->toBeNull();
|
||||
Storage::disk('public')->assertMissing($after->logo_dark_path);
|
||||
});
|
||||
|
||||
test('removing the dark-mode logo keeps the main one', function () {
|
||||
$setting = uploadBothLogos();
|
||||
|
||||
$this->delete(route('branding.logo-dark.destroy'))->assertRedirect();
|
||||
|
||||
$after = BrandingSetting::query()->sole();
|
||||
expect($after->logo_dark_path)->toBeNull()
|
||||
->and($after->logo_path)->toBe($setting->logo_path);
|
||||
Storage::disk('public')->assertMissing($setting->logo_dark_path);
|
||||
Storage::disk('public')->assertExists($setting->logo_path);
|
||||
});
|
||||
|
||||
test('removing the main logo removes the dark-mode one with it', function () {
|
||||
$setting = uploadBothLogos();
|
||||
|
||||
$this->delete(route('branding.destroy'))->assertRedirect();
|
||||
|
||||
$after = BrandingSetting::query()->sole();
|
||||
expect($after->logo_path)->toBeNull()
|
||||
->and($after->logo_dark_path)->toBeNull();
|
||||
Storage::disk('public')->assertMissing($setting->logo_dark_path);
|
||||
});
|
||||
|
||||
test('the dark-mode logo goes away with the capability, like the main one', function () {
|
||||
uploadBothLogos();
|
||||
|
||||
$shared = fn (): ?string => value(Inertia::getShared('branding'))['logo_dark_url'];
|
||||
expect($shared())->not->toBeNull();
|
||||
|
||||
config(['projectsend.capabilities_disabled' => 'branding.customize']);
|
||||
forgetRequestState();
|
||||
|
||||
expect($shared())->toBeNull();
|
||||
});
|
||||
|
||||
test('the dark-mode routes 404 when the capability has been taken away', function () {
|
||||
// Before any request: the capability set is worked out once, on the
|
||||
// first one.
|
||||
config(['projectsend.capabilities_disabled' => 'branding.customize']);
|
||||
|
||||
$this->post(route('branding.logo-dark.store'), ['logo_dark' => darkTestLogo()])->assertNotFound();
|
||||
$this->patch(route('branding.logo-dark.crop'), ['x' => 0, 'y' => 0, 'width' => 10, 'height' => 10])->assertNotFound();
|
||||
$this->delete(route('branding.logo-dark.restore'))->assertNotFound();
|
||||
$this->delete(route('branding.logo-dark.destroy'))->assertNotFound();
|
||||
});
|
||||
|
||||
test('the dark-mode routes need edit_settings', function () {
|
||||
$this->actingAs(staffWithPermissions([]));
|
||||
|
||||
$this->post(route('branding.logo-dark.store'), ['logo_dark' => darkTestLogo()])->assertForbidden();
|
||||
$this->delete(route('branding.logo-dark.destroy'))->assertForbidden();
|
||||
});
|
||||
|
||||
test('the API reports the dark-mode logo beside the main one', function () {
|
||||
$setting = uploadBothLogos();
|
||||
$staff = staffWithPermissions(['edit_settings']);
|
||||
$token = $staff->createToken('Integration', ['edit_settings'])->plainTextToken;
|
||||
auth()->logout();
|
||||
|
||||
$this->withToken($token)->getJson('/api/v1/modules/branding/logo')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.logo_dark_url', $setting->logoUrl(\App\Modules\Platform\Branding\LogoVariant::Dark));
|
||||
});
|
||||
Reference in New Issue
Block a user