diff --git a/resources/views/mails/download.blade.php b/resources/views/mails/download.blade.php index 1565ac8..3647d2d 100644 --- a/resources/views/mails/download.blade.php +++ b/resources/views/mails/download.blade.php @@ -1,15 +1,19 @@ @php - $attachment = $getState(); - $mailId = is_object($attachment) ? $attachment->mail_id : null; + $uuid = $getState(); + $attachmentModel = config('mails.models.attachment', \Backstage\Mails\Laravel\Models\MailAttachment::class); + $attachment = $uuid ? $attachmentModel::where('uuid', $uuid)->first() : null; + $mailId = $attachment?->mail_id; @endphp @if($mailId && $attachment) -getId() . '.mails.attachment.download', [ 'tenant' => Filament\Facades\Filament::getTenant(), 'mail' => $mailId, 'attachment' => $attachment->id, 'filename' => $attachment->filename, ]) }}" - class="rounded-md bg-white px-3.5 py-2.5 text-sm font-semibold cursor-pointer text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50">Download + class="fi-btn fi-btn-size-sm inline-flex items-center justify-center gap-1 rounded-lg bg-primary-600 px-3 py-1.5 text-sm font-semibold text-white shadow-sm transition-colors hover:bg-primary-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-600 dark:bg-primary-500 dark:hover:bg-primary-400"> + + Download + @endif diff --git a/resources/views/mails/html-formatted.blade.php b/resources/views/mails/html-formatted.blade.php new file mode 100644 index 0000000..13ae9b6 --- /dev/null +++ b/resources/views/mails/html-formatted.blade.php @@ -0,0 +1,43 @@ +@php + $formatted = trim($html); + + // Put each tag on its own line + $formatted = preg_replace('/>\s*\n<", $formatted); + + // Indent based on nesting depth + $lines = explode("\n", $formatted); + $indent = 0; + $result = []; + $selfClosingPattern = '/^<(?:meta|link|br|hr|img|input|source|col|area|base|embed|param|track|wbr)\b/i'; + $closingPattern = '/^<\//'; + $openingPattern = '/^<[a-zA-Z]/'; + + foreach ($lines as $line) { + $line = trim($line); + + if ($line === '') { + continue; + } + + if (preg_match($closingPattern, $line)) { + $indent = max(0, $indent - 1); + } + + $result[] = str_repeat(' ', $indent) . $line; + + if (preg_match($openingPattern, $line) + && !preg_match($selfClosingPattern, $line) + && !preg_match($closingPattern, $line) + && !str_contains($line, '/>') + && !preg_match('/<\/[a-zA-Z][^>]*>\s*$/', $line) + ) { + $indent++; + } + } + + $formatted = implode("\n", $result); +@endphp + +
+
{{ $formatted }}
+
diff --git a/resources/views/mails/html.blade.php b/resources/views/mails/html.blade.php index 3bb12fe..d82292a 100644 --- a/resources/views/mails/html.blade.php +++ b/resources/views/mails/html.blade.php @@ -1,5 +1,3 @@
-
-        {{ $html }}
-    
+
{{ trim($html) }}
diff --git a/resources/views/mails/preview.blade.php b/resources/views/mails/preview.blade.php index 0f45184..160fe39 100644 --- a/resources/views/mails/preview.blade.php +++ b/resources/views/mails/preview.blade.php @@ -1,6 +1,11 @@ -
+
diff --git a/src/Controllers/MailPreviewController.php b/src/Controllers/MailPreviewController.php index d017789..01ecb7f 100644 --- a/src/Controllers/MailPreviewController.php +++ b/src/Controllers/MailPreviewController.php @@ -13,6 +13,20 @@ public function __invoke(Request $request) /** @var Mail $mail */ $mail = Mail::find($request->mail); - return response($mail->html); + $resizeScript = << + function postHeight() { + var height = document.documentElement.scrollHeight || document.body.scrollHeight; + window.parent.postMessage({ type: 'mails-iframe-resize', mailId: '{$mail->id}', height: height }, '*'); + } + window.addEventListener('load', postHeight); + window.addEventListener('resize', postHeight); + new MutationObserver(postHeight).observe(document.body, { childList: true, subtree: true }); + + HTML; + + $html = $mail->html . $resizeScript; + + return response($html); } } diff --git a/src/Resources/MailResource.php b/src/Resources/MailResource.php index 0e4c630..a0e7e5b 100644 --- a/src/Resources/MailResource.php +++ b/src/Resources/MailResource.php @@ -278,6 +278,21 @@ public static function infolist(Schema $schema): Schema ->label(__('HTML Content')) ->columnSpanFull(), ]), + Tab::make('HTML (Formatted)') + ->schema([ + TextEntry::make('html') + ->hiddenLabel() + ->extraAttributes(['class' => 'overflow-x-auto']) + ->formatStateUsing(fn (string $state, Mail $record): View => view( + 'mails::mails.html-formatted', + ['html' => $state, 'mail' => $record], + )) + ->copyable() + ->copyMessage('Copied!') + ->copyMessageDuration(1500) + ->label(__('HTML Content (Formatted)')) + ->columnSpanFull(), + ]), Tab::make('Text') ->schema([ TextEntry::make('text') @@ -286,7 +301,7 @@ public static function infolist(Schema $schema): Schema ->copyMessage('Copied!') ->copyMessageDuration(1500) ->label(__('Text Content')) - ->formatStateUsing(fn (string $state): HtmlString => new HtmlString(nl2br(e($state)))) + ->formatStateUsing(fn (string $state): HtmlString => new HtmlString('' . nl2br(e($state)) . '')) ->columnSpanFull(), ]), ])->columnSpanFull(), @@ -308,17 +323,22 @@ public static function infolist(Schema $schema): Schema ->label(__('Attachments')) ->visible(fn (Mail $record) => $record->attachments->count() > 0) ->schema([ - Grid::make(3) + Grid::make(4) ->schema([ TextEntry::make('filename') ->label(__('Name')), TextEntry::make('size') - ->label(__('Size')), + ->label(__('Size')) + ->formatStateUsing(fn (int $state): string => match (true) { + $state >= 1073741824 => number_format($state / 1073741824, 2) . ' GB', + $state >= 1048576 => number_format($state / 1048576, 2) . ' MB', + $state >= 1024 => number_format($state / 1024, 2) . ' KB', + default => $state . ' bytes', + }), TextEntry::make('mime') ->label(__('Mime Type')), ViewEntry::make('uuid') ->label(__('Download')) - ->formatStateUsing(fn ($record) => $record) ->view('mails::mails.download'), ]), ]), @@ -392,7 +412,6 @@ public static function table(Table $table): Table ]) ->recordActions([ ViewAction::make() - // ->url(null) ->modal() ->slideOver() ->label(__('View')) diff --git a/src/Resources/MailResource/Pages/ListMails.php b/src/Resources/MailResource/Pages/ListMails.php index 3feb7ce..0bf49c5 100644 --- a/src/Resources/MailResource/Pages/ListMails.php +++ b/src/Resources/MailResource/Pages/ListMails.php @@ -17,6 +17,28 @@ public static function canAccess(array $parameters = []): bool return MailsPlugin::get()->userCanManageMails(); } + public function mountAction(string $name, array $arguments = [], array $context = []): mixed + { + $result = parent::mountAction($name, $arguments, $context); + + if ($name === 'view' && isset($context['table']) && isset($context['recordKey'])) { + $this->defaultTableAction = $name; + $this->defaultTableActionRecord = $context['recordKey']; + } + + return $result; + } + + public function unmountAction(bool $canCancelParentActions = true): void + { + parent::unmountAction($canCancelParentActions); + + if (empty($this->mountedActions)) { + $this->defaultTableAction = null; + $this->defaultTableActionRecord = null; + } + } + public static function getResource(): string { return config('mails.resources.mail', MailResource::class);