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
18 changes: 5 additions & 13 deletions app/Livewire/Posts/PostFormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
namespace App\Livewire\Posts;

use App\Dtos\PostDto;
use App\Enums\LivewireEventEnum;
use App\Enums\PostStateEnum;
use App\Http\Requests\Post\StorePostRequest;
use App\Services\PostService;
use App\Traits\LoggingTrait;
use App\Traits\SubsiteTrait;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Attributes\Locked;
use Livewire\Component;

final class PostFormComponent extends Component
Expand All @@ -24,7 +23,11 @@ final class PostFormComponent extends Component
public string $body = '';
public string $more_inside = '';
public string $tags = '';

#[Locked]
public int $subsiteId = 0;

#[Locked]
public int $userId = 0;

private PostService $postService;
Expand All @@ -49,17 +52,6 @@ public function render(): View
return view('livewire.posts.post-form-component');
}

#[On(LivewireEventEnum::EditorUpdated->value)]
public function saveEditorContent($editorId, $content): void
{
if ($editorId === 'post-body') {
$this->body = $content;
}
if ($editorId === 'more-inside') {
$this->more_inside = $content;
}
}

public function store(): void
{
$this->validate();
Expand Down
9 changes: 9 additions & 0 deletions app/Livewire/Wysiwyg/WysiwygComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@

use App\Enums\LivewireEventEnum;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Modelable;
use Livewire\Component;

final class WysiwygComponent extends Component
{
#[Modelable]
public string $content = '';

#[Locked]
public string $editorId;

#[Locked]
public string $label;

#[Locked]
public string $name;

public function mount(string $editorId, string $content = '', string $label = '', string $name = ''): void
Expand Down
1 change: 0 additions & 1 deletion app/Services/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function store(PostDto $dto): ?Post
'more_inside' => $this->purifierService->clean($dto->more_inside),
'user_id' => $dto->user_id,
'subsite_id' => $dto->subsite_id,
'state' => $dto->state,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if I agree with not saving state in PostService. Looking at spots in the codebase where PostService stores a post from a PostDto, state is getting set to variously draft or to published in the dto and this would kind of thwart that setting.
I wonder if you got rid of state here because posts don't currently get published and this solves the problem? I noticed that in local and in current staging but thought the problem could be solved by adding state as a fillable property to app/Models/Post.php

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I took this out here because it was crashing to try to set this after some earlier changes in #62 that made state unfillable in the Post model and just applied a default setting of draft for that column.

The reason for the change in #62 was that the draft/published state is actually managed differently via the laravel-drafts trait, which uses the is_current and is_published flags to manage draft state, rather than using the state column, so the state column was never changing from draft even when a post was published. I should probably remove it from the PostDto too!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! Thank you for the background info, that's fine then!

'published_at' => $dto->published_at,
'is_published' => $dto->is_published,
];
Expand Down
64 changes: 53 additions & 11 deletions resources/js/wysiwyg.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,76 @@ const cleanupKey = Symbol('cleanup');
// Set up a single textarea with CKEditor
async function initializeEditor(textarea, component) {
try {
const editorConfig = JSON.parse(document.querySelector('meta[name="ckeditor-config"]')?.content || 'null') || {}
let globalEditorConfig = null;
try {
globalEditorConfig = JSON.parse(document.querySelector('meta[name="ckeditor-config"]')?.content || 'null');
} catch (e) {
// Ignore
console.debug('Unable to parse CKEditor config from meta tag', e);
}

let localEditorConfig = null;
try {
localEditorConfig = JSON.parse(textarea.dataset.editorConfig || 'null');
} catch (e) {
// Ignore
console.debug('Unable to parse CKEditor config from textarea data-editor-config', e);
}

const editorConfig = {
...(globalEditorConfig || {}),
...(localEditorConfig || {}),
toolbar: {
items: [
'bold', 'italic', 'link',
'bulletedList', 'numberedList', 'blockQuote', '|',
'heading', 'insertTable', 'mediaEmbed', '|',
'undo', 'redo'
],
// Collapse into three dots menu when the toolbar is full.
shouldNotGroupWhenFull: false
}
}

const editor = await ClassicEditor.create(textarea, editorConfig);
const editorId = textarea.dataset.editorId;

// Listen for changes to the editor content and update the component's content property
editor.model.document.on('change:data', () => {
component.$wire.$set('content', editor.getData());
// Listen for changes to the editor content and update the component's content property.
const sync = () => {
const content = editor.getData();
textarea.value = content;
component.$wire.$set('content', content, false);
};

editor.model.document.on('change:data', sync);

// Also sync when the editor loses focus.
editor.ui.focusTracker.on('change:isFocused', ( _event, _name, isFocused ) => {
if (!isFocused) sync();
});

// Reset the editor content when we receive a notification from the backend.
const onEditorClear = (event) => {
const clearEditor = (event) => {
if (event.detail.editorId === editorId) {
editor.setData('');
textarea.value = '';
}
};

document.addEventListener('editor:clear', onEditorClear);
document.addEventListener('editor:clear', clearEditor);

// Update the component's content property when the force sync event is received
const onForceSync = () => {
component.$wire.$set('content', editor.getData());
const syncAndFlush = () => {
const content = editor.getData();
textarea.value = content;
component.$wire.$set('content', content);
};

document.addEventListener('livewire:force-sync', onForceSync);
document.addEventListener('livewire:force-sync', syncAndFlush);

textarea[cleanupKey] = () => {
document.removeEventListener('editor:clear', onEditorClear);
document.removeEventListener('livewire:force-sync', onForceSync);
document.removeEventListener('editor:clear', clearEditor);
document.removeEventListener('livewire:force-sync', syncAndFlush);
editor.destroy().catch(e => console.error('Error destroying editor', e));
};
} catch (error) {
Expand Down
3 changes: 3 additions & 0 deletions resources/sass/modules/_forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ fieldset {
border: none;
position: relative;
margin: 0;
// Reset min-inline-size to unset - Chrome sets it to min-content, which
// prevents CKEditor from responding to the screen width correctly.
min-inline-size: unset;
padding: 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<livewire:wysiwyg.wysiwyg-component
editor-id="{{ $this->editorId }}"
name="text"
content="{!! $comment->body !!}" />
wire:model="body" />

<div class="level">
@if($isEditing === true || $isReplying === true)
Expand Down
2 changes: 2 additions & 0 deletions resources/views/livewire/posts/post-form-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
<div wire:ignore>
<livewire:wysiwyg.wysiwyg-component
editor-id="post-body"
wire:model="body"
/>
</div>

<div wire:ignore>
<livewire:wysiwyg.wysiwyg-component
editor-id="more-inside"
label="{{ trans('More Inside') }}"
wire:model="more_inside"
/>
</div>
<x-forms.input
Expand Down
3 changes: 2 additions & 1 deletion resources/views/livewire/wysiwyg/wysiwyg-component.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
class="wysiwyg"
id="{{ $editorId }}"
data-editor-id="{{ $editorId }}"
wire:model.lazy="content">{{ $content }}</textarea>
data-editor-config="{{ json_encode($editorConfig) }}"
wire:model.lazy="content">{!! $content !!}</textarea>
</div>