Replies: 2 comments 1 reply
-
Hi, in v2 i achieved this like that <?php
namespace App\Filament\Actions;
use App\Models\Page;
use Closure;
use Filament\Forms\ComponentContainer;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use FilamentTiptapEditor\TiptapEditor;
use Str;
class LinkAction extends Action
{
public static function getDefaultName(): ?string
{
return 'filament_tiptap_link';
}
protected function buildUriForPage(?string $id): ?string
{
if ($id === null) {
return null;
}
$page = Page::find($id);
if ($page === null) {
return null;
}
return 'page://'.$page->id;
}
protected function setUp(): void
{
parent::setUp();
$this->mountUsing(function (TiptapEditor $component, ComponentContainer $form) {
$href = $component->getLivewire()->linkProps['href'];
$form->fill([
'href' => $href,
'type' => Str::startsWith($href, 'page://') ? 'page-link' : 'external-link',
'id' => Str::startsWith($href, 'page://') ? Str::after($href, 'page://') : null,
]);
});
$this->modalWidth('md');
$this->modalHeading(function (TiptapEditor $component) {
$context = blank($component->getLivewire()->linkProps['href']) ? 'insert' : 'update';
return __('filament-tiptap-editor::link-modal.heading.'.$context);
});
$this->form([
Grid::make(1)
->schema([
Select::make('type')
->reactive()
->options([
'external-link' => 'External Link',
'page-link' => 'Page Link',
])->afterStateUpdated(function ($state, Select $component): void {
if (! $state) {
return;
}
// NOTE: This chunk of code is a workaround for Livewire not letting
// you entangle to non-existent array keys, which wire:model
// would normally let you do.
$component
->getContainer()
->getComponent(fn (Component $component) => $component instanceof Group)
->getChildComponentContainer()
->fill();
}),
Group::make()
->reactive()
->schema(function (Closure $get) {
$type = $get('type');
if ($type === 'page-link') {
return [
Select::make('id')
->label('Page')
->searchable()
->options(fn () => Page::pluck('title', 'id')),
];
} else {
return [
TextInput::make('href')
->label(__('filament-tiptap-editor::link-modal.labels.url'))
->columnSpan('full')
->required(),
];
}
}),
]),
]);
$this->action(function (TiptapEditor $component, $data) {
$component->getLivewire()->dispatchBrowserEvent('insert-link', [
'statePath' => $component->getStatePath(),
'href' => $data['type'] === 'page-link' ? $this->buildUriForPage($data['id']) : $data['href'],
'hreflang' => '',
'target' => '',
'rel' => '',
]);
$component->state($component->getState());
});
$this->modalActions(
array_merge(
$this->getModalActions(),
[
\Filament\Forms\Components\Actions\Modal\Actions\Action::make('remove_link')
->color('danger')
->extraAttributes([
'x-on:click' => '$dispatch(\'unset-link\'); close()',
'style' => 'margin-left: auto;',
]),
],
)
);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Legend - thank you so much. I got it working in v3 like this:
and then I overrode the Link Mark using this:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi - I would like extend the link modal. My idea is to understand if the link is for a blog article model, or product or an external link.
Then I would like to format the links differently in each case when rendering the "blog post" created inside this tip tap editor. I.e. a blog link would default to "dofollow" and would render automatically with the latest handle (in the instance the link changed). The same for the product.
External links would default to "nofollow" to the inputted href.
I have tried to pass custom data to try and pick this up but I can't seem to make it work.
Do you have any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions