mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-03-14 12:23:33 +00:00
feat: inject device dimensions into framework
This commit is contained in:
parent
64e2b6cdbd
commit
0797f17ebb
3 changed files with 132 additions and 1 deletions
|
|
@ -4,11 +4,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property-read array<string, string> $css_variables
|
||||
* @property-read DevicePalette|null $palette
|
||||
*/
|
||||
final class DeviceModel extends Model
|
||||
|
|
@ -76,4 +78,33 @@ final class DeviceModel extends Model
|
|||
{
|
||||
return $this->belongsTo(DevicePalette::class, 'palette_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns css_variables with --screen-w and --screen-h filled from width/height
|
||||
* when puppeteer_window_size_strategy is v2 and they are not set.
|
||||
*
|
||||
* @return Attribute<array<string, string>, array<string, string>>
|
||||
*/
|
||||
protected function cssVariables(): Attribute
|
||||
{
|
||||
return Attribute::get(function (mixed $value, array $attributes): array {
|
||||
$vars = is_array($value) ? $value : (is_string($value) ? (json_decode($value, true) ?? []) : []);
|
||||
|
||||
if (config('app.puppeteer_window_size_strategy') !== 'v2') {
|
||||
return $vars;
|
||||
}
|
||||
|
||||
$width = $attributes['width'] ?? null;
|
||||
$height = $attributes['height'] ?? null;
|
||||
|
||||
if (empty($vars['--screen-w']) && $width !== null && $width !== '') {
|
||||
$vars['--screen-w'] = is_numeric($width) ? (int) $width.'px' : (string) $width;
|
||||
}
|
||||
if (empty($vars['--screen-h']) && $height !== null && $height !== '') {
|
||||
$vars['--screen-h'] = is_numeric($height) ? (int) $height.'px' : (string) $height;
|
||||
}
|
||||
|
||||
return $vars;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -528,7 +528,7 @@ class ImageGenerationService
|
|||
'deviceOrientation' => $deviceOrientation,
|
||||
'colorDepth' => $colorDepth,
|
||||
'scaleLevel' => $scaleLevel,
|
||||
'cssVariables' => $device->deviceModel?->css_variables,
|
||||
'cssVariables' => $device->deviceModel?->css_variables ?? [],
|
||||
];
|
||||
|
||||
// Add plugin name for error screens
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue