All files / src/fieldcontrols RichText.ts

92.59% Statements 25/27
90% Branches 9/10
71.43% Functions 5/7
92% Lines 23/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98            1x   1x 1x       1x                       1x           6x     1x 1x         1x       4x 4x 3x 3x 2x 1x 1x   1x   2x         1x 1x       5x                                                   5x     5x        
/**
 * @module FieldControls
 * 
 */ /** */
 
import { FieldSettings } from 'sn-client-js';
import { customElement } from 'aurelia-framework';
 
import { FieldBaseControl } from './FieldBaseControl';
import { ValidationRules } from 'aurelia-validation';
 
import { Quill as QuillType } from 'quill';
 
const Quill = require('quill') as QuillType;
 
/**
 * Field control for formatted long text using Quill Editor.
 * Usage:
 * 
 * ``` html
 * <rich-text content.bind="content" settings.bind="myLongTextFieldSetting"></rich-text>
 * ```
 */
 
@customElement('rich-text')
export class RichText extends FieldBaseControl<string, FieldSettings.LongTextFieldSetting> {
 
    quillElementRef: HTMLDivElement;
    containerRef: HTMLDivElement;
    quill: QuillType;
 
    isSelected: boolean = false;
    textValue: string;
    get rules(): any {
        const parentRules = super.rules;
        let thisRules = ValidationRules
            .ensure('value')
            .minLength(this.settings.MinLength || 0)
            .maxLength(this.settings.MaxLength || Infinity).rules || [];
 
        return [...parentRules, ...thisRules];
    }
 
    public onQuillSelectionChange(range, oldRange, source) {
        this.isSelected = range !== null;
        if (!this.isSelected) {
            let selectedNode: Node | null = window.getSelection().baseNode;
            while (selectedNode != null) {
                if (selectedNode === this.containerRef) {
                    this.isSelected = true;
                    return;
                }
                selectedNode = selectedNode.parentNode;
            }
            this.isSelected = false;
        }
    }
 
    public onQuillTextChange(delta?, oldDelta?, source?) {
        this.value = (this.quill as any).root.innerHTML;
        this.textValue = this.quill.getText().trim();
    }
 
    public attached() {
        this.quill = new (Quill as any)(this.quillElementRef, {
            modules: {
                toolbar:
                [
                    ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                    ['blockquote', 'code-block'],
 
                    [{ 'list': 'ordered' }, { 'list': 'bullet' }],
                    [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
                    [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
                    [{ 'direction': 'rtl' }],                         // text direction
 
                    [
                        { 'size': ['small', false, 'large', 'huge'] },   // custom dropdown
                        { 'header': [1, 2, 3, 4, 5, 6, false] }
                    ],
 
                    [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                    [{ 'align': [] }],
 
                    ['clean']                                         // remove formatting button
                ]
            },
            theme: 'snow'
        });
 
        this.quill
            .on('selection-change', (range, oldRange, source) => this.onQuillSelectionChange(range, oldRange, source))
            .on('text-change', (delta, oldDelta, source) => this.onQuillTextChange(delta, oldDelta, source));
        (this.quill as any).root.innerHTML = this.value;
    }
}