Skip to content

Commit

Permalink
Merge pull request #22 from tonysm/fix-livewire-issue
Browse files Browse the repository at this point in the history
Fix Livewire issue
  • Loading branch information
tonysm authored Apr 8, 2022
2 parents 64f59ea + 939bf9b commit 2953e7d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,71 @@ When storing references of custom attachments, the package uses another package
In case you want to rotate your key, you would need to loop-through all the rich text content, take all attachables with an `sgid` attribute, assign a new value to it with the new signature using the new secret, and store the content with that new value.
### Livewire
If you're binding a model that has rich text fields to a Livewire component, you may add the `WithRichTexts` trait to your component. Also, it's recommended that you keep the rich text in raw form until the moment you want to save that to the Rich Text field, something like this:
```php
<?php

namespace App\Http\Livewire;

use App\Models\User;
use Livewire\Component;
use Tonysm\RichTextLaravel\Livewire\WithRichTexts;

class UserProfileForm extends Component
{
use WithRichTexts;

public User $user;
public $bio = '';

protected $rules = [
'bio' => ['required'],
];

public function mount(User $user)
{
$this->user = $user;
$this->bio = $user->bio->toTrixHtml();
}

public function save()
{
$this->validate();

$this->user->update([
'bio' => $this->bio,
]);
}
}
```
In this example, the User model has a `bio` rich text field.
<details>
<summary>See the contents of the User model in the example</summary>
```php
<?php

use Illuminate\Foundation\Auth\User as Authenticatable;
use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

class User extends Model
{
use HasRichText;

protected $fillable = ['bio'];
protected $richTextFields = ['bio'];
}
```
</details>
## Testing
```bash
Expand Down
7 changes: 7 additions & 0 deletions src/Livewire/WithRichTexts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tonysm\RichTextLaravel\Livewire;

trait WithRichTexts
{
}
47 changes: 47 additions & 0 deletions src/LivewireSupportsRichText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Tonysm\RichTextLaravel;

use Livewire\Livewire;
use Tonysm\RichTextLaravel\Livewire\WithRichTexts;
use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

class LivewireSupportsRichText
{
public static function init()
{
new static;
}

public function __construct()
{
if (! class_exists(Livewire::class)) {
return;
}

Livewire::listen('property.dehydrate', function ($property, $value, $component, $response) {
$uses = array_flip(class_uses_recursive($component));

if (! in_array(WithRichTexts::class, $uses)) {
return;
}

$this->dehydratePropertyFromWithRichText($value);
});
}

protected function dehydratePropertyFromWithRichText($value)
{
if (! is_object($value)) {
return;
}

$uses = array_flip(class_uses_recursive($value));

if (! in_array(HasRichText::class, $uses)) {
return;
}

$value->unsetRichTextRelationshipsForLivewireDehydration();
}
}
11 changes: 11 additions & 0 deletions src/Models/Traits/HasRichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ protected static function bootHasRichText()
});
}

public function unsetRichTextRelationshipsForLivewireDehydration()
{
$relationships = array_map(fn ($field) => static::fieldToRichTextRelationship($field), $this->getRichTextFields());

foreach ($relationships as $relationship) {
if ($this->relationLoaded($relationship)) {
$this->unsetRelation($relationship);
}
}
}

protected static function registerRichTextRelationships(string $field): void
{
static::resolveRelationUsing(static::fieldToRichTextRelationship($field), function (Model $model) use ($field) {
Expand Down
5 changes: 5 additions & 0 deletions src/RichTextLaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function configurePackage(Package $package): void
->hasMigration('create_rich_texts_table')
->hasCommand(RichTextLaravelInstallCommand::class);
}

public function packageBooted()
{
LivewireSupportsRichText::init();
}
}

0 comments on commit 2953e7d

Please sign in to comment.