EditorEditor is rich text editor component based on Quill.

Editor - 图1

Documentation

Import

  1. import {EditorModule} from 'primeng/editor';
  2.  

Getting Started

Two-way value binding is defined with ngModel.

  1. <p-editor [(ngModel)]="text" [style]="{'height':'320px'}"></p-editor>
  2.  
  1. export class EditorDemo {
  2. text: string;
  3. }
  4.  

Model Driven Forms

Editor can be used in a model driven form as well.

  1. <p-editor formControlName="description" [style]="{'height':'320px'}"></p-editor>
  2.  

Toolbar

Editor provides a default toolbar with common options, to customize it define your elements inside the header element. Refer to Quill documentation for available controls.

  1. <p-editor [(ngModel)]="text2" [style]="{'height':'320px'}">
  2. <p-header>
  3. <span class="ql-format-group">
  4. <span title="Bold" class="ql-format-button ql-bold"></span>
  5. <span class="ql-format-separator"></span>
  6. <span title="Italic" class="ql-format-button ql-italic"></span>
  7. <span class="ql-format-separator"></span>
  8. <span title="Underline" class="ql-format-button ql-underline"></span>
  9. <span class="ql-format-separator"></span>
  10. <span title="Strikethrough" class="ql-format-button ql-strike"></span>
  11. </span>
  12. </p-header>
  13. </p-editor>
  14.  

Properties

NameTypeDefaultDescription
stylestringnullInline style of the container.
styleClassstringnullStyle class of the container.
placeholderstringnullPlaceholder text to show when editor is empty.
readonlybooleanfalseWhether to instantiate the editor to read-only mode.
formatsstring[]nullWhitelist of formats to display, see here for available options.
modulesanynullModules configuration of Editor, see here for available options.
debugstringnullShortcut for debug. Note debug is a static method and will affect other instances of Quill editors on the page. Only warning and error messages are enabled by default.
boundsElementdocument.bodyDOM Element or a CSS selector for a DOM Element, within which the editor’s ui elements (i.e. tooltips, etc.) should be confined. Currently, it only considers left and right boundaries..
scrollingContainerElementnullDOM Element or a CSS selector for a DOM Element, specifying which container has the scrollbars (i.e. overflow-y: auto), if is has been changed from the default ql-editor with custom CSS. Necessary to fix scroll jumping bugs when Quill is set to auto grow its height, and another ancestor container is responsible from the scrolling..

Events

NameParametersDescription
onTextChangeevent.delta: Representation of the change. event.source: Source of change. Will be either "user" or "api". event.htmlValue: Current value as html. event.textValue: Current value as text.Callback to invoke when the text of the editor is changed by the user.
onSelectionChangeevent.range: Object with index and length keys indicating where the selection exists. event.oldRange: Object with index and length keys indicating where the previous selection was.. event.source: Source of change. Will be either "user" or "api".Callback to invoke when selected text of editor changes.
onInitevent.editor: Quill editor instance. event.oldRange: Object with index and length keys indicating where the previous selection was.. event.source: Source of change. Will be either "user" or "api".Callback to invoke after editor is initialized.

Refer to Quill documentation for more information.

Methods

NameParametersDescription
getQuill-Returns the underlying quill instance.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-editor-containerContainer element
ui-editor-toolbarToolbar of the editor
ui-editor-contentEditable area

Dependencies

Quill Editor 1.0.

Resources of quill needs to be added to your application. Example setup with CLI is as follows;

  1. npm install quill --save
  2.  

Add Quill to scripts in angular-cli.json

  1. "scripts": [... "../node_modules/quill/dist/quill.js"],
  2.  

Add Quill css to styles in angular-cli.json

  1. "styles": [ ... "../node_modules/quill/dist/quill.core.css", "../node_modules/quill/dist/quill.snow.css"],

Source

View on GitHub

  1. <h3 class="first">Default</h3>
  2. <p-editor [(ngModel)]="text1" [style]="{'height':'320px'}"></p-editor>
  3. <p>Value: {{text1||'empty'}}</p>
  4. <button pButton type="button" label="Clear" icon="pi pi-times" (click)="text1=null"></button>
  5. <hr style="border-top:0px;border-color:#dde3e6">
  6. <h3>Custom Toolbar</h3>
  7. <p-editor [(ngModel)]="text2" [style]="{'height':'320px'}">
  8. <p-header>
  9. <span class="ql-formats">
  10. <button class="ql-bold" aria-label="Bold"></button>
  11. <button class="ql-italic" aria-label="Italic"></button>
  12. <button class="ql-underline" aria-label="Underline"></button>
  13. </span>
  14. </p-header>
  15. </p-editor>
  16. <p>Value: {{text2||'empty'}}</p>
  17. <button pButton type="button" label="Clear" icon="pi pi-times" (click)="text2=null"></button>
  18.  
  1. import {Component} from '@angular/core';
  2. @Component({
  3. templateUrl: './editordemo.html'
  4. })
  5. export class EditorDemo {
  6. text1: string = '<div>Hello World!</div><div>PrimeNG <b>Editor</b> Rocks</div><div><br></div>';
  7. text2: string;
  8. }
  9.