primeng
Version:
PrimeNG is an open source UI library for Angular featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeB
454 lines (449 loc) • 18.9 kB
JavaScript
import * as i1 from '@angular/common';
import { isPlatformServer, CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { Injectable, forwardRef, EventEmitter, inject, afterNextRender, ContentChildren, ContentChild, Output, Input, ViewEncapsulation, ChangeDetectionStrategy, Component, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { findSingle } from '@primeuix/utils';
import { SharedModule, Header, PrimeTemplate } from 'primeng/api';
import { BaseEditableHolder } from 'primeng/baseeditableholder';
import { style } from '@primeuix/styles/editor';
import { BaseStyle } from 'primeng/base';
const classes = {
root: ({ instance }) => [
'p-editor',
{
'p-invalid': instance.invalid()
}
],
toolbar: 'p-editor-toolbar',
content: 'p-editor-content'
};
class EditorStyle extends BaseStyle {
name = 'editor';
theme = style;
classes = classes;
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: EditorStyle, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: EditorStyle });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: EditorStyle, decorators: [{
type: Injectable
}] });
/**
*
* Editor groups a collection of contents in tabs.
*
* [Live Demo](https://www.primeng.org/editor/)
*
* @module editorstyle
*
*/
var EditorClasses;
(function (EditorClasses) {
/**
* Class name of the root element
*/
EditorClasses["root"] = "p-editor";
/**
* Class name of the toolbar element
*/
EditorClasses["toolbar"] = "p-editor-toolbar";
/**
* Class name of the content element
*/
EditorClasses["content"] = "p-editor-content";
})(EditorClasses || (EditorClasses = {}));
const EDITOR_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Editor),
multi: true
};
/**
* Editor groups a collection of contents in tabs.
* @group Components
*/
class Editor extends BaseEditableHolder {
/**
* Inline style of the container.
* @group Props
*/
style;
/**
* Style class of the container.
* @deprecated since v20.0.0, use `class` instead.
* @group Props
*/
styleClass;
/**
* Placeholder text to show when editor is empty.
* @group Props
*/
placeholder;
/**
* Whitelist of formats to display, see [here](https://quilljs.com/docs/formats/) for available options.
* @group Props
*/
formats;
/**
* Modules configuration of Editor, see [here](https://quilljs.com/docs/modules/) for available options.
* @group Props
*/
modules;
/**
* DOM Element or a CSS selector for a DOM Element, within which the editor’s p elements (i.e. tooltips, etc.) should be confined. Currently, it only considers left and right boundaries.
* @group Props
*/
bounds;
/**
* DOM 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..
* @group Props
*/
scrollingContainer;
/**
* Shortcut 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.
* @group Props
*/
debug;
/**
* Whether to instantiate the editor to read-only mode.
* @group Props
*/
get readonly() {
return this._readonly;
}
set readonly(val) {
this._readonly = val;
if (this.quill) {
if (this._readonly)
this.quill.disable();
else
this.quill.enable();
}
}
/**
* Callback to invoke when the quill modules are loaded.
* @param {EditorInitEvent} event - custom event.
* @group Emits
*/
onInit = new EventEmitter();
/**
* Callback to invoke when text of editor changes.
* @param {EditorTextChangeEvent} event - custom event.
* @group Emits
*/
onTextChange = new EventEmitter();
/**
* Callback to invoke when selection of the text changes.
* @param {EditorSelectionChangeEvent} event - custom event.
* @group Emits
*/
onSelectionChange = new EventEmitter();
toolbar;
value;
delayedCommand = null;
_readonly = false;
quill;
dynamicQuill;
/**
* Custom item template.
* @group Templates
*/
headerTemplate;
templates;
_headerTemplate;
get isAttachedQuillEditorToDOM() {
return this.quillElements?.editorElement?.isConnected;
}
quillElements;
_componentStyle = inject(EditorStyle);
constructor() {
super();
/**
* Read or write the DOM once, when initializing non-Angular (Quill) library.
*/
afterNextRender(() => {
this.initQuillElements();
this.initQuillEditor();
});
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'header':
this.headerTemplate = item.template;
break;
}
});
}
/**
* @override
*
* @see {@link BaseEditableHolder.writeControlValue}
* Writes the value to the control.
*/
writeControlValue(value) {
this.value = value;
if (this.quill) {
if (value) {
const command = () => {
this.quill.setContents(this.quill.clipboard.convert(this.dynamicQuill.version.startsWith('2') ? { html: this.value } : this.value));
};
if (this.isAttachedQuillEditorToDOM) {
command();
}
else {
this.delayedCommand = command;
}
}
else {
const command = () => {
this.quill.setText('');
};
if (this.isAttachedQuillEditorToDOM) {
command();
}
else {
this.delayedCommand = command;
}
}
}
}
getQuill() {
return this.quill;
}
initQuillEditor() {
if (isPlatformServer(this.platformId)) {
return;
}
/**
* Importing Quill at top level, throws `document is undefined` error during when
* building for SSR, so this dynamically loads quill when it's in browser module.
*/
if (!this.dynamicQuill) {
import('quill')
.then((quillModule) => {
this.dynamicQuill = quillModule.default;
this.createQuillEditor();
})
.catch((e) => console.error(e.message));
}
else {
this.createQuillEditor();
}
}
createQuillEditor() {
this.initQuillElements();
const { toolbarElement, editorElement } = this.quillElements;
let defaultModule = { toolbar: toolbarElement };
let modules = this.modules ? { ...defaultModule, ...this.modules } : defaultModule;
this.quill = new this.dynamicQuill(editorElement, {
modules: modules,
placeholder: this.placeholder,
readOnly: this.readonly,
theme: 'snow',
formats: this.formats,
bounds: this.bounds,
debug: this.debug,
scrollingContainer: this.scrollingContainer
});
const isQuill2 = this.dynamicQuill.version.startsWith('2');
if (this.value) {
this.quill.setContents(this.quill.clipboard.convert(isQuill2 ? { html: this.value } : this.value));
}
this.quill.on('text-change', (delta, oldContents, source) => {
if (source === 'user') {
let html = isQuill2 ? this.quill.getSemanticHTML() : findSingle(editorElement, '.ql-editor').innerHTML;
let text = this.quill.getText().trim();
if (html === '<p><br></p>') {
html = null;
}
this.onTextChange.emit({
htmlValue: html,
textValue: text,
delta: delta,
source: source
});
this.onModelChange(html);
this.onModelTouched();
}
});
this.quill.on('selection-change', (range, oldRange, source) => {
this.onSelectionChange.emit({
range: range,
oldRange: oldRange,
source: source
});
});
this.onInit.emit({
editor: this.quill
});
}
initQuillElements() {
if (!this.quillElements) {
this.quillElements = {
editorElement: findSingle(this.el.nativeElement, 'div.p-editor-content'),
toolbarElement: findSingle(this.el.nativeElement, 'div.p-editor-toolbar')
};
}
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: Editor, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.1.3", type: Editor, isStandalone: true, selector: "p-editor", inputs: { style: "style", styleClass: "styleClass", placeholder: "placeholder", formats: "formats", modules: "modules", bounds: "bounds", scrollingContainer: "scrollingContainer", debug: "debug", readonly: "readonly" }, outputs: { onInit: "onInit", onTextChange: "onTextChange", onSelectionChange: "onSelectionChange" }, host: { properties: { "class": "cn(cx('root'), styleClass)" } }, providers: [EDITOR_VALUE_ACCESSOR, EditorStyle], queries: [{ propertyName: "toolbar", first: true, predicate: Header, descendants: true }, { propertyName: "headerTemplate", first: true, predicate: ["header"] }, { propertyName: "templates", predicate: PrimeTemplate }], usesInheritance: true, ngImport: i0, template: `
<div [class]="cx('toolbar')" *ngIf="toolbar || headerTemplate || _headerTemplate">
<ng-content select="p-header"></ng-content>
<ng-container *ngTemplateOutlet="headerTemplate || _headerTemplate"></ng-container>
</div>
<div [class]="cx('toolbar')" *ngIf="!toolbar && !headerTemplate && !_headerTemplate">
<span class="ql-formats">
<select class="ql-header">
<option value="1">Heading</option>
<option value="2">Subheading</option>
<option selected>Normal</option>
</select>
<select class="ql-font">
<option selected>Sans Serif</option>
<option value="serif">Serif</option>
<option value="monospace">Monospace</option>
</select>
</span>
<span class="ql-formats">
<button class="ql-bold" aria-label="Bold" type="button"></button>
<button class="ql-italic" aria-label="Italic" type="button"></button>
<button class="ql-underline" aria-label="Underline" type="button"></button>
</span>
<span class="ql-formats">
<select class="ql-color"></select>
<select class="ql-background"></select>
</span>
<span class="ql-formats">
<button class="ql-list" value="ordered" aria-label="Ordered List" type="button"></button>
<button class="ql-list" value="bullet" aria-label="Unordered List" type="button"></button>
<select class="ql-align">
<option selected></option>
<option value="center">center</option>
<option value="right">right</option>
<option value="justify">justify</option>
</select>
</span>
<span class="ql-formats">
<button class="ql-link" aria-label="Insert Link" type="button"></button>
<button class="ql-image" aria-label="Insert Image" type="button"></button>
<button class="ql-code-block" aria-label="Insert Code Block" type="button"></button>
</span>
<span class="ql-formats">
<button class="ql-clean" aria-label="Remove Styles" type="button"></button>
</span>
</div>
<div [class]="cx('content')" [ngStyle]="style"></div>
`, isInline: true, dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "ngmodule", type: SharedModule }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: Editor, decorators: [{
type: Component,
args: [{
selector: 'p-editor',
standalone: true,
imports: [CommonModule, SharedModule],
template: `
<div [class]="cx('toolbar')" *ngIf="toolbar || headerTemplate || _headerTemplate">
<ng-content select="p-header"></ng-content>
<ng-container *ngTemplateOutlet="headerTemplate || _headerTemplate"></ng-container>
</div>
<div [class]="cx('toolbar')" *ngIf="!toolbar && !headerTemplate && !_headerTemplate">
<span class="ql-formats">
<select class="ql-header">
<option value="1">Heading</option>
<option value="2">Subheading</option>
<option selected>Normal</option>
</select>
<select class="ql-font">
<option selected>Sans Serif</option>
<option value="serif">Serif</option>
<option value="monospace">Monospace</option>
</select>
</span>
<span class="ql-formats">
<button class="ql-bold" aria-label="Bold" type="button"></button>
<button class="ql-italic" aria-label="Italic" type="button"></button>
<button class="ql-underline" aria-label="Underline" type="button"></button>
</span>
<span class="ql-formats">
<select class="ql-color"></select>
<select class="ql-background"></select>
</span>
<span class="ql-formats">
<button class="ql-list" value="ordered" aria-label="Ordered List" type="button"></button>
<button class="ql-list" value="bullet" aria-label="Unordered List" type="button"></button>
<select class="ql-align">
<option selected></option>
<option value="center">center</option>
<option value="right">right</option>
<option value="justify">justify</option>
</select>
</span>
<span class="ql-formats">
<button class="ql-link" aria-label="Insert Link" type="button"></button>
<button class="ql-image" aria-label="Insert Image" type="button"></button>
<button class="ql-code-block" aria-label="Insert Code Block" type="button"></button>
</span>
<span class="ql-formats">
<button class="ql-clean" aria-label="Remove Styles" type="button"></button>
</span>
</div>
<div [class]="cx('content')" [ngStyle]="style"></div>
`,
providers: [EDITOR_VALUE_ACCESSOR, EditorStyle],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
'[class]': "cn(cx('root'), styleClass)"
}
}]
}], ctorParameters: () => [], propDecorators: { style: [{
type: Input
}], styleClass: [{
type: Input
}], placeholder: [{
type: Input
}], formats: [{
type: Input
}], modules: [{
type: Input
}], bounds: [{
type: Input
}], scrollingContainer: [{
type: Input
}], debug: [{
type: Input
}], readonly: [{
type: Input
}], onInit: [{
type: Output
}], onTextChange: [{
type: Output
}], onSelectionChange: [{
type: Output
}], toolbar: [{
type: ContentChild,
args: [Header]
}], headerTemplate: [{
type: ContentChild,
args: ['header', { descendants: false }]
}], templates: [{
type: ContentChildren,
args: [PrimeTemplate]
}] } });
class EditorModule {
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: EditorModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.3", ngImport: i0, type: EditorModule, imports: [Editor, SharedModule], exports: [Editor, SharedModule] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: EditorModule, imports: [Editor, SharedModule, SharedModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: EditorModule, decorators: [{
type: NgModule,
args: [{
imports: [Editor, SharedModule],
exports: [Editor, SharedModule]
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { EDITOR_VALUE_ACCESSOR, Editor, EditorClasses, EditorModule, EditorStyle };
//# sourceMappingURL=primeng-editor.mjs.map