ngx-quill

repository·master·Indexed 23 days ago

https://github.com/killercodemonkey/ngx-quill

An Angular module providing a wrapper for the QuillJS rich text editor. It includes the <quill-editor> component, support for Template-driven and Reactive forms, and tools for global configuration via QuillConfigModule or provideQuillConfig. The library supports Angular version 2 and above and requires Quill version ^2.0.0. It offers features such as custom module integration, projection slots for custom toolbars, and the QuillViewComponent for read-only content rendering.

Tokens
8.3K
Snippets
14
Records
36
Agent score
83%

What's inside ngx-quill

  1. Secure HTML content with the sanitize option

    master

    When working with HTML strings as model values (e.g., via ngModel or formControl), you should manage XSS risks using the sanitize input parameter available on ngx-quill components.

    Caution: The sanitize option is deactivated by default to prevent the accidental stripping of expected content or styling. However, it is highly recommended to activate this option if you are handling untrusted HTML strings to ensure security via Angular's client-side sanitization.

  2. Install ngx-quill

    master

    To install ngx-quill, run the following command:

    npm install ngx-quill

    Peer Dependencies

    Ensure you have the following installed:

    • @angular/core, @angular/common, @angular/forms, @angular/platform-browser
    • quill version ^2.0.0
    • rxjs

    Theme Styling

    You must include Quill theme CSS in your project. You can add them to your index.html, include them in your CSS/SCSS files via @import, or add them to your build process.

    Available themes are bubble.css or snow.css (found in node_modules/quill/dist).

    Example using SCSS imports:

    @import '~quill/dist/quill.bubble.css';
    // or
    @import '~quill/dist/quill.snow.css';
  3. Create a custom toolbar using projection slots

    master

    You can build a completely custom toolbar by using projection slots within the <quill-editor> component. Use the following slots to position content:

    • [above-quill-editor-toolbar]: Content placed above the toolbar.
    • [quill-editor-toolbar]: The main toolbar area.
    • [below-quill-editor-toolbar]: Content placed below the toolbar.

    Note: It is recommended to use native EventListeners rather than Angular (output) listeners within these slots for better compatibility.

    <quill-editor>
      <div above-quill-editor-toolbar>
        above
      </div>
      <div quill-editor-toolbar>
        <span class="ql-formats">
          <button class="ql-bold" [title="'Bold'"></button>
        </span>
        <span class="ql-formats">
          <select class="ql-align" [title="'Aligment'">
            <option selected></option>
            <option value="center"></option>
            <option value="right"></option>
            <option value="justify"></option>
          </select>
        </span>
      </div>
      <div below-quill-editor-toolbar>
        below
      </div>
    </quill-editor>
  4. Configure ngx-quill in an NgModule

    master

    For standard webpack, angular-cli, and tsc builds, import QuillModule and call .forRoot() in your module's imports array.

    Note for Lazy Loading: If you use lazy-loaded modules, you must add QuillModule.forRoot() to your root module to ensure the Config services are correctly registered.

    Note for Angular CLI >= 6: If you need quill to be available globally, add quill to the scripts section of your angular.json.

    import { QuillModule } from 'ngx-quill'
    
    @NgModule({
      imports: [
        ...,
    
        QuillModule.forRoot()
      ],
      ...
    })
    class YourModule { ... }
  5. Configure global Quill settings

    master

    You can set custom default modules and Quill configuration options globally using QuillConfigModule or the provideQuillConfig function. This approach is useful because it avoids importing the entire ngx-quill library into your vendor bundle.

    Using QuillConfigModule (NgModule approach)

    Use QuillConfigModule.forRoot() in your @NgModule imports.

    Using provideQuillConfig (Standalone approach)

    If bootstrapping an Angular application using standalone features, use provideQuillConfig in the providers array of bootstrapApplication.

    import { QuillConfigModule } from 'ngx-quill/config';
    
    @NgModule({
      imports: [
        ...,
    
        QuillConfigModule.forRoot({
          modules: {
            syntax: true,
            toolbar: [...]
          }
        })
      ],
      ...
    })
    class AppModule {}
  6. Suppress global register warnings

    master

    By default, QuillJS logs a warning when Quill.register is called to overwrite an existing module. This is expected when using customOptions or customModules in ngx-quill, or in environments like Angular Universal where the module might execute on both server and browser.

    To suppress these warnings, set suppressGlobalRegisterWarning: true in your configuration.

  7. Register custom modules with CustomModule

    master

    To add custom Quill modules, use the customModules array within your QuillConfig. Each entry must be a CustomModule object containing a path and an implementation.

    The implementation can be the module constructor itself or an Observable that resolves to the constructor, allowing for lazy loading.

    Example: Synchronous implementation

    customModules = [
      { path: 'modules/blotFormatter', implementation: BlotFormatter }
    ];

    Example: Lazy-loaded implementation

    import { defer, from } from 'rxjs';
    
    const BlotFormatter$ = defer(() => import('quill-blot-formatter').then(m => m.default));
    
    customModules = [
      { path: 'modules/blotFormatter', implementation: BlotFormatter$ }
    ];
    export interface CustomModule {
      implementation: any
      path: string
    }
  8. Configure content formats in QuillViewComponent

    master

    When using QuillViewComponent, you can specify how the content input should be interpreted using the format input. The component uses the getFormat helper to determine the parsing logic.

    Supported formats:

    • 'text': The content is treated as plain text and set via quillEditor.setText().
    • 'html': The content is treated as HTML. If sanitize is enabled (either via the sanitize input or global config), the content is sanitized using Angular's DomSanitizer before being converted to Quill Deltas via the clipboard.
    • 'json': The content is treated as a JSON string and parsed via JSON.parse(). If parsing fails, it falls back to inserting the raw value as a single insertion.
    • 'object': The content is treated as a Quill Delta object and set via quillEditor.setContents().

    Note: If format is not explicitly provided, the component falls back to the global configuration defined in ngx-quill/config via this.service.config.format.

  9. Customize the Quill toolbar position

    master

    You can control where the toolbar appears relative to the editor using the customToolbarPosition input. The component uses content projection to allow you to place the toolbar in different slots.

    • top: The toolbar is rendered above the editor.
    • bottom: The toolbar is rendered below the editor.

    Use the following selectors for content projection:

    • [above-quill-editor-toolbar]: Content placed above the editor.
    • [quill-editor-toolbar]: The main toolbar container.
    • [below-quill-editor-toolbar]: Content placed below the editor.
  10. Configure global Quill settings with Standalone APIs

    master

    For standalone Angular applications, use the provideQuillConfig provider function during bootstrapping.

    import { provideQuillConfig } from 'ngx-quill/config';
    
    bootstrapApplication(AppComponent, {
      providers: [
        provideQuillConfig({
          modules: {
            syntax: true,
            toolbar: [...]
          }
        })
      ]
    })