Skip to content

Commit a1ba0c4

Browse files
authored
Fix comment editor updates for prefixed aliases (#88)
* Fix comment editor updates for prefixed aliases * Make alias assertions respect component prefix * Fix code style
1 parent 47a61c4 commit a1ba0c4

6 files changed

Lines changed: 39 additions & 7 deletions

File tree

resources/dist/commentions.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/commentions.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const debounce = (func, wait) => {
1717
};
1818

1919
document.addEventListener('alpine:init', () => {
20-
Alpine.data('editor', (content, mentions, component, placeholder, editorCssClasses) => {
20+
Alpine.data('editor', (content, mentions, component, placeholder, editorCssClasses, componentAlias = null) => {
2121
let editor
2222

2323
const defaultEditorCssClasses = `comm:prose comm:dark:prose-invert comm:prose-sm comm:sm:prose-base comm:lg:prose-lg comm:xl:prose-2xl comm:focus:outline-none comm:p-4 comm:min-w-full comm:w-full comm:rounded-lg comm:border comm:border-gray-300 comm:dark:border-gray-700`;
@@ -27,11 +27,10 @@ document.addEventListener('alpine:init', () => {
2727

2828
init() {
2929
const _this = this
30+
const targetComponent = componentAlias ?? `commentions::${component}`
3031

3132
const debouncedUpdate = debounce((editor) => {
32-
Livewire.dispatchTo(`commentions::${component}`, `body:updated`, {
33-
value: editor.getHTML()
34-
});
33+
Livewire.dispatchTo(targetComponent, `body:updated`, editor.getHTML());
3534
}, 300);
3635

3736
editor = new Editor({

resources/views/comment.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class="comm:text-xs comm:text-gray-300 comm:ml-1"
9292
@if ($editing)
9393
<div class="comm:mt-2">
9494
<div class="tip-tap-container comm:mb-2" wire:ignore>
95-
<div x-data="editor(@js($commentBody), @js($mentionables), 'comment', null, @js($this->getTipTapCssClasses()))">
95+
<div x-data="editor(@js($commentBody), @js($mentionables), 'comment', null, @js($this->getTipTapCssClasses()), @js($commentionsComponentPrefix . 'comment'))">
9696
<div x-ref="element"></div>
9797
</div>
9898
</div>

resources/views/comments.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{{-- tiptap editor --}}
99
<div class="comm:relative tip-tap-container comm:mb-2" x-on:click="wasFocused = true" wire:ignore>
1010
<div
11-
x-data="editor(@js($commentBody), @js($this->mentions), 'comments', @js($this->getPlaceholder()), @js($this->getTipTapCssClasses()))"
11+
x-data="editor(@js($commentBody), @js($this->mentions), 'comments', @js($this->getPlaceholder()), @js($this->getTipTapCssClasses()), @js($commentionsComponentPrefix . 'comments'))"
1212
>
1313
<div x-ref="element"></div>
1414
</div>

tests/Livewire/CommentTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Kirschbaum\Commentions\Comment;
44
use Kirschbaum\Commentions\Comment as CommentModel;
5+
use Kirschbaum\Commentions\Config;
56
use Kirschbaum\Commentions\Livewire\Comment as CommentComponent;
67
use Kirschbaum\Commentions\RenderableComment;
78
use Tests\Models\Post;
@@ -224,6 +225,23 @@
224225
]);
225226
});
226227

228+
test('editing comment editor includes prefixed component alias', function () {
229+
/** @var User $user */
230+
$user = User::factory()->create();
231+
actingAs($user);
232+
233+
$post = Post::factory()->create();
234+
$comment = CommentModel::factory()->author($user)->commentable($post)->create();
235+
236+
$componentAlias = Config::getComponentPrefix() . 'comment';
237+
238+
livewire(CommentComponent::class, [
239+
'comment' => $comment,
240+
])
241+
->call('edit')
242+
->assertSee($componentAlias, false);
243+
});
244+
227245
test('can render a custom renderable comment', function () {
228246
$comment = new RenderableComment(
229247
id: 1,

tests/Livewire/CommentsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Support\Facades\Event;
4+
use Kirschbaum\Commentions\Config;
45
use Kirschbaum\Commentions\Events\CommentWasCreatedEvent;
56
use Kirschbaum\Commentions\Livewire\Comments;
67
use Tests\Models\Post;
@@ -83,3 +84,17 @@
8384

8485
Event::assertNotDispatched(CommentWasCreatedEvent::class);
8586
});
87+
88+
test('comments editor includes prefixed component alias', function () {
89+
/** @var User $user */
90+
$user = User::factory()->create();
91+
actingAs($user);
92+
93+
$post = Post::factory()->create();
94+
95+
$componentAlias = Config::getComponentPrefix() . 'comments';
96+
97+
livewire(Comments::class, [
98+
'record' => $post,
99+
])->assertSee($componentAlias, false);
100+
});

0 commit comments

Comments
 (0)