Skip to content

Commit

Permalink
Merge pull request #206 from awcodes/fix/html-storage
Browse files Browse the repository at this point in the history
Fix: html storage
  • Loading branch information
awcodes authored Nov 20, 2023
2 parents 35d1dc8 + 122d2c3 commit e89e398
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'statePath' => null,
'tools' => [],
'blocks' => [],
'shouldSupportBlocks' => false,
])

<div x-ref="defaultFloatingMenu" class="flex gap-1 items-center" x-cloak>
Expand All @@ -12,5 +13,5 @@
@if (in_array('table', $tools)) <x-filament-tiptap-editor::tools.table :state-path="$statePath"/> @endif
@if (in_array('oembed', $tools)) <x-filament-tiptap-editor::tools.oembed :state-path="$statePath"/> @endif
@if (in_array('code-block', $tools)) <x-filament-tiptap-editor::tools.code-block :state-path="$statePath"/> @endif
@if (in_array('blocks', $tools) && $blocks) <x-dynamic-component component="filament-tiptap-editor::tools.blocks" :blocks="$blocks" :state-path="$statePath" /> @endif
@if ($blocks && $shouldSupportBlocks) <x-dynamic-component component="filament-tiptap-editor::tools.blocks" :blocks="$blocks" :state-path="$statePath" /> @endif
</div>
2 changes: 1 addition & 1 deletion resources/views/tiptap-editor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class="relative z-0 tiptap-wrapper rounded-md bg-white dark:bg-gray-900"
@endif

@if (! $isFloatingMenusDisabled() && filled($floatingMenuTools))
<x-filament-tiptap-editor::menus.default-floating-menu :state-path="$statePath" :tools="$floatingMenuTools" :blocks="$blocks"/>
<x-filament-tiptap-editor::menus.default-floating-menu :state-path="$statePath" :tools="$floatingMenuTools" :blocks="$blocks" :should-support-blocks="$shouldSupportBlocks"/>
@endif

<div class="flex h-full">
Expand Down
14 changes: 8 additions & 6 deletions src/TiptapEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function setUp(): void
$this->tools = config('filament-tiptap-editor.profiles.default');
$this->extensions = config('filament-tiptap-editor.extensions') ?? [];

$this->afterStateHydrated(function (TiptapEditor $component, string | array | null $state) {
$this->afterStateHydrated(function (TiptapEditor $component, string | array | null $state): void {

if (! $state) {
if ($this->expectsJSON()) {
Expand All @@ -64,27 +64,29 @@ protected function setUp(): void
if ($this->getBlocks() && $this->expectsJSON()) {
$state = $this->renderBlockPreviews($state, $component);
} elseif ($this->expectsText()) {
$state = $this->getText();
$state = tiptap_converter()->asText($state);
} elseif ($this->expectsHTML()) {
$state = tiptap_converter()->asHTML($state);
}

$component->state($state);
});

$this->afterStateUpdated(function (TiptapEditor $component, Component $livewire) {
$this->afterStateUpdated(function (TiptapEditor $component, Component $livewire): void {
$livewire->validateOnly($component->getStatePath());
});

$this->dehydrateStateUsing(function (TiptapEditor $component, string | array | null $state) {
$this->dehydrateStateUsing(function (TiptapEditor $component, string | array | null $state): string | array | null {

if ($state && $this->expectsJSON()) {
return $this->decodeBlocksBeforeSave($state);
}

if ($state && $this->expectsText()) {
return $component->getText();
return tiptap_converter()->asText($state);
}

return $state;
return tiptap_converter()->asHTML($state);
});

$this->registerListeners([
Expand Down
8 changes: 5 additions & 3 deletions tests/src/FormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use FilamentTiptapEditor\Enums\TiptapOutput;
use FilamentTiptapEditor\Tests\Models\Page;
use FilamentTiptapEditor\Tests\Fixtures\Livewire as LivewireFixture;
use FilamentTiptapEditor\Tests\Resources\PageResource\Pages\CreatePage;
Expand Down Expand Up @@ -76,11 +77,12 @@ public function form(Form $form): Form
->model(Page::class)
->schema([
TextInput::make('title'),
TiptapEditor::make('html_content'),
TiptapEditor::make('html_content')
->output(TiptapOutput::Html),
TiptapEditor::make('json_content')
->output(\FilamentTiptapEditor\Enums\TiptapOutput::Json),
->output(TiptapOutput::Json),
TiptapEditor::make('text_content')
->output(\FilamentTiptapEditor\Enums\TiptapOutput::Text),
->output(TiptapOutput::Text),
]);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/src/Resources/PageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static function form(Form $form): Form
return $form
->schema([
Forms\Components\TextInput::make('title'),
TiptapEditor::make('html_content'),
TiptapEditor::make('html_content')
->output(TiptapOutput::Html),
TiptapEditor::make('json_content')
->output(TiptapOutput::Json),
TiptapEditor::make('text_content')
Expand Down

0 comments on commit e89e398

Please sign in to comment.