Team-Tea-Time / laravel-forum

A slim, lean forum package designed for quick and easy integration in Laravel projects
https://laravel-forum.teamteatime.net/
MIT License
601 stars 165 forks source link

Add WYSIWYG #275

Closed somato-tomato closed 2 years ago

somato-tomato commented 2 years ago

Can i add WYSIWYG to thread create ?

Thank you!

Riari commented 2 years ago

WYSIWYG is not provided by the package, but you're free to implement it yourself. If you'd like to do that, I would suggest applying your choice of WYSIWYG editor to the textareas in the following views:

Keep in mind that regardless of your WYSIWYG editor choice, you need to take care not to allow any malicious code in posts, so you should sanitise post content either before it's inserted into the DB (preferable) or before it's used in the frontend.

By default, the Forum::render(string $content) utility method is used to prepare post content for display and strips HTML from it. In order to display a post with any kind of formatting or rich content, you need to let at least a subset of HTML through, so you'll need to either modify the post.partials.list view to render post content with your own method, or override the existing method in your own implementation of the Forum class. If you choose to do the latter, you can point the package to your own implementation of the class by changing the forum.web.utility_class config option.

Depending on the WYSIWYG format you choose, you'll probably also want to write post content to the DB in two formats:

To do this, I would recommend these steps:

  1. Create a migration to add a content_html column to the forum_posts table
  2. Implement listeners for the UserCreatedPost and UserUpdatedPost events provided by the package (see package events and the Event Listeners Laravel documentation) OR implement an Eloquent Observer on the Post model to react to the saving event
  3. In your listeners or observer, sanitise the post content (which should be in your WYSIWYG editor format) and convert it to HTML, then write it to the content_html column for the corresponding post (this should be slightly more efficient if you use the Observer approach as you should be able to simply assign to $post->content_html so it gets included in the standard INSERT/UPDATE query)
  4. Modify the post.partials.list view to make it use $post->content_html instead of $post->content (see https://github.com/Team-Tea-Time/laravel-forum/blob/5.0/views/post/partials/list.blade.php#L34-L39)

Once that's done, the content column will be used for the raw content, which will be used by your WYSIWYG editor in the post.edit view.


I hope that helps. Remember to refer to the package docs for details about available features.

Riari commented 2 years ago

Updated my reply to hopefully make it clearer and put more emphasis on security considerations.