ehyiah/ux-quill

Symfony UX Bundle to use Quill JS wysiwyg text editor with full and easy customisation

Installs: 12 287

Dependents: 0

Suggesters: 0

Security: 0

Stars: 16

Watchers: 1

Forks: 4

Open Issues: 3

Type:symfony-bundle

2.0.5 2024-06-14 18:23 UTC

This package is auto-updated.

Last update: 2024-12-14 19:58:18 UTC


README

Symfony UX Bundle implementing the Quill JS Wysiwyg https://quilljs.com/

If you need a easy to use WYSIWYG (with no complex configuration) into a symfony project this is what you need.

2.x.x tags cover the new Quill v2

1.x.x tags cover the Quill v1.3.7

Installation

Step 1 Require bundle

  composer require ehyiah/ux-quill

If you are using the AssetMapper Component you're done !

step 2 next run (If you are using webpack encore, not needed with AssetMapper)

    yarn install --force
    yarn watch

OR

    npm install --force
    npm run watch

It's done, you can use the QuillType to build a QuillJs WYSIWYG

You can add as many WYSIWYG fields inside same page like any normal fields.

Basic Usage

In a form, use QuillType. It works like a classic Type except it has more options : e.g:

    use Ehyiah\QuillJsBundle\Form\QuillType;
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('myField', QuillType::class)
        ;
    }

Display result

in a twig template :

  • if you use the default class styling option you may need to encapsulate the content so the quill stylesheet can be applied like this :
    <div class="ql-snow">
        <div class="ql-editor">
            {{ myField|raw }}
        </div>
    </div>
  • if you use the inline styling option simply :
    <div>{{ myField|raw }}</div>

you can of course sanitize HTML if you need to for security reason, but don't forget to configure it to your needs as many html balise and style elements will be removed by default. Same goes in your Form configuration

    'sanitize_html' => false,
    'sanitizer' => 'my_awesome_sanitizer_config

For the most basic this is only what you have to do.

Customize Options

    use Ehyiah\QuillJsBundle\Form\QuillType;
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('myField', QuillType::class, [
                'quill_extra_options' => [
                    'height' => '780px',
                    'theme' => 'snow',
                    'placeholder' => 'Hello Quill WYSIWYG',
                ],
                'quill_options' => [
                // this is where you customize the WYSIWYG by creating one or many Groups
                // if you create many groups, they will be separated by a space in the toolbar
                // you can also build your groups using a classic array but many classes are covering every Quill available Fields (see below for detailed list)
                    QuillGroup::build(
                        new BoldField(),
                        new ItalicField(),
                        // and many more
                    ),
                    QuillGroup::build(
                        new HeaderField(HeaderField::HEADER_OPTION_1),
                        new HeaderField(HeaderField::HEADER_OPTION_2),
                        // and many more
                    ),
                    // Or add all available fields at once
                    QuillGroup::buildWithAllFields()
                ]
            ])
        ;
    }

quill_options :

This is where you will choose what elements you want to display in your WYSIWYG. You can build an array like you would do following the QuillJs official documentation Or use a more convenient with Autocomplete using the many Fields Object in this bundle.

      QuillGroup::build(
          new HeaderField(HeaderField::HEADER_OPTION_1),
          new HeaderField(HeaderField::HEADER_OPTION_2),
      )

This example will display a h1 and h2 header options side by side

      QuillGroup::build(
          new HeaderField(HeaderField::HEADER_OPTION_1),
          new HeaderField(HeaderField::HEADER_OPTION_2),
      )
      QuillGroup::build(
          new BoldField(),
          new ItalicField(),
      )

This example will display a h1 and h2 header options side by side and another Group containing a Bold and an Italic fields

You can add as many Groups as you like or just One if you don't need the WYSIWYG options to have spaces between them.

Available Fields

  • Below is a list of fields not available in QuillJS but taken from community

quill_extra_options:

Image upload Handling

