Lectures List

Use TinyMCE editor on livewire laravel project

Make sure you have created an account at tinymce

and you have Tinymce api key

then use tinymce CDN link at your webpage head section

Create component in laravel

php artisan make:component components/input

Add tinymce.blade.php at input folder

Paste this code on tinymce.blade.php

<div
  x-data="{ value: @entangle($attributes->wire('model')) }"
  x-init="
      tinymce.init({
          target: $refs.tinymce,
          themes: 'modern',
          height: 200,
          menubar: false,
          plugins: [
              'advlist autolink lists link image charmap print preview anchor',
              'searchreplace visualblocks code fullscreen',
              'insertdatetime media table paste code help wordcount'
          ],
          toolbar: 'undo redo | formatselect | ' +
              'bold italic backcolor | alignleft aligncenter ' +
              'alignright alignjustify | bullist numlist outdent indent | ' +
              'removeformat | help',
          setup: function(editor) {
              editor.on('blur', function(e) {
                  value = editor.getContent()
              })
              editor.on('init', function (e) {
                  if (value != null) {
                      editor.setContent(value)
                  }
              })
              function putCursorToEnd() {
                  editor.selection.select(editor.getBody(), true);
                  editor.selection.collapse(false);
              }
              $watch('value', function (newValue) {
                  if (newValue !== editor.getContent()) {
                      editor.resetContent(newValue || '');
                      putCursorToEnd();
                  }
              });
          }
      })
  "
  wire:ignore
>
  <div>
      <input
          x-ref="tinymce"
          type="textarea"
          {{ $attributes->whereDoesntStartWith('wire:model') }}
      >
  </div>
</div>

use this component at the place of textarea input field, it automatically will create input field and enable tinymce on textarea.

<x-input.tinymce wire:model="editor" placeholder="Write post here..." />

Benefit of using this component

This component does not render automatically, we can use it as html editor.