Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions resources/views/mails/download.blade.php
Original file line number Diff line number Diff line change
@@ -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)
<a type="button"
href="{{ route('filament.' . Filament\Facades\Filament::getCurrentPanel()->getId() . '.mails.attachment.download', [
<a href="{{ route('filament.' . Filament\Facades\Filament::getCurrentPanel()->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</a>
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">
<x-filament::icon icon="heroicon-m-arrow-down-tray" class="h-4 w-4" />
Download
</a>
@endif
43 changes: 43 additions & 0 deletions resources/views/mails/html-formatted.blade.php
Original file line number Diff line number Diff line change
@@ -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

<div class="prose prose-sm sm:prose lg:prose-lg xl:prose-2xl max-w-full overflow-x-auto">
<pre class="whitespace-pre-wrap break-words"><code class="language-html">{{ $formatted }}</code></pre>
</div>
4 changes: 1 addition & 3 deletions resources/views/mails/html.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<div class="prose prose-sm sm:prose lg:prose-lg xl:prose-2xl max-w-full overflow-x-auto">
<pre class="whitespace-pre-wrap break-words">
<code class="language-html">{{ $html }}</code>
</pre>
<pre class="whitespace-pre-wrap break-words"><code class="language-html">{{ trim($html) }}</code></pre>
</div>
9 changes: 7 additions & 2 deletions resources/views/mails/preview.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<div class="w-full h-screen">
<div class="w-full" x-data="{ height: '250px' }" x-on:message.window="
if ($event.data && $event.data.type === 'mails-iframe-resize' && $event.data.mailId === '{{ $mail->id }}') {
height = $event.data.height + 'px';
}
">
<iframe
src="{{ route('filament.' . Filament\Facades\Filament::getCurrentPanel()->getId() . '.mails.preview', ['tenant' => Filament\Facades\Filament::getTenant(), 'mail' => $mail->id]) }}"
class="w-full h-full max-w-full" style="width: 100vw; height: 100vh; border: none;">
class="w-full border-none"
x-bind:style="'height: ' + height">
</iframe>
</div>
16 changes: 15 additions & 1 deletion src/Controllers/MailPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ public function __invoke(Request $request)
/** @var Mail $mail */
$mail = Mail::find($request->mail);

return response($mail->html);
$resizeScript = <<<HTML
<script>
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 });
</script>
HTML;

$html = $mail->html . $resizeScript;

return response($html);
}
}
29 changes: 24 additions & 5 deletions src/Resources/MailResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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('<code>' . nl2br(e($state)) . '</code>'))
->columnSpanFull(),
]),
])->columnSpanFull(),
Expand All @@ -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'),
]),
]),
Expand Down Expand Up @@ -392,7 +412,6 @@ public static function table(Table $table): Table
])
->recordActions([
ViewAction::make()
// ->url(null)
->modal()
->slideOver()
->label(__('View'))
Expand Down
22 changes: 22 additions & 0 deletions src/Resources/MailResource/Pages/ListMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down