in ImageField : QuillJS transform images in base64 encoded file by default to save your files. However, you can specify a custom endpoint to handle image uploading and pass in response the entire public URL to display the image.

  • currently handling :
  • data sending in base64 inside a json
  • OR
  • in a multipart/form-data
    'quill_extra_options' => [
        ///
        'upload_handler' => [
            'type' => 'json',
            // or 'type' => 'form',
            'path' => '/my-custom-endpoint/upload',
        ]
    ],
  • your endpoint must return the complete URL of the file example :
  https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/JavaScript-logo.png/480px-JavaScript-logo.png
  • in json mode data will look like this by calling $request->getContent() and application/json in content-type headers
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAJYCAQAAAAUb1BXAAAABGdBTUEAALGPC/xhBQAAACyygyyioiBqFCUIKC64x..."
  • in form mode you will find a multipart/form-data in content-type headers and file will be present in $request->files named file
  • then you can handle it like you would do with a FileType

Modules

https://quilljs.com/docs/modules

You can add/customize quill modules in this option field. You can create your own modules classes, they need to implement the ModuleInterface and add the name and options properties.

Extend Quill stimulus controller

If you need to extend default behavior of built-in controller, this is possible. exemple : you need to modify modules registration and/or add custom javascript to modify quill behaviour.

Some modules like Keyboard and Clipboard need custom javascript to be written. The easiest way to do so is to create a custom stimulus controller to extend the default behavior.

Create a new stimulus controller inside your project

// quill_extended_controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
    connect() {
        this.element.addEventListener('quill:connect', this._onConnect);
    }

    disconnect() {
        this.element.removeEventListener('quill:connect', this._onConnect);
    }

    _onConnect(event) {
        // The quill has been created
        console.log(event.detail); // You can access the quill instance using the event detail
        
        let quill = event.detail;
        // e.g : if you want to add a new keyboard binding
        quill.keyboard.addBinding({
            key: 'b',
            shortKey: true
        }, function(range, context) {
            this.quill.formatText(range, 'bold', true);
        });
          
        // e.g if you want to add a custom clipboard
        quill.clipboard.addMatcher(Node.TEXT_NODE, (node, delta) => {
            return new Delta().insert(node.data);
        });
    }
}

Then in your form

use Ehyiah\QuillJsBundle\Form\QuillType;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        // ...
        ->add('myField', QuillType::class, [
            'attr' => [
                'data-controller' => 'quill-extended',
            ]
        // ...
    ;
}

Easyadmin Integration

  • First create a quill-admin.js inside assets directory
    // start the Stimulus application
    import './bootstrap';

When using AssetMapper

create a new entry in importmap.php (the key must be quill-admin as it is the name used in the built-in QuillAdminField)

    'quill-admin' => [
        'path' => './assets/quill-admin.js',
        'entrypoint' => true,
    ],

and it should be done. but read below

WARNING => at the moment there seems to have an issue with easyadmin with the ->addAssetMapperEntries() function as I can not get it work as it should be. a quick fix is to add in your crudControllers

    public function configureAssets(Assets $assets): Assets
    {
        $assets->addAssetMapperEntry('quill-admin');
        return parent::configureAssets($assets); // TODO: Change the autogenerated stub
    }

OR

in your dashboard

    public function configureAssets(): Assets
    {
        $assets = Assets::new();
        $assets->addAssetMapperEntry('quill-admin');

        return $assets;
    }

When using webpack

  • Next create in webpack.config a new entry (the entry name must be quill-admin as it is the name used in the built-in QuillAdminField)
    .addEntry('quill-admin', './assets/quill-admin.js')

don't forget to recompile assets (yarn build/watch or npm equivalent).

EasyAdmin

Then you can use the QuillAdminField like this :

    QuillAdminField::new('quill')

Or add custom options like you would do with the normal type

    QuillAdminField::new('quill')
        ->setFormTypeOptions([
            'quill_options' =>
                QuillGroup::build(
                    new BoldField(),
                    new ItalicField(),
                    new HeaderField(HeaderField::HEADER_OPTION_1),
                    new HeaderField(HeaderField::HEADER_OPTION_2),
                )
        ])